blob: fbd8a98af88112deb4261fb7b378f1c841c03817 [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 *
Finn Thainaff0cf92016-01-03 16:06:09 +110035 * 4. Test SCSI-II tagged queueing (I have no devices which support
Finn Thain594d4ba2016-01-03 16:06:10 +110036 * tagged queueing)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * Design
41 *
Finn Thainaff0cf92016-01-03 16:06:09 +110042 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * one simply writes appropriate system specific macros (ie, data
Finn Thainaff0cf92016-01-03 16:06:09 +110044 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * memory mapped) and drops this file in their 'C' wrapper.
46 *
Finn Thainaff0cf92016-01-03 16:06:09 +110047 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * each 5380 in the system - commands that haven't been issued yet,
Finn Thainaff0cf92016-01-03 16:06:09 +110049 * and commands that are currently executing. This means that an
50 * unlimited number of commands may be queued, letting
51 * more commands propagate from the higher driver levels giving higher
52 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
53 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * while a command is already executing.
55 *
56 *
Finn Thainaff0cf92016-01-03 16:06:09 +110057 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
Finn Thainaff0cf92016-01-03 16:06:09 +110059 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
60 * piece of hardware that requires you to sit in a loop polling for
61 * the REQ signal as long as you are connected. Some devices are
62 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110063 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * broken devices are the exception rather than the rule and I'd rather
65 * spend my time optimizing for the normal case.
66 *
67 * Architecture :
68 *
69 * At the heart of the design is a coroutine, NCR5380_main,
70 * which is started from a workqueue for each NCR5380 host in the
71 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
72 * removing the commands from the issue queue and calling
Finn Thainaff0cf92016-01-03 16:06:09 +110073 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * Once a nexus is established, the NCR5380_information_transfer()
76 * phase goes through the various phases as instructed by the target.
77 * if the target goes into MSG IN and sends a DISCONNECT message,
78 * the command structure is placed into the per instance disconnected
Finn Thainaff0cf92016-01-03 16:06:09 +110079 * queue, and NCR5380_main tries to find more work. If the target is
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * idle for too long, the system will try to sleep.
81 *
82 * If a command has disconnected, eventually an interrupt will trigger,
83 * calling NCR5380_intr() which will in turn call NCR5380_reselect
84 * to reestablish a nexus. This will run main if necessary.
85 *
Finn Thainaff0cf92016-01-03 16:06:09 +110086 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * appropriate.
88 *
Finn Thainaff0cf92016-01-03 16:06:09 +110089 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * structures, being initialized after the command is connected
91 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
92 * Note that in violation of the standard, an implicit SAVE POINTERS operation
93 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
94 */
95
96/*
97 * Using this file :
98 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Finn Thainaff0cf92016-01-03 16:06:09 +110099 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * and macros and include this file in your driver.
101 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100102 * These macros control options :
103 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
Finn Thain594d4ba2016-01-03 16:06:10 +1100104 * defined.
Finn Thainaff0cf92016-01-03 16:06:09 +1100105 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Finn Thain594d4ba2016-01-03 16:06:10 +1100107 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
109 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
Finn Thain594d4ba2016-01-03 16:06:10 +1100110 * transceivers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
112 * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
Finn Thain594d4ba2016-01-03 16:06:10 +1100113 * override-configure an IRQ.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
116 *
Finn Thain8053b0e2016-03-23 21:10:19 +1100117 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
118 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100120 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * NCR5380_read(register) - read from the specified register
122 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100123 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100125 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100126 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * Either real DMA *or* pseudo DMA may be implemented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * NCR5380_dma_write_setup(instance, src, count) - initialize
131 * NCR5380_dma_read_setup(instance, dst, count) - initialize
132 * NCR5380_dma_residual(instance); - residual count
133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * The generic driver is initialized by calling NCR5380_init(instance),
Finn Thainaff0cf92016-01-03 16:06:09 +1100135 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
137 * possible) function may be used.
138 */
139
Finn Thaine5d55d12016-03-23 21:10:16 +1100140#ifndef NCR5380_io_delay
141#define NCR5380_io_delay(x)
142#endif
143
Finn Thain54d8fe42016-01-03 16:05:06 +1100144static int do_abort(struct Scsi_Host *);
145static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Finn Thainc16df322016-01-03 16:06:08 +1100147/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100148 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100149 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100151 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
153
Finn Thain710ddd02014-11-12 16:12:02 +1100154static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Finn Thainaff0cf92016-01-03 16:06:09 +1100156 /*
157 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * various queues are valid.
159 */
160
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200161 if (scsi_bufflen(cmd)) {
162 cmd->SCp.buffer = scsi_sglist(cmd);
163 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200164 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 cmd->SCp.this_residual = cmd->SCp.buffer->length;
166 } else {
167 cmd->SCp.buffer = NULL;
168 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200169 cmd->SCp.ptr = NULL;
170 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100172
173 cmd->SCp.Status = 0;
174 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/**
Finn Thainb32ade12016-01-03 16:05:41 +1100178 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100179 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100180 * @reg1: 5380 register to poll
181 * @bit1: Bitmask to check
182 * @val1: Expected value
183 * @reg2: Second 5380 register to poll
184 * @bit2: Second bitmask to check
185 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100186 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
Finn Thain2f854b82016-01-03 16:05:22 +1100188 * Polls the chip in a reasonably efficient manner waiting for an
189 * event to occur. After a short quick poll we begin to yield the CPU
190 * (if possible). In irq contexts the time-out is arbitrarily limited.
191 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Finn Thainb32ade12016-01-03 16:05:41 +1100193 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Finn Thainb32ade12016-01-03 16:05:41 +1100196static int NCR5380_poll_politely2(struct Scsi_Host *instance,
197 int reg1, int bit1, int val1,
198 int reg2, int bit2, int val2, int wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100199{
200 struct NCR5380_hostdata *hostdata = shost_priv(instance);
201 unsigned long deadline = jiffies + wait;
202 unsigned long n;
203
204 /* Busy-wait for up to 10 ms */
205 n = min(10000U, jiffies_to_usecs(wait));
206 n *= hostdata->accesses_per_ms;
Finn Thainb32ade12016-01-03 16:05:41 +1100207 n /= 2000;
Finn Thain2f854b82016-01-03 16:05:22 +1100208 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100209 if ((NCR5380_read(reg1) & bit1) == val1)
210 return 0;
211 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100214 } while (n--);
215
216 if (irqs_disabled() || in_interrupt())
217 return -ETIMEDOUT;
218
219 /* Repeatedly sleep for 1 ms until deadline */
220 while (time_is_after_jiffies(deadline)) {
221 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100222 if ((NCR5380_read(reg1) & bit1) == val1)
223 return 0;
224 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Finn Thain2f854b82016-01-03 16:05:22 +1100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -ETIMEDOUT;
229}
230
Finn Thainb32ade12016-01-03 16:05:41 +1100231static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
232 int reg, int bit, int val, int wait)
233{
234 return NCR5380_poll_politely2(instance, reg, bit, val,
235 reg, bit, val, wait);
236}
237
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100238#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static struct {
240 unsigned char mask;
241 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100242} signals[] = {
243 {SR_DBP, "PARITY"},
244 {SR_RST, "RST"},
245 {SR_BSY, "BSY"},
246 {SR_REQ, "REQ"},
247 {SR_MSG, "MSG"},
248 {SR_CD, "CD"},
249 {SR_IO, "IO"},
250 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100252},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253basrs[] = {
Finn Thainaff0cf92016-01-03 16:06:09 +1100254 {BASR_ATN, "ATN"},
255 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100257},
258icrs[] = {
259 {ICR_ASSERT_RST, "ASSERT RST"},
260 {ICR_ASSERT_ACK, "ASSERT ACK"},
261 {ICR_ASSERT_BSY, "ASSERT BSY"},
262 {ICR_ASSERT_SEL, "ASSERT SEL"},
263 {ICR_ASSERT_ATN, "ASSERT ATN"},
264 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100266},
267mrs[] = {
268 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
269 {MR_TARGET, "MODE TARGET"},
270 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
271 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
Finn Thain0d2cf862016-01-03 16:06:11 +1100272 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100273 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
274 {MR_DMA_MODE, "MODE DMA"},
275 {MR_ARBITRATE, "MODE ARBITRATION"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0, NULL}
277};
278
279/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100280 * NCR5380_print - print scsi bus signals
281 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100283 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285
286static void NCR5380_print(struct Scsi_Host *instance)
287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
291 status = NCR5380_read(STATUS_REG);
292 mr = NCR5380_read(MODE_REG);
293 icr = NCR5380_read(INITIATOR_COMMAND_REG);
294 basr = NCR5380_read(BUS_AND_STATUS_REG);
295
296 printk("STATUS_REG: %02x ", status);
297 for (i = 0; signals[i].mask; ++i)
298 if (status & signals[i].mask)
299 printk(",%s", signals[i].name);
300 printk("\nBASR: %02x ", basr);
301 for (i = 0; basrs[i].mask; ++i)
302 if (basr & basrs[i].mask)
303 printk(",%s", basrs[i].name);
304 printk("\nICR: %02x ", icr);
305 for (i = 0; icrs[i].mask; ++i)
306 if (icr & icrs[i].mask)
307 printk(",%s", icrs[i].name);
308 printk("\nMODE: %02x ", mr);
309 for (i = 0; mrs[i].mask; ++i)
310 if (mr & mrs[i].mask)
311 printk(",%s", mrs[i].name);
312 printk("\n");
313}
314
Finn Thain0d2cf862016-01-03 16:06:11 +1100315static struct {
316 unsigned char value;
317 const char *name;
318} phases[] = {
319 {PHASE_DATAOUT, "DATAOUT"},
320 {PHASE_DATAIN, "DATAIN"},
321 {PHASE_CMDOUT, "CMDOUT"},
322 {PHASE_STATIN, "STATIN"},
323 {PHASE_MSGOUT, "MSGOUT"},
324 {PHASE_MSGIN, "MSGIN"},
325 {PHASE_UNKNOWN, "UNKNOWN"}
326};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Finn Thainc16df322016-01-03 16:06:08 +1100328/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100329 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100330 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100332 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334
335static void NCR5380_print_phase(struct Scsi_Host *instance)
336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned char status;
338 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 status = NCR5380_read(STATUS_REG);
341 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100342 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100344 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
345 (phases[i].value != (status & PHASE_MASK)); ++i)
346 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100347 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350#endif
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Finn Thaind5f7e652016-01-03 16:05:03 +1100353static int probe_irq __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100356 * probe_intr - helper for IRQ autoprobe
357 * @irq: interrupt number
358 * @dev_id: unused
359 * @regs: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100361 * Set a flag to indicate the IRQ in question was received. This is
362 * used by the IRQ probe code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100364
David Howells7d12e782006-10-05 14:55:46 +0100365static irqreturn_t __init probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 probe_irq = irq;
368 return IRQ_HANDLED;
369}
370
371/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100372 * NCR5380_probe_irq - find the IRQ of an NCR5380
373 * @instance: NCR5380 controller
374 * @possible: bitmask of ISA IRQ lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100376 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
377 * and then looking to see what interrupt actually turned up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
379
Andrew Morton702809c2007-05-23 14:41:56 -0700380static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
381 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Finn Thaine8a60142016-01-03 16:05:54 +1100383 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned long timeout;
385 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Finn Thain22f5f102014-11-12 16:11:56 +1100387 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100388 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 trying_irqs |= mask;
390
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500391 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100392 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
395 * A interrupt is triggered whenever BSY = false, SEL = true
Finn Thainaff0cf92016-01-03 16:06:09 +1100396 * and a bit set in the SELECT_ENABLE_REG is asserted on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * SCSI bus.
398 *
399 * Note that the bus is only driven when the phase control signals
400 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
401 * to zero.
402 */
403
404 NCR5380_write(TARGET_COMMAND_REG, 0);
405 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
406 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
407 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
408
Finn Thain22f5f102014-11-12 16:11:56 +1100409 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800410 schedule_timeout_uninterruptible(1);
Finn Thainaff0cf92016-01-03 16:06:09 +1100411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 NCR5380_write(SELECT_ENABLE_REG, 0);
413 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
414
Finn Thain22f5f102014-11-12 16:11:56 +1100415 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (trying_irqs & mask)
417 free_irq(i, NULL);
418
419 return probe_irq;
420}
421
422/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100423 * NCR58380_info - report driver and host information
424 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100426 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
428
Finn Thain8c325132014-11-12 16:11:58 +1100429static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Finn Thain8c325132014-11-12 16:11:58 +1100431 struct NCR5380_hostdata *hostdata = shost_priv(instance);
432
433 return hostdata->info;
434}
435
436static void prepare_info(struct Scsi_Host *instance)
437{
438 struct NCR5380_hostdata *hostdata = shost_priv(instance);
439
440 snprintf(hostdata->info, sizeof(hostdata->info),
441 "%s, io_port 0x%lx, n_io_port %d, "
442 "base 0x%lx, irq %d, "
443 "can_queue %d, cmd_per_lun %d, "
444 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100445 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100446 "options { %s} ",
447 instance->hostt->name, instance->io_port, instance->n_io_port,
448 instance->base, instance->irq,
449 instance->can_queue, instance->cmd_per_lun,
450 instance->sg_tablesize, instance->this_id,
Finn Thain1bb46002016-03-23 21:10:14 +1100451 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100452 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100453 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#ifdef AUTOPROBE_IRQ
Finn Thain8c325132014-11-12 16:11:58 +1100455 "AUTOPROBE_IRQ "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100458 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100461 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#endif
Finn Thain8c325132014-11-12 16:11:58 +1100463 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100467 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100468 * @instance: adapter to configure
469 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100471 * Initializes *instance and corresponding 5380 chip,
472 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100474 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100475 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100477 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
479
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800480static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Finn Thaine8a60142016-01-03 16:05:54 +1100482 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100483 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100484 unsigned long deadline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Finn Thain0d2cf862016-01-03 16:06:11 +1100486 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100488 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
490 if (i > hostdata->id_mask)
491 hostdata->id_higher_mask |= i;
492 for (i = 0; i < 8; ++i)
493 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100494 hostdata->dma_len = 0;
495
Finn Thain11d2f632016-01-03 16:05:51 +1100496 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100498 hostdata->sensing = NULL;
499 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100500 INIT_LIST_HEAD(&hostdata->unissued);
501 INIT_LIST_HEAD(&hostdata->disconnected);
502
Finn Thain55181be2016-01-03 16:05:42 +1100503 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100504
Finn Thain8d8601a2016-01-03 16:05:37 +1100505 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100506 hostdata->work_q = alloc_workqueue("ncr5380_%d",
507 WQ_UNBOUND | WQ_MEM_RECLAIM,
508 1, instance->host_no);
509 if (!hostdata->work_q)
510 return -ENOMEM;
511
Finn Thain8c325132014-11-12 16:11:58 +1100512 prepare_info(instance);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
515 NCR5380_write(MODE_REG, MR_BASE);
516 NCR5380_write(TARGET_COMMAND_REG, 0);
517 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100518
519 /* Calibrate register polling loop */
520 i = 0;
521 deadline = jiffies + 1;
522 do {
523 cpu_relax();
524 } while (time_is_after_jiffies(deadline));
525 deadline += msecs_to_jiffies(256);
526 do {
527 NCR5380_read(STATUS_REG);
528 ++i;
529 cpu_relax();
530 } while (time_is_after_jiffies(deadline));
531 hostdata->accesses_per_ms = i / 256;
532
Finn Thainb6488f92016-01-03 16:05:08 +1100533 return 0;
534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Finn Thainb6488f92016-01-03 16:05:08 +1100536/**
537 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
538 * @instance: adapter to check
539 *
540 * If the system crashed, it may have crashed with a connected target and
541 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
542 * currently established nexus, which we know nothing about. Failing that
543 * do a bus reset.
544 *
545 * Note that a bus reset will cause the chip to assert IRQ.
546 *
547 * Returns 0 if successful, otherwise -ENXIO.
548 */
549
550static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
551{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100552 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100553 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
556 switch (pass) {
557 case 1:
558 case 3:
559 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100560 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
561 NCR5380_poll_politely(instance,
562 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100565 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 do_abort(instance);
567 break;
568 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100569 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100571 /* Wait after a reset; the SCSI standard calls for
572 * 250ms, we wait 500ms to be on the safe side.
573 * But some Toshiba CD-ROMs need ten times that.
574 */
575 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
576 msleep(2500);
577 else
578 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100581 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -ENXIO;
583 }
584 }
585 return 0;
586}
587
588/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100589 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100590 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100591 *
592 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800595static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Finn Thaine8a60142016-01-03 16:05:54 +1100597 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Finn Thain8d8601a2016-01-03 16:05:37 +1100599 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100600 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/**
Finn Thain677e0192016-01-03 16:05:59 +1100604 * complete_cmd - finish processing a command and return it to the SCSI ML
605 * @instance: the host instance
606 * @cmd: command to complete
607 */
608
609static void complete_cmd(struct Scsi_Host *instance,
610 struct scsi_cmnd *cmd)
611{
612 struct NCR5380_hostdata *hostdata = shost_priv(instance);
613
614 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
615
Finn Thainf27db8e2016-01-03 16:06:00 +1100616 if (hostdata->sensing == cmd) {
617 /* Autosense processing ends here */
618 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
619 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
620 set_host_byte(cmd, DID_ERROR);
621 } else
622 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
623 hostdata->sensing = NULL;
624 }
625
Finn Thain677e0192016-01-03 16:05:59 +1100626 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
627
628 cmd->scsi_done(cmd);
629}
630
631/**
Finn Thain1bb40582016-01-03 16:05:29 +1100632 * NCR5380_queue_command - queue a command
633 * @instance: the relevant SCSI adapter
634 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
Finn Thain1bb40582016-01-03 16:05:29 +1100636 * cmd is added to the per-instance issue queue, with minor
637 * twiddling done to the host specific fields of cmd. If the
638 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
640
Finn Thain1bb40582016-01-03 16:05:29 +1100641static int NCR5380_queue_command(struct Scsi_Host *instance,
642 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Finn Thain1bb40582016-01-03 16:05:29 +1100644 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100645 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100646 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648#if (NDEBUG & NDEBUG_NO_WRITE)
649 switch (cmd->cmnd[0]) {
650 case WRITE_6:
651 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100652 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100654 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100657#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 cmd->result = 0;
660
Finn Thain11d2f632016-01-03 16:05:51 +1100661 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100662
Finn Thainaff0cf92016-01-03 16:06:09 +1100663 /*
664 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100666 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * sense data is only guaranteed to be valid while the condition exists.
668 */
669
Finn Thain32b26a12016-01-03 16:05:58 +1100670 if (cmd->cmnd[0] == REQUEST_SENSE)
671 list_add(&ncmd->list, &hostdata->unissued);
672 else
673 list_add_tail(&ncmd->list, &hostdata->unissued);
674
Finn Thain11d2f632016-01-03 16:05:51 +1100675 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100676
Finn Thaindbb6b352016-01-03 16:05:53 +1100677 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
678 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100681 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100686 * dequeue_next_cmd - dequeue a command for processing
687 * @instance: the scsi host instance
688 *
689 * Priority is given to commands on the autosense queue. These commands
690 * need autosense because of a CHECK CONDITION result.
691 *
692 * Returns a command pointer if a command is found for a target that is
693 * not already busy. Otherwise returns NULL.
694 */
695
696static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
697{
698 struct NCR5380_hostdata *hostdata = shost_priv(instance);
699 struct NCR5380_cmd *ncmd;
700 struct scsi_cmnd *cmd;
701
Finn Thain8d5dbec2016-02-23 10:07:09 +1100702 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100703 list_for_each_entry(ncmd, &hostdata->unissued, list) {
704 cmd = NCR5380_to_scmd(ncmd);
705 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
706 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
707
708 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
709 list_del(&ncmd->list);
710 dsprintk(NDEBUG_QUEUES, instance,
711 "dequeue: removed %p from issue queue\n", cmd);
712 return cmd;
713 }
714 }
715 } else {
716 /* Autosense processing begins here */
717 ncmd = list_first_entry(&hostdata->autosense,
718 struct NCR5380_cmd, list);
719 list_del(&ncmd->list);
720 cmd = NCR5380_to_scmd(ncmd);
721 dsprintk(NDEBUG_QUEUES, instance,
722 "dequeue: removed %p from autosense queue\n", cmd);
723 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
724 hostdata->sensing = cmd;
725 return cmd;
726 }
727 return NULL;
728}
729
730static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
731{
732 struct NCR5380_hostdata *hostdata = shost_priv(instance);
733 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
734
Finn Thain8d5dbec2016-02-23 10:07:09 +1100735 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100736 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
737 list_add(&ncmd->list, &hostdata->autosense);
738 hostdata->sensing = NULL;
739 } else
740 list_add(&ncmd->list, &hostdata->unissued);
741}
742
743/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100744 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100746 * NCR5380_main is a coroutine that runs as long as more work can
747 * be done on the NCR5380 host adapters in a system. Both
748 * NCR5380_queue_command() and NCR5380_intr() will try to start it
749 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
751
David Howellsc4028952006-11-22 14:57:56 +0000752static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
David Howellsc4028952006-11-22 14:57:56 +0000754 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100755 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100761
Finn Thain0a4e3612016-01-03 16:06:07 +1100762 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100763 while (!hostdata->connected && !hostdata->selecting) {
764 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
765
766 if (!cmd)
767 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100768
769 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100772 * Attempt to establish an I_T_L nexus here.
773 * On success, instance->hostdata->connected is set.
774 * On failure, we must add the command back to the
775 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100777 /*
778 * REQUEST SENSE commands are issued without tagged
779 * queueing, even on SCSI-II devices because the
780 * contingent allegiance condition exists for the
781 * entire unit.
782 */
Finn Thain32b26a12016-01-03 16:05:58 +1100783
Finn Thainccf6efd2016-02-23 10:07:08 +1100784 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100785 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thainf27db8e2016-01-03 16:06:00 +1100786 } else {
787 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
788 "main: select failed, returning %p to queue\n", cmd);
789 requeue_cmd(instance, cmd);
790 }
791 }
Finn Thaine4dec682016-03-23 21:10:12 +1100792 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100793 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100796 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100797 spin_unlock_irq(&hostdata->lock);
798 if (!done)
799 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Finn Thain8053b0e2016-03-23 21:10:19 +1100803/*
804 * NCR5380_dma_complete - finish DMA transfer
805 * @instance: the scsi host instance
806 *
807 * Called by the interrupt handler when DMA finishes or a phase
808 * mismatch occurs (which would end the DMA transfer).
809 */
810
811static void NCR5380_dma_complete(struct Scsi_Host *instance)
812{
813 struct NCR5380_hostdata *hostdata = shost_priv(instance);
814 int transferred;
815 unsigned char **data;
816 int *count;
817 int saved_data = 0, overrun = 0;
818 unsigned char p;
819
820 if (hostdata->read_overruns) {
821 p = hostdata->connected->SCp.phase;
822 if (p & SR_IO) {
823 udelay(10);
824 if ((NCR5380_read(BUS_AND_STATUS_REG) &
825 (BASR_PHASE_MATCH | BASR_ACK)) ==
826 (BASR_PHASE_MATCH | BASR_ACK)) {
827 saved_data = NCR5380_read(INPUT_DATA_REG);
828 overrun = 1;
829 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
830 }
831 }
832 }
833
834 NCR5380_write(MODE_REG, MR_BASE);
835 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
836 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
837
838 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
839 hostdata->dma_len = 0;
840
841 data = (unsigned char **)&hostdata->connected->SCp.ptr;
842 count = &hostdata->connected->SCp.this_residual;
843 *data += transferred;
844 *count -= transferred;
845
846 if (hostdata->read_overruns) {
847 int cnt, toPIO;
848
849 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
850 cnt = toPIO = hostdata->read_overruns;
851 if (overrun) {
852 dsprintk(NDEBUG_DMA, instance,
853 "Got an input overrun, using saved byte\n");
854 *(*data)++ = saved_data;
855 (*count)--;
856 cnt--;
857 toPIO--;
858 }
859 if (toPIO > 0) {
860 dsprintk(NDEBUG_DMA, instance,
861 "Doing %d byte PIO to 0x%p\n", cnt, *data);
862 NCR5380_transfer_pio(instance, &p, &cnt, data);
863 *count -= toPIO - cnt;
864 }
865 }
866 }
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#ifndef DONT_USE_INTR
870
871/**
Finn Thaincd400822016-01-03 16:05:40 +1100872 * NCR5380_intr - generic NCR5380 irq handler
873 * @irq: interrupt number
874 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Finn Thaincd400822016-01-03 16:05:40 +1100876 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
877 * from the disconnected queue, and restarting NCR5380_main()
878 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *
Finn Thaincd400822016-01-03 16:05:40 +1100880 * The chip can assert IRQ in any of six different conditions. The IRQ flag
881 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
882 * Three of these six conditions are latched in the Bus and Status Register:
883 * - End of DMA (cleared by ending DMA Mode)
884 * - Parity error (cleared by reading RPIR)
885 * - Loss of BSY (cleared by reading RPIR)
886 * Two conditions have flag bits that are not latched:
887 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
888 * - Bus reset (non-maskable)
889 * The remaining condition has no flag bit at all:
890 * - Selection/reselection
891 *
892 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
893 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
894 * claimed that "the design of the [DP8490] interrupt logic ensures
895 * interrupts will not be lost (they can be on the DP5380)."
896 * The L5380/53C80 datasheet from LOGIC Devices has more details.
897 *
898 * Checking for bus reset by reading RST is futile because of interrupt
899 * latency, but a bus reset will reset chip logic. Checking for parity error
900 * is unnecessary because that interrupt is never enabled. A Loss of BSY
901 * condition will clear DMA Mode. We can tell when this occurs because the
902 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
904
Finn Thaincd400822016-01-03 16:05:40 +1100905static irqreturn_t NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800907 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100908 struct NCR5380_hostdata *hostdata = shost_priv(instance);
909 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned char basr;
911 unsigned long flags;
912
Finn Thain11d2f632016-01-03 16:05:51 +1100913 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Finn Thaincd400822016-01-03 16:05:40 +1100915 basr = NCR5380_read(BUS_AND_STATUS_REG);
916 if (basr & BASR_IRQ) {
917 unsigned char mr = NCR5380_read(MODE_REG);
918 unsigned char sr = NCR5380_read(STATUS_REG);
919
Finn Thainb7465452016-01-03 16:06:05 +1100920 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
921 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100922
Finn Thain8053b0e2016-03-23 21:10:19 +1100923 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
924 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
925 * We ack IRQ after clearing Mode Register. Workarounds
926 * for End of DMA errata need to happen in DMA Mode.
927 */
928
929 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
930
931 if (hostdata->connected) {
932 NCR5380_dma_complete(instance);
933 queue_work(hostdata->work_q, &hostdata->main_task);
934 } else {
935 NCR5380_write(MODE_REG, MR_BASE);
936 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
937 }
938 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100939 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
940 /* Probably reselected */
941 NCR5380_write(SELECT_ENABLE_REG, 0);
942 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
943
Finn Thainb7465452016-01-03 16:06:05 +1100944 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100945
946 if (!hostdata->connected) {
947 NCR5380_reselect(instance);
948 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Finn Thaincd400822016-01-03 16:05:40 +1100950 if (!hostdata->connected)
951 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
952 } else {
953 /* Probably Bus Reset */
954 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
955
Finn Thainb7465452016-01-03 16:06:05 +1100956 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaincd400822016-01-03 16:05:40 +1100957 }
958 handled = 1;
959 } else {
960 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
961 }
962
Finn Thain11d2f632016-01-03 16:05:51 +1100963 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100964
965 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Finn Thainaff0cf92016-01-03 16:06:09 +1100968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Finn Thainaff0cf92016-01-03 16:06:09 +1100970/*
Finn Thain710ddd02014-11-12 16:12:02 +1100971 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +1100972 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
974 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +1100975 * including ARBITRATION, SELECTION, and initial message out for
976 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100978 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +1100979 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +1100980 *
Finn Thain707d62b2016-01-03 16:06:02 +1100981 * Returns cmd if selection failed but should be retried,
982 * NULL if selection failed and should not be retried, or
983 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100985 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100986 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
987 * with registers as they should have been on entry - ie
988 * SELECT_ENABLE will be set appropriately, the NCR5380
989 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100991 * If successful : I_T_L or I_T_L_Q nexus will be established,
992 * instance->connected will be set to cmd.
993 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100995 * If failed (no target) : cmd->scsi_done() will be called, and the
996 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100998
Finn Thain707d62b2016-01-03 16:06:02 +1100999static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1000 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Finn Thaine8a60142016-01-03 16:05:54 +11001002 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned char tmp[3], phase;
1004 unsigned char *data;
1005 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +11001009 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1010 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Finn Thain707d62b2016-01-03 16:06:02 +11001012 /*
1013 * Arbitration and selection phases are slow and involve dropping the
1014 * lock, so we have to watch out for EH. An exception handler may
1015 * change 'selecting' to NULL. This function will then return NULL
1016 * so that the caller will forget about 'cmd'. (During information
1017 * transfer phases, EH may change 'connected' to NULL.)
1018 */
1019 hostdata->selecting = cmd;
1020
Finn Thainaff0cf92016-01-03 16:06:09 +11001021 /*
1022 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * data bus during SELECTION.
1024 */
1025
1026 NCR5380_write(TARGET_COMMAND_REG, 0);
1027
Finn Thainaff0cf92016-01-03 16:06:09 +11001028 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * Start arbitration.
1030 */
1031
1032 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1033 NCR5380_write(MODE_REG, MR_ARBITRATE);
1034
Finn Thain55500d92016-01-03 16:05:35 +11001035 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1036 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
1038
Finn Thain11d2f632016-01-03 16:05:51 +11001039 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001040 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1041 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1042 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001043 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001044 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1045 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001046 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001047 }
Finn Thainccf6efd2016-02-23 10:07:08 +11001048 if (!hostdata->selecting) {
1049 /* Command was aborted */
1050 NCR5380_write(MODE_REG, MR_BASE);
1051 goto out;
1052 }
Finn Thainb32ade12016-01-03 16:05:41 +11001053 if (err < 0) {
1054 NCR5380_write(MODE_REG, MR_BASE);
1055 shost_printk(KERN_ERR, instance,
1056 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001057 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001058 }
Finn Thain11d2f632016-01-03 16:05:51 +11001059 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001060
1061 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 udelay(3);
1063
1064 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001065 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1066 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1067 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001069 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001070 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Finn Thaincf13b082016-01-03 16:05:18 +11001073
1074 /* After/during arbitration, BSY should be asserted.
1075 * IBM DPES-31080 Version S31Q works now
1076 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1077 */
1078 NCR5380_write(INITIATOR_COMMAND_REG,
1079 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Finn Thainaff0cf92016-01-03 16:06:09 +11001081 /*
1082 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * a minimum so we'll udelay ceil(1.2)
1084 */
1085
Finn Thain9c3f0e22016-01-03 16:05:11 +11001086 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1087 udelay(15);
1088 else
1089 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Finn Thain11d2f632016-01-03 16:05:51 +11001091 spin_lock_irq(&hostdata->lock);
1092
Finn Thain72064a72016-01-03 16:05:44 +11001093 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1094 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001095 goto out;
1096
1097 if (!hostdata->selecting) {
1098 NCR5380_write(MODE_REG, MR_BASE);
1099 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1100 goto out;
1101 }
Finn Thain72064a72016-01-03 16:05:44 +11001102
Finn Thainb7465452016-01-03 16:06:05 +11001103 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Finn Thainaff0cf92016-01-03 16:06:09 +11001105 /*
1106 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * the host and target ID's on the SCSI bus.
1108 */
1109
Finn Thain3d07d222016-01-03 16:06:13 +11001110 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Finn Thainaff0cf92016-01-03 16:06:09 +11001112 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * Raise ATN while SEL is true before BSY goes false from arbitration,
1114 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1115 * phase immediately after selection.
1116 */
1117
Finn Thain3d07d222016-01-03 16:06:13 +11001118 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1119 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 NCR5380_write(MODE_REG, MR_BASE);
1121
Finn Thainaff0cf92016-01-03 16:06:09 +11001122 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * Reselect interrupts must be turned off prior to the dropping of BSY,
1124 * otherwise we will trigger an interrupt.
1125 */
1126 NCR5380_write(SELECT_ENABLE_REG, 0);
1127
Finn Thain11d2f632016-01-03 16:05:51 +11001128 spin_unlock_irq(&hostdata->lock);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001131 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * the BSY signal.
1133 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001134 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001137 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1138 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Finn Thainaff0cf92016-01-03 16:06:09 +11001140 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001142 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * appropriate propagation delay has expired, and we're confusing
1144 * a BSY signal from ourselves as the target's response to SELECTION.
1145 *
1146 * A small delay (the 'C++' frontend breaks the pipeline with an
1147 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001148 * tighter 'C' code breaks and requires this) solves the problem -
1149 * the 1 us delay is arbitrary, and only used because this delay will
1150 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * work there.
1152 *
1153 * wingel suggests that this could be due to failing to wait
1154 * one deskew delay.
1155 */
1156
1157 udelay(1);
1158
Finn Thainb7465452016-01-03 16:06:05 +11001159 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Finn Thainaff0cf92016-01-03 16:06:09 +11001161 /*
1162 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * selection.
1164 */
1165
Finn Thainae753a32016-01-03 16:05:23 +11001166 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1167 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001170 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1172 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001173 if (!hostdata->connected)
1174 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001175 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Finn Thainae753a32016-01-03 16:05:23 +11001178
1179 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001180 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001181 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001182 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001183 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1184 if (hostdata->selecting) {
1185 cmd->result = DID_BAD_TARGET << 16;
1186 complete_cmd(instance, cmd);
1187 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1188 cmd = NULL;
1189 }
1190 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001191 }
1192
Finn Thainaff0cf92016-01-03 16:06:09 +11001193 /*
1194 * No less than two deskew delays after the initiator detects the
1195 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 * change the DATA BUS. -wingel
1197 */
1198
1199 udelay(1);
1200
1201 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001204 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * was true but before BSY was false during selection, the information
1206 * transfer phase should be a MESSAGE OUT phase so that we can send the
1207 * IDENTIFY message.
Finn Thainaff0cf92016-01-03 16:06:09 +11001208 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1210 * message (2 bytes) with a tag ID that we increment with every command
1211 * until it wraps back to 0.
1212 *
1213 * XXX - it turns out that there are some broken SCSI-II devices,
Finn Thain594d4ba2016-01-03 16:06:10 +11001214 * which claim to support tagged queuing but fail when more than
1215 * some number of commands are issued at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
1217
1218 /* Wait for start of REQ/ACK handshake */
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001221 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001222 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001223 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1224 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001226 goto out;
1227 }
1228 if (!hostdata->selecting) {
1229 do_abort(instance);
1230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Finn Thainb7465452016-01-03 16:06:05 +11001233 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1234 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001235 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 len = 1;
1238 cmd->tag = 0;
1239
1240 /* Send message(s) */
1241 data = tmp;
1242 phase = PHASE_MSGOUT;
1243 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001244 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001248 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Boaz Harrosh28424d32007-09-10 22:37:45 +03001250 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Finn Thain707d62b2016-01-03 16:06:02 +11001252 cmd = NULL;
1253
1254out:
1255 if (!hostdata->selecting)
1256 return NULL;
1257 hostdata->selecting = NULL;
1258 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Finn Thainaff0cf92016-01-03 16:06:09 +11001261/*
1262 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001263 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 *
1265 * Purpose : transfers data in given phase using polled I/O
1266 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001267 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001268 * what phase is expected, *count - pointer to number of
1269 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001270 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001272 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001273 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001275 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
1277 * XXX Note : handling for bus free may be useful.
1278 */
1279
1280/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001281 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * IS 100% reliable, and for the actual data transfer where speed
1283 * counts, we will always do a pseudo DMA or DMA transfer.
1284 */
1285
Finn Thain0d2cf862016-01-03 16:06:11 +11001286static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1287 unsigned char *phase, int *count,
1288 unsigned char **data)
1289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned char p = *phase, tmp;
1291 int c = *count;
1292 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Finn Thainaff0cf92016-01-03 16:06:09 +11001294 /*
1295 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * phase specified in the appropriate bits of the TARGET COMMAND
1297 * REGISTER match the STATUS REGISTER
1298 */
1299
Finn Thain0d2cf862016-01-03 16:06:11 +11001300 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001303 /*
1304 * Wait for assertion of REQ, after which the phase bits will be
1305 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
1307
Finn Thain686f3992016-01-03 16:05:26 +11001308 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Finn Thainb7465452016-01-03 16:06:05 +11001311 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001314 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001315 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1316 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
1318 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* Do actual transfer from SCSI bus to / from memory */
1321 if (!(p & SR_IO))
1322 NCR5380_write(OUTPUT_DATA_REG, *d);
1323 else
1324 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1325
1326 ++d;
1327
Finn Thainaff0cf92016-01-03 16:06:09 +11001328 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * The SCSI standard suggests that in MSGOUT phase, the initiator
1330 * should drop ATN on the last byte of the message phase
1331 * after REQ has been asserted for the handshake but before
1332 * the initiator raises ACK.
1333 */
1334
1335 if (!(p & SR_IO)) {
1336 if (!((p & SR_MSG) && c > 1)) {
1337 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1338 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001339 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1340 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001342 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1343 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001345 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1346 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 } else {
1349 NCR5380_dprint(NDEBUG_PIO, instance);
1350 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1351 }
1352
Finn Thaina2edc4a2016-01-03 16:05:27 +11001353 if (NCR5380_poll_politely(instance,
1354 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1355 break;
1356
Finn Thainb7465452016-01-03 16:06:05 +11001357 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001360 * We have several special cases to consider during REQ/ACK handshaking :
1361 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001362 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001364 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001365 * message. We must exit with ACK asserted, so that the calling
1366 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
1368 * 3. ACK and ATN are clear and the target may proceed as normal.
1369 */
1370 if (!(p == PHASE_MSGIN && c == 1)) {
1371 if (p == PHASE_MSGOUT && c > 1)
1372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1373 else
1374 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1375 }
1376 } while (--c);
1377
Finn Thainb7465452016-01-03 16:06:05 +11001378 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 *count = c;
1381 *data = d;
1382 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001383 /* The phase read from the bus is valid if either REQ is (already)
1384 * asserted or if ACK hasn't been released yet. The latter applies if
1385 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1386 */
1387 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 *phase = tmp & PHASE_MASK;
1389 else
1390 *phase = PHASE_UNKNOWN;
1391
1392 if (!c || (*phase == p))
1393 return 0;
1394 else
1395 return -1;
1396}
1397
1398/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001399 * do_reset - issue a reset command
1400 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001402 * Issue a reset sequence to the NCR5380 and try and get the bus
1403 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001405 * This clears the reset interrupt flag because there may be no handler for
1406 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1407 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001409
Finn Thain54d8fe42016-01-03 16:05:06 +11001410static void do_reset(struct Scsi_Host *instance)
1411{
Finn Thain636b1ec2016-01-03 16:05:10 +11001412 unsigned long flags;
1413
1414 local_irq_save(flags);
1415 NCR5380_write(TARGET_COMMAND_REG,
1416 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001418 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001420 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1421 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Finn Thain80d3eb62016-01-03 16:05:34 +11001424/**
1425 * do_abort - abort the currently established nexus by going to
1426 * MESSAGE OUT phase and sending an ABORT message.
1427 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001429 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
1431
Finn Thain54d8fe42016-01-03 16:05:06 +11001432static int do_abort(struct Scsi_Host *instance)
1433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 unsigned char *msgptr, phase, tmp;
1435 int len;
1436 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 /* Request message out phase */
1439 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1440
Finn Thainaff0cf92016-01-03 16:06:09 +11001441 /*
1442 * Wait for the target to indicate a valid phase by asserting
1443 * REQ. Once this happens, we'll have either a MSGOUT phase
1444 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001446 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 * We really don't care what value was on the bus or what value
1448 * the target sees, so we just handshake.
1449 */
1450
Finn Thain80d3eb62016-01-03 16:05:34 +11001451 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001452 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001453 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Finn Thainf35d3472016-01-03 16:05:33 +11001455 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1458
Finn Thainf35d3472016-01-03 16:05:33 +11001459 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001460 NCR5380_write(INITIATOR_COMMAND_REG,
1461 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001462 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001463 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001464 goto timeout;
1465 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 tmp = ABORT;
1469 msgptr = &tmp;
1470 len = 1;
1471 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001472 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 /*
1475 * If we got here, and the command completed successfully,
1476 * we're about to go into bus free state.
1477 */
1478
1479 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001480
1481timeout:
1482 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1483 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Finn Thainaff0cf92016-01-03 16:06:09 +11001486/*
1487 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001488 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
1490 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001491 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001493 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001494 * what phase is expected, *count - pointer to number of
1495 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001496 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001498 * maximum number of bytes, 0 if all bytes or transferred or exit
1499 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001501 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
1503
1504
Finn Thain0d2cf862016-01-03 16:06:11 +11001505static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1506 unsigned char *phase, int *count,
1507 unsigned char **data)
1508{
1509 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 register int c = *count;
1511 register unsigned char p = *phase;
1512 register unsigned char *d = *data;
1513 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001514 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1517 *phase = tmp;
1518 return -1;
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Finn Thain8053b0e2016-03-23 21:10:19 +11001521 hostdata->connected->SCp.phase = p;
1522
1523 if (p & SR_IO) {
1524 if (hostdata->read_overruns)
1525 c -= hostdata->read_overruns;
1526 else if (hostdata->flags & FLAG_DMA_FIXUP)
1527 --c;
1528 }
1529
1530 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1531 (p & SR_IO) ? "receive" : "send", c, d);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001534 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1535 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Finn Thain8053b0e2016-03-23 21:10:19 +11001537 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1538 /* On the Medusa, it is a must to initialize the DMA before
1539 * starting the NCR. This is also the cleaner way for the TT.
1540 */
1541 if (p & SR_IO)
1542 result = NCR5380_dma_recv_setup(instance, d, c);
1543 else
1544 result = NCR5380_dma_send_setup(instance, d, c);
1545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Finn Thainaff0cf92016-01-03 16:06:09 +11001547 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001548 * On the PAS16 at least I/O recovery delays are not needed here.
1549 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
1551
1552 if (p & SR_IO) {
Finn Thaine5d55d12016-03-23 21:10:16 +11001553 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1555 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001556 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001558 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001560 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562
Finn Thain8053b0e2016-03-23 21:10:19 +11001563 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1564 /* On the Falcon, the DMA setup must be done after the last
1565 * NCR access, else the DMA setup gets trashed!
1566 */
1567 if (p & SR_IO)
1568 result = NCR5380_dma_recv_setup(instance, d, c);
1569 else
1570 result = NCR5380_dma_send_setup(instance, d, c);
1571 }
1572
1573 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1574 if (result < 0)
1575 return result;
1576
1577 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1578 if (result > 0) {
1579 hostdata->dma_len = result;
1580 return 0;
1581 }
1582
1583 /* The result is zero iff pseudo DMA send/receive was completed. */
1584 hostdata->dma_len = c;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/*
Finn Thaine4dec682016-03-23 21:10:12 +11001587 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001588 *
1589 * For DMA sends, we want to wait until the last byte has been
1590 * transferred out over the bus before we turn off DMA mode. Alas, there
1591 * seems to be no terribly good way of doing this on a 5380 under all
1592 * conditions. For non-scatter-gather operations, we can wait until REQ
1593 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1594 * are nastier, since the device will be expecting more data than we
1595 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1596 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1597 * why this signal was added to the newer chips) but on the older 538[01]
1598 * this signal does not exist. The workaround for this lack is a watchdog;
1599 * we bail out of the wait-loop after a modest amount of wait-time if
1600 * the usual exit conditions are not met. Not a terribly clean or
1601 * correct solution :-%
1602 *
1603 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1604 * If the chip is in DMA receive mode, it will respond to a target's
1605 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1606 * ACK, even if it has _already_ been notified by the DMA controller that
1607 * the current DMA transfer has completed! If the NCR5380 is then taken
1608 * out of DMA mode, this already-acknowledged byte is lost. This is
1609 * not a problem for "one DMA transfer per READ command", because
1610 * the situation will never arise... either all of the data is DMA'ed
1611 * properly, or the target switches to MESSAGE IN phase to signal a
1612 * disconnection (either operation bringing the DMA to a clean halt).
1613 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001614 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001615 * condition before taking the NCR5380 out of DMA mode. One or two extra
1616 * bytes are transferred via PIO as necessary to fill out the original
1617 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
1619
Finn Thain8053b0e2016-03-23 21:10:19 +11001620 if (hostdata->flags & FLAG_DMA_FIXUP) {
1621 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001623 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001624 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * the chip to latch the last byte, read it, and then disable
1626 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001627 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1629 * REQ is deasserted when ACK is asserted, and not reasserted
1630 * until ACK goes false. Since the NCR5380 won't lower ACK
1631 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001632 * the DMA port or we take the NCR5380 out of DMA mode, we
1633 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * byte.
1635 */
1636
Finn Thain55181be2016-01-03 16:05:42 +11001637 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1638 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001639 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001640 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Finn Thain55181be2016-01-03 16:05:42 +11001642 if (NCR5380_poll_politely(instance, STATUS_REG,
1643 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001644 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001645 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1646 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001647 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1648 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001650 * Wait for the last byte to be sent. If REQ is being asserted for
1651 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
Finn Thain55181be2016-01-03 16:05:42 +11001653 if (NCR5380_poll_politely2(instance,
1654 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1655 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001656 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001657 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001661
1662 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001663 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666/*
1667 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1668 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001669 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001670 * directs us to. Operates on the currently connected command,
1671 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 *
1673 * Inputs : instance, instance for which we are doing commands
1674 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001675 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001676 * modified if a command disconnects, *instance->connected will
1677 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001679 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001680 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
1682
Finn Thain0d2cf862016-01-03 16:06:11 +11001683static void NCR5380_information_transfer(struct Scsi_Host *instance)
1684{
Finn Thaine8a60142016-01-03 16:05:54 +11001685 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 unsigned char msgout = NOP;
1687 int sink = 0;
1688 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 unsigned char *data;
1691 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001692 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Finn Thain11d2f632016-01-03 16:05:51 +11001694 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001695 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 tmp = NCR5380_read(STATUS_REG);
1698 /* We only have a valid SCSI phase when REQ is asserted */
1699 if (tmp & SR_REQ) {
1700 phase = (tmp & PHASE_MASK);
1701 if (phase != old_phase) {
1702 old_phase = phase;
1703 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1704 }
1705 if (sink && (phase != PHASE_MSGOUT)) {
1706 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1707
Finn Thain3d07d222016-01-03 16:06:13 +11001708 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1709 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001710 while (NCR5380_read(STATUS_REG) & SR_REQ)
1711 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001712 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1713 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 sink = 0;
1715 continue;
1716 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 case PHASE_DATAOUT:
1720#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001721 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 sink = 1;
1723 do_abort(instance);
1724 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001725 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001726 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return;
1728#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001729 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001730 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * If there is no room left in the current buffer in the
1732 * scatter-gather list, move onto the next one.
1733 */
1734
1735 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1736 ++cmd->SCp.buffer;
1737 --cmd->SCp.buffers_residual;
1738 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001739 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001740 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1741 cmd->SCp.this_residual,
1742 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001746 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * PSEUDO-DMA for systems that are strictly PIO,
1748 * since we can let the hardware do the handshaking.
1749 *
1750 * For this to work, we need to know the transfersize
1751 * ahead of time, since the pseudo-DMA code will sit
1752 * in an unconditional loop.
1753 */
1754
Finn Thainff3d4572016-01-03 16:05:25 +11001755 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001756 if (!cmd->device->borken)
Finn Thainff3d4572016-01-03 16:05:25 +11001757 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Finn Thain438af512016-03-23 21:10:18 +11001759 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001761 if (NCR5380_transfer_dma(instance, &phase,
1762 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001764 * If the watchdog timer fires, all future
1765 * accesses to this device will use the
1766 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001768 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001769 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 sink = 1;
1772 do_abort(instance);
1773 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* XXX - need to source or sink data here, as appropriate */
Finn Thain8053b0e2016-03-23 21:10:19 +11001775 }
Finn Thainf825e402016-03-23 21:10:15 +11001776 } else {
Finn Thain16788472016-02-23 10:07:05 +11001777 /* Break up transfer into 3 ms chunks,
1778 * presuming 6 accesses per handshake.
1779 */
1780 transfersize = min((unsigned long)cmd->SCp.this_residual,
1781 hostdata->accesses_per_ms / 2);
1782 len = transfersize;
1783 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001784 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001785 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001786 }
Finn Thain16788472016-02-23 10:07:05 +11001787 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 case PHASE_MSGIN:
1789 len = 1;
1790 data = &tmp;
1791 NCR5380_transfer_pio(instance, &phase, &len, &data);
1792 cmd->SCp.Message = tmp;
1793
1794 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 case ABORT:
1796 case COMMAND_COMPLETE:
1797 /* Accept message by clearing ACK */
1798 sink = 1;
1799 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001800 dsprintk(NDEBUG_QUEUES, instance,
1801 "COMMAND COMPLETE %p target %d lun %llu\n",
1802 cmd, scmd_id(cmd), cmd->device->lun);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Finn Thainf27db8e2016-01-03 16:06:00 +11001806 cmd->result &= ~0xffff;
1807 cmd->result |= cmd->SCp.Status;
1808 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Finn Thainf27db8e2016-01-03 16:06:00 +11001810 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001811 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001812 else {
1813 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1814 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1815 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1816 cmd);
1817 list_add_tail(&ncmd->list,
1818 &hostdata->autosense);
1819 } else
1820 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822
Finn Thainaff0cf92016-01-03 16:06:09 +11001823 /*
1824 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * arbitration can resume.
1826 */
1827 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001828
1829 /* Enable reselect interrupts */
1830 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return;
1832 case MESSAGE_REJECT:
1833 /* Accept message by clearing ACK */
1834 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1835 switch (hostdata->last_message) {
1836 case HEAD_OF_QUEUE_TAG:
1837 case ORDERED_QUEUE_TAG:
1838 case SIMPLE_QUEUE_TAG:
1839 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001840 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 break;
1842 default:
1843 break;
1844 }
Finn Thain340b9612016-01-03 16:05:31 +11001845 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001846 case DISCONNECT:
1847 /* Accept message by clearing ACK */
1848 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1849 hostdata->connected = NULL;
1850 list_add(&ncmd->list, &hostdata->disconnected);
1851 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1852 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1853 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001854
Finn Thain0d2cf862016-01-03 16:06:11 +11001855 /*
1856 * Restore phase bits to 0 so an interrupted selection,
1857 * arbitration can resume.
1858 */
1859 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Finn Thain0d2cf862016-01-03 16:06:11 +11001861 /* Enable reselect interrupts */
1862 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1863 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001864 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001866 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * ignore SAVE/RESTORE pointers calls.
1868 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001869 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001871 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * compatible.
1873 */
1874 case SAVE_POINTERS:
1875 case RESTORE_POINTERS:
1876 /* Accept message by clearing ACK */
1877 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1878 break;
1879 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001880 /*
1881 * Start the message buffer with the EXTENDED_MESSAGE
1882 * byte, since spi_print_msg() wants the whole thing.
1883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 extended_msg[0] = EXTENDED_MESSAGE;
1885 /* Accept first byte by clearing ACK */
1886 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001887
1888 spin_unlock_irq(&hostdata->lock);
1889
Finn Thainb7465452016-01-03 16:06:05 +11001890 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 len = 2;
1893 data = extended_msg + 1;
1894 phase = PHASE_MSGIN;
1895 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001896 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1897 (int)extended_msg[1],
1898 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Finn Thaine0783ed2016-01-03 16:05:45 +11001900 if (!len && extended_msg[1] > 0 &&
1901 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* Accept third byte by clearing ACK */
1903 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1904 len = extended_msg[1] - 1;
1905 data = extended_msg + 3;
1906 phase = PHASE_MSGIN;
1907
1908 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001909 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1910 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 switch (extended_msg[2]) {
1913 case EXTENDED_SDTR:
1914 case EXTENDED_WDTR:
1915 case EXTENDED_MODIFY_DATA_POINTER:
1916 case EXTENDED_EXTENDED_IDENTIFY:
1917 tmp = 0;
1918 }
1919 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001920 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 tmp = 0;
1922 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001923 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1924 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 tmp = 0;
1926 }
Finn Thain11d2f632016-01-03 16:05:51 +11001927
1928 spin_lock_irq(&hostdata->lock);
1929 if (!hostdata->connected)
1930 return;
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /* Fall through to reject message */
1933
Finn Thainaff0cf92016-01-03 16:06:09 +11001934 /*
1935 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * reject it.
1937 */
1938 default:
1939 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001940 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001941 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 printk("\n");
1943 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001944 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001945 "rejecting unknown message %02x\n",
1946 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 else
Jeff Garzik017560f2005-10-24 18:04:36 -04001948 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001949 "rejecting unknown extended message code %02x, length %d\n",
1950 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 msgout = MESSAGE_REJECT;
1953 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1954 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001955 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
1957 case PHASE_MSGOUT:
1958 len = 1;
1959 data = &msgout;
1960 hostdata->last_message = msgout;
1961 NCR5380_transfer_pio(instance, &phase, &len, &data);
1962 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 hostdata->connected = NULL;
1964 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001965 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1967 return;
1968 }
1969 msgout = NOP;
1970 break;
1971 case PHASE_CMDOUT:
1972 len = cmd->cmd_len;
1973 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001974 /*
1975 * XXX for performance reasons, on machines with a
1976 * PSEUDO-DMA architecture we should probably
1977 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
1979 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
1981 case PHASE_STATIN:
1982 len = 1;
1983 data = &tmp;
1984 NCR5380_transfer_pio(instance, &phase, &len, &data);
1985 cmd->SCp.Status = tmp;
1986 break;
1987 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001988 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001989 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001990 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001991 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001992 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11001993 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001994 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
Finn Thain11d2f632016-01-03 16:05:51 +11001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999/*
2000 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2001 *
Finn Thainaff0cf92016-01-03 16:06:09 +11002002 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002003 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2004 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11002005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
2008
Finn Thain0d2cf862016-01-03 16:06:11 +11002009static void NCR5380_reselect(struct Scsi_Host *instance)
2010{
Finn Thaine8a60142016-01-03 16:05:54 +11002011 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 unsigned char target_mask;
2013 unsigned char lun, phase;
2014 int len;
2015 unsigned char msg[3];
2016 unsigned char *data;
Finn Thain32b26a12016-01-03 16:05:58 +11002017 struct NCR5380_cmd *ncmd;
2018 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /*
2021 * Disable arbitration, etc. since the host adapter obviously
2022 * lost, and tell an interrupted NCR5380_select() to restart.
2023 */
2024
2025 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11002028
2029 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Finn Thainaff0cf92016-01-03 16:06:09 +11002031 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 * At this point, we have detected that our SCSI ID is on the bus,
2033 * SEL is true and BSY was false for at least one bus settle delay
2034 * (400 ns).
2035 *
2036 * We must assert BSY ourselves, until the target drops the SEL
2037 * signal.
2038 */
2039
2040 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002041 if (NCR5380_poll_politely(instance,
2042 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2043 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2044 return;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2047
2048 /*
2049 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
2051
Finn Thain1cc160e2016-01-03 16:05:32 +11002052 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002053 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2054 do_abort(instance);
2055 return;
2056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 len = 1;
2059 data = msg;
2060 phase = PHASE_MSGIN;
2061 NCR5380_transfer_pio(instance, &phase, &len, &data);
2062
Finn Thain72064a72016-01-03 16:05:44 +11002063 if (len) {
2064 do_abort(instance);
2065 return;
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002069 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002070 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002071 printk("\n");
2072 do_abort(instance);
2073 return;
2074 }
2075 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Finn Thain72064a72016-01-03 16:05:44 +11002077 /*
2078 * We need to add code for SCSI-II to track which devices have
2079 * I_T_L_Q nexuses established, and which have simple I_T_L
2080 * nexuses so we can chose to do additional data transfer.
2081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Finn Thain72064a72016-01-03 16:05:44 +11002083 /*
2084 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2085 * just reestablished, and remove it from the disconnected queue.
2086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Finn Thain32b26a12016-01-03 16:05:58 +11002088 tmp = NULL;
2089 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2090 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2091
2092 if (target_mask == (1 << scmd_id(cmd)) &&
2093 lun == (u8)cmd->device->lun) {
2094 list_del(&ncmd->list);
2095 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002099
2100 if (tmp) {
2101 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2102 "reselect: removed %p from disconnected queue\n", tmp);
2103 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002104 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2105 target_mask, lun);
2106 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002107 * Since we have an established nexus that we can't do anything
2108 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Finn Thain72064a72016-01-03 16:05:44 +11002113
2114 /* Accept message by clearing ACK */
2115 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2116
2117 hostdata->connected = tmp;
Finn Thainb7465452016-01-03 16:06:05 +11002118 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2119 scmd_id(tmp), tmp->device->lun, tmp->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Finn Thain8b00c3d2016-01-03 16:06:01 +11002122/**
2123 * list_find_cmd - test for presence of a command in a linked list
2124 * @haystack: list of commands
2125 * @needle: command to search for
2126 */
2127
2128static bool list_find_cmd(struct list_head *haystack,
2129 struct scsi_cmnd *needle)
2130{
2131 struct NCR5380_cmd *ncmd;
2132
2133 list_for_each_entry(ncmd, haystack, list)
2134 if (NCR5380_to_scmd(ncmd) == needle)
2135 return true;
2136 return false;
2137}
2138
2139/**
2140 * list_remove_cmd - remove a command from linked list
2141 * @haystack: list of commands
2142 * @needle: command to remove
2143 */
2144
2145static bool list_del_cmd(struct list_head *haystack,
2146 struct scsi_cmnd *needle)
2147{
2148 if (list_find_cmd(haystack, needle)) {
2149 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2150
2151 list_del(&ncmd->list);
2152 return true;
2153 }
2154 return false;
2155}
2156
2157/**
2158 * NCR5380_abort - scsi host eh_abort_handler() method
2159 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002161 * Try to abort a given command by removing it from queues and/or sending
2162 * the target an abort message. This may not succeed in causing a target
2163 * to abort the command. Nonetheless, the low-level driver must forget about
2164 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002166 * The normal path taken by a command is as follows. For EH we trace this
2167 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002169 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2170 * [disconnected -> connected ->]...
2171 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002173 * If cmd was not found at all then presumably it has already been completed,
2174 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002175 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002176 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002177 * We have no option but to forget the aborted command (even if it still
2178 * lacks sense data). The mid-layer may re-issue a command that is in error
2179 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2180 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002181 *
2182 * The lock protects driver data structures, but EH handlers also use it
2183 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
2185
Finn Thain710ddd02014-11-12 16:12:02 +11002186static int NCR5380_abort(struct scsi_cmnd *cmd)
2187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002189 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002190 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002191 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002192
Finn Thain11d2f632016-01-03 16:05:51 +11002193 spin_lock_irqsave(&hostdata->lock, flags);
2194
Finn Thain32b26a12016-01-03 16:05:58 +11002195#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002196 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002197#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002198 NCR5380_dprint(NDEBUG_ANY, instance);
2199 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Finn Thain8b00c3d2016-01-03 16:06:01 +11002201 if (list_del_cmd(&hostdata->unissued, cmd)) {
2202 dsprintk(NDEBUG_ABORT, instance,
2203 "abort: removed %p from issue queue\n", cmd);
2204 cmd->result = DID_ABORT << 16;
2205 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002206 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002207 }
2208
Finn Thain707d62b2016-01-03 16:06:02 +11002209 if (hostdata->selecting == cmd) {
2210 dsprintk(NDEBUG_ABORT, instance,
2211 "abort: cmd %p == selecting\n", cmd);
2212 hostdata->selecting = NULL;
2213 cmd->result = DID_ABORT << 16;
2214 complete_cmd(instance, cmd);
2215 goto out;
2216 }
2217
Finn Thain8b00c3d2016-01-03 16:06:01 +11002218 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2219 dsprintk(NDEBUG_ABORT, instance,
2220 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002221 /* Can't call NCR5380_select() and send ABORT because that
2222 * means releasing the lock. Need a bus reset.
2223 */
Finn Thaindc183962016-02-23 10:07:07 +11002224 set_host_byte(cmd, DID_ERROR);
2225 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002226 result = FAILED;
2227 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002228 }
2229
2230 if (hostdata->connected == cmd) {
2231 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2232 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002233 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002234 if (do_abort(instance)) {
2235 set_host_byte(cmd, DID_ERROR);
2236 complete_cmd(instance, cmd);
2237 result = FAILED;
2238 goto out;
2239 }
2240 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002241 complete_cmd(instance, cmd);
2242 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002243 }
2244
Finn Thaindc183962016-02-23 10:07:07 +11002245 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002246 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002247 "abort: removed %p from sense queue\n", cmd);
2248 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002249 complete_cmd(instance, cmd);
2250 }
2251
2252out:
2253 if (result == FAILED)
2254 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2255 else
2256 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2257
2258 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002259 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002260
Finn Thain8b00c3d2016-01-03 16:06:01 +11002261 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264
Finn Thain3be1b3e2016-01-03 16:05:12 +11002265/**
2266 * NCR5380_bus_reset - reset the SCSI bus
2267 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002269 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
2271
Finn Thain710ddd02014-11-12 16:12:02 +11002272static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002273{
2274 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002275 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002276 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002277 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002278 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Finn Thain11d2f632016-01-03 16:05:51 +11002280 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002281
2282#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002283 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002284#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002285 NCR5380_dprint(NDEBUG_ANY, instance);
2286 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002287
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002288 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002289
Finn Thain62717f52016-01-03 16:06:03 +11002290 /* reset NCR registers */
2291 NCR5380_write(MODE_REG, MR_BASE);
2292 NCR5380_write(TARGET_COMMAND_REG, 0);
2293 NCR5380_write(SELECT_ENABLE_REG, 0);
2294
2295 /* After the reset, there are no more connected or disconnected commands
2296 * and no busy units; so clear the low-level status here to avoid
2297 * conflicts when the mid-level code tries to wake up the affected
2298 * commands!
2299 */
2300
Finn Thain1884c282016-02-23 10:07:04 +11002301 if (list_del_cmd(&hostdata->unissued, cmd)) {
2302 cmd->result = DID_RESET << 16;
2303 cmd->scsi_done(cmd);
2304 }
2305
2306 if (hostdata->selecting) {
2307 hostdata->selecting->result = DID_RESET << 16;
2308 complete_cmd(instance, hostdata->selecting);
2309 hostdata->selecting = NULL;
2310 }
Finn Thain62717f52016-01-03 16:06:03 +11002311
2312 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2313 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2314
2315 set_host_byte(cmd, DID_RESET);
2316 cmd->scsi_done(cmd);
2317 }
Finn Thain1884c282016-02-23 10:07:04 +11002318 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002319
2320 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2321 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2322
2323 set_host_byte(cmd, DID_RESET);
2324 cmd->scsi_done(cmd);
2325 }
Finn Thain1884c282016-02-23 10:07:04 +11002326 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002327
2328 if (hostdata->connected) {
2329 set_host_byte(hostdata->connected, DID_RESET);
2330 complete_cmd(instance, hostdata->connected);
2331 hostdata->connected = NULL;
2332 }
2333
Finn Thain62717f52016-01-03 16:06:03 +11002334 for (i = 0; i < 8; ++i)
2335 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002336 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002337
2338 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain11d2f632016-01-03 16:05:51 +11002339 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return SUCCESS;
2342}