blob: b040b83a5418fa3f3aaf84a95ee3d1dac7297d42 [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
Finn Thain350767f2019-11-16 14:36:57 +1100175static inline void set_resid_from_SCp(struct scsi_cmnd *cmd)
176{
177 int resid = cmd->SCp.this_residual;
178 struct scatterlist *s = cmd->SCp.buffer;
179
180 if (s)
181 while (!sg_is_last(s)) {
182 s = sg_next(s);
183 resid += s->length;
184 }
185 scsi_set_resid(cmd, resid);
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/**
Finn Thainb32ade12016-01-03 16:05:41 +1100189 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thaind5d37a02016-10-10 00:46:53 -0400190 * @hostdata: host private data
Finn Thainb32ade12016-01-03 16:05:41 +1100191 * @reg1: 5380 register to poll
192 * @bit1: Bitmask to check
193 * @val1: Expected value
194 * @reg2: Second 5380 register to poll
195 * @bit2: Second bitmask to check
196 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100197 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
Finn Thain2f854b82016-01-03 16:05:22 +1100199 * Polls the chip in a reasonably efficient manner waiting for an
200 * event to occur. After a short quick poll we begin to yield the CPU
201 * (if possible). In irq contexts the time-out is arbitrarily limited.
202 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
Finn Thainb32ade12016-01-03 16:05:41 +1100204 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Finn Thaind5d37a02016-10-10 00:46:53 -0400207static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
Finn Thain61e1ce52016-10-10 00:46:53 -0400208 unsigned int reg1, u8 bit1, u8 val1,
209 unsigned int reg2, u8 bit2, u8 val2,
210 unsigned long wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100211{
Finn Thaind4408dd2016-10-10 00:46:52 -0400212 unsigned long n = hostdata->poll_loops;
Finn Thain2f854b82016-01-03 16:05:22 +1100213 unsigned long deadline = jiffies + wait;
Finn Thain2f854b82016-01-03 16:05:22 +1100214
Finn Thain2f854b82016-01-03 16:05:22 +1100215 do {
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;
220 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100221 } while (n--);
222
223 if (irqs_disabled() || in_interrupt())
224 return -ETIMEDOUT;
225
226 /* Repeatedly sleep for 1 ms until deadline */
227 while (time_is_after_jiffies(deadline)) {
228 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100229 if ((NCR5380_read(reg1) & bit1) == val1)
230 return 0;
231 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Finn Thain2f854b82016-01-03 16:05:22 +1100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -ETIMEDOUT;
236}
237
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100238#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static struct {
240 unsigned char mask;
241 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100242} signals[] = {
243 {SR_DBP, "PARITY"},
244 {SR_RST, "RST"},
245 {SR_BSY, "BSY"},
246 {SR_REQ, "REQ"},
247 {SR_MSG, "MSG"},
248 {SR_CD, "CD"},
249 {SR_IO, "IO"},
250 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100252},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253basrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100254 {BASR_END_DMA_TRANSFER, "END OF DMA"},
255 {BASR_DRQ, "DRQ"},
256 {BASR_PARITY_ERROR, "PARITY ERROR"},
257 {BASR_IRQ, "IRQ"},
258 {BASR_PHASE_MATCH, "PHASE MATCH"},
259 {BASR_BUSY_ERROR, "BUSY ERROR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100260 {BASR_ATN, "ATN"},
261 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100263},
264icrs[] = {
265 {ICR_ASSERT_RST, "ASSERT RST"},
Finn Thain12866b92016-03-23 21:10:25 +1100266 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
267 {ICR_ARBITRATION_LOST, "LOST ARB."},
Finn Thainaff0cf92016-01-03 16:06:09 +1100268 {ICR_ASSERT_ACK, "ASSERT ACK"},
269 {ICR_ASSERT_BSY, "ASSERT BSY"},
270 {ICR_ASSERT_SEL, "ASSERT SEL"},
271 {ICR_ASSERT_ATN, "ASSERT ATN"},
272 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100274},
275mrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100276 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
277 {MR_TARGET, "TARGET"},
278 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
279 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
280 {MR_ENABLE_EOP_INTR, "EOP INTR"},
281 {MR_MONITOR_BSY, "MONITOR BSY"},
282 {MR_DMA_MODE, "DMA MODE"},
283 {MR_ARBITRATE, "ARBITRATE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 {0, NULL}
285};
286
287/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100288 * NCR5380_print - print scsi bus signals
289 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100291 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293
294static void NCR5380_print(struct Scsi_Host *instance)
295{
Finn Thain61e1ce52016-10-10 00:46:53 -0400296 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain8cee3e12019-03-08 08:49:02 +1100297 unsigned char status, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 status = NCR5380_read(STATUS_REG);
300 mr = NCR5380_read(MODE_REG);
301 icr = NCR5380_read(INITIATOR_COMMAND_REG);
302 basr = NCR5380_read(BUS_AND_STATUS_REG);
303
Finn Thain12866b92016-03-23 21:10:25 +1100304 printk(KERN_DEBUG "SR = 0x%02x : ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (i = 0; signals[i].mask; ++i)
306 if (status & signals[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100307 printk(KERN_CONT "%s, ", signals[i].name);
308 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 for (i = 0; basrs[i].mask; ++i)
310 if (basr & basrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100311 printk(KERN_CONT "%s, ", basrs[i].name);
312 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 for (i = 0; icrs[i].mask; ++i)
314 if (icr & icrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100315 printk(KERN_CONT "%s, ", icrs[i].name);
316 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 for (i = 0; mrs[i].mask; ++i)
318 if (mr & mrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100319 printk(KERN_CONT "%s, ", mrs[i].name);
320 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Finn Thain0d2cf862016-01-03 16:06:11 +1100323static struct {
324 unsigned char value;
325 const char *name;
326} phases[] = {
327 {PHASE_DATAOUT, "DATAOUT"},
328 {PHASE_DATAIN, "DATAIN"},
329 {PHASE_CMDOUT, "CMDOUT"},
330 {PHASE_STATIN, "STATIN"},
331 {PHASE_MSGOUT, "MSGOUT"},
332 {PHASE_MSGIN, "MSGIN"},
333 {PHASE_UNKNOWN, "UNKNOWN"}
334};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Finn Thainc16df322016-01-03 16:06:08 +1100336/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100337 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100338 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100340 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
342
343static void NCR5380_print_phase(struct Scsi_Host *instance)
344{
Finn Thain61e1ce52016-10-10 00:46:53 -0400345 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned char status;
347 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 status = NCR5380_read(STATUS_REG);
350 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100351 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100353 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
354 (phases[i].value != (status & PHASE_MASK)); ++i)
355 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100356 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358}
359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
Finn Thain09028462017-01-15 18:50:57 -0500362 * NCR5380_info - report driver and host information
Finn Thain594d4ba2016-01-03 16:06:10 +1100363 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100365 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
367
Finn Thain8c325132014-11-12 16:11:58 +1100368static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Finn Thain8c325132014-11-12 16:11:58 +1100370 struct NCR5380_hostdata *hostdata = shost_priv(instance);
371
372 return hostdata->info;
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100376 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100377 * @instance: adapter to configure
378 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100380 * Initializes *instance and corresponding 5380 chip,
381 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100383 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100384 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100386 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800389static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Finn Thaine8a60142016-01-03 16:05:54 +1100391 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100392 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100393 unsigned long deadline;
Finn Thaind4408dd2016-10-10 00:46:52 -0400394 unsigned long accesses_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Finn Thainae5e33a2016-03-23 21:10:23 +1100396 instance->max_lun = 7;
397
Finn Thain0d2cf862016-01-03 16:06:11 +1100398 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100400 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
402 if (i > hostdata->id_mask)
403 hostdata->id_higher_mask |= i;
404 for (i = 0; i < 8; ++i)
405 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100406 hostdata->dma_len = 0;
407
Finn Thain11d2f632016-01-03 16:05:51 +1100408 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100410 hostdata->sensing = NULL;
411 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100412 INIT_LIST_HEAD(&hostdata->unissued);
413 INIT_LIST_HEAD(&hostdata->disconnected);
414
Finn Thain55181be2016-01-03 16:05:42 +1100415 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100416
Finn Thain8d8601a2016-01-03 16:05:37 +1100417 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100418 hostdata->work_q = alloc_workqueue("ncr5380_%d",
419 WQ_UNBOUND | WQ_MEM_RECLAIM,
420 1, instance->host_no);
421 if (!hostdata->work_q)
422 return -ENOMEM;
423
Finn Thain09028462017-01-15 18:50:57 -0500424 snprintf(hostdata->info, sizeof(hostdata->info),
425 "%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}",
426 instance->hostt->name, instance->irq, hostdata->io_port,
427 hostdata->base, instance->can_queue, instance->cmd_per_lun,
428 instance->sg_tablesize, instance->this_id,
429 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
430 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
431 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
Finn Thain8c325132014-11-12 16:11:58 +1100432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
434 NCR5380_write(MODE_REG, MR_BASE);
435 NCR5380_write(TARGET_COMMAND_REG, 0);
436 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100437
438 /* Calibrate register polling loop */
439 i = 0;
440 deadline = jiffies + 1;
441 do {
442 cpu_relax();
443 } while (time_is_after_jiffies(deadline));
444 deadline += msecs_to_jiffies(256);
445 do {
446 NCR5380_read(STATUS_REG);
447 ++i;
448 cpu_relax();
449 } while (time_is_after_jiffies(deadline));
Finn Thaind4408dd2016-10-10 00:46:52 -0400450 accesses_per_ms = i / 256;
451 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
Finn Thain2f854b82016-01-03 16:05:22 +1100452
Finn Thainb6488f92016-01-03 16:05:08 +1100453 return 0;
454}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Finn Thainb6488f92016-01-03 16:05:08 +1100456/**
457 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
458 * @instance: adapter to check
459 *
460 * If the system crashed, it may have crashed with a connected target and
461 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
462 * currently established nexus, which we know nothing about. Failing that
463 * do a bus reset.
464 *
465 * Note that a bus reset will cause the chip to assert IRQ.
466 *
467 * Returns 0 if successful, otherwise -ENXIO.
468 */
469
470static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
471{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100472 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100473 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
476 switch (pass) {
477 case 1:
478 case 3:
479 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100480 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
Finn Thaind5d37a02016-10-10 00:46:53 -0400481 NCR5380_poll_politely(hostdata,
Finn Thain636b1ec2016-01-03 16:05:10 +1100482 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100485 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 do_abort(instance);
487 break;
488 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100489 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100491 /* Wait after a reset; the SCSI standard calls for
492 * 250ms, we wait 500ms to be on the safe side.
493 * But some Toshiba CD-ROMs need ten times that.
494 */
495 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
496 msleep(2500);
497 else
498 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
500 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100501 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENXIO;
503 }
504 }
505 return 0;
506}
507
508/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100509 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100510 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100511 *
512 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
514
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800515static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Finn Thaine8a60142016-01-03 16:05:54 +1100517 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Finn Thain8d8601a2016-01-03 16:05:37 +1100519 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100520 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/**
Finn Thain677e0192016-01-03 16:05:59 +1100524 * complete_cmd - finish processing a command and return it to the SCSI ML
525 * @instance: the host instance
526 * @cmd: command to complete
527 */
528
529static void complete_cmd(struct Scsi_Host *instance,
530 struct scsi_cmnd *cmd)
531{
532 struct NCR5380_hostdata *hostdata = shost_priv(instance);
533
534 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
535
Finn Thainf27db8e2016-01-03 16:06:00 +1100536 if (hostdata->sensing == cmd) {
537 /* Autosense processing ends here */
Finn Thain07035652018-09-27 11:17:11 +1000538 if (status_byte(cmd->result) != GOOD) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100539 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
Finn Thain07035652018-09-27 11:17:11 +1000540 } else {
Finn Thainf27db8e2016-01-03 16:06:00 +1100541 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
Finn Thain07035652018-09-27 11:17:11 +1000542 set_driver_byte(cmd, DRIVER_SENSE);
543 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100544 hostdata->sensing = NULL;
545 }
546
Finn Thain677e0192016-01-03 16:05:59 +1100547 cmd->scsi_done(cmd);
548}
549
550/**
Finn Thain1bb40582016-01-03 16:05:29 +1100551 * NCR5380_queue_command - queue a command
552 * @instance: the relevant SCSI adapter
553 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
Finn Thain1bb40582016-01-03 16:05:29 +1100555 * cmd is added to the per-instance issue queue, with minor
556 * twiddling done to the host specific fields of cmd. If the
557 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559
Finn Thain1bb40582016-01-03 16:05:29 +1100560static int NCR5380_queue_command(struct Scsi_Host *instance,
561 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Finn Thain1bb40582016-01-03 16:05:29 +1100563 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100564 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100565 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567#if (NDEBUG & NDEBUG_NO_WRITE)
568 switch (cmd->cmnd[0]) {
569 case WRITE_6:
570 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100571 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100573 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
575 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100576#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 cmd->result = 0;
579
Finn Thain52d3e562016-03-23 21:10:20 +1100580 if (!NCR5380_acquire_dma_irq(instance))
581 return SCSI_MLQUEUE_HOST_BUSY;
582
Finn Thain11d2f632016-01-03 16:05:51 +1100583 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100584
Finn Thainaff0cf92016-01-03 16:06:09 +1100585 /*
586 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100588 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * sense data is only guaranteed to be valid while the condition exists.
590 */
591
Finn Thain32b26a12016-01-03 16:05:58 +1100592 if (cmd->cmnd[0] == REQUEST_SENSE)
593 list_add(&ncmd->list, &hostdata->unissued);
594 else
595 list_add_tail(&ncmd->list, &hostdata->unissued);
596
Finn Thain11d2f632016-01-03 16:05:51 +1100597 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100598
Finn Thaindbb6b352016-01-03 16:05:53 +1100599 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
600 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100603 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605}
606
Finn Thain52d3e562016-03-23 21:10:20 +1100607static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
608{
609 struct NCR5380_hostdata *hostdata = shost_priv(instance);
610
611 /* Caller does the locking needed to set & test these data atomically */
612 if (list_empty(&hostdata->disconnected) &&
613 list_empty(&hostdata->unissued) &&
614 list_empty(&hostdata->autosense) &&
615 !hostdata->connected &&
Finn Thain4ab2a782017-01-15 18:50:57 -0500616 !hostdata->selecting) {
Finn Thain52d3e562016-03-23 21:10:20 +1100617 NCR5380_release_dma_irq(instance);
Finn Thain4ab2a782017-01-15 18:50:57 -0500618 }
Finn Thain52d3e562016-03-23 21:10:20 +1100619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100622 * dequeue_next_cmd - dequeue a command for processing
623 * @instance: the scsi host instance
624 *
625 * Priority is given to commands on the autosense queue. These commands
626 * need autosense because of a CHECK CONDITION result.
627 *
628 * Returns a command pointer if a command is found for a target that is
629 * not already busy. Otherwise returns NULL.
630 */
631
632static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
633{
634 struct NCR5380_hostdata *hostdata = shost_priv(instance);
635 struct NCR5380_cmd *ncmd;
636 struct scsi_cmnd *cmd;
637
Finn Thain8d5dbec2016-02-23 10:07:09 +1100638 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100639 list_for_each_entry(ncmd, &hostdata->unissued, list) {
640 cmd = NCR5380_to_scmd(ncmd);
641 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
642 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
643
644 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
645 list_del(&ncmd->list);
646 dsprintk(NDEBUG_QUEUES, instance,
647 "dequeue: removed %p from issue queue\n", cmd);
648 return cmd;
649 }
650 }
651 } else {
652 /* Autosense processing begins here */
653 ncmd = list_first_entry(&hostdata->autosense,
654 struct NCR5380_cmd, list);
655 list_del(&ncmd->list);
656 cmd = NCR5380_to_scmd(ncmd);
657 dsprintk(NDEBUG_QUEUES, instance,
658 "dequeue: removed %p from autosense queue\n", cmd);
659 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
660 hostdata->sensing = cmd;
661 return cmd;
662 }
663 return NULL;
664}
665
666static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
667{
668 struct NCR5380_hostdata *hostdata = shost_priv(instance);
669 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
670
Finn Thain8d5dbec2016-02-23 10:07:09 +1100671 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100672 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
673 list_add(&ncmd->list, &hostdata->autosense);
674 hostdata->sensing = NULL;
675 } else
676 list_add(&ncmd->list, &hostdata->unissued);
677}
678
679/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100680 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100682 * NCR5380_main is a coroutine that runs as long as more work can
683 * be done on the NCR5380 host adapters in a system. Both
684 * NCR5380_queue_command() and NCR5380_intr() will try to start it
685 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
687
David Howellsc4028952006-11-22 14:57:56 +0000688static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
David Howellsc4028952006-11-22 14:57:56 +0000690 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100691 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100697
Finn Thain0a4e3612016-01-03 16:06:07 +1100698 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100699 while (!hostdata->connected && !hostdata->selecting) {
700 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
701
702 if (!cmd)
703 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100704
705 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100708 * Attempt to establish an I_T_L nexus here.
709 * On success, instance->hostdata->connected is set.
710 * On failure, we must add the command back to the
711 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100713 /*
714 * REQUEST SENSE commands are issued without tagged
715 * queueing, even on SCSI-II devices because the
716 * contingent allegiance condition exists for the
717 * entire unit.
718 */
Finn Thain32b26a12016-01-03 16:05:58 +1100719
Finn Thainccf6efd2016-02-23 10:07:08 +1100720 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100721 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100722 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100723 } else {
724 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
725 "main: select failed, returning %p to queue\n", cmd);
726 requeue_cmd(instance, cmd);
727 }
728 }
Finn Thaine4dec682016-03-23 21:10:12 +1100729 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100730 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100733 }
Finn Thain57f31322019-06-09 11:19:11 +1000734 if (!hostdata->connected)
735 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain0a4e3612016-01-03 16:06:07 +1100736 spin_unlock_irq(&hostdata->lock);
737 if (!done)
738 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Finn Thain8053b0e2016-03-23 21:10:19 +1100742/*
743 * NCR5380_dma_complete - finish DMA transfer
744 * @instance: the scsi host instance
745 *
746 * Called by the interrupt handler when DMA finishes or a phase
747 * mismatch occurs (which would end the DMA transfer).
748 */
749
750static void NCR5380_dma_complete(struct Scsi_Host *instance)
751{
752 struct NCR5380_hostdata *hostdata = shost_priv(instance);
753 int transferred;
754 unsigned char **data;
755 int *count;
756 int saved_data = 0, overrun = 0;
757 unsigned char p;
758
759 if (hostdata->read_overruns) {
760 p = hostdata->connected->SCp.phase;
761 if (p & SR_IO) {
762 udelay(10);
763 if ((NCR5380_read(BUS_AND_STATUS_REG) &
764 (BASR_PHASE_MATCH | BASR_ACK)) ==
765 (BASR_PHASE_MATCH | BASR_ACK)) {
766 saved_data = NCR5380_read(INPUT_DATA_REG);
767 overrun = 1;
768 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
769 }
770 }
771 }
772
Finn Thaine9db3192016-03-23 21:10:21 +1100773#ifdef CONFIG_SUN3
774 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
775 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
776 instance->host_no);
777 BUG();
778 }
779
780 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
781 (BASR_PHASE_MATCH | BASR_ACK)) {
782 pr_err("scsi%d: BASR %02x\n", instance->host_no,
783 NCR5380_read(BUS_AND_STATUS_REG));
784 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
785 instance->host_no);
786 BUG();
787 }
788#endif
789
Finn Thain8053b0e2016-03-23 21:10:19 +1100790 NCR5380_write(MODE_REG, MR_BASE);
791 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
792 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
793
Finn Thain4a98f892016-10-10 00:46:53 -0400794 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
Finn Thain8053b0e2016-03-23 21:10:19 +1100795 hostdata->dma_len = 0;
796
797 data = (unsigned char **)&hostdata->connected->SCp.ptr;
798 count = &hostdata->connected->SCp.this_residual;
799 *data += transferred;
800 *count -= transferred;
801
802 if (hostdata->read_overruns) {
803 int cnt, toPIO;
804
805 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
806 cnt = toPIO = hostdata->read_overruns;
807 if (overrun) {
808 dsprintk(NDEBUG_DMA, instance,
809 "Got an input overrun, using saved byte\n");
810 *(*data)++ = saved_data;
811 (*count)--;
812 cnt--;
813 toPIO--;
814 }
815 if (toPIO > 0) {
816 dsprintk(NDEBUG_DMA, instance,
817 "Doing %d byte PIO to 0x%p\n", cnt, *data);
818 NCR5380_transfer_pio(instance, &p, &cnt, data);
819 *count -= toPIO - cnt;
820 }
821 }
822 }
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/**
Finn Thaincd400822016-01-03 16:05:40 +1100826 * NCR5380_intr - generic NCR5380 irq handler
827 * @irq: interrupt number
828 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 *
Finn Thaincd400822016-01-03 16:05:40 +1100830 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
831 * from the disconnected queue, and restarting NCR5380_main()
832 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 *
Finn Thaincd400822016-01-03 16:05:40 +1100834 * The chip can assert IRQ in any of six different conditions. The IRQ flag
835 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
836 * Three of these six conditions are latched in the Bus and Status Register:
837 * - End of DMA (cleared by ending DMA Mode)
838 * - Parity error (cleared by reading RPIR)
839 * - Loss of BSY (cleared by reading RPIR)
840 * Two conditions have flag bits that are not latched:
841 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
842 * - Bus reset (non-maskable)
843 * The remaining condition has no flag bit at all:
844 * - Selection/reselection
845 *
846 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
847 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
848 * claimed that "the design of the [DP8490] interrupt logic ensures
849 * interrupts will not be lost (they can be on the DP5380)."
850 * The L5380/53C80 datasheet from LOGIC Devices has more details.
851 *
852 * Checking for bus reset by reading RST is futile because of interrupt
853 * latency, but a bus reset will reset chip logic. Checking for parity error
854 * is unnecessary because that interrupt is never enabled. A Loss of BSY
855 * condition will clear DMA Mode. We can tell when this occurs because the
856 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
858
Finn Thaina46865d2016-03-23 21:10:27 +1100859static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800861 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100862 struct NCR5380_hostdata *hostdata = shost_priv(instance);
863 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned char basr;
865 unsigned long flags;
866
Finn Thain11d2f632016-01-03 16:05:51 +1100867 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Finn Thaincd400822016-01-03 16:05:40 +1100869 basr = NCR5380_read(BUS_AND_STATUS_REG);
870 if (basr & BASR_IRQ) {
871 unsigned char mr = NCR5380_read(MODE_REG);
872 unsigned char sr = NCR5380_read(STATUS_REG);
873
Finn Thainb7465452016-01-03 16:06:05 +1100874 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
875 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100876
Finn Thain8053b0e2016-03-23 21:10:19 +1100877 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
878 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
879 * We ack IRQ after clearing Mode Register. Workarounds
880 * for End of DMA errata need to happen in DMA Mode.
881 */
882
883 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
884
885 if (hostdata->connected) {
886 NCR5380_dma_complete(instance);
887 queue_work(hostdata->work_q, &hostdata->main_task);
888 } else {
889 NCR5380_write(MODE_REG, MR_BASE);
890 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
891 }
892 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100893 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
894 /* Probably reselected */
895 NCR5380_write(SELECT_ENABLE_REG, 0);
896 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
897
Finn Thainb7465452016-01-03 16:06:05 +1100898 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100899
900 if (!hostdata->connected) {
901 NCR5380_reselect(instance);
902 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Finn Thaincd400822016-01-03 16:05:40 +1100904 if (!hostdata->connected)
905 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
906 } else {
907 /* Probably Bus Reset */
908 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
909
Finn Thain6b0e87a2018-09-27 11:17:11 +1000910 if (sr & SR_RST) {
911 /* Certainly Bus Reset */
912 shost_printk(KERN_WARNING, instance,
913 "bus reset interrupt\n");
914 bus_reset_cleanup(instance);
915 } else {
916 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
917 }
Finn Thaine9db3192016-03-23 21:10:21 +1100918#ifdef SUN3_SCSI_VME
919 dregs->csr |= CSR_DMA_ENABLE;
920#endif
Finn Thaincd400822016-01-03 16:05:40 +1100921 }
922 handled = 1;
923 } else {
Finn Thain9af9fec2016-10-10 00:46:53 -0400924 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100925#ifdef SUN3_SCSI_VME
926 dregs->csr |= CSR_DMA_ENABLE;
927#endif
Finn Thaincd400822016-01-03 16:05:40 +1100928 }
929
Finn Thain11d2f632016-01-03 16:05:51 +1100930 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100931
932 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Finn Thaindad82612018-09-27 11:17:11 +1000935/**
936 * NCR5380_select - attempt arbitration and selection for a given command
937 * @instance: the Scsi_Host instance
938 * @cmd: the scsi_cmnd to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
Finn Thaindad82612018-09-27 11:17:11 +1000940 * This routine establishes an I_T_L nexus for a SCSI command. This involves
941 * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Finn Thaindad82612018-09-27 11:17:11 +1000943 * Returns true if the operation should be retried.
944 * Returns false if it should not be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100946 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100947 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
948 * with registers as they should have been on entry - ie
949 * SELECT_ENABLE will be set appropriately, the NCR5380
950 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Finn Thaindad82612018-09-27 11:17:11 +1000952 * If successful : the I_T_L nexus will be established, and
953 * hostdata->connected will be set to cmd.
Finn Thain594d4ba2016-01-03 16:06:10 +1100954 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100956 * If failed (no target) : cmd->scsi_done() will be called, and the
957 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100959
Finn Thaindad82612018-09-27 11:17:11 +1000960static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Finn Thain4ab2a782017-01-15 18:50:57 -0500961 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Finn Thaine8a60142016-01-03 16:05:54 +1100963 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 unsigned char tmp[3], phase;
965 unsigned char *data;
966 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int err;
Finn Thaindad82612018-09-27 11:17:11 +1000968 bool ret = true;
Finn Thain7c8ed782018-09-27 11:17:11 +1000969 bool can_disconnect = instance->irq != NO_IRQ &&
970 cmd->cmnd[0] != REQUEST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100973 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
974 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Finn Thain707d62b2016-01-03 16:06:02 +1100976 /*
977 * Arbitration and selection phases are slow and involve dropping the
978 * lock, so we have to watch out for EH. An exception handler may
Finn Thaindad82612018-09-27 11:17:11 +1000979 * change 'selecting' to NULL. This function will then return false
Finn Thain707d62b2016-01-03 16:06:02 +1100980 * so that the caller will forget about 'cmd'. (During information
981 * transfer phases, EH may change 'connected' to NULL.)
982 */
983 hostdata->selecting = cmd;
984
Finn Thainaff0cf92016-01-03 16:06:09 +1100985 /*
986 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * data bus during SELECTION.
988 */
989
990 NCR5380_write(TARGET_COMMAND_REG, 0);
991
Finn Thainaff0cf92016-01-03 16:06:09 +1100992 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * Start arbitration.
994 */
995
996 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
997 NCR5380_write(MODE_REG, MR_ARBITRATE);
998
Finn Thain55500d92016-01-03 16:05:35 +1100999 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1000 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
1002
Finn Thain11d2f632016-01-03 16:05:51 +11001003 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001004 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
Finn Thainb32ade12016-01-03 16:05:41 +11001005 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1006 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001007 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001008 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1009 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001010 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001011 }
Finn Thainccf6efd2016-02-23 10:07:08 +11001012 if (!hostdata->selecting) {
1013 /* Command was aborted */
1014 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001015 return false;
Finn Thainccf6efd2016-02-23 10:07:08 +11001016 }
Finn Thainb32ade12016-01-03 16:05:41 +11001017 if (err < 0) {
1018 NCR5380_write(MODE_REG, MR_BASE);
1019 shost_printk(KERN_ERR, instance,
1020 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001021 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001022 }
Finn Thain11d2f632016-01-03 16:05:51 +11001023 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001024
1025 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 udelay(3);
1027
1028 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001029 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1030 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1031 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001033 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001034 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001035 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Finn Thaincf13b082016-01-03 16:05:18 +11001037
1038 /* After/during arbitration, BSY should be asserted.
1039 * IBM DPES-31080 Version S31Q works now
1040 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1041 */
1042 NCR5380_write(INITIATOR_COMMAND_REG,
1043 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Finn Thainaff0cf92016-01-03 16:06:09 +11001045 /*
1046 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * a minimum so we'll udelay ceil(1.2)
1048 */
1049
Finn Thain9c3f0e22016-01-03 16:05:11 +11001050 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1051 udelay(15);
1052 else
1053 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Finn Thain11d2f632016-01-03 16:05:51 +11001055 spin_lock_irq(&hostdata->lock);
1056
Finn Thain72064a72016-01-03 16:05:44 +11001057 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1058 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001059 goto out;
1060
1061 if (!hostdata->selecting) {
1062 NCR5380_write(MODE_REG, MR_BASE);
1063 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001064 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001065 }
Finn Thain72064a72016-01-03 16:05:44 +11001066
Finn Thainb7465452016-01-03 16:06:05 +11001067 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Finn Thainaff0cf92016-01-03 16:06:09 +11001069 /*
1070 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * the host and target ID's on the SCSI bus.
1072 */
1073
Finn Thain3d07d222016-01-03 16:06:13 +11001074 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Finn Thainaff0cf92016-01-03 16:06:09 +11001076 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * Raise ATN while SEL is true before BSY goes false from arbitration,
1078 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1079 * phase immediately after selection.
1080 */
1081
Finn Thain3d07d222016-01-03 16:06:13 +11001082 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1083 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 NCR5380_write(MODE_REG, MR_BASE);
1085
Finn Thainaff0cf92016-01-03 16:06:09 +11001086 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * Reselect interrupts must be turned off prior to the dropping of BSY,
1088 * otherwise we will trigger an interrupt.
1089 */
1090 NCR5380_write(SELECT_ENABLE_REG, 0);
1091
Finn Thain11d2f632016-01-03 16:05:51 +11001092 spin_unlock_irq(&hostdata->lock);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001095 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * the BSY signal.
1097 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001098 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001101 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1102 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Finn Thainaff0cf92016-01-03 16:06:09 +11001104 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001106 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * appropriate propagation delay has expired, and we're confusing
1108 * a BSY signal from ourselves as the target's response to SELECTION.
1109 *
1110 * A small delay (the 'C++' frontend breaks the pipeline with an
1111 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001112 * tighter 'C' code breaks and requires this) solves the problem -
1113 * the 1 us delay is arbitrary, and only used because this delay will
1114 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * work there.
1116 *
1117 * wingel suggests that this could be due to failing to wait
1118 * one deskew delay.
1119 */
1120
1121 udelay(1);
1122
Finn Thainb7465452016-01-03 16:06:05 +11001123 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Finn Thainaff0cf92016-01-03 16:06:09 +11001125 /*
1126 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * selection.
1128 */
1129
Finn Thaind5d37a02016-10-10 00:46:53 -04001130 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
Finn Thainae753a32016-01-03 16:05:23 +11001131 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001134 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1136 NCR5380_reselect(instance);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001137 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Finn Thainae753a32016-01-03 16:05:23 +11001140
1141 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001142 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001143 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain6a162832018-09-27 11:17:11 +10001144
Finn Thain707d62b2016-01-03 16:06:02 +11001145 /* Can't touch cmd if it has been reclaimed by the scsi ML */
Finn Thain6a162832018-09-27 11:17:11 +10001146 if (!hostdata->selecting)
Finn Thaindad82612018-09-27 11:17:11 +10001147 return false;
Finn Thain6a162832018-09-27 11:17:11 +10001148
1149 cmd->result = DID_BAD_TARGET << 16;
1150 complete_cmd(instance, cmd);
1151 dsprintk(NDEBUG_SELECTION, instance,
1152 "target did not respond within 250ms\n");
Finn Thaindad82612018-09-27 11:17:11 +10001153 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001154 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001155 }
1156
Finn Thainaff0cf92016-01-03 16:06:09 +11001157 /*
1158 * No less than two deskew delays after the initiator detects the
1159 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * change the DATA BUS. -wingel
1161 */
1162
1163 udelay(1);
1164
1165 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001168 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * was true but before BSY was false during selection, the information
1170 * transfer phase should be a MESSAGE OUT phase so that we can send the
1171 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
1173
1174 /* Wait for start of REQ/ACK handshake */
1175
Finn Thaind5d37a02016-10-10 00:46:53 -04001176 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001177 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001178 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001179 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1180 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain707d62b2016-01-03 16:06:02 +11001181 goto out;
1182 }
1183 if (!hostdata->selecting) {
1184 do_abort(instance);
Finn Thaindad82612018-09-27 11:17:11 +10001185 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
Finn Thainb7465452016-01-03 16:06:05 +11001188 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1189 scmd_id(cmd));
Finn Thain7c8ed782018-09-27 11:17:11 +10001190 tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 data = tmp;
1194 phase = PHASE_MSGOUT;
1195 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb15e7912017-01-15 18:50:57 -05001196 if (len) {
1197 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1198 cmd->result = DID_ERROR << 16;
1199 complete_cmd(instance, cmd);
1200 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
Finn Thaindad82612018-09-27 11:17:11 +10001201 ret = false;
Finn Thainb15e7912017-01-15 18:50:57 -05001202 goto out;
1203 }
1204
Finn Thainb7465452016-01-03 16:06:05 +11001205 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001208 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Finn Thaine9db3192016-03-23 21:10:21 +11001210#ifdef SUN3_SCSI_VME
1211 dregs->csr |= CSR_INTR;
1212#endif
1213
Boaz Harrosh28424d32007-09-10 22:37:45 +03001214 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Finn Thaindad82612018-09-27 11:17:11 +10001216 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001217
1218out:
1219 if (!hostdata->selecting)
Finn Thain96edebd2018-10-24 18:45:33 +11001220 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001221 hostdata->selecting = NULL;
Finn Thaindad82612018-09-27 11:17:11 +10001222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Finn Thainaff0cf92016-01-03 16:06:09 +11001225/*
1226 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001227 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 *
1229 * Purpose : transfers data in given phase using polled I/O
1230 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001231 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001232 * what phase is expected, *count - pointer to number of
1233 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001234 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001236 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001237 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001239 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 *
1241 * XXX Note : handling for bus free may be useful.
1242 */
1243
1244/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001245 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * IS 100% reliable, and for the actual data transfer where speed
1247 * counts, we will always do a pseudo DMA or DMA transfer.
1248 */
1249
Finn Thain0d2cf862016-01-03 16:06:11 +11001250static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1251 unsigned char *phase, int *count,
1252 unsigned char **data)
1253{
Finn Thain61e1ce52016-10-10 00:46:53 -04001254 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned char p = *phase, tmp;
1256 int c = *count;
1257 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Finn Thainaff0cf92016-01-03 16:06:09 +11001259 /*
1260 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * phase specified in the appropriate bits of the TARGET COMMAND
1262 * REGISTER match the STATUS REGISTER
1263 */
1264
Finn Thain0d2cf862016-01-03 16:06:11 +11001265 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001268 /*
1269 * Wait for assertion of REQ, after which the phase bits will be
1270 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
1272
Finn Thaind5d37a02016-10-10 00:46:53 -04001273 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Finn Thainb7465452016-01-03 16:06:05 +11001276 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001279 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001280 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1281 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
1283 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* Do actual transfer from SCSI bus to / from memory */
1286 if (!(p & SR_IO))
1287 NCR5380_write(OUTPUT_DATA_REG, *d);
1288 else
1289 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1290
1291 ++d;
1292
Finn Thainaff0cf92016-01-03 16:06:09 +11001293 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 * The SCSI standard suggests that in MSGOUT phase, the initiator
1295 * should drop ATN on the last byte of the message phase
1296 * after REQ has been asserted for the handshake but before
1297 * the initiator raises ACK.
1298 */
1299
1300 if (!(p & SR_IO)) {
1301 if (!((p & SR_MSG) && c > 1)) {
1302 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1303 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001304 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1305 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001307 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1308 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001310 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1311 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 } else {
1314 NCR5380_dprint(NDEBUG_PIO, instance);
1315 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1316 }
1317
Finn Thaind5d37a02016-10-10 00:46:53 -04001318 if (NCR5380_poll_politely(hostdata,
Finn Thaina2edc4a2016-01-03 16:05:27 +11001319 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1320 break;
1321
Finn Thainb7465452016-01-03 16:06:05 +11001322 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001325 * We have several special cases to consider during REQ/ACK handshaking :
1326 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001327 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001329 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001330 * message. We must exit with ACK asserted, so that the calling
1331 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
1333 * 3. ACK and ATN are clear and the target may proceed as normal.
1334 */
1335 if (!(p == PHASE_MSGIN && c == 1)) {
1336 if (p == PHASE_MSGOUT && c > 1)
1337 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1338 else
1339 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1340 }
1341 } while (--c);
1342
Finn Thainb7465452016-01-03 16:06:05 +11001343 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 *count = c;
1346 *data = d;
1347 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001348 /* The phase read from the bus is valid if either REQ is (already)
1349 * asserted or if ACK hasn't been released yet. The latter applies if
1350 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1351 */
1352 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *phase = tmp & PHASE_MASK;
1354 else
1355 *phase = PHASE_UNKNOWN;
1356
1357 if (!c || (*phase == p))
1358 return 0;
1359 else
1360 return -1;
1361}
1362
1363/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001364 * do_reset - issue a reset command
1365 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001367 * Issue a reset sequence to the NCR5380 and try and get the bus
1368 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001370 * This clears the reset interrupt flag because there may be no handler for
1371 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1372 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001374
Finn Thain54d8fe42016-01-03 16:05:06 +11001375static void do_reset(struct Scsi_Host *instance)
1376{
Finn Thain61e1ce52016-10-10 00:46:53 -04001377 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001378 unsigned long flags;
1379
1380 local_irq_save(flags);
1381 NCR5380_write(TARGET_COMMAND_REG,
1382 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001384 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001386 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1387 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Finn Thain80d3eb62016-01-03 16:05:34 +11001390/**
1391 * do_abort - abort the currently established nexus by going to
1392 * MESSAGE OUT phase and sending an ABORT message.
1393 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001395 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
1397
Finn Thain54d8fe42016-01-03 16:05:06 +11001398static int do_abort(struct Scsi_Host *instance)
1399{
Finn Thain61e1ce52016-10-10 00:46:53 -04001400 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 unsigned char *msgptr, phase, tmp;
1402 int len;
1403 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Request message out phase */
1406 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1407
Finn Thainaff0cf92016-01-03 16:06:09 +11001408 /*
1409 * Wait for the target to indicate a valid phase by asserting
1410 * REQ. Once this happens, we'll have either a MSGOUT phase
1411 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * We really don't care what value was on the bus or what value
1415 * the target sees, so we just handshake.
1416 */
1417
Finn Thaind5d37a02016-10-10 00:46:53 -04001418 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001419 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001420 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Finn Thainf35d3472016-01-03 16:05:33 +11001422 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1425
Finn Thainf35d3472016-01-03 16:05:33 +11001426 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001427 NCR5380_write(INITIATOR_COMMAND_REG,
1428 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thaind5d37a02016-10-10 00:46:53 -04001429 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001430 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001431 goto timeout;
1432 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 tmp = ABORT;
1436 msgptr = &tmp;
1437 len = 1;
1438 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001439 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
1442 * If we got here, and the command completed successfully,
1443 * we're about to go into bus free state.
1444 */
1445
1446 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001447
1448timeout:
1449 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1450 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Finn Thainaff0cf92016-01-03 16:06:09 +11001453/*
1454 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001455 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 *
1457 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001458 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001460 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001461 * what phase is expected, *count - pointer to number of
1462 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001465 * maximum number of bytes, 0 if all bytes or transferred or exit
1466 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001468 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 */
1470
1471
Finn Thain0d2cf862016-01-03 16:06:11 +11001472static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1473 unsigned char *phase, int *count,
1474 unsigned char **data)
1475{
1476 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001477 int c = *count;
1478 unsigned char p = *phase;
1479 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001481 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1484 *phase = tmp;
1485 return -1;
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Finn Thain8053b0e2016-03-23 21:10:19 +11001488 hostdata->connected->SCp.phase = p;
1489
1490 if (p & SR_IO) {
1491 if (hostdata->read_overruns)
1492 c -= hostdata->read_overruns;
1493 else if (hostdata->flags & FLAG_DMA_FIXUP)
1494 --c;
1495 }
1496
1497 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1498 (p & SR_IO) ? "receive" : "send", c, d);
1499
Finn Thaine9db3192016-03-23 21:10:21 +11001500#ifdef CONFIG_SUN3
1501 /* send start chain */
1502 sun3scsi_dma_start(c, *data);
1503#endif
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001506 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1507 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Finn Thain8053b0e2016-03-23 21:10:19 +11001509 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1510 /* On the Medusa, it is a must to initialize the DMA before
1511 * starting the NCR. This is also the cleaner way for the TT.
1512 */
1513 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001514 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001515 else
Finn Thain4a98f892016-10-10 00:46:53 -04001516 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Finn Thainaff0cf92016-01-03 16:06:09 +11001519 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001520 * On the PAS16 at least I/O recovery delays are not needed here.
1521 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
1523
1524 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001525 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001526 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1528 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001529 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001531 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001533 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
Finn Thaine9db3192016-03-23 21:10:21 +11001536#ifdef CONFIG_SUN3
1537#ifdef SUN3_SCSI_VME
1538 dregs->csr |= CSR_DMA_ENABLE;
1539#endif
1540 sun3_dma_active = 1;
1541#endif
1542
Finn Thain8053b0e2016-03-23 21:10:19 +11001543 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1544 /* On the Falcon, the DMA setup must be done after the last
1545 * NCR access, else the DMA setup gets trashed!
1546 */
1547 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001548 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001549 else
Finn Thain4a98f892016-10-10 00:46:53 -04001550 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001551 }
1552
1553 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1554 if (result < 0)
1555 return result;
1556
1557 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1558 if (result > 0) {
1559 hostdata->dma_len = result;
1560 return 0;
1561 }
1562
1563 /* The result is zero iff pseudo DMA send/receive was completed. */
1564 hostdata->dma_len = c;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566/*
Finn Thaine4dec682016-03-23 21:10:12 +11001567 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001568 *
1569 * For DMA sends, we want to wait until the last byte has been
1570 * transferred out over the bus before we turn off DMA mode. Alas, there
1571 * seems to be no terribly good way of doing this on a 5380 under all
1572 * conditions. For non-scatter-gather operations, we can wait until REQ
1573 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1574 * are nastier, since the device will be expecting more data than we
1575 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1576 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1577 * why this signal was added to the newer chips) but on the older 538[01]
1578 * this signal does not exist. The workaround for this lack is a watchdog;
1579 * we bail out of the wait-loop after a modest amount of wait-time if
1580 * the usual exit conditions are not met. Not a terribly clean or
1581 * correct solution :-%
1582 *
1583 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1584 * If the chip is in DMA receive mode, it will respond to a target's
1585 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1586 * ACK, even if it has _already_ been notified by the DMA controller that
1587 * the current DMA transfer has completed! If the NCR5380 is then taken
1588 * out of DMA mode, this already-acknowledged byte is lost. This is
1589 * not a problem for "one DMA transfer per READ command", because
1590 * the situation will never arise... either all of the data is DMA'ed
1591 * properly, or the target switches to MESSAGE IN phase to signal a
1592 * disconnection (either operation bringing the DMA to a clean halt).
1593 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001594 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001595 * condition before taking the NCR5380 out of DMA mode. One or two extra
1596 * bytes are transferred via PIO as necessary to fill out the original
1597 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
1599
Finn Thain8053b0e2016-03-23 21:10:19 +11001600 if (hostdata->flags & FLAG_DMA_FIXUP) {
1601 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001603 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001604 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 * the chip to latch the last byte, read it, and then disable
1606 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001607 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1609 * REQ is deasserted when ACK is asserted, and not reasserted
1610 * until ACK goes false. Since the NCR5380 won't lower ACK
1611 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001612 * the DMA port or we take the NCR5380 out of DMA mode, we
1613 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * byte.
1615 */
1616
Finn Thaind5d37a02016-10-10 00:46:53 -04001617 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001618 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001619 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001620 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Finn Thaind5d37a02016-10-10 00:46:53 -04001622 if (NCR5380_poll_politely(hostdata, STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001623 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001624 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001625 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1626 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001627 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1628 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001630 * Wait for the last byte to be sent. If REQ is being asserted for
1631 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
Finn Thaind5d37a02016-10-10 00:46:53 -04001633 if (NCR5380_poll_politely2(hostdata,
Finn Thain55181be2016-01-03 16:05:42 +11001634 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1635 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001636 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001637 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001641
1642 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001643 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646/*
1647 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1648 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001649 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001650 * directs us to. Operates on the currently connected command,
1651 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 *
1653 * Inputs : instance, instance for which we are doing commands
1654 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001655 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001656 * modified if a command disconnects, *instance->connected will
1657 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001659 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001660 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
1662
Finn Thain0d2cf862016-01-03 16:06:11 +11001663static void NCR5380_information_transfer(struct Scsi_Host *instance)
Finn Thain4ab2a782017-01-15 18:50:57 -05001664 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Finn Thain0d2cf862016-01-03 16:06:11 +11001665{
Finn Thaine8a60142016-01-03 16:05:54 +11001666 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 unsigned char msgout = NOP;
1668 int sink = 0;
1669 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 unsigned char *data;
1672 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001673 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Finn Thaine9db3192016-03-23 21:10:21 +11001675#ifdef SUN3_SCSI_VME
1676 dregs->csr |= CSR_INTR;
1677#endif
1678
Finn Thain11d2f632016-01-03 16:05:51 +11001679 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001680 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 tmp = NCR5380_read(STATUS_REG);
1683 /* We only have a valid SCSI phase when REQ is asserted */
1684 if (tmp & SR_REQ) {
1685 phase = (tmp & PHASE_MASK);
1686 if (phase != old_phase) {
1687 old_phase = phase;
1688 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1689 }
Finn Thaine9db3192016-03-23 21:10:21 +11001690#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04001691 if (phase == PHASE_CMDOUT &&
1692 sun3_dma_setup_done != cmd) {
1693 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11001694
Finn Thain0e9fdd22019-06-18 09:37:57 +08001695 advance_sg_buffer(cmd);
Finn Thaine9db3192016-03-23 21:10:21 +11001696
Finn Thain4a98f892016-10-10 00:46:53 -04001697 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1698
1699 if (count > 0) {
1700 if (rq_data_dir(cmd->request))
1701 sun3scsi_dma_send_setup(hostdata,
1702 cmd->SCp.ptr, count);
1703 else
1704 sun3scsi_dma_recv_setup(hostdata,
1705 cmd->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11001706 sun3_dma_setup_done = cmd;
1707 }
1708#ifdef SUN3_SCSI_VME
1709 dregs->csr |= CSR_INTR;
1710#endif
1711 }
1712#endif /* CONFIG_SUN3 */
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (sink && (phase != PHASE_MSGOUT)) {
1715 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1716
Finn Thain3d07d222016-01-03 16:06:13 +11001717 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1718 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001719 while (NCR5380_read(STATUS_REG) & SR_REQ)
1720 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001721 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1722 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 sink = 0;
1724 continue;
1725 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 case PHASE_DATAOUT:
1729#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001730 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 sink = 1;
1732 do_abort(instance);
1733 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001734 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001735 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001736 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return;
1738#endif
Finn Thainbf1a0c6f2016-01-03 16:05:47 +11001739 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001740 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * If there is no room left in the current buffer in the
1742 * scatter-gather list, move onto the next one.
1743 */
1744
Finn Thain0e9fdd22019-06-18 09:37:57 +08001745 advance_sg_buffer(cmd);
1746 dsprintk(NDEBUG_INFORMATION, instance,
1747 "this residual %d, sg ents %d\n",
1748 cmd->SCp.this_residual,
1749 sg_nents(cmd->SCp.buffer));
Finn Thain0d2cf862016-01-03 16:06:11 +11001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001752 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * PSEUDO-DMA for systems that are strictly PIO,
1754 * since we can let the hardware do the handshaking.
1755 *
1756 * For this to work, we need to know the transfersize
1757 * ahead of time, since the pseudo-DMA code will sit
1758 * in an unconditional loop.
1759 */
1760
Finn Thainff3d4572016-01-03 16:05:25 +11001761 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001762 if (!cmd->device->borken)
Finn Thain4a98f892016-10-10 00:46:53 -04001763 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Finn Thain438af512016-03-23 21:10:18 +11001765 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001767 if (NCR5380_transfer_dma(instance, &phase,
1768 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001770 * If the watchdog timer fires, all future
1771 * accesses to this device will use the
1772 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001774 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001775 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 cmd->device->borken = 1;
Finn Thainf9dfed12019-06-09 11:19:11 +10001777 do_reset(instance);
1778 bus_reset_cleanup(instance);
Finn Thain8053b0e2016-03-23 21:10:19 +11001779 }
Finn Thainf825e402016-03-23 21:10:15 +11001780 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001781 /* Transfer a small chunk so that the
1782 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001783 */
Finn Thain08348b12016-08-31 14:44:56 +10001784 transfersize = min(cmd->SCp.this_residual,
1785 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001786 len = transfersize;
1787 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001788 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001789 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001790 }
Finn Thaine9db3192016-03-23 21:10:21 +11001791#ifdef CONFIG_SUN3
1792 if (sun3_dma_setup_done == cmd)
1793 sun3_dma_setup_done = NULL;
1794#endif
Finn Thain16788472016-02-23 10:07:05 +11001795 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 case PHASE_MSGIN:
1797 len = 1;
1798 data = &tmp;
1799 NCR5380_transfer_pio(instance, &phase, &len, &data);
1800 cmd->SCp.Message = tmp;
1801
1802 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 case ABORT:
1804 case COMMAND_COMPLETE:
1805 /* Accept message by clearing ACK */
1806 sink = 1;
1807 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001808 dsprintk(NDEBUG_QUEUES, instance,
1809 "COMMAND COMPLETE %p target %d lun %llu\n",
1810 cmd, scmd_id(cmd), cmd->device->lun);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001813 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Finn Thainf27db8e2016-01-03 16:06:00 +11001815 cmd->result &= ~0xffff;
1816 cmd->result |= cmd->SCp.Status;
1817 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Finn Thain350767f2019-11-16 14:36:57 +11001819 set_resid_from_SCp(cmd);
1820
Finn Thainf27db8e2016-01-03 16:06:00 +11001821 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001822 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001823 else {
1824 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1825 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1826 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1827 cmd);
1828 list_add_tail(&ncmd->list,
1829 &hostdata->autosense);
1830 } else
1831 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Finn Thainaff0cf92016-01-03 16:06:09 +11001834 /*
1835 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 * arbitration can resume.
1837 */
1838 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001839
Finn Thain52d3e562016-03-23 21:10:20 +11001840 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return;
1842 case MESSAGE_REJECT:
1843 /* Accept message by clearing ACK */
1844 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1845 switch (hostdata->last_message) {
1846 case HEAD_OF_QUEUE_TAG:
1847 case ORDERED_QUEUE_TAG:
1848 case SIMPLE_QUEUE_TAG:
1849 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001850 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852 default:
1853 break;
1854 }
Finn Thain340b9612016-01-03 16:05:31 +11001855 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001856 case DISCONNECT:
1857 /* Accept message by clearing ACK */
1858 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1859 hostdata->connected = NULL;
1860 list_add(&ncmd->list, &hostdata->disconnected);
1861 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1862 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1863 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001864
Finn Thain0d2cf862016-01-03 16:06:11 +11001865 /*
1866 * Restore phase bits to 0 so an interrupted selection,
1867 * arbitration can resume.
1868 */
1869 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Finn Thaine9db3192016-03-23 21:10:21 +11001871#ifdef SUN3_SCSI_VME
1872 dregs->csr |= CSR_DMA_ENABLE;
1873#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001874 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001875 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001877 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * ignore SAVE/RESTORE pointers calls.
1879 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001880 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001882 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 * compatible.
1884 */
1885 case SAVE_POINTERS:
1886 case RESTORE_POINTERS:
1887 /* Accept message by clearing ACK */
1888 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1889 break;
1890 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001891 /*
1892 * Start the message buffer with the EXTENDED_MESSAGE
1893 * byte, since spi_print_msg() wants the whole thing.
1894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 extended_msg[0] = EXTENDED_MESSAGE;
1896 /* Accept first byte by clearing ACK */
1897 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001898
1899 spin_unlock_irq(&hostdata->lock);
1900
Finn Thainb7465452016-01-03 16:06:05 +11001901 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 len = 2;
1904 data = extended_msg + 1;
1905 phase = PHASE_MSGIN;
1906 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001907 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1908 (int)extended_msg[1],
1909 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Finn Thaine0783ed2016-01-03 16:05:45 +11001911 if (!len && extended_msg[1] > 0 &&
1912 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /* Accept third byte by clearing ACK */
1914 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1915 len = extended_msg[1] - 1;
1916 data = extended_msg + 3;
1917 phase = PHASE_MSGIN;
1918
1919 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001920 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1921 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 switch (extended_msg[2]) {
1924 case EXTENDED_SDTR:
1925 case EXTENDED_WDTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 tmp = 0;
1927 }
1928 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001929 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 tmp = 0;
1931 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001932 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1933 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 tmp = 0;
1935 }
Finn Thain11d2f632016-01-03 16:05:51 +11001936
1937 spin_lock_irq(&hostdata->lock);
1938 if (!hostdata->connected)
1939 return;
1940
Finn Thaindf135e32019-03-08 08:49:02 +11001941 /* Reject message */
1942 /* Fall through */
1943 default:
Finn Thainaff0cf92016-01-03 16:06:09 +11001944 /*
1945 * If we get something weird that we aren't expecting,
Finn Thaindf135e32019-03-08 08:49:02 +11001946 * log it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
Finn Thain39bef872017-10-26 16:51:50 +11001948 if (tmp == EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001949 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001950 "rejecting unknown extended message code %02x, length %d\n",
Finn Thain39bef872017-10-26 16:51:50 +11001951 extended_msg[2], extended_msg[1]);
1952 else if (tmp)
1953 scmd_printk(KERN_INFO, cmd,
1954 "rejecting unknown message code %02x\n",
1955 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 msgout = MESSAGE_REJECT;
1958 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1959 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001960 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 break;
1962 case PHASE_MSGOUT:
1963 len = 1;
1964 data = &msgout;
1965 hostdata->last_message = msgout;
1966 NCR5380_transfer_pio(instance, &phase, &len, &data);
1967 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001969 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001971 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11001972 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return;
1974 }
1975 msgout = NOP;
1976 break;
1977 case PHASE_CMDOUT:
1978 len = cmd->cmd_len;
1979 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001980 /*
1981 * XXX for performance reasons, on machines with a
1982 * PSEUDO-DMA architecture we should probably
1983 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 */
1985 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 break;
1987 case PHASE_STATIN:
1988 len = 1;
1989 data = &tmp;
1990 NCR5380_transfer_pio(instance, &phase, &len, &data);
1991 cmd->SCp.Status = tmp;
1992 break;
1993 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001994 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001995 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001996 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001997 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001998 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001999 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002000 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
Finn Thain11d2f632016-01-03 16:05:51 +11002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/*
2006 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2007 *
Finn Thainaff0cf92016-01-03 16:06:09 +11002008 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002009 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2010 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11002011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 */
2014
Finn Thain0d2cf862016-01-03 16:06:11 +11002015static void NCR5380_reselect(struct Scsi_Host *instance)
2016{
Finn Thaine8a60142016-01-03 16:05:54 +11002017 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002019 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002021 struct NCR5380_cmd *ncmd;
2022 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /*
2025 * Disable arbitration, etc. since the host adapter obviously
2026 * lost, and tell an interrupted NCR5380_select() to restart.
2027 */
2028
2029 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain7ef55f62018-09-27 11:17:11 +10002032 if (!target_mask || target_mask & (target_mask - 1)) {
2033 shost_printk(KERN_WARNING, instance,
2034 "reselect: bad target_mask 0x%02x\n", target_mask);
2035 return;
2036 }
Finn Thainb7465452016-01-03 16:06:05 +11002037
Finn Thainaff0cf92016-01-03 16:06:09 +11002038 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * At this point, we have detected that our SCSI ID is on the bus,
2040 * SEL is true and BSY was false for at least one bus settle delay
2041 * (400 ns).
2042 *
2043 * We must assert BSY ourselves, until the target drops the SEL
2044 * signal.
2045 */
2046
2047 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thaind5d37a02016-10-10 00:46:53 -04002048 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002049 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
Finn Thain08267212018-09-27 11:17:11 +10002050 shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002051 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2052 return;
2053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2055
2056 /*
2057 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
2059
Finn Thaind5d37a02016-10-10 00:46:53 -04002060 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002061 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
Finn Thainca694af2018-09-27 11:17:11 +10002062 if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2063 /* BUS FREE phase */
2064 return;
Finn Thain08267212018-09-27 11:17:11 +10002065 shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002066 do_abort(instance);
2067 return;
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Finn Thaine9db3192016-03-23 21:10:21 +11002070#ifdef CONFIG_SUN3
2071 /* acknowledge toggle to MSGIN */
2072 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Finn Thaine9db3192016-03-23 21:10:21 +11002074 /* peek at the byte without really hitting the bus */
2075 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2076#else
2077 {
2078 int len = 1;
2079 unsigned char *data = msg;
2080 unsigned char phase = PHASE_MSGIN;
2081
2082 NCR5380_transfer_pio(instance, &phase, &len, &data);
2083
2084 if (len) {
2085 do_abort(instance);
2086 return;
2087 }
Finn Thain72064a72016-01-03 16:05:44 +11002088 }
Finn Thaine9db3192016-03-23 21:10:21 +11002089#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002092 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002093 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002094 printk("\n");
2095 do_abort(instance);
2096 return;
2097 }
2098 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Finn Thain72064a72016-01-03 16:05:44 +11002100 /*
2101 * We need to add code for SCSI-II to track which devices have
2102 * I_T_L_Q nexuses established, and which have simple I_T_L
2103 * nexuses so we can chose to do additional data transfer.
2104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Finn Thain72064a72016-01-03 16:05:44 +11002106 /*
2107 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2108 * just reestablished, and remove it from the disconnected queue.
2109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Finn Thain32b26a12016-01-03 16:05:58 +11002111 tmp = NULL;
2112 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2113 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2114
2115 if (target_mask == (1 << scmd_id(cmd)) &&
2116 lun == (u8)cmd->device->lun) {
2117 list_del(&ncmd->list);
2118 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002122
2123 if (tmp) {
2124 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2125 "reselect: removed %p from disconnected queue\n", tmp);
2126 } else {
Finn Thain45ddc1b2018-09-27 11:17:11 +10002127 int target = ffs(target_mask) - 1;
2128
Finn Thain72064a72016-01-03 16:05:44 +11002129 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2130 target_mask, lun);
2131 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002132 * Since we have an established nexus that we can't do anything
2133 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002134 */
Finn Thain45ddc1b2018-09-27 11:17:11 +10002135 if (do_abort(instance) == 0)
2136 hostdata->busy[target] &= ~(1 << lun);
Finn Thain72064a72016-01-03 16:05:44 +11002137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Finn Thain72064a72016-01-03 16:05:44 +11002139
Finn Thaine9db3192016-03-23 21:10:21 +11002140#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04002141 if (sun3_dma_setup_done != tmp) {
2142 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11002143
Finn Thain0e9fdd22019-06-18 09:37:57 +08002144 advance_sg_buffer(tmp);
Finn Thaine9db3192016-03-23 21:10:21 +11002145
Finn Thain4a98f892016-10-10 00:46:53 -04002146 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2147
2148 if (count > 0) {
2149 if (rq_data_dir(tmp->request))
2150 sun3scsi_dma_send_setup(hostdata,
2151 tmp->SCp.ptr, count);
2152 else
2153 sun3scsi_dma_recv_setup(hostdata,
2154 tmp->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11002155 sun3_dma_setup_done = tmp;
2156 }
2157 }
2158
2159 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2160#endif /* CONFIG_SUN3 */
2161
Finn Thain72064a72016-01-03 16:05:44 +11002162 /* Accept message by clearing ACK */
2163 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2164
2165 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002166 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2167 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
Finn Thain8b00c3d2016-01-03 16:06:01 +11002170/**
2171 * list_find_cmd - test for presence of a command in a linked list
2172 * @haystack: list of commands
2173 * @needle: command to search for
2174 */
2175
2176static bool list_find_cmd(struct list_head *haystack,
2177 struct scsi_cmnd *needle)
2178{
2179 struct NCR5380_cmd *ncmd;
2180
2181 list_for_each_entry(ncmd, haystack, list)
2182 if (NCR5380_to_scmd(ncmd) == needle)
2183 return true;
2184 return false;
2185}
2186
2187/**
2188 * list_remove_cmd - remove a command from linked list
2189 * @haystack: list of commands
2190 * @needle: command to remove
2191 */
2192
2193static bool list_del_cmd(struct list_head *haystack,
2194 struct scsi_cmnd *needle)
2195{
2196 if (list_find_cmd(haystack, needle)) {
2197 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2198
2199 list_del(&ncmd->list);
2200 return true;
2201 }
2202 return false;
2203}
2204
2205/**
2206 * NCR5380_abort - scsi host eh_abort_handler() method
2207 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002209 * Try to abort a given command by removing it from queues and/or sending
2210 * the target an abort message. This may not succeed in causing a target
2211 * to abort the command. Nonetheless, the low-level driver must forget about
2212 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002214 * The normal path taken by a command is as follows. For EH we trace this
2215 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002217 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2218 * [disconnected -> connected ->]...
2219 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002221 * If cmd was not found at all then presumably it has already been completed,
2222 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002223 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002224 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002225 * We have no option but to forget the aborted command (even if it still
2226 * lacks sense data). The mid-layer may re-issue a command that is in error
2227 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2228 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002229 *
2230 * The lock protects driver data structures, but EH handlers also use it
2231 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 */
2233
Finn Thain710ddd02014-11-12 16:12:02 +11002234static int NCR5380_abort(struct scsi_cmnd *cmd)
2235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002237 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002238 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002239 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002240
Finn Thain11d2f632016-01-03 16:05:51 +11002241 spin_lock_irqsave(&hostdata->lock, flags);
2242
Finn Thain32b26a12016-01-03 16:05:58 +11002243#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002244 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002245#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002246 NCR5380_dprint(NDEBUG_ANY, instance);
2247 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Finn Thain8b00c3d2016-01-03 16:06:01 +11002249 if (list_del_cmd(&hostdata->unissued, cmd)) {
2250 dsprintk(NDEBUG_ABORT, instance,
2251 "abort: removed %p from issue queue\n", cmd);
2252 cmd->result = DID_ABORT << 16;
2253 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002254 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002255 }
2256
Finn Thain707d62b2016-01-03 16:06:02 +11002257 if (hostdata->selecting == cmd) {
2258 dsprintk(NDEBUG_ABORT, instance,
2259 "abort: cmd %p == selecting\n", cmd);
2260 hostdata->selecting = NULL;
2261 cmd->result = DID_ABORT << 16;
2262 complete_cmd(instance, cmd);
2263 goto out;
2264 }
2265
Finn Thain8b00c3d2016-01-03 16:06:01 +11002266 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2267 dsprintk(NDEBUG_ABORT, instance,
2268 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002269 /* Can't call NCR5380_select() and send ABORT because that
2270 * means releasing the lock. Need a bus reset.
2271 */
Finn Thaindc183962016-02-23 10:07:07 +11002272 set_host_byte(cmd, DID_ERROR);
2273 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002274 result = FAILED;
2275 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002276 }
2277
2278 if (hostdata->connected == cmd) {
2279 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2280 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002281 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002282 if (do_abort(instance)) {
2283 set_host_byte(cmd, DID_ERROR);
2284 complete_cmd(instance, cmd);
2285 result = FAILED;
2286 goto out;
2287 }
2288 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002289 complete_cmd(instance, cmd);
2290 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002291 }
2292
Finn Thaindc183962016-02-23 10:07:07 +11002293 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002294 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002295 "abort: removed %p from sense queue\n", cmd);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002296 complete_cmd(instance, cmd);
2297 }
2298
2299out:
2300 if (result == FAILED)
2301 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002302 else {
2303 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002304 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002305 }
Finn Thain8b00c3d2016-01-03 16:06:01 +11002306
2307 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002308 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002309 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002310
Finn Thain8b00c3d2016-01-03 16:06:01 +11002311 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314
Finn Thain6b0e87a2018-09-27 11:17:11 +10002315static void bus_reset_cleanup(struct Scsi_Host *instance)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002316{
Finn Thain11d2f632016-01-03 16:05:51 +11002317 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002318 int i;
Finn Thain62717f52016-01-03 16:06:03 +11002319 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Finn Thain62717f52016-01-03 16:06:03 +11002321 /* reset NCR registers */
2322 NCR5380_write(MODE_REG, MR_BASE);
2323 NCR5380_write(TARGET_COMMAND_REG, 0);
2324 NCR5380_write(SELECT_ENABLE_REG, 0);
2325
2326 /* After the reset, there are no more connected or disconnected commands
2327 * and no busy units; so clear the low-level status here to avoid
2328 * conflicts when the mid-level code tries to wake up the affected
2329 * commands!
2330 */
2331
Finn Thain1884c282016-02-23 10:07:04 +11002332 if (hostdata->selecting) {
2333 hostdata->selecting->result = DID_RESET << 16;
2334 complete_cmd(instance, hostdata->selecting);
2335 hostdata->selecting = NULL;
2336 }
Finn Thain62717f52016-01-03 16:06:03 +11002337
2338 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2339 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2340
2341 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002342 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002343 }
Finn Thain1884c282016-02-23 10:07:04 +11002344 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002345
2346 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2347 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2348
Finn Thain62717f52016-01-03 16:06:03 +11002349 cmd->scsi_done(cmd);
2350 }
Finn Thain1884c282016-02-23 10:07:04 +11002351 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002352
2353 if (hostdata->connected) {
2354 set_host_byte(hostdata->connected, DID_RESET);
2355 complete_cmd(instance, hostdata->connected);
2356 hostdata->connected = NULL;
2357 }
2358
Finn Thain62717f52016-01-03 16:06:03 +11002359 for (i = 0; i < 8; ++i)
2360 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002361 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002362
2363 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002364 maybe_release_dma_irq(instance);
Finn Thain6b0e87a2018-09-27 11:17:11 +10002365}
2366
2367/**
2368 * NCR5380_host_reset - reset the SCSI host
2369 * @cmd: SCSI command undergoing EH
2370 *
2371 * Returns SUCCESS
2372 */
2373
2374static int NCR5380_host_reset(struct scsi_cmnd *cmd)
2375{
2376 struct Scsi_Host *instance = cmd->device->host;
2377 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2378 unsigned long flags;
2379 struct NCR5380_cmd *ncmd;
2380
2381 spin_lock_irqsave(&hostdata->lock, flags);
2382
2383#if (NDEBUG & NDEBUG_ANY)
2384 shost_printk(KERN_INFO, instance, __func__);
2385#endif
2386 NCR5380_dprint(NDEBUG_ANY, instance);
2387 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2388
2389 list_for_each_entry(ncmd, &hostdata->unissued, list) {
2390 struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
2391
2392 scmd->result = DID_RESET << 16;
2393 scmd->scsi_done(scmd);
2394 }
2395 INIT_LIST_HEAD(&hostdata->unissued);
2396
2397 do_reset(instance);
2398 bus_reset_cleanup(instance);
2399
Finn Thain11d2f632016-01-03 16:05:51 +11002400 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return SUCCESS;
2403}