blob: 518d10180a315fce3c414cc9cea233010522e7b1 [file] [log] [blame]
Finn Thainaff0cf92016-01-03 16:06:09 +11001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * NCR 5380 generic driver routines. These should make it *trivial*
Finn Thain594d4ba2016-01-03 16:06:10 +11003 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Finn Thain594d4ba2016-01-03 16:06:10 +11006 * Note that these routines also work with NR53c400 family chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Copyright 1993, Drew Eckhardt
Finn Thain594d4ba2016-01-03 16:06:10 +11009 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Finn Thainaff0cf92016-01-03 16:06:09 +110014 * For more information, please consult
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * NCR 5380 Family
17 * SCSI Protocol Controller
18 * Databook
19 *
20 * NCR Microelectronics
21 * 1635 Aeroplaza Drive
22 * Colorado Springs, CO 80916
23 * 1+ (719) 578-3400
24 * 1+ (800) 334-5454
25 */
26
27/*
Finn Thainc16df322016-01-03 16:06:08 +110028 * With contributions from Ray Van Tassle, Ingmar Baumgart,
29 * Ronald van Cuijlenborg, Alan Cox and others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Finn Thain52d3e562016-03-23 21:10:20 +110032/* Ported to Atari by Roman Hodek and others. */
33
Finn Thaine9db3192016-03-23 21:10:21 +110034/* Adapted for the Sun 3 by Sam Creasey. */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Design
38 *
Finn Thainaff0cf92016-01-03 16:06:09 +110039 * This is a generic 5380 driver. To use it on a different platform,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * one simply writes appropriate system specific macros (ie, data
Finn Thainaff0cf92016-01-03 16:06:09 +110041 * transfer - some PC's will use the I/O bus, 68K's must use
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * memory mapped) and drops this file in their 'C' wrapper.
43 *
Finn Thainaff0cf92016-01-03 16:06:09 +110044 * As far as command queueing, two queues are maintained for
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * each 5380 in the system - commands that haven't been issued yet,
Finn Thainaff0cf92016-01-03 16:06:09 +110046 * and commands that are currently executing. This means that an
47 * unlimited number of commands may be queued, letting
48 * more commands propagate from the higher driver levels giving higher
49 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
50 * allowing multiple commands to propagate all the way to a SCSI-II device
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * while a command is already executing.
52 *
53 *
Finn Thainaff0cf92016-01-03 16:06:09 +110054 * Issues specific to the NCR5380 :
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
Finn Thainaff0cf92016-01-03 16:06:09 +110056 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
57 * piece of hardware that requires you to sit in a loop polling for
58 * the REQ signal as long as you are connected. Some devices are
59 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
Finn Thain686f3992016-01-03 16:05:26 +110060 * while doing long seek operations. [...] These
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * broken devices are the exception rather than the rule and I'd rather
62 * spend my time optimizing for the normal case.
63 *
64 * Architecture :
65 *
66 * At the heart of the design is a coroutine, NCR5380_main,
67 * which is started from a workqueue for each NCR5380 host in the
68 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
69 * removing the commands from the issue queue and calling
Finn Thainaff0cf92016-01-03 16:06:09 +110070 * NCR5380_select() if a nexus is not established.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 *
72 * Once a nexus is established, the NCR5380_information_transfer()
73 * phase goes through the various phases as instructed by the target.
74 * if the target goes into MSG IN and sends a DISCONNECT message,
75 * the command structure is placed into the per instance disconnected
Finn Thainaff0cf92016-01-03 16:06:09 +110076 * queue, and NCR5380_main tries to find more work. If the target is
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * idle for too long, the system will try to sleep.
78 *
79 * If a command has disconnected, eventually an interrupt will trigger,
80 * calling NCR5380_intr() which will in turn call NCR5380_reselect
81 * to reestablish a nexus. This will run main if necessary.
82 *
Finn Thainaff0cf92016-01-03 16:06:09 +110083 * On command termination, the done function will be called as
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * appropriate.
85 *
Finn Thainaff0cf92016-01-03 16:06:09 +110086 * SCSI pointers are maintained in the SCp field of SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * structures, being initialized after the command is connected
88 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
89 * Note that in violation of the standard, an implicit SAVE POINTERS operation
90 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
91 */
92
93/*
94 * Using this file :
95 * This file a skeleton Linux SCSI driver for the NCR 5380 series
Finn Thainaff0cf92016-01-03 16:06:09 +110096 * of chips. To use it, you write an architecture specific functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * and macros and include this file in your driver.
98 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * NCR5380_read(register) - read from the specified register
102 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100103 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100105 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100106 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
108 * Either real DMA *or* pseudo DMA may be implemented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Finn Thain4a98f892016-10-10 00:46:53 -0400110 * NCR5380_dma_xfer_len - determine size of DMA/PDMA transfer
111 * NCR5380_dma_send_setup - execute DMA/PDMA from memory to 5380
112 * NCR5380_dma_recv_setup - execute DMA/PDMA from 5380 to memory
113 * NCR5380_dma_residual - residual byte count
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * The generic driver is initialized by calling NCR5380_init(instance),
Ondrej Zary906e4a3c2016-12-05 01:07:20 -0500116 * after setting the appropriate host specific fields and ID.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
Finn Thaine5d55d12016-03-23 21:10:16 +1100119#ifndef NCR5380_io_delay
120#define NCR5380_io_delay(x)
121#endif
122
Finn Thain52d3e562016-03-23 21:10:20 +1100123#ifndef NCR5380_acquire_dma_irq
124#define NCR5380_acquire_dma_irq(x) (1)
125#endif
126
127#ifndef NCR5380_release_dma_irq
128#define NCR5380_release_dma_irq(x)
129#endif
130
Finn Thain54d8fe42016-01-03 16:05:06 +1100131static int do_abort(struct Scsi_Host *);
132static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Finn Thainc16df322016-01-03 16:06:08 +1100134/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100135 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100136 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100138 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140
Finn Thain710ddd02014-11-12 16:12:02 +1100141static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Finn Thainaff0cf92016-01-03 16:06:09 +1100143 /*
144 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * various queues are valid.
146 */
147
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200148 if (scsi_bufflen(cmd)) {
149 cmd->SCp.buffer = scsi_sglist(cmd);
150 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200151 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 cmd->SCp.this_residual = cmd->SCp.buffer->length;
153 } else {
154 cmd->SCp.buffer = NULL;
155 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200156 cmd->SCp.ptr = NULL;
157 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100159
160 cmd->SCp.Status = 0;
161 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164/**
Finn Thainb32ade12016-01-03 16:05:41 +1100165 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thaind5d37a02016-10-10 00:46:53 -0400166 * @hostdata: host private data
Finn Thainb32ade12016-01-03 16:05:41 +1100167 * @reg1: 5380 register to poll
168 * @bit1: Bitmask to check
169 * @val1: Expected value
170 * @reg2: Second 5380 register to poll
171 * @bit2: Second bitmask to check
172 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100173 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Finn Thain2f854b82016-01-03 16:05:22 +1100175 * Polls the chip in a reasonably efficient manner waiting for an
176 * event to occur. After a short quick poll we begin to yield the CPU
177 * (if possible). In irq contexts the time-out is arbitrarily limited.
178 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
Finn Thainb32ade12016-01-03 16:05:41 +1100180 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Finn Thaind5d37a02016-10-10 00:46:53 -0400183static int NCR5380_poll_politely2(struct NCR5380_hostdata *hostdata,
Finn Thain61e1ce52016-10-10 00:46:53 -0400184 unsigned int reg1, u8 bit1, u8 val1,
185 unsigned int reg2, u8 bit2, u8 val2,
186 unsigned long wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100187{
Finn Thaind4408dd2016-10-10 00:46:52 -0400188 unsigned long n = hostdata->poll_loops;
Finn Thain2f854b82016-01-03 16:05:22 +1100189 unsigned long deadline = jiffies + wait;
Finn Thain2f854b82016-01-03 16:05:22 +1100190
Finn Thain2f854b82016-01-03 16:05:22 +1100191 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100192 if ((NCR5380_read(reg1) & bit1) == val1)
193 return 0;
194 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100197 } while (n--);
198
199 if (irqs_disabled() || in_interrupt())
200 return -ETIMEDOUT;
201
202 /* Repeatedly sleep for 1 ms until deadline */
203 while (time_is_after_jiffies(deadline)) {
204 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100205 if ((NCR5380_read(reg1) & bit1) == val1)
206 return 0;
207 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Finn Thain2f854b82016-01-03 16:05:22 +1100210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ETIMEDOUT;
212}
213
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100214#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static struct {
216 unsigned char mask;
217 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100218} signals[] = {
219 {SR_DBP, "PARITY"},
220 {SR_RST, "RST"},
221 {SR_BSY, "BSY"},
222 {SR_REQ, "REQ"},
223 {SR_MSG, "MSG"},
224 {SR_CD, "CD"},
225 {SR_IO, "IO"},
226 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100228},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229basrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100230 {BASR_END_DMA_TRANSFER, "END OF DMA"},
231 {BASR_DRQ, "DRQ"},
232 {BASR_PARITY_ERROR, "PARITY ERROR"},
233 {BASR_IRQ, "IRQ"},
234 {BASR_PHASE_MATCH, "PHASE MATCH"},
235 {BASR_BUSY_ERROR, "BUSY ERROR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100236 {BASR_ATN, "ATN"},
237 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100239},
240icrs[] = {
241 {ICR_ASSERT_RST, "ASSERT RST"},
Finn Thain12866b92016-03-23 21:10:25 +1100242 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
243 {ICR_ARBITRATION_LOST, "LOST ARB."},
Finn Thainaff0cf92016-01-03 16:06:09 +1100244 {ICR_ASSERT_ACK, "ASSERT ACK"},
245 {ICR_ASSERT_BSY, "ASSERT BSY"},
246 {ICR_ASSERT_SEL, "ASSERT SEL"},
247 {ICR_ASSERT_ATN, "ASSERT ATN"},
248 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100250},
251mrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100252 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
253 {MR_TARGET, "TARGET"},
254 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
255 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
256 {MR_ENABLE_EOP_INTR, "EOP INTR"},
257 {MR_MONITOR_BSY, "MONITOR BSY"},
258 {MR_DMA_MODE, "DMA MODE"},
259 {MR_ARBITRATE, "ARBITRATE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 {0, NULL}
261};
262
263/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100264 * NCR5380_print - print scsi bus signals
265 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100267 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269
270static void NCR5380_print(struct Scsi_Host *instance)
271{
Finn Thain61e1ce52016-10-10 00:46:53 -0400272 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
276 status = NCR5380_read(STATUS_REG);
277 mr = NCR5380_read(MODE_REG);
278 icr = NCR5380_read(INITIATOR_COMMAND_REG);
279 basr = NCR5380_read(BUS_AND_STATUS_REG);
280
Finn Thain12866b92016-03-23 21:10:25 +1100281 printk(KERN_DEBUG "SR = 0x%02x : ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 for (i = 0; signals[i].mask; ++i)
283 if (status & signals[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100284 printk(KERN_CONT "%s, ", signals[i].name);
285 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 for (i = 0; basrs[i].mask; ++i)
287 if (basr & basrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100288 printk(KERN_CONT "%s, ", basrs[i].name);
289 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 for (i = 0; icrs[i].mask; ++i)
291 if (icr & icrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100292 printk(KERN_CONT "%s, ", icrs[i].name);
293 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 for (i = 0; mrs[i].mask; ++i)
295 if (mr & mrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100296 printk(KERN_CONT "%s, ", mrs[i].name);
297 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Finn Thain0d2cf862016-01-03 16:06:11 +1100300static struct {
301 unsigned char value;
302 const char *name;
303} phases[] = {
304 {PHASE_DATAOUT, "DATAOUT"},
305 {PHASE_DATAIN, "DATAIN"},
306 {PHASE_CMDOUT, "CMDOUT"},
307 {PHASE_STATIN, "STATIN"},
308 {PHASE_MSGOUT, "MSGOUT"},
309 {PHASE_MSGIN, "MSGIN"},
310 {PHASE_UNKNOWN, "UNKNOWN"}
311};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Finn Thainc16df322016-01-03 16:06:08 +1100313/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100314 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100315 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100317 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319
320static void NCR5380_print_phase(struct Scsi_Host *instance)
321{
Finn Thain61e1ce52016-10-10 00:46:53 -0400322 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned char status;
324 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 status = NCR5380_read(STATUS_REG);
327 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100328 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100330 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
331 (phases[i].value != (status & PHASE_MASK)); ++i)
332 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100333 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335}
336#endif
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/**
Finn Thain09028462017-01-15 18:50:57 -0500339 * NCR5380_info - report driver and host information
Finn Thain594d4ba2016-01-03 16:06:10 +1100340 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100342 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344
Finn Thain8c325132014-11-12 16:11:58 +1100345static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Finn Thain8c325132014-11-12 16:11:58 +1100347 struct NCR5380_hostdata *hostdata = shost_priv(instance);
348
349 return hostdata->info;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100353 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100354 * @instance: adapter to configure
355 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100357 * Initializes *instance and corresponding 5380 chip,
358 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100360 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100361 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100363 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800366static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Finn Thaine8a60142016-01-03 16:05:54 +1100368 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100369 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100370 unsigned long deadline;
Finn Thaind4408dd2016-10-10 00:46:52 -0400371 unsigned long accesses_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Finn Thainae5e33a2016-03-23 21:10:23 +1100373 instance->max_lun = 7;
374
Finn Thain0d2cf862016-01-03 16:06:11 +1100375 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100377 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
379 if (i > hostdata->id_mask)
380 hostdata->id_higher_mask |= i;
381 for (i = 0; i < 8; ++i)
382 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100383 hostdata->dma_len = 0;
384
Finn Thain11d2f632016-01-03 16:05:51 +1100385 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100387 hostdata->sensing = NULL;
388 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100389 INIT_LIST_HEAD(&hostdata->unissued);
390 INIT_LIST_HEAD(&hostdata->disconnected);
391
Finn Thain55181be2016-01-03 16:05:42 +1100392 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100393
Finn Thain8d8601a2016-01-03 16:05:37 +1100394 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100395 hostdata->work_q = alloc_workqueue("ncr5380_%d",
396 WQ_UNBOUND | WQ_MEM_RECLAIM,
397 1, instance->host_no);
398 if (!hostdata->work_q)
399 return -ENOMEM;
400
Finn Thain09028462017-01-15 18:50:57 -0500401 snprintf(hostdata->info, sizeof(hostdata->info),
402 "%s, irq %d, io_port 0x%lx, base 0x%lx, can_queue %d, cmd_per_lun %d, sg_tablesize %d, this_id %d, flags { %s%s%s}",
403 instance->hostt->name, instance->irq, hostdata->io_port,
404 hostdata->base, instance->can_queue, instance->cmd_per_lun,
405 instance->sg_tablesize, instance->this_id,
406 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
407 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
408 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "");
Finn Thain8c325132014-11-12 16:11:58 +1100409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
411 NCR5380_write(MODE_REG, MR_BASE);
412 NCR5380_write(TARGET_COMMAND_REG, 0);
413 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100414
415 /* Calibrate register polling loop */
416 i = 0;
417 deadline = jiffies + 1;
418 do {
419 cpu_relax();
420 } while (time_is_after_jiffies(deadline));
421 deadline += msecs_to_jiffies(256);
422 do {
423 NCR5380_read(STATUS_REG);
424 ++i;
425 cpu_relax();
426 } while (time_is_after_jiffies(deadline));
Finn Thaind4408dd2016-10-10 00:46:52 -0400427 accesses_per_ms = i / 256;
428 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
Finn Thain2f854b82016-01-03 16:05:22 +1100429
Finn Thainb6488f92016-01-03 16:05:08 +1100430 return 0;
431}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Finn Thainb6488f92016-01-03 16:05:08 +1100433/**
434 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
435 * @instance: adapter to check
436 *
437 * If the system crashed, it may have crashed with a connected target and
438 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
439 * currently established nexus, which we know nothing about. Failing that
440 * do a bus reset.
441 *
442 * Note that a bus reset will cause the chip to assert IRQ.
443 *
444 * Returns 0 if successful, otherwise -ENXIO.
445 */
446
447static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
448{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100449 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100450 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
453 switch (pass) {
454 case 1:
455 case 3:
456 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100457 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
Finn Thaind5d37a02016-10-10 00:46:53 -0400458 NCR5380_poll_politely(hostdata,
Finn Thain636b1ec2016-01-03 16:05:10 +1100459 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100462 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 do_abort(instance);
464 break;
465 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100466 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100468 /* Wait after a reset; the SCSI standard calls for
469 * 250ms, we wait 500ms to be on the safe side.
470 * But some Toshiba CD-ROMs need ten times that.
471 */
472 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
473 msleep(2500);
474 else
475 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100478 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -ENXIO;
480 }
481 }
482 return 0;
483}
484
485/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100486 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100487 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100488 *
489 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
491
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800492static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Finn Thaine8a60142016-01-03 16:05:54 +1100494 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Finn Thain8d8601a2016-01-03 16:05:37 +1100496 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100497 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/**
Finn Thain677e0192016-01-03 16:05:59 +1100501 * complete_cmd - finish processing a command and return it to the SCSI ML
502 * @instance: the host instance
503 * @cmd: command to complete
504 */
505
506static void complete_cmd(struct Scsi_Host *instance,
507 struct scsi_cmnd *cmd)
508{
509 struct NCR5380_hostdata *hostdata = shost_priv(instance);
510
511 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
512
Finn Thainf27db8e2016-01-03 16:06:00 +1100513 if (hostdata->sensing == cmd) {
514 /* Autosense processing ends here */
515 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
516 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
517 set_host_byte(cmd, DID_ERROR);
518 } else
519 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
520 hostdata->sensing = NULL;
521 }
522
Finn Thain677e0192016-01-03 16:05:59 +1100523 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
524
525 cmd->scsi_done(cmd);
526}
527
528/**
Finn Thain1bb40582016-01-03 16:05:29 +1100529 * NCR5380_queue_command - queue a command
530 * @instance: the relevant SCSI adapter
531 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
Finn Thain1bb40582016-01-03 16:05:29 +1100533 * cmd is added to the per-instance issue queue, with minor
534 * twiddling done to the host specific fields of cmd. If the
535 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
537
Finn Thain1bb40582016-01-03 16:05:29 +1100538static int NCR5380_queue_command(struct Scsi_Host *instance,
539 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Finn Thain1bb40582016-01-03 16:05:29 +1100541 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100542 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100543 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545#if (NDEBUG & NDEBUG_NO_WRITE)
546 switch (cmd->cmnd[0]) {
547 case WRITE_6:
548 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100549 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100551 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100554#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 cmd->result = 0;
557
Finn Thain52d3e562016-03-23 21:10:20 +1100558 if (!NCR5380_acquire_dma_irq(instance))
559 return SCSI_MLQUEUE_HOST_BUSY;
560
Finn Thain11d2f632016-01-03 16:05:51 +1100561 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100562
Finn Thainaff0cf92016-01-03 16:06:09 +1100563 /*
564 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100566 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * sense data is only guaranteed to be valid while the condition exists.
568 */
569
Finn Thain32b26a12016-01-03 16:05:58 +1100570 if (cmd->cmnd[0] == REQUEST_SENSE)
571 list_add(&ncmd->list, &hostdata->unissued);
572 else
573 list_add_tail(&ncmd->list, &hostdata->unissued);
574
Finn Thain11d2f632016-01-03 16:05:51 +1100575 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100576
Finn Thaindbb6b352016-01-03 16:05:53 +1100577 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
578 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100581 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583}
584
Finn Thain52d3e562016-03-23 21:10:20 +1100585static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
586{
587 struct NCR5380_hostdata *hostdata = shost_priv(instance);
588
589 /* Caller does the locking needed to set & test these data atomically */
590 if (list_empty(&hostdata->disconnected) &&
591 list_empty(&hostdata->unissued) &&
592 list_empty(&hostdata->autosense) &&
593 !hostdata->connected &&
Finn Thain4ab2a782017-01-15 18:50:57 -0500594 !hostdata->selecting) {
Finn Thain52d3e562016-03-23 21:10:20 +1100595 NCR5380_release_dma_irq(instance);
Finn Thain4ab2a782017-01-15 18:50:57 -0500596 }
Finn Thain52d3e562016-03-23 21:10:20 +1100597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100600 * dequeue_next_cmd - dequeue a command for processing
601 * @instance: the scsi host instance
602 *
603 * Priority is given to commands on the autosense queue. These commands
604 * need autosense because of a CHECK CONDITION result.
605 *
606 * Returns a command pointer if a command is found for a target that is
607 * not already busy. Otherwise returns NULL.
608 */
609
610static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
611{
612 struct NCR5380_hostdata *hostdata = shost_priv(instance);
613 struct NCR5380_cmd *ncmd;
614 struct scsi_cmnd *cmd;
615
Finn Thain8d5dbec2016-02-23 10:07:09 +1100616 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100617 list_for_each_entry(ncmd, &hostdata->unissued, list) {
618 cmd = NCR5380_to_scmd(ncmd);
619 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
620 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
621
622 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
623 list_del(&ncmd->list);
624 dsprintk(NDEBUG_QUEUES, instance,
625 "dequeue: removed %p from issue queue\n", cmd);
626 return cmd;
627 }
628 }
629 } else {
630 /* Autosense processing begins here */
631 ncmd = list_first_entry(&hostdata->autosense,
632 struct NCR5380_cmd, list);
633 list_del(&ncmd->list);
634 cmd = NCR5380_to_scmd(ncmd);
635 dsprintk(NDEBUG_QUEUES, instance,
636 "dequeue: removed %p from autosense queue\n", cmd);
637 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
638 hostdata->sensing = cmd;
639 return cmd;
640 }
641 return NULL;
642}
643
644static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
645{
646 struct NCR5380_hostdata *hostdata = shost_priv(instance);
647 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
648
Finn Thain8d5dbec2016-02-23 10:07:09 +1100649 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100650 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
651 list_add(&ncmd->list, &hostdata->autosense);
652 hostdata->sensing = NULL;
653 } else
654 list_add(&ncmd->list, &hostdata->unissued);
655}
656
657/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100658 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100660 * NCR5380_main is a coroutine that runs as long as more work can
661 * be done on the NCR5380 host adapters in a system. Both
662 * NCR5380_queue_command() and NCR5380_intr() will try to start it
663 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
665
David Howellsc4028952006-11-22 14:57:56 +0000666static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
David Howellsc4028952006-11-22 14:57:56 +0000668 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100669 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100675
Finn Thain0a4e3612016-01-03 16:06:07 +1100676 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100677 while (!hostdata->connected && !hostdata->selecting) {
678 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
679
680 if (!cmd)
681 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100682
683 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100686 * Attempt to establish an I_T_L nexus here.
687 * On success, instance->hostdata->connected is set.
688 * On failure, we must add the command back to the
689 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100691 /*
692 * REQUEST SENSE commands are issued without tagged
693 * queueing, even on SCSI-II devices because the
694 * contingent allegiance condition exists for the
695 * entire unit.
696 */
Finn Thain32b26a12016-01-03 16:05:58 +1100697
Finn Thainccf6efd2016-02-23 10:07:08 +1100698 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100699 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100700 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100701 } else {
702 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
703 "main: select failed, returning %p to queue\n", cmd);
704 requeue_cmd(instance, cmd);
705 }
706 }
Finn Thaine4dec682016-03-23 21:10:12 +1100707 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100708 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100711 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100712 spin_unlock_irq(&hostdata->lock);
713 if (!done)
714 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Finn Thain8053b0e2016-03-23 21:10:19 +1100718/*
719 * NCR5380_dma_complete - finish DMA transfer
720 * @instance: the scsi host instance
721 *
722 * Called by the interrupt handler when DMA finishes or a phase
723 * mismatch occurs (which would end the DMA transfer).
724 */
725
726static void NCR5380_dma_complete(struct Scsi_Host *instance)
727{
728 struct NCR5380_hostdata *hostdata = shost_priv(instance);
729 int transferred;
730 unsigned char **data;
731 int *count;
732 int saved_data = 0, overrun = 0;
733 unsigned char p;
734
735 if (hostdata->read_overruns) {
736 p = hostdata->connected->SCp.phase;
737 if (p & SR_IO) {
738 udelay(10);
739 if ((NCR5380_read(BUS_AND_STATUS_REG) &
740 (BASR_PHASE_MATCH | BASR_ACK)) ==
741 (BASR_PHASE_MATCH | BASR_ACK)) {
742 saved_data = NCR5380_read(INPUT_DATA_REG);
743 overrun = 1;
744 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
745 }
746 }
747 }
748
Finn Thaine9db3192016-03-23 21:10:21 +1100749#ifdef CONFIG_SUN3
750 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
751 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
752 instance->host_no);
753 BUG();
754 }
755
756 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
757 (BASR_PHASE_MATCH | BASR_ACK)) {
758 pr_err("scsi%d: BASR %02x\n", instance->host_no,
759 NCR5380_read(BUS_AND_STATUS_REG));
760 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
761 instance->host_no);
762 BUG();
763 }
764#endif
765
Finn Thain8053b0e2016-03-23 21:10:19 +1100766 NCR5380_write(MODE_REG, MR_BASE);
767 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
768 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
769
Finn Thain4a98f892016-10-10 00:46:53 -0400770 transferred = hostdata->dma_len - NCR5380_dma_residual(hostdata);
Finn Thain8053b0e2016-03-23 21:10:19 +1100771 hostdata->dma_len = 0;
772
773 data = (unsigned char **)&hostdata->connected->SCp.ptr;
774 count = &hostdata->connected->SCp.this_residual;
775 *data += transferred;
776 *count -= transferred;
777
778 if (hostdata->read_overruns) {
779 int cnt, toPIO;
780
781 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
782 cnt = toPIO = hostdata->read_overruns;
783 if (overrun) {
784 dsprintk(NDEBUG_DMA, instance,
785 "Got an input overrun, using saved byte\n");
786 *(*data)++ = saved_data;
787 (*count)--;
788 cnt--;
789 toPIO--;
790 }
791 if (toPIO > 0) {
792 dsprintk(NDEBUG_DMA, instance,
793 "Doing %d byte PIO to 0x%p\n", cnt, *data);
794 NCR5380_transfer_pio(instance, &p, &cnt, data);
795 *count -= toPIO - cnt;
796 }
797 }
798 }
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/**
Finn Thaincd400822016-01-03 16:05:40 +1100802 * NCR5380_intr - generic NCR5380 irq handler
803 * @irq: interrupt number
804 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Finn Thaincd400822016-01-03 16:05:40 +1100806 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
807 * from the disconnected queue, and restarting NCR5380_main()
808 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Finn Thaincd400822016-01-03 16:05:40 +1100810 * The chip can assert IRQ in any of six different conditions. The IRQ flag
811 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
812 * Three of these six conditions are latched in the Bus and Status Register:
813 * - End of DMA (cleared by ending DMA Mode)
814 * - Parity error (cleared by reading RPIR)
815 * - Loss of BSY (cleared by reading RPIR)
816 * Two conditions have flag bits that are not latched:
817 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
818 * - Bus reset (non-maskable)
819 * The remaining condition has no flag bit at all:
820 * - Selection/reselection
821 *
822 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
823 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
824 * claimed that "the design of the [DP8490] interrupt logic ensures
825 * interrupts will not be lost (they can be on the DP5380)."
826 * The L5380/53C80 datasheet from LOGIC Devices has more details.
827 *
828 * Checking for bus reset by reading RST is futile because of interrupt
829 * latency, but a bus reset will reset chip logic. Checking for parity error
830 * is unnecessary because that interrupt is never enabled. A Loss of BSY
831 * condition will clear DMA Mode. We can tell when this occurs because the
832 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
834
Finn Thaina46865d2016-03-23 21:10:27 +1100835static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800837 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100838 struct NCR5380_hostdata *hostdata = shost_priv(instance);
839 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unsigned char basr;
841 unsigned long flags;
842
Finn Thain11d2f632016-01-03 16:05:51 +1100843 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Finn Thaincd400822016-01-03 16:05:40 +1100845 basr = NCR5380_read(BUS_AND_STATUS_REG);
846 if (basr & BASR_IRQ) {
847 unsigned char mr = NCR5380_read(MODE_REG);
848 unsigned char sr = NCR5380_read(STATUS_REG);
849
Finn Thainb7465452016-01-03 16:06:05 +1100850 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
851 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100852
Finn Thain8053b0e2016-03-23 21:10:19 +1100853 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
854 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
855 * We ack IRQ after clearing Mode Register. Workarounds
856 * for End of DMA errata need to happen in DMA Mode.
857 */
858
859 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
860
861 if (hostdata->connected) {
862 NCR5380_dma_complete(instance);
863 queue_work(hostdata->work_q, &hostdata->main_task);
864 } else {
865 NCR5380_write(MODE_REG, MR_BASE);
866 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
867 }
868 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100869 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
870 /* Probably reselected */
871 NCR5380_write(SELECT_ENABLE_REG, 0);
872 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
873
Finn Thainb7465452016-01-03 16:06:05 +1100874 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100875
876 if (!hostdata->connected) {
877 NCR5380_reselect(instance);
878 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Finn Thaincd400822016-01-03 16:05:40 +1100880 if (!hostdata->connected)
881 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
882 } else {
883 /* Probably Bus Reset */
884 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
885
Finn Thainb7465452016-01-03 16:06:05 +1100886 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100887#ifdef SUN3_SCSI_VME
888 dregs->csr |= CSR_DMA_ENABLE;
889#endif
Finn Thaincd400822016-01-03 16:05:40 +1100890 }
891 handled = 1;
892 } else {
Finn Thain9af9fec2016-10-10 00:46:53 -0400893 dsprintk(NDEBUG_INTR, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100894#ifdef SUN3_SCSI_VME
895 dregs->csr |= CSR_DMA_ENABLE;
896#endif
Finn Thaincd400822016-01-03 16:05:40 +1100897 }
898
Finn Thain11d2f632016-01-03 16:05:51 +1100899 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +1100900
901 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Finn Thainaff0cf92016-01-03 16:06:09 +1100904/*
Finn Thain710ddd02014-11-12 16:12:02 +1100905 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +1100906 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 *
908 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +1100909 * including ARBITRATION, SELECTION, and initial message out for
910 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100912 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +1100913 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +1100914 *
Finn Thain707d62b2016-01-03 16:06:02 +1100915 * Returns cmd if selection failed but should be retried,
916 * NULL if selection failed and should not be retried, or
917 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100919 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +1100920 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
921 * with registers as they should have been on entry - ie
922 * SELECT_ENABLE will be set appropriately, the NCR5380
923 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100925 * If successful : I_T_L or I_T_L_Q nexus will be established,
926 * instance->connected will be set to cmd.
927 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100929 * If failed (no target) : cmd->scsi_done() will be called, and the
930 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100932
Finn Thain707d62b2016-01-03 16:06:02 +1100933static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
934 struct scsi_cmnd *cmd)
Finn Thain4ab2a782017-01-15 18:50:57 -0500935 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Finn Thaine8a60142016-01-03 16:05:54 +1100937 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 unsigned char tmp[3], phase;
939 unsigned char *data;
940 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +1100944 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
945 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Finn Thain707d62b2016-01-03 16:06:02 +1100947 /*
948 * Arbitration and selection phases are slow and involve dropping the
949 * lock, so we have to watch out for EH. An exception handler may
950 * change 'selecting' to NULL. This function will then return NULL
951 * so that the caller will forget about 'cmd'. (During information
952 * transfer phases, EH may change 'connected' to NULL.)
953 */
954 hostdata->selecting = cmd;
955
Finn Thainaff0cf92016-01-03 16:06:09 +1100956 /*
957 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * data bus during SELECTION.
959 */
960
961 NCR5380_write(TARGET_COMMAND_REG, 0);
962
Finn Thainaff0cf92016-01-03 16:06:09 +1100963 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * Start arbitration.
965 */
966
967 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
968 NCR5380_write(MODE_REG, MR_ARBITRATE);
969
Finn Thain55500d92016-01-03 16:05:35 +1100970 /* The chip now waits for BUS FREE phase. Then after the 800 ns
971 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
973
Finn Thain11d2f632016-01-03 16:05:51 +1100974 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -0400975 err = NCR5380_poll_politely2(hostdata, MODE_REG, MR_ARBITRATE, 0,
Finn Thainb32ade12016-01-03 16:05:41 +1100976 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
977 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +1100978 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +1100979 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
980 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +1100981 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +1100982 }
Finn Thainccf6efd2016-02-23 10:07:08 +1100983 if (!hostdata->selecting) {
984 /* Command was aborted */
985 NCR5380_write(MODE_REG, MR_BASE);
986 goto out;
987 }
Finn Thainb32ade12016-01-03 16:05:41 +1100988 if (err < 0) {
989 NCR5380_write(MODE_REG, MR_BASE);
990 shost_printk(KERN_ERR, instance,
991 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +1100992 goto out;
Finn Thain55500d92016-01-03 16:05:35 +1100993 }
Finn Thain11d2f632016-01-03 16:05:51 +1100994 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +1100995
996 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 udelay(3);
998
999 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001000 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1001 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1002 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001004 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001005 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Finn Thaincf13b082016-01-03 16:05:18 +11001008
1009 /* After/during arbitration, BSY should be asserted.
1010 * IBM DPES-31080 Version S31Q works now
1011 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1012 */
1013 NCR5380_write(INITIATOR_COMMAND_REG,
1014 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Finn Thainaff0cf92016-01-03 16:06:09 +11001016 /*
1017 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * a minimum so we'll udelay ceil(1.2)
1019 */
1020
Finn Thain9c3f0e22016-01-03 16:05:11 +11001021 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1022 udelay(15);
1023 else
1024 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Finn Thain11d2f632016-01-03 16:05:51 +11001026 spin_lock_irq(&hostdata->lock);
1027
Finn Thain72064a72016-01-03 16:05:44 +11001028 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1029 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001030 goto out;
1031
1032 if (!hostdata->selecting) {
1033 NCR5380_write(MODE_REG, MR_BASE);
1034 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1035 goto out;
1036 }
Finn Thain72064a72016-01-03 16:05:44 +11001037
Finn Thainb7465452016-01-03 16:06:05 +11001038 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Finn Thainaff0cf92016-01-03 16:06:09 +11001040 /*
1041 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * the host and target ID's on the SCSI bus.
1043 */
1044
Finn Thain3d07d222016-01-03 16:06:13 +11001045 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Finn Thainaff0cf92016-01-03 16:06:09 +11001047 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * Raise ATN while SEL is true before BSY goes false from arbitration,
1049 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1050 * phase immediately after selection.
1051 */
1052
Finn Thain3d07d222016-01-03 16:06:13 +11001053 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1054 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 NCR5380_write(MODE_REG, MR_BASE);
1056
Finn Thainaff0cf92016-01-03 16:06:09 +11001057 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * Reselect interrupts must be turned off prior to the dropping of BSY,
1059 * otherwise we will trigger an interrupt.
1060 */
1061 NCR5380_write(SELECT_ENABLE_REG, 0);
1062
Finn Thain11d2f632016-01-03 16:05:51 +11001063 spin_unlock_irq(&hostdata->lock);
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001066 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * the BSY signal.
1068 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001069 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001072 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1073 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Finn Thainaff0cf92016-01-03 16:06:09 +11001075 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001077 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * appropriate propagation delay has expired, and we're confusing
1079 * a BSY signal from ourselves as the target's response to SELECTION.
1080 *
1081 * A small delay (the 'C++' frontend breaks the pipeline with an
1082 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001083 * tighter 'C' code breaks and requires this) solves the problem -
1084 * the 1 us delay is arbitrary, and only used because this delay will
1085 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * work there.
1087 *
1088 * wingel suggests that this could be due to failing to wait
1089 * one deskew delay.
1090 */
1091
1092 udelay(1);
1093
Finn Thainb7465452016-01-03 16:06:05 +11001094 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Finn Thainaff0cf92016-01-03 16:06:09 +11001096 /*
1097 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * selection.
1099 */
1100
Finn Thaind5d37a02016-10-10 00:46:53 -04001101 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_BSY, SR_BSY,
Finn Thainae753a32016-01-03 16:05:23 +11001102 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001105 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1107 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001108 if (!hostdata->connected)
1109 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001110 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001111 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Finn Thainae753a32016-01-03 16:05:23 +11001113
1114 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001115 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001116 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001117 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001118 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1119 if (hostdata->selecting) {
1120 cmd->result = DID_BAD_TARGET << 16;
1121 complete_cmd(instance, cmd);
1122 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1123 cmd = NULL;
1124 }
1125 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001126 }
1127
Finn Thainaff0cf92016-01-03 16:06:09 +11001128 /*
1129 * No less than two deskew delays after the initiator detects the
1130 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * change the DATA BUS. -wingel
1132 */
1133
1134 udelay(1);
1135
1136 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001139 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * was true but before BSY was false during selection, the information
1141 * transfer phase should be a MESSAGE OUT phase so that we can send the
1142 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
1144
1145 /* Wait for start of REQ/ACK handshake */
1146
Finn Thaind5d37a02016-10-10 00:46:53 -04001147 err = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001148 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001149 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001150 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1151 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001153 goto out;
1154 }
1155 if (!hostdata->selecting) {
1156 do_abort(instance);
1157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Finn Thainb7465452016-01-03 16:06:05 +11001160 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1161 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001162 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 data = tmp;
1166 phase = PHASE_MSGOUT;
1167 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001168 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001172 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Finn Thaine9db3192016-03-23 21:10:21 +11001174#ifdef SUN3_SCSI_VME
1175 dregs->csr |= CSR_INTR;
1176#endif
1177
Boaz Harrosh28424d32007-09-10 22:37:45 +03001178 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Finn Thain707d62b2016-01-03 16:06:02 +11001180 cmd = NULL;
1181
1182out:
1183 if (!hostdata->selecting)
1184 return NULL;
1185 hostdata->selecting = NULL;
1186 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Finn Thainaff0cf92016-01-03 16:06:09 +11001189/*
1190 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001191 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
1193 * Purpose : transfers data in given phase using polled I/O
1194 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001195 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001196 * what phase is expected, *count - pointer to number of
1197 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001198 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001200 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001201 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001203 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
1205 * XXX Note : handling for bus free may be useful.
1206 */
1207
1208/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001209 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * IS 100% reliable, and for the actual data transfer where speed
1211 * counts, we will always do a pseudo DMA or DMA transfer.
1212 */
1213
Finn Thain0d2cf862016-01-03 16:06:11 +11001214static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1215 unsigned char *phase, int *count,
1216 unsigned char **data)
1217{
Finn Thain61e1ce52016-10-10 00:46:53 -04001218 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 unsigned char p = *phase, tmp;
1220 int c = *count;
1221 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Finn Thainaff0cf92016-01-03 16:06:09 +11001223 /*
1224 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * phase specified in the appropriate bits of the TARGET COMMAND
1226 * REGISTER match the STATUS REGISTER
1227 */
1228
Finn Thain0d2cf862016-01-03 16:06:11 +11001229 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001232 /*
1233 * Wait for assertion of REQ, after which the phase bits will be
1234 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
1236
Finn Thaind5d37a02016-10-10 00:46:53 -04001237 if (NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Finn Thainb7465452016-01-03 16:06:05 +11001240 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001243 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001244 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1245 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
1247 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* Do actual transfer from SCSI bus to / from memory */
1250 if (!(p & SR_IO))
1251 NCR5380_write(OUTPUT_DATA_REG, *d);
1252 else
1253 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1254
1255 ++d;
1256
Finn Thainaff0cf92016-01-03 16:06:09 +11001257 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * The SCSI standard suggests that in MSGOUT phase, the initiator
1259 * should drop ATN on the last byte of the message phase
1260 * after REQ has been asserted for the handshake but before
1261 * the initiator raises ACK.
1262 */
1263
1264 if (!(p & SR_IO)) {
1265 if (!((p & SR_MSG) && c > 1)) {
1266 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1267 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001268 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1269 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001271 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1272 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001274 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1275 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 } else {
1278 NCR5380_dprint(NDEBUG_PIO, instance);
1279 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1280 }
1281
Finn Thaind5d37a02016-10-10 00:46:53 -04001282 if (NCR5380_poll_politely(hostdata,
Finn Thaina2edc4a2016-01-03 16:05:27 +11001283 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1284 break;
1285
Finn Thainb7465452016-01-03 16:06:05 +11001286 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001289 * We have several special cases to consider during REQ/ACK handshaking :
1290 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001291 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001293 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001294 * message. We must exit with ACK asserted, so that the calling
1295 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 *
1297 * 3. ACK and ATN are clear and the target may proceed as normal.
1298 */
1299 if (!(p == PHASE_MSGIN && c == 1)) {
1300 if (p == PHASE_MSGOUT && c > 1)
1301 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1302 else
1303 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1304 }
1305 } while (--c);
1306
Finn Thainb7465452016-01-03 16:06:05 +11001307 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 *count = c;
1310 *data = d;
1311 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001312 /* The phase read from the bus is valid if either REQ is (already)
1313 * asserted or if ACK hasn't been released yet. The latter applies if
1314 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1315 */
1316 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 *phase = tmp & PHASE_MASK;
1318 else
1319 *phase = PHASE_UNKNOWN;
1320
1321 if (!c || (*phase == p))
1322 return 0;
1323 else
1324 return -1;
1325}
1326
1327/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001328 * do_reset - issue a reset command
1329 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001331 * Issue a reset sequence to the NCR5380 and try and get the bus
1332 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001334 * This clears the reset interrupt flag because there may be no handler for
1335 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1336 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001338
Finn Thain54d8fe42016-01-03 16:05:06 +11001339static void do_reset(struct Scsi_Host *instance)
1340{
Finn Thain61e1ce52016-10-10 00:46:53 -04001341 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001342 unsigned long flags;
1343
1344 local_irq_save(flags);
1345 NCR5380_write(TARGET_COMMAND_REG,
1346 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001348 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001350 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1351 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Finn Thain80d3eb62016-01-03 16:05:34 +11001354/**
1355 * do_abort - abort the currently established nexus by going to
1356 * MESSAGE OUT phase and sending an ABORT message.
1357 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001359 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
1361
Finn Thain54d8fe42016-01-03 16:05:06 +11001362static int do_abort(struct Scsi_Host *instance)
1363{
Finn Thain61e1ce52016-10-10 00:46:53 -04001364 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 unsigned char *msgptr, phase, tmp;
1366 int len;
1367 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* Request message out phase */
1370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1371
Finn Thainaff0cf92016-01-03 16:06:09 +11001372 /*
1373 * Wait for the target to indicate a valid phase by asserting
1374 * REQ. Once this happens, we'll have either a MSGOUT phase
1375 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001377 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * We really don't care what value was on the bus or what value
1379 * the target sees, so we just handshake.
1380 */
1381
Finn Thaind5d37a02016-10-10 00:46:53 -04001382 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001383 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001384 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Finn Thainf35d3472016-01-03 16:05:33 +11001386 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1389
Finn Thainf35d3472016-01-03 16:05:33 +11001390 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001391 NCR5380_write(INITIATOR_COMMAND_REG,
1392 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thaind5d37a02016-10-10 00:46:53 -04001393 rc = NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001394 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001395 goto timeout;
1396 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 tmp = ABORT;
1400 msgptr = &tmp;
1401 len = 1;
1402 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001403 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /*
1406 * If we got here, and the command completed successfully,
1407 * we're about to go into bus free state.
1408 */
1409
1410 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001411
1412timeout:
1413 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1414 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Finn Thainaff0cf92016-01-03 16:06:09 +11001417/*
1418 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001419 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 *
1421 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001422 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001424 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001425 * what phase is expected, *count - pointer to number of
1426 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001427 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001429 * maximum number of bytes, 0 if all bytes or transferred or exit
1430 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001432 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 */
1434
1435
Finn Thain0d2cf862016-01-03 16:06:11 +11001436static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1437 unsigned char *phase, int *count,
1438 unsigned char **data)
1439{
1440 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001441 int c = *count;
1442 unsigned char p = *phase;
1443 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001445 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1448 *phase = tmp;
1449 return -1;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Finn Thain8053b0e2016-03-23 21:10:19 +11001452 hostdata->connected->SCp.phase = p;
1453
1454 if (p & SR_IO) {
1455 if (hostdata->read_overruns)
1456 c -= hostdata->read_overruns;
1457 else if (hostdata->flags & FLAG_DMA_FIXUP)
1458 --c;
1459 }
1460
1461 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1462 (p & SR_IO) ? "receive" : "send", c, d);
1463
Finn Thaine9db3192016-03-23 21:10:21 +11001464#ifdef CONFIG_SUN3
1465 /* send start chain */
1466 sun3scsi_dma_start(c, *data);
1467#endif
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001470 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1471 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Finn Thain8053b0e2016-03-23 21:10:19 +11001473 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1474 /* On the Medusa, it is a must to initialize the DMA before
1475 * starting the NCR. This is also the cleaner way for the TT.
1476 */
1477 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001478 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001479 else
Finn Thain4a98f892016-10-10 00:46:53 -04001480 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Finn Thainaff0cf92016-01-03 16:06:09 +11001483 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001484 * On the PAS16 at least I/O recovery delays are not needed here.
1485 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
1487
1488 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001489 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001490 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1492 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001493 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001495 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001497 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
Finn Thaine9db3192016-03-23 21:10:21 +11001500#ifdef CONFIG_SUN3
1501#ifdef SUN3_SCSI_VME
1502 dregs->csr |= CSR_DMA_ENABLE;
1503#endif
1504 sun3_dma_active = 1;
1505#endif
1506
Finn Thain8053b0e2016-03-23 21:10:19 +11001507 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1508 /* On the Falcon, the DMA setup must be done after the last
1509 * NCR access, else the DMA setup gets trashed!
1510 */
1511 if (p & SR_IO)
Finn Thain4a98f892016-10-10 00:46:53 -04001512 result = NCR5380_dma_recv_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001513 else
Finn Thain4a98f892016-10-10 00:46:53 -04001514 result = NCR5380_dma_send_setup(hostdata, d, c);
Finn Thain8053b0e2016-03-23 21:10:19 +11001515 }
1516
1517 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1518 if (result < 0)
1519 return result;
1520
1521 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1522 if (result > 0) {
1523 hostdata->dma_len = result;
1524 return 0;
1525 }
1526
1527 /* The result is zero iff pseudo DMA send/receive was completed. */
1528 hostdata->dma_len = c;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/*
Finn Thaine4dec682016-03-23 21:10:12 +11001531 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001532 *
1533 * For DMA sends, we want to wait until the last byte has been
1534 * transferred out over the bus before we turn off DMA mode. Alas, there
1535 * seems to be no terribly good way of doing this on a 5380 under all
1536 * conditions. For non-scatter-gather operations, we can wait until REQ
1537 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1538 * are nastier, since the device will be expecting more data than we
1539 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1540 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1541 * why this signal was added to the newer chips) but on the older 538[01]
1542 * this signal does not exist. The workaround for this lack is a watchdog;
1543 * we bail out of the wait-loop after a modest amount of wait-time if
1544 * the usual exit conditions are not met. Not a terribly clean or
1545 * correct solution :-%
1546 *
1547 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1548 * If the chip is in DMA receive mode, it will respond to a target's
1549 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1550 * ACK, even if it has _already_ been notified by the DMA controller that
1551 * the current DMA transfer has completed! If the NCR5380 is then taken
1552 * out of DMA mode, this already-acknowledged byte is lost. This is
1553 * not a problem for "one DMA transfer per READ command", because
1554 * the situation will never arise... either all of the data is DMA'ed
1555 * properly, or the target switches to MESSAGE IN phase to signal a
1556 * disconnection (either operation bringing the DMA to a clean halt).
1557 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001558 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001559 * condition before taking the NCR5380 out of DMA mode. One or two extra
1560 * bytes are transferred via PIO as necessary to fill out the original
1561 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 */
1563
Finn Thain8053b0e2016-03-23 21:10:19 +11001564 if (hostdata->flags & FLAG_DMA_FIXUP) {
1565 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001567 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001568 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * the chip to latch the last byte, read it, and then disable
1570 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001571 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1573 * REQ is deasserted when ACK is asserted, and not reasserted
1574 * until ACK goes false. Since the NCR5380 won't lower ACK
1575 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001576 * the DMA port or we take the NCR5380 out of DMA mode, we
1577 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * byte.
1579 */
1580
Finn Thaind5d37a02016-10-10 00:46:53 -04001581 if (NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001582 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001583 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001584 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Finn Thaind5d37a02016-10-10 00:46:53 -04001586 if (NCR5380_poll_politely(hostdata, STATUS_REG,
Finn Thain55181be2016-01-03 16:05:42 +11001587 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001588 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001589 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1590 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001591 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1592 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001594 * Wait for the last byte to be sent. If REQ is being asserted for
1595 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
Finn Thaind5d37a02016-10-10 00:46:53 -04001597 if (NCR5380_poll_politely2(hostdata,
Finn Thain55181be2016-01-03 16:05:42 +11001598 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1599 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001600 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001601 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001605
1606 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001607 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610/*
1611 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1612 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001613 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001614 * directs us to. Operates on the currently connected command,
1615 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 *
1617 * Inputs : instance, instance for which we are doing commands
1618 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001619 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001620 * modified if a command disconnects, *instance->connected will
1621 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001623 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001624 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
1626
Finn Thain0d2cf862016-01-03 16:06:11 +11001627static void NCR5380_information_transfer(struct Scsi_Host *instance)
Finn Thain4ab2a782017-01-15 18:50:57 -05001628 __releases(&hostdata->lock) __acquires(&hostdata->lock)
Finn Thain0d2cf862016-01-03 16:06:11 +11001629{
Finn Thaine8a60142016-01-03 16:05:54 +11001630 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 unsigned char msgout = NOP;
1632 int sink = 0;
1633 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 unsigned char *data;
1636 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001637 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Finn Thaine9db3192016-03-23 21:10:21 +11001639#ifdef SUN3_SCSI_VME
1640 dregs->csr |= CSR_INTR;
1641#endif
1642
Finn Thain11d2f632016-01-03 16:05:51 +11001643 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001644 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 tmp = NCR5380_read(STATUS_REG);
1647 /* We only have a valid SCSI phase when REQ is asserted */
1648 if (tmp & SR_REQ) {
1649 phase = (tmp & PHASE_MASK);
1650 if (phase != old_phase) {
1651 old_phase = phase;
1652 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1653 }
Finn Thaine9db3192016-03-23 21:10:21 +11001654#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04001655 if (phase == PHASE_CMDOUT &&
1656 sun3_dma_setup_done != cmd) {
1657 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11001658
1659 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
Finn Thain4a98f892016-10-10 00:46:53 -04001660 ++cmd->SCp.buffer;
1661 --cmd->SCp.buffers_residual;
1662 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1663 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thaine9db3192016-03-23 21:10:21 +11001664 }
1665
Finn Thain4a98f892016-10-10 00:46:53 -04001666 count = sun3scsi_dma_xfer_len(hostdata, cmd);
1667
1668 if (count > 0) {
1669 if (rq_data_dir(cmd->request))
1670 sun3scsi_dma_send_setup(hostdata,
1671 cmd->SCp.ptr, count);
1672 else
1673 sun3scsi_dma_recv_setup(hostdata,
1674 cmd->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11001675 sun3_dma_setup_done = cmd;
1676 }
1677#ifdef SUN3_SCSI_VME
1678 dregs->csr |= CSR_INTR;
1679#endif
1680 }
1681#endif /* CONFIG_SUN3 */
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (sink && (phase != PHASE_MSGOUT)) {
1684 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1685
Finn Thain3d07d222016-01-03 16:06:13 +11001686 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1687 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001688 while (NCR5380_read(STATUS_REG) & SR_REQ)
1689 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001690 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1691 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 sink = 0;
1693 continue;
1694 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 case PHASE_DATAOUT:
1698#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001699 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 sink = 1;
1701 do_abort(instance);
1702 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001703 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001704 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return;
1706#endif
Finn Thainbf1a0c6f2016-01-03 16:05:47 +11001707 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001708 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 * If there is no room left in the current buffer in the
1710 * scatter-gather list, move onto the next one.
1711 */
1712
1713 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1714 ++cmd->SCp.buffer;
1715 --cmd->SCp.buffers_residual;
1716 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001717 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001718 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1719 cmd->SCp.this_residual,
1720 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001724 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * PSEUDO-DMA for systems that are strictly PIO,
1726 * since we can let the hardware do the handshaking.
1727 *
1728 * For this to work, we need to know the transfersize
1729 * ahead of time, since the pseudo-DMA code will sit
1730 * in an unconditional loop.
1731 */
1732
Finn Thainff3d4572016-01-03 16:05:25 +11001733 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001734 if (!cmd->device->borken)
Finn Thain4a98f892016-10-10 00:46:53 -04001735 transfersize = NCR5380_dma_xfer_len(hostdata, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Finn Thain438af512016-03-23 21:10:18 +11001737 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001739 if (NCR5380_transfer_dma(instance, &phase,
1740 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001742 * If the watchdog timer fires, all future
1743 * accesses to this device will use the
1744 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001746 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001747 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 sink = 1;
1750 do_abort(instance);
1751 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* XXX - need to source or sink data here, as appropriate */
Finn Thain8053b0e2016-03-23 21:10:19 +11001753 }
Finn Thainf825e402016-03-23 21:10:15 +11001754 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001755 /* Transfer a small chunk so that the
1756 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001757 */
Finn Thain08348b12016-08-31 14:44:56 +10001758 transfersize = min(cmd->SCp.this_residual,
1759 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001760 len = transfersize;
1761 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001762 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001763 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001764 }
Finn Thaine9db3192016-03-23 21:10:21 +11001765#ifdef CONFIG_SUN3
1766 if (sun3_dma_setup_done == cmd)
1767 sun3_dma_setup_done = NULL;
1768#endif
Finn Thain16788472016-02-23 10:07:05 +11001769 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 case PHASE_MSGIN:
1771 len = 1;
1772 data = &tmp;
1773 NCR5380_transfer_pio(instance, &phase, &len, &data);
1774 cmd->SCp.Message = tmp;
1775
1776 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 case ABORT:
1778 case COMMAND_COMPLETE:
1779 /* Accept message by clearing ACK */
1780 sink = 1;
1781 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001782 dsprintk(NDEBUG_QUEUES, instance,
1783 "COMMAND COMPLETE %p target %d lun %llu\n",
1784 cmd, scmd_id(cmd), cmd->device->lun);
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Finn Thainf27db8e2016-01-03 16:06:00 +11001788 cmd->result &= ~0xffff;
1789 cmd->result |= cmd->SCp.Status;
1790 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Finn Thainf27db8e2016-01-03 16:06:00 +11001792 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001793 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001794 else {
1795 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1796 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1797 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1798 cmd);
1799 list_add_tail(&ncmd->list,
1800 &hostdata->autosense);
1801 } else
1802 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
Finn Thainaff0cf92016-01-03 16:06:09 +11001805 /*
1806 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 * arbitration can resume.
1808 */
1809 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001810
1811 /* Enable reselect interrupts */
1812 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain52d3e562016-03-23 21:10:20 +11001813
1814 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return;
1816 case MESSAGE_REJECT:
1817 /* Accept message by clearing ACK */
1818 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1819 switch (hostdata->last_message) {
1820 case HEAD_OF_QUEUE_TAG:
1821 case ORDERED_QUEUE_TAG:
1822 case SIMPLE_QUEUE_TAG:
1823 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001824 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 break;
1826 default:
1827 break;
1828 }
Finn Thain340b9612016-01-03 16:05:31 +11001829 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001830 case DISCONNECT:
1831 /* Accept message by clearing ACK */
1832 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1833 hostdata->connected = NULL;
1834 list_add(&ncmd->list, &hostdata->disconnected);
1835 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1836 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1837 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001838
Finn Thain0d2cf862016-01-03 16:06:11 +11001839 /*
1840 * Restore phase bits to 0 so an interrupted selection,
1841 * arbitration can resume.
1842 */
1843 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Finn Thain0d2cf862016-01-03 16:06:11 +11001845 /* Enable reselect interrupts */
1846 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine9db3192016-03-23 21:10:21 +11001847#ifdef SUN3_SCSI_VME
1848 dregs->csr |= CSR_DMA_ENABLE;
1849#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001850 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001851 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001853 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * ignore SAVE/RESTORE pointers calls.
1855 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001856 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001858 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * compatible.
1860 */
1861 case SAVE_POINTERS:
1862 case RESTORE_POINTERS:
1863 /* Accept message by clearing ACK */
1864 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1865 break;
1866 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001867 /*
1868 * Start the message buffer with the EXTENDED_MESSAGE
1869 * byte, since spi_print_msg() wants the whole thing.
1870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 extended_msg[0] = EXTENDED_MESSAGE;
1872 /* Accept first byte by clearing ACK */
1873 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001874
1875 spin_unlock_irq(&hostdata->lock);
1876
Finn Thainb7465452016-01-03 16:06:05 +11001877 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 len = 2;
1880 data = extended_msg + 1;
1881 phase = PHASE_MSGIN;
1882 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001883 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1884 (int)extended_msg[1],
1885 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Finn Thaine0783ed2016-01-03 16:05:45 +11001887 if (!len && extended_msg[1] > 0 &&
1888 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* Accept third byte by clearing ACK */
1890 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1891 len = extended_msg[1] - 1;
1892 data = extended_msg + 3;
1893 phase = PHASE_MSGIN;
1894
1895 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001896 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1897 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 switch (extended_msg[2]) {
1900 case EXTENDED_SDTR:
1901 case EXTENDED_WDTR:
1902 case EXTENDED_MODIFY_DATA_POINTER:
1903 case EXTENDED_EXTENDED_IDENTIFY:
1904 tmp = 0;
1905 }
1906 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001907 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 tmp = 0;
1909 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001910 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
1911 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 tmp = 0;
1913 }
Finn Thain11d2f632016-01-03 16:05:51 +11001914
1915 spin_lock_irq(&hostdata->lock);
1916 if (!hostdata->connected)
1917 return;
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* Fall through to reject message */
1920
Finn Thainaff0cf92016-01-03 16:06:09 +11001921 /*
1922 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * reject it.
1924 */
1925 default:
1926 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001927 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001928 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 printk("\n");
1930 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04001931 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001932 "rejecting unknown message %02x\n",
1933 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 else
Jeff Garzik017560f2005-10-24 18:04:36 -04001935 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001936 "rejecting unknown extended message code %02x, length %d\n",
1937 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 msgout = MESSAGE_REJECT;
1940 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1941 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001942 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 break;
1944 case PHASE_MSGOUT:
1945 len = 1;
1946 data = &msgout;
1947 hostdata->last_message = msgout;
1948 NCR5380_transfer_pio(instance, &phase, &len, &data);
1949 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 hostdata->connected = NULL;
1951 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001952 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11001953 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1955 return;
1956 }
1957 msgout = NOP;
1958 break;
1959 case PHASE_CMDOUT:
1960 len = cmd->cmd_len;
1961 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11001962 /*
1963 * XXX for performance reasons, on machines with a
1964 * PSEUDO-DMA architecture we should probably
1965 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
1967 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 break;
1969 case PHASE_STATIN:
1970 len = 1;
1971 data = &tmp;
1972 NCR5380_transfer_pio(instance, &phase, &len, &data);
1973 cmd->SCp.Status = tmp;
1974 break;
1975 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001976 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11001977 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11001978 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11001979 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11001980 spin_unlock_irq(&hostdata->lock);
Finn Thaind5d37a02016-10-10 00:46:53 -04001981 NCR5380_poll_politely(hostdata, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001982 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Finn Thain11d2f632016-01-03 16:05:51 +11001984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987/*
1988 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
1989 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001990 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11001991 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
1992 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11001993 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
1996
Finn Thain0d2cf862016-01-03 16:06:11 +11001997static void NCR5380_reselect(struct Scsi_Host *instance)
1998{
Finn Thaine8a60142016-01-03 16:05:54 +11001999 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002001 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002003 struct NCR5380_cmd *ncmd;
2004 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 /*
2007 * Disable arbitration, etc. since the host adapter obviously
2008 * lost, and tell an interrupted NCR5380_select() to restart.
2009 */
2010
2011 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11002014
2015 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Finn Thainaff0cf92016-01-03 16:06:09 +11002017 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 * At this point, we have detected that our SCSI ID is on the bus,
2019 * SEL is true and BSY was false for at least one bus settle delay
2020 * (400 ns).
2021 *
2022 * We must assert BSY ourselves, until the target drops the SEL
2023 * signal.
2024 */
2025
2026 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thaind5d37a02016-10-10 00:46:53 -04002027 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002028 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2029 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2030 return;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2033
2034 /*
2035 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
2037
Finn Thaind5d37a02016-10-10 00:46:53 -04002038 if (NCR5380_poll_politely(hostdata,
Finn Thain72064a72016-01-03 16:05:44 +11002039 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2040 do_abort(instance);
2041 return;
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Finn Thaine9db3192016-03-23 21:10:21 +11002044#ifdef CONFIG_SUN3
2045 /* acknowledge toggle to MSGIN */
2046 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Finn Thaine9db3192016-03-23 21:10:21 +11002048 /* peek at the byte without really hitting the bus */
2049 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2050#else
2051 {
2052 int len = 1;
2053 unsigned char *data = msg;
2054 unsigned char phase = PHASE_MSGIN;
2055
2056 NCR5380_transfer_pio(instance, &phase, &len, &data);
2057
2058 if (len) {
2059 do_abort(instance);
2060 return;
2061 }
Finn Thain72064a72016-01-03 16:05:44 +11002062 }
Finn Thaine9db3192016-03-23 21:10:21 +11002063#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002066 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002067 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002068 printk("\n");
2069 do_abort(instance);
2070 return;
2071 }
2072 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Finn Thain72064a72016-01-03 16:05:44 +11002074 /*
2075 * We need to add code for SCSI-II to track which devices have
2076 * I_T_L_Q nexuses established, and which have simple I_T_L
2077 * nexuses so we can chose to do additional data transfer.
2078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Finn Thain72064a72016-01-03 16:05:44 +11002080 /*
2081 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2082 * just reestablished, and remove it from the disconnected queue.
2083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Finn Thain32b26a12016-01-03 16:05:58 +11002085 tmp = NULL;
2086 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2087 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2088
2089 if (target_mask == (1 << scmd_id(cmd)) &&
2090 lun == (u8)cmd->device->lun) {
2091 list_del(&ncmd->list);
2092 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002096
2097 if (tmp) {
2098 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2099 "reselect: removed %p from disconnected queue\n", tmp);
2100 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002101 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2102 target_mask, lun);
2103 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002104 * Since we have an established nexus that we can't do anything
2105 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Finn Thain72064a72016-01-03 16:05:44 +11002110
Finn Thaine9db3192016-03-23 21:10:21 +11002111#ifdef CONFIG_SUN3
Finn Thain4a98f892016-10-10 00:46:53 -04002112 if (sun3_dma_setup_done != tmp) {
2113 int count;
Finn Thaine9db3192016-03-23 21:10:21 +11002114
2115 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
Finn Thain4a98f892016-10-10 00:46:53 -04002116 ++tmp->SCp.buffer;
2117 --tmp->SCp.buffers_residual;
2118 tmp->SCp.this_residual = tmp->SCp.buffer->length;
2119 tmp->SCp.ptr = sg_virt(tmp->SCp.buffer);
Finn Thaine9db3192016-03-23 21:10:21 +11002120 }
2121
Finn Thain4a98f892016-10-10 00:46:53 -04002122 count = sun3scsi_dma_xfer_len(hostdata, tmp);
2123
2124 if (count > 0) {
2125 if (rq_data_dir(tmp->request))
2126 sun3scsi_dma_send_setup(hostdata,
2127 tmp->SCp.ptr, count);
2128 else
2129 sun3scsi_dma_recv_setup(hostdata,
2130 tmp->SCp.ptr, count);
Finn Thaine9db3192016-03-23 21:10:21 +11002131 sun3_dma_setup_done = tmp;
2132 }
2133 }
2134
2135 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2136#endif /* CONFIG_SUN3 */
2137
Finn Thain72064a72016-01-03 16:05:44 +11002138 /* Accept message by clearing ACK */
2139 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2140
2141 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002142 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2143 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Finn Thain8b00c3d2016-01-03 16:06:01 +11002146/**
2147 * list_find_cmd - test for presence of a command in a linked list
2148 * @haystack: list of commands
2149 * @needle: command to search for
2150 */
2151
2152static bool list_find_cmd(struct list_head *haystack,
2153 struct scsi_cmnd *needle)
2154{
2155 struct NCR5380_cmd *ncmd;
2156
2157 list_for_each_entry(ncmd, haystack, list)
2158 if (NCR5380_to_scmd(ncmd) == needle)
2159 return true;
2160 return false;
2161}
2162
2163/**
2164 * list_remove_cmd - remove a command from linked list
2165 * @haystack: list of commands
2166 * @needle: command to remove
2167 */
2168
2169static bool list_del_cmd(struct list_head *haystack,
2170 struct scsi_cmnd *needle)
2171{
2172 if (list_find_cmd(haystack, needle)) {
2173 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2174
2175 list_del(&ncmd->list);
2176 return true;
2177 }
2178 return false;
2179}
2180
2181/**
2182 * NCR5380_abort - scsi host eh_abort_handler() method
2183 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002185 * Try to abort a given command by removing it from queues and/or sending
2186 * the target an abort message. This may not succeed in causing a target
2187 * to abort the command. Nonetheless, the low-level driver must forget about
2188 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002190 * The normal path taken by a command is as follows. For EH we trace this
2191 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002193 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2194 * [disconnected -> connected ->]...
2195 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002197 * If cmd was not found at all then presumably it has already been completed,
2198 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002199 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002200 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002201 * We have no option but to forget the aborted command (even if it still
2202 * lacks sense data). The mid-layer may re-issue a command that is in error
2203 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2204 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002205 *
2206 * The lock protects driver data structures, but EH handlers also use it
2207 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 */
2209
Finn Thain710ddd02014-11-12 16:12:02 +11002210static int NCR5380_abort(struct scsi_cmnd *cmd)
2211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002213 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002214 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002215 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002216
Finn Thain11d2f632016-01-03 16:05:51 +11002217 spin_lock_irqsave(&hostdata->lock, flags);
2218
Finn Thain32b26a12016-01-03 16:05:58 +11002219#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002220 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002221#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002222 NCR5380_dprint(NDEBUG_ANY, instance);
2223 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Finn Thain8b00c3d2016-01-03 16:06:01 +11002225 if (list_del_cmd(&hostdata->unissued, cmd)) {
2226 dsprintk(NDEBUG_ABORT, instance,
2227 "abort: removed %p from issue queue\n", cmd);
2228 cmd->result = DID_ABORT << 16;
2229 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002230 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002231 }
2232
Finn Thain707d62b2016-01-03 16:06:02 +11002233 if (hostdata->selecting == cmd) {
2234 dsprintk(NDEBUG_ABORT, instance,
2235 "abort: cmd %p == selecting\n", cmd);
2236 hostdata->selecting = NULL;
2237 cmd->result = DID_ABORT << 16;
2238 complete_cmd(instance, cmd);
2239 goto out;
2240 }
2241
Finn Thain8b00c3d2016-01-03 16:06:01 +11002242 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2243 dsprintk(NDEBUG_ABORT, instance,
2244 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002245 /* Can't call NCR5380_select() and send ABORT because that
2246 * means releasing the lock. Need a bus reset.
2247 */
Finn Thaindc183962016-02-23 10:07:07 +11002248 set_host_byte(cmd, DID_ERROR);
2249 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002250 result = FAILED;
2251 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002252 }
2253
2254 if (hostdata->connected == cmd) {
2255 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2256 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002257 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002258 if (do_abort(instance)) {
2259 set_host_byte(cmd, DID_ERROR);
2260 complete_cmd(instance, cmd);
2261 result = FAILED;
2262 goto out;
2263 }
2264 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002265 complete_cmd(instance, cmd);
2266 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002267 }
2268
Finn Thaindc183962016-02-23 10:07:07 +11002269 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002270 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002271 "abort: removed %p from sense queue\n", cmd);
2272 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002273 complete_cmd(instance, cmd);
2274 }
2275
2276out:
2277 if (result == FAILED)
2278 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2279 else
2280 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2281
2282 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002283 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002284 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002285
Finn Thain8b00c3d2016-01-03 16:06:01 +11002286 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
2289
Finn Thain3be1b3e2016-01-03 16:05:12 +11002290/**
2291 * NCR5380_bus_reset - reset the SCSI bus
2292 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002294 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
2296
Finn Thain710ddd02014-11-12 16:12:02 +11002297static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002298{
2299 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002300 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002301 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002302 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002303 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Finn Thain11d2f632016-01-03 16:05:51 +11002305 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002306
2307#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002308 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002309#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002310 NCR5380_dprint(NDEBUG_ANY, instance);
2311 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002312
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002313 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002314
Finn Thain62717f52016-01-03 16:06:03 +11002315 /* reset NCR registers */
2316 NCR5380_write(MODE_REG, MR_BASE);
2317 NCR5380_write(TARGET_COMMAND_REG, 0);
2318 NCR5380_write(SELECT_ENABLE_REG, 0);
2319
2320 /* After the reset, there are no more connected or disconnected commands
2321 * and no busy units; so clear the low-level status here to avoid
2322 * conflicts when the mid-level code tries to wake up the affected
2323 * commands!
2324 */
2325
Finn Thain1884c282016-02-23 10:07:04 +11002326 if (list_del_cmd(&hostdata->unissued, cmd)) {
2327 cmd->result = DID_RESET << 16;
2328 cmd->scsi_done(cmd);
2329 }
2330
2331 if (hostdata->selecting) {
2332 hostdata->selecting->result = DID_RESET << 16;
2333 complete_cmd(instance, hostdata->selecting);
2334 hostdata->selecting = NULL;
2335 }
Finn Thain62717f52016-01-03 16:06:03 +11002336
2337 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2338 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2339
2340 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002341 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002342 }
Finn Thain1884c282016-02-23 10:07:04 +11002343 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002344
2345 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2346 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2347
2348 set_host_byte(cmd, DID_RESET);
2349 cmd->scsi_done(cmd);
2350 }
Finn Thain1884c282016-02-23 10:07:04 +11002351 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002352
2353 if (hostdata->connected) {
2354 set_host_byte(hostdata->connected, DID_RESET);
2355 complete_cmd(instance, hostdata->connected);
2356 hostdata->connected = NULL;
2357 }
2358
Finn Thain62717f52016-01-03 16:06:03 +11002359 for (i = 0; i < 8; ++i)
2360 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002361 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002362
2363 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002364 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002365 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return SUCCESS;
2368}