blob: 33676c9ec5fb80517de4d44bbe939a00b4a89cbc [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 *
Finn Thainaff0cf92016-01-03 16:06:09 +110099 * These macros control options :
100 * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
Finn Thain594d4ba2016-01-03 16:06:10 +1100101 * defined.
Finn Thainaff0cf92016-01-03 16:06:09 +1100102 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
Finn Thain594d4ba2016-01-03 16:06:10 +1100104 * for commands that return with a CHECK CONDITION status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
106 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
Finn Thain594d4ba2016-01-03 16:06:10 +1100107 * transceivers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
110 *
Finn Thain8053b0e2016-03-23 21:10:19 +1100111 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * These macros MUST be defined :
Finn Thainaff0cf92016-01-03 16:06:09 +1100114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * NCR5380_read(register) - read from the specified register
116 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100117 * NCR5380_write(register, value) - write to the specific register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
Finn Thainaff0cf92016-01-03 16:06:09 +1100119 * NCR5380_implementation_fields - additional fields needed for this
Finn Thain594d4ba2016-01-03 16:06:10 +1100120 * specific implementation of the NCR5380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
122 * Either real DMA *or* pseudo DMA may be implemented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * NCR5380_dma_write_setup(instance, src, count) - initialize
125 * NCR5380_dma_read_setup(instance, dst, count) - initialize
126 * NCR5380_dma_residual(instance); - residual count
127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * The generic driver is initialized by calling NCR5380_init(instance),
Finn Thainaff0cf92016-01-03 16:06:09 +1100129 * after setting the appropriate host specific fields and ID. If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
131 * possible) function may be used.
132 */
133
Finn Thaine5d55d12016-03-23 21:10:16 +1100134#ifndef NCR5380_io_delay
135#define NCR5380_io_delay(x)
136#endif
137
Finn Thain52d3e562016-03-23 21:10:20 +1100138#ifndef NCR5380_acquire_dma_irq
139#define NCR5380_acquire_dma_irq(x) (1)
140#endif
141
142#ifndef NCR5380_release_dma_irq
143#define NCR5380_release_dma_irq(x)
144#endif
145
Finn Thain54d8fe42016-01-03 16:05:06 +1100146static int do_abort(struct Scsi_Host *);
147static void do_reset(struct Scsi_Host *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Finn Thainc16df322016-01-03 16:06:08 +1100149/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100150 * initialize_SCp - init the scsi pointer field
Finn Thain594d4ba2016-01-03 16:06:10 +1100151 * @cmd: command block to set up
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100153 * Set up the internal fields in the SCSI command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
155
Finn Thain710ddd02014-11-12 16:12:02 +1100156static inline void initialize_SCp(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Finn Thainaff0cf92016-01-03 16:06:09 +1100158 /*
159 * Initialize the Scsi Pointer field so that all of the commands in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * various queues are valid.
161 */
162
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200163 if (scsi_bufflen(cmd)) {
164 cmd->SCp.buffer = scsi_sglist(cmd);
165 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
Jens Axboe45711f12007-10-22 21:19:53 +0200166 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 cmd->SCp.this_residual = cmd->SCp.buffer->length;
168 } else {
169 cmd->SCp.buffer = NULL;
170 cmd->SCp.buffers_residual = 0;
Boaz Harrosh9e0fe442007-11-05 11:23:35 +0200171 cmd->SCp.ptr = NULL;
172 cmd->SCp.this_residual = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Finn Thainf27db8e2016-01-03 16:06:00 +1100174
175 cmd->SCp.Status = 0;
176 cmd->SCp.Message = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/**
Finn Thainb32ade12016-01-03 16:05:41 +1100180 * NCR5380_poll_politely2 - wait for two chip register values
Finn Thain2f854b82016-01-03 16:05:22 +1100181 * @instance: controller to poll
Finn Thainb32ade12016-01-03 16:05:41 +1100182 * @reg1: 5380 register to poll
183 * @bit1: Bitmask to check
184 * @val1: Expected value
185 * @reg2: Second 5380 register to poll
186 * @bit2: Second bitmask to check
187 * @val2: Second expected value
Finn Thain2f854b82016-01-03 16:05:22 +1100188 * @wait: Time-out in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
Finn Thain2f854b82016-01-03 16:05:22 +1100190 * Polls the chip in a reasonably efficient manner waiting for an
191 * event to occur. After a short quick poll we begin to yield the CPU
192 * (if possible). In irq contexts the time-out is arbitrarily limited.
193 * Callers may hold locks as long as they are held in irq mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Finn Thainb32ade12016-01-03 16:05:41 +1100195 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Finn Thainb32ade12016-01-03 16:05:41 +1100198static int NCR5380_poll_politely2(struct Scsi_Host *instance,
Finn Thain61e1ce52016-10-10 00:46:53 -0400199 unsigned int reg1, u8 bit1, u8 val1,
200 unsigned int reg2, u8 bit2, u8 val2,
201 unsigned long wait)
Finn Thain2f854b82016-01-03 16:05:22 +1100202{
203 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thaind4408dd2016-10-10 00:46:52 -0400204 unsigned long n = hostdata->poll_loops;
Finn Thain2f854b82016-01-03 16:05:22 +1100205 unsigned long deadline = jiffies + wait;
Finn Thain2f854b82016-01-03 16:05:22 +1100206
Finn Thain2f854b82016-01-03 16:05:22 +1100207 do {
Finn Thainb32ade12016-01-03 16:05:41 +1100208 if ((NCR5380_read(reg1) & bit1) == val1)
209 return 0;
210 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212 cpu_relax();
Finn Thain2f854b82016-01-03 16:05:22 +1100213 } while (n--);
214
215 if (irqs_disabled() || in_interrupt())
216 return -ETIMEDOUT;
217
218 /* Repeatedly sleep for 1 ms until deadline */
219 while (time_is_after_jiffies(deadline)) {
220 schedule_timeout_uninterruptible(1);
Finn Thainb32ade12016-01-03 16:05:41 +1100221 if ((NCR5380_read(reg1) & bit1) == val1)
222 return 0;
223 if ((NCR5380_read(reg2) & bit2) == val2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Finn Thain2f854b82016-01-03 16:05:22 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -ETIMEDOUT;
228}
229
viro@ZenIV.linux.org.uk185a7a12005-09-07 23:18:24 +0100230#if NDEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static struct {
232 unsigned char mask;
233 const char *name;
Finn Thainaff0cf92016-01-03 16:06:09 +1100234} signals[] = {
235 {SR_DBP, "PARITY"},
236 {SR_RST, "RST"},
237 {SR_BSY, "BSY"},
238 {SR_REQ, "REQ"},
239 {SR_MSG, "MSG"},
240 {SR_CD, "CD"},
241 {SR_IO, "IO"},
242 {SR_SEL, "SEL"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100244},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245basrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100246 {BASR_END_DMA_TRANSFER, "END OF DMA"},
247 {BASR_DRQ, "DRQ"},
248 {BASR_PARITY_ERROR, "PARITY ERROR"},
249 {BASR_IRQ, "IRQ"},
250 {BASR_PHASE_MATCH, "PHASE MATCH"},
251 {BASR_BUSY_ERROR, "BUSY ERROR"},
Finn Thainaff0cf92016-01-03 16:06:09 +1100252 {BASR_ATN, "ATN"},
253 {BASR_ACK, "ACK"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100255},
256icrs[] = {
257 {ICR_ASSERT_RST, "ASSERT RST"},
Finn Thain12866b92016-03-23 21:10:25 +1100258 {ICR_ARBITRATION_PROGRESS, "ARB. IN PROGRESS"},
259 {ICR_ARBITRATION_LOST, "LOST ARB."},
Finn Thainaff0cf92016-01-03 16:06:09 +1100260 {ICR_ASSERT_ACK, "ASSERT ACK"},
261 {ICR_ASSERT_BSY, "ASSERT BSY"},
262 {ICR_ASSERT_SEL, "ASSERT SEL"},
263 {ICR_ASSERT_ATN, "ASSERT ATN"},
264 {ICR_ASSERT_DATA, "ASSERT DATA"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 {0, NULL}
Finn Thainaff0cf92016-01-03 16:06:09 +1100266},
267mrs[] = {
Finn Thain12866b92016-03-23 21:10:25 +1100268 {MR_BLOCK_DMA_MODE, "BLOCK DMA MODE"},
269 {MR_TARGET, "TARGET"},
270 {MR_ENABLE_PAR_CHECK, "PARITY CHECK"},
271 {MR_ENABLE_PAR_INTR, "PARITY INTR"},
272 {MR_ENABLE_EOP_INTR, "EOP INTR"},
273 {MR_MONITOR_BSY, "MONITOR BSY"},
274 {MR_DMA_MODE, "DMA MODE"},
275 {MR_ARBITRATE, "ARBITRATE"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0, NULL}
277};
278
279/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100280 * NCR5380_print - print scsi bus signals
281 * @instance: adapter state to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100283 * Print the SCSI bus signals for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285
286static void NCR5380_print(struct Scsi_Host *instance)
287{
Finn Thain61e1ce52016-10-10 00:46:53 -0400288 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned char status, data, basr, mr, icr, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
292 status = NCR5380_read(STATUS_REG);
293 mr = NCR5380_read(MODE_REG);
294 icr = NCR5380_read(INITIATOR_COMMAND_REG);
295 basr = NCR5380_read(BUS_AND_STATUS_REG);
296
Finn Thain12866b92016-03-23 21:10:25 +1100297 printk(KERN_DEBUG "SR = 0x%02x : ", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 for (i = 0; signals[i].mask; ++i)
299 if (status & signals[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100300 printk(KERN_CONT "%s, ", signals[i].name);
301 printk(KERN_CONT "\nBASR = 0x%02x : ", basr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (i = 0; basrs[i].mask; ++i)
303 if (basr & basrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100304 printk(KERN_CONT "%s, ", basrs[i].name);
305 printk(KERN_CONT "\nICR = 0x%02x : ", icr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 for (i = 0; icrs[i].mask; ++i)
307 if (icr & icrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100308 printk(KERN_CONT "%s, ", icrs[i].name);
309 printk(KERN_CONT "\nMR = 0x%02x : ", mr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 for (i = 0; mrs[i].mask; ++i)
311 if (mr & mrs[i].mask)
Finn Thain12866b92016-03-23 21:10:25 +1100312 printk(KERN_CONT "%s, ", mrs[i].name);
313 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Finn Thain0d2cf862016-01-03 16:06:11 +1100316static struct {
317 unsigned char value;
318 const char *name;
319} phases[] = {
320 {PHASE_DATAOUT, "DATAOUT"},
321 {PHASE_DATAIN, "DATAIN"},
322 {PHASE_CMDOUT, "CMDOUT"},
323 {PHASE_STATIN, "STATIN"},
324 {PHASE_MSGOUT, "MSGOUT"},
325 {PHASE_MSGIN, "MSGIN"},
326 {PHASE_UNKNOWN, "UNKNOWN"}
327};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Finn Thainc16df322016-01-03 16:06:08 +1100329/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100330 * NCR5380_print_phase - show SCSI phase
Finn Thain594d4ba2016-01-03 16:06:10 +1100331 * @instance: adapter to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100333 * Print the current SCSI phase for debugging purposes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335
336static void NCR5380_print_phase(struct Scsi_Host *instance)
337{
Finn Thain61e1ce52016-10-10 00:46:53 -0400338 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned char status;
340 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 status = NCR5380_read(STATUS_REG);
343 if (!(status & SR_REQ))
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100344 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 else {
Finn Thain0d2cf862016-01-03 16:06:11 +1100346 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
347 (phases[i].value != (status & PHASE_MASK)); ++i)
348 ;
Finn Thain6a6ff4a2016-01-03 16:06:04 +1100349 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351}
352#endif
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Arnd Bergmann77f18a82016-10-11 11:23:23 +0200355static int probe_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100358 * probe_intr - helper for IRQ autoprobe
359 * @irq: interrupt number
360 * @dev_id: unused
361 * @regs: unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100363 * Set a flag to indicate the IRQ in question was received. This is
364 * used by the IRQ probe code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Finn Thainaff0cf92016-01-03 16:06:09 +1100366
Arnd Bergmann77f18a82016-10-11 11:23:23 +0200367static irqreturn_t probe_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 probe_irq = irq;
370 return IRQ_HANDLED;
371}
372
373/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100374 * NCR5380_probe_irq - find the IRQ of an NCR5380
375 * @instance: NCR5380 controller
376 * @possible: bitmask of ISA IRQ lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100378 * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
379 * and then looking to see what interrupt actually turned up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
381
Arnd Bergmann77f18a82016-10-11 11:23:23 +0200382static int __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
Andrew Morton702809c2007-05-23 14:41:56 -0700383 int possible)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Finn Thaine8a60142016-01-03 16:05:54 +1100385 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long timeout;
387 int trying_irqs, i, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Finn Thain22f5f102014-11-12 16:11:56 +1100389 for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100390 if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 trying_irqs |= mask;
392
Nicholas Mc Guire4e5a8002015-02-04 13:30:20 -0500393 timeout = jiffies + msecs_to_jiffies(250);
Finn Thain22f5f102014-11-12 16:11:56 +1100394 probe_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * A interrupt is triggered whenever BSY = false, SEL = true
Finn Thainaff0cf92016-01-03 16:06:09 +1100398 * and a bit set in the SELECT_ENABLE_REG is asserted on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * SCSI bus.
400 *
401 * Note that the bus is only driven when the phase control signals
402 * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
403 * to zero.
404 */
405
406 NCR5380_write(TARGET_COMMAND_REG, 0);
407 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
408 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
409 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
410
Finn Thain22f5f102014-11-12 16:11:56 +1100411 while (probe_irq == NO_IRQ && time_before(jiffies, timeout))
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800412 schedule_timeout_uninterruptible(1);
Finn Thainaff0cf92016-01-03 16:06:09 +1100413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 NCR5380_write(SELECT_ENABLE_REG, 0);
415 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
416
Finn Thain22f5f102014-11-12 16:11:56 +1100417 for (i = 1, mask = 2; i < 16; ++i, mask <<= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (trying_irqs & mask)
419 free_irq(i, NULL);
420
421 return probe_irq;
422}
423
424/**
Finn Thain594d4ba2016-01-03 16:06:10 +1100425 * NCR58380_info - report driver and host information
426 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100428 * For use as the host template info() handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
430
Finn Thain8c325132014-11-12 16:11:58 +1100431static const char *NCR5380_info(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Finn Thain8c325132014-11-12 16:11:58 +1100433 struct NCR5380_hostdata *hostdata = shost_priv(instance);
434
435 return hostdata->info;
436}
437
438static void prepare_info(struct Scsi_Host *instance)
439{
440 struct NCR5380_hostdata *hostdata = shost_priv(instance);
441
442 snprintf(hostdata->info, sizeof(hostdata->info),
Finn Thain820682b2016-10-10 00:46:53 -0400443 "%s, irq %d, "
444 "io_port 0x%lx, base 0x%lx, "
Finn Thain8c325132014-11-12 16:11:58 +1100445 "can_queue %d, cmd_per_lun %d, "
446 "sg_tablesize %d, this_id %d, "
Finn Thainbe3f4122016-01-03 16:05:50 +1100447 "flags { %s%s%s}, "
Finn Thain8c325132014-11-12 16:11:58 +1100448 "options { %s} ",
Finn Thain820682b2016-10-10 00:46:53 -0400449 instance->hostt->name, instance->irq,
450 hostdata->io_port, hostdata->base,
Finn Thain8c325132014-11-12 16:11:58 +1100451 instance->can_queue, instance->cmd_per_lun,
452 instance->sg_tablesize, instance->this_id,
Finn Thain1bb46002016-03-23 21:10:14 +1100453 hostdata->flags & FLAG_DMA_FIXUP ? "DMA_FIXUP " : "",
Finn Thain8c325132014-11-12 16:11:58 +1100454 hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "",
Finn Thain9c3f0e22016-01-03 16:05:11 +1100455 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#ifdef DIFFERENTIAL
Finn Thain8c325132014-11-12 16:11:58 +1100457 "DIFFERENTIAL "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#ifdef PARITY
Finn Thain8c325132014-11-12 16:11:58 +1100460 "PARITY "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#endif
Finn Thain8c325132014-11-12 16:11:58 +1100462 "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100466 * NCR5380_init - initialise an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100467 * @instance: adapter to configure
468 * @flags: control flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100470 * Initializes *instance and corresponding 5380 chip,
471 * with flags OR'd into the initial flags value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100473 * Notes : I assume that the host, hostno, and id bits have been
Finn Thain0d2cf862016-01-03 16:06:11 +1100474 * set correctly. I don't care about the irq and other fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100476 * Returns 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
478
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800479static int NCR5380_init(struct Scsi_Host *instance, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Finn Thaine8a60142016-01-03 16:05:54 +1100481 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100482 int i;
Finn Thain2f854b82016-01-03 16:05:22 +1100483 unsigned long deadline;
Finn Thaind4408dd2016-10-10 00:46:52 -0400484 unsigned long accesses_per_ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Finn Thainae5e33a2016-03-23 21:10:23 +1100486 instance->max_lun = 7;
487
Finn Thain0d2cf862016-01-03 16:06:11 +1100488 hostdata->host = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 hostdata->id_mask = 1 << instance->this_id;
Finn Thain0d2cf862016-01-03 16:06:11 +1100490 hostdata->id_higher_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
492 if (i > hostdata->id_mask)
493 hostdata->id_higher_mask |= i;
494 for (i = 0; i < 8; ++i)
495 hostdata->busy[i] = 0;
Finn Thaine4dec682016-03-23 21:10:12 +1100496 hostdata->dma_len = 0;
497
Finn Thain11d2f632016-01-03 16:05:51 +1100498 spin_lock_init(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 hostdata->connected = NULL;
Finn Thainf27db8e2016-01-03 16:06:00 +1100500 hostdata->sensing = NULL;
501 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain32b26a12016-01-03 16:05:58 +1100502 INIT_LIST_HEAD(&hostdata->unissued);
503 INIT_LIST_HEAD(&hostdata->disconnected);
504
Finn Thain55181be2016-01-03 16:05:42 +1100505 hostdata->flags = flags;
Finn Thainaff0cf92016-01-03 16:06:09 +1100506
Finn Thain8d8601a2016-01-03 16:05:37 +1100507 INIT_WORK(&hostdata->main_task, NCR5380_main);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100508 hostdata->work_q = alloc_workqueue("ncr5380_%d",
509 WQ_UNBOUND | WQ_MEM_RECLAIM,
510 1, instance->host_no);
511 if (!hostdata->work_q)
512 return -ENOMEM;
513
Finn Thain8c325132014-11-12 16:11:58 +1100514 prepare_info(instance);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
517 NCR5380_write(MODE_REG, MR_BASE);
518 NCR5380_write(TARGET_COMMAND_REG, 0);
519 NCR5380_write(SELECT_ENABLE_REG, 0);
Finn Thain2f854b82016-01-03 16:05:22 +1100520
521 /* Calibrate register polling loop */
522 i = 0;
523 deadline = jiffies + 1;
524 do {
525 cpu_relax();
526 } while (time_is_after_jiffies(deadline));
527 deadline += msecs_to_jiffies(256);
528 do {
529 NCR5380_read(STATUS_REG);
530 ++i;
531 cpu_relax();
532 } while (time_is_after_jiffies(deadline));
Finn Thaind4408dd2016-10-10 00:46:52 -0400533 accesses_per_ms = i / 256;
534 hostdata->poll_loops = NCR5380_REG_POLL_TIME * accesses_per_ms / 2;
Finn Thain2f854b82016-01-03 16:05:22 +1100535
Finn Thainb6488f92016-01-03 16:05:08 +1100536 return 0;
537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Finn Thainb6488f92016-01-03 16:05:08 +1100539/**
540 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
541 * @instance: adapter to check
542 *
543 * If the system crashed, it may have crashed with a connected target and
544 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
545 * currently established nexus, which we know nothing about. Failing that
546 * do a bus reset.
547 *
548 * Note that a bus reset will cause the chip to assert IRQ.
549 *
550 * Returns 0 if successful, otherwise -ENXIO.
551 */
552
553static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
554{
Finn Thain9c3f0e22016-01-03 16:05:11 +1100555 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainb6488f92016-01-03 16:05:08 +1100556 int pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
559 switch (pass) {
560 case 1:
561 case 3:
562 case 5:
Finn Thain636b1ec2016-01-03 16:05:10 +1100563 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
564 NCR5380_poll_politely(instance,
565 STATUS_REG, SR_BSY, 0, 5 * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567 case 2:
Finn Thain636b1ec2016-01-03 16:05:10 +1100568 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 do_abort(instance);
570 break;
571 case 4:
Finn Thain636b1ec2016-01-03 16:05:10 +1100572 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 do_reset(instance);
Finn Thain9c3f0e22016-01-03 16:05:11 +1100574 /* Wait after a reset; the SCSI standard calls for
575 * 250ms, we wait 500ms to be on the safe side.
576 * But some Toshiba CD-ROMs need ten times that.
577 */
578 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
579 msleep(2500);
580 else
581 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583 case 6:
Finn Thain636b1ec2016-01-03 16:05:10 +1100584 shost_printk(KERN_ERR, instance, "bus locked solid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ENXIO;
586 }
587 }
588 return 0;
589}
590
591/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100592 * NCR5380_exit - remove an NCR5380
Finn Thain594d4ba2016-01-03 16:06:10 +1100593 * @instance: adapter to remove
Finn Thain0d2cf862016-01-03 16:06:11 +1100594 *
595 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
597
Randy Dunlapa43cf0f2008-01-22 21:39:33 -0800598static void NCR5380_exit(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Finn Thaine8a60142016-01-03 16:05:54 +1100600 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Finn Thain8d8601a2016-01-03 16:05:37 +1100602 cancel_work_sync(&hostdata->main_task);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100603 destroy_workqueue(hostdata->work_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/**
Finn Thain677e0192016-01-03 16:05:59 +1100607 * complete_cmd - finish processing a command and return it to the SCSI ML
608 * @instance: the host instance
609 * @cmd: command to complete
610 */
611
612static void complete_cmd(struct Scsi_Host *instance,
613 struct scsi_cmnd *cmd)
614{
615 struct NCR5380_hostdata *hostdata = shost_priv(instance);
616
617 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
618
Finn Thainf27db8e2016-01-03 16:06:00 +1100619 if (hostdata->sensing == cmd) {
620 /* Autosense processing ends here */
621 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
622 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
623 set_host_byte(cmd, DID_ERROR);
624 } else
625 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
626 hostdata->sensing = NULL;
627 }
628
Finn Thain677e0192016-01-03 16:05:59 +1100629 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
630
631 cmd->scsi_done(cmd);
632}
633
634/**
Finn Thain1bb40582016-01-03 16:05:29 +1100635 * NCR5380_queue_command - queue a command
636 * @instance: the relevant SCSI adapter
637 * @cmd: SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *
Finn Thain1bb40582016-01-03 16:05:29 +1100639 * cmd is added to the per-instance issue queue, with minor
640 * twiddling done to the host specific fields of cmd. If the
641 * main coroutine is not running, it is restarted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
643
Finn Thain1bb40582016-01-03 16:05:29 +1100644static int NCR5380_queue_command(struct Scsi_Host *instance,
645 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Finn Thain1bb40582016-01-03 16:05:29 +1100647 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain32b26a12016-01-03 16:05:58 +1100648 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
Finn Thain1bb40582016-01-03 16:05:29 +1100649 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651#if (NDEBUG & NDEBUG_NO_WRITE)
652 switch (cmd->cmnd[0]) {
653 case WRITE_6:
654 case WRITE_10:
Finn Thaindbb6b352016-01-03 16:05:53 +1100655 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 cmd->result = (DID_ERROR << 16);
Finn Thain1bb40582016-01-03 16:05:29 +1100657 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659 }
Finn Thain0d2cf862016-01-03 16:06:11 +1100660#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 cmd->result = 0;
663
Finn Thain52d3e562016-03-23 21:10:20 +1100664 if (!NCR5380_acquire_dma_irq(instance))
665 return SCSI_MLQUEUE_HOST_BUSY;
666
Finn Thain11d2f632016-01-03 16:05:51 +1100667 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100668
Finn Thainaff0cf92016-01-03 16:06:09 +1100669 /*
670 * Insert the cmd into the issue queue. Note that REQUEST SENSE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * commands are added to the head of the queue since any command will
Finn Thainaff0cf92016-01-03 16:06:09 +1100672 * clear the contingent allegiance condition that exists and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * sense data is only guaranteed to be valid while the condition exists.
674 */
675
Finn Thain32b26a12016-01-03 16:05:58 +1100676 if (cmd->cmnd[0] == REQUEST_SENSE)
677 list_add(&ncmd->list, &hostdata->unissued);
678 else
679 list_add_tail(&ncmd->list, &hostdata->unissued);
680
Finn Thain11d2f632016-01-03 16:05:51 +1100681 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain1bb40582016-01-03 16:05:29 +1100682
Finn Thaindbb6b352016-01-03 16:05:53 +1100683 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
684 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* Kick off command processing */
Finn Thain8d8601a2016-01-03 16:05:37 +1100687 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689}
690
Finn Thain52d3e562016-03-23 21:10:20 +1100691static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
692{
693 struct NCR5380_hostdata *hostdata = shost_priv(instance);
694
695 /* Caller does the locking needed to set & test these data atomically */
696 if (list_empty(&hostdata->disconnected) &&
697 list_empty(&hostdata->unissued) &&
698 list_empty(&hostdata->autosense) &&
699 !hostdata->connected &&
700 !hostdata->selecting)
701 NCR5380_release_dma_irq(instance);
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/**
Finn Thainf27db8e2016-01-03 16:06:00 +1100705 * dequeue_next_cmd - dequeue a command for processing
706 * @instance: the scsi host instance
707 *
708 * Priority is given to commands on the autosense queue. These commands
709 * need autosense because of a CHECK CONDITION result.
710 *
711 * Returns a command pointer if a command is found for a target that is
712 * not already busy. Otherwise returns NULL.
713 */
714
715static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
716{
717 struct NCR5380_hostdata *hostdata = shost_priv(instance);
718 struct NCR5380_cmd *ncmd;
719 struct scsi_cmnd *cmd;
720
Finn Thain8d5dbec2016-02-23 10:07:09 +1100721 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100722 list_for_each_entry(ncmd, &hostdata->unissued, list) {
723 cmd = NCR5380_to_scmd(ncmd);
724 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
725 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
726
727 if (!(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))) {
728 list_del(&ncmd->list);
729 dsprintk(NDEBUG_QUEUES, instance,
730 "dequeue: removed %p from issue queue\n", cmd);
731 return cmd;
732 }
733 }
734 } else {
735 /* Autosense processing begins here */
736 ncmd = list_first_entry(&hostdata->autosense,
737 struct NCR5380_cmd, list);
738 list_del(&ncmd->list);
739 cmd = NCR5380_to_scmd(ncmd);
740 dsprintk(NDEBUG_QUEUES, instance,
741 "dequeue: removed %p from autosense queue\n", cmd);
742 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
743 hostdata->sensing = cmd;
744 return cmd;
745 }
746 return NULL;
747}
748
749static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
750{
751 struct NCR5380_hostdata *hostdata = shost_priv(instance);
752 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
753
Finn Thain8d5dbec2016-02-23 10:07:09 +1100754 if (hostdata->sensing == cmd) {
Finn Thainf27db8e2016-01-03 16:06:00 +1100755 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
756 list_add(&ncmd->list, &hostdata->autosense);
757 hostdata->sensing = NULL;
758 } else
759 list_add(&ncmd->list, &hostdata->unissued);
760}
761
762/**
Finn Thain0d2cf862016-01-03 16:06:11 +1100763 * NCR5380_main - NCR state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
Finn Thain594d4ba2016-01-03 16:06:10 +1100765 * NCR5380_main is a coroutine that runs as long as more work can
766 * be done on the NCR5380 host adapters in a system. Both
767 * NCR5380_queue_command() and NCR5380_intr() will try to start it
768 * in case it is not running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
770
David Howellsc4028952006-11-22 14:57:56 +0000771static void NCR5380_main(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
David Howellsc4028952006-11-22 14:57:56 +0000773 struct NCR5380_hostdata *hostdata =
Finn Thain8d8601a2016-01-03 16:05:37 +1100774 container_of(work, struct NCR5380_hostdata, main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct Scsi_Host *instance = hostdata->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int done;
Finn Thainaff0cf92016-01-03 16:06:09 +1100777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 done = 1;
Finn Thain11d2f632016-01-03 16:05:51 +1100780
Finn Thain0a4e3612016-01-03 16:06:07 +1100781 spin_lock_irq(&hostdata->lock);
Finn Thainccf6efd2016-02-23 10:07:08 +1100782 while (!hostdata->connected && !hostdata->selecting) {
783 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
784
785 if (!cmd)
786 break;
Finn Thainf27db8e2016-01-03 16:06:00 +1100787
788 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
Finn Thainf27db8e2016-01-03 16:06:00 +1100791 * Attempt to establish an I_T_L nexus here.
792 * On success, instance->hostdata->connected is set.
793 * On failure, we must add the command back to the
794 * issue queue so we can keep trying.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Finn Thainf27db8e2016-01-03 16:06:00 +1100796 /*
797 * REQUEST SENSE commands are issued without tagged
798 * queueing, even on SCSI-II devices because the
799 * contingent allegiance condition exists for the
800 * entire unit.
801 */
Finn Thain32b26a12016-01-03 16:05:58 +1100802
Finn Thainccf6efd2016-02-23 10:07:08 +1100803 if (!NCR5380_select(instance, cmd)) {
Finn Thain707d62b2016-01-03 16:06:02 +1100804 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
Finn Thain52d3e562016-03-23 21:10:20 +1100805 maybe_release_dma_irq(instance);
Finn Thainf27db8e2016-01-03 16:06:00 +1100806 } else {
807 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
808 "main: select failed, returning %p to queue\n", cmd);
809 requeue_cmd(instance, cmd);
810 }
811 }
Finn Thaine4dec682016-03-23 21:10:12 +1100812 if (hostdata->connected && !hostdata->dma_len) {
Finn Thainb7465452016-01-03 16:06:05 +1100813 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 NCR5380_information_transfer(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 done = 0;
Finn Thain1d3db592016-01-03 16:05:24 +1100816 }
Finn Thain0a4e3612016-01-03 16:06:07 +1100817 spin_unlock_irq(&hostdata->lock);
818 if (!done)
819 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 } while (!done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Finn Thain8053b0e2016-03-23 21:10:19 +1100823/*
824 * NCR5380_dma_complete - finish DMA transfer
825 * @instance: the scsi host instance
826 *
827 * Called by the interrupt handler when DMA finishes or a phase
828 * mismatch occurs (which would end the DMA transfer).
829 */
830
831static void NCR5380_dma_complete(struct Scsi_Host *instance)
832{
833 struct NCR5380_hostdata *hostdata = shost_priv(instance);
834 int transferred;
835 unsigned char **data;
836 int *count;
837 int saved_data = 0, overrun = 0;
838 unsigned char p;
839
840 if (hostdata->read_overruns) {
841 p = hostdata->connected->SCp.phase;
842 if (p & SR_IO) {
843 udelay(10);
844 if ((NCR5380_read(BUS_AND_STATUS_REG) &
845 (BASR_PHASE_MATCH | BASR_ACK)) ==
846 (BASR_PHASE_MATCH | BASR_ACK)) {
847 saved_data = NCR5380_read(INPUT_DATA_REG);
848 overrun = 1;
849 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
850 }
851 }
852 }
853
Finn Thaine9db3192016-03-23 21:10:21 +1100854#ifdef CONFIG_SUN3
855 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
856 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
857 instance->host_no);
858 BUG();
859 }
860
861 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
862 (BASR_PHASE_MATCH | BASR_ACK)) {
863 pr_err("scsi%d: BASR %02x\n", instance->host_no,
864 NCR5380_read(BUS_AND_STATUS_REG));
865 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
866 instance->host_no);
867 BUG();
868 }
869#endif
870
Finn Thain8053b0e2016-03-23 21:10:19 +1100871 NCR5380_write(MODE_REG, MR_BASE);
872 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
873 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
874
875 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
876 hostdata->dma_len = 0;
877
878 data = (unsigned char **)&hostdata->connected->SCp.ptr;
879 count = &hostdata->connected->SCp.this_residual;
880 *data += transferred;
881 *count -= transferred;
882
883 if (hostdata->read_overruns) {
884 int cnt, toPIO;
885
886 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
887 cnt = toPIO = hostdata->read_overruns;
888 if (overrun) {
889 dsprintk(NDEBUG_DMA, instance,
890 "Got an input overrun, using saved byte\n");
891 *(*data)++ = saved_data;
892 (*count)--;
893 cnt--;
894 toPIO--;
895 }
896 if (toPIO > 0) {
897 dsprintk(NDEBUG_DMA, instance,
898 "Doing %d byte PIO to 0x%p\n", cnt, *data);
899 NCR5380_transfer_pio(instance, &p, &cnt, data);
900 *count -= toPIO - cnt;
901 }
902 }
903 }
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/**
Finn Thaincd400822016-01-03 16:05:40 +1100907 * NCR5380_intr - generic NCR5380 irq handler
908 * @irq: interrupt number
909 * @dev_id: device info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 *
Finn Thaincd400822016-01-03 16:05:40 +1100911 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
912 * from the disconnected queue, and restarting NCR5380_main()
913 * as required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 *
Finn Thaincd400822016-01-03 16:05:40 +1100915 * The chip can assert IRQ in any of six different conditions. The IRQ flag
916 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
917 * Three of these six conditions are latched in the Bus and Status Register:
918 * - End of DMA (cleared by ending DMA Mode)
919 * - Parity error (cleared by reading RPIR)
920 * - Loss of BSY (cleared by reading RPIR)
921 * Two conditions have flag bits that are not latched:
922 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
923 * - Bus reset (non-maskable)
924 * The remaining condition has no flag bit at all:
925 * - Selection/reselection
926 *
927 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
928 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
929 * claimed that "the design of the [DP8490] interrupt logic ensures
930 * interrupts will not be lost (they can be on the DP5380)."
931 * The L5380/53C80 datasheet from LOGIC Devices has more details.
932 *
933 * Checking for bus reset by reading RST is futile because of interrupt
934 * latency, but a bus reset will reset chip logic. Checking for parity error
935 * is unnecessary because that interrupt is never enabled. A Loss of BSY
936 * condition will clear DMA Mode. We can tell when this occurs because the
937 * the Busy Monitor interrupt is enabled together with DMA Mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
939
Finn Thaina46865d2016-03-23 21:10:27 +1100940static irqreturn_t __maybe_unused NCR5380_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jeff Garzikbaa9aac2007-12-13 16:14:14 -0800942 struct Scsi_Host *instance = dev_id;
Finn Thaincd400822016-01-03 16:05:40 +1100943 struct NCR5380_hostdata *hostdata = shost_priv(instance);
944 int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 unsigned char basr;
946 unsigned long flags;
947
Finn Thain11d2f632016-01-03 16:05:51 +1100948 spin_lock_irqsave(&hostdata->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Finn Thaincd400822016-01-03 16:05:40 +1100950 basr = NCR5380_read(BUS_AND_STATUS_REG);
951 if (basr & BASR_IRQ) {
952 unsigned char mr = NCR5380_read(MODE_REG);
953 unsigned char sr = NCR5380_read(STATUS_REG);
954
Finn Thainb7465452016-01-03 16:06:05 +1100955 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
956 irq, basr, sr, mr);
Finn Thaincd400822016-01-03 16:05:40 +1100957
Finn Thain8053b0e2016-03-23 21:10:19 +1100958 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
959 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
960 * We ack IRQ after clearing Mode Register. Workarounds
961 * for End of DMA errata need to happen in DMA Mode.
962 */
963
964 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
965
966 if (hostdata->connected) {
967 NCR5380_dma_complete(instance);
968 queue_work(hostdata->work_q, &hostdata->main_task);
969 } else {
970 NCR5380_write(MODE_REG, MR_BASE);
971 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
972 }
973 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
Finn Thaincd400822016-01-03 16:05:40 +1100974 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
975 /* Probably reselected */
976 NCR5380_write(SELECT_ENABLE_REG, 0);
977 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
978
Finn Thainb7465452016-01-03 16:06:05 +1100979 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
Finn Thaincd400822016-01-03 16:05:40 +1100980
981 if (!hostdata->connected) {
982 NCR5380_reselect(instance);
983 queue_work(hostdata->work_q, &hostdata->main_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Finn Thaincd400822016-01-03 16:05:40 +1100985 if (!hostdata->connected)
986 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
987 } else {
988 /* Probably Bus Reset */
989 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
990
Finn Thainb7465452016-01-03 16:06:05 +1100991 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100992#ifdef SUN3_SCSI_VME
993 dregs->csr |= CSR_DMA_ENABLE;
994#endif
Finn Thaincd400822016-01-03 16:05:40 +1100995 }
996 handled = 1;
997 } else {
998 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
Finn Thaine9db3192016-03-23 21:10:21 +1100999#ifdef SUN3_SCSI_VME
1000 dregs->csr |= CSR_DMA_ENABLE;
1001#endif
Finn Thaincd400822016-01-03 16:05:40 +11001002 }
1003
Finn Thain11d2f632016-01-03 16:05:51 +11001004 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thaincd400822016-01-03 16:05:40 +11001005
1006 return IRQ_RETVAL(handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Finn Thainaff0cf92016-01-03 16:06:09 +11001009/*
Finn Thain710ddd02014-11-12 16:12:02 +11001010 * Function : int NCR5380_select(struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001011 * struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 *
1013 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
Finn Thain594d4ba2016-01-03 16:06:10 +11001014 * including ARBITRATION, SELECTION, and initial message out for
1015 * IDENTIFY and queue messages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001017 * Inputs : instance - instantiation of the 5380 driver on which this
Finn Thain594d4ba2016-01-03 16:06:10 +11001018 * target lives, cmd - SCSI command to execute.
Finn Thainaff0cf92016-01-03 16:06:09 +11001019 *
Finn Thain707d62b2016-01-03 16:06:02 +11001020 * Returns cmd if selection failed but should be retried,
1021 * NULL if selection failed and should not be retried, or
1022 * NULL if selection succeeded (hostdata->connected == cmd).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001024 * Side effects :
Finn Thain594d4ba2016-01-03 16:06:10 +11001025 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1026 * with registers as they should have been on entry - ie
1027 * SELECT_ENABLE will be set appropriately, the NCR5380
1028 * will cease to drive any SCSI bus signals.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001030 * If successful : I_T_L or I_T_L_Q nexus will be established,
1031 * instance->connected will be set to cmd.
1032 * SELECT interrupt will be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001034 * If failed (no target) : cmd->scsi_done() will be called, and the
1035 * cmd->result host byte set to DID_BAD_TARGET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001037
Finn Thain707d62b2016-01-03 16:06:02 +11001038static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1039 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Finn Thaine8a60142016-01-03 16:05:54 +11001041 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 unsigned char tmp[3], phase;
1043 unsigned char *data;
1044 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
Finn Thainb7465452016-01-03 16:06:05 +11001048 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1049 instance->this_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Finn Thain707d62b2016-01-03 16:06:02 +11001051 /*
1052 * Arbitration and selection phases are slow and involve dropping the
1053 * lock, so we have to watch out for EH. An exception handler may
1054 * change 'selecting' to NULL. This function will then return NULL
1055 * so that the caller will forget about 'cmd'. (During information
1056 * transfer phases, EH may change 'connected' to NULL.)
1057 */
1058 hostdata->selecting = cmd;
1059
Finn Thainaff0cf92016-01-03 16:06:09 +11001060 /*
1061 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * data bus during SELECTION.
1063 */
1064
1065 NCR5380_write(TARGET_COMMAND_REG, 0);
1066
Finn Thainaff0cf92016-01-03 16:06:09 +11001067 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * Start arbitration.
1069 */
1070
1071 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1072 NCR5380_write(MODE_REG, MR_ARBITRATE);
1073
Finn Thain55500d92016-01-03 16:05:35 +11001074 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1075 * Bus Free Delay, arbitration will begin.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
1077
Finn Thain11d2f632016-01-03 16:05:51 +11001078 spin_unlock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001079 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1080 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1081 ICR_ARBITRATION_PROGRESS, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001082 spin_lock_irq(&hostdata->lock);
Finn Thainb32ade12016-01-03 16:05:41 +11001083 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1084 /* Reselection interrupt */
Finn Thain707d62b2016-01-03 16:06:02 +11001085 goto out;
Finn Thainb32ade12016-01-03 16:05:41 +11001086 }
Finn Thainccf6efd2016-02-23 10:07:08 +11001087 if (!hostdata->selecting) {
1088 /* Command was aborted */
1089 NCR5380_write(MODE_REG, MR_BASE);
1090 goto out;
1091 }
Finn Thainb32ade12016-01-03 16:05:41 +11001092 if (err < 0) {
1093 NCR5380_write(MODE_REG, MR_BASE);
1094 shost_printk(KERN_ERR, instance,
1095 "select: arbitration timeout\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001096 goto out;
Finn Thain55500d92016-01-03 16:05:35 +11001097 }
Finn Thain11d2f632016-01-03 16:05:51 +11001098 spin_unlock_irq(&hostdata->lock);
Finn Thain55500d92016-01-03 16:05:35 +11001099
1100 /* The SCSI-2 arbitration delay is 2.4 us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 udelay(3);
1102
1103 /* Check for lost arbitration */
Finn Thain0d2cf862016-01-03 16:06:11 +11001104 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1105 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1106 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 NCR5380_write(MODE_REG, MR_BASE);
Finn Thainb7465452016-01-03 16:06:05 +11001108 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
Finn Thain11d2f632016-01-03 16:05:51 +11001109 spin_lock_irq(&hostdata->lock);
Finn Thain707d62b2016-01-03 16:06:02 +11001110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Finn Thaincf13b082016-01-03 16:05:18 +11001112
1113 /* After/during arbitration, BSY should be asserted.
1114 * IBM DPES-31080 Version S31Q works now
1115 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1116 */
1117 NCR5380_write(INITIATOR_COMMAND_REG,
1118 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Finn Thainaff0cf92016-01-03 16:06:09 +11001120 /*
1121 * Again, bus clear + bus settle time is 1.2us, however, this is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * a minimum so we'll udelay ceil(1.2)
1123 */
1124
Finn Thain9c3f0e22016-01-03 16:05:11 +11001125 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1126 udelay(15);
1127 else
1128 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Finn Thain11d2f632016-01-03 16:05:51 +11001130 spin_lock_irq(&hostdata->lock);
1131
Finn Thain72064a72016-01-03 16:05:44 +11001132 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1133 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
Finn Thain707d62b2016-01-03 16:06:02 +11001134 goto out;
1135
1136 if (!hostdata->selecting) {
1137 NCR5380_write(MODE_REG, MR_BASE);
1138 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1139 goto out;
1140 }
Finn Thain72064a72016-01-03 16:05:44 +11001141
Finn Thainb7465452016-01-03 16:06:05 +11001142 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Finn Thainaff0cf92016-01-03 16:06:09 +11001144 /*
1145 * Now that we have won arbitration, start Selection process, asserting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * the host and target ID's on the SCSI bus.
1147 */
1148
Finn Thain3d07d222016-01-03 16:06:13 +11001149 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Finn Thainaff0cf92016-01-03 16:06:09 +11001151 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * Raise ATN while SEL is true before BSY goes false from arbitration,
1153 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1154 * phase immediately after selection.
1155 */
1156
Finn Thain3d07d222016-01-03 16:06:13 +11001157 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1158 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 NCR5380_write(MODE_REG, MR_BASE);
1160
Finn Thainaff0cf92016-01-03 16:06:09 +11001161 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * Reselect interrupts must be turned off prior to the dropping of BSY,
1163 * otherwise we will trigger an interrupt.
1164 */
1165 NCR5380_write(SELECT_ENABLE_REG, 0);
1166
Finn Thain11d2f632016-01-03 16:05:51 +11001167 spin_unlock_irq(&hostdata->lock);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001170 * The initiator shall then wait at least two deskew delays and release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * the BSY signal.
1172 */
Finn Thain0d2cf862016-01-03 16:06:11 +11001173 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* Reset BSY */
Finn Thain3d07d222016-01-03 16:06:13 +11001176 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1177 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Finn Thainaff0cf92016-01-03 16:06:09 +11001179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * Something weird happens when we cease to drive BSY - looks
Finn Thainaff0cf92016-01-03 16:06:09 +11001181 * like the board/chip is letting us do another read before the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * appropriate propagation delay has expired, and we're confusing
1183 * a BSY signal from ourselves as the target's response to SELECTION.
1184 *
1185 * A small delay (the 'C++' frontend breaks the pipeline with an
1186 * unnecessary jump, making it work on my 386-33/Trantor T128, the
Finn Thainaff0cf92016-01-03 16:06:09 +11001187 * tighter 'C' code breaks and requires this) solves the problem -
1188 * the 1 us delay is arbitrary, and only used because this delay will
1189 * be the same on other platforms and since it works here, it should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * work there.
1191 *
1192 * wingel suggests that this could be due to failing to wait
1193 * one deskew delay.
1194 */
1195
1196 udelay(1);
1197
Finn Thainb7465452016-01-03 16:06:05 +11001198 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Finn Thainaff0cf92016-01-03 16:06:09 +11001200 /*
1201 * The SCSI specification calls for a 250 ms timeout for the actual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 * selection.
1203 */
1204
Finn Thainae753a32016-01-03 16:05:23 +11001205 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1206 msecs_to_jiffies(250));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
Finn Thain11d2f632016-01-03 16:05:51 +11001209 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1211 NCR5380_reselect(instance);
Finn Thaincd400822016-01-03 16:05:40 +11001212 if (!hostdata->connected)
1213 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001214 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
Finn Thain707d62b2016-01-03 16:06:02 +11001215 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Finn Thainae753a32016-01-03 16:05:23 +11001217
1218 if (err < 0) {
Finn Thain11d2f632016-01-03 16:05:51 +11001219 spin_lock_irq(&hostdata->lock);
Finn Thainae753a32016-01-03 16:05:23 +11001220 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thainae753a32016-01-03 16:05:23 +11001221 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001222 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1223 if (hostdata->selecting) {
1224 cmd->result = DID_BAD_TARGET << 16;
1225 complete_cmd(instance, cmd);
1226 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1227 cmd = NULL;
1228 }
1229 goto out;
Finn Thainae753a32016-01-03 16:05:23 +11001230 }
1231
Finn Thainaff0cf92016-01-03 16:06:09 +11001232 /*
1233 * No less than two deskew delays after the initiator detects the
1234 * BSY signal is true, it shall release the SEL signal and may
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * change the DATA BUS. -wingel
1236 */
1237
1238 udelay(1);
1239
1240 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001243 * Since we followed the SCSI spec, and raised ATN while SEL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * was true but before BSY was false during selection, the information
1245 * transfer phase should be a MESSAGE OUT phase so that we can send the
1246 * IDENTIFY message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
1248
1249 /* Wait for start of REQ/ACK handshake */
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11001252 spin_lock_irq(&hostdata->lock);
Finn Thain1cc160e2016-01-03 16:05:32 +11001253 if (err < 0) {
Finn Thain55500d92016-01-03 16:05:35 +11001254 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1255 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain707d62b2016-01-03 16:06:02 +11001257 goto out;
1258 }
1259 if (!hostdata->selecting) {
1260 do_abort(instance);
1261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
Finn Thainb7465452016-01-03 16:06:05 +11001264 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1265 scmd_id(cmd));
Finn Thain22f5f102014-11-12 16:11:56 +11001266 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 data = tmp;
1270 phase = PHASE_MSGOUT;
1271 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001272 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* XXX need to handle errors here */
Finn Thain11d2f632016-01-03 16:05:51 +11001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 hostdata->connected = cmd;
Finn Thain3d07d222016-01-03 16:06:13 +11001276 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Finn Thaine9db3192016-03-23 21:10:21 +11001278#ifdef SUN3_SCSI_VME
1279 dregs->csr |= CSR_INTR;
1280#endif
1281
Boaz Harrosh28424d32007-09-10 22:37:45 +03001282 initialize_SCp(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Finn Thain707d62b2016-01-03 16:06:02 +11001284 cmd = NULL;
1285
1286out:
1287 if (!hostdata->selecting)
1288 return NULL;
1289 hostdata->selecting = NULL;
1290 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Finn Thainaff0cf92016-01-03 16:06:09 +11001293/*
1294 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001295 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 *
1297 * Purpose : transfers data in given phase using polled I/O
1298 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001299 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001300 * what phase is expected, *count - pointer to number of
1301 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 * Returns : -1 when different phase is entered without transferring
Finn Thain0d2cf862016-01-03 16:06:11 +11001304 * maximum number of bytes, 0 if all bytes are transferred or exit
Finn Thain594d4ba2016-01-03 16:06:10 +11001305 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001307 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 *
1309 * XXX Note : handling for bus free may be useful.
1310 */
1311
1312/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001313 * Note : this code is not as quick as it could be, however it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * IS 100% reliable, and for the actual data transfer where speed
1315 * counts, we will always do a pseudo DMA or DMA transfer.
1316 */
1317
Finn Thain0d2cf862016-01-03 16:06:11 +11001318static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1319 unsigned char *phase, int *count,
1320 unsigned char **data)
1321{
Finn Thain61e1ce52016-10-10 00:46:53 -04001322 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned char p = *phase, tmp;
1324 int c = *count;
1325 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Finn Thainaff0cf92016-01-03 16:06:09 +11001327 /*
1328 * The NCR5380 chip will only drive the SCSI bus when the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * phase specified in the appropriate bits of the TARGET COMMAND
1330 * REGISTER match the STATUS REGISTER
1331 */
1332
Finn Thain0d2cf862016-01-03 16:06:11 +11001333 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 do {
Finn Thainaff0cf92016-01-03 16:06:09 +11001336 /*
1337 * Wait for assertion of REQ, after which the phase bits will be
1338 * valid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
1340
Finn Thain686f3992016-01-03 16:05:26 +11001341 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Finn Thainb7465452016-01-03 16:06:05 +11001344 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* Check for phase mismatch */
Finn Thain686f3992016-01-03 16:05:26 +11001347 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
Finn Thainb7465452016-01-03 16:06:05 +11001348 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1349 NCR5380_dprint_phase(NDEBUG_PIO, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 break;
1351 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* Do actual transfer from SCSI bus to / from memory */
1354 if (!(p & SR_IO))
1355 NCR5380_write(OUTPUT_DATA_REG, *d);
1356 else
1357 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1358
1359 ++d;
1360
Finn Thainaff0cf92016-01-03 16:06:09 +11001361 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * The SCSI standard suggests that in MSGOUT phase, the initiator
1363 * should drop ATN on the last byte of the message phase
1364 * after REQ has been asserted for the handshake but before
1365 * the initiator raises ACK.
1366 */
1367
1368 if (!(p & SR_IO)) {
1369 if (!((p & SR_MSG) && c > 1)) {
1370 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1371 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001372 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1373 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } else {
Finn Thain3d07d222016-01-03 16:06:13 +11001375 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1376 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 NCR5380_dprint(NDEBUG_PIO, instance);
Finn Thain3d07d222016-01-03 16:06:13 +11001378 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1379 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 } else {
1382 NCR5380_dprint(NDEBUG_PIO, instance);
1383 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1384 }
1385
Finn Thaina2edc4a2016-01-03 16:05:27 +11001386 if (NCR5380_poll_politely(instance,
1387 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1388 break;
1389
Finn Thainb7465452016-01-03 16:06:05 +11001390 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392/*
Finn Thainaff0cf92016-01-03 16:06:09 +11001393 * We have several special cases to consider during REQ/ACK handshaking :
1394 * 1. We were in MSGOUT phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001395 * message. ATN must be dropped as ACK is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001397 * 2. We are in a MSGIN phase, and we are on the last byte of the
Finn Thain594d4ba2016-01-03 16:06:10 +11001398 * message. We must exit with ACK asserted, so that the calling
1399 * code may raise ATN before dropping ACK to reject the message.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
1401 * 3. ACK and ATN are clear and the target may proceed as normal.
1402 */
1403 if (!(p == PHASE_MSGIN && c == 1)) {
1404 if (p == PHASE_MSGOUT && c > 1)
1405 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1406 else
1407 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1408 }
1409 } while (--c);
1410
Finn Thainb7465452016-01-03 16:06:05 +11001411 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 *count = c;
1414 *data = d;
1415 tmp = NCR5380_read(STATUS_REG);
Finn Thaina2edc4a2016-01-03 16:05:27 +11001416 /* The phase read from the bus is valid if either REQ is (already)
1417 * asserted or if ACK hasn't been released yet. The latter applies if
1418 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1419 */
1420 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *phase = tmp & PHASE_MASK;
1422 else
1423 *phase = PHASE_UNKNOWN;
1424
1425 if (!c || (*phase == p))
1426 return 0;
1427 else
1428 return -1;
1429}
1430
1431/**
Finn Thain636b1ec2016-01-03 16:05:10 +11001432 * do_reset - issue a reset command
1433 * @instance: adapter to reset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001435 * Issue a reset sequence to the NCR5380 and try and get the bus
1436 * back into sane shape.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 *
Finn Thain636b1ec2016-01-03 16:05:10 +11001438 * This clears the reset interrupt flag because there may be no handler for
1439 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1440 * been installed. And when in EH we may have released the ST DMA interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
Finn Thainaff0cf92016-01-03 16:06:09 +11001442
Finn Thain54d8fe42016-01-03 16:05:06 +11001443static void do_reset(struct Scsi_Host *instance)
1444{
Finn Thain61e1ce52016-10-10 00:46:53 -04001445 struct NCR5380_hostdata __maybe_unused *hostdata = shost_priv(instance);
Finn Thain636b1ec2016-01-03 16:05:10 +11001446 unsigned long flags;
1447
1448 local_irq_save(flags);
1449 NCR5380_write(TARGET_COMMAND_REG,
1450 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Finn Thain636b1ec2016-01-03 16:05:10 +11001452 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain636b1ec2016-01-03 16:05:10 +11001454 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1455 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Finn Thain80d3eb62016-01-03 16:05:34 +11001458/**
1459 * do_abort - abort the currently established nexus by going to
1460 * MESSAGE OUT phase and sending an ABORT message.
1461 * @instance: relevant scsi host instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
Finn Thain80d3eb62016-01-03 16:05:34 +11001463 * Returns 0 on success, -1 on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465
Finn Thain54d8fe42016-01-03 16:05:06 +11001466static int do_abort(struct Scsi_Host *instance)
1467{
Finn Thain61e1ce52016-10-10 00:46:53 -04001468 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 unsigned char *msgptr, phase, tmp;
1470 int len;
1471 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* Request message out phase */
1474 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1475
Finn Thainaff0cf92016-01-03 16:06:09 +11001476 /*
1477 * Wait for the target to indicate a valid phase by asserting
1478 * REQ. Once this happens, we'll have either a MSGOUT phase
1479 * and can immediately send the ABORT message, or we'll have some
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * other phase and will have to source/sink data.
Finn Thainaff0cf92016-01-03 16:06:09 +11001481 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * We really don't care what value was on the bus or what value
1483 * the target sees, so we just handshake.
1484 */
1485
Finn Thain80d3eb62016-01-03 16:05:34 +11001486 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001487 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001488 goto timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Finn Thainf35d3472016-01-03 16:05:33 +11001490 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
Finn Thainaff0cf92016-01-03 16:06:09 +11001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1493
Finn Thainf35d3472016-01-03 16:05:33 +11001494 if (tmp != PHASE_MSGOUT) {
Finn Thain0d2cf862016-01-03 16:06:11 +11001495 NCR5380_write(INITIATOR_COMMAND_REG,
1496 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
Finn Thain54d8fe42016-01-03 16:05:06 +11001497 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
Finn Thain1cc160e2016-01-03 16:05:32 +11001498 if (rc < 0)
Finn Thain80d3eb62016-01-03 16:05:34 +11001499 goto timeout;
1500 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 tmp = ABORT;
1504 msgptr = &tmp;
1505 len = 1;
1506 phase = PHASE_MSGOUT;
Finn Thain54d8fe42016-01-03 16:05:06 +11001507 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /*
1510 * If we got here, and the command completed successfully,
1511 * we're about to go into bus free state.
1512 */
1513
1514 return len ? -1 : 0;
Finn Thain80d3eb62016-01-03 16:05:34 +11001515
1516timeout:
1517 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1518 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Finn Thainaff0cf92016-01-03 16:06:09 +11001521/*
1522 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
Finn Thain594d4ba2016-01-03 16:06:10 +11001523 * unsigned char *phase, int *count, unsigned char **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 *
1525 * Purpose : transfers data in given phase using either real
Finn Thain594d4ba2016-01-03 16:06:10 +11001526 * or pseudo DMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001528 * Inputs : instance - instance of driver, *phase - pointer to
Finn Thain594d4ba2016-01-03 16:06:10 +11001529 * what phase is expected, *count - pointer to number of
1530 * bytes to transfer, **data - pointer to data pointer.
Finn Thainaff0cf92016-01-03 16:06:09 +11001531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * Returns : -1 when different phase is entered without transferring
Finn Thain594d4ba2016-01-03 16:06:10 +11001533 * maximum number of bytes, 0 if all bytes or transferred or exit
1534 * is in same phase.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
Finn Thain594d4ba2016-01-03 16:06:10 +11001536 * Also, *phase, *count, *data are modified in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
1538
1539
Finn Thain0d2cf862016-01-03 16:06:11 +11001540static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1541 unsigned char *phase, int *count,
1542 unsigned char **data)
1543{
1544 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thainf0ea73a2016-03-23 21:10:26 +11001545 int c = *count;
1546 unsigned char p = *phase;
1547 unsigned char *d = *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 unsigned char tmp;
Finn Thain8053b0e2016-03-23 21:10:19 +11001549 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1552 *phase = tmp;
1553 return -1;
1554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Finn Thain8053b0e2016-03-23 21:10:19 +11001556 hostdata->connected->SCp.phase = p;
1557
1558 if (p & SR_IO) {
1559 if (hostdata->read_overruns)
1560 c -= hostdata->read_overruns;
1561 else if (hostdata->flags & FLAG_DMA_FIXUP)
1562 --c;
1563 }
1564
1565 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1566 (p & SR_IO) ? "receive" : "send", c, d);
1567
Finn Thaine9db3192016-03-23 21:10:21 +11001568#ifdef CONFIG_SUN3
1569 /* send start chain */
1570 sun3scsi_dma_start(c, *data);
1571#endif
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
Finn Thain8053b0e2016-03-23 21:10:19 +11001574 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1575 MR_ENABLE_EOP_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Finn Thain8053b0e2016-03-23 21:10:19 +11001577 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1578 /* On the Medusa, it is a must to initialize the DMA before
1579 * starting the NCR. This is also the cleaner way for the TT.
1580 */
1581 if (p & SR_IO)
1582 result = NCR5380_dma_recv_setup(instance, d, c);
1583 else
1584 result = NCR5380_dma_send_setup(instance, d, c);
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Finn Thainaff0cf92016-01-03 16:06:09 +11001587 /*
Finn Thain594d4ba2016-01-03 16:06:10 +11001588 * On the PAS16 at least I/O recovery delays are not needed here.
1589 * Everyone else seems to want them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 */
1591
1592 if (p & SR_IO) {
Finn Thaine9db3192016-03-23 21:10:21 +11001593 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thaine5d55d12016-03-23 21:10:16 +11001594 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1596 } else {
Finn Thaine5d55d12016-03-23 21:10:16 +11001597 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
Finn Thaine5d55d12016-03-23 21:10:16 +11001599 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 NCR5380_write(START_DMA_SEND_REG, 0);
Finn Thaine5d55d12016-03-23 21:10:16 +11001601 NCR5380_io_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603
Finn Thaine9db3192016-03-23 21:10:21 +11001604#ifdef CONFIG_SUN3
1605#ifdef SUN3_SCSI_VME
1606 dregs->csr |= CSR_DMA_ENABLE;
1607#endif
1608 sun3_dma_active = 1;
1609#endif
1610
Finn Thain8053b0e2016-03-23 21:10:19 +11001611 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1612 /* On the Falcon, the DMA setup must be done after the last
1613 * NCR access, else the DMA setup gets trashed!
1614 */
1615 if (p & SR_IO)
1616 result = NCR5380_dma_recv_setup(instance, d, c);
1617 else
1618 result = NCR5380_dma_send_setup(instance, d, c);
1619 }
1620
1621 /* On failure, NCR5380_dma_xxxx_setup() returns a negative int. */
1622 if (result < 0)
1623 return result;
1624
1625 /* For real DMA, result is the byte count. DMA interrupt is expected. */
1626 if (result > 0) {
1627 hostdata->dma_len = result;
1628 return 0;
1629 }
1630
1631 /* The result is zero iff pseudo DMA send/receive was completed. */
1632 hostdata->dma_len = c;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/*
Finn Thaine4dec682016-03-23 21:10:12 +11001635 * A note regarding the DMA errata workarounds for early NMOS silicon.
Finn Thainc16df322016-01-03 16:06:08 +11001636 *
1637 * For DMA sends, we want to wait until the last byte has been
1638 * transferred out over the bus before we turn off DMA mode. Alas, there
1639 * seems to be no terribly good way of doing this on a 5380 under all
1640 * conditions. For non-scatter-gather operations, we can wait until REQ
1641 * and ACK both go false, or until a phase mismatch occurs. Gather-sends
1642 * are nastier, since the device will be expecting more data than we
1643 * are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
1644 * could test Last Byte Sent to assure transfer (I imagine this is precisely
1645 * why this signal was added to the newer chips) but on the older 538[01]
1646 * this signal does not exist. The workaround for this lack is a watchdog;
1647 * we bail out of the wait-loop after a modest amount of wait-time if
1648 * the usual exit conditions are not met. Not a terribly clean or
1649 * correct solution :-%
1650 *
1651 * DMA receive is equally tricky due to a nasty characteristic of the NCR5380.
1652 * If the chip is in DMA receive mode, it will respond to a target's
1653 * REQ by latching the SCSI data into the INPUT DATA register and asserting
1654 * ACK, even if it has _already_ been notified by the DMA controller that
1655 * the current DMA transfer has completed! If the NCR5380 is then taken
1656 * out of DMA mode, this already-acknowledged byte is lost. This is
1657 * not a problem for "one DMA transfer per READ command", because
1658 * the situation will never arise... either all of the data is DMA'ed
1659 * properly, or the target switches to MESSAGE IN phase to signal a
1660 * disconnection (either operation bringing the DMA to a clean halt).
1661 * However, in order to handle scatter-receive, we must work around the
Finn Thaine4dec682016-03-23 21:10:12 +11001662 * problem. The chosen fix is to DMA fewer bytes, then check for the
Finn Thainc16df322016-01-03 16:06:08 +11001663 * condition before taking the NCR5380 out of DMA mode. One or two extra
1664 * bytes are transferred via PIO as necessary to fill out the original
1665 * request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
1667
Finn Thain8053b0e2016-03-23 21:10:19 +11001668 if (hostdata->flags & FLAG_DMA_FIXUP) {
1669 if (p & SR_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
Finn Thaine4dec682016-03-23 21:10:12 +11001671 * The workaround was to transfer fewer bytes than we
Finn Thainaff0cf92016-01-03 16:06:09 +11001672 * intended to with the pseudo-DMA read function, wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * the chip to latch the last byte, read it, and then disable
1674 * pseudo-DMA mode.
Finn Thainaff0cf92016-01-03 16:06:09 +11001675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
1677 * REQ is deasserted when ACK is asserted, and not reasserted
1678 * until ACK goes false. Since the NCR5380 won't lower ACK
1679 * until DACK is asserted, which won't happen unless we twiddle
Finn Thainaff0cf92016-01-03 16:06:09 +11001680 * the DMA port or we take the NCR5380 out of DMA mode, we
1681 * can guarantee that we won't handshake another extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * byte.
1683 */
1684
Finn Thain55181be2016-01-03 16:05:42 +11001685 if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG,
1686 BASR_DRQ, BASR_DRQ, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001687 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001688 shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
Finn Thain55181be2016-01-03 16:05:42 +11001690 if (NCR5380_poll_politely(instance, STATUS_REG,
1691 SR_REQ, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001692 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001693 shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n");
1694 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001695 d[*count - 1] = NCR5380_read(INPUT_DATA_REG);
1696 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001698 * Wait for the last byte to be sent. If REQ is being asserted for
1699 * the byte we're interested, we'll ACK it and it will go false.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 */
Finn Thain55181be2016-01-03 16:05:42 +11001701 if (NCR5380_poll_politely2(instance,
1702 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
1703 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) {
Finn Thain438af512016-03-23 21:10:18 +11001704 result = -1;
Finn Thain55181be2016-01-03 16:05:42 +11001705 shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Finn Thain8053b0e2016-03-23 21:10:19 +11001709
1710 NCR5380_dma_complete(instance);
Finn Thain438af512016-03-23 21:10:18 +11001711 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714/*
1715 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1716 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001717 * Purpose : run through the various SCSI phases and do as the target
Finn Thain594d4ba2016-01-03 16:06:10 +11001718 * directs us to. Operates on the currently connected command,
1719 * instance->connected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 *
1721 * Inputs : instance, instance for which we are doing commands
1722 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001723 * Side effects : SCSI things happen, the disconnected queue will be
Finn Thain594d4ba2016-01-03 16:06:10 +11001724 * modified if a command disconnects, *instance->connected will
1725 * change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001727 * XXX Note : we need to watch for bus free or a reset condition here
Finn Thain594d4ba2016-01-03 16:06:10 +11001728 * to recover from an unexpected bus free condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 */
1730
Finn Thain0d2cf862016-01-03 16:06:11 +11001731static void NCR5380_information_transfer(struct Scsi_Host *instance)
1732{
Finn Thaine8a60142016-01-03 16:05:54 +11001733 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 unsigned char msgout = NOP;
1735 int sink = 0;
1736 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int transfersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 unsigned char *data;
1739 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
Finn Thain11d2f632016-01-03 16:05:51 +11001740 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Finn Thaine9db3192016-03-23 21:10:21 +11001742#ifdef SUN3_SCSI_VME
1743 dregs->csr |= CSR_INTR;
1744#endif
1745
Finn Thain11d2f632016-01-03 16:05:51 +11001746 while ((cmd = hostdata->connected)) {
Finn Thain32b26a12016-01-03 16:05:58 +11001747 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 tmp = NCR5380_read(STATUS_REG);
1750 /* We only have a valid SCSI phase when REQ is asserted */
1751 if (tmp & SR_REQ) {
1752 phase = (tmp & PHASE_MASK);
1753 if (phase != old_phase) {
1754 old_phase = phase;
1755 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1756 }
Finn Thaine9db3192016-03-23 21:10:21 +11001757#ifdef CONFIG_SUN3
1758 if (phase == PHASE_CMDOUT) {
1759 void *d;
1760 unsigned long count;
1761
1762 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1763 count = cmd->SCp.buffer->length;
1764 d = sg_virt(cmd->SCp.buffer);
1765 } else {
1766 count = cmd->SCp.this_residual;
1767 d = cmd->SCp.ptr;
1768 }
1769
1770 if (sun3_dma_setup_done != cmd &&
1771 sun3scsi_dma_xfer_len(count, cmd) > 0) {
1772 sun3scsi_dma_setup(instance, d, count,
1773 rq_data_dir(cmd->request));
1774 sun3_dma_setup_done = cmd;
1775 }
1776#ifdef SUN3_SCSI_VME
1777 dregs->csr |= CSR_INTR;
1778#endif
1779 }
1780#endif /* CONFIG_SUN3 */
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (sink && (phase != PHASE_MSGOUT)) {
1783 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1784
Finn Thain3d07d222016-01-03 16:06:13 +11001785 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1786 ICR_ASSERT_ACK);
Finn Thain0d2cf862016-01-03 16:06:11 +11001787 while (NCR5380_read(STATUS_REG) & SR_REQ)
1788 ;
Finn Thain3d07d222016-01-03 16:06:13 +11001789 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1790 ICR_ASSERT_ATN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 sink = 0;
1792 continue;
1793 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 switch (phase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 case PHASE_DATAOUT:
1797#if (NDEBUG & NDEBUG_NO_DATAOUT)
Finn Thain6a6ff4a2016-01-03 16:06:04 +11001798 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 sink = 1;
1800 do_abort(instance);
1801 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11001802 complete_cmd(instance, cmd);
Finn Thaindc183962016-02-23 10:07:07 +11001803 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return;
1805#endif
Finn Thainbf1a0c62016-01-03 16:05:47 +11001806 case PHASE_DATAIN:
Finn Thainaff0cf92016-01-03 16:06:09 +11001807 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * If there is no room left in the current buffer in the
1809 * scatter-gather list, move onto the next one.
1810 */
1811
1812 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1813 ++cmd->SCp.buffer;
1814 --cmd->SCp.buffers_residual;
1815 cmd->SCp.this_residual = cmd->SCp.buffer->length;
Jens Axboe45711f12007-10-22 21:19:53 +02001816 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
Finn Thainb7465452016-01-03 16:06:05 +11001817 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1818 cmd->SCp.this_residual,
1819 cmd->SCp.buffers_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Finn Thain0d2cf862016-01-03 16:06:11 +11001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
Finn Thainaff0cf92016-01-03 16:06:09 +11001823 * The preferred transfer method is going to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 * PSEUDO-DMA for systems that are strictly PIO,
1825 * since we can let the hardware do the handshaking.
1826 *
1827 * For this to work, we need to know the transfersize
1828 * ahead of time, since the pseudo-DMA code will sit
1829 * in an unconditional loop.
1830 */
1831
Finn Thainff3d4572016-01-03 16:05:25 +11001832 transfersize = 0;
Finn Thain7e9ec8d2016-03-23 21:10:11 +11001833 if (!cmd->device->borken)
Finn Thainff3d4572016-01-03 16:05:25 +11001834 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Finn Thain438af512016-03-23 21:10:18 +11001836 if (transfersize > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 len = transfersize;
Finn Thain0d2cf862016-01-03 16:06:11 +11001838 if (NCR5380_transfer_dma(instance, &phase,
1839 &len, (unsigned char **)&cmd->SCp.ptr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11001841 * If the watchdog timer fires, all future
1842 * accesses to this device will use the
1843 * polled-IO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 */
Jeff Garzik017560f2005-10-24 18:04:36 -04001845 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11001846 "switching to slow handshake\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 cmd->device->borken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 sink = 1;
1849 do_abort(instance);
1850 cmd->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* XXX - need to source or sink data here, as appropriate */
Finn Thain8053b0e2016-03-23 21:10:19 +11001852 }
Finn Thainf825e402016-03-23 21:10:15 +11001853 } else {
Finn Thain08348b12016-08-31 14:44:56 +10001854 /* Transfer a small chunk so that the
1855 * irq mode lock is not held too long.
Finn Thain16788472016-02-23 10:07:05 +11001856 */
Finn Thain08348b12016-08-31 14:44:56 +10001857 transfersize = min(cmd->SCp.this_residual,
1858 NCR5380_PIO_CHUNK_SIZE);
Finn Thain16788472016-02-23 10:07:05 +11001859 len = transfersize;
1860 NCR5380_transfer_pio(instance, &phase, &len,
Finn Thain3d07d222016-01-03 16:06:13 +11001861 (unsigned char **)&cmd->SCp.ptr);
Finn Thain16788472016-02-23 10:07:05 +11001862 cmd->SCp.this_residual -= transfersize - len;
Finn Thain11d2f632016-01-03 16:05:51 +11001863 }
Finn Thaine9db3192016-03-23 21:10:21 +11001864#ifdef CONFIG_SUN3
1865 if (sun3_dma_setup_done == cmd)
1866 sun3_dma_setup_done = NULL;
1867#endif
Finn Thain16788472016-02-23 10:07:05 +11001868 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 case PHASE_MSGIN:
1870 len = 1;
1871 data = &tmp;
1872 NCR5380_transfer_pio(instance, &phase, &len, &data);
1873 cmd->SCp.Message = tmp;
1874
1875 switch (tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 case ABORT:
1877 case COMMAND_COMPLETE:
1878 /* Accept message by clearing ACK */
1879 sink = 1;
1880 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001881 dsprintk(NDEBUG_QUEUES, instance,
1882 "COMMAND COMPLETE %p target %d lun %llu\n",
1883 cmd, scmd_id(cmd), cmd->device->lun);
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 hostdata->connected = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Finn Thainf27db8e2016-01-03 16:06:00 +11001887 cmd->result &= ~0xffff;
1888 cmd->result |= cmd->SCp.Status;
1889 cmd->result |= cmd->SCp.Message << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Finn Thainf27db8e2016-01-03 16:06:00 +11001891 if (cmd->cmnd[0] == REQUEST_SENSE)
Finn Thain677e0192016-01-03 16:05:59 +11001892 complete_cmd(instance, cmd);
Finn Thainf27db8e2016-01-03 16:06:00 +11001893 else {
1894 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
1895 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
1896 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
1897 cmd);
1898 list_add_tail(&ncmd->list,
1899 &hostdata->autosense);
1900 } else
1901 complete_cmd(instance, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
Finn Thainaff0cf92016-01-03 16:06:09 +11001904 /*
1905 * Restore phase bits to 0 so an interrupted selection,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 * arbitration can resume.
1907 */
1908 NCR5380_write(TARGET_COMMAND_REG, 0);
Finn Thain72064a72016-01-03 16:05:44 +11001909
1910 /* Enable reselect interrupts */
1911 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thain52d3e562016-03-23 21:10:20 +11001912
1913 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return;
1915 case MESSAGE_REJECT:
1916 /* Accept message by clearing ACK */
1917 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1918 switch (hostdata->last_message) {
1919 case HEAD_OF_QUEUE_TAG:
1920 case ORDERED_QUEUE_TAG:
1921 case SIMPLE_QUEUE_TAG:
1922 cmd->device->simple_tags = 0;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001923 hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925 default:
1926 break;
1927 }
Finn Thain340b9612016-01-03 16:05:31 +11001928 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11001929 case DISCONNECT:
1930 /* Accept message by clearing ACK */
1931 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1932 hostdata->connected = NULL;
1933 list_add(&ncmd->list, &hostdata->disconnected);
1934 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
1935 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
1936 cmd, scmd_id(cmd), cmd->device->lun);
Finn Thain0d3d9a42016-01-03 16:05:55 +11001937
Finn Thain0d2cf862016-01-03 16:06:11 +11001938 /*
1939 * Restore phase bits to 0 so an interrupted selection,
1940 * arbitration can resume.
1941 */
1942 NCR5380_write(TARGET_COMMAND_REG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Finn Thain0d2cf862016-01-03 16:06:11 +11001944 /* Enable reselect interrupts */
1945 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
Finn Thaine9db3192016-03-23 21:10:21 +11001946#ifdef SUN3_SCSI_VME
1947 dregs->csr |= CSR_DMA_ENABLE;
1948#endif
Finn Thain0d2cf862016-01-03 16:06:11 +11001949 return;
Finn Thainaff0cf92016-01-03 16:06:09 +11001950 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
Finn Thainaff0cf92016-01-03 16:06:09 +11001952 * operation, in violation of the SCSI spec so we can safely
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * ignore SAVE/RESTORE pointers calls.
1954 *
Finn Thainaff0cf92016-01-03 16:06:09 +11001955 * Unfortunately, some disks violate the SCSI spec and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * don't issue the required SAVE_POINTERS message before
Finn Thainaff0cf92016-01-03 16:06:09 +11001957 * disconnecting, and we have to break spec to remain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * compatible.
1959 */
1960 case SAVE_POINTERS:
1961 case RESTORE_POINTERS:
1962 /* Accept message by clearing ACK */
1963 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1964 break;
1965 case EXTENDED_MESSAGE:
Finn Thainc16df322016-01-03 16:06:08 +11001966 /*
1967 * Start the message buffer with the EXTENDED_MESSAGE
1968 * byte, since spi_print_msg() wants the whole thing.
1969 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 extended_msg[0] = EXTENDED_MESSAGE;
1971 /* Accept first byte by clearing ACK */
1972 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
Finn Thain11d2f632016-01-03 16:05:51 +11001973
1974 spin_unlock_irq(&hostdata->lock);
1975
Finn Thainb7465452016-01-03 16:06:05 +11001976 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 len = 2;
1979 data = extended_msg + 1;
1980 phase = PHASE_MSGIN;
1981 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001982 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
1983 (int)extended_msg[1],
1984 (int)extended_msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Finn Thaine0783ed2016-01-03 16:05:45 +11001986 if (!len && extended_msg[1] > 0 &&
1987 extended_msg[1] <= sizeof(extended_msg) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* Accept third byte by clearing ACK */
1989 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1990 len = extended_msg[1] - 1;
1991 data = extended_msg + 3;
1992 phase = PHASE_MSGIN;
1993
1994 NCR5380_transfer_pio(instance, &phase, &len, &data);
Finn Thainb7465452016-01-03 16:06:05 +11001995 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
1996 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 switch (extended_msg[2]) {
1999 case EXTENDED_SDTR:
2000 case EXTENDED_WDTR:
2001 case EXTENDED_MODIFY_DATA_POINTER:
2002 case EXTENDED_EXTENDED_IDENTIFY:
2003 tmp = 0;
2004 }
2005 } else if (len) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002006 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 tmp = 0;
2008 } else {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002009 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2010 extended_msg[2], extended_msg[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 tmp = 0;
2012 }
Finn Thain11d2f632016-01-03 16:05:51 +11002013
2014 spin_lock_irq(&hostdata->lock);
2015 if (!hostdata->connected)
2016 return;
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* Fall through to reject message */
2019
Finn Thainaff0cf92016-01-03 16:06:09 +11002020 /*
2021 * If we get something weird that we aren't expecting,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 * reject it.
2023 */
2024 default:
2025 if (!tmp) {
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002026 shost_printk(KERN_ERR, instance, "rejecting message ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002027 spi_print_msg(extended_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 printk("\n");
2029 } else if (tmp != EXTENDED_MESSAGE)
Jeff Garzik017560f2005-10-24 18:04:36 -04002030 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11002031 "rejecting unknown message %02x\n",
2032 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 else
Jeff Garzik017560f2005-10-24 18:04:36 -04002034 scmd_printk(KERN_INFO, cmd,
Finn Thain0d2cf862016-01-03 16:06:11 +11002035 "rejecting unknown extended message code %02x, length %d\n",
2036 extended_msg[1], extended_msg[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 msgout = MESSAGE_REJECT;
2039 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2040 break;
Finn Thain0d2cf862016-01-03 16:06:11 +11002041 } /* switch (tmp) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 break;
2043 case PHASE_MSGOUT:
2044 len = 1;
2045 data = &msgout;
2046 hostdata->last_message = msgout;
2047 NCR5380_transfer_pio(instance, &phase, &len, &data);
2048 if (msgout == ABORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 hostdata->connected = NULL;
2050 cmd->result = DID_ERROR << 16;
Finn Thain677e0192016-01-03 16:05:59 +11002051 complete_cmd(instance, cmd);
Finn Thain52d3e562016-03-23 21:10:20 +11002052 maybe_release_dma_irq(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2054 return;
2055 }
2056 msgout = NOP;
2057 break;
2058 case PHASE_CMDOUT:
2059 len = cmd->cmd_len;
2060 data = cmd->cmnd;
Finn Thainaff0cf92016-01-03 16:06:09 +11002061 /*
2062 * XXX for performance reasons, on machines with a
2063 * PSEUDO-DMA architecture we should probably
2064 * use the dma transfer function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
2066 NCR5380_transfer_pio(instance, &phase, &len, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 break;
2068 case PHASE_STATIN:
2069 len = 1;
2070 data = &tmp;
2071 NCR5380_transfer_pio(instance, &phase, &len, &data);
2072 cmd->SCp.Status = tmp;
2073 break;
2074 default:
Finn Thain6a6ff4a2016-01-03 16:06:04 +11002075 shost_printk(KERN_ERR, instance, "unknown phase\n");
Finn Thain4dde8f72014-03-18 11:42:17 +11002076 NCR5380_dprint(NDEBUG_ANY, instance);
Finn Thain0d2cf862016-01-03 16:06:11 +11002077 } /* switch(phase) */
Finn Thain686f3992016-01-03 16:05:26 +11002078 } else {
Finn Thain11d2f632016-01-03 16:05:51 +11002079 spin_unlock_irq(&hostdata->lock);
Finn Thain686f3992016-01-03 16:05:26 +11002080 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
Finn Thain11d2f632016-01-03 16:05:51 +11002081 spin_lock_irq(&hostdata->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Finn Thain11d2f632016-01-03 16:05:51 +11002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
2086/*
2087 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2088 *
Finn Thainaff0cf92016-01-03 16:06:09 +11002089 * Purpose : does reselection, initializing the instance->connected
Finn Thain594d4ba2016-01-03 16:06:10 +11002090 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2091 * nexus has been reestablished,
Finn Thainaff0cf92016-01-03 16:06:09 +11002092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 * Inputs : instance - this instance of the NCR5380.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 */
2095
Finn Thain0d2cf862016-01-03 16:06:11 +11002096static void NCR5380_reselect(struct Scsi_Host *instance)
2097{
Finn Thaine8a60142016-01-03 16:05:54 +11002098 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 unsigned char target_mask;
Finn Thaine9db3192016-03-23 21:10:21 +11002100 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 unsigned char msg[3];
Finn Thain32b26a12016-01-03 16:05:58 +11002102 struct NCR5380_cmd *ncmd;
2103 struct scsi_cmnd *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /*
2106 * Disable arbitration, etc. since the host adapter obviously
2107 * lost, and tell an interrupted NCR5380_select() to restart.
2108 */
2109
2110 NCR5380_write(MODE_REG, MR_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
Finn Thainb7465452016-01-03 16:06:05 +11002113
2114 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Finn Thainaff0cf92016-01-03 16:06:09 +11002116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * At this point, we have detected that our SCSI ID is on the bus,
2118 * SEL is true and BSY was false for at least one bus settle delay
2119 * (400 ns).
2120 *
2121 * We must assert BSY ourselves, until the target drops the SEL
2122 * signal.
2123 */
2124
2125 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
Finn Thain72064a72016-01-03 16:05:44 +11002126 if (NCR5380_poll_politely(instance,
2127 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2128 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2129 return;
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2132
2133 /*
2134 * Wait for target to go into MSGIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 */
2136
Finn Thain1cc160e2016-01-03 16:05:32 +11002137 if (NCR5380_poll_politely(instance,
Finn Thain72064a72016-01-03 16:05:44 +11002138 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2139 do_abort(instance);
2140 return;
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Finn Thaine9db3192016-03-23 21:10:21 +11002143#ifdef CONFIG_SUN3
2144 /* acknowledge toggle to MSGIN */
2145 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Finn Thaine9db3192016-03-23 21:10:21 +11002147 /* peek at the byte without really hitting the bus */
2148 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2149#else
2150 {
2151 int len = 1;
2152 unsigned char *data = msg;
2153 unsigned char phase = PHASE_MSGIN;
2154
2155 NCR5380_transfer_pio(instance, &phase, &len, &data);
2156
2157 if (len) {
2158 do_abort(instance);
2159 return;
2160 }
Finn Thain72064a72016-01-03 16:05:44 +11002161 }
Finn Thaine9db3192016-03-23 21:10:21 +11002162#endif /* CONFIG_SUN3 */
Finn Thain72064a72016-01-03 16:05:44 +11002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (!(msg[0] & 0x80)) {
Finn Thain72064a72016-01-03 16:05:44 +11002165 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
Matthew Wilcox1abfd372005-12-15 16:22:01 -05002166 spi_print_msg(msg);
Finn Thain72064a72016-01-03 16:05:44 +11002167 printk("\n");
2168 do_abort(instance);
2169 return;
2170 }
2171 lun = msg[0] & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Finn Thain72064a72016-01-03 16:05:44 +11002173 /*
2174 * We need to add code for SCSI-II to track which devices have
2175 * I_T_L_Q nexuses established, and which have simple I_T_L
2176 * nexuses so we can chose to do additional data transfer.
2177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Finn Thain72064a72016-01-03 16:05:44 +11002179 /*
2180 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2181 * just reestablished, and remove it from the disconnected queue.
2182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Finn Thain32b26a12016-01-03 16:05:58 +11002184 tmp = NULL;
2185 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2186 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2187
2188 if (target_mask == (1 << scmd_id(cmd)) &&
2189 lun == (u8)cmd->device->lun) {
2190 list_del(&ncmd->list);
2191 tmp = cmd;
Finn Thain72064a72016-01-03 16:05:44 +11002192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194 }
Finn Thain0d3d9a42016-01-03 16:05:55 +11002195
2196 if (tmp) {
2197 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2198 "reselect: removed %p from disconnected queue\n", tmp);
2199 } else {
Finn Thain72064a72016-01-03 16:05:44 +11002200 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2201 target_mask, lun);
2202 /*
Finn Thain0d2cf862016-01-03 16:06:11 +11002203 * Since we have an established nexus that we can't do anything
2204 * with, we must abort it.
Finn Thain72064a72016-01-03 16:05:44 +11002205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 do_abort(instance);
Finn Thain72064a72016-01-03 16:05:44 +11002207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Finn Thain72064a72016-01-03 16:05:44 +11002209
Finn Thaine9db3192016-03-23 21:10:21 +11002210#ifdef CONFIG_SUN3
2211 {
2212 void *d;
2213 unsigned long count;
2214
2215 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2216 count = tmp->SCp.buffer->length;
2217 d = sg_virt(tmp->SCp.buffer);
2218 } else {
2219 count = tmp->SCp.this_residual;
2220 d = tmp->SCp.ptr;
2221 }
2222
2223 if (sun3_dma_setup_done != tmp &&
2224 sun3scsi_dma_xfer_len(count, tmp) > 0) {
2225 sun3scsi_dma_setup(instance, d, count,
2226 rq_data_dir(tmp->request));
2227 sun3_dma_setup_done = tmp;
2228 }
2229 }
2230
2231 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2232#endif /* CONFIG_SUN3 */
2233
Finn Thain72064a72016-01-03 16:05:44 +11002234 /* Accept message by clearing ACK */
2235 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2236
2237 hostdata->connected = tmp;
Finn Thainc4ec6f92016-03-23 21:10:22 +11002238 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2239 scmd_id(tmp), tmp->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Finn Thain8b00c3d2016-01-03 16:06:01 +11002242/**
2243 * list_find_cmd - test for presence of a command in a linked list
2244 * @haystack: list of commands
2245 * @needle: command to search for
2246 */
2247
2248static bool list_find_cmd(struct list_head *haystack,
2249 struct scsi_cmnd *needle)
2250{
2251 struct NCR5380_cmd *ncmd;
2252
2253 list_for_each_entry(ncmd, haystack, list)
2254 if (NCR5380_to_scmd(ncmd) == needle)
2255 return true;
2256 return false;
2257}
2258
2259/**
2260 * list_remove_cmd - remove a command from linked list
2261 * @haystack: list of commands
2262 * @needle: command to remove
2263 */
2264
2265static bool list_del_cmd(struct list_head *haystack,
2266 struct scsi_cmnd *needle)
2267{
2268 if (list_find_cmd(haystack, needle)) {
2269 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2270
2271 list_del(&ncmd->list);
2272 return true;
2273 }
2274 return false;
2275}
2276
2277/**
2278 * NCR5380_abort - scsi host eh_abort_handler() method
2279 * @cmd: the command to be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002281 * Try to abort a given command by removing it from queues and/or sending
2282 * the target an abort message. This may not succeed in causing a target
2283 * to abort the command. Nonetheless, the low-level driver must forget about
2284 * the command because the mid-layer reclaims it and it may be re-issued.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002286 * The normal path taken by a command is as follows. For EH we trace this
2287 * same path to locate and abort the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002289 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2290 * [disconnected -> connected ->]...
2291 * [autosense -> connected ->] done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002293 * If cmd was not found at all then presumably it has already been completed,
2294 * in which case return SUCCESS to try to avoid further EH measures.
Finn Thaindc183962016-02-23 10:07:07 +11002295 *
Finn Thain8b00c3d2016-01-03 16:06:01 +11002296 * If the command has not completed yet, we must not fail to find it.
Finn Thaindc183962016-02-23 10:07:07 +11002297 * We have no option but to forget the aborted command (even if it still
2298 * lacks sense data). The mid-layer may re-issue a command that is in error
2299 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2300 * this driver are such that a command can appear on one queue only.
Finn Thain71a00592016-02-23 10:07:06 +11002301 *
2302 * The lock protects driver data structures, but EH handlers also use it
2303 * to serialize their own execution and prevent their own re-entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 */
2305
Finn Thain710ddd02014-11-12 16:12:02 +11002306static int NCR5380_abort(struct scsi_cmnd *cmd)
2307{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct Scsi_Host *instance = cmd->device->host;
Finn Thaine8a60142016-01-03 16:05:54 +11002309 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002310 unsigned long flags;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002311 int result = SUCCESS;
Hannes Reinecke1fa6b5f2014-10-24 14:26:58 +02002312
Finn Thain11d2f632016-01-03 16:05:51 +11002313 spin_lock_irqsave(&hostdata->lock, flags);
2314
Finn Thain32b26a12016-01-03 16:05:58 +11002315#if (NDEBUG & NDEBUG_ANY)
Finn Thain8b00c3d2016-01-03 16:06:01 +11002316 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain32b26a12016-01-03 16:05:58 +11002317#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002318 NCR5380_dprint(NDEBUG_ANY, instance);
2319 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Finn Thain8b00c3d2016-01-03 16:06:01 +11002321 if (list_del_cmd(&hostdata->unissued, cmd)) {
2322 dsprintk(NDEBUG_ABORT, instance,
2323 "abort: removed %p from issue queue\n", cmd);
2324 cmd->result = DID_ABORT << 16;
2325 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
Finn Thaindc183962016-02-23 10:07:07 +11002326 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002327 }
2328
Finn Thain707d62b2016-01-03 16:06:02 +11002329 if (hostdata->selecting == cmd) {
2330 dsprintk(NDEBUG_ABORT, instance,
2331 "abort: cmd %p == selecting\n", cmd);
2332 hostdata->selecting = NULL;
2333 cmd->result = DID_ABORT << 16;
2334 complete_cmd(instance, cmd);
2335 goto out;
2336 }
2337
Finn Thain8b00c3d2016-01-03 16:06:01 +11002338 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2339 dsprintk(NDEBUG_ABORT, instance,
2340 "abort: removed %p from disconnected list\n", cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002341 /* Can't call NCR5380_select() and send ABORT because that
2342 * means releasing the lock. Need a bus reset.
2343 */
Finn Thaindc183962016-02-23 10:07:07 +11002344 set_host_byte(cmd, DID_ERROR);
2345 complete_cmd(instance, cmd);
Finn Thain71a00592016-02-23 10:07:06 +11002346 result = FAILED;
2347 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002348 }
2349
2350 if (hostdata->connected == cmd) {
2351 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2352 hostdata->connected = NULL;
Finn Thaindc183962016-02-23 10:07:07 +11002353 hostdata->dma_len = 0;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002354 if (do_abort(instance)) {
2355 set_host_byte(cmd, DID_ERROR);
2356 complete_cmd(instance, cmd);
2357 result = FAILED;
2358 goto out;
2359 }
2360 set_host_byte(cmd, DID_ABORT);
Finn Thaindc183962016-02-23 10:07:07 +11002361 complete_cmd(instance, cmd);
2362 goto out;
Finn Thain8b00c3d2016-01-03 16:06:01 +11002363 }
2364
Finn Thaindc183962016-02-23 10:07:07 +11002365 if (list_del_cmd(&hostdata->autosense, cmd)) {
Finn Thain8b00c3d2016-01-03 16:06:01 +11002366 dsprintk(NDEBUG_ABORT, instance,
Finn Thaindc183962016-02-23 10:07:07 +11002367 "abort: removed %p from sense queue\n", cmd);
2368 set_host_byte(cmd, DID_ERROR);
Finn Thain8b00c3d2016-01-03 16:06:01 +11002369 complete_cmd(instance, cmd);
2370 }
2371
2372out:
2373 if (result == FAILED)
2374 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2375 else
2376 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2377
2378 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002379 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002380 spin_unlock_irqrestore(&hostdata->lock, flags);
Finn Thain32b26a12016-01-03 16:05:58 +11002381
Finn Thain8b00c3d2016-01-03 16:06:01 +11002382 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
2385
Finn Thain3be1b3e2016-01-03 16:05:12 +11002386/**
2387 * NCR5380_bus_reset - reset the SCSI bus
2388 * @cmd: SCSI command undergoing EH
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 *
Finn Thain3be1b3e2016-01-03 16:05:12 +11002390 * Returns SUCCESS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 */
2392
Finn Thain710ddd02014-11-12 16:12:02 +11002393static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002394{
2395 struct Scsi_Host *instance = cmd->device->host;
Finn Thain11d2f632016-01-03 16:05:51 +11002396 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Finn Thain62717f52016-01-03 16:06:03 +11002397 int i;
Finn Thain11d2f632016-01-03 16:05:51 +11002398 unsigned long flags;
Finn Thain62717f52016-01-03 16:06:03 +11002399 struct NCR5380_cmd *ncmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Finn Thain11d2f632016-01-03 16:05:51 +11002401 spin_lock_irqsave(&hostdata->lock, flags);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002402
2403#if (NDEBUG & NDEBUG_ANY)
Finn Thain62717f52016-01-03 16:06:03 +11002404 scmd_printk(KERN_INFO, cmd, __func__);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002405#endif
Finn Thaine5c3fdd2016-01-03 16:05:52 +11002406 NCR5380_dprint(NDEBUG_ANY, instance);
2407 NCR5380_dprint_phase(NDEBUG_ANY, instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002408
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002409 do_reset(instance);
Finn Thain3be1b3e2016-01-03 16:05:12 +11002410
Finn Thain62717f52016-01-03 16:06:03 +11002411 /* reset NCR registers */
2412 NCR5380_write(MODE_REG, MR_BASE);
2413 NCR5380_write(TARGET_COMMAND_REG, 0);
2414 NCR5380_write(SELECT_ENABLE_REG, 0);
2415
2416 /* After the reset, there are no more connected or disconnected commands
2417 * and no busy units; so clear the low-level status here to avoid
2418 * conflicts when the mid-level code tries to wake up the affected
2419 * commands!
2420 */
2421
Finn Thain1884c282016-02-23 10:07:04 +11002422 if (list_del_cmd(&hostdata->unissued, cmd)) {
2423 cmd->result = DID_RESET << 16;
2424 cmd->scsi_done(cmd);
2425 }
2426
2427 if (hostdata->selecting) {
2428 hostdata->selecting->result = DID_RESET << 16;
2429 complete_cmd(instance, hostdata->selecting);
2430 hostdata->selecting = NULL;
2431 }
Finn Thain62717f52016-01-03 16:06:03 +11002432
2433 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2434 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2435
2436 set_host_byte(cmd, DID_RESET);
Finn Thain216fad92016-03-23 21:10:32 +11002437 complete_cmd(instance, cmd);
Finn Thain62717f52016-01-03 16:06:03 +11002438 }
Finn Thain1884c282016-02-23 10:07:04 +11002439 INIT_LIST_HEAD(&hostdata->disconnected);
Finn Thain62717f52016-01-03 16:06:03 +11002440
2441 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2442 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2443
2444 set_host_byte(cmd, DID_RESET);
2445 cmd->scsi_done(cmd);
2446 }
Finn Thain1884c282016-02-23 10:07:04 +11002447 INIT_LIST_HEAD(&hostdata->autosense);
Finn Thain62717f52016-01-03 16:06:03 +11002448
2449 if (hostdata->connected) {
2450 set_host_byte(hostdata->connected, DID_RESET);
2451 complete_cmd(instance, hostdata->connected);
2452 hostdata->connected = NULL;
2453 }
2454
Finn Thain62717f52016-01-03 16:06:03 +11002455 for (i = 0; i < 8; ++i)
2456 hostdata->busy[i] = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002457 hostdata->dma_len = 0;
Finn Thain62717f52016-01-03 16:06:03 +11002458
2459 queue_work(hostdata->work_q, &hostdata->main_task);
Finn Thain52d3e562016-03-23 21:10:20 +11002460 maybe_release_dma_irq(instance);
Finn Thain11d2f632016-01-03 16:05:51 +11002461 spin_unlock_irqrestore(&hostdata->lock, flags);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -04002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return SUCCESS;
2464}