blob: d654a6cc4162f8ff7a8dcf9675886f816b09bc5b [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 Thain0b7a2232019-11-16 14:36:57 +1100132static unsigned int disconnect_mask = ~0;
133module_param(disconnect_mask, int, 0444);
134
Finn Thain54d8fe42016-01-03 16:05:06 +1100135static int do_abort(struct Scsi_Host *);
136static void do_reset(struct Scsi_Host *);
Finn Thain6b0e87a2018-09-27 11:17:11 +1000137static void bus_reset_cleanup(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Finn Thainc16df322016-01-03 16:06:08 +1100139/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100140 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100141 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100143 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
145
Finn Thain710ddd02014-11-12 16:12:02 +1100146static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Finn Thainaff0cf92016-01-03 16:06:09 +1100148 /*
149 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * various queues are valid.
151 */
152
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200153 if (scsi_bufflen(cmd)) {
154 cmd->SCp.buffer = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +0200155 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 cmd->SCp.this_residual = cmd->SCp.buffer->length;
157 } else {
158 cmd->SCp.buffer = NULL;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200159 cmd->SCp.ptr = NULL;
160 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100162
163 cmd->SCp.Status = 0;
164 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Finn Thain0e9fdd22019-06-18 09:37:57 +0800167static inline void advance_sg_buffer(struct scsi_cmnd *cmd)
168{
169 struct scatterlist *s = cmd->SCp.buffer;
170
171 if (!cmd->SCp.this_residual && s && !sg_is_last(s)) {
172 cmd->SCp.buffer = sg_next(s);
173 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
174 cmd->SCp.this_residual = cmd->SCp.buffer->length;
175 }
176}
177
Finn Thain350767f2019-11-16 14:36:57 +1100178static inline void set_resid_from_SCp(struct scsi_cmnd *cmd)
179{
180 int resid = cmd->SCp.this_residual;
181 struct scatterlist *s = cmd->SCp.buffer;
182
183 if (s)
184 while (!sg_is_last(s)) {
185 s = sg_next(s);
186 resid += s->length;
187 }
188 scsi_set_resid(cmd, resid);
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/**
Finn Thainb32ade12016-01-03 16:05:41 +1100192 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thaind5d37a02016-10-10 00:46:53 -0400193 * @hostdata: host private data
Finn Thainb32ade12016-01-03 16:05:41 +1100194 * @reg1: 5380 register to poll
195 * @bit1: Bitmask to check
196 * @val1: Expected value
197 * @reg2: Second 5380 register to poll
198 * @bit2: Second bitmask to check
199 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100200 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Finn Thain2f854b82016-01-03 16:05:22 +1100202 * Polls the chip in a reasonably efficient manner waiting for an
203 * event to occur. After a short quick poll we begin to yield the CPU
204 * (if possible). In irq contexts the time-out is arbitrarily limited.
205 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
Finn Thainb32ade12016-01-03 16:05:41 +1100207 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Finn Thaind5d37a02016-10-10 00:46:53 -0400210static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
Finn Thain61e1ce52016-10-10 00:46:53 -0400211 unsigned int reg1, u8 bit1, u8 val1,
212 unsigned int reg2, u8 bit2, u8 val2,
213 unsigned long wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100214{
Finn Thaind4408dd2016-10-10 00:46:52 -0400215 unsigned long n = hostdata->poll_loops;
Finn Thain2f854b82016-01-03 16:05:22 +1100216 unsigned long deadline = jiffies + wait;
Finn Thain2f854b82016-01-03 16:05:22 +1100217
Finn Thain2f854b82016-01-03 16:05:22 +1100218 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100219 if ((NCR5380_read(reg1) & bit1) == val1)
220 return 0;
221 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100224 } while (n--);
225
226 if (irqs_disabled() || in_interrupt())
227 return -ETIMEDOUT;
228
229 /* Repeatedly sleep for 1 ms until deadline */
230 while (time_is_after_jiffies(deadline)) {
231 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100232 if ((NCR5380_read(reg1) & bit1) == val1)
233 return 0;
234 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Finn Thain2f854b82016-01-03 16:05:22 +1100237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -ETIMEDOUT;
239}
240
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100241#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static struct {
243 unsigned char mask;
244 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100245} signals[] = {
246 {SR_DBP, "PARITY"},
247 {SR_RST, "RST"},
248 {SR_BSY, "BSY"},
249 {SR_REQ, "REQ"},
250 {SR_MSG, "MSG"},
251 {SR_CD, "CD"},
252 {SR_IO, "IO"},
253 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100255},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256basrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100257 {BASR_END_DMA_TRANSFER, "END OF DMA"},
258 {BASR_DRQ, "DRQ"},
259 {BASR_PARITY_ERROR, "PARITY ERROR"},
260 {BASR_IRQ, "IRQ"},
261 {BASR_PHASE_MATCH, "PHASE MATCH"},
262 {BASR_BUSY_ERROR, "BUSY ERROR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100263 {BASR_ATN, "ATN"},
264 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100266},
267icrs[] = {
268 {ICR_ASSERT_RST, "ASSERT RST"},
Finn Thain12866b92016-03-23 21:10:25 +1100269 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
270 {ICR_ARBITRATION_LOST, "LOST ARB."},
Finn Thainaff0cf92016-01-03 16:06:09 +1100271 {ICR_ASSERT_ACK, "ASSERT ACK"},
272 {ICR_ASSERT_BSY, "ASSERT BSY"},
273 {ICR_ASSERT_SEL, "ASSERT SEL"},
274 {ICR_ASSERT_ATN, "ASSERT ATN"},
275 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100277},
278mrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100279 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
280 {MR_TARGET, "TARGET"},
281 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
282 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
283 {MR_ENABLE_EOP_INTR, "EOP INTR"},
284 {MR_MONITOR_BSY, "MONITOR BSY"},
285 {MR_DMA_MODE, "DMA MODE"},
286 {MR_ARBITRATE, "ARBITRATE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 {0, NULL}
288};
289
290/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100291 * NCR5380_print - print scsi bus signals
292 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100294 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
296
297static void NCR5380_print(struct Scsi_Host *instance)
298{
Finn Thain61e1ce52016-10-10 00:46:53 -0400299 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain8cee3e12019-03-08 08:49:02 +1100300 unsigned char status, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 status = NCR5380_read(STATUS_REG);
303 mr = NCR5380_read(MODE_REG);
304 icr = NCR5380_read(INITIATOR_COMMAND_REG);
305 basr = NCR5380_read(BUS_AND_STATUS_REG);
306
Finn Thain12866b92016-03-23 21:10:25 +1100307 printk(KERN_DEBUG "SR = 0x%02x : ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 for (i = 0; signals[i].mask; ++i)
309 if (status & signals[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100310 printk(KERN_CONT "%s, ", signals[i].name);
311 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 for (i = 0; basrs[i].mask; ++i)
313 if (basr & basrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100314 printk(KERN_CONT "%s, ", basrs[i].name);
315 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 for (i = 0; icrs[i].mask; ++i)
317 if (icr & icrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100318 printk(KERN_CONT "%s, ", icrs[i].name);
319 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 for (i = 0; mrs[i].mask; ++i)
321 if (mr & mrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100322 printk(KERN_CONT "%s, ", mrs[i].name);
323 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Finn Thain0d2cf862016-01-03 16:06:11 +1100326static struct {
327 unsigned char value;
328 const char *name;
329} phases[] = {
330 {PHASE_DATAOUT, "DATAOUT"},
331 {PHASE_DATAIN, "DATAIN"},
332 {PHASE_CMDOUT, "CMDOUT"},
333 {PHASE_STATIN, "STATIN"},
334 {PHASE_MSGOUT, "MSGOUT"},
335 {PHASE_MSGIN, "MSGIN"},
336 {PHASE_UNKNOWN, "UNKNOWN"}
337};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Finn Thainc16df322016-01-03 16:06:08 +1100339/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100340 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100341 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100343 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
345
346static void NCR5380_print_phase(struct Scsi_Host *instance)
347{
Finn Thain61e1ce52016-10-10 00:46:53 -0400348 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned char status;
350 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 status = NCR5380_read(STATUS_REG);
353 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100354 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100356 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
357 (phases[i].value != (status & PHASE_MASK)); ++i)
358 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100359 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361}
362#endif
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/**
Finn Thain09028462017-01-15 18:50:57 -0500365 * NCR5380_info - report driver and host information
Finn Thain594d4ba2016-01-03 16:06:10 +1100366 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100368 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370
Finn Thain8c325132014-11-12 16:11:58 +1100371static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Finn Thain8c325132014-11-12 16:11:58 +1100373 struct NCR5380_hostdata *hostdata = shost_priv(instance);
374
375 return hostdata->info;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100379 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100380 * @instance: adapter to configure
381 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100383 * Initializes *instance and corresponding 5380 chip,
384 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100386 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100387 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100389 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800392static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Finn Thaine8a60142016-01-03 16:05:54 +1100394 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100395 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100396 unsigned long deadline;
Finn Thaind4408dd2016-10-10 00:46:52 -0400397 unsigned long accesses_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Finn Thainae5e33a2016-03-23 21:10:23 +1100399 instance->max_lun = 7;
400
Finn Thain0d2cf862016-01-03 16:06:11 +1100401 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100403 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
405 if (i > hostdata->id_mask)
406 hostdata->id_higher_mask |= i;
407 for (i = 0; i < 8; ++i)
408 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100409 hostdata->dma_len = 0;
410
Finn Thain11d2f632016-01-03 16:05:51 +1100411 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100413 hostdata->sensing = NULL;
414 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100415 INIT_LIST_HEAD(&hostdata->unissued);
416 INIT_LIST_HEAD(&hostdata->disconnected);
417
Finn Thain55181be2016-01-03 16:05:42 +1100418 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100419
Finn Thain8d8601a2016-01-03 16:05:37 +1100420 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100421 hostdata->work_q = alloc_workqueue("ncr5380_%d",
422 WQ_UNBOUND | WQ_MEM_RECLAIM,
423 1, instance->host_no);
424 if (!hostdata->work_q)
425 return -ENOMEM;
426
Finn Thain09028462017-01-15 18:50:57 -0500427 snprintf(hostdata->info, sizeof(hostdata->info),
428 "%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}",
429 instance->hostt->name, instance->irq, hostdata->io_port,
430 hostdata->base, instance->can_queue, instance->cmd_per_lun,
431 instance->sg_tablesize, instance->this_id,
432 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
433 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
434 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
Finn Thain8c325132014-11-12 16:11:58 +1100435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
437 NCR5380_write(MODE_REG, MR_BASE);
438 NCR5380_write(TARGET_COMMAND_REG, 0);
439 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100440
441 /* Calibrate register polling loop */
442 i = 0;
443 deadline = jiffies + 1;
444 do {
445 cpu_relax();
446 } while (time_is_after_jiffies(deadline));
447 deadline += msecs_to_jiffies(256);
448 do {
449 NCR5380_read(STATUS_REG);
450 ++i;
451 cpu_relax();
452 } while (time_is_after_jiffies(deadline));
Finn Thaind4408dd2016-10-10 00:46:52 -0400453 accesses_per_ms = i / 256;
454 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
Finn Thain2f854b82016-01-03 16:05:22 +1100455
Finn Thainb6488f92016-01-03 16:05:08 +1100456 return 0;
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Finn Thainb6488f92016-01-03 16:05:08 +1100459/**
460 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
461 * @instance: adapter to check
462 *
463 * If the system crashed, it may have crashed with a connected target and
464 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
465 * currently established nexus, which we know nothing about. Failing that
466 * do a bus reset.
467 *
468 * Note that a bus reset will cause the chip to assert IRQ.
469 *
470 * Returns 0 if successful, otherwise -ENXIO.
471 */
472
473static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
474{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100475 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100476 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
479 switch (pass) {
480 case 1:
481 case 3:
482 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100483 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
Finn Thaind5d37a02016-10-10 00:46:53 -0400484 NCR5380_poll_politely(hostdata,
Finn Thain636b1ec2016-01-03 16:05:10 +1100485 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100488 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 do_abort(instance);
490 break;
491 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100492 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100494 /* Wait after a reset; the SCSI standard calls for
495 * 250ms, we wait 500ms to be on the safe side.
496 * But some Toshiba CD-ROMs need ten times that.
497 */
498 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
499 msleep(2500);
500 else
501 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100504 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -ENXIO;
506 }
507 }
508 return 0;
509}
510
511/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100512 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100513 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100514 *
515 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800518static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Finn Thaine8a60142016-01-03 16:05:54 +1100520 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Finn Thain8d8601a2016-01-03 16:05:37 +1100522 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100523 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/**
Finn Thain677e0192016-01-03 16:05:59 +1100527 * complete_cmd - finish processing a command and return it to the SCSI ML
528 * @instance: the host instance
529 * @cmd: command to complete
530 */
531
532static void complete_cmd(struct Scsi_Host *instance,
533 struct scsi_cmnd *cmd)
534{
535 struct NCR5380_hostdata *hostdata = shost_priv(instance);
536
537 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
538
Finn Thainf27db8e2016-01-03 16:06:00 +1100539 if (hostdata->sensing == cmd) {
540 /* Autosense processing ends here */
Finn Thain07035652018-09-27 11:17:11 +1000541 if (status_byte(cmd->result) != GOOD) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100542 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
Finn Thain07035652018-09-27 11:17:11 +1000543 } else {
Finn Thainf27db8e2016-01-03 16:06:00 +1100544 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
Finn Thain07035652018-09-27 11:17:11 +1000545 set_driver_byte(cmd, DRIVER_SENSE);
546 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100547 hostdata->sensing = NULL;
548 }
549
Finn Thain677e0192016-01-03 16:05:59 +1100550 cmd->scsi_done(cmd);
551}
552
553/**
Finn Thain1bb40582016-01-03 16:05:29 +1100554 * NCR5380_queue_command - queue a command
555 * @instance: the relevant SCSI adapter
556 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Finn Thain1bb40582016-01-03 16:05:29 +1100558 * cmd is added to the per-instance issue queue, with minor
559 * twiddling done to the host specific fields of cmd. If the
560 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
562
Finn Thain1bb40582016-01-03 16:05:29 +1100563static int NCR5380_queue_command(struct Scsi_Host *instance,
564 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Finn Thain1bb40582016-01-03 16:05:29 +1100566 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100567 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100568 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570#if (NDEBUG & NDEBUG_NO_WRITE)
571 switch (cmd->cmnd[0]) {
572 case WRITE_6:
573 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100574 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100576 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100579#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 cmd->result = 0;
582
Finn Thain52d3e562016-03-23 21:10:20 +1100583 if (!NCR5380_acquire_dma_irq(instance))
584 return SCSI_MLQUEUE_HOST_BUSY;
585
Finn Thain11d2f632016-01-03 16:05:51 +1100586 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100587
Finn Thainaff0cf92016-01-03 16:06:09 +1100588 /*
589 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100591 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * sense data is only guaranteed to be valid while the condition exists.
593 */
594
Finn Thain32b26a12016-01-03 16:05:58 +1100595 if (cmd->cmnd[0] == REQUEST_SENSE)
596 list_add(&ncmd->list, &hostdata->unissued);
597 else
598 list_add_tail(&ncmd->list, &hostdata->unissued);
599
Finn Thain11d2f632016-01-03 16:05:51 +1100600 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100601
Finn Thaindbb6b352016-01-03 16:05:53 +1100602 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
603 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100606 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608}
609
Finn Thain52d3e562016-03-23 21:10:20 +1100610static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
611{
612 struct NCR5380_hostdata *hostdata = shost_priv(instance);
613
614 /* Caller does the locking needed to set & test these data atomically */
615 if (list_empty(&hostdata->disconnected) &&
616 list_empty(&hostdata->unissued) &&
617 list_empty(&hostdata->autosense) &&
618 !hostdata->connected &&
Finn Thain4ab2a782017-01-15 18:50:57 -0500619 !hostdata->selecting) {
Finn Thain52d3e562016-03-23 21:10:20 +1100620 NCR5380_release_dma_irq(instance);
Finn Thain4ab2a782017-01-15 18:50:57 -0500621 }
Finn Thain52d3e562016-03-23 21:10:20 +1100622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100625 * dequeue_next_cmd - dequeue a command for processing
626 * @instance: the scsi host instance
627 *
628 * Priority is given to commands on the autosense queue. These commands
629 * need autosense because of a CHECK CONDITION result.
630 *
631 * Returns a command pointer if a command is found for a target that is
632 * not already busy. Otherwise returns NULL.
633 */
634
635static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
636{
637 struct NCR5380_hostdata *hostdata = shost_priv(instance);
638 struct NCR5380_cmd *ncmd;
639 struct scsi_cmnd *cmd;
640
Finn Thain8d5dbec2016-02-23 10:07:09 +1100641 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100642 list_for_each_entry(ncmd, &hostdata->unissued, list) {
643 cmd = NCR5380_to_scmd(ncmd);
644 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
645 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
646
647 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
648 list_del(&ncmd->list);
649 dsprintk(NDEBUG_QUEUES, instance,
650 "dequeue: removed %p from issue queue\n", cmd);
651 return cmd;
652 }
653 }
654 } else {
655 /* Autosense processing begins here */
656 ncmd = list_first_entry(&hostdata->autosense,
657 struct NCR5380_cmd, list);
658 list_del(&ncmd->list);
659 cmd = NCR5380_to_scmd(ncmd);
660 dsprintk(NDEBUG_QUEUES, instance,
661 "dequeue: removed %p from autosense queue\n", cmd);
662 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
663 hostdata->sensing = cmd;
664 return cmd;
665 }
666 return NULL;
667}
668
669static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
670{
671 struct NCR5380_hostdata *hostdata = shost_priv(instance);
672 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
673
Finn Thain8d5dbec2016-02-23 10:07:09 +1100674 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100675 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
676 list_add(&ncmd->list, &hostdata->autosense);
677 hostdata->sensing = NULL;
678 } else
679 list_add(&ncmd->list, &hostdata->unissued);
680}
681
682/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100683 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100685 * NCR5380_main is a coroutine that runs as long as more work can
686 * be done on the NCR5380 host adapters in a system. Both
687 * NCR5380_queue_command() and NCR5380_intr() will try to start it
688 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690
David Howellsc4028952006-11-22 14:57:56 +0000691static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
David Howellsc4028952006-11-22 14:57:56 +0000693 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100694 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100700
Finn Thain0a4e3612016-01-03 16:06:07 +1100701 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100702 while (!hostdata->connected && !hostdata->selecting) {
703 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
704
705 if (!cmd)
706 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100707
708 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100711 * Attempt to establish an I_T_L nexus here.
712 * On success, instance->hostdata->connected is set.
713 * On failure, we must add the command back to the
714 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100716 /*
717 * REQUEST SENSE commands are issued without tagged
718 * queueing, even on SCSI-II devices because the
719 * contingent allegiance condition exists for the
720 * entire unit.
721 */
Finn Thain32b26a12016-01-03 16:05:58 +1100722
Finn Thainccf6efd2016-02-23 10:07:08 +1100723 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100724 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100725 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100726 } else {
727 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
728 "main: select failed, returning %p to queue\n", cmd);
729 requeue_cmd(instance, cmd);
730 }
731 }
Finn Thaine4dec682016-03-23 21:10:12 +1100732 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100733 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100736 }
Finn Thain57f31322019-06-09 11:19:11 +1000737 if (!hostdata->connected)
738 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain0a4e3612016-01-03 16:06:07 +1100739 spin_unlock_irq(&hostdata->lock);
740 if (!done)
741 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Finn Thain8053b0e2016-03-23 21:10:19 +1100745/*
746 * NCR5380_dma_complete - finish DMA transfer
747 * @instance: the scsi host instance
748 *
749 * Called by the interrupt handler when DMA finishes or a phase
750 * mismatch occurs (which would end the DMA transfer).
751 */
752
753static void NCR5380_dma_complete(struct Scsi_Host *instance)
754{
755 struct NCR5380_hostdata *hostdata = shost_priv(instance);
756 int transferred;
757 unsigned char **data;
758 int *count;
759 int saved_data = 0, overrun = 0;
760 unsigned char p;
761
762 if (hostdata->read_overruns) {
763 p = hostdata->connected->SCp.phase;
764 if (p & SR_IO) {
765 udelay(10);
766 if ((NCR5380_read(BUS_AND_STATUS_REG) &
767 (BASR_PHASE_MATCH | BASR_ACK)) ==
768 (BASR_PHASE_MATCH | BASR_ACK)) {
769 saved_data = NCR5380_read(INPUT_DATA_REG);
770 overrun = 1;
771 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
772 }
773 }
774 }
775
Finn Thaine9db3192016-03-23 21:10:21 +1100776#ifdef CONFIG_SUN3
777 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
778 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
779 instance->host_no);
780 BUG();
781 }
782
783 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
784 (BASR_PHASE_MATCH | BASR_ACK)) {
785 pr_err("scsi%d: BASR %02x\n", instance->host_no,
786 NCR5380_read(BUS_AND_STATUS_REG));
787 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
788 instance->host_no);
789 BUG();
790 }
791#endif
792
Finn Thain8053b0e2016-03-23 21:10:19 +1100793 NCR5380_write(MODE_REG, MR_BASE);
794 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
795 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
796
Finn Thain4a98f892016-10-10 00:46:53 -0400797 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
Finn Thain8053b0e2016-03-23 21:10:19 +1100798 hostdata->dma_len = 0;
799
800 data = (unsigned char **)&hostdata->connected->SCp.ptr;
801 count = &hostdata->connected->SCp.this_residual;
802 *data += transferred;
803 *count -= transferred;
804
805 if (hostdata->read_overruns) {
806 int cnt, toPIO;
807
808 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
809 cnt = toPIO = hostdata->read_overruns;
810 if (overrun) {
811 dsprintk(NDEBUG_DMA, instance,
812 "Got an input overrun, using saved byte\n");
813 *(*data)++ = saved_data;
814 (*count)--;
815 cnt--;
816 toPIO--;
817 }
818 if (toPIO > 0) {
819 dsprintk(NDEBUG_DMA, instance,
820 "Doing %d byte PIO to 0x%p\n", cnt, *data);
821 NCR5380_transfer_pio(instance, &p, &cnt, data);
822 *count -= toPIO - cnt;
823 }
824 }
825 }
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/**
Finn Thaincd400822016-01-03 16:05:40 +1100829 * NCR5380_intr - generic NCR5380 irq handler
830 * @irq: interrupt number
831 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 *
Finn Thaincd400822016-01-03 16:05:40 +1100833 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
834 * from the disconnected queue, and restarting NCR5380_main()
835 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *
Finn Thaincd400822016-01-03 16:05:40 +1100837 * The chip can assert IRQ in any of six different conditions. The IRQ flag
838 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
839 * Three of these six conditions are latched in the Bus and Status Register:
840 * - End of DMA (cleared by ending DMA Mode)
841 * - Parity error (cleared by reading RPIR)
842 * - Loss of BSY (cleared by reading RPIR)
843 * Two conditions have flag bits that are not latched:
844 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
845 * - Bus reset (non-maskable)
846 * The remaining condition has no flag bit at all:
847 * - Selection/reselection
848 *
849 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
850 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
851 * claimed that "the design of the [DP8490] interrupt logic ensures
852 * interrupts will not be lost (they can be on the DP5380)."
853 * The L5380/53C80 datasheet from LOGIC Devices has more details.
854 *
855 * Checking for bus reset by reading RST is futile because of interrupt
856 * latency, but a bus reset will reset chip logic. Checking for parity error
857 * is unnecessary because that interrupt is never enabled. A Loss of BSY
858 * condition will clear DMA Mode. We can tell when this occurs because the
859 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
861
Finn Thaina46865d2016-03-23 21:10:27 +1100862static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800864 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100865 struct NCR5380_hostdata *hostdata = shost_priv(instance);
866 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned char basr;
868 unsigned long flags;
869
Finn Thain11d2f632016-01-03 16:05:51 +1100870 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Finn Thaincd400822016-01-03 16:05:40 +1100872 basr = NCR5380_read(BUS_AND_STATUS_REG);
873 if (basr & BASR_IRQ) {
874 unsigned char mr = NCR5380_read(MODE_REG);
875 unsigned char sr = NCR5380_read(STATUS_REG);
876
Finn Thainb7465452016-01-03 16:06:05 +1100877 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
878 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100879
Finn Thain8053b0e2016-03-23 21:10:19 +1100880 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
881 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
882 * We ack IRQ after clearing Mode Register. Workarounds
883 * for End of DMA errata need to happen in DMA Mode.
884 */
885
886 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
887
888 if (hostdata->connected) {
889 NCR5380_dma_complete(instance);
890 queue_work(hostdata->work_q, &hostdata->main_task);
891 } else {
892 NCR5380_write(MODE_REG, MR_BASE);
893 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
894 }
895 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100896 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
897 /* Probably reselected */
898 NCR5380_write(SELECT_ENABLE_REG, 0);
899 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
900
Finn Thainb7465452016-01-03 16:06:05 +1100901 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100902
903 if (!hostdata->connected) {
904 NCR5380_reselect(instance);
905 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Finn Thaincd400822016-01-03 16:05:40 +1100907 if (!hostdata->connected)
908 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
909 } else {
910 /* Probably Bus Reset */
911 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
912
Finn Thain6b0e87a2018-09-27 11:17:11 +1000913 if (sr & SR_RST) {
914 /* Certainly Bus Reset */
915 shost_printk(KERN_WARNING, instance,
916 "bus reset interrupt\n");
917 bus_reset_cleanup(instance);
918 } else {
919 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
920 }
Finn Thaine9db3192016-03-23 21:10:21 +1100921#ifdef SUN3_SCSI_VME
922 dregs->csr |= CSR_DMA_ENABLE;
923#endif
Finn Thaincd400822016-01-03 16:05:40 +1100924 }
925 handled = 1;
926 } else {
Finn Thain9af9fec2016-10-10 00:46:53 -0400927 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100928#ifdef SUN3_SCSI_VME
929 dregs->csr |= CSR_DMA_ENABLE;
930#endif
Finn Thaincd400822016-01-03 16:05:40 +1100931 }
932
Finn Thain11d2f632016-01-03 16:05:51 +1100933 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100934
935 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Finn Thaindad82612018-09-27 11:17:11 +1000938/**
939 * NCR5380_select - attempt arbitration and selection for a given command
940 * @instance: the Scsi_Host instance
941 * @cmd: the scsi_cmnd to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Finn Thaindad82612018-09-27 11:17:11 +1000943 * This routine establishes an I_T_L nexus for a SCSI command. This involves
944 * ARBITRATION, SELECTION and MESSAGE OUT phases and an IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
Finn Thaindad82612018-09-27 11:17:11 +1000946 * Returns true if the operation should be retried.
947 * Returns false if it should not be retried.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100949 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100950 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
951 * with registers as they should have been on entry - ie
952 * SELECT_ENABLE will be set appropriately, the NCR5380
953 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
Finn Thaindad82612018-09-27 11:17:11 +1000955 * If successful : the I_T_L nexus will be established, and
956 * hostdata->connected will be set to cmd.
Finn Thain594d4ba2016-01-03 16:06:10 +1100957 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100959 * If failed (no target) : cmd->scsi_done() will be called, and the
960 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100962
Finn Thaindad82612018-09-27 11:17:11 +1000963static bool NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
Finn Thain4ab2a782017-01-15 18:50:57 -0500964 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Finn Thaine8a60142016-01-03 16:05:54 +1100966 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 unsigned char tmp[3], phase;
968 unsigned char *data;
969 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int err;
Finn Thaindad82612018-09-27 11:17:11 +1000971 bool ret = true;
Finn Thain7c8ed782018-09-27 11:17:11 +1000972 bool can_disconnect = instance->irq != NO_IRQ &&
Finn Thain0b7a2232019-11-16 14:36:57 +1100973 cmd->cmnd[0] != REQUEST_SENSE &&
974 (disconnect_mask & BIT(scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100977 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
978 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Finn Thain707d62b2016-01-03 16:06:02 +1100980 /*
981 * Arbitration and selection phases are slow and involve dropping the
982 * lock, so we have to watch out for EH. An exception handler may
Finn Thaindad82612018-09-27 11:17:11 +1000983 * change 'selecting' to NULL. This function will then return false
Finn Thain707d62b2016-01-03 16:06:02 +1100984 * so that the caller will forget about 'cmd'. (During information
985 * transfer phases, EH may change 'connected' to NULL.)
986 */
987 hostdata->selecting = cmd;
988
Finn Thainaff0cf92016-01-03 16:06:09 +1100989 /*
990 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * data bus during SELECTION.
992 */
993
994 NCR5380_write(TARGET_COMMAND_REG, 0);
995
Finn Thainaff0cf92016-01-03 16:06:09 +1100996 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * Start arbitration.
998 */
999
1000 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1001 NCR5380_write(MODE_REG, MR_ARBITRATE);
1002
Finn Thain55500d92016-01-03 16:05:35 +11001003 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1004 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
1006
Finn Thain11d2f632016-01-03 16:05:51 +11001007 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001008 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
Finn Thainb32ade12016-01-03 16:05:41 +11001009 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1010 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001011 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001012 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1013 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001014 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001015 }
Finn Thainccf6efd2016-02-23 10:07:08 +11001016 if (!hostdata->selecting) {
1017 /* Command was aborted */
1018 NCR5380_write(MODE_REG, MR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001019 return false;
Finn Thainccf6efd2016-02-23 10:07:08 +11001020 }
Finn Thainb32ade12016-01-03 16:05:41 +11001021 if (err < 0) {
1022 NCR5380_write(MODE_REG, MR_BASE);
1023 shost_printk(KERN_ERR, instance,
1024 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001025 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001026 }
Finn Thain11d2f632016-01-03 16:05:51 +11001027 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001028
1029 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 udelay(3);
1031
1032 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001033 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1034 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1035 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001037 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001038 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Finn Thaincf13b082016-01-03 16:05:18 +11001041
1042 /* After/during arbitration, BSY should be asserted.
1043 * IBM DPES-31080 Version S31Q works now
1044 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1045 */
1046 NCR5380_write(INITIATOR_COMMAND_REG,
1047 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Finn Thainaff0cf92016-01-03 16:06:09 +11001049 /*
1050 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * a minimum so we'll udelay ceil(1.2)
1052 */
1053
Finn Thain9c3f0e22016-01-03 16:05:11 +11001054 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1055 udelay(15);
1056 else
1057 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Finn Thain11d2f632016-01-03 16:05:51 +11001059 spin_lock_irq(&hostdata->lock);
1060
Finn Thain72064a72016-01-03 16:05:44 +11001061 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1062 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001063 goto out;
1064
1065 if (!hostdata->selecting) {
1066 NCR5380_write(MODE_REG, MR_BASE);
1067 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaindad82612018-09-27 11:17:11 +10001068 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001069 }
Finn Thain72064a72016-01-03 16:05:44 +11001070
Finn Thainb7465452016-01-03 16:06:05 +11001071 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Finn Thainaff0cf92016-01-03 16:06:09 +11001073 /*
1074 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 * the host and target ID's on the SCSI bus.
1076 */
1077
Finn Thain3d07d222016-01-03 16:06:13 +11001078 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Finn Thainaff0cf92016-01-03 16:06:09 +11001080 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * Raise ATN while SEL is true before BSY goes false from arbitration,
1082 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1083 * phase immediately after selection.
1084 */
1085
Finn Thain3d07d222016-01-03 16:06:13 +11001086 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1087 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 NCR5380_write(MODE_REG, MR_BASE);
1089
Finn Thainaff0cf92016-01-03 16:06:09 +11001090 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * Reselect interrupts must be turned off prior to the dropping of BSY,
1092 * otherwise we will trigger an interrupt.
1093 */
1094 NCR5380_write(SELECT_ENABLE_REG, 0);
1095
Finn Thain11d2f632016-01-03 16:05:51 +11001096 spin_unlock_irq(&hostdata->lock);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001099 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 * the BSY signal.
1101 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001102 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001105 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1106 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Finn Thainaff0cf92016-01-03 16:06:09 +11001108 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001110 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * appropriate propagation delay has expired, and we're confusing
1112 * a BSY signal from ourselves as the target's response to SELECTION.
1113 *
1114 * A small delay (the 'C++' frontend breaks the pipeline with an
1115 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001116 * tighter 'C' code breaks and requires this) solves the problem -
1117 * the 1 us delay is arbitrary, and only used because this delay will
1118 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * work there.
1120 *
1121 * wingel suggests that this could be due to failing to wait
1122 * one deskew delay.
1123 */
1124
1125 udelay(1);
1126
Finn Thainb7465452016-01-03 16:06:05 +11001127 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Finn Thainaff0cf92016-01-03 16:06:09 +11001129 /*
1130 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * selection.
1132 */
1133
Finn Thaind5d37a02016-10-10 00:46:53 -04001134 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
Finn Thainae753a32016-01-03 16:05:23 +11001135 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001138 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1140 NCR5380_reselect(instance);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001141 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Finn Thainae753a32016-01-03 16:05:23 +11001144
1145 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001146 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001147 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain6a162832018-09-27 11:17:11 +10001148
Finn Thain707d62b2016-01-03 16:06:02 +11001149 /* Can't touch cmd if it has been reclaimed by the scsi ML */
Finn Thain6a162832018-09-27 11:17:11 +10001150 if (!hostdata->selecting)
Finn Thaindad82612018-09-27 11:17:11 +10001151 return false;
Finn Thain6a162832018-09-27 11:17:11 +10001152
1153 cmd->result = DID_BAD_TARGET << 16;
1154 complete_cmd(instance, cmd);
1155 dsprintk(NDEBUG_SELECTION, instance,
1156 "target did not respond within 250ms\n");
Finn Thaindad82612018-09-27 11:17:11 +10001157 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001158 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001159 }
1160
Finn Thainaff0cf92016-01-03 16:06:09 +11001161 /*
1162 * No less than two deskew delays after the initiator detects the
1163 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * change the DATA BUS. -wingel
1165 */
1166
1167 udelay(1);
1168
1169 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001172 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 * was true but before BSY was false during selection, the information
1174 * transfer phase should be a MESSAGE OUT phase so that we can send the
1175 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
1177
1178 /* Wait for start of REQ/ACK handshake */
1179
Finn Thaind5d37a02016-10-10 00:46:53 -04001180 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001181 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001182 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001183 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1184 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain707d62b2016-01-03 16:06:02 +11001185 goto out;
1186 }
1187 if (!hostdata->selecting) {
1188 do_abort(instance);
Finn Thaindad82612018-09-27 11:17:11 +10001189 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
Finn Thainb7465452016-01-03 16:06:05 +11001192 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1193 scmd_id(cmd));
Finn Thain7c8ed782018-09-27 11:17:11 +10001194 tmp[0] = IDENTIFY(can_disconnect, cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 data = tmp;
1198 phase = PHASE_MSGOUT;
1199 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb15e7912017-01-15 18:50:57 -05001200 if (len) {
1201 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1202 cmd->result = DID_ERROR << 16;
1203 complete_cmd(instance, cmd);
1204 dsprintk(NDEBUG_SELECTION, instance, "IDENTIFY message transfer failed\n");
Finn Thaindad82612018-09-27 11:17:11 +10001205 ret = false;
Finn Thainb15e7912017-01-15 18:50:57 -05001206 goto out;
1207 }
1208
Finn Thainb7465452016-01-03 16:06:05 +11001209 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001212 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Finn Thaine9db3192016-03-23 21:10:21 +11001214#ifdef SUN3_SCSI_VME
1215 dregs->csr |= CSR_INTR;
1216#endif
1217
Boaz Harrosh28424d32007-09-10 22:37:45 +03001218 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Finn Thaindad82612018-09-27 11:17:11 +10001220 ret = false;
Finn Thain707d62b2016-01-03 16:06:02 +11001221
1222out:
1223 if (!hostdata->selecting)
Finn Thain96edebd2018-10-24 18:45:33 +11001224 return false;
Finn Thain707d62b2016-01-03 16:06:02 +11001225 hostdata->selecting = NULL;
Finn Thaindad82612018-09-27 11:17:11 +10001226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Finn Thainaff0cf92016-01-03 16:06:09 +11001229/*
1230 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001231 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 *
1233 * Purpose : transfers data in given phase using polled I/O
1234 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001235 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001236 * what phase is expected, *count - pointer to number of
1237 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001240 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001241 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001243 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
1245 * XXX Note : handling for bus free may be useful.
1246 */
1247
1248/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001249 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * IS 100% reliable, and for the actual data transfer where speed
1251 * counts, we will always do a pseudo DMA or DMA transfer.
1252 */
1253
Finn Thain0d2cf862016-01-03 16:06:11 +11001254static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1255 unsigned char *phase, int *count,
1256 unsigned char **data)
1257{
Finn Thain61e1ce52016-10-10 00:46:53 -04001258 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 unsigned char p = *phase, tmp;
1260 int c = *count;
1261 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Finn Thainaff0cf92016-01-03 16:06:09 +11001263 /*
1264 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * phase specified in the appropriate bits of the TARGET COMMAND
1266 * REGISTER match the STATUS REGISTER
1267 */
1268
Finn Thain0d2cf862016-01-03 16:06:11 +11001269 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001272 /*
1273 * Wait for assertion of REQ, after which the phase bits will be
1274 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
1276
Finn Thaind5d37a02016-10-10 00:46:53 -04001277 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Finn Thainb7465452016-01-03 16:06:05 +11001280 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001283 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001284 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1285 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
1287 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /* Do actual transfer from SCSI bus to / from memory */
1290 if (!(p & SR_IO))
1291 NCR5380_write(OUTPUT_DATA_REG, *d);
1292 else
1293 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1294
1295 ++d;
1296
Finn Thainaff0cf92016-01-03 16:06:09 +11001297 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 * The SCSI standard suggests that in MSGOUT phase, the initiator
1299 * should drop ATN on the last byte of the message phase
1300 * after REQ has been asserted for the handshake but before
1301 * the initiator raises ACK.
1302 */
1303
1304 if (!(p & SR_IO)) {
1305 if (!((p & SR_MSG) && c > 1)) {
1306 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1307 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001308 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1309 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001311 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1312 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001314 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1315 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 } else {
1318 NCR5380_dprint(NDEBUG_PIO, instance);
1319 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1320 }
1321
Finn Thaind5d37a02016-10-10 00:46:53 -04001322 if (NCR5380_poll_politely(hostdata,
Finn Thaina2edc4a2016-01-03 16:05:27 +11001323 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1324 break;
1325
Finn Thainb7465452016-01-03 16:06:05 +11001326 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001329 * We have several special cases to consider during REQ/ACK handshaking :
1330 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001331 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001333 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001334 * message. We must exit with ACK asserted, so that the calling
1335 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
1337 * 3. ACK and ATN are clear and the target may proceed as normal.
1338 */
1339 if (!(p == PHASE_MSGIN && c == 1)) {
1340 if (p == PHASE_MSGOUT && c > 1)
1341 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1342 else
1343 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1344 }
1345 } while (--c);
1346
Finn Thainb7465452016-01-03 16:06:05 +11001347 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 *count = c;
1350 *data = d;
1351 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001352 /* The phase read from the bus is valid if either REQ is (already)
1353 * asserted or if ACK hasn't been released yet. The latter applies if
1354 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1355 */
1356 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 *phase = tmp & PHASE_MASK;
1358 else
1359 *phase = PHASE_UNKNOWN;
1360
1361 if (!c || (*phase == p))
1362 return 0;
1363 else
1364 return -1;
1365}
1366
1367/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001368 * do_reset - issue a reset command
1369 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001371 * Issue a reset sequence to the NCR5380 and try and get the bus
1372 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001374 * This clears the reset interrupt flag because there may be no handler for
1375 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1376 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001378
Finn Thain54d8fe42016-01-03 16:05:06 +11001379static void do_reset(struct Scsi_Host *instance)
1380{
Finn Thain61e1ce52016-10-10 00:46:53 -04001381 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001382 unsigned long flags;
1383
1384 local_irq_save(flags);
1385 NCR5380_write(TARGET_COMMAND_REG,
1386 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001388 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001390 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1391 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Finn Thain80d3eb62016-01-03 16:05:34 +11001394/**
1395 * do_abort - abort the currently established nexus by going to
1396 * MESSAGE OUT phase and sending an ABORT message.
1397 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 *
Finn Thaind04fc41a2019-11-16 14:36:57 +11001399 * Returns 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401
Finn Thain54d8fe42016-01-03 16:05:06 +11001402static int do_abort(struct Scsi_Host *instance)
1403{
Finn Thain61e1ce52016-10-10 00:46:53 -04001404 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 unsigned char *msgptr, phase, tmp;
1406 int len;
1407 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* Request message out phase */
1410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1411
Finn Thainaff0cf92016-01-03 16:06:09 +11001412 /*
1413 * Wait for the target to indicate a valid phase by asserting
1414 * REQ. Once this happens, we'll have either a MSGOUT phase
1415 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001417 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 * We really don't care what value was on the bus or what value
1419 * the target sees, so we just handshake.
1420 */
1421
Finn Thaind5d37a02016-10-10 00:46:53 -04001422 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001423 if (rc < 0)
Finn Thaind04fc41a2019-11-16 14:36:57 +11001424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Finn Thainf35d3472016-01-03 16:05:33 +11001426 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1429
Finn Thainf35d3472016-01-03 16:05:33 +11001430 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001431 NCR5380_write(INITIATOR_COMMAND_REG,
1432 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thaind5d37a02016-10-10 00:46:53 -04001433 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001434 if (rc < 0)
Finn Thaind04fc41a2019-11-16 14:36:57 +11001435 goto out;
Finn Thain80d3eb62016-01-03 16:05:34 +11001436 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 tmp = ABORT;
1440 msgptr = &tmp;
1441 len = 1;
1442 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001443 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Finn Thaind04fc41a2019-11-16 14:36:57 +11001444 if (len)
1445 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /*
1448 * If we got here, and the command completed successfully,
1449 * we're about to go into bus free state.
1450 */
1451
Finn Thaind04fc41a2019-11-16 14:36:57 +11001452out:
Finn Thain80d3eb62016-01-03 16:05:34 +11001453 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaind04fc41a2019-11-16 14:36:57 +11001454 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Finn Thainaff0cf92016-01-03 16:06:09 +11001457/*
1458 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001459 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
1461 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001462 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001464 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001465 * what phase is expected, *count - pointer to number of
1466 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001467 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001469 * maximum number of bytes, 0 if all bytes or transferred or exit
1470 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001472 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474
1475
Finn Thain0d2cf862016-01-03 16:06:11 +11001476static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1477 unsigned char *phase, int *count,
1478 unsigned char **data)
1479{
1480 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001481 int c = *count;
1482 unsigned char p = *phase;
1483 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001485 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1488 *phase = tmp;
1489 return -1;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Finn Thain8053b0e2016-03-23 21:10:19 +11001492 hostdata->connected->SCp.phase = p;
1493
1494 if (p & SR_IO) {
1495 if (hostdata->read_overruns)
1496 c -= hostdata->read_overruns;
1497 else if (hostdata->flags & FLAG_DMA_FIXUP)
1498 --c;
1499 }
1500
1501 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1502 (p & SR_IO) ? "receive" : "send", c, d);
1503
Finn Thaine9db3192016-03-23 21:10:21 +11001504#ifdef CONFIG_SUN3
1505 /* send start chain */
1506 sun3scsi_dma_start(c, *data);
1507#endif
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001510 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1511 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Finn Thain8053b0e2016-03-23 21:10:19 +11001513 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1514 /* On the Medusa, it is a must to initialize the DMA before
1515 * starting the NCR. This is also the cleaner way for the TT.
1516 */
1517 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001518 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001519 else
Finn Thain4a98f892016-10-10 00:46:53 -04001520 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Finn Thainaff0cf92016-01-03 16:06:09 +11001523 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001524 * On the PAS16 at least I/O recovery delays are not needed here.
1525 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
1527
1528 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001529 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001530 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1532 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001533 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001535 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001537 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Finn Thaine9db3192016-03-23 21:10:21 +11001540#ifdef CONFIG_SUN3
1541#ifdef SUN3_SCSI_VME
1542 dregs->csr |= CSR_DMA_ENABLE;
1543#endif
1544 sun3_dma_active = 1;
1545#endif
1546
Finn Thain8053b0e2016-03-23 21:10:19 +11001547 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1548 /* On the Falcon, the DMA setup must be done after the last
1549 * NCR access, else the DMA setup gets trashed!
1550 */
1551 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001552 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001553 else
Finn Thain4a98f892016-10-10 00:46:53 -04001554 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001555 }
1556
1557 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1558 if (result < 0)
1559 return result;
1560
1561 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1562 if (result > 0) {
1563 hostdata->dma_len = result;
1564 return 0;
1565 }
1566
1567 /* The result is zero iff pseudo DMA send/receive was completed. */
1568 hostdata->dma_len = c;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/*
Finn Thaine4dec682016-03-23 21:10:12 +11001571 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001572 *
1573 * For DMA sends, we want to wait until the last byte has been
1574 * transferred out over the bus before we turn off DMA mode. Alas, there
1575 * seems to be no terribly good way of doing this on a 5380 under all
1576 * conditions. For non-scatter-gather operations, we can wait until REQ
1577 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1578 * are nastier, since the device will be expecting more data than we
1579 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1580 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1581 * why this signal was added to the newer chips) but on the older 538[01]
1582 * this signal does not exist. The workaround for this lack is a watchdog;
1583 * we bail out of the wait-loop after a modest amount of wait-time if
1584 * the usual exit conditions are not met. Not a terribly clean or
1585 * correct solution :-%
1586 *
1587 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1588 * If the chip is in DMA receive mode, it will respond to a target's
1589 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1590 * ACK, even if it has _already_ been notified by the DMA controller that
1591 * the current DMA transfer has completed! If the NCR5380 is then taken
1592 * out of DMA mode, this already-acknowledged byte is lost. This is
1593 * not a problem for "one DMA transfer per READ command", because
1594 * the situation will never arise... either all of the data is DMA'ed
1595 * properly, or the target switches to MESSAGE IN phase to signal a
1596 * disconnection (either operation bringing the DMA to a clean halt).
1597 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001598 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001599 * condition before taking the NCR5380 out of DMA mode. One or two extra
1600 * bytes are transferred via PIO as necessary to fill out the original
1601 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
1603
Finn Thain8053b0e2016-03-23 21:10:19 +11001604 if (hostdata->flags & FLAG_DMA_FIXUP) {
1605 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001607 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001608 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 * the chip to latch the last byte, read it, and then disable
1610 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001611 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1613 * REQ is deasserted when ACK is asserted, and not reasserted
1614 * until ACK goes false. Since the NCR5380 won't lower ACK
1615 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001616 * the DMA port or we take the NCR5380 out of DMA mode, we
1617 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * byte.
1619 */
1620
Finn Thaind5d37a02016-10-10 00:46:53 -04001621 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001622 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001623 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001624 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
Finn Thaind5d37a02016-10-10 00:46:53 -04001626 if (NCR5380_poll_politely(hostdata, STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001627 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001628 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001629 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1630 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001631 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1632 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001634 * Wait for the last byte to be sent. If REQ is being asserted for
1635 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 */
Finn Thaind5d37a02016-10-10 00:46:53 -04001637 if (NCR5380_poll_politely2(hostdata,
Finn Thain55181be2016-01-03 16:05:42 +11001638 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1639 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001640 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001641 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001645
1646 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001647 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650/*
1651 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1652 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001653 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001654 * directs us to. Operates on the currently connected command,
1655 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 *
1657 * Inputs : instance, instance for which we are doing commands
1658 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001659 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001660 * modified if a command disconnects, *instance->connected will
1661 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001663 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001664 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
1666
Finn Thain0d2cf862016-01-03 16:06:11 +11001667static void NCR5380_information_transfer(struct Scsi_Host *instance)
Finn Thain4ab2a782017-01-15 18:50:57 -05001668 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Finn Thain0d2cf862016-01-03 16:06:11 +11001669{
Finn Thaine8a60142016-01-03 16:05:54 +11001670 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 unsigned char msgout = NOP;
1672 int sink = 0;
1673 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 unsigned char *data;
1676 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001677 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Finn Thaine9db3192016-03-23 21:10:21 +11001679#ifdef SUN3_SCSI_VME
1680 dregs->csr |= CSR_INTR;
1681#endif
1682
Finn Thain11d2f632016-01-03 16:05:51 +11001683 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001684 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tmp = NCR5380_read(STATUS_REG);
1687 /* We only have a valid SCSI phase when REQ is asserted */
1688 if (tmp & SR_REQ) {
1689 phase = (tmp & PHASE_MASK);
1690 if (phase != old_phase) {
1691 old_phase = phase;
1692 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1693 }
Finn Thaine9db3192016-03-23 21:10:21 +11001694#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04001695 if (phase == PHASE_CMDOUT &&
1696 sun3_dma_setup_done != cmd) {
1697 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11001698
Finn Thain0e9fdd22019-06-18 09:37:57 +08001699 advance_sg_buffer(cmd);
Finn Thaine9db3192016-03-23 21:10:21 +11001700
Finn Thain4a98f892016-10-10 00:46:53 -04001701 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1702
1703 if (count > 0) {
1704 if (rq_data_dir(cmd->request))
1705 sun3scsi_dma_send_setup(hostdata,
1706 cmd->SCp.ptr, count);
1707 else
1708 sun3scsi_dma_recv_setup(hostdata,
1709 cmd->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11001710 sun3_dma_setup_done = cmd;
1711 }
1712#ifdef SUN3_SCSI_VME
1713 dregs->csr |= CSR_INTR;
1714#endif
1715 }
1716#endif /* CONFIG_SUN3 */
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (sink && (phase != PHASE_MSGOUT)) {
1719 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1720
Finn Thain3d07d222016-01-03 16:06:13 +11001721 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1722 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001723 while (NCR5380_read(STATUS_REG) & SR_REQ)
1724 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001725 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1726 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 sink = 0;
1728 continue;
1729 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 case PHASE_DATAOUT:
1733#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001734 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 sink = 1;
1736 do_abort(instance);
1737 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001738 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001739 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001740 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return;
1742#endif
Finn Thainbf1a0c6f2016-01-03 16:05:47 +11001743 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * If there is no room left in the current buffer in the
1746 * scatter-gather list, move onto the next one.
1747 */
1748
Finn Thain0e9fdd22019-06-18 09:37:57 +08001749 advance_sg_buffer(cmd);
1750 dsprintk(NDEBUG_INFORMATION, instance,
1751 "this residual %d, sg ents %d\n",
1752 cmd->SCp.this_residual,
1753 sg_nents(cmd->SCp.buffer));
Finn Thain0d2cf862016-01-03 16:06:11 +11001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001756 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * PSEUDO-DMA for systems that are strictly PIO,
1758 * since we can let the hardware do the handshaking.
1759 *
1760 * For this to work, we need to know the transfersize
1761 * ahead of time, since the pseudo-DMA code will sit
1762 * in an unconditional loop.
1763 */
1764
Finn Thainff3d4572016-01-03 16:05:25 +11001765 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001766 if (!cmd->device->borken)
Finn Thain4a98f892016-10-10 00:46:53 -04001767 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Finn Thain438af512016-03-23 21:10:18 +11001769 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001771 if (NCR5380_transfer_dma(instance, &phase,
1772 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001774 * If the watchdog timer fires, all future
1775 * accesses to this device will use the
1776 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001778 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001779 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 cmd->device->borken = 1;
Finn Thainf9dfed12019-06-09 11:19:11 +10001781 do_reset(instance);
1782 bus_reset_cleanup(instance);
Finn Thain8053b0e2016-03-23 21:10:19 +11001783 }
Finn Thainf825e402016-03-23 21:10:15 +11001784 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001785 /* Transfer a small chunk so that the
1786 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001787 */
Finn Thain08348b12016-08-31 14:44:56 +10001788 transfersize = min(cmd->SCp.this_residual,
1789 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001790 len = transfersize;
1791 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001792 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001793 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001794 }
Finn Thaine9db3192016-03-23 21:10:21 +11001795#ifdef CONFIG_SUN3
1796 if (sun3_dma_setup_done == cmd)
1797 sun3_dma_setup_done = NULL;
1798#endif
Finn Thain16788472016-02-23 10:07:05 +11001799 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 case PHASE_MSGIN:
1801 len = 1;
1802 data = &tmp;
1803 NCR5380_transfer_pio(instance, &phase, &len, &data);
1804 cmd->SCp.Message = tmp;
1805
1806 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 case ABORT:
1808 case COMMAND_COMPLETE:
1809 /* Accept message by clearing ACK */
1810 sink = 1;
1811 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001812 dsprintk(NDEBUG_QUEUES, instance,
1813 "COMMAND COMPLETE %p target %d lun %llu\n",
1814 cmd, scmd_id(cmd), cmd->device->lun);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001817 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Finn Thainf27db8e2016-01-03 16:06:00 +11001819 cmd->result &= ~0xffff;
1820 cmd->result |= cmd->SCp.Status;
1821 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Finn Thain350767f2019-11-16 14:36:57 +11001823 set_resid_from_SCp(cmd);
1824
Finn Thainf27db8e2016-01-03 16:06:00 +11001825 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001826 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001827 else {
1828 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1829 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1830 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1831 cmd);
1832 list_add_tail(&ncmd->list,
1833 &hostdata->autosense);
1834 } else
1835 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837
Finn Thainaff0cf92016-01-03 16:06:09 +11001838 /*
1839 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 * arbitration can resume.
1841 */
1842 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001843
Finn Thain52d3e562016-03-23 21:10:20 +11001844 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return;
1846 case MESSAGE_REJECT:
1847 /* Accept message by clearing ACK */
1848 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1849 switch (hostdata->last_message) {
1850 case HEAD_OF_QUEUE_TAG:
1851 case ORDERED_QUEUE_TAG:
1852 case SIMPLE_QUEUE_TAG:
1853 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001854 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856 default:
1857 break;
1858 }
Finn Thain340b9612016-01-03 16:05:31 +11001859 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001860 case DISCONNECT:
1861 /* Accept message by clearing ACK */
1862 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1863 hostdata->connected = NULL;
1864 list_add(&ncmd->list, &hostdata->disconnected);
1865 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1866 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1867 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001868
Finn Thain0d2cf862016-01-03 16:06:11 +11001869 /*
1870 * Restore phase bits to 0 so an interrupted selection,
1871 * arbitration can resume.
1872 */
1873 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Finn Thaine9db3192016-03-23 21:10:21 +11001875#ifdef SUN3_SCSI_VME
1876 dregs->csr |= CSR_DMA_ENABLE;
1877#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001878 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001879 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001881 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 * ignore SAVE/RESTORE pointers calls.
1883 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001884 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001886 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * compatible.
1888 */
1889 case SAVE_POINTERS:
1890 case RESTORE_POINTERS:
1891 /* Accept message by clearing ACK */
1892 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1893 break;
1894 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001895 /*
1896 * Start the message buffer with the EXTENDED_MESSAGE
1897 * byte, since spi_print_msg() wants the whole thing.
1898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 extended_msg[0] = EXTENDED_MESSAGE;
1900 /* Accept first byte by clearing ACK */
1901 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001902
1903 spin_unlock_irq(&hostdata->lock);
1904
Finn Thainb7465452016-01-03 16:06:05 +11001905 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 len = 2;
1908 data = extended_msg + 1;
1909 phase = PHASE_MSGIN;
1910 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001911 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1912 (int)extended_msg[1],
1913 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Finn Thaine0783ed2016-01-03 16:05:45 +11001915 if (!len && extended_msg[1] > 0 &&
1916 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* Accept third byte by clearing ACK */
1918 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1919 len = extended_msg[1] - 1;
1920 data = extended_msg + 3;
1921 phase = PHASE_MSGIN;
1922
1923 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001924 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1925 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 switch (extended_msg[2]) {
1928 case EXTENDED_SDTR:
1929 case EXTENDED_WDTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 tmp = 0;
1931 }
1932 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001933 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 tmp = 0;
1935 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001936 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1937 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 tmp = 0;
1939 }
Finn Thain11d2f632016-01-03 16:05:51 +11001940
1941 spin_lock_irq(&hostdata->lock);
1942 if (!hostdata->connected)
1943 return;
1944
Finn Thaindf135e32019-03-08 08:49:02 +11001945 /* Reject message */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001946 fallthrough;
Finn Thaindf135e32019-03-08 08:49:02 +11001947 default:
Finn Thainaff0cf92016-01-03 16:06:09 +11001948 /*
1949 * If we get something weird that we aren't expecting,
Finn Thaindf135e32019-03-08 08:49:02 +11001950 * log it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
Finn Thain39bef872017-10-26 16:51:50 +11001952 if (tmp == EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001953 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001954 "rejecting unknown extended message code %02x, length %d\n",
Finn Thain39bef872017-10-26 16:51:50 +11001955 extended_msg[2], extended_msg[1]);
1956 else if (tmp)
1957 scmd_printk(KERN_INFO, cmd,
1958 "rejecting unknown message code %02x\n",
1959 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 msgout = MESSAGE_REJECT;
1962 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1963 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001964 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
1966 case PHASE_MSGOUT:
1967 len = 1;
1968 data = &msgout;
1969 hostdata->last_message = msgout;
1970 NCR5380_transfer_pio(instance, &phase, &len, &data);
1971 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 hostdata->connected = NULL;
Finn Thain45ddc1b2018-09-27 11:17:11 +10001973 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001975 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11001976 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return;
1978 }
1979 msgout = NOP;
1980 break;
1981 case PHASE_CMDOUT:
1982 len = cmd->cmd_len;
1983 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001984 /*
1985 * XXX for performance reasons, on machines with a
1986 * PSEUDO-DMA architecture we should probably
1987 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
1989 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 break;
1991 case PHASE_STATIN:
1992 len = 1;
1993 data = &tmp;
1994 NCR5380_transfer_pio(instance, &phase, &len, &data);
1995 cmd->SCp.Status = tmp;
1996 break;
1997 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001998 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001999 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11002000 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002001 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002002 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04002003 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002004 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Finn Thain11d2f632016-01-03 16:05:51 +11002006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009/*
2010 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2011 *
Finn Thainaff0cf92016-01-03 16:06:09 +11002012 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002013 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2014 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11002015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
2018
Finn Thain0d2cf862016-01-03 16:06:11 +11002019static void NCR5380_reselect(struct Scsi_Host *instance)
2020{
Finn Thaine8a60142016-01-03 16:05:54 +11002021 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002023 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002025 struct NCR5380_cmd *ncmd;
2026 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 /*
2029 * Disable arbitration, etc. since the host adapter obviously
2030 * lost, and tell an interrupted NCR5380_select() to restart.
2031 */
2032
2033 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thain7ef55f62018-09-27 11:17:11 +10002036 if (!target_mask || target_mask & (target_mask - 1)) {
2037 shost_printk(KERN_WARNING, instance,
2038 "reselect: bad target_mask 0x%02x\n", target_mask);
2039 return;
2040 }
Finn Thainb7465452016-01-03 16:06:05 +11002041
Finn Thainaff0cf92016-01-03 16:06:09 +11002042 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 * At this point, we have detected that our SCSI ID is on the bus,
2044 * SEL is true and BSY was false for at least one bus settle delay
2045 * (400 ns).
2046 *
2047 * We must assert BSY ourselves, until the target drops the SEL
2048 * signal.
2049 */
2050
2051 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thaind5d37a02016-10-10 00:46:53 -04002052 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002053 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
Finn Thain08267212018-09-27 11:17:11 +10002054 shost_printk(KERN_ERR, instance, "reselect: !SEL timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002055 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2056 return;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2059
2060 /*
2061 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
2063
Finn Thaind5d37a02016-10-10 00:46:53 -04002064 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002065 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
Finn Thainca694af2018-09-27 11:17:11 +10002066 if ((NCR5380_read(STATUS_REG) & (SR_BSY | SR_SEL)) == 0)
2067 /* BUS FREE phase */
2068 return;
Finn Thain08267212018-09-27 11:17:11 +10002069 shost_printk(KERN_ERR, instance, "reselect: REQ timeout\n");
Finn Thain72064a72016-01-03 16:05:44 +11002070 do_abort(instance);
2071 return;
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Finn Thaine9db3192016-03-23 21:10:21 +11002074#ifdef CONFIG_SUN3
2075 /* acknowledge toggle to MSGIN */
2076 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Finn Thaine9db3192016-03-23 21:10:21 +11002078 /* peek at the byte without really hitting the bus */
2079 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2080#else
2081 {
2082 int len = 1;
2083 unsigned char *data = msg;
2084 unsigned char phase = PHASE_MSGIN;
2085
2086 NCR5380_transfer_pio(instance, &phase, &len, &data);
2087
2088 if (len) {
2089 do_abort(instance);
2090 return;
2091 }
Finn Thain72064a72016-01-03 16:05:44 +11002092 }
Finn Thaine9db3192016-03-23 21:10:21 +11002093#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002096 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002097 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002098 printk("\n");
2099 do_abort(instance);
2100 return;
2101 }
2102 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Finn Thain72064a72016-01-03 16:05:44 +11002104 /*
2105 * We need to add code for SCSI-II to track which devices have
2106 * I_T_L_Q nexuses established, and which have simple I_T_L
2107 * nexuses so we can chose to do additional data transfer.
2108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Finn Thain72064a72016-01-03 16:05:44 +11002110 /*
2111 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2112 * just reestablished, and remove it from the disconnected queue.
2113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Finn Thain32b26a12016-01-03 16:05:58 +11002115 tmp = NULL;
2116 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2117 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2118
2119 if (target_mask == (1 << scmd_id(cmd)) &&
2120 lun == (u8)cmd->device->lun) {
2121 list_del(&ncmd->list);
2122 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002126
2127 if (tmp) {
2128 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2129 "reselect: removed %p from disconnected queue\n", tmp);
2130 } else {
Finn Thain45ddc1b2018-09-27 11:17:11 +10002131 int target = ffs(target_mask) - 1;
2132
Finn Thain72064a72016-01-03 16:05:44 +11002133 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2134 target_mask, lun);
2135 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002136 * Since we have an established nexus that we can't do anything
2137 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002138 */
Finn Thain45ddc1b2018-09-27 11:17:11 +10002139 if (do_abort(instance) == 0)
2140 hostdata->busy[target] &= ~(1 << lun);
Finn Thain72064a72016-01-03 16:05:44 +11002141 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Finn Thain72064a72016-01-03 16:05:44 +11002143
Finn Thaine9db3192016-03-23 21:10:21 +11002144#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04002145 if (sun3_dma_setup_done != tmp) {
2146 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11002147
Finn Thain0e9fdd22019-06-18 09:37:57 +08002148 advance_sg_buffer(tmp);
Finn Thaine9db3192016-03-23 21:10:21 +11002149
Finn Thain4a98f892016-10-10 00:46:53 -04002150 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2151
2152 if (count > 0) {
2153 if (rq_data_dir(tmp->request))
2154 sun3scsi_dma_send_setup(hostdata,
2155 tmp->SCp.ptr, count);
2156 else
2157 sun3scsi_dma_recv_setup(hostdata,
2158 tmp->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11002159 sun3_dma_setup_done = tmp;
2160 }
2161 }
2162
2163 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2164#endif /* CONFIG_SUN3 */
2165
Finn Thain72064a72016-01-03 16:05:44 +11002166 /* Accept message by clearing ACK */
2167 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2168
2169 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002170 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2171 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Finn Thain8b00c3d2016-01-03 16:06:01 +11002174/**
2175 * list_find_cmd - test for presence of a command in a linked list
2176 * @haystack: list of commands
2177 * @needle: command to search for
2178 */
2179
2180static bool list_find_cmd(struct list_head *haystack,
2181 struct scsi_cmnd *needle)
2182{
2183 struct NCR5380_cmd *ncmd;
2184
2185 list_for_each_entry(ncmd, haystack, list)
2186 if (NCR5380_to_scmd(ncmd) == needle)
2187 return true;
2188 return false;
2189}
2190
2191/**
2192 * list_remove_cmd - remove a command from linked list
2193 * @haystack: list of commands
2194 * @needle: command to remove
2195 */
2196
2197static bool list_del_cmd(struct list_head *haystack,
2198 struct scsi_cmnd *needle)
2199{
2200 if (list_find_cmd(haystack, needle)) {
2201 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2202
2203 list_del(&ncmd->list);
2204 return true;
2205 }
2206 return false;
2207}
2208
2209/**
2210 * NCR5380_abort - scsi host eh_abort_handler() method
2211 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002213 * Try to abort a given command by removing it from queues and/or sending
2214 * the target an abort message. This may not succeed in causing a target
2215 * to abort the command. Nonetheless, the low-level driver must forget about
2216 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002218 * The normal path taken by a command is as follows. For EH we trace this
2219 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002221 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2222 * [disconnected -> connected ->]...
2223 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002225 * If cmd was not found at all then presumably it has already been completed,
2226 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002227 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002228 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002229 * We have no option but to forget the aborted command (even if it still
2230 * lacks sense data). The mid-layer may re-issue a command that is in error
2231 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2232 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002233 *
2234 * The lock protects driver data structures, but EH handlers also use it
2235 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 */
2237
Finn Thain710ddd02014-11-12 16:12:02 +11002238static int NCR5380_abort(struct scsi_cmnd *cmd)
2239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002241 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002242 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002243 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002244
Finn Thain11d2f632016-01-03 16:05:51 +11002245 spin_lock_irqsave(&hostdata->lock, flags);
2246
Finn Thain32b26a12016-01-03 16:05:58 +11002247#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002248 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002249#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002250 NCR5380_dprint(NDEBUG_ANY, instance);
2251 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Finn Thain8b00c3d2016-01-03 16:06:01 +11002253 if (list_del_cmd(&hostdata->unissued, cmd)) {
2254 dsprintk(NDEBUG_ABORT, instance,
2255 "abort: removed %p from issue queue\n", cmd);
2256 cmd->result = DID_ABORT << 16;
2257 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002258 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002259 }
2260
Finn Thain707d62b2016-01-03 16:06:02 +11002261 if (hostdata->selecting == cmd) {
2262 dsprintk(NDEBUG_ABORT, instance,
2263 "abort: cmd %p == selecting\n", cmd);
2264 hostdata->selecting = NULL;
2265 cmd->result = DID_ABORT << 16;
2266 complete_cmd(instance, cmd);
2267 goto out;
2268 }
2269
Finn Thain8b00c3d2016-01-03 16:06:01 +11002270 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2271 dsprintk(NDEBUG_ABORT, instance,
2272 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002273 /* Can't call NCR5380_select() and send ABORT because that
2274 * means releasing the lock. Need a bus reset.
2275 */
Finn Thaindc183962016-02-23 10:07:07 +11002276 set_host_byte(cmd, DID_ERROR);
2277 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002278 result = FAILED;
2279 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002280 }
2281
2282 if (hostdata->connected == cmd) {
2283 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2284 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002285 hostdata->dma_len = 0;
Finn Thaind04fc41a2019-11-16 14:36:57 +11002286 if (do_abort(instance) < 0) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002287 set_host_byte(cmd, DID_ERROR);
2288 complete_cmd(instance, cmd);
2289 result = FAILED;
2290 goto out;
2291 }
2292 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002293 complete_cmd(instance, cmd);
2294 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002295 }
2296
Finn Thaindc183962016-02-23 10:07:07 +11002297 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002298 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002299 "abort: removed %p from sense queue\n", cmd);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002300 complete_cmd(instance, cmd);
2301 }
2302
2303out:
2304 if (result == FAILED)
2305 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002306 else {
2307 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002308 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
Finn Thain45ddc1b2018-09-27 11:17:11 +10002309 }
Finn Thain8b00c3d2016-01-03 16:06:01 +11002310
2311 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002312 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002313 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002314
Finn Thain8b00c3d2016-01-03 16:06:01 +11002315 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
2318
Finn Thain6b0e87a2018-09-27 11:17:11 +10002319static void bus_reset_cleanup(struct Scsi_Host *instance)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002320{
Finn Thain11d2f632016-01-03 16:05:51 +11002321 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002322 int i;
Finn Thain62717f52016-01-03 16:06:03 +11002323 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Finn Thain62717f52016-01-03 16:06:03 +11002325 /* reset NCR registers */
2326 NCR5380_write(MODE_REG, MR_BASE);
2327 NCR5380_write(TARGET_COMMAND_REG, 0);
2328 NCR5380_write(SELECT_ENABLE_REG, 0);
2329
2330 /* After the reset, there are no more connected or disconnected commands
2331 * and no busy units; so clear the low-level status here to avoid
2332 * conflicts when the mid-level code tries to wake up the affected
2333 * commands!
2334 */
2335
Finn Thain1884c282016-02-23 10:07:04 +11002336 if (hostdata->selecting) {
2337 hostdata->selecting->result = DID_RESET << 16;
2338 complete_cmd(instance, hostdata->selecting);
2339 hostdata->selecting = NULL;
2340 }
Finn Thain62717f52016-01-03 16:06:03 +11002341
2342 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2343 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2344
2345 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002346 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002347 }
Finn Thain1884c282016-02-23 10:07:04 +11002348 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002349
2350 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2351 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2352
Finn Thain62717f52016-01-03 16:06:03 +11002353 cmd->scsi_done(cmd);
2354 }
Finn Thain1884c282016-02-23 10:07:04 +11002355 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002356
2357 if (hostdata->connected) {
2358 set_host_byte(hostdata->connected, DID_RESET);
2359 complete_cmd(instance, hostdata->connected);
2360 hostdata->connected = NULL;
2361 }
2362
Finn Thain62717f52016-01-03 16:06:03 +11002363 for (i = 0; i < 8; ++i)
2364 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002365 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002366
2367 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002368 maybe_release_dma_irq(instance);
Finn Thain6b0e87a2018-09-27 11:17:11 +10002369}
2370
2371/**
2372 * NCR5380_host_reset - reset the SCSI host
2373 * @cmd: SCSI command undergoing EH
2374 *
2375 * Returns SUCCESS
2376 */
2377
2378static int NCR5380_host_reset(struct scsi_cmnd *cmd)
2379{
2380 struct Scsi_Host *instance = cmd->device->host;
2381 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2382 unsigned long flags;
2383 struct NCR5380_cmd *ncmd;
2384
2385 spin_lock_irqsave(&hostdata->lock, flags);
2386
2387#if (NDEBUG & NDEBUG_ANY)
2388 shost_printk(KERN_INFO, instance, __func__);
2389#endif
2390 NCR5380_dprint(NDEBUG_ANY, instance);
2391 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2392
2393 list_for_each_entry(ncmd, &hostdata->unissued, list) {
2394 struct scsi_cmnd *scmd = NCR5380_to_scmd(ncmd);
2395
2396 scmd->result = DID_RESET << 16;
2397 scmd->scsi_done(scmd);
2398 }
2399 INIT_LIST_HEAD(&hostdata->unissued);
2400
2401 do_reset(instance);
2402 bus_reset_cleanup(instance);
2403
Finn Thain11d2f632016-01-03 16:05:51 +11002404 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return SUCCESS;
2407}