blob: e05cf505f0d44783ff185de976018a64fc02f531 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * NCR5380_dma_write_setup(instance, src, count) - initialize
132 * NCR5380_dma_read_setup(instance, dst, count) - initialize
133 * NCR5380_dma_residual(instance); - residual count
134 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * The generic driver is initialized by calling NCR5380_init(instance),
Finn Thainaff0cf92016-01-03 16:06:09 +1100136 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
138 * possible) function may be used.
139 */
140
Finn Thaine5d55d12016-03-23 21:10:16 +1100141#ifndef NCR5380_io_delay
142#define NCR5380_io_delay(x)
143#endif
144
Finn Thain54d8fe42016-01-03 16:05:06 +1100145static int do_abort(struct Scsi_Host *);
146static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Finn Thainc16df322016-01-03 16:06:08 +1100148/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100149 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100150 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100152 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
154
Finn Thain710ddd02014-11-12 16:12:02 +1100155static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Finn Thainaff0cf92016-01-03 16:06:09 +1100157 /*
158 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * various queues are valid.
160 */
161
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200162 if (scsi_bufflen(cmd)) {
163 cmd->SCp.buffer = scsi_sglist(cmd);
164 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200165 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 cmd->SCp.this_residual = cmd->SCp.buffer->length;
167 } else {
168 cmd->SCp.buffer = NULL;
169 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200170 cmd->SCp.ptr = NULL;
171 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100173
174 cmd->SCp.Status = 0;
175 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/**
Finn Thainb32ade12016-01-03 16:05:41 +1100179 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100180 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100181 * @reg1: 5380 register to poll
182 * @bit1: Bitmask to check
183 * @val1: Expected value
184 * @reg2: Second 5380 register to poll
185 * @bit2: Second bitmask to check
186 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100187 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
Finn Thain2f854b82016-01-03 16:05:22 +1100189 * Polls the chip in a reasonably efficient manner waiting for an
190 * event to occur. After a short quick poll we begin to yield the CPU
191 * (if possible). In irq contexts the time-out is arbitrarily limited.
192 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Finn Thainb32ade12016-01-03 16:05:41 +1100194 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Finn Thainb32ade12016-01-03 16:05:41 +1100197static int NCR5380_poll_politely2(struct Scsi_Host *instance,
198 int reg1, int bit1, int val1,
199 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100200{
201 struct NCR5380_hostdata *hostdata = shost_priv(instance);
202 unsigned long deadline = jiffies + wait;
203 unsigned long n;
204
205 /* Busy-wait for up to 10 ms */
206 n = min(10000U, jiffies_to_usecs(wait));
207 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100208 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100209 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100210 if ((NCR5380_read(reg1) & bit1) == val1)
211 return 0;
212 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100215 } while (n--);
216
217 if (irqs_disabled() || in_interrupt())
218 return -ETIMEDOUT;
219
220 /* Repeatedly sleep for 1 ms until deadline */
221 while (time_is_after_jiffies(deadline)) {
222 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100223 if ((NCR5380_read(reg1) & bit1) == val1)
224 return 0;
225 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Finn Thain2f854b82016-01-03 16:05:22 +1100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -ETIMEDOUT;
230}
231
Finn Thainb32ade12016-01-03 16:05:41 +1100232static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
233 int reg, int bit, int val, int wait)
234{
235 return NCR5380_poll_politely2(instance, reg, bit, val,
236 reg, bit, val, wait);
237}
238
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100239#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static struct {
241 unsigned char mask;
242 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100243} signals[] = {
244 {SR_DBP, "PARITY"},
245 {SR_RST, "RST"},
246 {SR_BSY, "BSY"},
247 {SR_REQ, "REQ"},
248 {SR_MSG, "MSG"},
249 {SR_CD, "CD"},
250 {SR_IO, "IO"},
251 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100253},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254basrs[] = {
Finn Thainaff0cf92016-01-03 16:06:09 +1100255 {BASR_ATN, "ATN"},
256 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100258},
259icrs[] = {
260 {ICR_ASSERT_RST, "ASSERT RST"},
261 {ICR_ASSERT_ACK, "ASSERT ACK"},
262 {ICR_ASSERT_BSY, "ASSERT BSY"},
263 {ICR_ASSERT_SEL, "ASSERT SEL"},
264 {ICR_ASSERT_ATN, "ASSERT ATN"},
265 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100267},
268mrs[] = {
269 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
270 {MR_TARGET, "MODE TARGET"},
271 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
272 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
Finn Thain0d2cf862016-01-03 16:06:11 +1100273 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100274 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
275 {MR_DMA_MODE, "MODE DMA"},
276 {MR_ARBITRATE, "MODE ARBITRATION"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 {0, NULL}
278};
279
280/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100281 * NCR5380_print - print scsi bus signals
282 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100284 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286
287static void NCR5380_print(struct Scsi_Host *instance)
288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
292 status = NCR5380_read(STATUS_REG);
293 mr = NCR5380_read(MODE_REG);
294 icr = NCR5380_read(INITIATOR_COMMAND_REG);
295 basr = NCR5380_read(BUS_AND_STATUS_REG);
296
297 printk("STATUS_REG: %02x ", status);
298 for (i = 0; signals[i].mask; ++i)
299 if (status & signals[i].mask)
300 printk(",%s", signals[i].name);
301 printk("\nBASR: %02x ", basr);
302 for (i = 0; basrs[i].mask; ++i)
303 if (basr & basrs[i].mask)
304 printk(",%s", basrs[i].name);
305 printk("\nICR: %02x ", icr);
306 for (i = 0; icrs[i].mask; ++i)
307 if (icr & icrs[i].mask)
308 printk(",%s", icrs[i].name);
309 printk("\nMODE: %02x ", mr);
310 for (i = 0; mrs[i].mask; ++i)
311 if (mr & mrs[i].mask)
312 printk(",%s", mrs[i].name);
313 printk("\n");
314}
315
Finn Thain0d2cf862016-01-03 16:06:11 +1100316static struct {
317 unsigned char value;
318 const char *name;
319} phases[] = {
320 {PHASE_DATAOUT, "DATAOUT"},
321 {PHASE_DATAIN, "DATAIN"},
322 {PHASE_CMDOUT, "CMDOUT"},
323 {PHASE_STATIN, "STATIN"},
324 {PHASE_MSGOUT, "MSGOUT"},
325 {PHASE_MSGIN, "MSGIN"},
326 {PHASE_UNKNOWN, "UNKNOWN"}
327};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Finn Thainc16df322016-01-03 16:06:08 +1100329/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100330 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100331 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100333 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335
336static void NCR5380_print_phase(struct Scsi_Host *instance)
337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned char status;
339 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 status = NCR5380_read(STATUS_REG);
342 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100343 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100345 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
346 (phases[i].value != (status & PHASE_MASK)); ++i)
347 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100348 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350}
351#endif
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Finn Thaind5f7e652016-01-03 16:05:03 +1100354static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100357 * probe_intr - helper for IRQ autoprobe
358 * @irq: interrupt number
359 * @dev_id: unused
360 * @regs: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100362 * Set a flag to indicate the IRQ in question was received. This is
363 * used by the IRQ probe code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100365
David Howells7d12e782006-10-05 14:55:46 +0100366static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 probe_irq = irq;
369 return IRQ_HANDLED;
370}
371
372/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100373 * NCR5380_probe_irq - find the IRQ of an NCR5380
374 * @instance: NCR5380 controller
375 * @possible: bitmask of ISA IRQ lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100377 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
378 * and then looking to see what interrupt actually turned up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380
Andrew Morton702809c2007-05-23 14:41:56 -0700381static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
382 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Finn Thaine8a60142016-01-03 16:05:54 +1100384 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned long timeout;
386 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Finn Thain22f5f102014-11-12 16:11:56 +1100388 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100389 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 trying_irqs |= mask;
391
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500392 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100393 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * A interrupt is triggered whenever BSY = false, SEL = true
Finn Thainaff0cf92016-01-03 16:06:09 +1100397 * and a bit set in the SELECT_ENABLE_REG is asserted on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * SCSI bus.
399 *
400 * Note that the bus is only driven when the phase control signals
401 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
402 * to zero.
403 */
404
405 NCR5380_write(TARGET_COMMAND_REG, 0);
406 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
407 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
408 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
409
Finn Thain22f5f102014-11-12 16:11:56 +1100410 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800411 schedule_timeout_uninterruptible(1);
Finn Thainaff0cf92016-01-03 16:06:09 +1100412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 NCR5380_write(SELECT_ENABLE_REG, 0);
414 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
415
Finn Thain22f5f102014-11-12 16:11:56 +1100416 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (trying_irqs & mask)
418 free_irq(i, NULL);
419
420 return probe_irq;
421}
422
423/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100424 * NCR58380_info - report driver and host information
425 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100427 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
429
Finn Thain8c325132014-11-12 16:11:58 +1100430static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Finn Thain8c325132014-11-12 16:11:58 +1100432 struct NCR5380_hostdata *hostdata = shost_priv(instance);
433
434 return hostdata->info;
435}
436
437static void prepare_info(struct Scsi_Host *instance)
438{
439 struct NCR5380_hostdata *hostdata = shost_priv(instance);
440
441 snprintf(hostdata->info, sizeof(hostdata->info),
442 "%s, io_port 0x%lx, n_io_port %d, "
443 "base 0x%lx, irq %d, "
444 "can_queue %d, cmd_per_lun %d, "
445 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100446 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100447 "options { %s} ",
448 instance->hostt->name, instance->io_port, instance->n_io_port,
449 instance->base, instance->irq,
450 instance->can_queue, instance->cmd_per_lun,
451 instance->sg_tablesize, instance->this_id,
Finn Thain1bb46002016-03-23 21:10:14 +1100452 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100453 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100454 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100456 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100459 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100462 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#endif
Finn Thain8c325132014-11-12 16:11:58 +1100464 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100468 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100469 * @instance: adapter to configure
470 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100472 * Initializes *instance and corresponding 5380 chip,
473 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100475 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100476 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100478 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
480
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800481static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Finn Thaine8a60142016-01-03 16:05:54 +1100483 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100484 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100485 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Finn Thain0d2cf862016-01-03 16:06:11 +1100487 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100489 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
491 if (i > hostdata->id_mask)
492 hostdata->id_higher_mask |= i;
493 for (i = 0; i < 8; ++i)
494 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100495 hostdata->dma_len = 0;
496
Finn Thain11d2f632016-01-03 16:05:51 +1100497 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100499 hostdata->sensing = NULL;
500 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100501 INIT_LIST_HEAD(&hostdata->unissued);
502 INIT_LIST_HEAD(&hostdata->disconnected);
503
Finn Thain55181be2016-01-03 16:05:42 +1100504 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100505
Finn Thain8d8601a2016-01-03 16:05:37 +1100506 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100507 hostdata->work_q = alloc_workqueue("ncr5380_%d",
508 WQ_UNBOUND | WQ_MEM_RECLAIM,
509 1, instance->host_no);
510 if (!hostdata->work_q)
511 return -ENOMEM;
512
Finn Thain8c325132014-11-12 16:11:58 +1100513 prepare_info(instance);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
516 NCR5380_write(MODE_REG, MR_BASE);
517 NCR5380_write(TARGET_COMMAND_REG, 0);
518 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100519
520 /* Calibrate register polling loop */
521 i = 0;
522 deadline = jiffies + 1;
523 do {
524 cpu_relax();
525 } while (time_is_after_jiffies(deadline));
526 deadline += msecs_to_jiffies(256);
527 do {
528 NCR5380_read(STATUS_REG);
529 ++i;
530 cpu_relax();
531 } while (time_is_after_jiffies(deadline));
532 hostdata->accesses_per_ms = i / 256;
533
Finn Thainb6488f92016-01-03 16:05:08 +1100534 return 0;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Finn Thainb6488f92016-01-03 16:05:08 +1100537/**
538 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
539 * @instance: adapter to check
540 *
541 * If the system crashed, it may have crashed with a connected target and
542 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
543 * currently established nexus, which we know nothing about. Failing that
544 * do a bus reset.
545 *
546 * Note that a bus reset will cause the chip to assert IRQ.
547 *
548 * Returns 0 if successful, otherwise -ENXIO.
549 */
550
551static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
552{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100553 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100554 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
557 switch (pass) {
558 case 1:
559 case 3:
560 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100561 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
562 NCR5380_poll_politely(instance,
563 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100566 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 do_abort(instance);
568 break;
569 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100570 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100572 /* Wait after a reset; the SCSI standard calls for
573 * 250ms, we wait 500ms to be on the safe side.
574 * But some Toshiba CD-ROMs need ten times that.
575 */
576 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
577 msleep(2500);
578 else
579 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 break;
581 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100582 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -ENXIO;
584 }
585 }
586 return 0;
587}
588
589/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100590 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100591 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100592 *
593 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800596static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Finn Thaine8a60142016-01-03 16:05:54 +1100598 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Finn Thain8d8601a2016-01-03 16:05:37 +1100600 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100601 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/**
Finn Thain677e0192016-01-03 16:05:59 +1100605 * complete_cmd - finish processing a command and return it to the SCSI ML
606 * @instance: the host instance
607 * @cmd: command to complete
608 */
609
610static void complete_cmd(struct Scsi_Host *instance,
611 struct scsi_cmnd *cmd)
612{
613 struct NCR5380_hostdata *hostdata = shost_priv(instance);
614
615 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
616
Finn Thainf27db8e2016-01-03 16:06:00 +1100617 if (hostdata->sensing == cmd) {
618 /* Autosense processing ends here */
619 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
620 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
621 set_host_byte(cmd, DID_ERROR);
622 } else
623 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
624 hostdata->sensing = NULL;
625 }
626
Finn Thain677e0192016-01-03 16:05:59 +1100627 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
628
629 cmd->scsi_done(cmd);
630}
631
632/**
Finn Thain1bb40582016-01-03 16:05:29 +1100633 * NCR5380_queue_command - queue a command
634 * @instance: the relevant SCSI adapter
635 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
Finn Thain1bb40582016-01-03 16:05:29 +1100637 * cmd is added to the per-instance issue queue, with minor
638 * twiddling done to the host specific fields of cmd. If the
639 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
641
Finn Thain1bb40582016-01-03 16:05:29 +1100642static int NCR5380_queue_command(struct Scsi_Host *instance,
643 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Finn Thain1bb40582016-01-03 16:05:29 +1100645 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100646 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100647 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649#if (NDEBUG & NDEBUG_NO_WRITE)
650 switch (cmd->cmnd[0]) {
651 case WRITE_6:
652 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100653 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100655 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100658#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 cmd->result = 0;
661
Finn Thain11d2f632016-01-03 16:05:51 +1100662 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100663
Finn Thainaff0cf92016-01-03 16:06:09 +1100664 /*
665 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100667 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * sense data is only guaranteed to be valid while the condition exists.
669 */
670
Finn Thain32b26a12016-01-03 16:05:58 +1100671 if (cmd->cmnd[0] == REQUEST_SENSE)
672 list_add(&ncmd->list, &hostdata->unissued);
673 else
674 list_add_tail(&ncmd->list, &hostdata->unissued);
675
Finn Thain11d2f632016-01-03 16:05:51 +1100676 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100677
Finn Thaindbb6b352016-01-03 16:05:53 +1100678 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
679 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100682 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100687 * dequeue_next_cmd - dequeue a command for processing
688 * @instance: the scsi host instance
689 *
690 * Priority is given to commands on the autosense queue. These commands
691 * need autosense because of a CHECK CONDITION result.
692 *
693 * Returns a command pointer if a command is found for a target that is
694 * not already busy. Otherwise returns NULL.
695 */
696
697static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
698{
699 struct NCR5380_hostdata *hostdata = shost_priv(instance);
700 struct NCR5380_cmd *ncmd;
701 struct scsi_cmnd *cmd;
702
Finn Thain8d5dbec2016-02-23 10:07:09 +1100703 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100704 list_for_each_entry(ncmd, &hostdata->unissued, list) {
705 cmd = NCR5380_to_scmd(ncmd);
706 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
707 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
708
709 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
710 list_del(&ncmd->list);
711 dsprintk(NDEBUG_QUEUES, instance,
712 "dequeue: removed %p from issue queue\n", cmd);
713 return cmd;
714 }
715 }
716 } else {
717 /* Autosense processing begins here */
718 ncmd = list_first_entry(&hostdata->autosense,
719 struct NCR5380_cmd, list);
720 list_del(&ncmd->list);
721 cmd = NCR5380_to_scmd(ncmd);
722 dsprintk(NDEBUG_QUEUES, instance,
723 "dequeue: removed %p from autosense queue\n", cmd);
724 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
725 hostdata->sensing = cmd;
726 return cmd;
727 }
728 return NULL;
729}
730
731static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
732{
733 struct NCR5380_hostdata *hostdata = shost_priv(instance);
734 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
735
Finn Thain8d5dbec2016-02-23 10:07:09 +1100736 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100737 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
738 list_add(&ncmd->list, &hostdata->autosense);
739 hostdata->sensing = NULL;
740 } else
741 list_add(&ncmd->list, &hostdata->unissued);
742}
743
744/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100745 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100747 * NCR5380_main is a coroutine that runs as long as more work can
748 * be done on the NCR5380 host adapters in a system. Both
749 * NCR5380_queue_command() and NCR5380_intr() will try to start it
750 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
752
David Howellsc4028952006-11-22 14:57:56 +0000753static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
David Howellsc4028952006-11-22 14:57:56 +0000755 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100756 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100762
Finn Thain0a4e3612016-01-03 16:06:07 +1100763 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100764 while (!hostdata->connected && !hostdata->selecting) {
765 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
766
767 if (!cmd)
768 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100769
770 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100773 * Attempt to establish an I_T_L nexus here.
774 * On success, instance->hostdata->connected is set.
775 * On failure, we must add the command back to the
776 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100778 /*
779 * REQUEST SENSE commands are issued without tagged
780 * queueing, even on SCSI-II devices because the
781 * contingent allegiance condition exists for the
782 * entire unit.
783 */
Finn Thain32b26a12016-01-03 16:05:58 +1100784
Finn Thainccf6efd2016-02-23 10:07:08 +1100785 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100786 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +1100787 } else {
788 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
789 "main: select failed, returning %p to queue\n", cmd);
790 requeue_cmd(instance, cmd);
791 }
792 }
Finn Thaine4dec682016-03-23 21:10:12 +1100793 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100794 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100797 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100798 spin_unlock_irq(&hostdata->lock);
799 if (!done)
800 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804#ifndef DONT_USE_INTR
805
806/**
Finn Thaincd400822016-01-03 16:05:40 +1100807 * NCR5380_intr - generic NCR5380 irq handler
808 * @irq: interrupt number
809 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
Finn Thaincd400822016-01-03 16:05:40 +1100811 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
812 * from the disconnected queue, and restarting NCR5380_main()
813 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Finn Thaincd400822016-01-03 16:05:40 +1100815 * The chip can assert IRQ in any of six different conditions. The IRQ flag
816 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
817 * Three of these six conditions are latched in the Bus and Status Register:
818 * - End of DMA (cleared by ending DMA Mode)
819 * - Parity error (cleared by reading RPIR)
820 * - Loss of BSY (cleared by reading RPIR)
821 * Two conditions have flag bits that are not latched:
822 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
823 * - Bus reset (non-maskable)
824 * The remaining condition has no flag bit at all:
825 * - Selection/reselection
826 *
827 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
828 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
829 * claimed that "the design of the [DP8490] interrupt logic ensures
830 * interrupts will not be lost (they can be on the DP5380)."
831 * The L5380/53C80 datasheet from LOGIC Devices has more details.
832 *
833 * Checking for bus reset by reading RST is futile because of interrupt
834 * latency, but a bus reset will reset chip logic. Checking for parity error
835 * is unnecessary because that interrupt is never enabled. A Loss of BSY
836 * condition will clear DMA Mode. We can tell when this occurs because the
837 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
839
Finn Thaincd400822016-01-03 16:05:40 +1100840static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800842 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100843 struct NCR5380_hostdata *hostdata = shost_priv(instance);
844 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned char basr;
846 unsigned long flags;
847
Finn Thain11d2f632016-01-03 16:05:51 +1100848 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Finn Thaincd400822016-01-03 16:05:40 +1100850 basr = NCR5380_read(BUS_AND_STATUS_REG);
851 if (basr & BASR_IRQ) {
852 unsigned char mr = NCR5380_read(MODE_REG);
853 unsigned char sr = NCR5380_read(STATUS_REG);
854
Finn Thainb7465452016-01-03 16:06:05 +1100855 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
856 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100857
Finn Thaincd400822016-01-03 16:05:40 +1100858 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
859 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
860 /* Probably reselected */
861 NCR5380_write(SELECT_ENABLE_REG, 0);
862 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
863
Finn Thainb7465452016-01-03 16:06:05 +1100864 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100865
866 if (!hostdata->connected) {
867 NCR5380_reselect(instance);
868 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Finn Thaincd400822016-01-03 16:05:40 +1100870 if (!hostdata->connected)
871 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
872 } else {
873 /* Probably Bus Reset */
874 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
875
Finn Thainb7465452016-01-03 16:06:05 +1100876 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +1100877 }
878 handled = 1;
879 } else {
880 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
881 }
882
Finn Thain11d2f632016-01-03 16:05:51 +1100883 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100884
885 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Finn Thainaff0cf92016-01-03 16:06:09 +1100888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Finn Thainaff0cf92016-01-03 16:06:09 +1100890/*
Finn Thain710ddd02014-11-12 16:12:02 +1100891 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +1100892 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 *
894 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +1100895 * including ARBITRATION, SELECTION, and initial message out for
896 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100898 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +1100899 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +1100900 *
Finn Thain707d62b2016-01-03 16:06:02 +1100901 * Returns cmd if selection failed but should be retried,
902 * NULL if selection failed and should not be retried, or
903 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100905 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100906 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
907 * with registers as they should have been on entry - ie
908 * SELECT_ENABLE will be set appropriately, the NCR5380
909 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100911 * If successful : I_T_L or I_T_L_Q nexus will be established,
912 * instance->connected will be set to cmd.
913 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100915 * If failed (no target) : cmd->scsi_done() will be called, and the
916 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100918
Finn Thain707d62b2016-01-03 16:06:02 +1100919static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
920 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Finn Thaine8a60142016-01-03 16:05:54 +1100922 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned char tmp[3], phase;
924 unsigned char *data;
925 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100929 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
930 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Finn Thain707d62b2016-01-03 16:06:02 +1100932 /*
933 * Arbitration and selection phases are slow and involve dropping the
934 * lock, so we have to watch out for EH. An exception handler may
935 * change 'selecting' to NULL. This function will then return NULL
936 * so that the caller will forget about 'cmd'. (During information
937 * transfer phases, EH may change 'connected' to NULL.)
938 */
939 hostdata->selecting = cmd;
940
Finn Thainaff0cf92016-01-03 16:06:09 +1100941 /*
942 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * data bus during SELECTION.
944 */
945
946 NCR5380_write(TARGET_COMMAND_REG, 0);
947
Finn Thainaff0cf92016-01-03 16:06:09 +1100948 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * Start arbitration.
950 */
951
952 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
953 NCR5380_write(MODE_REG, MR_ARBITRATE);
954
Finn Thain55500d92016-01-03 16:05:35 +1100955 /* The chip now waits for BUS FREE phase. Then after the 800 ns
956 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958
Finn Thain11d2f632016-01-03 16:05:51 +1100959 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100960 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
961 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
962 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100963 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100964 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
965 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100966 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +1100967 }
Finn Thainccf6efd2016-02-23 10:07:08 +1100968 if (!hostdata->selecting) {
969 /* Command was aborted */
970 NCR5380_write(MODE_REG, MR_BASE);
971 goto out;
972 }
Finn Thainb32ade12016-01-03 16:05:41 +1100973 if (err < 0) {
974 NCR5380_write(MODE_REG, MR_BASE);
975 shost_printk(KERN_ERR, instance,
976 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +1100977 goto out;
Finn Thain55500d92016-01-03 16:05:35 +1100978 }
Finn Thain11d2f632016-01-03 16:05:51 +1100979 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +1100980
981 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 udelay(3);
983
984 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +1100985 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
986 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
987 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +1100989 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +1100990 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +1100991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Finn Thaincf13b082016-01-03 16:05:18 +1100993
994 /* After/during arbitration, BSY should be asserted.
995 * IBM DPES-31080 Version S31Q works now
996 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
997 */
998 NCR5380_write(INITIATOR_COMMAND_REG,
999 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Finn Thainaff0cf92016-01-03 16:06:09 +11001001 /*
1002 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 * a minimum so we'll udelay ceil(1.2)
1004 */
1005
Finn Thain9c3f0e22016-01-03 16:05:11 +11001006 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1007 udelay(15);
1008 else
1009 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Finn Thain11d2f632016-01-03 16:05:51 +11001011 spin_lock_irq(&hostdata->lock);
1012
Finn Thain72064a72016-01-03 16:05:44 +11001013 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1014 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001015 goto out;
1016
1017 if (!hostdata->selecting) {
1018 NCR5380_write(MODE_REG, MR_BASE);
1019 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1020 goto out;
1021 }
Finn Thain72064a72016-01-03 16:05:44 +11001022
Finn Thainb7465452016-01-03 16:06:05 +11001023 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Finn Thainaff0cf92016-01-03 16:06:09 +11001025 /*
1026 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * the host and target ID's on the SCSI bus.
1028 */
1029
Finn Thain3d07d222016-01-03 16:06:13 +11001030 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Finn Thainaff0cf92016-01-03 16:06:09 +11001032 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * Raise ATN while SEL is true before BSY goes false from arbitration,
1034 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1035 * phase immediately after selection.
1036 */
1037
Finn Thain3d07d222016-01-03 16:06:13 +11001038 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1039 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 NCR5380_write(MODE_REG, MR_BASE);
1041
Finn Thainaff0cf92016-01-03 16:06:09 +11001042 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * Reselect interrupts must be turned off prior to the dropping of BSY,
1044 * otherwise we will trigger an interrupt.
1045 */
1046 NCR5380_write(SELECT_ENABLE_REG, 0);
1047
Finn Thain11d2f632016-01-03 16:05:51 +11001048 spin_unlock_irq(&hostdata->lock);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001051 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * the BSY signal.
1053 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001054 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001057 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1058 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Finn Thainaff0cf92016-01-03 16:06:09 +11001060 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001062 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * appropriate propagation delay has expired, and we're confusing
1064 * a BSY signal from ourselves as the target's response to SELECTION.
1065 *
1066 * A small delay (the 'C++' frontend breaks the pipeline with an
1067 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001068 * tighter 'C' code breaks and requires this) solves the problem -
1069 * the 1 us delay is arbitrary, and only used because this delay will
1070 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * work there.
1072 *
1073 * wingel suggests that this could be due to failing to wait
1074 * one deskew delay.
1075 */
1076
1077 udelay(1);
1078
Finn Thainb7465452016-01-03 16:06:05 +11001079 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Finn Thainaff0cf92016-01-03 16:06:09 +11001081 /*
1082 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * selection.
1084 */
1085
Finn Thainae753a32016-01-03 16:05:23 +11001086 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1087 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001090 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1092 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001093 if (!hostdata->connected)
1094 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001095 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001096 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Finn Thainae753a32016-01-03 16:05:23 +11001098
1099 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001100 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001101 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001102 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001103 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1104 if (hostdata->selecting) {
1105 cmd->result = DID_BAD_TARGET << 16;
1106 complete_cmd(instance, cmd);
1107 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1108 cmd = NULL;
1109 }
1110 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001111 }
1112
Finn Thainaff0cf92016-01-03 16:06:09 +11001113 /*
1114 * No less than two deskew delays after the initiator detects the
1115 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * change the DATA BUS. -wingel
1117 */
1118
1119 udelay(1);
1120
1121 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001124 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * was true but before BSY was false during selection, the information
1126 * transfer phase should be a MESSAGE OUT phase so that we can send the
1127 * IDENTIFY message.
Finn Thainaff0cf92016-01-03 16:06:09 +11001128 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1130 * message (2 bytes) with a tag ID that we increment with every command
1131 * until it wraps back to 0.
1132 *
1133 * XXX - it turns out that there are some broken SCSI-II devices,
Finn Thain594d4ba2016-01-03 16:06:10 +11001134 * which claim to support tagged queuing but fail when more than
1135 * some number of commands are issued at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
1137
1138 /* Wait for start of REQ/ACK handshake */
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001141 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001142 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001143 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1144 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001146 goto out;
1147 }
1148 if (!hostdata->selecting) {
1149 do_abort(instance);
1150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
Finn Thainb7465452016-01-03 16:06:05 +11001153 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1154 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001155 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 len = 1;
1158 cmd->tag = 0;
1159
1160 /* Send message(s) */
1161 data = tmp;
1162 phase = PHASE_MSGOUT;
1163 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001164 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001168 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Boaz Harrosh28424d32007-09-10 22:37:45 +03001170 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Finn Thain707d62b2016-01-03 16:06:02 +11001172 cmd = NULL;
1173
1174out:
1175 if (!hostdata->selecting)
1176 return NULL;
1177 hostdata->selecting = NULL;
1178 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Finn Thainaff0cf92016-01-03 16:06:09 +11001181/*
1182 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001183 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 *
1185 * Purpose : transfers data in given phase using polled I/O
1186 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001187 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001188 * what phase is expected, *count - pointer to number of
1189 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001192 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001193 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001195 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 *
1197 * XXX Note : handling for bus free may be useful.
1198 */
1199
1200/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001201 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 * IS 100% reliable, and for the actual data transfer where speed
1203 * counts, we will always do a pseudo DMA or DMA transfer.
1204 */
1205
Finn Thain0d2cf862016-01-03 16:06:11 +11001206static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1207 unsigned char *phase, int *count,
1208 unsigned char **data)
1209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 unsigned char p = *phase, tmp;
1211 int c = *count;
1212 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Finn Thainaff0cf92016-01-03 16:06:09 +11001214 /*
1215 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * phase specified in the appropriate bits of the TARGET COMMAND
1217 * REGISTER match the STATUS REGISTER
1218 */
1219
Finn Thain0d2cf862016-01-03 16:06:11 +11001220 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001223 /*
1224 * Wait for assertion of REQ, after which the phase bits will be
1225 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
1227
Finn Thain686f3992016-01-03 16:05:26 +11001228 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Finn Thainb7465452016-01-03 16:06:05 +11001231 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001234 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001235 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1236 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 break;
1238 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* Do actual transfer from SCSI bus to / from memory */
1241 if (!(p & SR_IO))
1242 NCR5380_write(OUTPUT_DATA_REG, *d);
1243 else
1244 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1245
1246 ++d;
1247
Finn Thainaff0cf92016-01-03 16:06:09 +11001248 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * The SCSI standard suggests that in MSGOUT phase, the initiator
1250 * should drop ATN on the last byte of the message phase
1251 * after REQ has been asserted for the handshake but before
1252 * the initiator raises ACK.
1253 */
1254
1255 if (!(p & SR_IO)) {
1256 if (!((p & SR_MSG) && c > 1)) {
1257 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1258 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001259 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1260 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001262 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1263 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 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_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 } else {
1269 NCR5380_dprint(NDEBUG_PIO, instance);
1270 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1271 }
1272
Finn Thaina2edc4a2016-01-03 16:05:27 +11001273 if (NCR5380_poll_politely(instance,
1274 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1275 break;
1276
Finn Thainb7465452016-01-03 16:06:05 +11001277 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001280 * We have several special cases to consider during REQ/ACK handshaking :
1281 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001282 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001284 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001285 * message. We must exit with ACK asserted, so that the calling
1286 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 *
1288 * 3. ACK and ATN are clear and the target may proceed as normal.
1289 */
1290 if (!(p == PHASE_MSGIN && c == 1)) {
1291 if (p == PHASE_MSGOUT && c > 1)
1292 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1293 else
1294 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1295 }
1296 } while (--c);
1297
Finn Thainb7465452016-01-03 16:06:05 +11001298 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 *count = c;
1301 *data = d;
1302 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001303 /* The phase read from the bus is valid if either REQ is (already)
1304 * asserted or if ACK hasn't been released yet. The latter applies if
1305 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1306 */
1307 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 *phase = tmp & PHASE_MASK;
1309 else
1310 *phase = PHASE_UNKNOWN;
1311
1312 if (!c || (*phase == p))
1313 return 0;
1314 else
1315 return -1;
1316}
1317
1318/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001319 * do_reset - issue a reset command
1320 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001322 * Issue a reset sequence to the NCR5380 and try and get the bus
1323 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001325 * This clears the reset interrupt flag because there may be no handler for
1326 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1327 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001329
Finn Thain54d8fe42016-01-03 16:05:06 +11001330static void do_reset(struct Scsi_Host *instance)
1331{
Finn Thain636b1ec2016-01-03 16:05:10 +11001332 unsigned long flags;
1333
1334 local_irq_save(flags);
1335 NCR5380_write(TARGET_COMMAND_REG,
1336 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001338 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001340 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1341 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Finn Thain80d3eb62016-01-03 16:05:34 +11001344/**
1345 * do_abort - abort the currently established nexus by going to
1346 * MESSAGE OUT phase and sending an ABORT message.
1347 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001349 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
1351
Finn Thain54d8fe42016-01-03 16:05:06 +11001352static int do_abort(struct Scsi_Host *instance)
1353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 unsigned char *msgptr, phase, tmp;
1355 int len;
1356 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /* Request message out phase */
1359 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1360
Finn Thainaff0cf92016-01-03 16:06:09 +11001361 /*
1362 * Wait for the target to indicate a valid phase by asserting
1363 * REQ. Once this happens, we'll have either a MSGOUT phase
1364 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001366 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * We really don't care what value was on the bus or what value
1368 * the target sees, so we just handshake.
1369 */
1370
Finn Thain80d3eb62016-01-03 16:05:34 +11001371 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001372 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001373 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Finn Thainf35d3472016-01-03 16:05:33 +11001375 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1378
Finn Thainf35d3472016-01-03 16:05:33 +11001379 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001380 NCR5380_write(INITIATOR_COMMAND_REG,
1381 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001382 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001383 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001384 goto timeout;
1385 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 tmp = ABORT;
1389 msgptr = &tmp;
1390 len = 1;
1391 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001392 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /*
1395 * If we got here, and the command completed successfully,
1396 * we're about to go into bus free state.
1397 */
1398
1399 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001400
1401timeout:
1402 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1403 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Finn Thainaff0cf92016-01-03 16:06:09 +11001406/*
1407 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001408 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 *
1410 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001411 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001413 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001414 * what phase is expected, *count - pointer to number of
1415 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001416 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001418 * maximum number of bytes, 0 if all bytes or transferred or exit
1419 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001421 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
1423
1424
Finn Thain0d2cf862016-01-03 16:06:11 +11001425static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1426 unsigned char *phase, int *count,
1427 unsigned char **data)
1428{
1429 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 register int c = *count;
1431 register unsigned char p = *phase;
1432 register unsigned char *d = *data;
1433 unsigned char tmp;
Finn Thain438af512016-03-23 21:10:18 +11001434 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1437 *phase = tmp;
1438 return -1;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /*
1444 * Note : on my sample board, watch-dog timeouts occurred when interrupts
Finn Thainaff0cf92016-01-03 16:06:09 +11001445 * were not disabled for the duration of a single DMA transfer, from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * before the setting of DMA mode to after transfer of the last byte.
1447 */
1448
Finn Thain1bb46002016-03-23 21:10:14 +11001449 if (hostdata->flags & FLAG_DMA_FIXUP)
1450 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
1451 else
Finn Thaincd400822016-01-03 16:05:40 +11001452 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1453 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Finn Thain52a6a1c2014-03-18 11:42:18 +11001455 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Finn Thainaff0cf92016-01-03 16:06:09 +11001457 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001458 * On the PAS16 at least I/O recovery delays are not needed here.
1459 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
1461
1462 if (p & SR_IO) {
Finn Thaine5d55d12016-03-23 21:10:16 +11001463 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1465 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001466 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001468 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001470 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/*
Finn Thaine4dec682016-03-23 21:10:12 +11001474 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001475 *
1476 * For DMA sends, we want to wait until the last byte has been
1477 * transferred out over the bus before we turn off DMA mode. Alas, there
1478 * seems to be no terribly good way of doing this on a 5380 under all
1479 * conditions. For non-scatter-gather operations, we can wait until REQ
1480 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1481 * are nastier, since the device will be expecting more data than we
1482 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1483 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1484 * why this signal was added to the newer chips) but on the older 538[01]
1485 * this signal does not exist. The workaround for this lack is a watchdog;
1486 * we bail out of the wait-loop after a modest amount of wait-time if
1487 * the usual exit conditions are not met. Not a terribly clean or
1488 * correct solution :-%
1489 *
1490 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1491 * If the chip is in DMA receive mode, it will respond to a target's
1492 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1493 * ACK, even if it has _already_ been notified by the DMA controller that
1494 * the current DMA transfer has completed! If the NCR5380 is then taken
1495 * out of DMA mode, this already-acknowledged byte is lost. This is
1496 * not a problem for "one DMA transfer per READ command", because
1497 * the situation will never arise... either all of the data is DMA'ed
1498 * properly, or the target switches to MESSAGE IN phase to signal a
1499 * disconnection (either operation bringing the DMA to a clean halt).
1500 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001501 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001502 * condition before taking the NCR5380 out of DMA mode. One or two extra
1503 * bytes are transferred via PIO as necessary to fill out the original
1504 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 */
1506
1507 if (p & SR_IO) {
Finn Thain438af512016-03-23 21:10:18 +11001508 result = NCR5380_dma_recv_setup(instance, d,
Finn Thain1bb46002016-03-23 21:10:14 +11001509 hostdata->flags & FLAG_DMA_FIXUP ? c - 1 : c);
Finn Thain438af512016-03-23 21:10:18 +11001510 if (!result && (hostdata->flags & FLAG_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001512 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001513 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * the chip to latch the last byte, read it, and then disable
1515 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001516 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1518 * REQ is deasserted when ACK is asserted, and not reasserted
1519 * until ACK goes false. Since the NCR5380 won't lower ACK
1520 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001521 * the DMA port or we take the NCR5380 out of DMA mode, we
1522 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * byte.
1524 */
1525
Finn Thain55181be2016-01-03 16:05:42 +11001526 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1527 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001528 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001529 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Finn Thain55181be2016-01-03 16:05:42 +11001531 if (NCR5380_poll_politely(instance, STATUS_REG,
1532 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001533 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001534 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1535 }
1536 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 } else {
Finn Thain438af512016-03-23 21:10:18 +11001539 result = NCR5380_dma_send_setup(instance, d, c);
1540 if (!result && (hostdata->flags & FLAG_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001542 * Wait for the last byte to be sent. If REQ is being asserted for
1543 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Finn Thain55181be2016-01-03 16:05:42 +11001545 if (NCR5380_poll_politely2(instance,
1546 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1547 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001548 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001549 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 NCR5380_write(MODE_REG, MR_BASE);
1554 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001555 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 *data = d + c;
1557 *count = 0;
Finn Thain438af512016-03-23 21:10:18 +11001558 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561/*
1562 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1563 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001564 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001565 * directs us to. Operates on the currently connected command,
1566 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 *
1568 * Inputs : instance, instance for which we are doing commands
1569 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001570 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001571 * modified if a command disconnects, *instance->connected will
1572 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001574 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001575 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
1577
Finn Thain0d2cf862016-01-03 16:06:11 +11001578static void NCR5380_information_transfer(struct Scsi_Host *instance)
1579{
Finn Thaine8a60142016-01-03 16:05:54 +11001580 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 unsigned char msgout = NOP;
1582 int sink = 0;
1583 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 unsigned char *data;
1586 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001587 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Finn Thain11d2f632016-01-03 16:05:51 +11001589 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001590 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 tmp = NCR5380_read(STATUS_REG);
1593 /* We only have a valid SCSI phase when REQ is asserted */
1594 if (tmp & SR_REQ) {
1595 phase = (tmp & PHASE_MASK);
1596 if (phase != old_phase) {
1597 old_phase = phase;
1598 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1599 }
1600 if (sink && (phase != PHASE_MSGOUT)) {
1601 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1602
Finn Thain3d07d222016-01-03 16:06:13 +11001603 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1604 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001605 while (NCR5380_read(STATUS_REG) & SR_REQ)
1606 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001607 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1608 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 sink = 0;
1610 continue;
1611 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 case PHASE_DATAOUT:
1615#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001616 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 sink = 1;
1618 do_abort(instance);
1619 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001620 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001621 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return;
1623#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001624 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001625 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 * If there is no room left in the current buffer in the
1627 * scatter-gather list, move onto the next one.
1628 */
1629
1630 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1631 ++cmd->SCp.buffer;
1632 --cmd->SCp.buffers_residual;
1633 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001634 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001635 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1636 cmd->SCp.this_residual,
1637 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001641 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * PSEUDO-DMA for systems that are strictly PIO,
1643 * since we can let the hardware do the handshaking.
1644 *
1645 * For this to work, we need to know the transfersize
1646 * ahead of time, since the pseudo-DMA code will sit
1647 * in an unconditional loop.
1648 */
1649
Finn Thainff3d4572016-01-03 16:05:25 +11001650 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001651 if (!cmd->device->borken)
Finn Thainff3d4572016-01-03 16:05:25 +11001652 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Finn Thain438af512016-03-23 21:10:18 +11001654 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001656 if (NCR5380_transfer_dma(instance, &phase,
1657 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001659 * If the watchdog timer fires, all future
1660 * accesses to this device will use the
1661 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001663 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001664 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 sink = 1;
1667 do_abort(instance);
1668 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* XXX - need to source or sink data here, as appropriate */
1670 } else
1671 cmd->SCp.this_residual -= transfersize - len;
Finn Thainf825e402016-03-23 21:10:15 +11001672 } else {
Finn Thain16788472016-02-23 10:07:05 +11001673 /* Break up transfer into 3 ms chunks,
1674 * presuming 6 accesses per handshake.
1675 */
1676 transfersize = min((unsigned long)cmd->SCp.this_residual,
1677 hostdata->accesses_per_ms / 2);
1678 len = transfersize;
1679 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001680 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001681 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001682 }
Finn Thain16788472016-02-23 10:07:05 +11001683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 case PHASE_MSGIN:
1685 len = 1;
1686 data = &tmp;
1687 NCR5380_transfer_pio(instance, &phase, &len, &data);
1688 cmd->SCp.Message = tmp;
1689
1690 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 case ABORT:
1692 case COMMAND_COMPLETE:
1693 /* Accept message by clearing ACK */
1694 sink = 1;
1695 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001696 dsprintk(NDEBUG_QUEUES, instance,
1697 "COMMAND COMPLETE %p target %d lun %llu\n",
1698 cmd, scmd_id(cmd), cmd->device->lun);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Finn Thainf27db8e2016-01-03 16:06:00 +11001702 cmd->result &= ~0xffff;
1703 cmd->result |= cmd->SCp.Status;
1704 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Finn Thainf27db8e2016-01-03 16:06:00 +11001706 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001707 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001708 else {
1709 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1710 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1711 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1712 cmd);
1713 list_add_tail(&ncmd->list,
1714 &hostdata->autosense);
1715 } else
1716 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
Finn Thainaff0cf92016-01-03 16:06:09 +11001719 /*
1720 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * arbitration can resume.
1722 */
1723 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001724
1725 /* Enable reselect interrupts */
1726 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return;
1728 case MESSAGE_REJECT:
1729 /* Accept message by clearing ACK */
1730 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1731 switch (hostdata->last_message) {
1732 case HEAD_OF_QUEUE_TAG:
1733 case ORDERED_QUEUE_TAG:
1734 case SIMPLE_QUEUE_TAG:
1735 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001736 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 break;
1738 default:
1739 break;
1740 }
Finn Thain340b9612016-01-03 16:05:31 +11001741 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001742 case DISCONNECT:
1743 /* Accept message by clearing ACK */
1744 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1745 hostdata->connected = NULL;
1746 list_add(&ncmd->list, &hostdata->disconnected);
1747 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1748 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1749 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001750
Finn Thain0d2cf862016-01-03 16:06:11 +11001751 /*
1752 * Restore phase bits to 0 so an interrupted selection,
1753 * arbitration can resume.
1754 */
1755 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Finn Thain0d2cf862016-01-03 16:06:11 +11001757 /* Enable reselect interrupts */
1758 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1759 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001760 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001762 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * ignore SAVE/RESTORE pointers calls.
1764 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001765 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001767 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * compatible.
1769 */
1770 case SAVE_POINTERS:
1771 case RESTORE_POINTERS:
1772 /* Accept message by clearing ACK */
1773 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1774 break;
1775 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001776 /*
1777 * Start the message buffer with the EXTENDED_MESSAGE
1778 * byte, since spi_print_msg() wants the whole thing.
1779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 extended_msg[0] = EXTENDED_MESSAGE;
1781 /* Accept first byte by clearing ACK */
1782 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001783
1784 spin_unlock_irq(&hostdata->lock);
1785
Finn Thainb7465452016-01-03 16:06:05 +11001786 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 len = 2;
1789 data = extended_msg + 1;
1790 phase = PHASE_MSGIN;
1791 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001792 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1793 (int)extended_msg[1],
1794 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Finn Thaine0783ed2016-01-03 16:05:45 +11001796 if (!len && extended_msg[1] > 0 &&
1797 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* Accept third byte by clearing ACK */
1799 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1800 len = extended_msg[1] - 1;
1801 data = extended_msg + 3;
1802 phase = PHASE_MSGIN;
1803
1804 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001805 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1806 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 switch (extended_msg[2]) {
1809 case EXTENDED_SDTR:
1810 case EXTENDED_WDTR:
1811 case EXTENDED_MODIFY_DATA_POINTER:
1812 case EXTENDED_EXTENDED_IDENTIFY:
1813 tmp = 0;
1814 }
1815 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001816 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 tmp = 0;
1818 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001819 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1820 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 tmp = 0;
1822 }
Finn Thain11d2f632016-01-03 16:05:51 +11001823
1824 spin_lock_irq(&hostdata->lock);
1825 if (!hostdata->connected)
1826 return;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* Fall through to reject message */
1829
Finn Thainaff0cf92016-01-03 16:06:09 +11001830 /*
1831 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * reject it.
1833 */
1834 default:
1835 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001836 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001837 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 printk("\n");
1839 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001840 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001841 "rejecting unknown message %02x\n",
1842 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 else
Jeff Garzik017560f2005-10-24 18:04:36 -04001844 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001845 "rejecting unknown extended message code %02x, length %d\n",
1846 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 msgout = MESSAGE_REJECT;
1849 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1850 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001851 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 break;
1853 case PHASE_MSGOUT:
1854 len = 1;
1855 data = &msgout;
1856 hostdata->last_message = msgout;
1857 NCR5380_transfer_pio(instance, &phase, &len, &data);
1858 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 hostdata->connected = NULL;
1860 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001861 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1863 return;
1864 }
1865 msgout = NOP;
1866 break;
1867 case PHASE_CMDOUT:
1868 len = cmd->cmd_len;
1869 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001870 /*
1871 * XXX for performance reasons, on machines with a
1872 * PSEUDO-DMA architecture we should probably
1873 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
1875 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 break;
1877 case PHASE_STATIN:
1878 len = 1;
1879 data = &tmp;
1880 NCR5380_transfer_pio(instance, &phase, &len, &data);
1881 cmd->SCp.Status = tmp;
1882 break;
1883 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001884 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001885 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001886 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001887 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001888 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11001889 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001890 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Finn Thain11d2f632016-01-03 16:05:51 +11001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895/*
1896 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1897 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001898 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11001899 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1900 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11001901 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 */
1904
Finn Thain0d2cf862016-01-03 16:06:11 +11001905static void NCR5380_reselect(struct Scsi_Host *instance)
1906{
Finn Thaine8a60142016-01-03 16:05:54 +11001907 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 unsigned char target_mask;
1909 unsigned char lun, phase;
1910 int len;
1911 unsigned char msg[3];
1912 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11001913 struct NCR5380_cmd *ncmd;
1914 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /*
1917 * Disable arbitration, etc. since the host adapter obviously
1918 * lost, and tell an interrupted NCR5380_select() to restart.
1919 */
1920
1921 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11001924
1925 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Finn Thainaff0cf92016-01-03 16:06:09 +11001927 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 * At this point, we have detected that our SCSI ID is on the bus,
1929 * SEL is true and BSY was false for at least one bus settle delay
1930 * (400 ns).
1931 *
1932 * We must assert BSY ourselves, until the target drops the SEL
1933 * signal.
1934 */
1935
1936 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11001937 if (NCR5380_poll_politely(instance,
1938 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
1939 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1940 return;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1943
1944 /*
1945 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
1947
Finn Thain1cc160e2016-01-03 16:05:32 +11001948 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11001949 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
1950 do_abort(instance);
1951 return;
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 len = 1;
1955 data = msg;
1956 phase = PHASE_MSGIN;
1957 NCR5380_transfer_pio(instance, &phase, &len, &data);
1958
Finn Thain72064a72016-01-03 16:05:44 +11001959 if (len) {
1960 do_abort(instance);
1961 return;
1962 }
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11001965 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001966 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11001967 printk("\n");
1968 do_abort(instance);
1969 return;
1970 }
1971 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Finn Thain72064a72016-01-03 16:05:44 +11001973 /*
1974 * We need to add code for SCSI-II to track which devices have
1975 * I_T_L_Q nexuses established, and which have simple I_T_L
1976 * nexuses so we can chose to do additional data transfer.
1977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Finn Thain72064a72016-01-03 16:05:44 +11001979 /*
1980 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
1981 * just reestablished, and remove it from the disconnected queue.
1982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Finn Thain32b26a12016-01-03 16:05:58 +11001984 tmp = NULL;
1985 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
1986 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
1987
1988 if (target_mask == (1 << scmd_id(cmd)) &&
1989 lun == (u8)cmd->device->lun) {
1990 list_del(&ncmd->list);
1991 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11001992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11001995
1996 if (tmp) {
1997 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
1998 "reselect: removed %p from disconnected queue\n", tmp);
1999 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002000 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2001 target_mask, lun);
2002 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002003 * Since we have an established nexus that we can't do anything
2004 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Finn Thain72064a72016-01-03 16:05:44 +11002009
2010 /* Accept message by clearing ACK */
2011 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2012
2013 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002014 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2015 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Finn Thain8b00c3d2016-01-03 16:06:01 +11002018/**
2019 * list_find_cmd - test for presence of a command in a linked list
2020 * @haystack: list of commands
2021 * @needle: command to search for
2022 */
2023
2024static bool list_find_cmd(struct list_head *haystack,
2025 struct scsi_cmnd *needle)
2026{
2027 struct NCR5380_cmd *ncmd;
2028
2029 list_for_each_entry(ncmd, haystack, list)
2030 if (NCR5380_to_scmd(ncmd) == needle)
2031 return true;
2032 return false;
2033}
2034
2035/**
2036 * list_remove_cmd - remove a command from linked list
2037 * @haystack: list of commands
2038 * @needle: command to remove
2039 */
2040
2041static bool list_del_cmd(struct list_head *haystack,
2042 struct scsi_cmnd *needle)
2043{
2044 if (list_find_cmd(haystack, needle)) {
2045 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2046
2047 list_del(&ncmd->list);
2048 return true;
2049 }
2050 return false;
2051}
2052
2053/**
2054 * NCR5380_abort - scsi host eh_abort_handler() method
2055 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002057 * Try to abort a given command by removing it from queues and/or sending
2058 * the target an abort message. This may not succeed in causing a target
2059 * to abort the command. Nonetheless, the low-level driver must forget about
2060 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002062 * The normal path taken by a command is as follows. For EH we trace this
2063 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002065 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2066 * [disconnected -> connected ->]...
2067 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002069 * If cmd was not found at all then presumably it has already been completed,
2070 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002071 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002072 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002073 * We have no option but to forget the aborted command (even if it still
2074 * lacks sense data). The mid-layer may re-issue a command that is in error
2075 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2076 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002077 *
2078 * The lock protects driver data structures, but EH handlers also use it
2079 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
2081
Finn Thain710ddd02014-11-12 16:12:02 +11002082static int NCR5380_abort(struct scsi_cmnd *cmd)
2083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002085 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002086 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002087 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002088
Finn Thain11d2f632016-01-03 16:05:51 +11002089 spin_lock_irqsave(&hostdata->lock, flags);
2090
Finn Thain32b26a12016-01-03 16:05:58 +11002091#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002092 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002093#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002094 NCR5380_dprint(NDEBUG_ANY, instance);
2095 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Finn Thain8b00c3d2016-01-03 16:06:01 +11002097 if (list_del_cmd(&hostdata->unissued, cmd)) {
2098 dsprintk(NDEBUG_ABORT, instance,
2099 "abort: removed %p from issue queue\n", cmd);
2100 cmd->result = DID_ABORT << 16;
2101 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002102 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002103 }
2104
Finn Thain707d62b2016-01-03 16:06:02 +11002105 if (hostdata->selecting == cmd) {
2106 dsprintk(NDEBUG_ABORT, instance,
2107 "abort: cmd %p == selecting\n", cmd);
2108 hostdata->selecting = NULL;
2109 cmd->result = DID_ABORT << 16;
2110 complete_cmd(instance, cmd);
2111 goto out;
2112 }
2113
Finn Thain8b00c3d2016-01-03 16:06:01 +11002114 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2115 dsprintk(NDEBUG_ABORT, instance,
2116 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002117 /* Can't call NCR5380_select() and send ABORT because that
2118 * means releasing the lock. Need a bus reset.
2119 */
Finn Thaindc183962016-02-23 10:07:07 +11002120 set_host_byte(cmd, DID_ERROR);
2121 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002122 result = FAILED;
2123 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002124 }
2125
2126 if (hostdata->connected == cmd) {
2127 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2128 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002129 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002130 if (do_abort(instance)) {
2131 set_host_byte(cmd, DID_ERROR);
2132 complete_cmd(instance, cmd);
2133 result = FAILED;
2134 goto out;
2135 }
2136 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002137 complete_cmd(instance, cmd);
2138 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002139 }
2140
Finn Thaindc183962016-02-23 10:07:07 +11002141 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002142 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002143 "abort: removed %p from sense queue\n", cmd);
2144 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002145 complete_cmd(instance, cmd);
2146 }
2147
2148out:
2149 if (result == FAILED)
2150 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2151 else
2152 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2153
2154 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002155 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002156
Finn Thain8b00c3d2016-01-03 16:06:01 +11002157 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
2160
Finn Thain3be1b3e2016-01-03 16:05:12 +11002161/**
2162 * NCR5380_bus_reset - reset the SCSI bus
2163 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002165 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
2167
Finn Thain710ddd02014-11-12 16:12:02 +11002168static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002169{
2170 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002171 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002172 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002173 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002174 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Finn Thain11d2f632016-01-03 16:05:51 +11002176 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002177
2178#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002179 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002180#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002181 NCR5380_dprint(NDEBUG_ANY, instance);
2182 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002183
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002184 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002185
Finn Thain62717f52016-01-03 16:06:03 +11002186 /* reset NCR registers */
2187 NCR5380_write(MODE_REG, MR_BASE);
2188 NCR5380_write(TARGET_COMMAND_REG, 0);
2189 NCR5380_write(SELECT_ENABLE_REG, 0);
2190
2191 /* After the reset, there are no more connected or disconnected commands
2192 * and no busy units; so clear the low-level status here to avoid
2193 * conflicts when the mid-level code tries to wake up the affected
2194 * commands!
2195 */
2196
Finn Thain1884c282016-02-23 10:07:04 +11002197 if (list_del_cmd(&hostdata->unissued, cmd)) {
2198 cmd->result = DID_RESET << 16;
2199 cmd->scsi_done(cmd);
2200 }
2201
2202 if (hostdata->selecting) {
2203 hostdata->selecting->result = DID_RESET << 16;
2204 complete_cmd(instance, hostdata->selecting);
2205 hostdata->selecting = NULL;
2206 }
Finn Thain62717f52016-01-03 16:06:03 +11002207
2208 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2209 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2210
2211 set_host_byte(cmd, DID_RESET);
2212 cmd->scsi_done(cmd);
2213 }
Finn Thain1884c282016-02-23 10:07:04 +11002214 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002215
2216 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2217 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2218
2219 set_host_byte(cmd, DID_RESET);
2220 cmd->scsi_done(cmd);
2221 }
Finn Thain1884c282016-02-23 10:07:04 +11002222 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002223
2224 if (hostdata->connected) {
2225 set_host_byte(hostdata->connected, DID_RESET);
2226 complete_cmd(instance, hostdata->connected);
2227 hostdata->connected = NULL;
2228 }
2229
Finn Thain62717f52016-01-03 16:06:03 +11002230 for (i = 0; i < 8; ++i)
2231 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002232 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002233
2234 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002235 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return SUCCESS;
2238}