blob: 536426f25e866dab327bf8764e4b19a3422d0590 [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 Thain57f31322019-06-09 11:19:11 +1000721 if (!hostdata->connected)
722 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain0a4e3612016-01-03 16:06:07 +1100723 spin_unlock_irq(&hostdata->lock);
724 if (!done)
725 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Finn Thain8053b0e2016-03-23 21:10:19 +1100729/*
730 * NCR5380_dma_complete - finish DMA transfer
731 * @instance: the scsi host instance
732 *
733 * Called by the interrupt handler when DMA finishes or a phase
734 * mismatch occurs (which would end the DMA transfer).
735 */
736
737static void NCR5380_dma_complete(struct Scsi_Host *instance)
738{
739 struct NCR5380_hostdata *hostdata = shost_priv(instance);
740 int transferred;
741 unsigned char **data;
742 int *count;
743 int saved_data = 0, overrun = 0;
744 unsigned char p;
745
746 if (hostdata->read_overruns) {
747 p = hostdata->connected->SCp.phase;
748 if (p & SR_IO) {
749 udelay(10);
750 if ((NCR5380_read(BUS_AND_STATUS_REG) &
751 (BASR_PHASE_MATCH | BASR_ACK)) ==
752 (BASR_PHASE_MATCH | BASR_ACK)) {
753 saved_data = NCR5380_read(INPUT_DATA_REG);
754 overrun = 1;
755 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
756 }
757 }
758 }
759
Finn Thaine9db3192016-03-23 21:10:21 +1100760#ifdef CONFIG_SUN3
761 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
762 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
763 instance->host_no);
764 BUG();
765 }
766
767 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
768 (BASR_PHASE_MATCH | BASR_ACK)) {
769 pr_err("scsi%d: BASR %02x\n", instance->host_no,
770 NCR5380_read(BUS_AND_STATUS_REG));
771 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
772 instance->host_no);
773 BUG();
774 }
775#endif
776
Finn Thain8053b0e2016-03-23 21:10:19 +1100777 NCR5380_write(MODE_REG, MR_BASE);
778 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
779 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
780
Finn Thain4a98f892016-10-10 00:46:53 -0400781 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
Finn Thain8053b0e2016-03-23 21:10:19 +1100782 hostdata->dma_len = 0;
783
784 data = (unsigned char **)&hostdata->connected->SCp.ptr;
785 count = &hostdata->connected->SCp.this_residual;
786 *data += transferred;
787 *count -= transferred;
788
789 if (hostdata->read_overruns) {
790 int cnt, toPIO;
791
792 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
793 cnt = toPIO = hostdata->read_overruns;
794 if (overrun) {
795 dsprintk(NDEBUG_DMA, instance,
796 "Got an input overrun, using saved byte\n");
797 *(*data)++ = saved_data;
798 (*count)--;
799 cnt--;
800 toPIO--;
801 }
802 if (toPIO > 0) {
803 dsprintk(NDEBUG_DMA, instance,
804 "Doing %d byte PIO to 0x%p\n", cnt, *data);
805 NCR5380_transfer_pio(instance, &p, &cnt, data);
806 *count -= toPIO - cnt;
807 }
808 }
809 }
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/**
Finn Thaincd400822016-01-03 16:05:40 +1100813 * NCR5380_intr - generic NCR5380 irq handler
814 * @irq: interrupt number
815 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
Finn Thaincd400822016-01-03 16:05:40 +1100817 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
818 * from the disconnected queue, and restarting NCR5380_main()
819 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
Finn Thaincd400822016-01-03 16:05:40 +1100821 * The chip can assert IRQ in any of six different conditions. The IRQ flag
822 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
823 * Three of these six conditions are latched in the Bus and Status Register:
824 * - End of DMA (cleared by ending DMA Mode)
825 * - Parity error (cleared by reading RPIR)
826 * - Loss of BSY (cleared by reading RPIR)
827 * Two conditions have flag bits that are not latched:
828 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
829 * - Bus reset (non-maskable)
830 * The remaining condition has no flag bit at all:
831 * - Selection/reselection
832 *
833 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
834 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
835 * claimed that "the design of the [DP8490] interrupt logic ensures
836 * interrupts will not be lost (they can be on the DP5380)."
837 * The L5380/53C80 datasheet from LOGIC Devices has more details.
838 *
839 * Checking for bus reset by reading RST is futile because of interrupt
840 * latency, but a bus reset will reset chip logic. Checking for parity error
841 * is unnecessary because that interrupt is never enabled. A Loss of BSY
842 * condition will clear DMA Mode. We can tell when this occurs because the
843 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
845
Finn Thaina46865d2016-03-23 21:10:27 +1100846static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800848 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100849 struct NCR5380_hostdata *hostdata = shost_priv(instance);
850 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned char basr;
852 unsigned long flags;
853
Finn Thain11d2f632016-01-03 16:05:51 +1100854 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Finn Thaincd400822016-01-03 16:05:40 +1100856 basr = NCR5380_read(BUS_AND_STATUS_REG);
857 if (basr & BASR_IRQ) {
858 unsigned char mr = NCR5380_read(MODE_REG);
859 unsigned char sr = NCR5380_read(STATUS_REG);
860
Finn Thainb7465452016-01-03 16:06:05 +1100861 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
862 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100863
Finn Thain8053b0e2016-03-23 21:10:19 +1100864 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
865 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
866 * We ack IRQ after clearing Mode Register. Workarounds
867 * for End of DMA errata need to happen in DMA Mode.
868 */
869
870 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
871
872 if (hostdata->connected) {
873 NCR5380_dma_complete(instance);
874 queue_work(hostdata->work_q, &hostdata->main_task);
875 } else {
876 NCR5380_write(MODE_REG, MR_BASE);
877 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
878 }
879 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100880 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
881 /* Probably reselected */
882 NCR5380_write(SELECT_ENABLE_REG, 0);
883 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
884
Finn Thainb7465452016-01-03 16:06:05 +1100885 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100886
887 if (!hostdata->connected) {
888 NCR5380_reselect(instance);
889 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Finn Thaincd400822016-01-03 16:05:40 +1100891 if (!hostdata->connected)
892 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
893 } else {
894 /* Probably Bus Reset */
895 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
896
Finn Thain6b0e87a2018-09-27 11:17:11 +1000897 if (sr & SR_RST) {
898 /* Certainly Bus Reset */
899 shost_printk(KERN_WARNING, instance,
900 "bus reset interrupt\n");
901 bus_reset_cleanup(instance);
902 } else {
903 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
904 }
Finn Thaine9db3192016-03-23 21:10:21 +1100905#ifdef SUN3_SCSI_VME
906 dregs->csr |= CSR_DMA_ENABLE;
907#endif
Finn Thaincd400822016-01-03 16:05:40 +1100908 }
909 handled = 1;
910 } else {
Finn Thain9af9fec2016-10-10 00:46:53 -0400911 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100912#ifdef SUN3_SCSI_VME
913 dregs->csr |= CSR_DMA_ENABLE;
914#endif
Finn Thaincd400822016-01-03 16:05:40 +1100915 }
916
Finn Thain11d2f632016-01-03 16:05:51 +1100917 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100918
919 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Finn Thaindad82612018-09-27 11:17:11 +1000922/**
923 * NCR5380_select - attempt arbitration and selection for a given command
924 * @instance: the Scsi_Host instance
925 * @cmd: the scsi_cmnd to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
Finn Thaindad82612018-09-27 11:17:11 +1000927 * This routine establishes an I_T_L nexus for a SCSI command. This involves
928 * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Finn Thaindad82612018-09-27 11:17:11 +1000930 * Returns true if the operation should be retried.
931 * Returns false if it should not be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100933 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100934 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
935 * with registers as they should have been on entry - ie
936 * SELECT_ENABLE will be set appropriately, the NCR5380
937 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *
Finn Thaindad82612018-09-27 11:17:11 +1000939 * If successful : the I_T_L nexus will be established, and
940 * hostdata->connected will be set to cmd.
Finn Thain594d4ba2016-01-03 16:06:10 +1100941 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100943 * If failed (no target) : cmd->scsi_done() will be called, and the
944 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100946
Finn Thaindad82612018-09-27 11:17:11 +1000947static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Finn Thain4ab2a782017-01-15 18:50:57 -0500948 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Finn Thaine8a60142016-01-03 16:05:54 +1100950 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned char tmp[3], phase;
952 unsigned char *data;
953 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int err;
Finn Thaindad82612018-09-27 11:17:11 +1000955 bool ret = true;
Finn Thain7c8ed782018-09-27 11:17:11 +1000956 bool can_disconnect = instance->irq != NO_IRQ &&
957 cmd->cmnd[0] != REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100960 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
961 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Finn Thain707d62b2016-01-03 16:06:02 +1100963 /*
964 * Arbitration and selection phases are slow and involve dropping the
965 * lock, so we have to watch out for EH. An exception handler may
Finn Thaindad82612018-09-27 11:17:11 +1000966 * change 'selecting' to NULL. This function will then return false
Finn Thain707d62b2016-01-03 16:06:02 +1100967 * so that the caller will forget about 'cmd'. (During information
968 * transfer phases, EH may change 'connected' to NULL.)
969 */
970 hostdata->selecting = cmd;
971
Finn Thainaff0cf92016-01-03 16:06:09 +1100972 /*
973 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * data bus during SELECTION.
975 */
976
977 NCR5380_write(TARGET_COMMAND_REG, 0);
978
Finn Thainaff0cf92016-01-03 16:06:09 +1100979 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * Start arbitration.
981 */
982
983 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
984 NCR5380_write(MODE_REG, MR_ARBITRATE);
985
Finn Thain55500d92016-01-03 16:05:35 +1100986 /* The chip now waits for BUS FREE phase. Then after the 800 ns
987 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
989
Finn Thain11d2f632016-01-03 16:05:51 +1100990 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -0400991 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
Finn Thainb32ade12016-01-03 16:05:41 +1100992 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
993 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100994 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100995 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
996 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100997 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +1100998 }
Finn Thainccf6efd2016-02-23 10:07:08 +1100999 if (!hostdata->selecting) {
1000 /* Command was aborted */
1001 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001002 return false;
Finn Thainccf6efd2016-02-23 10:07:08 +11001003 }
Finn Thainb32ade12016-01-03 16:05:41 +11001004 if (err < 0) {
1005 NCR5380_write(MODE_REG, MR_BASE);
1006 shost_printk(KERN_ERR, instance,
1007 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001008 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001009 }
Finn Thain11d2f632016-01-03 16:05:51 +11001010 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001011
1012 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 udelay(3);
1014
1015 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001016 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1017 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1018 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001020 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001021 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Finn Thaincf13b082016-01-03 16:05:18 +11001024
1025 /* After/during arbitration, BSY should be asserted.
1026 * IBM DPES-31080 Version S31Q works now
1027 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1028 */
1029 NCR5380_write(INITIATOR_COMMAND_REG,
1030 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Finn Thainaff0cf92016-01-03 16:06:09 +11001032 /*
1033 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * a minimum so we'll udelay ceil(1.2)
1035 */
1036
Finn Thain9c3f0e22016-01-03 16:05:11 +11001037 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1038 udelay(15);
1039 else
1040 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Finn Thain11d2f632016-01-03 16:05:51 +11001042 spin_lock_irq(&hostdata->lock);
1043
Finn Thain72064a72016-01-03 16:05:44 +11001044 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1045 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001046 goto out;
1047
1048 if (!hostdata->selecting) {
1049 NCR5380_write(MODE_REG, MR_BASE);
1050 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001051 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001052 }
Finn Thain72064a72016-01-03 16:05:44 +11001053
Finn Thainb7465452016-01-03 16:06:05 +11001054 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Finn Thainaff0cf92016-01-03 16:06:09 +11001056 /*
1057 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * the host and target ID's on the SCSI bus.
1059 */
1060
Finn Thain3d07d222016-01-03 16:06:13 +11001061 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Finn Thainaff0cf92016-01-03 16:06:09 +11001063 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 * Raise ATN while SEL is true before BSY goes false from arbitration,
1065 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1066 * phase immediately after selection.
1067 */
1068
Finn Thain3d07d222016-01-03 16:06:13 +11001069 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1070 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 NCR5380_write(MODE_REG, MR_BASE);
1072
Finn Thainaff0cf92016-01-03 16:06:09 +11001073 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * Reselect interrupts must be turned off prior to the dropping of BSY,
1075 * otherwise we will trigger an interrupt.
1076 */
1077 NCR5380_write(SELECT_ENABLE_REG, 0);
1078
Finn Thain11d2f632016-01-03 16:05:51 +11001079 spin_unlock_irq(&hostdata->lock);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001082 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * the BSY signal.
1084 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001085 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001088 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1089 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Finn Thainaff0cf92016-01-03 16:06:09 +11001091 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001093 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 * appropriate propagation delay has expired, and we're confusing
1095 * a BSY signal from ourselves as the target's response to SELECTION.
1096 *
1097 * A small delay (the 'C++' frontend breaks the pipeline with an
1098 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001099 * tighter 'C' code breaks and requires this) solves the problem -
1100 * the 1 us delay is arbitrary, and only used because this delay will
1101 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 * work there.
1103 *
1104 * wingel suggests that this could be due to failing to wait
1105 * one deskew delay.
1106 */
1107
1108 udelay(1);
1109
Finn Thainb7465452016-01-03 16:06:05 +11001110 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Finn Thainaff0cf92016-01-03 16:06:09 +11001112 /*
1113 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * selection.
1115 */
1116
Finn Thaind5d37a02016-10-10 00:46:53 -04001117 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
Finn Thainae753a32016-01-03 16:05:23 +11001118 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001121 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1123 NCR5380_reselect(instance);
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 Thain6a162832018-09-27 11:17:11 +10001131
Finn Thain707d62b2016-01-03 16:06:02 +11001132 /* Can't touch cmd if it has been reclaimed by the scsi ML */
Finn Thain6a162832018-09-27 11:17:11 +10001133 if (!hostdata->selecting)
Finn Thaindad82612018-09-27 11:17:11 +10001134 return false;
Finn Thain6a162832018-09-27 11:17:11 +10001135
1136 cmd->result = DID_BAD_TARGET << 16;
1137 complete_cmd(instance, cmd);
1138 dsprintk(NDEBUG_SELECTION, instance,
1139 "target did not respond within 250ms\n");
Finn Thaindad82612018-09-27 11:17:11 +10001140 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001141 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001142 }
1143
Finn Thainaff0cf92016-01-03 16:06:09 +11001144 /*
1145 * No less than two deskew delays after the initiator detects the
1146 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 * change the DATA BUS. -wingel
1148 */
1149
1150 udelay(1);
1151
1152 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001155 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * was true but before BSY was false during selection, the information
1157 * transfer phase should be a MESSAGE OUT phase so that we can send the
1158 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
1160
1161 /* Wait for start of REQ/ACK handshake */
1162
Finn Thaind5d37a02016-10-10 00:46:53 -04001163 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001164 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001165 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001166 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1167 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain707d62b2016-01-03 16:06:02 +11001168 goto out;
1169 }
1170 if (!hostdata->selecting) {
1171 do_abort(instance);
Finn Thaindad82612018-09-27 11:17:11 +10001172 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
Finn Thainb7465452016-01-03 16:06:05 +11001175 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1176 scmd_id(cmd));
Finn Thain7c8ed782018-09-27 11:17:11 +10001177 tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 data = tmp;
1181 phase = PHASE_MSGOUT;
1182 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb15e7912017-01-15 18:50:57 -05001183 if (len) {
1184 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1185 cmd->result = DID_ERROR << 16;
1186 complete_cmd(instance, cmd);
1187 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
Finn Thaindad82612018-09-27 11:17:11 +10001188 ret = false;
Finn Thainb15e7912017-01-15 18:50:57 -05001189 goto out;
1190 }
1191
Finn Thainb7465452016-01-03 16:06:05 +11001192 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001195 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Finn Thaine9db3192016-03-23 21:10:21 +11001197#ifdef SUN3_SCSI_VME
1198 dregs->csr |= CSR_INTR;
1199#endif
1200
Boaz Harrosh28424d32007-09-10 22:37:45 +03001201 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Finn Thaindad82612018-09-27 11:17:11 +10001203 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001204
1205out:
1206 if (!hostdata->selecting)
Finn Thain96edebd2018-10-24 18:45:33 +11001207 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001208 hostdata->selecting = NULL;
Finn Thaindad82612018-09-27 11:17:11 +10001209 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Finn Thainaff0cf92016-01-03 16:06:09 +11001212/*
1213 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001214 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
1216 * Purpose : transfers data in given phase using polled I/O
1217 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001218 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001219 * what phase is expected, *count - pointer to number of
1220 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001223 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001224 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001226 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 *
1228 * XXX Note : handling for bus free may be useful.
1229 */
1230
1231/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001232 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 * IS 100% reliable, and for the actual data transfer where speed
1234 * counts, we will always do a pseudo DMA or DMA transfer.
1235 */
1236
Finn Thain0d2cf862016-01-03 16:06:11 +11001237static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1238 unsigned char *phase, int *count,
1239 unsigned char **data)
1240{
Finn Thain61e1ce52016-10-10 00:46:53 -04001241 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 unsigned char p = *phase, tmp;
1243 int c = *count;
1244 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Finn Thainaff0cf92016-01-03 16:06:09 +11001246 /*
1247 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * phase specified in the appropriate bits of the TARGET COMMAND
1249 * REGISTER match the STATUS REGISTER
1250 */
1251
Finn Thain0d2cf862016-01-03 16:06:11 +11001252 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001255 /*
1256 * Wait for assertion of REQ, after which the phase bits will be
1257 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
1259
Finn Thaind5d37a02016-10-10 00:46:53 -04001260 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Finn Thainb7465452016-01-03 16:06:05 +11001263 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001266 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001267 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1268 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
1270 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /* Do actual transfer from SCSI bus to / from memory */
1273 if (!(p & SR_IO))
1274 NCR5380_write(OUTPUT_DATA_REG, *d);
1275 else
1276 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1277
1278 ++d;
1279
Finn Thainaff0cf92016-01-03 16:06:09 +11001280 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * The SCSI standard suggests that in MSGOUT phase, the initiator
1282 * should drop ATN on the last byte of the message phase
1283 * after REQ has been asserted for the handshake but before
1284 * the initiator raises ACK.
1285 */
1286
1287 if (!(p & SR_IO)) {
1288 if (!((p & SR_MSG) && c > 1)) {
1289 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1290 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001291 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1292 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001294 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1295 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001297 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1298 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 } else {
1301 NCR5380_dprint(NDEBUG_PIO, instance);
1302 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1303 }
1304
Finn Thaind5d37a02016-10-10 00:46:53 -04001305 if (NCR5380_poll_politely(hostdata,
Finn Thaina2edc4a2016-01-03 16:05:27 +11001306 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1307 break;
1308
Finn Thainb7465452016-01-03 16:06:05 +11001309 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001312 * We have several special cases to consider during REQ/ACK handshaking :
1313 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001314 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001316 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001317 * message. We must exit with ACK asserted, so that the calling
1318 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 *
1320 * 3. ACK and ATN are clear and the target may proceed as normal.
1321 */
1322 if (!(p == PHASE_MSGIN && c == 1)) {
1323 if (p == PHASE_MSGOUT && c > 1)
1324 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1325 else
1326 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1327 }
1328 } while (--c);
1329
Finn Thainb7465452016-01-03 16:06:05 +11001330 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 *count = c;
1333 *data = d;
1334 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001335 /* The phase read from the bus is valid if either REQ is (already)
1336 * asserted or if ACK hasn't been released yet. The latter applies if
1337 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1338 */
1339 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *phase = tmp & PHASE_MASK;
1341 else
1342 *phase = PHASE_UNKNOWN;
1343
1344 if (!c || (*phase == p))
1345 return 0;
1346 else
1347 return -1;
1348}
1349
1350/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001351 * do_reset - issue a reset command
1352 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001354 * Issue a reset sequence to the NCR5380 and try and get the bus
1355 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001357 * This clears the reset interrupt flag because there may be no handler for
1358 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1359 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001361
Finn Thain54d8fe42016-01-03 16:05:06 +11001362static void do_reset(struct Scsi_Host *instance)
1363{
Finn Thain61e1ce52016-10-10 00:46:53 -04001364 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001365 unsigned long flags;
1366
1367 local_irq_save(flags);
1368 NCR5380_write(TARGET_COMMAND_REG,
1369 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001371 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001373 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1374 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Finn Thain80d3eb62016-01-03 16:05:34 +11001377/**
1378 * do_abort - abort the currently established nexus by going to
1379 * MESSAGE OUT phase and sending an ABORT message.
1380 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001382 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384
Finn Thain54d8fe42016-01-03 16:05:06 +11001385static int do_abort(struct Scsi_Host *instance)
1386{
Finn Thain61e1ce52016-10-10 00:46:53 -04001387 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unsigned char *msgptr, phase, tmp;
1389 int len;
1390 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Request message out phase */
1393 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1394
Finn Thainaff0cf92016-01-03 16:06:09 +11001395 /*
1396 * Wait for the target to indicate a valid phase by asserting
1397 * REQ. Once this happens, we'll have either a MSGOUT phase
1398 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001400 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 * We really don't care what value was on the bus or what value
1402 * the target sees, so we just handshake.
1403 */
1404
Finn Thaind5d37a02016-10-10 00:46:53 -04001405 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001406 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001407 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Finn Thainf35d3472016-01-03 16:05:33 +11001409 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1412
Finn Thainf35d3472016-01-03 16:05:33 +11001413 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001414 NCR5380_write(INITIATOR_COMMAND_REG,
1415 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thaind5d37a02016-10-10 00:46:53 -04001416 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001417 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001418 goto timeout;
1419 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 tmp = ABORT;
1423 msgptr = &tmp;
1424 len = 1;
1425 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001426 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /*
1429 * If we got here, and the command completed successfully,
1430 * we're about to go into bus free state.
1431 */
1432
1433 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001434
1435timeout:
1436 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1437 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Finn Thainaff0cf92016-01-03 16:06:09 +11001440/*
1441 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001442 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 *
1444 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001445 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001447 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001448 * what phase is expected, *count - pointer to number of
1449 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001450 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001452 * maximum number of bytes, 0 if all bytes or transferred or exit
1453 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001455 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
1457
1458
Finn Thain0d2cf862016-01-03 16:06:11 +11001459static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1460 unsigned char *phase, int *count,
1461 unsigned char **data)
1462{
1463 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001464 int c = *count;
1465 unsigned char p = *phase;
1466 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001468 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1471 *phase = tmp;
1472 return -1;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Finn Thain8053b0e2016-03-23 21:10:19 +11001475 hostdata->connected->SCp.phase = p;
1476
1477 if (p & SR_IO) {
1478 if (hostdata->read_overruns)
1479 c -= hostdata->read_overruns;
1480 else if (hostdata->flags & FLAG_DMA_FIXUP)
1481 --c;
1482 }
1483
1484 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1485 (p & SR_IO) ? "receive" : "send", c, d);
1486
Finn Thaine9db3192016-03-23 21:10:21 +11001487#ifdef CONFIG_SUN3
1488 /* send start chain */
1489 sun3scsi_dma_start(c, *data);
1490#endif
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001493 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1494 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Finn Thain8053b0e2016-03-23 21:10:19 +11001496 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1497 /* On the Medusa, it is a must to initialize the DMA before
1498 * starting the NCR. This is also the cleaner way for the TT.
1499 */
1500 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001501 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001502 else
Finn Thain4a98f892016-10-10 00:46:53 -04001503 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Finn Thainaff0cf92016-01-03 16:06:09 +11001506 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001507 * On the PAS16 at least I/O recovery delays are not needed here.
1508 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
1510
1511 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001512 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001513 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1515 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001516 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001518 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001520 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Finn Thaine9db3192016-03-23 21:10:21 +11001523#ifdef CONFIG_SUN3
1524#ifdef SUN3_SCSI_VME
1525 dregs->csr |= CSR_DMA_ENABLE;
1526#endif
1527 sun3_dma_active = 1;
1528#endif
1529
Finn Thain8053b0e2016-03-23 21:10:19 +11001530 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1531 /* On the Falcon, the DMA setup must be done after the last
1532 * NCR access, else the DMA setup gets trashed!
1533 */
1534 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001535 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001536 else
Finn Thain4a98f892016-10-10 00:46:53 -04001537 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001538 }
1539
1540 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1541 if (result < 0)
1542 return result;
1543
1544 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1545 if (result > 0) {
1546 hostdata->dma_len = result;
1547 return 0;
1548 }
1549
1550 /* The result is zero iff pseudo DMA send/receive was completed. */
1551 hostdata->dma_len = c;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/*
Finn Thaine4dec682016-03-23 21:10:12 +11001554 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001555 *
1556 * For DMA sends, we want to wait until the last byte has been
1557 * transferred out over the bus before we turn off DMA mode. Alas, there
1558 * seems to be no terribly good way of doing this on a 5380 under all
1559 * conditions. For non-scatter-gather operations, we can wait until REQ
1560 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1561 * are nastier, since the device will be expecting more data than we
1562 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1563 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1564 * why this signal was added to the newer chips) but on the older 538[01]
1565 * this signal does not exist. The workaround for this lack is a watchdog;
1566 * we bail out of the wait-loop after a modest amount of wait-time if
1567 * the usual exit conditions are not met. Not a terribly clean or
1568 * correct solution :-%
1569 *
1570 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1571 * If the chip is in DMA receive mode, it will respond to a target's
1572 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1573 * ACK, even if it has _already_ been notified by the DMA controller that
1574 * the current DMA transfer has completed! If the NCR5380 is then taken
1575 * out of DMA mode, this already-acknowledged byte is lost. This is
1576 * not a problem for "one DMA transfer per READ command", because
1577 * the situation will never arise... either all of the data is DMA'ed
1578 * properly, or the target switches to MESSAGE IN phase to signal a
1579 * disconnection (either operation bringing the DMA to a clean halt).
1580 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001581 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001582 * condition before taking the NCR5380 out of DMA mode. One or two extra
1583 * bytes are transferred via PIO as necessary to fill out the original
1584 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
1586
Finn Thain8053b0e2016-03-23 21:10:19 +11001587 if (hostdata->flags & FLAG_DMA_FIXUP) {
1588 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001590 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001591 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * the chip to latch the last byte, read it, and then disable
1593 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1596 * REQ is deasserted when ACK is asserted, and not reasserted
1597 * until ACK goes false. Since the NCR5380 won't lower ACK
1598 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001599 * the DMA port or we take the NCR5380 out of DMA mode, we
1600 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * byte.
1602 */
1603
Finn Thaind5d37a02016-10-10 00:46:53 -04001604 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001605 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001606 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001607 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Finn Thaind5d37a02016-10-10 00:46:53 -04001609 if (NCR5380_poll_politely(hostdata, STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001610 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001611 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001612 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1613 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001614 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1615 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001617 * Wait for the last byte to be sent. If REQ is being asserted for
1618 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Finn Thaind5d37a02016-10-10 00:46:53 -04001620 if (NCR5380_poll_politely2(hostdata,
Finn Thain55181be2016-01-03 16:05:42 +11001621 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1622 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001623 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001624 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001628
1629 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001630 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633/*
1634 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1635 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001636 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001637 * directs us to. Operates on the currently connected command,
1638 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 *
1640 * Inputs : instance, instance for which we are doing commands
1641 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001642 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001643 * modified if a command disconnects, *instance->connected will
1644 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001646 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001647 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
1649
Finn Thain0d2cf862016-01-03 16:06:11 +11001650static void NCR5380_information_transfer(struct Scsi_Host *instance)
Finn Thain4ab2a782017-01-15 18:50:57 -05001651 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Finn Thain0d2cf862016-01-03 16:06:11 +11001652{
Finn Thaine8a60142016-01-03 16:05:54 +11001653 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 unsigned char msgout = NOP;
1655 int sink = 0;
1656 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 unsigned char *data;
1659 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001660 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Finn Thaine9db3192016-03-23 21:10:21 +11001662#ifdef SUN3_SCSI_VME
1663 dregs->csr |= CSR_INTR;
1664#endif
1665
Finn Thain11d2f632016-01-03 16:05:51 +11001666 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001667 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 tmp = NCR5380_read(STATUS_REG);
1670 /* We only have a valid SCSI phase when REQ is asserted */
1671 if (tmp & SR_REQ) {
1672 phase = (tmp & PHASE_MASK);
1673 if (phase != old_phase) {
1674 old_phase = phase;
1675 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1676 }
Finn Thaine9db3192016-03-23 21:10:21 +11001677#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04001678 if (phase == PHASE_CMDOUT &&
1679 sun3_dma_setup_done != cmd) {
1680 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11001681
Finn Thain0e9fdd22019-06-18 09:37:57 +08001682 advance_sg_buffer(cmd);
Finn Thaine9db3192016-03-23 21:10:21 +11001683
Finn Thain4a98f892016-10-10 00:46:53 -04001684 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1685
1686 if (count > 0) {
1687 if (rq_data_dir(cmd->request))
1688 sun3scsi_dma_send_setup(hostdata,
1689 cmd->SCp.ptr, count);
1690 else
1691 sun3scsi_dma_recv_setup(hostdata,
1692 cmd->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11001693 sun3_dma_setup_done = cmd;
1694 }
1695#ifdef SUN3_SCSI_VME
1696 dregs->csr |= CSR_INTR;
1697#endif
1698 }
1699#endif /* CONFIG_SUN3 */
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (sink && (phase != PHASE_MSGOUT)) {
1702 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1703
Finn Thain3d07d222016-01-03 16:06:13 +11001704 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1705 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001706 while (NCR5380_read(STATUS_REG) & SR_REQ)
1707 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001708 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1709 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 sink = 0;
1711 continue;
1712 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 case PHASE_DATAOUT:
1716#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001717 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 sink = 1;
1719 do_abort(instance);
1720 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001721 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001722 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001723 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return;
1725#endif
Finn Thainbf1a0c6f2016-01-03 16:05:47 +11001726 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001727 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * If there is no room left in the current buffer in the
1729 * scatter-gather list, move onto the next one.
1730 */
1731
Finn Thain0e9fdd22019-06-18 09:37:57 +08001732 advance_sg_buffer(cmd);
1733 dsprintk(NDEBUG_INFORMATION, instance,
1734 "this residual %d, sg ents %d\n",
1735 cmd->SCp.this_residual,
1736 sg_nents(cmd->SCp.buffer));
Finn Thain0d2cf862016-01-03 16:06:11 +11001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001739 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * PSEUDO-DMA for systems that are strictly PIO,
1741 * since we can let the hardware do the handshaking.
1742 *
1743 * For this to work, we need to know the transfersize
1744 * ahead of time, since the pseudo-DMA code will sit
1745 * in an unconditional loop.
1746 */
1747
Finn Thainff3d4572016-01-03 16:05:25 +11001748 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001749 if (!cmd->device->borken)
Finn Thain4a98f892016-10-10 00:46:53 -04001750 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Finn Thain438af512016-03-23 21:10:18 +11001752 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001754 if (NCR5380_transfer_dma(instance, &phase,
1755 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001757 * If the watchdog timer fires, all future
1758 * accesses to this device will use the
1759 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001761 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001762 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 cmd->device->borken = 1;
Finn Thainf9dfed12019-06-09 11:19:11 +10001764 do_reset(instance);
1765 bus_reset_cleanup(instance);
Finn Thain8053b0e2016-03-23 21:10:19 +11001766 }
Finn Thainf825e402016-03-23 21:10:15 +11001767 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001768 /* Transfer a small chunk so that the
1769 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001770 */
Finn Thain08348b12016-08-31 14:44:56 +10001771 transfersize = min(cmd->SCp.this_residual,
1772 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001773 len = transfersize;
1774 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001775 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001776 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001777 }
Finn Thaine9db3192016-03-23 21:10:21 +11001778#ifdef CONFIG_SUN3
1779 if (sun3_dma_setup_done == cmd)
1780 sun3_dma_setup_done = NULL;
1781#endif
Finn Thain16788472016-02-23 10:07:05 +11001782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 case PHASE_MSGIN:
1784 len = 1;
1785 data = &tmp;
1786 NCR5380_transfer_pio(instance, &phase, &len, &data);
1787 cmd->SCp.Message = tmp;
1788
1789 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 case ABORT:
1791 case COMMAND_COMPLETE:
1792 /* Accept message by clearing ACK */
1793 sink = 1;
1794 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001795 dsprintk(NDEBUG_QUEUES, instance,
1796 "COMMAND COMPLETE %p target %d lun %llu\n",
1797 cmd, scmd_id(cmd), cmd->device->lun);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001800 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Finn Thainf27db8e2016-01-03 16:06:00 +11001802 cmd->result &= ~0xffff;
1803 cmd->result |= cmd->SCp.Status;
1804 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Finn Thainf27db8e2016-01-03 16:06:00 +11001806 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001807 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001808 else {
1809 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1810 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1811 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1812 cmd);
1813 list_add_tail(&ncmd->list,
1814 &hostdata->autosense);
1815 } else
1816 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818
Finn Thainaff0cf92016-01-03 16:06:09 +11001819 /*
1820 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * arbitration can resume.
1822 */
1823 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001824
Finn Thain52d3e562016-03-23 21:10:20 +11001825 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return;
1827 case MESSAGE_REJECT:
1828 /* Accept message by clearing ACK */
1829 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1830 switch (hostdata->last_message) {
1831 case HEAD_OF_QUEUE_TAG:
1832 case ORDERED_QUEUE_TAG:
1833 case SIMPLE_QUEUE_TAG:
1834 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001835 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 break;
1837 default:
1838 break;
1839 }
Finn Thain340b9612016-01-03 16:05:31 +11001840 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001841 case DISCONNECT:
1842 /* Accept message by clearing ACK */
1843 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1844 hostdata->connected = NULL;
1845 list_add(&ncmd->list, &hostdata->disconnected);
1846 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1847 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1848 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001849
Finn Thain0d2cf862016-01-03 16:06:11 +11001850 /*
1851 * Restore phase bits to 0 so an interrupted selection,
1852 * arbitration can resume.
1853 */
1854 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Finn Thaine9db3192016-03-23 21:10:21 +11001856#ifdef SUN3_SCSI_VME
1857 dregs->csr |= CSR_DMA_ENABLE;
1858#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001859 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001860 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001862 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * ignore SAVE/RESTORE pointers calls.
1864 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001865 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001867 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * compatible.
1869 */
1870 case SAVE_POINTERS:
1871 case RESTORE_POINTERS:
1872 /* Accept message by clearing ACK */
1873 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1874 break;
1875 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001876 /*
1877 * Start the message buffer with the EXTENDED_MESSAGE
1878 * byte, since spi_print_msg() wants the whole thing.
1879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 extended_msg[0] = EXTENDED_MESSAGE;
1881 /* Accept first byte by clearing ACK */
1882 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001883
1884 spin_unlock_irq(&hostdata->lock);
1885
Finn Thainb7465452016-01-03 16:06:05 +11001886 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 len = 2;
1889 data = extended_msg + 1;
1890 phase = PHASE_MSGIN;
1891 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001892 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1893 (int)extended_msg[1],
1894 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Finn Thaine0783ed2016-01-03 16:05:45 +11001896 if (!len && extended_msg[1] > 0 &&
1897 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* Accept third byte by clearing ACK */
1899 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1900 len = extended_msg[1] - 1;
1901 data = extended_msg + 3;
1902 phase = PHASE_MSGIN;
1903
1904 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001905 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1906 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 switch (extended_msg[2]) {
1909 case EXTENDED_SDTR:
1910 case EXTENDED_WDTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 tmp = 0;
1912 }
1913 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001914 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 tmp = 0;
1916 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001917 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1918 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 tmp = 0;
1920 }
Finn Thain11d2f632016-01-03 16:05:51 +11001921
1922 spin_lock_irq(&hostdata->lock);
1923 if (!hostdata->connected)
1924 return;
1925
Finn Thaindf135e32019-03-08 08:49:02 +11001926 /* Reject message */
1927 /* Fall through */
1928 default:
Finn Thainaff0cf92016-01-03 16:06:09 +11001929 /*
1930 * If we get something weird that we aren't expecting,
Finn Thaindf135e32019-03-08 08:49:02 +11001931 * log it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Finn Thain39bef872017-10-26 16:51:50 +11001933 if (tmp == EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001934 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001935 "rejecting unknown extended message code %02x, length %d\n",
Finn Thain39bef872017-10-26 16:51:50 +11001936 extended_msg[2], extended_msg[1]);
1937 else if (tmp)
1938 scmd_printk(KERN_INFO, cmd,
1939 "rejecting unknown message code %02x\n",
1940 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 msgout = MESSAGE_REJECT;
1943 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1944 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001945 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
1947 case PHASE_MSGOUT:
1948 len = 1;
1949 data = &msgout;
1950 hostdata->last_message = msgout;
1951 NCR5380_transfer_pio(instance, &phase, &len, &data);
1952 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001954 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001956 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11001957 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return;
1959 }
1960 msgout = NOP;
1961 break;
1962 case PHASE_CMDOUT:
1963 len = cmd->cmd_len;
1964 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001965 /*
1966 * XXX for performance reasons, on machines with a
1967 * PSEUDO-DMA architecture we should probably
1968 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
1970 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
1972 case PHASE_STATIN:
1973 len = 1;
1974 data = &tmp;
1975 NCR5380_transfer_pio(instance, &phase, &len, &data);
1976 cmd->SCp.Status = tmp;
1977 break;
1978 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001979 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001980 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001981 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001982 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001983 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001984 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001985 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Finn Thain11d2f632016-01-03 16:05:51 +11001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990/*
1991 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1992 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001993 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11001994 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1995 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11001996 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 */
1999
Finn Thain0d2cf862016-01-03 16:06:11 +11002000static void NCR5380_reselect(struct Scsi_Host *instance)
2001{
Finn Thaine8a60142016-01-03 16:05:54 +11002002 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002004 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002006 struct NCR5380_cmd *ncmd;
2007 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /*
2010 * Disable arbitration, etc. since the host adapter obviously
2011 * lost, and tell an interrupted NCR5380_select() to restart.
2012 */
2013
2014 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain7ef55f62018-09-27 11:17:11 +10002017 if (!target_mask || target_mask & (target_mask - 1)) {
2018 shost_printk(KERN_WARNING, instance,
2019 "reselect: bad target_mask 0x%02x\n", target_mask);
2020 return;
2021 }
Finn Thainb7465452016-01-03 16:06:05 +11002022
Finn Thainaff0cf92016-01-03 16:06:09 +11002023 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 * At this point, we have detected that our SCSI ID is on the bus,
2025 * SEL is true and BSY was false for at least one bus settle delay
2026 * (400 ns).
2027 *
2028 * We must assert BSY ourselves, until the target drops the SEL
2029 * signal.
2030 */
2031
2032 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thaind5d37a02016-10-10 00:46:53 -04002033 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002034 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
Finn Thain08267212018-09-27 11:17:11 +10002035 shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002036 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2037 return;
2038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2040
2041 /*
2042 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
2044
Finn Thaind5d37a02016-10-10 00:46:53 -04002045 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002046 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
Finn Thainca694af2018-09-27 11:17:11 +10002047 if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2048 /* BUS FREE phase */
2049 return;
Finn Thain08267212018-09-27 11:17:11 +10002050 shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002051 do_abort(instance);
2052 return;
2053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Finn Thaine9db3192016-03-23 21:10:21 +11002055#ifdef CONFIG_SUN3
2056 /* acknowledge toggle to MSGIN */
2057 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Finn Thaine9db3192016-03-23 21:10:21 +11002059 /* peek at the byte without really hitting the bus */
2060 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2061#else
2062 {
2063 int len = 1;
2064 unsigned char *data = msg;
2065 unsigned char phase = PHASE_MSGIN;
2066
2067 NCR5380_transfer_pio(instance, &phase, &len, &data);
2068
2069 if (len) {
2070 do_abort(instance);
2071 return;
2072 }
Finn Thain72064a72016-01-03 16:05:44 +11002073 }
Finn Thaine9db3192016-03-23 21:10:21 +11002074#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002077 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002078 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002079 printk("\n");
2080 do_abort(instance);
2081 return;
2082 }
2083 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Finn Thain72064a72016-01-03 16:05:44 +11002085 /*
2086 * We need to add code for SCSI-II to track which devices have
2087 * I_T_L_Q nexuses established, and which have simple I_T_L
2088 * nexuses so we can chose to do additional data transfer.
2089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Finn Thain72064a72016-01-03 16:05:44 +11002091 /*
2092 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2093 * just reestablished, and remove it from the disconnected queue.
2094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Finn Thain32b26a12016-01-03 16:05:58 +11002096 tmp = NULL;
2097 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2098 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2099
2100 if (target_mask == (1 << scmd_id(cmd)) &&
2101 lun == (u8)cmd->device->lun) {
2102 list_del(&ncmd->list);
2103 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002107
2108 if (tmp) {
2109 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2110 "reselect: removed %p from disconnected queue\n", tmp);
2111 } else {
Finn Thain45ddc1b2018-09-27 11:17:11 +10002112 int target = ffs(target_mask) - 1;
2113
Finn Thain72064a72016-01-03 16:05:44 +11002114 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2115 target_mask, lun);
2116 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002117 * Since we have an established nexus that we can't do anything
2118 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002119 */
Finn Thain45ddc1b2018-09-27 11:17:11 +10002120 if (do_abort(instance) == 0)
2121 hostdata->busy[target] &= ~(1 << lun);
Finn Thain72064a72016-01-03 16:05:44 +11002122 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Finn Thain72064a72016-01-03 16:05:44 +11002124
Finn Thaine9db3192016-03-23 21:10:21 +11002125#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04002126 if (sun3_dma_setup_done != tmp) {
2127 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11002128
Finn Thain0e9fdd22019-06-18 09:37:57 +08002129 advance_sg_buffer(tmp);
Finn Thaine9db3192016-03-23 21:10:21 +11002130
Finn Thain4a98f892016-10-10 00:46:53 -04002131 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2132
2133 if (count > 0) {
2134 if (rq_data_dir(tmp->request))
2135 sun3scsi_dma_send_setup(hostdata,
2136 tmp->SCp.ptr, count);
2137 else
2138 sun3scsi_dma_recv_setup(hostdata,
2139 tmp->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11002140 sun3_dma_setup_done = tmp;
2141 }
2142 }
2143
2144 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2145#endif /* CONFIG_SUN3 */
2146
Finn Thain72064a72016-01-03 16:05:44 +11002147 /* Accept message by clearing ACK */
2148 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2149
2150 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002151 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2152 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
Finn Thain8b00c3d2016-01-03 16:06:01 +11002155/**
2156 * list_find_cmd - test for presence of a command in a linked list
2157 * @haystack: list of commands
2158 * @needle: command to search for
2159 */
2160
2161static bool list_find_cmd(struct list_head *haystack,
2162 struct scsi_cmnd *needle)
2163{
2164 struct NCR5380_cmd *ncmd;
2165
2166 list_for_each_entry(ncmd, haystack, list)
2167 if (NCR5380_to_scmd(ncmd) == needle)
2168 return true;
2169 return false;
2170}
2171
2172/**
2173 * list_remove_cmd - remove a command from linked list
2174 * @haystack: list of commands
2175 * @needle: command to remove
2176 */
2177
2178static bool list_del_cmd(struct list_head *haystack,
2179 struct scsi_cmnd *needle)
2180{
2181 if (list_find_cmd(haystack, needle)) {
2182 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2183
2184 list_del(&ncmd->list);
2185 return true;
2186 }
2187 return false;
2188}
2189
2190/**
2191 * NCR5380_abort - scsi host eh_abort_handler() method
2192 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002194 * Try to abort a given command by removing it from queues and/or sending
2195 * the target an abort message. This may not succeed in causing a target
2196 * to abort the command. Nonetheless, the low-level driver must forget about
2197 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002199 * The normal path taken by a command is as follows. For EH we trace this
2200 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002202 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2203 * [disconnected -> connected ->]...
2204 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002206 * If cmd was not found at all then presumably it has already been completed,
2207 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002208 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002209 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002210 * We have no option but to forget the aborted command (even if it still
2211 * lacks sense data). The mid-layer may re-issue a command that is in error
2212 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2213 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002214 *
2215 * The lock protects driver data structures, but EH handlers also use it
2216 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 */
2218
Finn Thain710ddd02014-11-12 16:12:02 +11002219static int NCR5380_abort(struct scsi_cmnd *cmd)
2220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002222 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002223 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002224 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002225
Finn Thain11d2f632016-01-03 16:05:51 +11002226 spin_lock_irqsave(&hostdata->lock, flags);
2227
Finn Thain32b26a12016-01-03 16:05:58 +11002228#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002229 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002230#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002231 NCR5380_dprint(NDEBUG_ANY, instance);
2232 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Finn Thain8b00c3d2016-01-03 16:06:01 +11002234 if (list_del_cmd(&hostdata->unissued, cmd)) {
2235 dsprintk(NDEBUG_ABORT, instance,
2236 "abort: removed %p from issue queue\n", cmd);
2237 cmd->result = DID_ABORT << 16;
2238 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002239 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002240 }
2241
Finn Thain707d62b2016-01-03 16:06:02 +11002242 if (hostdata->selecting == cmd) {
2243 dsprintk(NDEBUG_ABORT, instance,
2244 "abort: cmd %p == selecting\n", cmd);
2245 hostdata->selecting = NULL;
2246 cmd->result = DID_ABORT << 16;
2247 complete_cmd(instance, cmd);
2248 goto out;
2249 }
2250
Finn Thain8b00c3d2016-01-03 16:06:01 +11002251 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2252 dsprintk(NDEBUG_ABORT, instance,
2253 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002254 /* Can't call NCR5380_select() and send ABORT because that
2255 * means releasing the lock. Need a bus reset.
2256 */
Finn Thaindc183962016-02-23 10:07:07 +11002257 set_host_byte(cmd, DID_ERROR);
2258 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002259 result = FAILED;
2260 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002261 }
2262
2263 if (hostdata->connected == cmd) {
2264 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2265 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002266 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002267 if (do_abort(instance)) {
2268 set_host_byte(cmd, DID_ERROR);
2269 complete_cmd(instance, cmd);
2270 result = FAILED;
2271 goto out;
2272 }
2273 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002274 complete_cmd(instance, cmd);
2275 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002276 }
2277
Finn Thaindc183962016-02-23 10:07:07 +11002278 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002279 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002280 "abort: removed %p from sense queue\n", cmd);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002281 complete_cmd(instance, cmd);
2282 }
2283
2284out:
2285 if (result == FAILED)
2286 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002287 else {
2288 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002289 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002290 }
Finn Thain8b00c3d2016-01-03 16:06:01 +11002291
2292 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002293 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002294 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002295
Finn Thain8b00c3d2016-01-03 16:06:01 +11002296 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
2299
Finn Thain6b0e87a2018-09-27 11:17:11 +10002300static void bus_reset_cleanup(struct Scsi_Host *instance)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002301{
Finn Thain11d2f632016-01-03 16:05:51 +11002302 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002303 int i;
Finn Thain62717f52016-01-03 16:06:03 +11002304 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Finn Thain62717f52016-01-03 16:06:03 +11002306 /* reset NCR registers */
2307 NCR5380_write(MODE_REG, MR_BASE);
2308 NCR5380_write(TARGET_COMMAND_REG, 0);
2309 NCR5380_write(SELECT_ENABLE_REG, 0);
2310
2311 /* After the reset, there are no more connected or disconnected commands
2312 * and no busy units; so clear the low-level status here to avoid
2313 * conflicts when the mid-level code tries to wake up the affected
2314 * commands!
2315 */
2316
Finn Thain1884c282016-02-23 10:07:04 +11002317 if (hostdata->selecting) {
2318 hostdata->selecting->result = DID_RESET << 16;
2319 complete_cmd(instance, hostdata->selecting);
2320 hostdata->selecting = NULL;
2321 }
Finn Thain62717f52016-01-03 16:06:03 +11002322
2323 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2324 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2325
2326 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002327 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002328 }
Finn Thain1884c282016-02-23 10:07:04 +11002329 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002330
2331 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2332 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2333
Finn Thain62717f52016-01-03 16:06:03 +11002334 cmd->scsi_done(cmd);
2335 }
Finn Thain1884c282016-02-23 10:07:04 +11002336 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002337
2338 if (hostdata->connected) {
2339 set_host_byte(hostdata->connected, DID_RESET);
2340 complete_cmd(instance, hostdata->connected);
2341 hostdata->connected = NULL;
2342 }
2343
Finn Thain62717f52016-01-03 16:06:03 +11002344 for (i = 0; i < 8; ++i)
2345 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002346 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002347
2348 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002349 maybe_release_dma_irq(instance);
Finn Thain6b0e87a2018-09-27 11:17:11 +10002350}
2351
2352/**
2353 * NCR5380_host_reset - reset the SCSI host
2354 * @cmd: SCSI command undergoing EH
2355 *
2356 * Returns SUCCESS
2357 */
2358
2359static int NCR5380_host_reset(struct scsi_cmnd *cmd)
2360{
2361 struct Scsi_Host *instance = cmd->device->host;
2362 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2363 unsigned long flags;
2364 struct NCR5380_cmd *ncmd;
2365
2366 spin_lock_irqsave(&hostdata->lock, flags);
2367
2368#if (NDEBUG & NDEBUG_ANY)
2369 shost_printk(KERN_INFO, instance, __func__);
2370#endif
2371 NCR5380_dprint(NDEBUG_ANY, instance);
2372 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2373
2374 list_for_each_entry(ncmd, &hostdata->unissued, list) {
2375 struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
2376
2377 scmd->result = DID_RESET << 16;
2378 scmd->scsi_done(scmd);
2379 }
2380 INIT_LIST_HEAD(&hostdata->unissued);
2381
2382 do_reset(instance);
2383 bus_reset_cleanup(instance);
2384
Finn Thain11d2f632016-01-03 16:05:51 +11002385 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return SUCCESS;
2388}