blob: 826b63d1aa84c8665d36b46aa032155c52b7d58f [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#ifdef BOARD_REQUIRES_NO_DELAY
43#define io_recovery_delay(x)
44#else
45#define io_recovery_delay(x) udelay(x)
46#endif
47
48/*
49 * Design
50 *
Finn Thainaff0cf92016-01-03 16:06:09 +110051 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * one simply writes appropriate system specific macros (ie, data
Finn Thainaff0cf92016-01-03 16:06:09 +110053 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * memory mapped) and drops this file in their 'C' wrapper.
55 *
Finn Thainaff0cf92016-01-03 16:06:09 +110056 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * each 5380 in the system - commands that haven't been issued yet,
Finn Thainaff0cf92016-01-03 16:06:09 +110058 * and commands that are currently executing. This means that an
59 * unlimited number of commands may be queued, letting
60 * more commands propagate from the higher driver levels giving higher
61 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
62 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * while a command is already executing.
64 *
65 *
Finn Thainaff0cf92016-01-03 16:06:09 +110066 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
Finn Thainaff0cf92016-01-03 16:06:09 +110068 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
69 * piece of hardware that requires you to sit in a loop polling for
70 * the REQ signal as long as you are connected. Some devices are
71 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110072 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * broken devices are the exception rather than the rule and I'd rather
74 * spend my time optimizing for the normal case.
75 *
76 * Architecture :
77 *
78 * At the heart of the design is a coroutine, NCR5380_main,
79 * which is started from a workqueue for each NCR5380 host in the
80 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
81 * removing the commands from the issue queue and calling
Finn Thainaff0cf92016-01-03 16:06:09 +110082 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
84 * Once a nexus is established, the NCR5380_information_transfer()
85 * phase goes through the various phases as instructed by the target.
86 * if the target goes into MSG IN and sends a DISCONNECT message,
87 * the command structure is placed into the per instance disconnected
Finn Thainaff0cf92016-01-03 16:06:09 +110088 * queue, and NCR5380_main tries to find more work. If the target is
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * idle for too long, the system will try to sleep.
90 *
91 * If a command has disconnected, eventually an interrupt will trigger,
92 * calling NCR5380_intr() which will in turn call NCR5380_reselect
93 * to reestablish a nexus. This will run main if necessary.
94 *
Finn Thainaff0cf92016-01-03 16:06:09 +110095 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * appropriate.
97 *
Finn Thainaff0cf92016-01-03 16:06:09 +110098 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * structures, being initialized after the command is connected
100 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
101 * Note that in violation of the standard, an implicit SAVE POINTERS operation
102 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
103 */
104
105/*
106 * Using this file :
107 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Finn Thainaff0cf92016-01-03 16:06:09 +1100108 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * and macros and include this file in your driver.
110 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100111 * These macros control options :
112 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
Finn Thain594d4ba2016-01-03 16:06:10 +1100113 * defined.
Finn Thainaff0cf92016-01-03 16:06:09 +1100114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Finn Thain594d4ba2016-01-03 16:06:10 +1100116 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
118 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
Finn Thain594d4ba2016-01-03 16:06:10 +1100119 * transceivers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
Finn Thain594d4ba2016-01-03 16:06:10 +1100122 * override-configure an IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
125 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * NCR5380_read(register) - read from the specified register
129 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100130 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100132 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100133 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
135 * Either real DMA *or* pseudo DMA may be implemented
Finn Thainaff0cf92016-01-03 16:06:09 +1100136 * Note that the DMA setup functions should return the number of bytes
Finn Thain594d4ba2016-01-03 16:06:10 +1100137 * that they were able to program the controller for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * NCR5380_dma_write_setup(instance, src, count) - initialize
140 * NCR5380_dma_read_setup(instance, dst, count) - initialize
141 * NCR5380_dma_residual(instance); - residual count
142 *
143 * PSEUDO functions :
144 * NCR5380_pwrite(instance, src, count)
145 * NCR5380_pread(instance, dst, count);
146 *
147 * The generic driver is initialized by calling NCR5380_init(instance),
Finn Thainaff0cf92016-01-03 16:06:09 +1100148 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
150 * possible) function may be used.
151 */
152
Finn Thain54d8fe42016-01-03 16:05:06 +1100153static int do_abort(struct Scsi_Host *);
154static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Finn Thainc16df322016-01-03 16:06:08 +1100156/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100157 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100158 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100160 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
162
Finn Thain710ddd02014-11-12 16:12:02 +1100163static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Finn Thainaff0cf92016-01-03 16:06:09 +1100165 /*
166 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * various queues are valid.
168 */
169
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200170 if (scsi_bufflen(cmd)) {
171 cmd->SCp.buffer = scsi_sglist(cmd);
172 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200173 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 cmd->SCp.this_residual = cmd->SCp.buffer->length;
175 } else {
176 cmd->SCp.buffer = NULL;
177 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200178 cmd->SCp.ptr = NULL;
179 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100181
182 cmd->SCp.Status = 0;
183 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/**
Finn Thainb32ade12016-01-03 16:05:41 +1100187 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100188 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100189 * @reg1: 5380 register to poll
190 * @bit1: Bitmask to check
191 * @val1: Expected value
192 * @reg2: Second 5380 register to poll
193 * @bit2: Second bitmask to check
194 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100195 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
Finn Thain2f854b82016-01-03 16:05:22 +1100197 * Polls the chip in a reasonably efficient manner waiting for an
198 * event to occur. After a short quick poll we begin to yield the CPU
199 * (if possible). In irq contexts the time-out is arbitrarily limited.
200 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Finn Thainb32ade12016-01-03 16:05:41 +1100202 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Finn Thainb32ade12016-01-03 16:05:41 +1100205static int NCR5380_poll_politely2(struct Scsi_Host *instance,
206 int reg1, int bit1, int val1,
207 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100208{
209 struct NCR5380_hostdata *hostdata = shost_priv(instance);
210 unsigned long deadline = jiffies + wait;
211 unsigned long n;
212
213 /* Busy-wait for up to 10 ms */
214 n = min(10000U, jiffies_to_usecs(wait));
215 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100216 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100217 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100218 if ((NCR5380_read(reg1) & bit1) == val1)
219 return 0;
220 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100223 } while (n--);
224
225 if (irqs_disabled() || in_interrupt())
226 return -ETIMEDOUT;
227
228 /* Repeatedly sleep for 1 ms until deadline */
229 while (time_is_after_jiffies(deadline)) {
230 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100231 if ((NCR5380_read(reg1) & bit1) == val1)
232 return 0;
233 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Finn Thain2f854b82016-01-03 16:05:22 +1100236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -ETIMEDOUT;
238}
239
Finn Thainb32ade12016-01-03 16:05:41 +1100240static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
241 int reg, int bit, int val, int wait)
242{
243 return NCR5380_poll_politely2(instance, reg, bit, val,
244 reg, bit, val, wait);
245}
246
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100247#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static struct {
249 unsigned char mask;
250 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100251} signals[] = {
252 {SR_DBP, "PARITY"},
253 {SR_RST, "RST"},
254 {SR_BSY, "BSY"},
255 {SR_REQ, "REQ"},
256 {SR_MSG, "MSG"},
257 {SR_CD, "CD"},
258 {SR_IO, "IO"},
259 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100261},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262basrs[] = {
Finn Thainaff0cf92016-01-03 16:06:09 +1100263 {BASR_ATN, "ATN"},
264 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100266},
267icrs[] = {
268 {ICR_ASSERT_RST, "ASSERT RST"},
269 {ICR_ASSERT_ACK, "ASSERT ACK"},
270 {ICR_ASSERT_BSY, "ASSERT BSY"},
271 {ICR_ASSERT_SEL, "ASSERT SEL"},
272 {ICR_ASSERT_ATN, "ASSERT ATN"},
273 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100275},
276mrs[] = {
277 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
278 {MR_TARGET, "MODE TARGET"},
279 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
280 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
Finn Thain0d2cf862016-01-03 16:06:11 +1100281 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100282 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
283 {MR_DMA_MODE, "MODE DMA"},
284 {MR_ARBITRATE, "MODE ARBITRATION"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 {0, NULL}
286};
287
288/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100289 * NCR5380_print - print scsi bus signals
290 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100292 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294
295static void NCR5380_print(struct Scsi_Host *instance)
296{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
300 status = NCR5380_read(STATUS_REG);
301 mr = NCR5380_read(MODE_REG);
302 icr = NCR5380_read(INITIATOR_COMMAND_REG);
303 basr = NCR5380_read(BUS_AND_STATUS_REG);
304
305 printk("STATUS_REG: %02x ", status);
306 for (i = 0; signals[i].mask; ++i)
307 if (status & signals[i].mask)
308 printk(",%s", signals[i].name);
309 printk("\nBASR: %02x ", basr);
310 for (i = 0; basrs[i].mask; ++i)
311 if (basr & basrs[i].mask)
312 printk(",%s", basrs[i].name);
313 printk("\nICR: %02x ", icr);
314 for (i = 0; icrs[i].mask; ++i)
315 if (icr & icrs[i].mask)
316 printk(",%s", icrs[i].name);
317 printk("\nMODE: %02x ", mr);
318 for (i = 0; mrs[i].mask; ++i)
319 if (mr & mrs[i].mask)
320 printk(",%s", mrs[i].name);
321 printk("\n");
322}
323
Finn Thain0d2cf862016-01-03 16:06:11 +1100324static struct {
325 unsigned char value;
326 const char *name;
327} phases[] = {
328 {PHASE_DATAOUT, "DATAOUT"},
329 {PHASE_DATAIN, "DATAIN"},
330 {PHASE_CMDOUT, "CMDOUT"},
331 {PHASE_STATIN, "STATIN"},
332 {PHASE_MSGOUT, "MSGOUT"},
333 {PHASE_MSGIN, "MSGIN"},
334 {PHASE_UNKNOWN, "UNKNOWN"}
335};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Finn Thainc16df322016-01-03 16:06:08 +1100337/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100338 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100339 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100341 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
343
344static void NCR5380_print_phase(struct Scsi_Host *instance)
345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned char status;
347 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 status = NCR5380_read(STATUS_REG);
350 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100351 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100353 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
354 (phases[i].value != (status & PHASE_MASK)); ++i)
355 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100356 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358}
359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Finn Thaind5f7e652016-01-03 16:05:03 +1100362static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100365 * probe_intr - helper for IRQ autoprobe
366 * @irq: interrupt number
367 * @dev_id: unused
368 * @regs: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100370 * Set a flag to indicate the IRQ in question was received. This is
371 * used by the IRQ probe code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100373
David Howells7d12e782006-10-05 14:55:46 +0100374static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 probe_irq = irq;
377 return IRQ_HANDLED;
378}
379
380/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100381 * NCR5380_probe_irq - find the IRQ of an NCR5380
382 * @instance: NCR5380 controller
383 * @possible: bitmask of ISA IRQ lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100385 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
386 * and then looking to see what interrupt actually turned up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388
Andrew Morton702809c2007-05-23 14:41:56 -0700389static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
390 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Finn Thaine8a60142016-01-03 16:05:54 +1100392 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned long timeout;
394 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Finn Thain22f5f102014-11-12 16:11:56 +1100396 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100397 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 trying_irqs |= mask;
399
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500400 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100401 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * A interrupt is triggered whenever BSY = false, SEL = true
Finn Thainaff0cf92016-01-03 16:06:09 +1100405 * and a bit set in the SELECT_ENABLE_REG is asserted on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * SCSI bus.
407 *
408 * Note that the bus is only driven when the phase control signals
409 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
410 * to zero.
411 */
412
413 NCR5380_write(TARGET_COMMAND_REG, 0);
414 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
415 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
416 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
417
Finn Thain22f5f102014-11-12 16:11:56 +1100418 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800419 schedule_timeout_uninterruptible(1);
Finn Thainaff0cf92016-01-03 16:06:09 +1100420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 NCR5380_write(SELECT_ENABLE_REG, 0);
422 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
423
Finn Thain22f5f102014-11-12 16:11:56 +1100424 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (trying_irqs & mask)
426 free_irq(i, NULL);
427
428 return probe_irq;
429}
430
431/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100432 * NCR58380_info - report driver and host information
433 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100435 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437
Finn Thain8c325132014-11-12 16:11:58 +1100438static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Finn Thain8c325132014-11-12 16:11:58 +1100440 struct NCR5380_hostdata *hostdata = shost_priv(instance);
441
442 return hostdata->info;
443}
444
445static void prepare_info(struct Scsi_Host *instance)
446{
447 struct NCR5380_hostdata *hostdata = shost_priv(instance);
448
449 snprintf(hostdata->info, sizeof(hostdata->info),
450 "%s, io_port 0x%lx, n_io_port %d, "
451 "base 0x%lx, irq %d, "
452 "can_queue %d, cmd_per_lun %d, "
453 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100454 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100455 "options { %s} ",
456 instance->hostt->name, instance->io_port, instance->n_io_port,
457 instance->base, instance->irq,
458 instance->can_queue, instance->cmd_per_lun,
459 instance->sg_tablesize, instance->this_id,
Finn Thain55181be2016-01-03 16:05:42 +1100460 hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100461 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100462 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100464 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100467 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100470 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#endif
472#ifdef PSEUDO_DMA
Finn Thain8c325132014-11-12 16:11:58 +1100473 "PSEUDO_DMA "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#endif
Finn Thain8c325132014-11-12 16:11:58 +1100475 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Finn Thaina9c2dc42014-11-12 16:11:59 +1100478#ifdef PSEUDO_DMA
Al Virodd7ab712013-03-31 01:15:54 -0400479static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance,
480 char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Finn Thaina9c2dc42014-11-12 16:11:59 +1100482 struct NCR5380_hostdata *hostdata = shost_priv(instance);
483
484 hostdata->spin_max_r = 0;
485 hostdata->spin_max_w = 0;
486 return 0;
Al Virodd7ab712013-03-31 01:15:54 -0400487}
Al Virodd7ab712013-03-31 01:15:54 -0400488
489static int __maybe_unused NCR5380_show_info(struct seq_file *m,
Finn Thain0d2cf862016-01-03 16:06:11 +1100490 struct Scsi_Host *instance)
Al Virodd7ab712013-03-31 01:15:54 -0400491{
Finn Thaine8a60142016-01-03 16:05:54 +1100492 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Rasmus Villemoes0c3de382014-12-03 00:10:49 +0100494 seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n",
Finn Thaina9c2dc42014-11-12 16:11:59 +1100495 hostdata->spin_max_w, hostdata->spin_max_r);
Al Virodd7ab712013-03-31 01:15:54 -0400496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
Finn Thaine5c3fdd2016-01-03 16:05:52 +1100498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100501 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100502 * @instance: adapter to configure
503 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100505 * Initializes *instance and corresponding 5380 chip,
506 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100508 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100509 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100511 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800514static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Finn Thaine8a60142016-01-03 16:05:54 +1100516 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100517 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100518 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Finn Thain0d2cf862016-01-03 16:06:11 +1100520 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100522 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
524 if (i > hostdata->id_mask)
525 hostdata->id_higher_mask |= i;
526 for (i = 0; i < 8; ++i)
527 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100528 hostdata->dma_len = 0;
529
Finn Thain11d2f632016-01-03 16:05:51 +1100530 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100532 hostdata->sensing = NULL;
533 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100534 INIT_LIST_HEAD(&hostdata->unissued);
535 INIT_LIST_HEAD(&hostdata->disconnected);
536
Finn Thain55181be2016-01-03 16:05:42 +1100537 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100538
Finn Thain8d8601a2016-01-03 16:05:37 +1100539 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100540 hostdata->work_q = alloc_workqueue("ncr5380_%d",
541 WQ_UNBOUND | WQ_MEM_RECLAIM,
542 1, instance->host_no);
543 if (!hostdata->work_q)
544 return -ENOMEM;
545
Finn Thain8c325132014-11-12 16:11:58 +1100546 prepare_info(instance);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
549 NCR5380_write(MODE_REG, MR_BASE);
550 NCR5380_write(TARGET_COMMAND_REG, 0);
551 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100552
553 /* Calibrate register polling loop */
554 i = 0;
555 deadline = jiffies + 1;
556 do {
557 cpu_relax();
558 } while (time_is_after_jiffies(deadline));
559 deadline += msecs_to_jiffies(256);
560 do {
561 NCR5380_read(STATUS_REG);
562 ++i;
563 cpu_relax();
564 } while (time_is_after_jiffies(deadline));
565 hostdata->accesses_per_ms = i / 256;
566
Finn Thainb6488f92016-01-03 16:05:08 +1100567 return 0;
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Finn Thainb6488f92016-01-03 16:05:08 +1100570/**
571 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
572 * @instance: adapter to check
573 *
574 * If the system crashed, it may have crashed with a connected target and
575 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
576 * currently established nexus, which we know nothing about. Failing that
577 * do a bus reset.
578 *
579 * Note that a bus reset will cause the chip to assert IRQ.
580 *
581 * Returns 0 if successful, otherwise -ENXIO.
582 */
583
584static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
585{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100586 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100587 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
590 switch (pass) {
591 case 1:
592 case 3:
593 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100594 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
595 NCR5380_poll_politely(instance,
596 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100599 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 do_abort(instance);
601 break;
602 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100603 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100605 /* Wait after a reset; the SCSI standard calls for
606 * 250ms, we wait 500ms to be on the safe side.
607 * But some Toshiba CD-ROMs need ten times that.
608 */
609 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
610 msleep(2500);
611 else
612 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100615 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -ENXIO;
617 }
618 }
619 return 0;
620}
621
622/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100623 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100624 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100625 *
626 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800629static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Finn Thaine8a60142016-01-03 16:05:54 +1100631 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Finn Thain8d8601a2016-01-03 16:05:37 +1100633 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100634 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/**
Finn Thain677e0192016-01-03 16:05:59 +1100638 * complete_cmd - finish processing a command and return it to the SCSI ML
639 * @instance: the host instance
640 * @cmd: command to complete
641 */
642
643static void complete_cmd(struct Scsi_Host *instance,
644 struct scsi_cmnd *cmd)
645{
646 struct NCR5380_hostdata *hostdata = shost_priv(instance);
647
648 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
649
Finn Thainf27db8e2016-01-03 16:06:00 +1100650 if (hostdata->sensing == cmd) {
651 /* Autosense processing ends here */
652 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
653 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
654 set_host_byte(cmd, DID_ERROR);
655 } else
656 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
657 hostdata->sensing = NULL;
658 }
659
Finn Thain677e0192016-01-03 16:05:59 +1100660 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
661
662 cmd->scsi_done(cmd);
663}
664
665/**
Finn Thain1bb40582016-01-03 16:05:29 +1100666 * NCR5380_queue_command - queue a command
667 * @instance: the relevant SCSI adapter
668 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
Finn Thain1bb40582016-01-03 16:05:29 +1100670 * cmd is added to the per-instance issue queue, with minor
671 * twiddling done to the host specific fields of cmd. If the
672 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
674
Finn Thain1bb40582016-01-03 16:05:29 +1100675static int NCR5380_queue_command(struct Scsi_Host *instance,
676 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Finn Thain1bb40582016-01-03 16:05:29 +1100678 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100679 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100680 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682#if (NDEBUG & NDEBUG_NO_WRITE)
683 switch (cmd->cmnd[0]) {
684 case WRITE_6:
685 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100686 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100688 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100691#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 cmd->result = 0;
694
Finn Thain11d2f632016-01-03 16:05:51 +1100695 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100696
Finn Thainaff0cf92016-01-03 16:06:09 +1100697 /*
698 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100700 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * sense data is only guaranteed to be valid while the condition exists.
702 */
703
Finn Thain32b26a12016-01-03 16:05:58 +1100704 if (cmd->cmnd[0] == REQUEST_SENSE)
705 list_add(&ncmd->list, &hostdata->unissued);
706 else
707 list_add_tail(&ncmd->list, &hostdata->unissued);
708
Finn Thain11d2f632016-01-03 16:05:51 +1100709 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100710
Finn Thaindbb6b352016-01-03 16:05:53 +1100711 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
712 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100715 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100720 * dequeue_next_cmd - dequeue a command for processing
721 * @instance: the scsi host instance
722 *
723 * Priority is given to commands on the autosense queue. These commands
724 * need autosense because of a CHECK CONDITION result.
725 *
726 * Returns a command pointer if a command is found for a target that is
727 * not already busy. Otherwise returns NULL.
728 */
729
730static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
731{
732 struct NCR5380_hostdata *hostdata = shost_priv(instance);
733 struct NCR5380_cmd *ncmd;
734 struct scsi_cmnd *cmd;
735
Finn Thain8d5dbec2016-02-23 10:07:09 +1100736 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100737 list_for_each_entry(ncmd, &hostdata->unissued, list) {
738 cmd = NCR5380_to_scmd(ncmd);
739 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
740 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
741
742 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
743 list_del(&ncmd->list);
744 dsprintk(NDEBUG_QUEUES, instance,
745 "dequeue: removed %p from issue queue\n", cmd);
746 return cmd;
747 }
748 }
749 } else {
750 /* Autosense processing begins here */
751 ncmd = list_first_entry(&hostdata->autosense,
752 struct NCR5380_cmd, list);
753 list_del(&ncmd->list);
754 cmd = NCR5380_to_scmd(ncmd);
755 dsprintk(NDEBUG_QUEUES, instance,
756 "dequeue: removed %p from autosense queue\n", cmd);
757 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
758 hostdata->sensing = cmd;
759 return cmd;
760 }
761 return NULL;
762}
763
764static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
765{
766 struct NCR5380_hostdata *hostdata = shost_priv(instance);
767 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
768
Finn Thain8d5dbec2016-02-23 10:07:09 +1100769 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100770 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
771 list_add(&ncmd->list, &hostdata->autosense);
772 hostdata->sensing = NULL;
773 } else
774 list_add(&ncmd->list, &hostdata->unissued);
775}
776
777/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100778 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100780 * NCR5380_main is a coroutine that runs as long as more work can
781 * be done on the NCR5380 host adapters in a system. Both
782 * NCR5380_queue_command() and NCR5380_intr() will try to start it
783 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785
David Howellsc4028952006-11-22 14:57:56 +0000786static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
David Howellsc4028952006-11-22 14:57:56 +0000788 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100789 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100795
Finn Thain0a4e3612016-01-03 16:06:07 +1100796 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100797 while (!hostdata->connected && !hostdata->selecting) {
798 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
799
800 if (!cmd)
801 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100802
803 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100806 * Attempt to establish an I_T_L nexus here.
807 * On success, instance->hostdata->connected is set.
808 * On failure, we must add the command back to the
809 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100811 /*
812 * REQUEST SENSE commands are issued without tagged
813 * queueing, even on SCSI-II devices because the
814 * contingent allegiance condition exists for the
815 * entire unit.
816 */
Finn Thain32b26a12016-01-03 16:05:58 +1100817
Finn Thainccf6efd2016-02-23 10:07:08 +1100818 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100819 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +1100820 } else {
821 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
822 "main: select failed, returning %p to queue\n", cmd);
823 requeue_cmd(instance, cmd);
824 }
825 }
Finn Thaine4dec682016-03-23 21:10:12 +1100826 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100827 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100830 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100831 spin_unlock_irq(&hostdata->lock);
832 if (!done)
833 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837#ifndef DONT_USE_INTR
838
839/**
Finn Thaincd400822016-01-03 16:05:40 +1100840 * NCR5380_intr - generic NCR5380 irq handler
841 * @irq: interrupt number
842 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 *
Finn Thaincd400822016-01-03 16:05:40 +1100844 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
845 * from the disconnected queue, and restarting NCR5380_main()
846 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 *
Finn Thaincd400822016-01-03 16:05:40 +1100848 * The chip can assert IRQ in any of six different conditions. The IRQ flag
849 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
850 * Three of these six conditions are latched in the Bus and Status Register:
851 * - End of DMA (cleared by ending DMA Mode)
852 * - Parity error (cleared by reading RPIR)
853 * - Loss of BSY (cleared by reading RPIR)
854 * Two conditions have flag bits that are not latched:
855 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
856 * - Bus reset (non-maskable)
857 * The remaining condition has no flag bit at all:
858 * - Selection/reselection
859 *
860 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
861 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
862 * claimed that "the design of the [DP8490] interrupt logic ensures
863 * interrupts will not be lost (they can be on the DP5380)."
864 * The L5380/53C80 datasheet from LOGIC Devices has more details.
865 *
866 * Checking for bus reset by reading RST is futile because of interrupt
867 * latency, but a bus reset will reset chip logic. Checking for parity error
868 * is unnecessary because that interrupt is never enabled. A Loss of BSY
869 * condition will clear DMA Mode. We can tell when this occurs because the
870 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872
Finn Thaincd400822016-01-03 16:05:40 +1100873static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800875 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100876 struct NCR5380_hostdata *hostdata = shost_priv(instance);
877 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned char basr;
879 unsigned long flags;
880
Finn Thain11d2f632016-01-03 16:05:51 +1100881 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Finn Thaincd400822016-01-03 16:05:40 +1100883 basr = NCR5380_read(BUS_AND_STATUS_REG);
884 if (basr & BASR_IRQ) {
885 unsigned char mr = NCR5380_read(MODE_REG);
886 unsigned char sr = NCR5380_read(STATUS_REG);
887
Finn Thainb7465452016-01-03 16:06:05 +1100888 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
889 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100890
Finn Thaincd400822016-01-03 16:05:40 +1100891 if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
892 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
893 /* Probably reselected */
894 NCR5380_write(SELECT_ENABLE_REG, 0);
895 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
896
Finn Thainb7465452016-01-03 16:06:05 +1100897 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100898
899 if (!hostdata->connected) {
900 NCR5380_reselect(instance);
901 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Finn Thaincd400822016-01-03 16:05:40 +1100903 if (!hostdata->connected)
904 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
905 } else {
906 /* Probably Bus Reset */
907 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
908
Finn Thainb7465452016-01-03 16:06:05 +1100909 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +1100910 }
911 handled = 1;
912 } else {
913 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
914 }
915
Finn Thain11d2f632016-01-03 16:05:51 +1100916 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100917
918 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Finn Thainaff0cf92016-01-03 16:06:09 +1100921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Finn Thainaff0cf92016-01-03 16:06:09 +1100923/*
Finn Thain710ddd02014-11-12 16:12:02 +1100924 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +1100925 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +1100928 * including ARBITRATION, SELECTION, and initial message out for
929 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100931 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +1100932 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +1100933 *
Finn Thain707d62b2016-01-03 16:06:02 +1100934 * Returns cmd if selection failed but should be retried,
935 * NULL if selection failed and should not be retried, or
936 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100938 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100939 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
940 * with registers as they should have been on entry - ie
941 * SELECT_ENABLE will be set appropriately, the NCR5380
942 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100944 * If successful : I_T_L or I_T_L_Q nexus will be established,
945 * instance->connected will be set to cmd.
946 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100948 * If failed (no target) : cmd->scsi_done() will be called, and the
949 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100951
Finn Thain707d62b2016-01-03 16:06:02 +1100952static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
953 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Finn Thaine8a60142016-01-03 16:05:54 +1100955 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned char tmp[3], phase;
957 unsigned char *data;
958 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100962 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
963 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Finn Thain707d62b2016-01-03 16:06:02 +1100965 /*
966 * Arbitration and selection phases are slow and involve dropping the
967 * lock, so we have to watch out for EH. An exception handler may
968 * change 'selecting' to NULL. This function will then return NULL
969 * so that the caller will forget about 'cmd'. (During information
970 * transfer phases, EH may change 'connected' to NULL.)
971 */
972 hostdata->selecting = cmd;
973
Finn Thainaff0cf92016-01-03 16:06:09 +1100974 /*
975 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * data bus during SELECTION.
977 */
978
979 NCR5380_write(TARGET_COMMAND_REG, 0);
980
Finn Thainaff0cf92016-01-03 16:06:09 +1100981 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Start arbitration.
983 */
984
985 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
986 NCR5380_write(MODE_REG, MR_ARBITRATE);
987
Finn Thain55500d92016-01-03 16:05:35 +1100988 /* The chip now waits for BUS FREE phase. Then after the 800 ns
989 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
991
Finn Thain11d2f632016-01-03 16:05:51 +1100992 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100993 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
994 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
995 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100996 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100997 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
998 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100999 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001000 }
Finn Thainccf6efd2016-02-23 10:07:08 +11001001 if (!hostdata->selecting) {
1002 /* Command was aborted */
1003 NCR5380_write(MODE_REG, MR_BASE);
1004 goto out;
1005 }
Finn Thainb32ade12016-01-03 16:05:41 +11001006 if (err < 0) {
1007 NCR5380_write(MODE_REG, MR_BASE);
1008 shost_printk(KERN_ERR, instance,
1009 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001010 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001011 }
Finn Thain11d2f632016-01-03 16:05:51 +11001012 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001013
1014 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 udelay(3);
1016
1017 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001018 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1019 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1020 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001022 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001023 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001024 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Finn Thaincf13b082016-01-03 16:05:18 +11001026
1027 /* After/during arbitration, BSY should be asserted.
1028 * IBM DPES-31080 Version S31Q works now
1029 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1030 */
1031 NCR5380_write(INITIATOR_COMMAND_REG,
1032 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Finn Thainaff0cf92016-01-03 16:06:09 +11001034 /*
1035 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * a minimum so we'll udelay ceil(1.2)
1037 */
1038
Finn Thain9c3f0e22016-01-03 16:05:11 +11001039 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1040 udelay(15);
1041 else
1042 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Finn Thain11d2f632016-01-03 16:05:51 +11001044 spin_lock_irq(&hostdata->lock);
1045
Finn Thain72064a72016-01-03 16:05:44 +11001046 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1047 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001048 goto out;
1049
1050 if (!hostdata->selecting) {
1051 NCR5380_write(MODE_REG, MR_BASE);
1052 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1053 goto out;
1054 }
Finn Thain72064a72016-01-03 16:05:44 +11001055
Finn Thainb7465452016-01-03 16:06:05 +11001056 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Finn Thainaff0cf92016-01-03 16:06:09 +11001058 /*
1059 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 * the host and target ID's on the SCSI bus.
1061 */
1062
Finn Thain3d07d222016-01-03 16:06:13 +11001063 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Finn Thainaff0cf92016-01-03 16:06:09 +11001065 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * Raise ATN while SEL is true before BSY goes false from arbitration,
1067 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1068 * phase immediately after selection.
1069 */
1070
Finn Thain3d07d222016-01-03 16:06:13 +11001071 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1072 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 NCR5380_write(MODE_REG, MR_BASE);
1074
Finn Thainaff0cf92016-01-03 16:06:09 +11001075 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * Reselect interrupts must be turned off prior to the dropping of BSY,
1077 * otherwise we will trigger an interrupt.
1078 */
1079 NCR5380_write(SELECT_ENABLE_REG, 0);
1080
Finn Thain11d2f632016-01-03 16:05:51 +11001081 spin_unlock_irq(&hostdata->lock);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001084 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * the BSY signal.
1086 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001087 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001090 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1091 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Finn Thainaff0cf92016-01-03 16:06:09 +11001093 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001095 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * appropriate propagation delay has expired, and we're confusing
1097 * a BSY signal from ourselves as the target's response to SELECTION.
1098 *
1099 * A small delay (the 'C++' frontend breaks the pipeline with an
1100 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001101 * tighter 'C' code breaks and requires this) solves the problem -
1102 * the 1 us delay is arbitrary, and only used because this delay will
1103 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * work there.
1105 *
1106 * wingel suggests that this could be due to failing to wait
1107 * one deskew delay.
1108 */
1109
1110 udelay(1);
1111
Finn Thainb7465452016-01-03 16:06:05 +11001112 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Finn Thainaff0cf92016-01-03 16:06:09 +11001114 /*
1115 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * selection.
1117 */
1118
Finn Thainae753a32016-01-03 16:05:23 +11001119 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1120 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001123 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1125 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001126 if (!hostdata->connected)
1127 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001128 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Finn Thainae753a32016-01-03 16:05:23 +11001131
1132 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001133 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001134 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001135 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001136 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1137 if (hostdata->selecting) {
1138 cmd->result = DID_BAD_TARGET << 16;
1139 complete_cmd(instance, cmd);
1140 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1141 cmd = NULL;
1142 }
1143 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001144 }
1145
Finn Thainaff0cf92016-01-03 16:06:09 +11001146 /*
1147 * No less than two deskew delays after the initiator detects the
1148 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 * change the DATA BUS. -wingel
1150 */
1151
1152 udelay(1);
1153
1154 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001157 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 * was true but before BSY was false during selection, the information
1159 * transfer phase should be a MESSAGE OUT phase so that we can send the
1160 * IDENTIFY message.
Finn Thainaff0cf92016-01-03 16:06:09 +11001161 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1163 * message (2 bytes) with a tag ID that we increment with every command
1164 * until it wraps back to 0.
1165 *
1166 * XXX - it turns out that there are some broken SCSI-II devices,
Finn Thain594d4ba2016-01-03 16:06:10 +11001167 * which claim to support tagged queuing but fail when more than
1168 * some number of commands are issued at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170
1171 /* Wait for start of REQ/ACK handshake */
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001174 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001175 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001176 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1177 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001179 goto out;
1180 }
1181 if (!hostdata->selecting) {
1182 do_abort(instance);
1183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
Finn Thainb7465452016-01-03 16:06:05 +11001186 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1187 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001188 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 len = 1;
1191 cmd->tag = 0;
1192
1193 /* Send message(s) */
1194 data = tmp;
1195 phase = PHASE_MSGOUT;
1196 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001197 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001201 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Boaz Harrosh28424d32007-09-10 22:37:45 +03001203 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Finn Thain707d62b2016-01-03 16:06:02 +11001205 cmd = NULL;
1206
1207out:
1208 if (!hostdata->selecting)
1209 return NULL;
1210 hostdata->selecting = NULL;
1211 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Finn Thainaff0cf92016-01-03 16:06:09 +11001214/*
1215 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001216 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 *
1218 * Purpose : transfers data in given phase using polled I/O
1219 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001220 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001221 * what phase is expected, *count - pointer to number of
1222 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001223 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001225 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001226 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001228 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 *
1230 * XXX Note : handling for bus free may be useful.
1231 */
1232
1233/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001234 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * IS 100% reliable, and for the actual data transfer where speed
1236 * counts, we will always do a pseudo DMA or DMA transfer.
1237 */
1238
Finn Thain0d2cf862016-01-03 16:06:11 +11001239static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1240 unsigned char *phase, int *count,
1241 unsigned char **data)
1242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 unsigned char p = *phase, tmp;
1244 int c = *count;
1245 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Finn Thainaff0cf92016-01-03 16:06:09 +11001247 /*
1248 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * phase specified in the appropriate bits of the TARGET COMMAND
1250 * REGISTER match the STATUS REGISTER
1251 */
1252
Finn Thain0d2cf862016-01-03 16:06:11 +11001253 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001256 /*
1257 * Wait for assertion of REQ, after which the phase bits will be
1258 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260
Finn Thain686f3992016-01-03 16:05:26 +11001261 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Finn Thainb7465452016-01-03 16:06:05 +11001264 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001267 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001268 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1269 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
1271 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* Do actual transfer from SCSI bus to / from memory */
1274 if (!(p & SR_IO))
1275 NCR5380_write(OUTPUT_DATA_REG, *d);
1276 else
1277 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1278
1279 ++d;
1280
Finn Thainaff0cf92016-01-03 16:06:09 +11001281 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * The SCSI standard suggests that in MSGOUT phase, the initiator
1283 * should drop ATN on the last byte of the message phase
1284 * after REQ has been asserted for the handshake but before
1285 * the initiator raises ACK.
1286 */
1287
1288 if (!(p & SR_IO)) {
1289 if (!((p & SR_MSG) && c > 1)) {
1290 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1291 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001292 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1293 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001295 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1296 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001298 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1299 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 } else {
1302 NCR5380_dprint(NDEBUG_PIO, instance);
1303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1304 }
1305
Finn Thaina2edc4a2016-01-03 16:05:27 +11001306 if (NCR5380_poll_politely(instance,
1307 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1308 break;
1309
Finn Thainb7465452016-01-03 16:06:05 +11001310 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001313 * We have several special cases to consider during REQ/ACK handshaking :
1314 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001315 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001317 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001318 * message. We must exit with ACK asserted, so that the calling
1319 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 *
1321 * 3. ACK and ATN are clear and the target may proceed as normal.
1322 */
1323 if (!(p == PHASE_MSGIN && c == 1)) {
1324 if (p == PHASE_MSGOUT && c > 1)
1325 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1326 else
1327 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1328 }
1329 } while (--c);
1330
Finn Thainb7465452016-01-03 16:06:05 +11001331 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 *count = c;
1334 *data = d;
1335 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001336 /* The phase read from the bus is valid if either REQ is (already)
1337 * asserted or if ACK hasn't been released yet. The latter applies if
1338 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1339 */
1340 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 *phase = tmp & PHASE_MASK;
1342 else
1343 *phase = PHASE_UNKNOWN;
1344
1345 if (!c || (*phase == p))
1346 return 0;
1347 else
1348 return -1;
1349}
1350
1351/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001352 * do_reset - issue a reset command
1353 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001355 * Issue a reset sequence to the NCR5380 and try and get the bus
1356 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001358 * This clears the reset interrupt flag because there may be no handler for
1359 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1360 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001362
Finn Thain54d8fe42016-01-03 16:05:06 +11001363static void do_reset(struct Scsi_Host *instance)
1364{
Finn Thain636b1ec2016-01-03 16:05:10 +11001365 unsigned long flags;
1366
1367 local_irq_save(flags);
1368 NCR5380_write(TARGET_COMMAND_REG,
1369 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001371 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001373 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1374 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Finn Thain80d3eb62016-01-03 16:05:34 +11001377/**
1378 * do_abort - abort the currently established nexus by going to
1379 * MESSAGE OUT phase and sending an ABORT message.
1380 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001382 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384
Finn Thain54d8fe42016-01-03 16:05:06 +11001385static int do_abort(struct Scsi_Host *instance)
1386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 unsigned char *msgptr, phase, tmp;
1388 int len;
1389 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* Request message out phase */
1392 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1393
Finn Thainaff0cf92016-01-03 16:06:09 +11001394 /*
1395 * Wait for the target to indicate a valid phase by asserting
1396 * REQ. Once this happens, we'll have either a MSGOUT phase
1397 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001399 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * We really don't care what value was on the bus or what value
1401 * the target sees, so we just handshake.
1402 */
1403
Finn Thain80d3eb62016-01-03 16:05:34 +11001404 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001405 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001406 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Finn Thainf35d3472016-01-03 16:05:33 +11001408 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1411
Finn Thainf35d3472016-01-03 16:05:33 +11001412 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001413 NCR5380_write(INITIATOR_COMMAND_REG,
1414 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001415 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001416 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001417 goto timeout;
1418 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 tmp = ABORT;
1422 msgptr = &tmp;
1423 len = 1;
1424 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001425 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /*
1428 * If we got here, and the command completed successfully,
1429 * we're about to go into bus free state.
1430 */
1431
1432 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001433
1434timeout:
1435 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1436 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Finn Thaine4dec682016-03-23 21:10:12 +11001439#if defined(PSEUDO_DMA)
Finn Thainaff0cf92016-01-03 16:06:09 +11001440/*
1441 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001442 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 *
1444 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001445 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001447 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001448 * what phase is expected, *count - pointer to number of
1449 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001450 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001452 * maximum number of bytes, 0 if all bytes or transferred or exit
1453 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001455 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
1457
1458
Finn Thain0d2cf862016-01-03 16:06:11 +11001459static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1460 unsigned char *phase, int *count,
1461 unsigned char **data)
1462{
1463 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 register int c = *count;
1465 register unsigned char p = *phase;
1466 register unsigned char *d = *data;
1467 unsigned char tmp;
1468 int foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1471 *phase = tmp;
1472 return -1;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /*
1478 * Note : on my sample board, watch-dog timeouts occurred when interrupts
Finn Thainaff0cf92016-01-03 16:06:09 +11001479 * were not disabled for the duration of a single DMA transfer, from
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * before the setting of DMA mode to after transfer of the last byte.
1481 */
1482
Finn Thain55181be2016-01-03 16:05:42 +11001483 if (hostdata->flags & FLAG_NO_DMA_FIXUP)
Finn Thaincd400822016-01-03 16:05:40 +11001484 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1485 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 else
Finn Thaincd400822016-01-03 16:05:40 +11001487 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Finn Thain52a6a1c2014-03-18 11:42:18 +11001489 dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Finn Thainaff0cf92016-01-03 16:06:09 +11001491 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001492 * On the PAS16 at least I/O recovery delays are not needed here.
1493 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
1495
1496 if (p & SR_IO) {
1497 io_recovery_delay(1);
1498 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1499 } else {
1500 io_recovery_delay(1);
1501 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1502 io_recovery_delay(1);
1503 NCR5380_write(START_DMA_SEND_REG, 0);
1504 io_recovery_delay(1);
1505 }
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
Finn Thaine4dec682016-03-23 21:10:12 +11001508 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001509 *
1510 * For DMA sends, we want to wait until the last byte has been
1511 * transferred out over the bus before we turn off DMA mode. Alas, there
1512 * seems to be no terribly good way of doing this on a 5380 under all
1513 * conditions. For non-scatter-gather operations, we can wait until REQ
1514 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1515 * are nastier, since the device will be expecting more data than we
1516 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1517 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1518 * why this signal was added to the newer chips) but on the older 538[01]
1519 * this signal does not exist. The workaround for this lack is a watchdog;
1520 * we bail out of the wait-loop after a modest amount of wait-time if
1521 * the usual exit conditions are not met. Not a terribly clean or
1522 * correct solution :-%
1523 *
1524 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1525 * If the chip is in DMA receive mode, it will respond to a target's
1526 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1527 * ACK, even if it has _already_ been notified by the DMA controller that
1528 * the current DMA transfer has completed! If the NCR5380 is then taken
1529 * out of DMA mode, this already-acknowledged byte is lost. This is
1530 * not a problem for "one DMA transfer per READ command", because
1531 * the situation will never arise... either all of the data is DMA'ed
1532 * properly, or the target switches to MESSAGE IN phase to signal a
1533 * disconnection (either operation bringing the DMA to a clean halt).
1534 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001535 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001536 * condition before taking the NCR5380 out of DMA mode. One or two extra
1537 * bytes are transferred via PIO as necessary to fill out the original
1538 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
1540
1541 if (p & SR_IO) {
Finn Thain55181be2016-01-03 16:05:42 +11001542 foo = NCR5380_pread(instance, d,
1543 hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1);
1544 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001546 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001547 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 * the chip to latch the last byte, read it, and then disable
1549 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001550 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1552 * REQ is deasserted when ACK is asserted, and not reasserted
1553 * until ACK goes false. Since the NCR5380 won't lower ACK
1554 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001555 * the DMA port or we take the NCR5380 out of DMA mode, we
1556 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * byte.
1558 */
1559
Finn Thain55181be2016-01-03 16:05:42 +11001560 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1561 BASR_DRQ, BASR_DRQ, HZ) < 0) {
1562 foo = -1;
1563 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Finn Thain55181be2016-01-03 16:05:42 +11001565 if (NCR5380_poll_politely(instance, STATUS_REG,
1566 SR_REQ, 0, HZ) < 0) {
1567 foo = -1;
1568 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1569 }
1570 d[c - 1] = NCR5380_read(INPUT_DATA_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 foo = NCR5380_pwrite(instance, d, c);
Finn Thain55181be2016-01-03 16:05:42 +11001574 if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001576 * Wait for the last byte to be sent. If REQ is being asserted for
1577 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
Finn Thain55181be2016-01-03 16:05:42 +11001579 if (NCR5380_poll_politely2(instance,
1580 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1581 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
1582 foo = -1;
1583 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 NCR5380_write(MODE_REG, MR_BASE);
1588 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaincd400822016-01-03 16:05:40 +11001589 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 *data = d + c;
1591 *count = 0;
1592 *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
Finn Thaine4dec682016-03-23 21:10:12 +11001595#endif /* PSEUDO_DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*
1598 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1599 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001600 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001601 * directs us to. Operates on the currently connected command,
1602 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *
1604 * Inputs : instance, instance for which we are doing commands
1605 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001606 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001607 * modified if a command disconnects, *instance->connected will
1608 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001610 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001611 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 */
1613
Finn Thain0d2cf862016-01-03 16:06:11 +11001614static void NCR5380_information_transfer(struct Scsi_Host *instance)
1615{
Finn Thaine8a60142016-01-03 16:05:54 +11001616 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 unsigned char msgout = NOP;
1618 int sink = 0;
1619 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 unsigned char *data;
1622 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001623 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Finn Thain11d2f632016-01-03 16:05:51 +11001625 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001626 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 tmp = NCR5380_read(STATUS_REG);
1629 /* We only have a valid SCSI phase when REQ is asserted */
1630 if (tmp & SR_REQ) {
1631 phase = (tmp & PHASE_MASK);
1632 if (phase != old_phase) {
1633 old_phase = phase;
1634 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1635 }
1636 if (sink && (phase != PHASE_MSGOUT)) {
1637 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1638
Finn Thain3d07d222016-01-03 16:06:13 +11001639 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1640 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001641 while (NCR5380_read(STATUS_REG) & SR_REQ)
1642 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001643 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1644 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 sink = 0;
1646 continue;
1647 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 case PHASE_DATAOUT:
1651#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001652 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 sink = 1;
1654 do_abort(instance);
1655 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001656 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001657 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return;
1659#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001660 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001661 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 * If there is no room left in the current buffer in the
1663 * scatter-gather list, move onto the next one.
1664 */
1665
1666 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1667 ++cmd->SCp.buffer;
1668 --cmd->SCp.buffers_residual;
1669 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001670 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001671 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1672 cmd->SCp.this_residual,
1673 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001677 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * PSEUDO-DMA for systems that are strictly PIO,
1679 * since we can let the hardware do the handshaking.
1680 *
1681 * For this to work, we need to know the transfersize
1682 * ahead of time, since the pseudo-DMA code will sit
1683 * in an unconditional loop.
1684 */
1685
Finn Thaine4dec682016-03-23 21:10:12 +11001686#if defined(PSEUDO_DMA)
Finn Thainff3d4572016-01-03 16:05:25 +11001687 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001688 if (!cmd->device->borken)
Finn Thainff3d4572016-01-03 16:05:25 +11001689 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Finn Thainff3d4572016-01-03 16:05:25 +11001691 if (transfersize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001693 if (NCR5380_transfer_dma(instance, &phase,
1694 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001696 * If the watchdog timer fires, all future
1697 * accesses to this device will use the
1698 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001700 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001701 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 sink = 1;
1704 do_abort(instance);
1705 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* XXX - need to source or sink data here, as appropriate */
1707 } else
1708 cmd->SCp.this_residual -= transfersize - len;
1709 } else
Finn Thaine4dec682016-03-23 21:10:12 +11001710#endif /* PSEUDO_DMA */
Finn Thain11d2f632016-01-03 16:05:51 +11001711 {
Finn Thain16788472016-02-23 10:07:05 +11001712 /* Break up transfer into 3 ms chunks,
1713 * presuming 6 accesses per handshake.
1714 */
1715 transfersize = min((unsigned long)cmd->SCp.this_residual,
1716 hostdata->accesses_per_ms / 2);
1717 len = transfersize;
1718 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001719 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001720 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001721 }
Finn Thain16788472016-02-23 10:07:05 +11001722 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 case PHASE_MSGIN:
1724 len = 1;
1725 data = &tmp;
1726 NCR5380_transfer_pio(instance, &phase, &len, &data);
1727 cmd->SCp.Message = tmp;
1728
1729 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 case ABORT:
1731 case COMMAND_COMPLETE:
1732 /* Accept message by clearing ACK */
1733 sink = 1;
1734 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001735 dsprintk(NDEBUG_QUEUES, instance,
1736 "COMMAND COMPLETE %p target %d lun %llu\n",
1737 cmd, scmd_id(cmd), cmd->device->lun);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Finn Thainf27db8e2016-01-03 16:06:00 +11001741 cmd->result &= ~0xffff;
1742 cmd->result |= cmd->SCp.Status;
1743 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Finn Thainf27db8e2016-01-03 16:06:00 +11001745 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001746 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001747 else {
1748 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1749 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1750 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1751 cmd);
1752 list_add_tail(&ncmd->list,
1753 &hostdata->autosense);
1754 } else
1755 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
Finn Thainaff0cf92016-01-03 16:06:09 +11001758 /*
1759 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * arbitration can resume.
1761 */
1762 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001763
1764 /* Enable reselect interrupts */
1765 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return;
1767 case MESSAGE_REJECT:
1768 /* Accept message by clearing ACK */
1769 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1770 switch (hostdata->last_message) {
1771 case HEAD_OF_QUEUE_TAG:
1772 case ORDERED_QUEUE_TAG:
1773 case SIMPLE_QUEUE_TAG:
1774 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001775 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777 default:
1778 break;
1779 }
Finn Thain340b9612016-01-03 16:05:31 +11001780 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001781 case DISCONNECT:
1782 /* Accept message by clearing ACK */
1783 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1784 hostdata->connected = NULL;
1785 list_add(&ncmd->list, &hostdata->disconnected);
1786 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1787 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1788 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001789
Finn Thain0d2cf862016-01-03 16:06:11 +11001790 /*
1791 * Restore phase bits to 0 so an interrupted selection,
1792 * arbitration can resume.
1793 */
1794 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Finn Thain0d2cf862016-01-03 16:06:11 +11001796 /* Enable reselect interrupts */
1797 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1798 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001801 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 * ignore SAVE/RESTORE pointers calls.
1803 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001804 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001806 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 * compatible.
1808 */
1809 case SAVE_POINTERS:
1810 case RESTORE_POINTERS:
1811 /* Accept message by clearing ACK */
1812 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1813 break;
1814 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001815 /*
1816 * Start the message buffer with the EXTENDED_MESSAGE
1817 * byte, since spi_print_msg() wants the whole thing.
1818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 extended_msg[0] = EXTENDED_MESSAGE;
1820 /* Accept first byte by clearing ACK */
1821 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001822
1823 spin_unlock_irq(&hostdata->lock);
1824
Finn Thainb7465452016-01-03 16:06:05 +11001825 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 len = 2;
1828 data = extended_msg + 1;
1829 phase = PHASE_MSGIN;
1830 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001831 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1832 (int)extended_msg[1],
1833 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Finn Thaine0783ed2016-01-03 16:05:45 +11001835 if (!len && extended_msg[1] > 0 &&
1836 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* Accept third byte by clearing ACK */
1838 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1839 len = extended_msg[1] - 1;
1840 data = extended_msg + 3;
1841 phase = PHASE_MSGIN;
1842
1843 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001844 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1845 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 switch (extended_msg[2]) {
1848 case EXTENDED_SDTR:
1849 case EXTENDED_WDTR:
1850 case EXTENDED_MODIFY_DATA_POINTER:
1851 case EXTENDED_EXTENDED_IDENTIFY:
1852 tmp = 0;
1853 }
1854 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001855 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 tmp = 0;
1857 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001858 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1859 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 tmp = 0;
1861 }
Finn Thain11d2f632016-01-03 16:05:51 +11001862
1863 spin_lock_irq(&hostdata->lock);
1864 if (!hostdata->connected)
1865 return;
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /* Fall through to reject message */
1868
Finn Thainaff0cf92016-01-03 16:06:09 +11001869 /*
1870 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * reject it.
1872 */
1873 default:
1874 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001875 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001876 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 printk("\n");
1878 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001879 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001880 "rejecting unknown message %02x\n",
1881 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 else
Jeff Garzik017560f2005-10-24 18:04:36 -04001883 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001884 "rejecting unknown extended message code %02x, length %d\n",
1885 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 msgout = MESSAGE_REJECT;
1888 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1889 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001890 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 break;
1892 case PHASE_MSGOUT:
1893 len = 1;
1894 data = &msgout;
1895 hostdata->last_message = msgout;
1896 NCR5380_transfer_pio(instance, &phase, &len, &data);
1897 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 hostdata->connected = NULL;
1899 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001900 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1902 return;
1903 }
1904 msgout = NOP;
1905 break;
1906 case PHASE_CMDOUT:
1907 len = cmd->cmd_len;
1908 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001909 /*
1910 * XXX for performance reasons, on machines with a
1911 * PSEUDO-DMA architecture we should probably
1912 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
1914 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
1916 case PHASE_STATIN:
1917 len = 1;
1918 data = &tmp;
1919 NCR5380_transfer_pio(instance, &phase, &len, &data);
1920 cmd->SCp.Status = tmp;
1921 break;
1922 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001923 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001924 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001925 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001926 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001927 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11001928 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001929 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Finn Thain11d2f632016-01-03 16:05:51 +11001931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
1934/*
1935 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1936 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001937 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11001938 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1939 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11001940 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
1943
Finn Thain0d2cf862016-01-03 16:06:11 +11001944static void NCR5380_reselect(struct Scsi_Host *instance)
1945{
Finn Thaine8a60142016-01-03 16:05:54 +11001946 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 unsigned char target_mask;
1948 unsigned char lun, phase;
1949 int len;
1950 unsigned char msg[3];
1951 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11001952 struct NCR5380_cmd *ncmd;
1953 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /*
1956 * Disable arbitration, etc. since the host adapter obviously
1957 * lost, and tell an interrupted NCR5380_select() to restart.
1958 */
1959
1960 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11001963
1964 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Finn Thainaff0cf92016-01-03 16:06:09 +11001966 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * At this point, we have detected that our SCSI ID is on the bus,
1968 * SEL is true and BSY was false for at least one bus settle delay
1969 * (400 ns).
1970 *
1971 * We must assert BSY ourselves, until the target drops the SEL
1972 * signal.
1973 */
1974
1975 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11001976 if (NCR5380_poll_politely(instance,
1977 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
1978 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1979 return;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1982
1983 /*
1984 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 */
1986
Finn Thain1cc160e2016-01-03 16:05:32 +11001987 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11001988 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
1989 do_abort(instance);
1990 return;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 len = 1;
1994 data = msg;
1995 phase = PHASE_MSGIN;
1996 NCR5380_transfer_pio(instance, &phase, &len, &data);
1997
Finn Thain72064a72016-01-03 16:05:44 +11001998 if (len) {
1999 do_abort(instance);
2000 return;
2001 }
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002004 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002005 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002006 printk("\n");
2007 do_abort(instance);
2008 return;
2009 }
2010 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Finn Thain72064a72016-01-03 16:05:44 +11002012 /*
2013 * We need to add code for SCSI-II to track which devices have
2014 * I_T_L_Q nexuses established, and which have simple I_T_L
2015 * nexuses so we can chose to do additional data transfer.
2016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Finn Thain72064a72016-01-03 16:05:44 +11002018 /*
2019 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2020 * just reestablished, and remove it from the disconnected queue.
2021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Finn Thain32b26a12016-01-03 16:05:58 +11002023 tmp = NULL;
2024 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2025 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2026
2027 if (target_mask == (1 << scmd_id(cmd)) &&
2028 lun == (u8)cmd->device->lun) {
2029 list_del(&ncmd->list);
2030 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002034
2035 if (tmp) {
2036 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2037 "reselect: removed %p from disconnected queue\n", tmp);
2038 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002039 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2040 target_mask, lun);
2041 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002042 * Since we have an established nexus that we can't do anything
2043 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Finn Thain72064a72016-01-03 16:05:44 +11002048
2049 /* Accept message by clearing ACK */
2050 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2051
2052 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002053 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2054 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Finn Thain8b00c3d2016-01-03 16:06:01 +11002057/**
2058 * list_find_cmd - test for presence of a command in a linked list
2059 * @haystack: list of commands
2060 * @needle: command to search for
2061 */
2062
2063static bool list_find_cmd(struct list_head *haystack,
2064 struct scsi_cmnd *needle)
2065{
2066 struct NCR5380_cmd *ncmd;
2067
2068 list_for_each_entry(ncmd, haystack, list)
2069 if (NCR5380_to_scmd(ncmd) == needle)
2070 return true;
2071 return false;
2072}
2073
2074/**
2075 * list_remove_cmd - remove a command from linked list
2076 * @haystack: list of commands
2077 * @needle: command to remove
2078 */
2079
2080static bool list_del_cmd(struct list_head *haystack,
2081 struct scsi_cmnd *needle)
2082{
2083 if (list_find_cmd(haystack, needle)) {
2084 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2085
2086 list_del(&ncmd->list);
2087 return true;
2088 }
2089 return false;
2090}
2091
2092/**
2093 * NCR5380_abort - scsi host eh_abort_handler() method
2094 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002096 * Try to abort a given command by removing it from queues and/or sending
2097 * the target an abort message. This may not succeed in causing a target
2098 * to abort the command. Nonetheless, the low-level driver must forget about
2099 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002101 * The normal path taken by a command is as follows. For EH we trace this
2102 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002104 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2105 * [disconnected -> connected ->]...
2106 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002108 * If cmd was not found at all then presumably it has already been completed,
2109 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002110 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002111 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002112 * We have no option but to forget the aborted command (even if it still
2113 * lacks sense data). The mid-layer may re-issue a command that is in error
2114 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2115 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002116 *
2117 * The lock protects driver data structures, but EH handlers also use it
2118 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 */
2120
Finn Thain710ddd02014-11-12 16:12:02 +11002121static int NCR5380_abort(struct scsi_cmnd *cmd)
2122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002124 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002125 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002126 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002127
Finn Thain11d2f632016-01-03 16:05:51 +11002128 spin_lock_irqsave(&hostdata->lock, flags);
2129
Finn Thain32b26a12016-01-03 16:05:58 +11002130#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002131 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002132#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002133 NCR5380_dprint(NDEBUG_ANY, instance);
2134 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Finn Thain8b00c3d2016-01-03 16:06:01 +11002136 if (list_del_cmd(&hostdata->unissued, cmd)) {
2137 dsprintk(NDEBUG_ABORT, instance,
2138 "abort: removed %p from issue queue\n", cmd);
2139 cmd->result = DID_ABORT << 16;
2140 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002141 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002142 }
2143
Finn Thain707d62b2016-01-03 16:06:02 +11002144 if (hostdata->selecting == cmd) {
2145 dsprintk(NDEBUG_ABORT, instance,
2146 "abort: cmd %p == selecting\n", cmd);
2147 hostdata->selecting = NULL;
2148 cmd->result = DID_ABORT << 16;
2149 complete_cmd(instance, cmd);
2150 goto out;
2151 }
2152
Finn Thain8b00c3d2016-01-03 16:06:01 +11002153 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2154 dsprintk(NDEBUG_ABORT, instance,
2155 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002156 /* Can't call NCR5380_select() and send ABORT because that
2157 * means releasing the lock. Need a bus reset.
2158 */
Finn Thaindc183962016-02-23 10:07:07 +11002159 set_host_byte(cmd, DID_ERROR);
2160 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002161 result = FAILED;
2162 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002163 }
2164
2165 if (hostdata->connected == cmd) {
2166 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2167 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002168 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002169 if (do_abort(instance)) {
2170 set_host_byte(cmd, DID_ERROR);
2171 complete_cmd(instance, cmd);
2172 result = FAILED;
2173 goto out;
2174 }
2175 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002176 complete_cmd(instance, cmd);
2177 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002178 }
2179
Finn Thaindc183962016-02-23 10:07:07 +11002180 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002181 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002182 "abort: removed %p from sense queue\n", cmd);
2183 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002184 complete_cmd(instance, cmd);
2185 }
2186
2187out:
2188 if (result == FAILED)
2189 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2190 else
2191 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2192
2193 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002194 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002195
Finn Thain8b00c3d2016-01-03 16:06:01 +11002196 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199
Finn Thain3be1b3e2016-01-03 16:05:12 +11002200/**
2201 * NCR5380_bus_reset - reset the SCSI bus
2202 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002204 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 */
2206
Finn Thain710ddd02014-11-12 16:12:02 +11002207static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002208{
2209 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002210 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002211 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002212 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002213 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Finn Thain11d2f632016-01-03 16:05:51 +11002215 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002216
2217#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002218 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002219#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002220 NCR5380_dprint(NDEBUG_ANY, instance);
2221 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002222
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002223 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002224
Finn Thain62717f52016-01-03 16:06:03 +11002225 /* reset NCR registers */
2226 NCR5380_write(MODE_REG, MR_BASE);
2227 NCR5380_write(TARGET_COMMAND_REG, 0);
2228 NCR5380_write(SELECT_ENABLE_REG, 0);
2229
2230 /* After the reset, there are no more connected or disconnected commands
2231 * and no busy units; so clear the low-level status here to avoid
2232 * conflicts when the mid-level code tries to wake up the affected
2233 * commands!
2234 */
2235
Finn Thain1884c282016-02-23 10:07:04 +11002236 if (list_del_cmd(&hostdata->unissued, cmd)) {
2237 cmd->result = DID_RESET << 16;
2238 cmd->scsi_done(cmd);
2239 }
2240
2241 if (hostdata->selecting) {
2242 hostdata->selecting->result = DID_RESET << 16;
2243 complete_cmd(instance, hostdata->selecting);
2244 hostdata->selecting = NULL;
2245 }
Finn Thain62717f52016-01-03 16:06:03 +11002246
2247 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2248 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2249
2250 set_host_byte(cmd, DID_RESET);
2251 cmd->scsi_done(cmd);
2252 }
Finn Thain1884c282016-02-23 10:07:04 +11002253 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002254
2255 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2256 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2257
2258 set_host_byte(cmd, DID_RESET);
2259 cmd->scsi_done(cmd);
2260 }
Finn Thain1884c282016-02-23 10:07:04 +11002261 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002262
2263 if (hostdata->connected) {
2264 set_host_byte(hostdata->connected, DID_RESET);
2265 complete_cmd(instance, hostdata->connected);
2266 hostdata->connected = NULL;
2267 }
2268
Finn Thain62717f52016-01-03 16:06:03 +11002269 for (i = 0; i < 8; ++i)
2270 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002271 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002272
2273 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002274 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return SUCCESS;
2277}