blob: 014a01f6875fc5bd539b87edacf6fec3e868aa94 [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
32/*
Finn Thainaff0cf92016-01-03 16:06:09 +110033 * Further development / testing that should be done :
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
Finn Thain594d4ba2016-01-03 16:06:10 +110035 * code so that everything does the same thing that's done at the
36 * end of a pseudo-DMA read operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Finn Thainaff0cf92016-01-03 16:06:09 +110038 * 4. Test SCSI-II tagged queueing (I have no devices which support
Finn Thain594d4ba2016-01-03 16:06:10 +110039 * tagged queueing)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * Design
44 *
Finn Thainaff0cf92016-01-03 16:06:09 +110045 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * one simply writes appropriate system specific macros (ie, data
Finn Thainaff0cf92016-01-03 16:06:09 +110047 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * memory mapped) and drops this file in their 'C' wrapper.
49 *
Finn Thainaff0cf92016-01-03 16:06:09 +110050 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * each 5380 in the system - commands that haven't been issued yet,
Finn Thainaff0cf92016-01-03 16:06:09 +110052 * and commands that are currently executing. This means that an
53 * unlimited number of commands may be queued, letting
54 * more commands propagate from the higher driver levels giving higher
55 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
56 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * while a command is already executing.
58 *
59 *
Finn Thainaff0cf92016-01-03 16:06:09 +110060 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
Finn Thainaff0cf92016-01-03 16:06:09 +110062 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
63 * piece of hardware that requires you to sit in a loop polling for
64 * the REQ signal as long as you are connected. Some devices are
65 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110066 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * broken devices are the exception rather than the rule and I'd rather
68 * spend my time optimizing for the normal case.
69 *
70 * Architecture :
71 *
72 * At the heart of the design is a coroutine, NCR5380_main,
73 * which is started from a workqueue for each NCR5380 host in the
74 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
75 * removing the commands from the issue queue and calling
Finn Thainaff0cf92016-01-03 16:06:09 +110076 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * Once a nexus is established, the NCR5380_information_transfer()
79 * phase goes through the various phases as instructed by the target.
80 * if the target goes into MSG IN and sends a DISCONNECT message,
81 * the command structure is placed into the per instance disconnected
Finn Thainaff0cf92016-01-03 16:06:09 +110082 * queue, and NCR5380_main tries to find more work. If the target is
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * idle for too long, the system will try to sleep.
84 *
85 * If a command has disconnected, eventually an interrupt will trigger,
86 * calling NCR5380_intr() which will in turn call NCR5380_reselect
87 * to reestablish a nexus. This will run main if necessary.
88 *
Finn Thainaff0cf92016-01-03 16:06:09 +110089 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * appropriate.
91 *
Finn Thainaff0cf92016-01-03 16:06:09 +110092 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * structures, being initialized after the command is connected
94 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
95 * Note that in violation of the standard, an implicit SAVE POINTERS operation
96 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
97 */
98
99/*
100 * Using this file :
101 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Finn Thainaff0cf92016-01-03 16:06:09 +1100102 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * and macros and include this file in your driver.
104 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100105 * These macros control options :
106 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
Finn Thain594d4ba2016-01-03 16:06:10 +1100107 * defined.
Finn Thainaff0cf92016-01-03 16:06:09 +1100108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Finn Thain594d4ba2016-01-03 16:06:10 +1100110 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
112 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
Finn Thain594d4ba2016-01-03 16:06:10 +1100113 * transceivers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
115 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
Finn Thain594d4ba2016-01-03 16:06:10 +1100116 * override-configure an IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * NCR5380_read(register) - read from the specified register
123 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100124 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100126 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100127 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 *
129 * Either real DMA *or* pseudo DMA may be implemented
Finn Thainaff0cf92016-01-03 16:06:09 +1100130 * Note that the DMA setup functions should return the number of bytes
Finn Thain594d4ba2016-01-03 16:06:10 +1100131 * that they were able to program the controller for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * NCR5380_dma_write_setup(instance, src, count) - initialize
134 * NCR5380_dma_read_setup(instance, dst, count) - initialize
135 * NCR5380_dma_residual(instance); - residual count
136 *
137 * PSEUDO functions :
138 * NCR5380_pwrite(instance, src, count)
139 * NCR5380_pread(instance, dst, count);
140 *
141 * The generic driver is initialized by calling NCR5380_init(instance),
Finn Thainaff0cf92016-01-03 16:06:09 +1100142 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
144 * possible) function may be used.
145 */
146
Finn Thaine5d55d12016-03-23 21:10:16 +1100147#ifndef NCR5380_io_delay
148#define NCR5380_io_delay(x)
149#endif
150
Finn Thain54d8fe42016-01-03 16:05:06 +1100151static int do_abort(struct Scsi_Host *);
152static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Finn Thainc16df322016-01-03 16:06:08 +1100154/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100155 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100156 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100158 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
160
Finn Thain710ddd02014-11-12 16:12:02 +1100161static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Finn Thainaff0cf92016-01-03 16:06:09 +1100163 /*
164 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * various queues are valid.
166 */
167
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200168 if (scsi_bufflen(cmd)) {
169 cmd->SCp.buffer = scsi_sglist(cmd);
170 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200171 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 cmd->SCp.this_residual = cmd->SCp.buffer->length;
173 } else {
174 cmd->SCp.buffer = NULL;
175 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200176 cmd->SCp.ptr = NULL;
177 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100179
180 cmd->SCp.Status = 0;
181 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/**
Finn Thainb32ade12016-01-03 16:05:41 +1100185 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100186 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100187 * @reg1: 5380 register to poll
188 * @bit1: Bitmask to check
189 * @val1: Expected value
190 * @reg2: Second 5380 register to poll
191 * @bit2: Second bitmask to check
192 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100193 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Finn Thain2f854b82016-01-03 16:05:22 +1100195 * Polls the chip in a reasonably efficient manner waiting for an
196 * event to occur. After a short quick poll we begin to yield the CPU
197 * (if possible). In irq contexts the time-out is arbitrarily limited.
198 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
Finn Thainb32ade12016-01-03 16:05:41 +1100200 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Finn Thainb32ade12016-01-03 16:05:41 +1100203static int NCR5380_poll_politely2(struct Scsi_Host *instance,
204 int reg1, int bit1, int val1,
205 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100206{
207 struct NCR5380_hostdata *hostdata = shost_priv(instance);
208 unsigned long deadline = jiffies + wait;
209 unsigned long n;
210
211 /* Busy-wait for up to 10 ms */
212 n = min(10000U, jiffies_to_usecs(wait));
213 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100214 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100215 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100216 if ((NCR5380_read(reg1) & bit1) == val1)
217 return 0;
218 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100221 } while (n--);
222
223 if (irqs_disabled() || in_interrupt())
224 return -ETIMEDOUT;
225
226 /* Repeatedly sleep for 1 ms until deadline */
227 while (time_is_after_jiffies(deadline)) {
228 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100229 if ((NCR5380_read(reg1) & bit1) == val1)
230 return 0;
231 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Finn Thain2f854b82016-01-03 16:05:22 +1100234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -ETIMEDOUT;
236}
237
Finn Thainb32ade12016-01-03 16:05:41 +1100238static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
239 int reg, int bit, int val, int wait)
240{
241 return NCR5380_poll_politely2(instance, reg, bit, val,
242 reg, bit, val, wait);
243}
244
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100245#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static struct {
247 unsigned char mask;
248 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100249} signals[] = {
250 {SR_DBP, "PARITY"},
251 {SR_RST, "RST"},
252 {SR_BSY, "BSY"},
253 {SR_REQ, "REQ"},
254 {SR_MSG, "MSG"},
255 {SR_CD, "CD"},
256 {SR_IO, "IO"},
257 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100259},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260basrs[] = {
Finn Thainaff0cf92016-01-03 16:06:09 +1100261 {BASR_ATN, "ATN"},
262 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100264},
265icrs[] = {
266 {ICR_ASSERT_RST, "ASSERT RST"},
267 {ICR_ASSERT_ACK, "ASSERT ACK"},
268 {ICR_ASSERT_BSY, "ASSERT BSY"},
269 {ICR_ASSERT_SEL, "ASSERT SEL"},
270 {ICR_ASSERT_ATN, "ASSERT ATN"},
271 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100273},
274mrs[] = {
275 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
276 {MR_TARGET, "MODE TARGET"},
277 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
278 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
Finn Thain0d2cf862016-01-03 16:06:11 +1100279 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100280 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
281 {MR_DMA_MODE, "MODE DMA"},
282 {MR_ARBITRATE, "MODE ARBITRATION"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 {0, NULL}
284};
285
286/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100287 * NCR5380_print - print scsi bus signals
288 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100290 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292
293static void NCR5380_print(struct Scsi_Host *instance)
294{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
298 status = NCR5380_read(STATUS_REG);
299 mr = NCR5380_read(MODE_REG);
300 icr = NCR5380_read(INITIATOR_COMMAND_REG);
301 basr = NCR5380_read(BUS_AND_STATUS_REG);
302
303 printk("STATUS_REG: %02x ", status);
304 for (i = 0; signals[i].mask; ++i)
305 if (status & signals[i].mask)
306 printk(",%s", signals[i].name);
307 printk("\nBASR: %02x ", basr);
308 for (i = 0; basrs[i].mask; ++i)
309 if (basr & basrs[i].mask)
310 printk(",%s", basrs[i].name);
311 printk("\nICR: %02x ", icr);
312 for (i = 0; icrs[i].mask; ++i)
313 if (icr & icrs[i].mask)
314 printk(",%s", icrs[i].name);
315 printk("\nMODE: %02x ", mr);
316 for (i = 0; mrs[i].mask; ++i)
317 if (mr & mrs[i].mask)
318 printk(",%s", mrs[i].name);
319 printk("\n");
320}
321
Finn Thain0d2cf862016-01-03 16:06:11 +1100322static struct {
323 unsigned char value;
324 const char *name;
325} phases[] = {
326 {PHASE_DATAOUT, "DATAOUT"},
327 {PHASE_DATAIN, "DATAIN"},
328 {PHASE_CMDOUT, "CMDOUT"},
329 {PHASE_STATIN, "STATIN"},
330 {PHASE_MSGOUT, "MSGOUT"},
331 {PHASE_MSGIN, "MSGIN"},
332 {PHASE_UNKNOWN, "UNKNOWN"}
333};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Finn Thainc16df322016-01-03 16:06:08 +1100335/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100336 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100337 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100339 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341
342static void NCR5380_print_phase(struct Scsi_Host *instance)
343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned char status;
345 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 status = NCR5380_read(STATUS_REG);
348 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100349 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100351 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
352 (phases[i].value != (status & PHASE_MASK)); ++i)
353 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100354 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356}
357#endif
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Finn Thaind5f7e652016-01-03 16:05:03 +1100360static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100363 * probe_intr - helper for IRQ autoprobe
364 * @irq: interrupt number
365 * @dev_id: unused
366 * @regs: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100368 * Set a flag to indicate the IRQ in question was received. This is
369 * used by the IRQ probe code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100371
David Howells7d12e782006-10-05 14:55:46 +0100372static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 probe_irq = irq;
375 return IRQ_HANDLED;
376}
377
378/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100379 * NCR5380_probe_irq - find the IRQ of an NCR5380
380 * @instance: NCR5380 controller
381 * @possible: bitmask of ISA IRQ lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100383 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
384 * and then looking to see what interrupt actually turned up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
386
Andrew Morton702809c2007-05-23 14:41:56 -0700387static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
388 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Finn Thaine8a60142016-01-03 16:05:54 +1100390 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned long timeout;
392 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Finn Thain22f5f102014-11-12 16:11:56 +1100394 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100395 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 trying_irqs |= mask;
397
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500398 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100399 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * A interrupt is triggered whenever BSY = false, SEL = true
Finn Thainaff0cf92016-01-03 16:06:09 +1100403 * and a bit set in the SELECT_ENABLE_REG is asserted on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * SCSI bus.
405 *
406 * Note that the bus is only driven when the phase control signals
407 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
408 * to zero.
409 */
410
411 NCR5380_write(TARGET_COMMAND_REG, 0);
412 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
413 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
415
Finn Thain22f5f102014-11-12 16:11:56 +1100416 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800417 schedule_timeout_uninterruptible(1);
Finn Thainaff0cf92016-01-03 16:06:09 +1100418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 NCR5380_write(SELECT_ENABLE_REG, 0);
420 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
421
Finn Thain22f5f102014-11-12 16:11:56 +1100422 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (trying_irqs & mask)
424 free_irq(i, NULL);
425
426 return probe_irq;
427}
428
429/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100430 * NCR58380_info - report driver and host information
431 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100433 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435
Finn Thain8c325132014-11-12 16:11:58 +1100436static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Finn Thain8c325132014-11-12 16:11:58 +1100438 struct NCR5380_hostdata *hostdata = shost_priv(instance);
439
440 return hostdata->info;
441}
442
443static void prepare_info(struct Scsi_Host *instance)
444{
445 struct NCR5380_hostdata *hostdata = shost_priv(instance);
446
447 snprintf(hostdata->info, sizeof(hostdata->info),
448 "%s, io_port 0x%lx, n_io_port %d, "
449 "base 0x%lx, irq %d, "
450 "can_queue %d, cmd_per_lun %d, "
451 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100452 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100453 "options { %s} ",
454 instance->hostt->name, instance->io_port, instance->n_io_port,
455 instance->base, instance->irq,
456 instance->can_queue, instance->cmd_per_lun,
457 instance->sg_tablesize, instance->this_id,
Finn Thain1bb46002016-03-23 21:10:14 +1100458 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100459 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100460 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100462 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100465 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100468 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#endif
Finn Thain8c325132014-11-12 16:11:58 +1100470 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100474 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100475 * @instance: adapter to configure
476 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100478 * Initializes *instance and corresponding 5380 chip,
479 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100481 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100482 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100484 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
486
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800487static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Finn Thaine8a60142016-01-03 16:05:54 +1100489 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100490 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100491 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
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 Thain11d2f632016-01-03 16:05:51 +1100668 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100669
Finn Thainaff0cf92016-01-03 16:06:09 +1100670 /*
671 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100673 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * sense data is only guaranteed to be valid while the condition exists.
675 */
676
Finn Thain32b26a12016-01-03 16:05:58 +1100677 if (cmd->cmnd[0] == REQUEST_SENSE)
678 list_add(&ncmd->list, &hostdata->unissued);
679 else
680 list_add_tail(&ncmd->list, &hostdata->unissued);
681
Finn Thain11d2f632016-01-03 16:05:51 +1100682 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100683
Finn Thaindbb6b352016-01-03 16:05:53 +1100684 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
685 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100688 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100693 * dequeue_next_cmd - dequeue a command for processing
694 * @instance: the scsi host instance
695 *
696 * Priority is given to commands on the autosense queue. These commands
697 * need autosense because of a CHECK CONDITION result.
698 *
699 * Returns a command pointer if a command is found for a target that is
700 * not already busy. Otherwise returns NULL.
701 */
702
703static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
704{
705 struct NCR5380_hostdata *hostdata = shost_priv(instance);
706 struct NCR5380_cmd *ncmd;
707 struct scsi_cmnd *cmd;
708
Finn Thain8d5dbec2016-02-23 10:07:09 +1100709 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100710 list_for_each_entry(ncmd, &hostdata->unissued, list) {
711 cmd = NCR5380_to_scmd(ncmd);
712 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
713 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
714
715 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
716 list_del(&ncmd->list);
717 dsprintk(NDEBUG_QUEUES, instance,
718 "dequeue: removed %p from issue queue\n", cmd);
719 return cmd;
720 }
721 }
722 } else {
723 /* Autosense processing begins here */
724 ncmd = list_first_entry(&hostdata->autosense,
725 struct NCR5380_cmd, list);
726 list_del(&ncmd->list);
727 cmd = NCR5380_to_scmd(ncmd);
728 dsprintk(NDEBUG_QUEUES, instance,
729 "dequeue: removed %p from autosense queue\n", cmd);
730 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
731 hostdata->sensing = cmd;
732 return cmd;
733 }
734 return NULL;
735}
736
737static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
738{
739 struct NCR5380_hostdata *hostdata = shost_priv(instance);
740 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
741
Finn Thain8d5dbec2016-02-23 10:07:09 +1100742 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100743 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
744 list_add(&ncmd->list, &hostdata->autosense);
745 hostdata->sensing = NULL;
746 } else
747 list_add(&ncmd->list, &hostdata->unissued);
748}
749
750/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100751 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100753 * NCR5380_main is a coroutine that runs as long as more work can
754 * be done on the NCR5380 host adapters in a system. Both
755 * NCR5380_queue_command() and NCR5380_intr() will try to start it
756 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758
David Howellsc4028952006-11-22 14:57:56 +0000759static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
David Howellsc4028952006-11-22 14:57:56 +0000761 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100762 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100768
Finn Thain0a4e3612016-01-03 16:06:07 +1100769 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100770 while (!hostdata->connected && !hostdata->selecting) {
771 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
772
773 if (!cmd)
774 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100775
776 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100779 * Attempt to establish an I_T_L nexus here.
780 * On success, instance->hostdata->connected is set.
781 * On failure, we must add the command back to the
782 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100784 /*
785 * REQUEST SENSE commands are issued without tagged
786 * queueing, even on SCSI-II devices because the
787 * contingent allegiance condition exists for the
788 * entire unit.
789 */
Finn Thain32b26a12016-01-03 16:05:58 +1100790
Finn Thainccf6efd2016-02-23 10:07:08 +1100791 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100792 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +1100793 } else {
794 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
795 "main: select failed, returning %p to queue\n", cmd);
796 requeue_cmd(instance, cmd);
797 }
798 }
Finn Thaine4dec682016-03-23 21:10:12 +1100799 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100800 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100803 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100804 spin_unlock_irq(&hostdata->lock);
805 if (!done)
806 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810#ifndef DONT_USE_INTR
811
812/**
Finn Thaincd400822016-01-03 16:05:40 +1100813 * NCR5380_intr - generic NCR5380 irq handler
814 * @irq: interrupt number
815 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
Finn Thaincd400822016-01-03 16:05:40 +1100817 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
818 * from the disconnected queue, and restarting NCR5380_main()
819 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
Finn Thaincd400822016-01-03 16:05:40 +1100821 * The chip can assert IRQ in any of six different conditions. The IRQ flag
822 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
823 * Three of these six conditions are latched in the Bus and Status Register:
824 * - End of DMA (cleared by ending DMA Mode)
825 * - Parity error (cleared by reading RPIR)
826 * - Loss of BSY (cleared by reading RPIR)
827 * Two conditions have flag bits that are not latched:
828 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
829 * - Bus reset (non-maskable)
830 * The remaining condition has no flag bit at all:
831 * - Selection/reselection
832 *
833 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
834 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
835 * claimed that "the design of the [DP8490] interrupt logic ensures
836 * interrupts will not be lost (they can be on the DP5380)."
837 * The L5380/53C80 datasheet from LOGIC Devices has more details.
838 *
839 * Checking for bus reset by reading RST is futile because of interrupt
840 * latency, but a bus reset will reset chip logic. Checking for parity error
841 * is unnecessary because that interrupt is never enabled. A Loss of BSY
842 * condition will clear DMA Mode. We can tell when this occurs because the
843 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
845
Finn Thaincd400822016-01-03 16:05:40 +1100846static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800848 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100849 struct NCR5380_hostdata *hostdata = shost_priv(instance);
850 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned char basr;
852 unsigned long flags;
853
Finn Thain11d2f632016-01-03 16:05:51 +1100854 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Finn Thaincd400822016-01-03 16:05:40 +1100856 basr = NCR5380_read(BUS_AND_STATUS_REG);
857 if (basr & BASR_IRQ) {
858 unsigned char mr = NCR5380_read(MODE_REG);
859 unsigned char sr = NCR5380_read(STATUS_REG);
860
Finn Thainb7465452016-01-03 16:06:05 +1100861 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
862 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100863
Finn Thaincd400822016-01-03 16:05:40 +1100864 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
865 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
866 /* Probably reselected */
867 NCR5380_write(SELECT_ENABLE_REG, 0);
868 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
869
Finn Thainb7465452016-01-03 16:06:05 +1100870 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100871
872 if (!hostdata->connected) {
873 NCR5380_reselect(instance);
874 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Finn Thaincd400822016-01-03 16:05:40 +1100876 if (!hostdata->connected)
877 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
878 } else {
879 /* Probably Bus Reset */
880 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
881
Finn Thainb7465452016-01-03 16:06:05 +1100882 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +1100883 }
884 handled = 1;
885 } else {
886 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
887 }
888
Finn Thain11d2f632016-01-03 16:05:51 +1100889 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100890
891 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Finn Thainaff0cf92016-01-03 16:06:09 +1100894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Finn Thainaff0cf92016-01-03 16:06:09 +1100896/*
Finn Thain710ddd02014-11-12 16:12:02 +1100897 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +1100898 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
900 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +1100901 * including ARBITRATION, SELECTION, and initial message out for
902 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100904 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +1100905 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +1100906 *
Finn Thain707d62b2016-01-03 16:06:02 +1100907 * Returns cmd if selection failed but should be retried,
908 * NULL if selection failed and should not be retried, or
909 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100911 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100912 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
913 * with registers as they should have been on entry - ie
914 * SELECT_ENABLE will be set appropriately, the NCR5380
915 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100917 * If successful : I_T_L or I_T_L_Q nexus will be established,
918 * instance->connected will be set to cmd.
919 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100921 * If failed (no target) : cmd->scsi_done() will be called, and the
922 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100924
Finn Thain707d62b2016-01-03 16:06:02 +1100925static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
926 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Finn Thaine8a60142016-01-03 16:05:54 +1100928 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned char tmp[3], phase;
930 unsigned char *data;
931 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100935 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
936 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Finn Thain707d62b2016-01-03 16:06:02 +1100938 /*
939 * Arbitration and selection phases are slow and involve dropping the
940 * lock, so we have to watch out for EH. An exception handler may
941 * change 'selecting' to NULL. This function will then return NULL
942 * so that the caller will forget about 'cmd'. (During information
943 * transfer phases, EH may change 'connected' to NULL.)
944 */
945 hostdata->selecting = cmd;
946
Finn Thainaff0cf92016-01-03 16:06:09 +1100947 /*
948 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * data bus during SELECTION.
950 */
951
952 NCR5380_write(TARGET_COMMAND_REG, 0);
953
Finn Thainaff0cf92016-01-03 16:06:09 +1100954 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * Start arbitration.
956 */
957
958 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
959 NCR5380_write(MODE_REG, MR_ARBITRATE);
960
Finn Thain55500d92016-01-03 16:05:35 +1100961 /* The chip now waits for BUS FREE phase. Then after the 800 ns
962 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
964
Finn Thain11d2f632016-01-03 16:05:51 +1100965 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100966 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
967 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
968 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100969 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100970 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
971 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100972 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +1100973 }
Finn Thainccf6efd2016-02-23 10:07:08 +1100974 if (!hostdata->selecting) {
975 /* Command was aborted */
976 NCR5380_write(MODE_REG, MR_BASE);
977 goto out;
978 }
Finn Thainb32ade12016-01-03 16:05:41 +1100979 if (err < 0) {
980 NCR5380_write(MODE_REG, MR_BASE);
981 shost_printk(KERN_ERR, instance,
982 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +1100983 goto out;
Finn Thain55500d92016-01-03 16:05:35 +1100984 }
Finn Thain11d2f632016-01-03 16:05:51 +1100985 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +1100986
987 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 udelay(3);
989
990 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +1100991 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
992 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
993 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +1100995 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +1100996 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +1100997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Finn Thaincf13b082016-01-03 16:05:18 +1100999
1000 /* After/during arbitration, BSY should be asserted.
1001 * IBM DPES-31080 Version S31Q works now
1002 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1003 */
1004 NCR5380_write(INITIATOR_COMMAND_REG,
1005 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Finn Thainaff0cf92016-01-03 16:06:09 +11001007 /*
1008 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * a minimum so we'll udelay ceil(1.2)
1010 */
1011
Finn Thain9c3f0e22016-01-03 16:05:11 +11001012 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1013 udelay(15);
1014 else
1015 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Finn Thain11d2f632016-01-03 16:05:51 +11001017 spin_lock_irq(&hostdata->lock);
1018
Finn Thain72064a72016-01-03 16:05:44 +11001019 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1020 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001021 goto out;
1022
1023 if (!hostdata->selecting) {
1024 NCR5380_write(MODE_REG, MR_BASE);
1025 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1026 goto out;
1027 }
Finn Thain72064a72016-01-03 16:05:44 +11001028
Finn Thainb7465452016-01-03 16:06:05 +11001029 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Finn Thainaff0cf92016-01-03 16:06:09 +11001031 /*
1032 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * the host and target ID's on the SCSI bus.
1034 */
1035
Finn Thain3d07d222016-01-03 16:06:13 +11001036 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Finn Thainaff0cf92016-01-03 16:06:09 +11001038 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * Raise ATN while SEL is true before BSY goes false from arbitration,
1040 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1041 * phase immediately after selection.
1042 */
1043
Finn Thain3d07d222016-01-03 16:06:13 +11001044 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1045 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 NCR5380_write(MODE_REG, MR_BASE);
1047
Finn Thainaff0cf92016-01-03 16:06:09 +11001048 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * Reselect interrupts must be turned off prior to the dropping of BSY,
1050 * otherwise we will trigger an interrupt.
1051 */
1052 NCR5380_write(SELECT_ENABLE_REG, 0);
1053
Finn Thain11d2f632016-01-03 16:05:51 +11001054 spin_unlock_irq(&hostdata->lock);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001057 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * the BSY signal.
1059 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001060 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001063 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1064 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Finn Thainaff0cf92016-01-03 16:06:09 +11001066 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001068 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * appropriate propagation delay has expired, and we're confusing
1070 * a BSY signal from ourselves as the target's response to SELECTION.
1071 *
1072 * A small delay (the 'C++' frontend breaks the pipeline with an
1073 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001074 * tighter 'C' code breaks and requires this) solves the problem -
1075 * the 1 us delay is arbitrary, and only used because this delay will
1076 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * work there.
1078 *
1079 * wingel suggests that this could be due to failing to wait
1080 * one deskew delay.
1081 */
1082
1083 udelay(1);
1084
Finn Thainb7465452016-01-03 16:06:05 +11001085 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Finn Thainaff0cf92016-01-03 16:06:09 +11001087 /*
1088 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * selection.
1090 */
1091
Finn Thainae753a32016-01-03 16:05:23 +11001092 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1093 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001096 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1098 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001099 if (!hostdata->connected)
1100 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001101 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Finn Thainae753a32016-01-03 16:05:23 +11001104
1105 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001106 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001107 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001108 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001109 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1110 if (hostdata->selecting) {
1111 cmd->result = DID_BAD_TARGET << 16;
1112 complete_cmd(instance, cmd);
1113 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1114 cmd = NULL;
1115 }
1116 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001117 }
1118
Finn Thainaff0cf92016-01-03 16:06:09 +11001119 /*
1120 * No less than two deskew delays after the initiator detects the
1121 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * change the DATA BUS. -wingel
1123 */
1124
1125 udelay(1);
1126
1127 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001130 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * was true but before BSY was false during selection, the information
1132 * transfer phase should be a MESSAGE OUT phase so that we can send the
1133 * IDENTIFY message.
Finn Thainaff0cf92016-01-03 16:06:09 +11001134 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1136 * message (2 bytes) with a tag ID that we increment with every command
1137 * until it wraps back to 0.
1138 *
1139 * XXX - it turns out that there are some broken SCSI-II devices,
Finn Thain594d4ba2016-01-03 16:06:10 +11001140 * which claim to support tagged queuing but fail when more than
1141 * some number of commands are issued at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
1143
1144 /* Wait for start of REQ/ACK handshake */
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001147 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001148 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001149 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1150 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001152 goto out;
1153 }
1154 if (!hostdata->selecting) {
1155 do_abort(instance);
1156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
Finn Thainb7465452016-01-03 16:06:05 +11001159 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1160 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001161 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 len = 1;
1164 cmd->tag = 0;
1165
1166 /* Send message(s) */
1167 data = tmp;
1168 phase = PHASE_MSGOUT;
1169 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001170 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001174 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Boaz Harrosh28424d32007-09-10 22:37:45 +03001176 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Finn Thain707d62b2016-01-03 16:06:02 +11001178 cmd = NULL;
1179
1180out:
1181 if (!hostdata->selecting)
1182 return NULL;
1183 hostdata->selecting = NULL;
1184 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Finn Thainaff0cf92016-01-03 16:06:09 +11001187/*
1188 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001189 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 *
1191 * Purpose : transfers data in given phase using polled I/O
1192 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001193 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001194 * what phase is expected, *count - pointer to number of
1195 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001196 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001198 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001199 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001201 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
1203 * XXX Note : handling for bus free may be useful.
1204 */
1205
1206/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001207 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * IS 100% reliable, and for the actual data transfer where speed
1209 * counts, we will always do a pseudo DMA or DMA transfer.
1210 */
1211
Finn Thain0d2cf862016-01-03 16:06:11 +11001212static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1213 unsigned char *phase, int *count,
1214 unsigned char **data)
1215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 unsigned char p = *phase, tmp;
1217 int c = *count;
1218 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Finn Thainaff0cf92016-01-03 16:06:09 +11001220 /*
1221 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * phase specified in the appropriate bits of the TARGET COMMAND
1223 * REGISTER match the STATUS REGISTER
1224 */
1225
Finn Thain0d2cf862016-01-03 16:06:11 +11001226 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001229 /*
1230 * Wait for assertion of REQ, after which the phase bits will be
1231 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
1233
Finn Thain686f3992016-01-03 16:05:26 +11001234 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Finn Thainb7465452016-01-03 16:06:05 +11001237 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001240 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001241 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1242 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
1244 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* Do actual transfer from SCSI bus to / from memory */
1247 if (!(p & SR_IO))
1248 NCR5380_write(OUTPUT_DATA_REG, *d);
1249 else
1250 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1251
1252 ++d;
1253
Finn Thainaff0cf92016-01-03 16:06:09 +11001254 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * The SCSI standard suggests that in MSGOUT phase, the initiator
1256 * should drop ATN on the last byte of the message phase
1257 * after REQ has been asserted for the handshake but before
1258 * the initiator raises ACK.
1259 */
1260
1261 if (!(p & SR_IO)) {
1262 if (!((p & SR_MSG) && c > 1)) {
1263 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1264 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001265 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1266 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001268 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1269 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001271 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1272 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 } else {
1275 NCR5380_dprint(NDEBUG_PIO, instance);
1276 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1277 }
1278
Finn Thaina2edc4a2016-01-03 16:05:27 +11001279 if (NCR5380_poll_politely(instance,
1280 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1281 break;
1282
Finn Thainb7465452016-01-03 16:06:05 +11001283 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001286 * We have several special cases to consider during REQ/ACK handshaking :
1287 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001288 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001290 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001291 * message. We must exit with ACK asserted, so that the calling
1292 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 *
1294 * 3. ACK and ATN are clear and the target may proceed as normal.
1295 */
1296 if (!(p == PHASE_MSGIN && c == 1)) {
1297 if (p == PHASE_MSGOUT && c > 1)
1298 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1299 else
1300 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1301 }
1302 } while (--c);
1303
Finn Thainb7465452016-01-03 16:06:05 +11001304 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 *count = c;
1307 *data = d;
1308 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001309 /* The phase read from the bus is valid if either REQ is (already)
1310 * asserted or if ACK hasn't been released yet. The latter applies if
1311 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1312 */
1313 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 *phase = tmp & PHASE_MASK;
1315 else
1316 *phase = PHASE_UNKNOWN;
1317
1318 if (!c || (*phase == p))
1319 return 0;
1320 else
1321 return -1;
1322}
1323
1324/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001325 * do_reset - issue a reset command
1326 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001328 * Issue a reset sequence to the NCR5380 and try and get the bus
1329 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001331 * This clears the reset interrupt flag because there may be no handler for
1332 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1333 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001335
Finn Thain54d8fe42016-01-03 16:05:06 +11001336static void do_reset(struct Scsi_Host *instance)
1337{
Finn Thain636b1ec2016-01-03 16:05:10 +11001338 unsigned long flags;
1339
1340 local_irq_save(flags);
1341 NCR5380_write(TARGET_COMMAND_REG,
1342 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001344 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001346 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1347 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Finn Thain80d3eb62016-01-03 16:05:34 +11001350/**
1351 * do_abort - abort the currently established nexus by going to
1352 * MESSAGE OUT phase and sending an ABORT message.
1353 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001355 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
1357
Finn Thain54d8fe42016-01-03 16:05:06 +11001358static int do_abort(struct Scsi_Host *instance)
1359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 unsigned char *msgptr, phase, tmp;
1361 int len;
1362 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* Request message out phase */
1365 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1366
Finn Thainaff0cf92016-01-03 16:06:09 +11001367 /*
1368 * Wait for the target to indicate a valid phase by asserting
1369 * REQ. Once this happens, we'll have either a MSGOUT phase
1370 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001372 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * We really don't care what value was on the bus or what value
1374 * the target sees, so we just handshake.
1375 */
1376
Finn Thain80d3eb62016-01-03 16:05:34 +11001377 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001378 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001379 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Finn Thainf35d3472016-01-03 16:05:33 +11001381 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1384
Finn Thainf35d3472016-01-03 16:05:33 +11001385 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001386 NCR5380_write(INITIATOR_COMMAND_REG,
1387 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001388 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001389 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001390 goto timeout;
1391 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 tmp = ABORT;
1395 msgptr = &tmp;
1396 len = 1;
1397 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001398 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * If we got here, and the command completed successfully,
1402 * we're about to go into bus free state.
1403 */
1404
1405 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001406
1407timeout:
1408 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1409 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Finn Thainaff0cf92016-01-03 16:06:09 +11001412/*
1413 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001414 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 *
1416 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001417 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001419 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001420 * what phase is expected, *count - pointer to number of
1421 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001424 * maximum number of bytes, 0 if all bytes or transferred or exit
1425 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001427 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
1429
1430
Finn Thain0d2cf862016-01-03 16:06:11 +11001431static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1432 unsigned char *phase, int *count,
1433 unsigned char **data)
1434{
1435 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 register int c = *count;
1437 register unsigned char p = *phase;
1438 register unsigned char *d = *data;
1439 unsigned char tmp;
1440 int foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1443 *phase = tmp;
1444 return -1;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /*
1450 * Note : on my sample board, watch-dog timeouts occurred when interrupts
Finn Thainaff0cf92016-01-03 16:06:09 +11001451 * were not disabled for the duration of a single DMA transfer, from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * before the setting of DMA mode to after transfer of the last byte.
1453 */
1454
Finn Thain1bb46002016-03-23 21:10:14 +11001455 if (hostdata->flags & FLAG_DMA_FIXUP)
1456 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
1457 else
Finn Thaincd400822016-01-03 16:05:40 +11001458 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1459 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Finn Thain52a6a1c2014-03-18 11:42:18 +11001461 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Finn Thainaff0cf92016-01-03 16:06:09 +11001463 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001464 * On the PAS16 at least I/O recovery delays are not needed here.
1465 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
1467
1468 if (p & SR_IO) {
Finn Thaine5d55d12016-03-23 21:10:16 +11001469 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1471 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001472 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001474 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001476 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479/*
Finn Thaine4dec682016-03-23 21:10:12 +11001480 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001481 *
1482 * For DMA sends, we want to wait until the last byte has been
1483 * transferred out over the bus before we turn off DMA mode. Alas, there
1484 * seems to be no terribly good way of doing this on a 5380 under all
1485 * conditions. For non-scatter-gather operations, we can wait until REQ
1486 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1487 * are nastier, since the device will be expecting more data than we
1488 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1489 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1490 * why this signal was added to the newer chips) but on the older 538[01]
1491 * this signal does not exist. The workaround for this lack is a watchdog;
1492 * we bail out of the wait-loop after a modest amount of wait-time if
1493 * the usual exit conditions are not met. Not a terribly clean or
1494 * correct solution :-%
1495 *
1496 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1497 * If the chip is in DMA receive mode, it will respond to a target's
1498 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1499 * ACK, even if it has _already_ been notified by the DMA controller that
1500 * the current DMA transfer has completed! If the NCR5380 is then taken
1501 * out of DMA mode, this already-acknowledged byte is lost. This is
1502 * not a problem for "one DMA transfer per READ command", because
1503 * the situation will never arise... either all of the data is DMA'ed
1504 * properly, or the target switches to MESSAGE IN phase to signal a
1505 * disconnection (either operation bringing the DMA to a clean halt).
1506 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001507 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001508 * condition before taking the NCR5380 out of DMA mode. One or two extra
1509 * bytes are transferred via PIO as necessary to fill out the original
1510 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
1513 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001514 foo = NCR5380_pread(instance, d,
Finn Thain1bb46002016-03-23 21:10:14 +11001515 hostdata->flags & FLAG_DMA_FIXUP ? c - 1 : c);
1516 if (!foo && (hostdata->flags & FLAG_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001518 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001519 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 * the chip to latch the last byte, read it, and then disable
1521 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001522 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1524 * REQ is deasserted when ACK is asserted, and not reasserted
1525 * until ACK goes false. Since the NCR5380 won't lower ACK
1526 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001527 * the DMA port or we take the NCR5380 out of DMA mode, we
1528 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 * byte.
1530 */
1531
Finn Thain55181be2016-01-03 16:05:42 +11001532 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1533 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1534 foo = -1;
1535 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Finn Thain55181be2016-01-03 16:05:42 +11001537 if (NCR5380_poll_politely(instance, STATUS_REG,
1538 SR_REQ, 0, HZ) < 0) {
1539 foo = -1;
1540 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1541 }
1542 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 foo = NCR5380_pwrite(instance, d, c);
Finn Thain1bb46002016-03-23 21:10:14 +11001546 if (!foo && (hostdata->flags & FLAG_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001548 * Wait for the last byte to be sent. If REQ is being asserted for
1549 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
Finn Thain55181be2016-01-03 16:05:42 +11001551 if (NCR5380_poll_politely2(instance,
1552 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1553 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1554 foo = -1;
1555 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 NCR5380_write(MODE_REG, MR_BASE);
1560 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001561 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 *data = d + c;
1563 *count = 0;
1564 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/*
1569 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1570 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001571 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001572 * directs us to. Operates on the currently connected command,
1573 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 *
1575 * Inputs : instance, instance for which we are doing commands
1576 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001577 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001578 * modified if a command disconnects, *instance->connected will
1579 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001581 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001582 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
1584
Finn Thain0d2cf862016-01-03 16:06:11 +11001585static void NCR5380_information_transfer(struct Scsi_Host *instance)
1586{
Finn Thaine8a60142016-01-03 16:05:54 +11001587 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 unsigned char msgout = NOP;
1589 int sink = 0;
1590 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 unsigned char *data;
1593 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001594 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Finn Thain11d2f632016-01-03 16:05:51 +11001596 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001597 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 tmp = NCR5380_read(STATUS_REG);
1600 /* We only have a valid SCSI phase when REQ is asserted */
1601 if (tmp & SR_REQ) {
1602 phase = (tmp & PHASE_MASK);
1603 if (phase != old_phase) {
1604 old_phase = phase;
1605 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1606 }
1607 if (sink && (phase != PHASE_MSGOUT)) {
1608 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1609
Finn Thain3d07d222016-01-03 16:06:13 +11001610 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1611 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001612 while (NCR5380_read(STATUS_REG) & SR_REQ)
1613 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001614 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1615 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 sink = 0;
1617 continue;
1618 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 case PHASE_DATAOUT:
1622#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001623 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 sink = 1;
1625 do_abort(instance);
1626 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001627 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001628 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return;
1630#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001631 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001632 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * If there is no room left in the current buffer in the
1634 * scatter-gather list, move onto the next one.
1635 */
1636
1637 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1638 ++cmd->SCp.buffer;
1639 --cmd->SCp.buffers_residual;
1640 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001641 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001642 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1643 cmd->SCp.this_residual,
1644 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001648 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 * PSEUDO-DMA for systems that are strictly PIO,
1650 * since we can let the hardware do the handshaking.
1651 *
1652 * For this to work, we need to know the transfersize
1653 * ahead of time, since the pseudo-DMA code will sit
1654 * in an unconditional loop.
1655 */
1656
Finn Thainff3d4572016-01-03 16:05:25 +11001657 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001658 if (!cmd->device->borken)
Finn Thainff3d4572016-01-03 16:05:25 +11001659 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Finn Thainff3d4572016-01-03 16:05:25 +11001661 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001663 if (NCR5380_transfer_dma(instance, &phase,
1664 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001666 * If the watchdog timer fires, all future
1667 * accesses to this device will use the
1668 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001670 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001671 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 sink = 1;
1674 do_abort(instance);
1675 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* XXX - need to source or sink data here, as appropriate */
1677 } else
1678 cmd->SCp.this_residual -= transfersize - len;
Finn Thainf825e402016-03-23 21:10:15 +11001679 } else {
Finn Thain16788472016-02-23 10:07:05 +11001680 /* Break up transfer into 3 ms chunks,
1681 * presuming 6 accesses per handshake.
1682 */
1683 transfersize = min((unsigned long)cmd->SCp.this_residual,
1684 hostdata->accesses_per_ms / 2);
1685 len = transfersize;
1686 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001687 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001688 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001689 }
Finn Thain16788472016-02-23 10:07:05 +11001690 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 case PHASE_MSGIN:
1692 len = 1;
1693 data = &tmp;
1694 NCR5380_transfer_pio(instance, &phase, &len, &data);
1695 cmd->SCp.Message = tmp;
1696
1697 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 case ABORT:
1699 case COMMAND_COMPLETE:
1700 /* Accept message by clearing ACK */
1701 sink = 1;
1702 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001703 dsprintk(NDEBUG_QUEUES, instance,
1704 "COMMAND COMPLETE %p target %d lun %llu\n",
1705 cmd, scmd_id(cmd), cmd->device->lun);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Finn Thainf27db8e2016-01-03 16:06:00 +11001709 cmd->result &= ~0xffff;
1710 cmd->result |= cmd->SCp.Status;
1711 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Finn Thainf27db8e2016-01-03 16:06:00 +11001713 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001714 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001715 else {
1716 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1717 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1718 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1719 cmd);
1720 list_add_tail(&ncmd->list,
1721 &hostdata->autosense);
1722 } else
1723 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
Finn Thainaff0cf92016-01-03 16:06:09 +11001726 /*
1727 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * arbitration can resume.
1729 */
1730 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001731
1732 /* Enable reselect interrupts */
1733 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return;
1735 case MESSAGE_REJECT:
1736 /* Accept message by clearing ACK */
1737 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1738 switch (hostdata->last_message) {
1739 case HEAD_OF_QUEUE_TAG:
1740 case ORDERED_QUEUE_TAG:
1741 case SIMPLE_QUEUE_TAG:
1742 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001743 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745 default:
1746 break;
1747 }
Finn Thain340b9612016-01-03 16:05:31 +11001748 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001749 case DISCONNECT:
1750 /* Accept message by clearing ACK */
1751 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1752 hostdata->connected = NULL;
1753 list_add(&ncmd->list, &hostdata->disconnected);
1754 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1755 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1756 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001757
Finn Thain0d2cf862016-01-03 16:06:11 +11001758 /*
1759 * Restore phase bits to 0 so an interrupted selection,
1760 * arbitration can resume.
1761 */
1762 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Finn Thain0d2cf862016-01-03 16:06:11 +11001764 /* Enable reselect interrupts */
1765 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1766 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001767 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001769 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * ignore SAVE/RESTORE pointers calls.
1771 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001772 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001774 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 * compatible.
1776 */
1777 case SAVE_POINTERS:
1778 case RESTORE_POINTERS:
1779 /* Accept message by clearing ACK */
1780 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1781 break;
1782 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001783 /*
1784 * Start the message buffer with the EXTENDED_MESSAGE
1785 * byte, since spi_print_msg() wants the whole thing.
1786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 extended_msg[0] = EXTENDED_MESSAGE;
1788 /* Accept first byte by clearing ACK */
1789 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001790
1791 spin_unlock_irq(&hostdata->lock);
1792
Finn Thainb7465452016-01-03 16:06:05 +11001793 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 len = 2;
1796 data = extended_msg + 1;
1797 phase = PHASE_MSGIN;
1798 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001799 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1800 (int)extended_msg[1],
1801 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Finn Thaine0783ed2016-01-03 16:05:45 +11001803 if (!len && extended_msg[1] > 0 &&
1804 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* Accept third byte by clearing ACK */
1806 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1807 len = extended_msg[1] - 1;
1808 data = extended_msg + 3;
1809 phase = PHASE_MSGIN;
1810
1811 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001812 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1813 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 switch (extended_msg[2]) {
1816 case EXTENDED_SDTR:
1817 case EXTENDED_WDTR:
1818 case EXTENDED_MODIFY_DATA_POINTER:
1819 case EXTENDED_EXTENDED_IDENTIFY:
1820 tmp = 0;
1821 }
1822 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001823 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 tmp = 0;
1825 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001826 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1827 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 tmp = 0;
1829 }
Finn Thain11d2f632016-01-03 16:05:51 +11001830
1831 spin_lock_irq(&hostdata->lock);
1832 if (!hostdata->connected)
1833 return;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /* Fall through to reject message */
1836
Finn Thainaff0cf92016-01-03 16:06:09 +11001837 /*
1838 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 * reject it.
1840 */
1841 default:
1842 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001843 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001844 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 printk("\n");
1846 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001847 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001848 "rejecting unknown message %02x\n",
1849 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 else
Jeff Garzik017560f2005-10-24 18:04:36 -04001851 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001852 "rejecting unknown extended message code %02x, length %d\n",
1853 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 msgout = MESSAGE_REJECT;
1856 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1857 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001858 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860 case PHASE_MSGOUT:
1861 len = 1;
1862 data = &msgout;
1863 hostdata->last_message = msgout;
1864 NCR5380_transfer_pio(instance, &phase, &len, &data);
1865 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 hostdata->connected = NULL;
1867 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001868 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1870 return;
1871 }
1872 msgout = NOP;
1873 break;
1874 case PHASE_CMDOUT:
1875 len = cmd->cmd_len;
1876 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001877 /*
1878 * XXX for performance reasons, on machines with a
1879 * PSEUDO-DMA architecture we should probably
1880 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
1882 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
1884 case PHASE_STATIN:
1885 len = 1;
1886 data = &tmp;
1887 NCR5380_transfer_pio(instance, &phase, &len, &data);
1888 cmd->SCp.Status = tmp;
1889 break;
1890 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001891 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001892 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001893 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001894 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001895 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11001896 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001897 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Finn Thain11d2f632016-01-03 16:05:51 +11001899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902/*
1903 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1904 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001905 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11001906 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1907 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11001908 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
1911
Finn Thain0d2cf862016-01-03 16:06:11 +11001912static void NCR5380_reselect(struct Scsi_Host *instance)
1913{
Finn Thaine8a60142016-01-03 16:05:54 +11001914 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 unsigned char target_mask;
1916 unsigned char lun, phase;
1917 int len;
1918 unsigned char msg[3];
1919 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11001920 struct NCR5380_cmd *ncmd;
1921 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /*
1924 * Disable arbitration, etc. since the host adapter obviously
1925 * lost, and tell an interrupted NCR5380_select() to restart.
1926 */
1927
1928 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11001931
1932 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Finn Thainaff0cf92016-01-03 16:06:09 +11001934 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 * At this point, we have detected that our SCSI ID is on the bus,
1936 * SEL is true and BSY was false for at least one bus settle delay
1937 * (400 ns).
1938 *
1939 * We must assert BSY ourselves, until the target drops the SEL
1940 * signal.
1941 */
1942
1943 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11001944 if (NCR5380_poll_politely(instance,
1945 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
1946 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1947 return;
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1950
1951 /*
1952 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
1954
Finn Thain1cc160e2016-01-03 16:05:32 +11001955 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11001956 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
1957 do_abort(instance);
1958 return;
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 len = 1;
1962 data = msg;
1963 phase = PHASE_MSGIN;
1964 NCR5380_transfer_pio(instance, &phase, &len, &data);
1965
Finn Thain72064a72016-01-03 16:05:44 +11001966 if (len) {
1967 do_abort(instance);
1968 return;
1969 }
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11001972 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001973 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11001974 printk("\n");
1975 do_abort(instance);
1976 return;
1977 }
1978 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Finn Thain72064a72016-01-03 16:05:44 +11001980 /*
1981 * We need to add code for SCSI-II to track which devices have
1982 * I_T_L_Q nexuses established, and which have simple I_T_L
1983 * nexuses so we can chose to do additional data transfer.
1984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Finn Thain72064a72016-01-03 16:05:44 +11001986 /*
1987 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
1988 * just reestablished, and remove it from the disconnected queue.
1989 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Finn Thain32b26a12016-01-03 16:05:58 +11001991 tmp = NULL;
1992 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
1993 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
1994
1995 if (target_mask == (1 << scmd_id(cmd)) &&
1996 lun == (u8)cmd->device->lun) {
1997 list_del(&ncmd->list);
1998 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11001999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002002
2003 if (tmp) {
2004 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2005 "reselect: removed %p from disconnected queue\n", tmp);
2006 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002007 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2008 target_mask, lun);
2009 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002010 * Since we have an established nexus that we can't do anything
2011 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
Finn Thain72064a72016-01-03 16:05:44 +11002016
2017 /* Accept message by clearing ACK */
2018 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2019
2020 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002021 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2022 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Finn Thain8b00c3d2016-01-03 16:06:01 +11002025/**
2026 * list_find_cmd - test for presence of a command in a linked list
2027 * @haystack: list of commands
2028 * @needle: command to search for
2029 */
2030
2031static bool list_find_cmd(struct list_head *haystack,
2032 struct scsi_cmnd *needle)
2033{
2034 struct NCR5380_cmd *ncmd;
2035
2036 list_for_each_entry(ncmd, haystack, list)
2037 if (NCR5380_to_scmd(ncmd) == needle)
2038 return true;
2039 return false;
2040}
2041
2042/**
2043 * list_remove_cmd - remove a command from linked list
2044 * @haystack: list of commands
2045 * @needle: command to remove
2046 */
2047
2048static bool list_del_cmd(struct list_head *haystack,
2049 struct scsi_cmnd *needle)
2050{
2051 if (list_find_cmd(haystack, needle)) {
2052 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2053
2054 list_del(&ncmd->list);
2055 return true;
2056 }
2057 return false;
2058}
2059
2060/**
2061 * NCR5380_abort - scsi host eh_abort_handler() method
2062 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002064 * Try to abort a given command by removing it from queues and/or sending
2065 * the target an abort message. This may not succeed in causing a target
2066 * to abort the command. Nonetheless, the low-level driver must forget about
2067 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002069 * The normal path taken by a command is as follows. For EH we trace this
2070 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002072 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2073 * [disconnected -> connected ->]...
2074 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002076 * If cmd was not found at all then presumably it has already been completed,
2077 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002078 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002079 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002080 * We have no option but to forget the aborted command (even if it still
2081 * lacks sense data). The mid-layer may re-issue a command that is in error
2082 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2083 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002084 *
2085 * The lock protects driver data structures, but EH handlers also use it
2086 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 */
2088
Finn Thain710ddd02014-11-12 16:12:02 +11002089static int NCR5380_abort(struct scsi_cmnd *cmd)
2090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002092 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002093 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002094 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002095
Finn Thain11d2f632016-01-03 16:05:51 +11002096 spin_lock_irqsave(&hostdata->lock, flags);
2097
Finn Thain32b26a12016-01-03 16:05:58 +11002098#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002099 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002100#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002101 NCR5380_dprint(NDEBUG_ANY, instance);
2102 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Finn Thain8b00c3d2016-01-03 16:06:01 +11002104 if (list_del_cmd(&hostdata->unissued, cmd)) {
2105 dsprintk(NDEBUG_ABORT, instance,
2106 "abort: removed %p from issue queue\n", cmd);
2107 cmd->result = DID_ABORT << 16;
2108 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002109 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002110 }
2111
Finn Thain707d62b2016-01-03 16:06:02 +11002112 if (hostdata->selecting == cmd) {
2113 dsprintk(NDEBUG_ABORT, instance,
2114 "abort: cmd %p == selecting\n", cmd);
2115 hostdata->selecting = NULL;
2116 cmd->result = DID_ABORT << 16;
2117 complete_cmd(instance, cmd);
2118 goto out;
2119 }
2120
Finn Thain8b00c3d2016-01-03 16:06:01 +11002121 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2122 dsprintk(NDEBUG_ABORT, instance,
2123 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002124 /* Can't call NCR5380_select() and send ABORT because that
2125 * means releasing the lock. Need a bus reset.
2126 */
Finn Thaindc183962016-02-23 10:07:07 +11002127 set_host_byte(cmd, DID_ERROR);
2128 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002129 result = FAILED;
2130 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002131 }
2132
2133 if (hostdata->connected == cmd) {
2134 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2135 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002136 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002137 if (do_abort(instance)) {
2138 set_host_byte(cmd, DID_ERROR);
2139 complete_cmd(instance, cmd);
2140 result = FAILED;
2141 goto out;
2142 }
2143 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002144 complete_cmd(instance, cmd);
2145 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002146 }
2147
Finn Thaindc183962016-02-23 10:07:07 +11002148 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002149 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002150 "abort: removed %p from sense queue\n", cmd);
2151 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002152 complete_cmd(instance, cmd);
2153 }
2154
2155out:
2156 if (result == FAILED)
2157 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2158 else
2159 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2160
2161 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002162 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002163
Finn Thain8b00c3d2016-01-03 16:06:01 +11002164 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167
Finn Thain3be1b3e2016-01-03 16:05:12 +11002168/**
2169 * NCR5380_bus_reset - reset the SCSI bus
2170 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002172 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
2174
Finn Thain710ddd02014-11-12 16:12:02 +11002175static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002176{
2177 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002178 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002179 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002180 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002181 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Finn Thain11d2f632016-01-03 16:05:51 +11002183 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002184
2185#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002186 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002187#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002188 NCR5380_dprint(NDEBUG_ANY, instance);
2189 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002190
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002191 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002192
Finn Thain62717f52016-01-03 16:06:03 +11002193 /* reset NCR registers */
2194 NCR5380_write(MODE_REG, MR_BASE);
2195 NCR5380_write(TARGET_COMMAND_REG, 0);
2196 NCR5380_write(SELECT_ENABLE_REG, 0);
2197
2198 /* After the reset, there are no more connected or disconnected commands
2199 * and no busy units; so clear the low-level status here to avoid
2200 * conflicts when the mid-level code tries to wake up the affected
2201 * commands!
2202 */
2203
Finn Thain1884c282016-02-23 10:07:04 +11002204 if (list_del_cmd(&hostdata->unissued, cmd)) {
2205 cmd->result = DID_RESET << 16;
2206 cmd->scsi_done(cmd);
2207 }
2208
2209 if (hostdata->selecting) {
2210 hostdata->selecting->result = DID_RESET << 16;
2211 complete_cmd(instance, hostdata->selecting);
2212 hostdata->selecting = NULL;
2213 }
Finn Thain62717f52016-01-03 16:06:03 +11002214
2215 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2216 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2217
2218 set_host_byte(cmd, DID_RESET);
2219 cmd->scsi_done(cmd);
2220 }
Finn Thain1884c282016-02-23 10:07:04 +11002221 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002222
2223 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2224 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2225
2226 set_host_byte(cmd, DID_RESET);
2227 cmd->scsi_done(cmd);
2228 }
Finn Thain1884c282016-02-23 10:07:04 +11002229 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002230
2231 if (hostdata->connected) {
2232 set_host_byte(hostdata->connected, DID_RESET);
2233 complete_cmd(instance, hostdata->connected);
2234 hostdata->connected = NULL;
2235 }
2236
Finn Thain62717f52016-01-03 16:06:03 +11002237 for (i = 0; i < 8; ++i)
2238 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002239 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002240
2241 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002242 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return SUCCESS;
2245}