blob: 5187c31a48bec55850d6d8d88c687e0303e3cdd5 [file] [log] [blame]
Finn Thainaff0cf92016-01-03 16:06:09 +11001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Finn Thain594d4ba2016-01-03 16:06:10 +11003 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Finn Thain594d4ba2016-01-03 16:06:10 +11006 * Note that these routines also work with NR53c400 family chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Copyright 1993, Drew Eckhardt
Finn Thain594d4ba2016-01-03 16:06:10 +11009 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Finn Thainaff0cf92016-01-03 16:06:09 +110014 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
Finn Thainc16df322016-01-03 16:06:08 +110028 * With contributions from Ray Van Tassle, Ingmar Baumgart,
29 * Ronald van Cuijlenborg, Alan Cox and others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Finn Thain52d3e562016-03-23 21:10:20 +110032/* Ported to Atari by Roman Hodek and others. */
33
Finn Thaine9db3192016-03-23 21:10:21 +110034/* Adapted for the Sun 3 by Sam Creasey. */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Design
38 *
Finn Thainaff0cf92016-01-03 16:06:09 +110039 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * one simply writes appropriate system specific macros (ie, data
Finn Thainaff0cf92016-01-03 16:06:09 +110041 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * memory mapped) and drops this file in their 'C' wrapper.
43 *
Finn Thainaff0cf92016-01-03 16:06:09 +110044 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * each 5380 in the system - commands that haven't been issued yet,
Finn Thainaff0cf92016-01-03 16:06:09 +110046 * and commands that are currently executing. This means that an
47 * unlimited number of commands may be queued, letting
48 * more commands propagate from the higher driver levels giving higher
49 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
50 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * while a command is already executing.
52 *
53 *
Finn Thainaff0cf92016-01-03 16:06:09 +110054 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
Finn Thainaff0cf92016-01-03 16:06:09 +110056 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
57 * piece of hardware that requires you to sit in a loop polling for
58 * the REQ signal as long as you are connected. Some devices are
59 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110060 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * broken devices are the exception rather than the rule and I'd rather
62 * spend my time optimizing for the normal case.
63 *
64 * Architecture :
65 *
66 * At the heart of the design is a coroutine, NCR5380_main,
67 * which is started from a workqueue for each NCR5380 host in the
68 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
69 * removing the commands from the issue queue and calling
Finn Thainaff0cf92016-01-03 16:06:09 +110070 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
72 * Once a nexus is established, the NCR5380_information_transfer()
73 * phase goes through the various phases as instructed by the target.
74 * if the target goes into MSG IN and sends a DISCONNECT message,
75 * the command structure is placed into the per instance disconnected
Finn Thainaff0cf92016-01-03 16:06:09 +110076 * queue, and NCR5380_main tries to find more work. If the target is
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * idle for too long, the system will try to sleep.
78 *
79 * If a command has disconnected, eventually an interrupt will trigger,
80 * calling NCR5380_intr() which will in turn call NCR5380_reselect
81 * to reestablish a nexus. This will run main if necessary.
82 *
Finn Thainaff0cf92016-01-03 16:06:09 +110083 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * appropriate.
85 *
Finn Thainaff0cf92016-01-03 16:06:09 +110086 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * structures, being initialized after the command is connected
88 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
89 * Note that in violation of the standard, an implicit SAVE POINTERS operation
90 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
91 */
92
93/*
94 * Using this file :
95 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Finn Thainaff0cf92016-01-03 16:06:09 +110096 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * and macros and include this file in your driver.
98 *
Finn Thainaff0cf92016-01-03 16:06:09 +110099 * These macros control options :
100 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
Finn Thain594d4ba2016-01-03 16:06:10 +1100101 * defined.
Finn Thainaff0cf92016-01-03 16:06:09 +1100102 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Finn Thain594d4ba2016-01-03 16:06:10 +1100104 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
106 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
Finn Thain594d4ba2016-01-03 16:06:10 +1100107 * transceivers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
109 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
Finn Thain594d4ba2016-01-03 16:06:10 +1100110 * override-configure an IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
113 *
Finn Thain8053b0e2016-03-23 21:10:19 +1100114 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
115 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * NCR5380_read(register) - read from the specified register
119 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100120 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100122 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100123 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
125 * Either real DMA *or* pseudo DMA may be implemented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * NCR5380_dma_write_setup(instance, src, count) - initialize
128 * NCR5380_dma_read_setup(instance, dst, count) - initialize
129 * NCR5380_dma_residual(instance); - residual count
130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * The generic driver is initialized by calling NCR5380_init(instance),
Finn Thainaff0cf92016-01-03 16:06:09 +1100132 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
134 * possible) function may be used.
135 */
136
Finn Thaine5d55d12016-03-23 21:10:16 +1100137#ifndef NCR5380_io_delay
138#define NCR5380_io_delay(x)
139#endif
140
Finn Thain52d3e562016-03-23 21:10:20 +1100141#ifndef NCR5380_acquire_dma_irq
142#define NCR5380_acquire_dma_irq(x) (1)
143#endif
144
145#ifndef NCR5380_release_dma_irq
146#define NCR5380_release_dma_irq(x)
147#endif
148
Finn Thain54d8fe42016-01-03 16:05:06 +1100149static int do_abort(struct Scsi_Host *);
150static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Finn Thainc16df322016-01-03 16:06:08 +1100152/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100153 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100154 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100156 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158
Finn Thain710ddd02014-11-12 16:12:02 +1100159static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Finn Thainaff0cf92016-01-03 16:06:09 +1100161 /*
162 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * various queues are valid.
164 */
165
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200166 if (scsi_bufflen(cmd)) {
167 cmd->SCp.buffer = scsi_sglist(cmd);
168 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200169 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 cmd->SCp.this_residual = cmd->SCp.buffer->length;
171 } else {
172 cmd->SCp.buffer = NULL;
173 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200174 cmd->SCp.ptr = NULL;
175 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100177
178 cmd->SCp.Status = 0;
179 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Finn Thainb32ade12016-01-03 16:05:41 +1100183 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100184 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100185 * @reg1: 5380 register to poll
186 * @bit1: Bitmask to check
187 * @val1: Expected value
188 * @reg2: Second 5380 register to poll
189 * @bit2: Second bitmask to check
190 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100191 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Finn Thain2f854b82016-01-03 16:05:22 +1100193 * Polls the chip in a reasonably efficient manner waiting for an
194 * event to occur. After a short quick poll we begin to yield the CPU
195 * (if possible). In irq contexts the time-out is arbitrarily limited.
196 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
Finn Thainb32ade12016-01-03 16:05:41 +1100198 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Finn Thainb32ade12016-01-03 16:05:41 +1100201static int NCR5380_poll_politely2(struct Scsi_Host *instance,
202 int reg1, int bit1, int val1,
203 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100204{
205 struct NCR5380_hostdata *hostdata = shost_priv(instance);
206 unsigned long deadline = jiffies + wait;
207 unsigned long n;
208
209 /* Busy-wait for up to 10 ms */
210 n = min(10000U, jiffies_to_usecs(wait));
211 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100212 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100213 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100214 if ((NCR5380_read(reg1) & bit1) == val1)
215 return 0;
216 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100219 } while (n--);
220
221 if (irqs_disabled() || in_interrupt())
222 return -ETIMEDOUT;
223
224 /* Repeatedly sleep for 1 ms until deadline */
225 while (time_is_after_jiffies(deadline)) {
226 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100227 if ((NCR5380_read(reg1) & bit1) == val1)
228 return 0;
229 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Finn Thain2f854b82016-01-03 16:05:22 +1100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -ETIMEDOUT;
234}
235
Finn Thainb32ade12016-01-03 16:05:41 +1100236static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
237 int reg, int bit, int val, int wait)
238{
239 return NCR5380_poll_politely2(instance, reg, bit, val,
240 reg, bit, val, wait);
241}
242
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100243#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static struct {
245 unsigned char mask;
246 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100247} signals[] = {
248 {SR_DBP, "PARITY"},
249 {SR_RST, "RST"},
250 {SR_BSY, "BSY"},
251 {SR_REQ, "REQ"},
252 {SR_MSG, "MSG"},
253 {SR_CD, "CD"},
254 {SR_IO, "IO"},
255 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100257},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258basrs[] = {
Finn Thainaff0cf92016-01-03 16:06:09 +1100259 {BASR_ATN, "ATN"},
260 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100262},
263icrs[] = {
264 {ICR_ASSERT_RST, "ASSERT RST"},
265 {ICR_ASSERT_ACK, "ASSERT ACK"},
266 {ICR_ASSERT_BSY, "ASSERT BSY"},
267 {ICR_ASSERT_SEL, "ASSERT SEL"},
268 {ICR_ASSERT_ATN, "ASSERT ATN"},
269 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100271},
272mrs[] = {
273 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
274 {MR_TARGET, "MODE TARGET"},
275 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
276 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
Finn Thain0d2cf862016-01-03 16:06:11 +1100277 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100278 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
279 {MR_DMA_MODE, "MODE DMA"},
280 {MR_ARBITRATE, "MODE ARBITRATION"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 {0, NULL}
282};
283
284/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100285 * NCR5380_print - print scsi bus signals
286 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100288 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290
291static void NCR5380_print(struct Scsi_Host *instance)
292{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
296 status = NCR5380_read(STATUS_REG);
297 mr = NCR5380_read(MODE_REG);
298 icr = NCR5380_read(INITIATOR_COMMAND_REG);
299 basr = NCR5380_read(BUS_AND_STATUS_REG);
300
301 printk("STATUS_REG: %02x ", status);
302 for (i = 0; signals[i].mask; ++i)
303 if (status & signals[i].mask)
304 printk(",%s", signals[i].name);
305 printk("\nBASR: %02x ", basr);
306 for (i = 0; basrs[i].mask; ++i)
307 if (basr & basrs[i].mask)
308 printk(",%s", basrs[i].name);
309 printk("\nICR: %02x ", icr);
310 for (i = 0; icrs[i].mask; ++i)
311 if (icr & icrs[i].mask)
312 printk(",%s", icrs[i].name);
313 printk("\nMODE: %02x ", mr);
314 for (i = 0; mrs[i].mask; ++i)
315 if (mr & mrs[i].mask)
316 printk(",%s", mrs[i].name);
317 printk("\n");
318}
319
Finn Thain0d2cf862016-01-03 16:06:11 +1100320static struct {
321 unsigned char value;
322 const char *name;
323} phases[] = {
324 {PHASE_DATAOUT, "DATAOUT"},
325 {PHASE_DATAIN, "DATAIN"},
326 {PHASE_CMDOUT, "CMDOUT"},
327 {PHASE_STATIN, "STATIN"},
328 {PHASE_MSGOUT, "MSGOUT"},
329 {PHASE_MSGIN, "MSGIN"},
330 {PHASE_UNKNOWN, "UNKNOWN"}
331};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Finn Thainc16df322016-01-03 16:06:08 +1100333/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100334 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100335 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100337 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339
340static void NCR5380_print_phase(struct Scsi_Host *instance)
341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned char status;
343 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 status = NCR5380_read(STATUS_REG);
346 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100347 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100349 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
350 (phases[i].value != (status & PHASE_MASK)); ++i)
351 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100352 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354}
355#endif
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Finn Thaind5f7e652016-01-03 16:05:03 +1100358static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100361 * probe_intr - helper for IRQ autoprobe
362 * @irq: interrupt number
363 * @dev_id: unused
364 * @regs: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100366 * Set a flag to indicate the IRQ in question was received. This is
367 * used by the IRQ probe code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100369
David Howells7d12e782006-10-05 14:55:46 +0100370static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 probe_irq = irq;
373 return IRQ_HANDLED;
374}
375
376/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100377 * NCR5380_probe_irq - find the IRQ of an NCR5380
378 * @instance: NCR5380 controller
379 * @possible: bitmask of ISA IRQ lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100381 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
382 * and then looking to see what interrupt actually turned up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
384
Andrew Morton702809c2007-05-23 14:41:56 -0700385static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
386 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Finn Thaine8a60142016-01-03 16:05:54 +1100388 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned long timeout;
390 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Finn Thain22f5f102014-11-12 16:11:56 +1100392 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100393 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 trying_irqs |= mask;
395
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500396 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100397 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * A interrupt is triggered whenever BSY = false, SEL = true
Finn Thainaff0cf92016-01-03 16:06:09 +1100401 * and a bit set in the SELECT_ENABLE_REG is asserted on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 * SCSI bus.
403 *
404 * Note that the bus is only driven when the phase control signals
405 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
406 * to zero.
407 */
408
409 NCR5380_write(TARGET_COMMAND_REG, 0);
410 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
411 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
412 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
413
Finn Thain22f5f102014-11-12 16:11:56 +1100414 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800415 schedule_timeout_uninterruptible(1);
Finn Thainaff0cf92016-01-03 16:06:09 +1100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 NCR5380_write(SELECT_ENABLE_REG, 0);
418 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
419
Finn Thain22f5f102014-11-12 16:11:56 +1100420 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (trying_irqs & mask)
422 free_irq(i, NULL);
423
424 return probe_irq;
425}
426
427/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100428 * NCR58380_info - report driver and host information
429 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100431 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 */
433
Finn Thain8c325132014-11-12 16:11:58 +1100434static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Finn Thain8c325132014-11-12 16:11:58 +1100436 struct NCR5380_hostdata *hostdata = shost_priv(instance);
437
438 return hostdata->info;
439}
440
441static void prepare_info(struct Scsi_Host *instance)
442{
443 struct NCR5380_hostdata *hostdata = shost_priv(instance);
444
445 snprintf(hostdata->info, sizeof(hostdata->info),
446 "%s, io_port 0x%lx, n_io_port %d, "
447 "base 0x%lx, irq %d, "
448 "can_queue %d, cmd_per_lun %d, "
449 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100450 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100451 "options { %s} ",
452 instance->hostt->name, instance->io_port, instance->n_io_port,
453 instance->base, instance->irq,
454 instance->can_queue, instance->cmd_per_lun,
455 instance->sg_tablesize, instance->this_id,
Finn Thain1bb46002016-03-23 21:10:14 +1100456 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100457 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100458 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100460 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100463 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100466 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#endif
Finn Thain8c325132014-11-12 16:11:58 +1100468 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100472 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100473 * @instance: adapter to configure
474 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100476 * Initializes *instance and corresponding 5380 chip,
477 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100479 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100480 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100482 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
484
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800485static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Finn Thaine8a60142016-01-03 16:05:54 +1100487 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100488 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100489 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Finn Thainae5e33a2016-03-23 21:10:23 +1100491 instance->max_lun = 7;
492
Finn Thain0d2cf862016-01-03 16:06:11 +1100493 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100495 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
497 if (i > hostdata->id_mask)
498 hostdata->id_higher_mask |= i;
499 for (i = 0; i < 8; ++i)
500 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100501 hostdata->dma_len = 0;
502
Finn Thain11d2f632016-01-03 16:05:51 +1100503 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100505 hostdata->sensing = NULL;
506 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100507 INIT_LIST_HEAD(&hostdata->unissued);
508 INIT_LIST_HEAD(&hostdata->disconnected);
509
Finn Thain55181be2016-01-03 16:05:42 +1100510 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100511
Finn Thain8d8601a2016-01-03 16:05:37 +1100512 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100513 hostdata->work_q = alloc_workqueue("ncr5380_%d",
514 WQ_UNBOUND | WQ_MEM_RECLAIM,
515 1, instance->host_no);
516 if (!hostdata->work_q)
517 return -ENOMEM;
518
Finn Thain8c325132014-11-12 16:11:58 +1100519 prepare_info(instance);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
522 NCR5380_write(MODE_REG, MR_BASE);
523 NCR5380_write(TARGET_COMMAND_REG, 0);
524 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100525
526 /* Calibrate register polling loop */
527 i = 0;
528 deadline = jiffies + 1;
529 do {
530 cpu_relax();
531 } while (time_is_after_jiffies(deadline));
532 deadline += msecs_to_jiffies(256);
533 do {
534 NCR5380_read(STATUS_REG);
535 ++i;
536 cpu_relax();
537 } while (time_is_after_jiffies(deadline));
538 hostdata->accesses_per_ms = i / 256;
539
Finn Thainb6488f92016-01-03 16:05:08 +1100540 return 0;
541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Finn Thainb6488f92016-01-03 16:05:08 +1100543/**
544 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
545 * @instance: adapter to check
546 *
547 * If the system crashed, it may have crashed with a connected target and
548 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
549 * currently established nexus, which we know nothing about. Failing that
550 * do a bus reset.
551 *
552 * Note that a bus reset will cause the chip to assert IRQ.
553 *
554 * Returns 0 if successful, otherwise -ENXIO.
555 */
556
557static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
558{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100559 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100560 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
563 switch (pass) {
564 case 1:
565 case 3:
566 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100567 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
568 NCR5380_poll_politely(instance,
569 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100572 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 do_abort(instance);
574 break;
575 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100576 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100578 /* Wait after a reset; the SCSI standard calls for
579 * 250ms, we wait 500ms to be on the safe side.
580 * But some Toshiba CD-ROMs need ten times that.
581 */
582 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
583 msleep(2500);
584 else
585 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100588 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return -ENXIO;
590 }
591 }
592 return 0;
593}
594
595/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100596 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100597 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100598 *
599 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800602static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Finn Thaine8a60142016-01-03 16:05:54 +1100604 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Finn Thain8d8601a2016-01-03 16:05:37 +1100606 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100607 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/**
Finn Thain677e0192016-01-03 16:05:59 +1100611 * complete_cmd - finish processing a command and return it to the SCSI ML
612 * @instance: the host instance
613 * @cmd: command to complete
614 */
615
616static void complete_cmd(struct Scsi_Host *instance,
617 struct scsi_cmnd *cmd)
618{
619 struct NCR5380_hostdata *hostdata = shost_priv(instance);
620
621 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
622
Finn Thainf27db8e2016-01-03 16:06:00 +1100623 if (hostdata->sensing == cmd) {
624 /* Autosense processing ends here */
625 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
626 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
627 set_host_byte(cmd, DID_ERROR);
628 } else
629 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
630 hostdata->sensing = NULL;
631 }
632
Finn Thain677e0192016-01-03 16:05:59 +1100633 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
634
635 cmd->scsi_done(cmd);
636}
637
638/**
Finn Thain1bb40582016-01-03 16:05:29 +1100639 * NCR5380_queue_command - queue a command
640 * @instance: the relevant SCSI adapter
641 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
Finn Thain1bb40582016-01-03 16:05:29 +1100643 * cmd is added to the per-instance issue queue, with minor
644 * twiddling done to the host specific fields of cmd. If the
645 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
647
Finn Thain1bb40582016-01-03 16:05:29 +1100648static int NCR5380_queue_command(struct Scsi_Host *instance,
649 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Finn Thain1bb40582016-01-03 16:05:29 +1100651 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100652 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100653 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655#if (NDEBUG & NDEBUG_NO_WRITE)
656 switch (cmd->cmnd[0]) {
657 case WRITE_6:
658 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100659 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100661 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100664#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 cmd->result = 0;
667
Finn Thain52d3e562016-03-23 21:10:20 +1100668 if (!NCR5380_acquire_dma_irq(instance))
669 return SCSI_MLQUEUE_HOST_BUSY;
670
Finn Thain11d2f632016-01-03 16:05:51 +1100671 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100672
Finn Thainaff0cf92016-01-03 16:06:09 +1100673 /*
674 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100676 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * sense data is only guaranteed to be valid while the condition exists.
678 */
679
Finn Thain32b26a12016-01-03 16:05:58 +1100680 if (cmd->cmnd[0] == REQUEST_SENSE)
681 list_add(&ncmd->list, &hostdata->unissued);
682 else
683 list_add_tail(&ncmd->list, &hostdata->unissued);
684
Finn Thain11d2f632016-01-03 16:05:51 +1100685 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100686
Finn Thaindbb6b352016-01-03 16:05:53 +1100687 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
688 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100691 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693}
694
Finn Thain52d3e562016-03-23 21:10:20 +1100695static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
696{
697 struct NCR5380_hostdata *hostdata = shost_priv(instance);
698
699 /* Caller does the locking needed to set & test these data atomically */
700 if (list_empty(&hostdata->disconnected) &&
701 list_empty(&hostdata->unissued) &&
702 list_empty(&hostdata->autosense) &&
703 !hostdata->connected &&
704 !hostdata->selecting)
705 NCR5380_release_dma_irq(instance);
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100709 * dequeue_next_cmd - dequeue a command for processing
710 * @instance: the scsi host instance
711 *
712 * Priority is given to commands on the autosense queue. These commands
713 * need autosense because of a CHECK CONDITION result.
714 *
715 * Returns a command pointer if a command is found for a target that is
716 * not already busy. Otherwise returns NULL.
717 */
718
719static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
720{
721 struct NCR5380_hostdata *hostdata = shost_priv(instance);
722 struct NCR5380_cmd *ncmd;
723 struct scsi_cmnd *cmd;
724
Finn Thain8d5dbec2016-02-23 10:07:09 +1100725 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100726 list_for_each_entry(ncmd, &hostdata->unissued, list) {
727 cmd = NCR5380_to_scmd(ncmd);
728 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
729 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
730
731 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
732 list_del(&ncmd->list);
733 dsprintk(NDEBUG_QUEUES, instance,
734 "dequeue: removed %p from issue queue\n", cmd);
735 return cmd;
736 }
737 }
738 } else {
739 /* Autosense processing begins here */
740 ncmd = list_first_entry(&hostdata->autosense,
741 struct NCR5380_cmd, list);
742 list_del(&ncmd->list);
743 cmd = NCR5380_to_scmd(ncmd);
744 dsprintk(NDEBUG_QUEUES, instance,
745 "dequeue: removed %p from autosense queue\n", cmd);
746 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
747 hostdata->sensing = cmd;
748 return cmd;
749 }
750 return NULL;
751}
752
753static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
754{
755 struct NCR5380_hostdata *hostdata = shost_priv(instance);
756 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
757
Finn Thain8d5dbec2016-02-23 10:07:09 +1100758 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100759 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
760 list_add(&ncmd->list, &hostdata->autosense);
761 hostdata->sensing = NULL;
762 } else
763 list_add(&ncmd->list, &hostdata->unissued);
764}
765
766/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100767 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100769 * NCR5380_main is a coroutine that runs as long as more work can
770 * be done on the NCR5380 host adapters in a system. Both
771 * NCR5380_queue_command() and NCR5380_intr() will try to start it
772 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
774
David Howellsc4028952006-11-22 14:57:56 +0000775static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
David Howellsc4028952006-11-22 14:57:56 +0000777 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100778 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100784
Finn Thain0a4e3612016-01-03 16:06:07 +1100785 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100786 while (!hostdata->connected && !hostdata->selecting) {
787 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
788
789 if (!cmd)
790 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100791
792 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100795 * Attempt to establish an I_T_L nexus here.
796 * On success, instance->hostdata->connected is set.
797 * On failure, we must add the command back to the
798 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100800 /*
801 * REQUEST SENSE commands are issued without tagged
802 * queueing, even on SCSI-II devices because the
803 * contingent allegiance condition exists for the
804 * entire unit.
805 */
Finn Thain32b26a12016-01-03 16:05:58 +1100806
Finn Thainccf6efd2016-02-23 10:07:08 +1100807 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100808 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100809 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100810 } else {
811 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
812 "main: select failed, returning %p to queue\n", cmd);
813 requeue_cmd(instance, cmd);
814 }
815 }
Finn Thaine4dec682016-03-23 21:10:12 +1100816 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100817 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100820 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100821 spin_unlock_irq(&hostdata->lock);
822 if (!done)
823 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Finn Thain8053b0e2016-03-23 21:10:19 +1100827/*
828 * NCR5380_dma_complete - finish DMA transfer
829 * @instance: the scsi host instance
830 *
831 * Called by the interrupt handler when DMA finishes or a phase
832 * mismatch occurs (which would end the DMA transfer).
833 */
834
835static void NCR5380_dma_complete(struct Scsi_Host *instance)
836{
837 struct NCR5380_hostdata *hostdata = shost_priv(instance);
838 int transferred;
839 unsigned char **data;
840 int *count;
841 int saved_data = 0, overrun = 0;
842 unsigned char p;
843
844 if (hostdata->read_overruns) {
845 p = hostdata->connected->SCp.phase;
846 if (p & SR_IO) {
847 udelay(10);
848 if ((NCR5380_read(BUS_AND_STATUS_REG) &
849 (BASR_PHASE_MATCH | BASR_ACK)) ==
850 (BASR_PHASE_MATCH | BASR_ACK)) {
851 saved_data = NCR5380_read(INPUT_DATA_REG);
852 overrun = 1;
853 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
854 }
855 }
856 }
857
Finn Thaine9db3192016-03-23 21:10:21 +1100858#ifdef CONFIG_SUN3
859 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
860 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
861 instance->host_no);
862 BUG();
863 }
864
865 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
866 (BASR_PHASE_MATCH | BASR_ACK)) {
867 pr_err("scsi%d: BASR %02x\n", instance->host_no,
868 NCR5380_read(BUS_AND_STATUS_REG));
869 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
870 instance->host_no);
871 BUG();
872 }
873#endif
874
Finn Thain8053b0e2016-03-23 21:10:19 +1100875 NCR5380_write(MODE_REG, MR_BASE);
876 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
877 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
878
879 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
880 hostdata->dma_len = 0;
881
882 data = (unsigned char **)&hostdata->connected->SCp.ptr;
883 count = &hostdata->connected->SCp.this_residual;
884 *data += transferred;
885 *count -= transferred;
886
887 if (hostdata->read_overruns) {
888 int cnt, toPIO;
889
890 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
891 cnt = toPIO = hostdata->read_overruns;
892 if (overrun) {
893 dsprintk(NDEBUG_DMA, instance,
894 "Got an input overrun, using saved byte\n");
895 *(*data)++ = saved_data;
896 (*count)--;
897 cnt--;
898 toPIO--;
899 }
900 if (toPIO > 0) {
901 dsprintk(NDEBUG_DMA, instance,
902 "Doing %d byte PIO to 0x%p\n", cnt, *data);
903 NCR5380_transfer_pio(instance, &p, &cnt, data);
904 *count -= toPIO - cnt;
905 }
906 }
907 }
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#ifndef DONT_USE_INTR
911
912/**
Finn Thaincd400822016-01-03 16:05:40 +1100913 * NCR5380_intr - generic NCR5380 irq handler
914 * @irq: interrupt number
915 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 *
Finn Thaincd400822016-01-03 16:05:40 +1100917 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
918 * from the disconnected queue, and restarting NCR5380_main()
919 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *
Finn Thaincd400822016-01-03 16:05:40 +1100921 * The chip can assert IRQ in any of six different conditions. The IRQ flag
922 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
923 * Three of these six conditions are latched in the Bus and Status Register:
924 * - End of DMA (cleared by ending DMA Mode)
925 * - Parity error (cleared by reading RPIR)
926 * - Loss of BSY (cleared by reading RPIR)
927 * Two conditions have flag bits that are not latched:
928 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
929 * - Bus reset (non-maskable)
930 * The remaining condition has no flag bit at all:
931 * - Selection/reselection
932 *
933 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
934 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
935 * claimed that "the design of the [DP8490] interrupt logic ensures
936 * interrupts will not be lost (they can be on the DP5380)."
937 * The L5380/53C80 datasheet from LOGIC Devices has more details.
938 *
939 * Checking for bus reset by reading RST is futile because of interrupt
940 * latency, but a bus reset will reset chip logic. Checking for parity error
941 * is unnecessary because that interrupt is never enabled. A Loss of BSY
942 * condition will clear DMA Mode. We can tell when this occurs because the
943 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
945
Finn Thaincd400822016-01-03 16:05:40 +1100946static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800948 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100949 struct NCR5380_hostdata *hostdata = shost_priv(instance);
950 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned char basr;
952 unsigned long flags;
953
Finn Thain11d2f632016-01-03 16:05:51 +1100954 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Finn Thaincd400822016-01-03 16:05:40 +1100956 basr = NCR5380_read(BUS_AND_STATUS_REG);
957 if (basr & BASR_IRQ) {
958 unsigned char mr = NCR5380_read(MODE_REG);
959 unsigned char sr = NCR5380_read(STATUS_REG);
960
Finn Thainb7465452016-01-03 16:06:05 +1100961 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
962 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100963
Finn Thain8053b0e2016-03-23 21:10:19 +1100964 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
965 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
966 * We ack IRQ after clearing Mode Register. Workarounds
967 * for End of DMA errata need to happen in DMA Mode.
968 */
969
970 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
971
972 if (hostdata->connected) {
973 NCR5380_dma_complete(instance);
974 queue_work(hostdata->work_q, &hostdata->main_task);
975 } else {
976 NCR5380_write(MODE_REG, MR_BASE);
977 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
978 }
979 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100980 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
981 /* Probably reselected */
982 NCR5380_write(SELECT_ENABLE_REG, 0);
983 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
984
Finn Thainb7465452016-01-03 16:06:05 +1100985 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100986
987 if (!hostdata->connected) {
988 NCR5380_reselect(instance);
989 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Finn Thaincd400822016-01-03 16:05:40 +1100991 if (!hostdata->connected)
992 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
993 } else {
994 /* Probably Bus Reset */
995 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
996
Finn Thainb7465452016-01-03 16:06:05 +1100997 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100998#ifdef SUN3_SCSI_VME
999 dregs->csr |= CSR_DMA_ENABLE;
1000#endif
Finn Thaincd400822016-01-03 16:05:40 +11001001 }
1002 handled = 1;
1003 } else {
1004 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +11001005#ifdef SUN3_SCSI_VME
1006 dregs->csr |= CSR_DMA_ENABLE;
1007#endif
Finn Thaincd400822016-01-03 16:05:40 +11001008 }
1009
Finn Thain11d2f632016-01-03 16:05:51 +11001010 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +11001011
1012 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Finn Thainaff0cf92016-01-03 16:06:09 +11001015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Finn Thainaff0cf92016-01-03 16:06:09 +11001017/*
Finn Thain710ddd02014-11-12 16:12:02 +11001018 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001019 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 *
1021 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +11001022 * including ARBITRATION, SELECTION, and initial message out for
1023 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001025 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +11001026 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +11001027 *
Finn Thain707d62b2016-01-03 16:06:02 +11001028 * Returns cmd if selection failed but should be retried,
1029 * NULL if selection failed and should not be retried, or
1030 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001032 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +11001033 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1034 * with registers as they should have been on entry - ie
1035 * SELECT_ENABLE will be set appropriately, the NCR5380
1036 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001038 * If successful : I_T_L or I_T_L_Q nexus will be established,
1039 * instance->connected will be set to cmd.
1040 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001042 * If failed (no target) : cmd->scsi_done() will be called, and the
1043 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001045
Finn Thain707d62b2016-01-03 16:06:02 +11001046static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1047 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Finn Thaine8a60142016-01-03 16:05:54 +11001049 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned char tmp[3], phase;
1051 unsigned char *data;
1052 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +11001056 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1057 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Finn Thain707d62b2016-01-03 16:06:02 +11001059 /*
1060 * Arbitration and selection phases are slow and involve dropping the
1061 * lock, so we have to watch out for EH. An exception handler may
1062 * change 'selecting' to NULL. This function will then return NULL
1063 * so that the caller will forget about 'cmd'. (During information
1064 * transfer phases, EH may change 'connected' to NULL.)
1065 */
1066 hostdata->selecting = cmd;
1067
Finn Thainaff0cf92016-01-03 16:06:09 +11001068 /*
1069 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 * data bus during SELECTION.
1071 */
1072
1073 NCR5380_write(TARGET_COMMAND_REG, 0);
1074
Finn Thainaff0cf92016-01-03 16:06:09 +11001075 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * Start arbitration.
1077 */
1078
1079 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1080 NCR5380_write(MODE_REG, MR_ARBITRATE);
1081
Finn Thain55500d92016-01-03 16:05:35 +11001082 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1083 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
1085
Finn Thain11d2f632016-01-03 16:05:51 +11001086 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001087 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1088 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1089 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001090 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001091 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1092 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001093 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001094 }
Finn Thainccf6efd2016-02-23 10:07:08 +11001095 if (!hostdata->selecting) {
1096 /* Command was aborted */
1097 NCR5380_write(MODE_REG, MR_BASE);
1098 goto out;
1099 }
Finn Thainb32ade12016-01-03 16:05:41 +11001100 if (err < 0) {
1101 NCR5380_write(MODE_REG, MR_BASE);
1102 shost_printk(KERN_ERR, instance,
1103 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001104 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001105 }
Finn Thain11d2f632016-01-03 16:05:51 +11001106 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001107
1108 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 udelay(3);
1110
1111 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001112 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1113 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1114 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001116 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001117 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
Finn Thaincf13b082016-01-03 16:05:18 +11001120
1121 /* After/during arbitration, BSY should be asserted.
1122 * IBM DPES-31080 Version S31Q works now
1123 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1124 */
1125 NCR5380_write(INITIATOR_COMMAND_REG,
1126 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Finn Thainaff0cf92016-01-03 16:06:09 +11001128 /*
1129 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * a minimum so we'll udelay ceil(1.2)
1131 */
1132
Finn Thain9c3f0e22016-01-03 16:05:11 +11001133 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1134 udelay(15);
1135 else
1136 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Finn Thain11d2f632016-01-03 16:05:51 +11001138 spin_lock_irq(&hostdata->lock);
1139
Finn Thain72064a72016-01-03 16:05:44 +11001140 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1141 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001142 goto out;
1143
1144 if (!hostdata->selecting) {
1145 NCR5380_write(MODE_REG, MR_BASE);
1146 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1147 goto out;
1148 }
Finn Thain72064a72016-01-03 16:05:44 +11001149
Finn Thainb7465452016-01-03 16:06:05 +11001150 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Finn Thainaff0cf92016-01-03 16:06:09 +11001152 /*
1153 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 * the host and target ID's on the SCSI bus.
1155 */
1156
Finn Thain3d07d222016-01-03 16:06:13 +11001157 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Finn Thainaff0cf92016-01-03 16:06:09 +11001159 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * Raise ATN while SEL is true before BSY goes false from arbitration,
1161 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1162 * phase immediately after selection.
1163 */
1164
Finn Thain3d07d222016-01-03 16:06:13 +11001165 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1166 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 NCR5380_write(MODE_REG, MR_BASE);
1168
Finn Thainaff0cf92016-01-03 16:06:09 +11001169 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 * Reselect interrupts must be turned off prior to the dropping of BSY,
1171 * otherwise we will trigger an interrupt.
1172 */
1173 NCR5380_write(SELECT_ENABLE_REG, 0);
1174
Finn Thain11d2f632016-01-03 16:05:51 +11001175 spin_unlock_irq(&hostdata->lock);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001178 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * the BSY signal.
1180 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001181 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001184 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1185 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Finn Thainaff0cf92016-01-03 16:06:09 +11001187 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001189 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * appropriate propagation delay has expired, and we're confusing
1191 * a BSY signal from ourselves as the target's response to SELECTION.
1192 *
1193 * A small delay (the 'C++' frontend breaks the pipeline with an
1194 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001195 * tighter 'C' code breaks and requires this) solves the problem -
1196 * the 1 us delay is arbitrary, and only used because this delay will
1197 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 * work there.
1199 *
1200 * wingel suggests that this could be due to failing to wait
1201 * one deskew delay.
1202 */
1203
1204 udelay(1);
1205
Finn Thainb7465452016-01-03 16:06:05 +11001206 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Finn Thainaff0cf92016-01-03 16:06:09 +11001208 /*
1209 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * selection.
1211 */
1212
Finn Thainae753a32016-01-03 16:05:23 +11001213 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1214 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001217 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1219 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001220 if (!hostdata->connected)
1221 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001222 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Finn Thainae753a32016-01-03 16:05:23 +11001225
1226 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001227 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001228 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001229 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001230 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1231 if (hostdata->selecting) {
1232 cmd->result = DID_BAD_TARGET << 16;
1233 complete_cmd(instance, cmd);
1234 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1235 cmd = NULL;
1236 }
1237 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001238 }
1239
Finn Thainaff0cf92016-01-03 16:06:09 +11001240 /*
1241 * No less than two deskew delays after the initiator detects the
1242 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 * change the DATA BUS. -wingel
1244 */
1245
1246 udelay(1);
1247
1248 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001251 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 * was true but before BSY was false during selection, the information
1253 * transfer phase should be a MESSAGE OUT phase so that we can send the
1254 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
1256
1257 /* Wait for start of REQ/ACK handshake */
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001260 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001261 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001262 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1263 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001265 goto out;
1266 }
1267 if (!hostdata->selecting) {
1268 do_abort(instance);
1269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Finn Thainb7465452016-01-03 16:06:05 +11001272 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1273 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001274 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 data = tmp;
1278 phase = PHASE_MSGOUT;
1279 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001280 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001284 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Finn Thaine9db3192016-03-23 21:10:21 +11001286#ifdef SUN3_SCSI_VME
1287 dregs->csr |= CSR_INTR;
1288#endif
1289
Boaz Harrosh28424d32007-09-10 22:37:45 +03001290 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Finn Thain707d62b2016-01-03 16:06:02 +11001292 cmd = NULL;
1293
1294out:
1295 if (!hostdata->selecting)
1296 return NULL;
1297 hostdata->selecting = NULL;
1298 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Finn Thainaff0cf92016-01-03 16:06:09 +11001301/*
1302 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001303 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 *
1305 * Purpose : transfers data in given phase using polled I/O
1306 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001307 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001308 * what phase is expected, *count - pointer to number of
1309 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001310 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001312 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001313 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001315 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 *
1317 * XXX Note : handling for bus free may be useful.
1318 */
1319
1320/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001321 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * IS 100% reliable, and for the actual data transfer where speed
1323 * counts, we will always do a pseudo DMA or DMA transfer.
1324 */
1325
Finn Thain0d2cf862016-01-03 16:06:11 +11001326static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1327 unsigned char *phase, int *count,
1328 unsigned char **data)
1329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 unsigned char p = *phase, tmp;
1331 int c = *count;
1332 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Finn Thainaff0cf92016-01-03 16:06:09 +11001334 /*
1335 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * phase specified in the appropriate bits of the TARGET COMMAND
1337 * REGISTER match the STATUS REGISTER
1338 */
1339
Finn Thain0d2cf862016-01-03 16:06:11 +11001340 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001343 /*
1344 * Wait for assertion of REQ, after which the phase bits will be
1345 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 */
1347
Finn Thain686f3992016-01-03 16:05:26 +11001348 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Finn Thainb7465452016-01-03 16:06:05 +11001351 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001354 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001355 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1356 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 break;
1358 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* Do actual transfer from SCSI bus to / from memory */
1361 if (!(p & SR_IO))
1362 NCR5380_write(OUTPUT_DATA_REG, *d);
1363 else
1364 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1365
1366 ++d;
1367
Finn Thainaff0cf92016-01-03 16:06:09 +11001368 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * The SCSI standard suggests that in MSGOUT phase, the initiator
1370 * should drop ATN on the last byte of the message phase
1371 * after REQ has been asserted for the handshake but before
1372 * the initiator raises ACK.
1373 */
1374
1375 if (!(p & SR_IO)) {
1376 if (!((p & SR_MSG) && c > 1)) {
1377 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1378 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001379 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1380 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001382 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1383 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001385 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1386 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 } else {
1389 NCR5380_dprint(NDEBUG_PIO, instance);
1390 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1391 }
1392
Finn Thaina2edc4a2016-01-03 16:05:27 +11001393 if (NCR5380_poll_politely(instance,
1394 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1395 break;
1396
Finn Thainb7465452016-01-03 16:06:05 +11001397 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001400 * We have several special cases to consider during REQ/ACK handshaking :
1401 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001402 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001404 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001405 * message. We must exit with ACK asserted, so that the calling
1406 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 *
1408 * 3. ACK and ATN are clear and the target may proceed as normal.
1409 */
1410 if (!(p == PHASE_MSGIN && c == 1)) {
1411 if (p == PHASE_MSGOUT && c > 1)
1412 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1413 else
1414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1415 }
1416 } while (--c);
1417
Finn Thainb7465452016-01-03 16:06:05 +11001418 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 *count = c;
1421 *data = d;
1422 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001423 /* The phase read from the bus is valid if either REQ is (already)
1424 * asserted or if ACK hasn't been released yet. The latter applies if
1425 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1426 */
1427 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 *phase = tmp & PHASE_MASK;
1429 else
1430 *phase = PHASE_UNKNOWN;
1431
1432 if (!c || (*phase == p))
1433 return 0;
1434 else
1435 return -1;
1436}
1437
1438/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001439 * do_reset - issue a reset command
1440 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001442 * Issue a reset sequence to the NCR5380 and try and get the bus
1443 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001445 * This clears the reset interrupt flag because there may be no handler for
1446 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1447 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001449
Finn Thain54d8fe42016-01-03 16:05:06 +11001450static void do_reset(struct Scsi_Host *instance)
1451{
Finn Thain636b1ec2016-01-03 16:05:10 +11001452 unsigned long flags;
1453
1454 local_irq_save(flags);
1455 NCR5380_write(TARGET_COMMAND_REG,
1456 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001458 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001460 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1461 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Finn Thain80d3eb62016-01-03 16:05:34 +11001464/**
1465 * do_abort - abort the currently established nexus by going to
1466 * MESSAGE OUT phase and sending an ABORT message.
1467 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001469 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
1471
Finn Thain54d8fe42016-01-03 16:05:06 +11001472static int do_abort(struct Scsi_Host *instance)
1473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned char *msgptr, phase, tmp;
1475 int len;
1476 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /* Request message out phase */
1479 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1480
Finn Thainaff0cf92016-01-03 16:06:09 +11001481 /*
1482 * Wait for the target to indicate a valid phase by asserting
1483 * REQ. Once this happens, we'll have either a MSGOUT phase
1484 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001486 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * We really don't care what value was on the bus or what value
1488 * the target sees, so we just handshake.
1489 */
1490
Finn Thain80d3eb62016-01-03 16:05:34 +11001491 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001492 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001493 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Finn Thainf35d3472016-01-03 16:05:33 +11001495 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1498
Finn Thainf35d3472016-01-03 16:05:33 +11001499 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001500 NCR5380_write(INITIATOR_COMMAND_REG,
1501 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001502 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001503 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001504 goto timeout;
1505 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 tmp = ABORT;
1509 msgptr = &tmp;
1510 len = 1;
1511 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001512 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /*
1515 * If we got here, and the command completed successfully,
1516 * we're about to go into bus free state.
1517 */
1518
1519 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001520
1521timeout:
1522 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1523 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Finn Thainaff0cf92016-01-03 16:06:09 +11001526/*
1527 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001528 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 *
1530 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001531 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001533 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001534 * what phase is expected, *count - pointer to number of
1535 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001536 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001538 * maximum number of bytes, 0 if all bytes or transferred or exit
1539 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001541 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
1543
1544
Finn Thain0d2cf862016-01-03 16:06:11 +11001545static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1546 unsigned char *phase, int *count,
1547 unsigned char **data)
1548{
1549 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 register int c = *count;
1551 register unsigned char p = *phase;
1552 register unsigned char *d = *data;
1553 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001554 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1557 *phase = tmp;
1558 return -1;
1559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Finn Thain8053b0e2016-03-23 21:10:19 +11001561 hostdata->connected->SCp.phase = p;
1562
1563 if (p & SR_IO) {
1564 if (hostdata->read_overruns)
1565 c -= hostdata->read_overruns;
1566 else if (hostdata->flags & FLAG_DMA_FIXUP)
1567 --c;
1568 }
1569
1570 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1571 (p & SR_IO) ? "receive" : "send", c, d);
1572
Finn Thaine9db3192016-03-23 21:10:21 +11001573#ifdef CONFIG_SUN3
1574 /* send start chain */
1575 sun3scsi_dma_start(c, *data);
1576#endif
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001579 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1580 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Finn Thain8053b0e2016-03-23 21:10:19 +11001582 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1583 /* On the Medusa, it is a must to initialize the DMA before
1584 * starting the NCR. This is also the cleaner way for the TT.
1585 */
1586 if (p & SR_IO)
1587 result = NCR5380_dma_recv_setup(instance, d, c);
1588 else
1589 result = NCR5380_dma_send_setup(instance, d, c);
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Finn Thainaff0cf92016-01-03 16:06:09 +11001592 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001593 * On the PAS16 at least I/O recovery delays are not needed here.
1594 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
1596
1597 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001598 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001599 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1601 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001602 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001604 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001606 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
Finn Thaine9db3192016-03-23 21:10:21 +11001609#ifdef CONFIG_SUN3
1610#ifdef SUN3_SCSI_VME
1611 dregs->csr |= CSR_DMA_ENABLE;
1612#endif
1613 sun3_dma_active = 1;
1614#endif
1615
Finn Thain8053b0e2016-03-23 21:10:19 +11001616 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1617 /* On the Falcon, the DMA setup must be done after the last
1618 * NCR access, else the DMA setup gets trashed!
1619 */
1620 if (p & SR_IO)
1621 result = NCR5380_dma_recv_setup(instance, d, c);
1622 else
1623 result = NCR5380_dma_send_setup(instance, d, c);
1624 }
1625
1626 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1627 if (result < 0)
1628 return result;
1629
1630 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1631 if (result > 0) {
1632 hostdata->dma_len = result;
1633 return 0;
1634 }
1635
1636 /* The result is zero iff pseudo DMA send/receive was completed. */
1637 hostdata->dma_len = c;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
Finn Thaine4dec682016-03-23 21:10:12 +11001640 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001641 *
1642 * For DMA sends, we want to wait until the last byte has been
1643 * transferred out over the bus before we turn off DMA mode. Alas, there
1644 * seems to be no terribly good way of doing this on a 5380 under all
1645 * conditions. For non-scatter-gather operations, we can wait until REQ
1646 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1647 * are nastier, since the device will be expecting more data than we
1648 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1649 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1650 * why this signal was added to the newer chips) but on the older 538[01]
1651 * this signal does not exist. The workaround for this lack is a watchdog;
1652 * we bail out of the wait-loop after a modest amount of wait-time if
1653 * the usual exit conditions are not met. Not a terribly clean or
1654 * correct solution :-%
1655 *
1656 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1657 * If the chip is in DMA receive mode, it will respond to a target's
1658 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1659 * ACK, even if it has _already_ been notified by the DMA controller that
1660 * the current DMA transfer has completed! If the NCR5380 is then taken
1661 * out of DMA mode, this already-acknowledged byte is lost. This is
1662 * not a problem for "one DMA transfer per READ command", because
1663 * the situation will never arise... either all of the data is DMA'ed
1664 * properly, or the target switches to MESSAGE IN phase to signal a
1665 * disconnection (either operation bringing the DMA to a clean halt).
1666 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001667 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001668 * condition before taking the NCR5380 out of DMA mode. One or two extra
1669 * bytes are transferred via PIO as necessary to fill out the original
1670 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
1672
Finn Thain8053b0e2016-03-23 21:10:19 +11001673 if (hostdata->flags & FLAG_DMA_FIXUP) {
1674 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001676 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001677 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * the chip to latch the last byte, read it, and then disable
1679 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1682 * REQ is deasserted when ACK is asserted, and not reasserted
1683 * until ACK goes false. Since the NCR5380 won't lower ACK
1684 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001685 * the DMA port or we take the NCR5380 out of DMA mode, we
1686 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * byte.
1688 */
1689
Finn Thain55181be2016-01-03 16:05:42 +11001690 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1691 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001692 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001693 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Finn Thain55181be2016-01-03 16:05:42 +11001695 if (NCR5380_poll_politely(instance, STATUS_REG,
1696 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001697 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001698 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1699 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001700 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1701 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001703 * Wait for the last byte to be sent. If REQ is being asserted for
1704 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Finn Thain55181be2016-01-03 16:05:42 +11001706 if (NCR5380_poll_politely2(instance,
1707 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1708 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001709 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001710 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001714
1715 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001716 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719/*
1720 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1721 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001722 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001723 * directs us to. Operates on the currently connected command,
1724 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
1726 * Inputs : instance, instance for which we are doing commands
1727 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001728 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001729 * modified if a command disconnects, *instance->connected will
1730 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001732 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001733 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
1735
Finn Thain0d2cf862016-01-03 16:06:11 +11001736static void NCR5380_information_transfer(struct Scsi_Host *instance)
1737{
Finn Thaine8a60142016-01-03 16:05:54 +11001738 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 unsigned char msgout = NOP;
1740 int sink = 0;
1741 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 unsigned char *data;
1744 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001745 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Finn Thaine9db3192016-03-23 21:10:21 +11001747#ifdef SUN3_SCSI_VME
1748 dregs->csr |= CSR_INTR;
1749#endif
1750
Finn Thain11d2f632016-01-03 16:05:51 +11001751 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001752 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 tmp = NCR5380_read(STATUS_REG);
1755 /* We only have a valid SCSI phase when REQ is asserted */
1756 if (tmp & SR_REQ) {
1757 phase = (tmp & PHASE_MASK);
1758 if (phase != old_phase) {
1759 old_phase = phase;
1760 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1761 }
Finn Thaine9db3192016-03-23 21:10:21 +11001762#ifdef CONFIG_SUN3
1763 if (phase == PHASE_CMDOUT) {
1764 void *d;
1765 unsigned long count;
1766
1767 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1768 count = cmd->SCp.buffer->length;
1769 d = sg_virt(cmd->SCp.buffer);
1770 } else {
1771 count = cmd->SCp.this_residual;
1772 d = cmd->SCp.ptr;
1773 }
1774
1775 if (sun3_dma_setup_done != cmd &&
1776 sun3scsi_dma_xfer_len(count, cmd) > 0) {
1777 sun3scsi_dma_setup(instance, d, count,
1778 rq_data_dir(cmd->request));
1779 sun3_dma_setup_done = cmd;
1780 }
1781#ifdef SUN3_SCSI_VME
1782 dregs->csr |= CSR_INTR;
1783#endif
1784 }
1785#endif /* CONFIG_SUN3 */
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (sink && (phase != PHASE_MSGOUT)) {
1788 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1789
Finn Thain3d07d222016-01-03 16:06:13 +11001790 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1791 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001792 while (NCR5380_read(STATUS_REG) & SR_REQ)
1793 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001794 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1795 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 sink = 0;
1797 continue;
1798 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 case PHASE_DATAOUT:
1802#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001803 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 sink = 1;
1805 do_abort(instance);
1806 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001807 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001808 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return;
1810#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001811 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001812 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * If there is no room left in the current buffer in the
1814 * scatter-gather list, move onto the next one.
1815 */
1816
1817 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1818 ++cmd->SCp.buffer;
1819 --cmd->SCp.buffers_residual;
1820 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001821 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001822 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1823 cmd->SCp.this_residual,
1824 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001828 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * PSEUDO-DMA for systems that are strictly PIO,
1830 * since we can let the hardware do the handshaking.
1831 *
1832 * For this to work, we need to know the transfersize
1833 * ahead of time, since the pseudo-DMA code will sit
1834 * in an unconditional loop.
1835 */
1836
Finn Thainff3d4572016-01-03 16:05:25 +11001837 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001838 if (!cmd->device->borken)
Finn Thainff3d4572016-01-03 16:05:25 +11001839 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Finn Thain438af512016-03-23 21:10:18 +11001841 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001843 if (NCR5380_transfer_dma(instance, &phase,
1844 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001846 * If the watchdog timer fires, all future
1847 * accesses to this device will use the
1848 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001850 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001851 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 sink = 1;
1854 do_abort(instance);
1855 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /* XXX - need to source or sink data here, as appropriate */
Finn Thain8053b0e2016-03-23 21:10:19 +11001857 }
Finn Thainf825e402016-03-23 21:10:15 +11001858 } else {
Finn Thain16788472016-02-23 10:07:05 +11001859 /* Break up transfer into 3 ms chunks,
1860 * presuming 6 accesses per handshake.
1861 */
1862 transfersize = min((unsigned long)cmd->SCp.this_residual,
1863 hostdata->accesses_per_ms / 2);
1864 len = transfersize;
1865 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001866 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001867 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001868 }
Finn Thaine9db3192016-03-23 21:10:21 +11001869#ifdef CONFIG_SUN3
1870 if (sun3_dma_setup_done == cmd)
1871 sun3_dma_setup_done = NULL;
1872#endif
Finn Thain16788472016-02-23 10:07:05 +11001873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 case PHASE_MSGIN:
1875 len = 1;
1876 data = &tmp;
1877 NCR5380_transfer_pio(instance, &phase, &len, &data);
1878 cmd->SCp.Message = tmp;
1879
1880 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 case ABORT:
1882 case COMMAND_COMPLETE:
1883 /* Accept message by clearing ACK */
1884 sink = 1;
1885 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001886 dsprintk(NDEBUG_QUEUES, instance,
1887 "COMMAND COMPLETE %p target %d lun %llu\n",
1888 cmd, scmd_id(cmd), cmd->device->lun);
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Finn Thainf27db8e2016-01-03 16:06:00 +11001892 cmd->result &= ~0xffff;
1893 cmd->result |= cmd->SCp.Status;
1894 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Finn Thainf27db8e2016-01-03 16:06:00 +11001896 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001897 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001898 else {
1899 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1900 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1901 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1902 cmd);
1903 list_add_tail(&ncmd->list,
1904 &hostdata->autosense);
1905 } else
1906 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
Finn Thainaff0cf92016-01-03 16:06:09 +11001909 /*
1910 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 * arbitration can resume.
1912 */
1913 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001914
1915 /* Enable reselect interrupts */
1916 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain52d3e562016-03-23 21:10:20 +11001917
1918 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return;
1920 case MESSAGE_REJECT:
1921 /* Accept message by clearing ACK */
1922 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1923 switch (hostdata->last_message) {
1924 case HEAD_OF_QUEUE_TAG:
1925 case ORDERED_QUEUE_TAG:
1926 case SIMPLE_QUEUE_TAG:
1927 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001928 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
1930 default:
1931 break;
1932 }
Finn Thain340b9612016-01-03 16:05:31 +11001933 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001934 case DISCONNECT:
1935 /* Accept message by clearing ACK */
1936 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1937 hostdata->connected = NULL;
1938 list_add(&ncmd->list, &hostdata->disconnected);
1939 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1940 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1941 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001942
Finn Thain0d2cf862016-01-03 16:06:11 +11001943 /*
1944 * Restore phase bits to 0 so an interrupted selection,
1945 * arbitration can resume.
1946 */
1947 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Finn Thain0d2cf862016-01-03 16:06:11 +11001949 /* Enable reselect interrupts */
1950 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine9db3192016-03-23 21:10:21 +11001951#ifdef SUN3_SCSI_VME
1952 dregs->csr |= CSR_DMA_ENABLE;
1953#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001954 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001955 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001957 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * ignore SAVE/RESTORE pointers calls.
1959 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001960 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001962 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * compatible.
1964 */
1965 case SAVE_POINTERS:
1966 case RESTORE_POINTERS:
1967 /* Accept message by clearing ACK */
1968 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1969 break;
1970 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001971 /*
1972 * Start the message buffer with the EXTENDED_MESSAGE
1973 * byte, since spi_print_msg() wants the whole thing.
1974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 extended_msg[0] = EXTENDED_MESSAGE;
1976 /* Accept first byte by clearing ACK */
1977 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001978
1979 spin_unlock_irq(&hostdata->lock);
1980
Finn Thainb7465452016-01-03 16:06:05 +11001981 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 len = 2;
1984 data = extended_msg + 1;
1985 phase = PHASE_MSGIN;
1986 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001987 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1988 (int)extended_msg[1],
1989 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Finn Thaine0783ed2016-01-03 16:05:45 +11001991 if (!len && extended_msg[1] > 0 &&
1992 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Accept third byte by clearing ACK */
1994 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1995 len = extended_msg[1] - 1;
1996 data = extended_msg + 3;
1997 phase = PHASE_MSGIN;
1998
1999 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11002000 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
2001 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 switch (extended_msg[2]) {
2004 case EXTENDED_SDTR:
2005 case EXTENDED_WDTR:
2006 case EXTENDED_MODIFY_DATA_POINTER:
2007 case EXTENDED_EXTENDED_IDENTIFY:
2008 tmp = 0;
2009 }
2010 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002011 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 tmp = 0;
2013 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002014 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2015 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 tmp = 0;
2017 }
Finn Thain11d2f632016-01-03 16:05:51 +11002018
2019 spin_lock_irq(&hostdata->lock);
2020 if (!hostdata->connected)
2021 return;
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Fall through to reject message */
2024
Finn Thainaff0cf92016-01-03 16:06:09 +11002025 /*
2026 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * reject it.
2028 */
2029 default:
2030 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002031 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002032 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 printk("\n");
2034 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002035 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11002036 "rejecting unknown message %02x\n",
2037 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002039 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11002040 "rejecting unknown extended message code %02x, length %d\n",
2041 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 msgout = MESSAGE_REJECT;
2044 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2045 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11002046 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 break;
2048 case PHASE_MSGOUT:
2049 len = 1;
2050 data = &msgout;
2051 hostdata->last_message = msgout;
2052 NCR5380_transfer_pio(instance, &phase, &len, &data);
2053 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 hostdata->connected = NULL;
2055 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002056 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11002057 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2059 return;
2060 }
2061 msgout = NOP;
2062 break;
2063 case PHASE_CMDOUT:
2064 len = cmd->cmd_len;
2065 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11002066 /*
2067 * XXX for performance reasons, on machines with a
2068 * PSEUDO-DMA architecture we should probably
2069 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 */
2071 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 break;
2073 case PHASE_STATIN:
2074 len = 1;
2075 data = &tmp;
2076 NCR5380_transfer_pio(instance, &phase, &len, &data);
2077 cmd->SCp.Status = tmp;
2078 break;
2079 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002080 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11002081 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11002082 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002083 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002084 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002085 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002086 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Finn Thain11d2f632016-01-03 16:05:51 +11002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/*
2092 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2093 *
Finn Thainaff0cf92016-01-03 16:06:09 +11002094 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002095 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2096 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11002097 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
2100
Finn Thain0d2cf862016-01-03 16:06:11 +11002101static void NCR5380_reselect(struct Scsi_Host *instance)
2102{
Finn Thaine8a60142016-01-03 16:05:54 +11002103 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002105 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002107 struct NCR5380_cmd *ncmd;
2108 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /*
2111 * Disable arbitration, etc. since the host adapter obviously
2112 * lost, and tell an interrupted NCR5380_select() to restart.
2113 */
2114
2115 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11002118
2119 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Finn Thainaff0cf92016-01-03 16:06:09 +11002121 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 * At this point, we have detected that our SCSI ID is on the bus,
2123 * SEL is true and BSY was false for at least one bus settle delay
2124 * (400 ns).
2125 *
2126 * We must assert BSY ourselves, until the target drops the SEL
2127 * signal.
2128 */
2129
2130 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002131 if (NCR5380_poll_politely(instance,
2132 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2133 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2134 return;
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2137
2138 /*
2139 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 */
2141
Finn Thain1cc160e2016-01-03 16:05:32 +11002142 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002143 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2144 do_abort(instance);
2145 return;
2146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Finn Thaine9db3192016-03-23 21:10:21 +11002148#ifdef CONFIG_SUN3
2149 /* acknowledge toggle to MSGIN */
2150 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Finn Thaine9db3192016-03-23 21:10:21 +11002152 /* peek at the byte without really hitting the bus */
2153 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2154#else
2155 {
2156 int len = 1;
2157 unsigned char *data = msg;
2158 unsigned char phase = PHASE_MSGIN;
2159
2160 NCR5380_transfer_pio(instance, &phase, &len, &data);
2161
2162 if (len) {
2163 do_abort(instance);
2164 return;
2165 }
Finn Thain72064a72016-01-03 16:05:44 +11002166 }
Finn Thaine9db3192016-03-23 21:10:21 +11002167#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002170 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002171 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002172 printk("\n");
2173 do_abort(instance);
2174 return;
2175 }
2176 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Finn Thain72064a72016-01-03 16:05:44 +11002178 /*
2179 * We need to add code for SCSI-II to track which devices have
2180 * I_T_L_Q nexuses established, and which have simple I_T_L
2181 * nexuses so we can chose to do additional data transfer.
2182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Finn Thain72064a72016-01-03 16:05:44 +11002184 /*
2185 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2186 * just reestablished, and remove it from the disconnected queue.
2187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Finn Thain32b26a12016-01-03 16:05:58 +11002189 tmp = NULL;
2190 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2191 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2192
2193 if (target_mask == (1 << scmd_id(cmd)) &&
2194 lun == (u8)cmd->device->lun) {
2195 list_del(&ncmd->list);
2196 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002200
2201 if (tmp) {
2202 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2203 "reselect: removed %p from disconnected queue\n", tmp);
2204 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002205 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2206 target_mask, lun);
2207 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002208 * Since we have an established nexus that we can't do anything
2209 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002212 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
Finn Thain72064a72016-01-03 16:05:44 +11002214
Finn Thaine9db3192016-03-23 21:10:21 +11002215#ifdef CONFIG_SUN3
2216 {
2217 void *d;
2218 unsigned long count;
2219
2220 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2221 count = tmp->SCp.buffer->length;
2222 d = sg_virt(tmp->SCp.buffer);
2223 } else {
2224 count = tmp->SCp.this_residual;
2225 d = tmp->SCp.ptr;
2226 }
2227
2228 if (sun3_dma_setup_done != tmp &&
2229 sun3scsi_dma_xfer_len(count, tmp) > 0) {
2230 sun3scsi_dma_setup(instance, d, count,
2231 rq_data_dir(tmp->request));
2232 sun3_dma_setup_done = tmp;
2233 }
2234 }
2235
2236 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2237#endif /* CONFIG_SUN3 */
2238
Finn Thain72064a72016-01-03 16:05:44 +11002239 /* Accept message by clearing ACK */
2240 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2241
2242 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002243 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2244 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Finn Thain8b00c3d2016-01-03 16:06:01 +11002247/**
2248 * list_find_cmd - test for presence of a command in a linked list
2249 * @haystack: list of commands
2250 * @needle: command to search for
2251 */
2252
2253static bool list_find_cmd(struct list_head *haystack,
2254 struct scsi_cmnd *needle)
2255{
2256 struct NCR5380_cmd *ncmd;
2257
2258 list_for_each_entry(ncmd, haystack, list)
2259 if (NCR5380_to_scmd(ncmd) == needle)
2260 return true;
2261 return false;
2262}
2263
2264/**
2265 * list_remove_cmd - remove a command from linked list
2266 * @haystack: list of commands
2267 * @needle: command to remove
2268 */
2269
2270static bool list_del_cmd(struct list_head *haystack,
2271 struct scsi_cmnd *needle)
2272{
2273 if (list_find_cmd(haystack, needle)) {
2274 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2275
2276 list_del(&ncmd->list);
2277 return true;
2278 }
2279 return false;
2280}
2281
2282/**
2283 * NCR5380_abort - scsi host eh_abort_handler() method
2284 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002286 * Try to abort a given command by removing it from queues and/or sending
2287 * the target an abort message. This may not succeed in causing a target
2288 * to abort the command. Nonetheless, the low-level driver must forget about
2289 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002291 * The normal path taken by a command is as follows. For EH we trace this
2292 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002294 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2295 * [disconnected -> connected ->]...
2296 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002298 * If cmd was not found at all then presumably it has already been completed,
2299 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002300 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002301 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002302 * We have no option but to forget the aborted command (even if it still
2303 * lacks sense data). The mid-layer may re-issue a command that is in error
2304 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2305 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002306 *
2307 * The lock protects driver data structures, but EH handlers also use it
2308 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 */
2310
Finn Thain710ddd02014-11-12 16:12:02 +11002311static int NCR5380_abort(struct scsi_cmnd *cmd)
2312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002314 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002315 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002316 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002317
Finn Thain11d2f632016-01-03 16:05:51 +11002318 spin_lock_irqsave(&hostdata->lock, flags);
2319
Finn Thain32b26a12016-01-03 16:05:58 +11002320#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002321 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002322#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002323 NCR5380_dprint(NDEBUG_ANY, instance);
2324 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Finn Thain8b00c3d2016-01-03 16:06:01 +11002326 if (list_del_cmd(&hostdata->unissued, cmd)) {
2327 dsprintk(NDEBUG_ABORT, instance,
2328 "abort: removed %p from issue queue\n", cmd);
2329 cmd->result = DID_ABORT << 16;
2330 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002331 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002332 }
2333
Finn Thain707d62b2016-01-03 16:06:02 +11002334 if (hostdata->selecting == cmd) {
2335 dsprintk(NDEBUG_ABORT, instance,
2336 "abort: cmd %p == selecting\n", cmd);
2337 hostdata->selecting = NULL;
2338 cmd->result = DID_ABORT << 16;
2339 complete_cmd(instance, cmd);
2340 goto out;
2341 }
2342
Finn Thain8b00c3d2016-01-03 16:06:01 +11002343 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2344 dsprintk(NDEBUG_ABORT, instance,
2345 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002346 /* Can't call NCR5380_select() and send ABORT because that
2347 * means releasing the lock. Need a bus reset.
2348 */
Finn Thaindc183962016-02-23 10:07:07 +11002349 set_host_byte(cmd, DID_ERROR);
2350 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002351 result = FAILED;
2352 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002353 }
2354
2355 if (hostdata->connected == cmd) {
2356 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2357 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002358 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002359 if (do_abort(instance)) {
2360 set_host_byte(cmd, DID_ERROR);
2361 complete_cmd(instance, cmd);
2362 result = FAILED;
2363 goto out;
2364 }
2365 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002366 complete_cmd(instance, cmd);
2367 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002368 }
2369
Finn Thaindc183962016-02-23 10:07:07 +11002370 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002371 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002372 "abort: removed %p from sense queue\n", cmd);
2373 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002374 complete_cmd(instance, cmd);
2375 }
2376
2377out:
2378 if (result == FAILED)
2379 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2380 else
2381 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2382
2383 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002384 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002385 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002386
Finn Thain8b00c3d2016-01-03 16:06:01 +11002387 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
2390
Finn Thain3be1b3e2016-01-03 16:05:12 +11002391/**
2392 * NCR5380_bus_reset - reset the SCSI bus
2393 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002395 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
2397
Finn Thain710ddd02014-11-12 16:12:02 +11002398static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002399{
2400 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002401 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002402 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002403 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002404 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Finn Thain11d2f632016-01-03 16:05:51 +11002406 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002407
2408#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002409 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002410#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002411 NCR5380_dprint(NDEBUG_ANY, instance);
2412 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002413
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002414 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002415
Finn Thain62717f52016-01-03 16:06:03 +11002416 /* reset NCR registers */
2417 NCR5380_write(MODE_REG, MR_BASE);
2418 NCR5380_write(TARGET_COMMAND_REG, 0);
2419 NCR5380_write(SELECT_ENABLE_REG, 0);
2420
2421 /* After the reset, there are no more connected or disconnected commands
2422 * and no busy units; so clear the low-level status here to avoid
2423 * conflicts when the mid-level code tries to wake up the affected
2424 * commands!
2425 */
2426
Finn Thain1884c282016-02-23 10:07:04 +11002427 if (list_del_cmd(&hostdata->unissued, cmd)) {
2428 cmd->result = DID_RESET << 16;
2429 cmd->scsi_done(cmd);
2430 }
2431
2432 if (hostdata->selecting) {
2433 hostdata->selecting->result = DID_RESET << 16;
2434 complete_cmd(instance, hostdata->selecting);
2435 hostdata->selecting = NULL;
2436 }
Finn Thain62717f52016-01-03 16:06:03 +11002437
2438 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2439 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2440
2441 set_host_byte(cmd, DID_RESET);
2442 cmd->scsi_done(cmd);
2443 }
Finn Thain1884c282016-02-23 10:07:04 +11002444 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002445
2446 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2447 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2448
2449 set_host_byte(cmd, DID_RESET);
2450 cmd->scsi_done(cmd);
2451 }
Finn Thain1884c282016-02-23 10:07:04 +11002452 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002453
2454 if (hostdata->connected) {
2455 set_host_byte(hostdata->connected, DID_RESET);
2456 complete_cmd(instance, hostdata->connected);
2457 hostdata->connected = NULL;
2458 }
2459
Finn Thain62717f52016-01-03 16:06:03 +11002460 for (i = 0; i < 8; ++i)
2461 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002462 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002463
2464 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002465 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002466 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return SUCCESS;
2469}