aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFinn Thain <fthain@telegraphics.com.au>2016-03-23 06:10:22 -0400
committerMartin K. Petersen <martin.petersen@oracle.com>2016-04-11 16:57:09 -0400
commitc4ec6f924f0682e1f40107204152e977d6b1bd07 (patch)
tree63093bc0616f30e7a3d140923b9551a72d2f69cc
parente9db3198e08b6a01e2847f732e595bb8e89153c1 (diff)
ncr5380: Remove disused atari_NCR5380.c core driver
Now that atari_scsi and sun3_scsi have been converted to use the NCR5380.c core driver, remove atari_NCR5380.c. Also remove the last vestiges of its Tagged Command Queueing implementation from the wrapper drivers. The TCQ support in atari_NCR5380.c is abandoned by this patch. It is not merged into the remaining core driver because, 1) atari_scsi defines SUPPORT_TAGS but leaves FLAG_TAGGED_QUEUING disabled by default, which indicates that it is mostly undesirable. 2) I'm told that it doesn't work correctly when enabled. 3) The algorithm does not make use of block layer tags which it will have to do because scmd->tag is deprecated. 4) sun3_scsi doesn't define SUPPORT_TAGS at all, yet the the SUPPORT_TAGS macro interacts with the CONFIG_SUN3 macro in 'interesting' ways. 5) Compile-time configuration with macros like SUPPORT_TAGS caused the configuration space to explode, leading to untestable and unmaintainable code that is too hard to reason about. The merge_contiguous_buffers() code is also abandoned. This was unused by sun3_scsi. Only atari_scsi used it and then only on TT, because only TT supports scatter/gather. I suspect that the TT would work fine with ENABLE_CLUSTERING instead. If someone can benchmark the difference then perhaps the merge_contiguous_buffers() code can be be justified. Until then we are better off without the extra complexity. Signed-off-by: Finn Thain <fthain@telegraphics.com.au> Reviewed-by: Hannes Reinecke <hare@suse.com> Tested-by: Michael Schmitz <schmitzmic@gmail.com> Tested-by: Ondrej Zary <linux@rainbow-software.org> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r--drivers/scsi/NCR5380.c22
-rw-r--r--drivers/scsi/NCR5380.h19
-rw-r--r--drivers/scsi/atari_NCR5380.c2632
-rw-r--r--drivers/scsi/atari_scsi.c11
-rw-r--r--drivers/scsi/mac_scsi.c8
-rw-r--r--drivers/scsi/sun3_scsi.c11
6 files changed, 4 insertions, 2699 deletions
diff --git a/drivers/scsi/NCR5380.c b/drivers/scsi/NCR5380.c
index a59b71f96365..305330b26349 100644
--- a/drivers/scsi/NCR5380.c
+++ b/drivers/scsi/NCR5380.c
@@ -34,13 +34,6 @@
34/* Adapted for the Sun 3 by Sam Creasey. */ 34/* Adapted for the Sun 3 by Sam Creasey. */
35 35
36/* 36/*
37 * Further development / testing that should be done :
38 *
39 * 4. Test SCSI-II tagged queueing (I have no devices which support
40 * tagged queueing)
41 */
42
43/*
44 * Design 37 * Design
45 * 38 *
46 * This is a generic 5380 driver. To use it on a different platform, 39 * This is a generic 5380 driver. To use it on a different platform,
@@ -1257,14 +1250,6 @@ static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1257 * was true but before BSY was false during selection, the information 1250 * was true but before BSY was false during selection, the information
1258 * transfer phase should be a MESSAGE OUT phase so that we can send the 1251 * transfer phase should be a MESSAGE OUT phase so that we can send the
1259 * IDENTIFY message. 1252 * IDENTIFY message.
1260 *
1261 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1262 * message (2 bytes) with a tag ID that we increment with every command
1263 * until it wraps back to 0.
1264 *
1265 * XXX - it turns out that there are some broken SCSI-II devices,
1266 * which claim to support tagged queuing but fail when more than
1267 * some number of commands are issued at once.
1268 */ 1253 */
1269 1254
1270 /* Wait for start of REQ/ACK handshake */ 1255 /* Wait for start of REQ/ACK handshake */
@@ -1287,9 +1272,6 @@ static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1287 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun); 1272 tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun);
1288 1273
1289 len = 1; 1274 len = 1;
1290 cmd->tag = 0;
1291
1292 /* Send message(s) */
1293 data = tmp; 1275 data = tmp;
1294 phase = PHASE_MSGOUT; 1276 phase = PHASE_MSGOUT;
1295 NCR5380_transfer_pio(instance, &phase, &len, &data); 1277 NCR5380_transfer_pio(instance, &phase, &len, &data);
@@ -2256,8 +2238,8 @@ static void NCR5380_reselect(struct Scsi_Host *instance)
2256 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); 2238 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2257 2239
2258 hostdata->connected = tmp; 2240 hostdata->connected = tmp;
2259 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n", 2241 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu\n",
2260 scmd_id(tmp), tmp->device->lun, tmp->tag); 2242 scmd_id(tmp), tmp->device->lun);
2261} 2243}
2262 2244
2263/** 2245/**
diff --git a/drivers/scsi/NCR5380.h b/drivers/scsi/NCR5380.h
index 8adf7377de4c..5c2411f05852 100644
--- a/drivers/scsi/NCR5380.h
+++ b/drivers/scsi/NCR5380.h
@@ -199,13 +199,6 @@
199 199
200#define PHASE_SR_TO_TCR(phase) ((phase) >> 2) 200#define PHASE_SR_TO_TCR(phase) ((phase) >> 2)
201 201
202/*
203 * "Special" value for the (unsigned char) command tag, to indicate
204 * I_T_L nexus instead of I_T_L_Q.
205 */
206
207#define TAG_NONE 0xff
208
209/* 202/*
210 * These are "special" values for the irq and dma_channel fields of the 203 * These are "special" values for the irq and dma_channel fields of the
211 * Scsi_Host structure 204 * Scsi_Host structure
@@ -223,17 +216,8 @@
223#define FLAG_DMA_FIXUP 1 /* Use DMA errata workarounds */ 216#define FLAG_DMA_FIXUP 1 /* Use DMA errata workarounds */
224#define FLAG_NO_PSEUDO_DMA 8 /* Inhibit DMA */ 217#define FLAG_NO_PSEUDO_DMA 8 /* Inhibit DMA */
225#define FLAG_LATE_DMA_SETUP 32 /* Setup NCR before DMA H/W */ 218#define FLAG_LATE_DMA_SETUP 32 /* Setup NCR before DMA H/W */
226#define FLAG_TAGGED_QUEUING 64 /* as X3T9.2 spelled it */
227#define FLAG_TOSHIBA_DELAY 128 /* Allow for borken CD-ROMs */ 219#define FLAG_TOSHIBA_DELAY 128 /* Allow for borken CD-ROMs */
228 220
229#ifdef SUPPORT_TAGS
230struct tag_alloc {
231 DECLARE_BITMAP(allocated, MAX_TAGS);
232 int nr_allocated;
233 int queue_size;
234};
235#endif
236
237struct NCR5380_hostdata { 221struct NCR5380_hostdata {
238 NCR5380_implementation_fields; /* implementation specific */ 222 NCR5380_implementation_fields; /* implementation specific */
239 struct Scsi_Host *host; /* Host backpointer */ 223 struct Scsi_Host *host; /* Host backpointer */
@@ -254,9 +238,6 @@ struct NCR5380_hostdata {
254 int read_overruns; /* number of bytes to cut from a 238 int read_overruns; /* number of bytes to cut from a
255 * transfer to handle chip overruns */ 239 * transfer to handle chip overruns */
256 struct work_struct main_task; 240 struct work_struct main_task;
257#ifdef SUPPORT_TAGS
258 struct tag_alloc TagAlloc[8][8]; /* 8 targets and 8 LUNs */
259#endif
260 struct workqueue_struct *work_q; 241 struct workqueue_struct *work_q;
261 unsigned long accesses_per_ms; /* chip register accesses per ms */ 242 unsigned long accesses_per_ms; /* chip register accesses per ms */
262}; 243};
diff --git a/drivers/scsi/atari_NCR5380.c b/drivers/scsi/atari_NCR5380.c
deleted file mode 100644
index 4101d2de4333..000000000000
--- a/drivers/scsi/atari_NCR5380.c
+++ /dev/null
@@ -1,2632 +0,0 @@
1/*
2 * NCR 5380 generic driver routines. These should make it *trivial*
3 * to implement 5380 SCSI drivers under Linux with a non-trantor
4 * architecture.
5 *
6 * Note that these routines also work with NR53c400 family chips.
7 *
8 * Copyright 1993, Drew Eckhardt
9 * Visionary Computing
10 * (Unix and Linux consulting and custom programming)
11 * drew@colorado.edu
12 * +1 (303) 666-5836
13 *
14 * For more information, please consult
15 *
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/* Ported to Atari by Roman Hodek and others. */
28
29/* Adapted for the sun3 by Sam Creasey. */
30
31/*
32 * Design
33 *
34 * This is a generic 5380 driver. To use it on a different platform,
35 * one simply writes appropriate system specific macros (ie, data
36 * transfer - some PC's will use the I/O bus, 68K's must use
37 * memory mapped) and drops this file in their 'C' wrapper.
38 *
39 * As far as command queueing, two queues are maintained for
40 * each 5380 in the system - commands that haven't been issued yet,
41 * and commands that are currently executing. This means that an
42 * unlimited number of commands may be queued, letting
43 * more commands propagate from the higher driver levels giving higher
44 * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
45 * allowing multiple commands to propagate all the way to a SCSI-II device
46 * while a command is already executing.
47 *
48 *
49 * Issues specific to the NCR5380 :
50 *
51 * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
52 * piece of hardware that requires you to sit in a loop polling for
53 * the REQ signal as long as you are connected. Some devices are
54 * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
55 * while doing long seek operations. [...] These
56 * broken devices are the exception rather than the rule and I'd rather
57 * spend my time optimizing for the normal case.
58 *
59 * Architecture :
60 *
61 * At the heart of the design is a coroutine, NCR5380_main,
62 * which is started from a workqueue for each NCR5380 host in the
63 * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
64 * removing the commands from the issue queue and calling
65 * NCR5380_select() if a nexus is not established.
66 *
67 * Once a nexus is established, the NCR5380_information_transfer()
68 * phase goes through the various phases as instructed by the target.
69 * if the target goes into MSG IN and sends a DISCONNECT message,
70 * the command structure is placed into the per instance disconnected
71 * queue, and NCR5380_main tries to find more work. If the target is
72 * idle for too long, the system will try to sleep.
73 *
74 * If a command has disconnected, eventually an interrupt will trigger,
75 * calling NCR5380_intr() which will in turn call NCR5380_reselect
76 * to reestablish a nexus. This will run main if necessary.
77 *
78 * On command termination, the done function will be called as
79 * appropriate.
80 *
81 * SCSI pointers are maintained in the SCp field of SCSI command
82 * structures, being initialized after the command is connected
83 * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
84 * Note that in violation of the standard, an implicit SAVE POINTERS operation
85 * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
86 */
87
88/*
89 * Using this file :
90 * This file a skeleton Linux SCSI driver for the NCR 5380 series
91 * of chips. To use it, you write an architecture specific functions
92 * and macros and include this file in your driver.
93 *
94 * These macros control options :
95 * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
96 * for commands that return with a CHECK CONDITION status.
97 *
98 * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
99 * transceivers.
100 *
101 * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
102 *
103 * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
104 *
105 * These macros MUST be defined :
106 *
107 * NCR5380_read(register) - read from the specified register
108 *
109 * NCR5380_write(register, value) - write to the specific register
110 *
111 * NCR5380_implementation_fields - additional fields needed for this
112 * specific implementation of the NCR5380
113 *
114 * Either real DMA *or* pseudo DMA may be implemented
115 * Note that the DMA setup functions should return the number of bytes
116 * that they were able to program the controller for.
117 *
118 * NCR5380_dma_write_setup(instance, src, count) - initialize
119 * NCR5380_dma_read_setup(instance, dst, count) - initialize
120 * NCR5380_dma_residual(instance); - residual count
121 *
122 * PSEUDO functions :
123 * NCR5380_pwrite(instance, src, count)
124 * NCR5380_pread(instance, dst, count);
125 *
126 * The generic driver is initialized by calling NCR5380_init(instance),
127 * after setting the appropriate host specific fields and ID. If the
128 * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
129 * possible) function may be used.
130 */
131
132static int do_abort(struct Scsi_Host *);
133static void do_reset(struct Scsi_Host *);
134
135#ifdef SUPPORT_TAGS
136
137/*
138 * Functions for handling tagged queuing
139 * =====================================
140 *
141 * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
142 *
143 * Using consecutive numbers for the tags is no good idea in my eyes. There
144 * could be wrong re-usings if the counter (8 bit!) wraps and some early
145 * command has been preempted for a long time. My solution: a bitfield for
146 * remembering used tags.
147 *
148 * There's also the problem that each target has a certain queue size, but we
149 * cannot know it in advance :-( We just see a QUEUE_FULL status being
150 * returned. So, in this case, the driver internal queue size assumption is
151 * reduced to the number of active tags if QUEUE_FULL is returned by the
152 * target.
153 *
154 * We're also not allowed running tagged commands as long as an untagged
155 * command is active. And REQUEST SENSE commands after a contingent allegiance
156 * condition _must_ be untagged. To keep track whether an untagged command has
157 * been issued, the host->busy array is still employed, as it is without
158 * support for tagged queuing.
159 *
160 * One could suspect that there are possible race conditions between
161 * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
162 * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
163 * which already guaranteed to be running at most once. It is also the only
164 * place where tags/LUNs are allocated. So no other allocation can slip
165 * between that pair, there could only happen a reselection, which can free a
166 * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
167 * important: the tag bit must be cleared before 'nr_allocated' is decreased.
168 */
169
170static void __init init_tags(struct NCR5380_hostdata *hostdata)
171{
172 int target, lun;
173 struct tag_alloc *ta;
174
175 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
176 return;
177
178 for (target = 0; target < 8; ++target) {
179 for (lun = 0; lun < 8; ++lun) {
180 ta = &hostdata->TagAlloc[target][lun];
181 bitmap_zero(ta->allocated, MAX_TAGS);
182 ta->nr_allocated = 0;
183 /* At the beginning, assume the maximum queue size we could
184 * support (MAX_TAGS). This value will be decreased if the target
185 * returns QUEUE_FULL status.
186 */
187 ta->queue_size = MAX_TAGS;
188 }
189 }
190}
191
192
193/* Check if we can issue a command to this LUN: First see if the LUN is marked
194 * busy by an untagged command. If the command should use tagged queuing, also
195 * check that there is a free tag and the target's queue won't overflow. This
196 * function should be called with interrupts disabled to avoid race
197 * conditions.
198 */
199
200static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged)
201{
202 u8 lun = cmd->device->lun;
203 struct Scsi_Host *instance = cmd->device->host;
204 struct NCR5380_hostdata *hostdata = shost_priv(instance);
205
206 if (hostdata->busy[cmd->device->id] & (1 << lun))
207 return 1;
208 if (!should_be_tagged ||
209 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
210 !cmd->device->tagged_supported)
211 return 0;
212 if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >=
213 hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) {
214 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d: no free tags\n",
215 scmd_id(cmd), lun);
216 return 1;
217 }
218 return 0;
219}
220
221
222/* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
223 * must be called before!), or reserve the LUN in 'busy' if the command is
224 * untagged.
225 */
226
227static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged)
228{
229 u8 lun = cmd->device->lun;
230 struct Scsi_Host *instance = cmd->device->host;
231 struct NCR5380_hostdata *hostdata = shost_priv(instance);
232
233 /* If we or the target don't support tagged queuing, allocate the LUN for
234 * an untagged command.
235 */
236 if (!should_be_tagged ||
237 !(hostdata->flags & FLAG_TAGGED_QUEUING) ||
238 !cmd->device->tagged_supported) {
239 cmd->tag = TAG_NONE;
240 hostdata->busy[cmd->device->id] |= (1 << lun);
241 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d now allocated by untagged command\n",
242 scmd_id(cmd), lun);
243 } else {
244 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
245
246 cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS);
247 set_bit(cmd->tag, ta->allocated);
248 ta->nr_allocated++;
249 dsprintk(NDEBUG_TAGS, instance, "using tag %d for target %d lun %d (%d tags allocated)\n",
250 cmd->tag, scmd_id(cmd), lun, ta->nr_allocated);
251 }
252}
253
254
255/* Mark the tag of command 'cmd' as free, or in case of an untagged command,
256 * unlock the LUN.
257 */
258
259static void cmd_free_tag(struct scsi_cmnd *cmd)
260{
261 u8 lun = cmd->device->lun;
262 struct Scsi_Host *instance = cmd->device->host;
263 struct NCR5380_hostdata *hostdata = shost_priv(instance);
264
265 if (cmd->tag == TAG_NONE) {
266 hostdata->busy[cmd->device->id] &= ~(1 << lun);
267 dsprintk(NDEBUG_TAGS, instance, "target %d lun %d untagged cmd freed\n",
268 scmd_id(cmd), lun);
269 } else if (cmd->tag >= MAX_TAGS) {
270 shost_printk(KERN_NOTICE, instance,
271 "trying to free bad tag %d!\n", cmd->tag);
272 } else {
273 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
274 clear_bit(cmd->tag, ta->allocated);
275 ta->nr_allocated--;
276 dsprintk(NDEBUG_TAGS, instance, "freed tag %d for target %d lun %d\n",
277 cmd->tag, scmd_id(cmd), lun);
278 }
279}
280
281
282static void free_all_tags(struct NCR5380_hostdata *hostdata)
283{
284 int target, lun;
285 struct tag_alloc *ta;
286
287 if (!(hostdata->flags & FLAG_TAGGED_QUEUING))
288 return;
289
290 for (target = 0; target < 8; ++target) {
291 for (lun = 0; lun < 8; ++lun) {
292 ta = &hostdata->TagAlloc[target][lun];
293 bitmap_zero(ta->allocated, MAX_TAGS);
294 ta->nr_allocated = 0;
295 }
296 }
297}
298
299#endif /* SUPPORT_TAGS */
300
301/**
302 * merge_contiguous_buffers - coalesce scatter-gather list entries
303 * @cmd: command requesting IO
304 *
305 * Try to merge several scatter-gather buffers into one DMA transfer.
306 * This is possible if the scatter buffers lie on physically
307 * contiguous addresses. The first scatter-gather buffer's data are
308 * assumed to be already transferred into cmd->SCp.this_residual.
309 * Every buffer merged avoids an interrupt and a DMA setup operation.
310 */
311
312static void merge_contiguous_buffers(struct scsi_cmnd *cmd)
313{
314#if !defined(CONFIG_SUN3)
315 unsigned long endaddr;
316#if (NDEBUG & NDEBUG_MERGING)
317 unsigned long oldlen = cmd->SCp.this_residual;
318 int cnt = 1;
319#endif
320
321 for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
322 cmd->SCp.buffers_residual &&
323 virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) {
324 dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n",
325 page_address(sg_page(&cmd->SCp.buffer[1])), endaddr);
326#if (NDEBUG & NDEBUG_MERGING)
327 ++cnt;
328#endif
329 ++cmd->SCp.buffer;
330 --cmd->SCp.buffers_residual;
331 cmd->SCp.this_residual += cmd->SCp.buffer->length;
332 endaddr += cmd->SCp.buffer->length;
333 }
334#if (NDEBUG & NDEBUG_MERGING)
335 if (oldlen != cmd->SCp.this_residual)
336 dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n",
337 cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
338#endif
339#endif /* !defined(CONFIG_SUN3) */
340}
341
342/**
343 * initialize_SCp - init the scsi pointer field
344 * @cmd: command block to set up
345 *
346 * Set up the internal fields in the SCSI command.
347 */
348
349static inline void initialize_SCp(struct scsi_cmnd *cmd)
350{
351 /*
352 * Initialize the Scsi Pointer field so that all of the commands in the
353 * various queues are valid.
354 */
355
356 if (scsi_bufflen(cmd)) {
357 cmd->SCp.buffer = scsi_sglist(cmd);
358 cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
359 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
360 cmd->SCp.this_residual = cmd->SCp.buffer->length;
361
362 merge_contiguous_buffers(cmd);
363 } else {
364 cmd->SCp.buffer = NULL;
365 cmd->SCp.buffers_residual = 0;
366 cmd->SCp.ptr = NULL;
367 cmd->SCp.this_residual = 0;
368 }
369
370 cmd->SCp.Status = 0;
371 cmd->SCp.Message = 0;
372}
373
374/**
375 * NCR5380_poll_politely2 - wait for two chip register values
376 * @instance: controller to poll
377 * @reg1: 5380 register to poll
378 * @bit1: Bitmask to check
379 * @val1: Expected value
380 * @reg2: Second 5380 register to poll
381 * @bit2: Second bitmask to check
382 * @val2: Second expected value
383 * @wait: Time-out in jiffies
384 *
385 * Polls the chip in a reasonably efficient manner waiting for an
386 * event to occur. After a short quick poll we begin to yield the CPU
387 * (if possible). In irq contexts the time-out is arbitrarily limited.
388 * Callers may hold locks as long as they are held in irq mode.
389 *
390 * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT.
391 */
392
393static int NCR5380_poll_politely2(struct Scsi_Host *instance,
394 int reg1, int bit1, int val1,
395 int reg2, int bit2, int val2, int wait)
396{
397 struct NCR5380_hostdata *hostdata = shost_priv(instance);
398 unsigned long deadline = jiffies + wait;
399 unsigned long n;
400
401 /* Busy-wait for up to 10 ms */
402 n = min(10000U, jiffies_to_usecs(wait));
403 n *= hostdata->accesses_per_ms;
404 n /= 2000;
405 do {
406 if ((NCR5380_read(reg1) & bit1) == val1)
407 return 0;
408 if ((NCR5380_read(reg2) & bit2) == val2)
409 return 0;
410 cpu_relax();
411 } while (n--);
412
413 if (irqs_disabled() || in_interrupt())
414 return -ETIMEDOUT;
415
416 /* Repeatedly sleep for 1 ms until deadline */
417 while (time_is_after_jiffies(deadline)) {
418 schedule_timeout_uninterruptible(1);
419 if ((NCR5380_read(reg1) & bit1) == val1)
420 return 0;
421 if ((NCR5380_read(reg2) & bit2) == val2)
422 return 0;
423 }
424
425 return -ETIMEDOUT;
426}
427
428static inline int NCR5380_poll_politely(struct Scsi_Host *instance,
429 int reg, int bit, int val, int wait)
430{
431 return NCR5380_poll_politely2(instance, reg, bit, val,
432 reg, bit, val, wait);
433}
434
435#if NDEBUG
436static struct {
437 unsigned char mask;
438 const char *name;
439} signals[] = {
440 {SR_DBP, "PARITY"},
441 {SR_RST, "RST"},
442 {SR_BSY, "BSY"},
443 {SR_REQ, "REQ"},
444 {SR_MSG, "MSG"},
445 {SR_CD, "CD"},
446 {SR_IO, "IO"},
447 {SR_SEL, "SEL"},
448 {0, NULL}
449},
450basrs[] = {
451 {BASR_ATN, "ATN"},
452 {BASR_ACK, "ACK"},
453 {0, NULL}
454},
455icrs[] = {
456 {ICR_ASSERT_RST, "ASSERT RST"},
457 {ICR_ASSERT_ACK, "ASSERT ACK"},
458 {ICR_ASSERT_BSY, "ASSERT BSY"},
459 {ICR_ASSERT_SEL, "ASSERT SEL"},
460 {ICR_ASSERT_ATN, "ASSERT ATN"},
461 {ICR_ASSERT_DATA, "ASSERT DATA"},
462 {0, NULL}
463},
464mrs[] = {
465 {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
466 {MR_TARGET, "MODE TARGET"},
467 {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
468 {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
469 {MR_ENABLE_EOP_INTR, "MODE EOP INTR"},
470 {MR_MONITOR_BSY, "MODE MONITOR BSY"},
471 {MR_DMA_MODE, "MODE DMA"},
472 {MR_ARBITRATE, "MODE ARBITRATION"},
473 {0, NULL}
474};
475
476/**
477 * NCR5380_print - print scsi bus signals
478 * @instance: adapter state to dump
479 *
480 * Print the SCSI bus signals for debugging purposes
481 */
482
483static void NCR5380_print(struct Scsi_Host *instance)
484{
485 unsigned char status, data, basr, mr, icr, i;
486
487 data = NCR5380_read(CURRENT_SCSI_DATA_REG);
488 status = NCR5380_read(STATUS_REG);
489 mr = NCR5380_read(MODE_REG);
490 icr = NCR5380_read(INITIATOR_COMMAND_REG);
491 basr = NCR5380_read(BUS_AND_STATUS_REG);
492
493 printk("STATUS_REG: %02x ", status);
494 for (i = 0; signals[i].mask; ++i)
495 if (status & signals[i].mask)
496 printk(",%s", signals[i].name);
497 printk("\nBASR: %02x ", basr);
498 for (i = 0; basrs[i].mask; ++i)
499 if (basr & basrs[i].mask)
500 printk(",%s", basrs[i].name);
501 printk("\nICR: %02x ", icr);
502 for (i = 0; icrs[i].mask; ++i)
503 if (icr & icrs[i].mask)
504 printk(",%s", icrs[i].name);
505 printk("\nMODE: %02x ", mr);
506 for (i = 0; mrs[i].mask; ++i)
507 if (mr & mrs[i].mask)
508 printk(",%s", mrs[i].name);
509 printk("\n");
510}
511
512static struct {
513 unsigned char value;
514 const char *name;
515} phases[] = {
516 {PHASE_DATAOUT, "DATAOUT"},
517 {PHASE_DATAIN, "DATAIN"},
518 {PHASE_CMDOUT, "CMDOUT"},
519 {PHASE_STATIN, "STATIN"},
520 {PHASE_MSGOUT, "MSGOUT"},
521 {PHASE_MSGIN, "MSGIN"},
522 {PHASE_UNKNOWN, "UNKNOWN"}
523};
524
525/**
526 * NCR5380_print_phase - show SCSI phase
527 * @instance: adapter to dump
528 *
529 * Print the current SCSI phase for debugging purposes
530 */
531
532static void NCR5380_print_phase(struct Scsi_Host *instance)
533{
534 unsigned char status;
535 int i;
536
537 status = NCR5380_read(STATUS_REG);
538 if (!(status & SR_REQ))
539 shost_printk(KERN_DEBUG, instance, "REQ not asserted, phase unknown.\n");
540 else {
541 for (i = 0; (phases[i].value != PHASE_UNKNOWN) &&
542 (phases[i].value != (status & PHASE_MASK)); ++i)
543 ;
544 shost_printk(KERN_DEBUG, instance, "phase %s\n", phases[i].name);
545 }
546}
547#endif
548
549/**
550 * NCR58380_info - report driver and host information
551 * @instance: relevant scsi host instance
552 *
553 * For use as the host template info() handler.
554 */
555
556static const char *NCR5380_info(struct Scsi_Host *instance)
557{
558 struct NCR5380_hostdata *hostdata = shost_priv(instance);
559
560 return hostdata->info;
561}
562
563static void prepare_info(struct Scsi_Host *instance)
564{
565 struct NCR5380_hostdata *hostdata = shost_priv(instance);
566
567 snprintf(hostdata->info, sizeof(hostdata->info),
568 "%s, io_port 0x%lx, n_io_port %d, "
569 "base 0x%lx, irq %d, "
570 "can_queue %d, cmd_per_lun %d, "
571 "sg_tablesize %d, this_id %d, "
572 "flags { %s%s}, "
573 "options { %s} ",
574 instance->hostt->name, instance->io_port, instance->n_io_port,
575 instance->base, instance->irq,
576 instance->can_queue, instance->cmd_per_lun,
577 instance->sg_tablesize, instance->this_id,
578 hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "",
579 hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "",
580#ifdef DIFFERENTIAL
581 "DIFFERENTIAL "
582#endif
583#ifdef PARITY
584 "PARITY "
585#endif
586#ifdef SUPPORT_TAGS
587 "SUPPORT_TAGS "
588#endif
589 "");
590}
591
592/**
593 * NCR5380_init - initialise an NCR5380
594 * @instance: adapter to configure
595 * @flags: control flags
596 *
597 * Initializes *instance and corresponding 5380 chip,
598 * with flags OR'd into the initial flags value.
599 *
600 * Notes : I assume that the host, hostno, and id bits have been
601 * set correctly. I don't care about the irq and other fields.
602 *
603 * Returns 0 for success
604 */
605
606static int __init NCR5380_init(struct Scsi_Host *instance, int flags)
607{
608 struct NCR5380_hostdata *hostdata = shost_priv(instance);
609 int i;
610 unsigned long deadline;
611
612 hostdata->host = instance;
613 hostdata->id_mask = 1 << instance->this_id;
614 hostdata->id_higher_mask = 0;
615 for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
616 if (i > hostdata->id_mask)
617 hostdata->id_higher_mask |= i;
618 for (i = 0; i < 8; ++i)
619 hostdata->busy[i] = 0;
620#ifdef SUPPORT_TAGS
621 init_tags(hostdata);
622#endif
623 hostdata->dma_len = 0;
624
625 spin_lock_init(&hostdata->lock);
626 hostdata->connected = NULL;
627 hostdata->sensing = NULL;
628 INIT_LIST_HEAD(&hostdata->autosense);
629 INIT_LIST_HEAD(&hostdata->unissued);
630 INIT_LIST_HEAD(&hostdata->disconnected);
631
632 hostdata->flags = flags;
633
634 INIT_WORK(&hostdata->main_task, NCR5380_main);
635 hostdata->work_q = alloc_workqueue("ncr5380_%d",
636 WQ_UNBOUND | WQ_MEM_RECLAIM,
637 1, instance->host_no);
638 if (!hostdata->work_q)
639 return -ENOMEM;
640
641 prepare_info(instance);
642
643 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
644 NCR5380_write(MODE_REG, MR_BASE);
645 NCR5380_write(TARGET_COMMAND_REG, 0);
646 NCR5380_write(SELECT_ENABLE_REG, 0);
647
648 /* Calibrate register polling loop */
649 i = 0;
650 deadline = jiffies + 1;
651 do {
652 cpu_relax();
653 } while (time_is_after_jiffies(deadline));
654 deadline += msecs_to_jiffies(256);
655 do {
656 NCR5380_read(STATUS_REG);
657 ++i;
658 cpu_relax();
659 } while (time_is_after_jiffies(deadline));
660 hostdata->accesses_per_ms = i / 256;
661
662 return 0;
663}
664
665/**
666 * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems.
667 * @instance: adapter to check
668 *
669 * If the system crashed, it may have crashed with a connected target and
670 * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the
671 * currently established nexus, which we know nothing about. Failing that
672 * do a bus reset.
673 *
674 * Note that a bus reset will cause the chip to assert IRQ.
675 *
676 * Returns 0 if successful, otherwise -ENXIO.
677 */
678
679static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance)
680{
681 struct NCR5380_hostdata *hostdata = shost_priv(instance);
682 int pass;
683
684 for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
685 switch (pass) {
686 case 1:
687 case 3:
688 case 5:
689 shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n");
690 NCR5380_poll_politely(instance,
691 STATUS_REG, SR_BSY, 0, 5 * HZ);
692 break;
693 case 2:
694 shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n");
695 do_abort(instance);
696 break;
697 case 4:
698 shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n");
699 do_reset(instance);
700 /* Wait after a reset; the SCSI standard calls for
701 * 250ms, we wait 500ms to be on the safe side.
702 * But some Toshiba CD-ROMs need ten times that.
703 */
704 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
705 msleep(2500);
706 else
707 msleep(500);
708 break;
709 case 6:
710 shost_printk(KERN_ERR, instance, "bus locked solid\n");
711 return -ENXIO;
712 }
713 }
714 return 0;
715}
716
717/**
718 * NCR5380_exit - remove an NCR5380
719 * @instance: adapter to remove
720 *
721 * Assumes that no more work can be queued (e.g. by NCR5380_intr).
722 */
723
724static void NCR5380_exit(struct Scsi_Host *instance)
725{
726 struct NCR5380_hostdata *hostdata = shost_priv(instance);
727
728 cancel_work_sync(&hostdata->main_task);
729 destroy_workqueue(hostdata->work_q);
730}
731
732/**
733 * complete_cmd - finish processing a command and return it to the SCSI ML
734 * @instance: the host instance
735 * @cmd: command to complete
736 */
737
738static void complete_cmd(struct Scsi_Host *instance,
739 struct scsi_cmnd *cmd)
740{
741 struct NCR5380_hostdata *hostdata = shost_priv(instance);
742
743 dsprintk(NDEBUG_QUEUES, instance, "complete_cmd: cmd %p\n", cmd);
744
745 if (hostdata->sensing == cmd) {
746 /* Autosense processing ends here */
747 if ((cmd->result & 0xff) != SAM_STAT_GOOD) {
748 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
749 set_host_byte(cmd, DID_ERROR);
750 } else
751 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
752 hostdata->sensing = NULL;
753 }
754
755#ifdef SUPPORT_TAGS
756 cmd_free_tag(cmd);
757#else
758 hostdata->busy[scmd_id(cmd)] &= ~(1 << cmd->device->lun);
759#endif
760 cmd->scsi_done(cmd);
761}
762
763/**
764 * NCR5380_queue_command - queue a command
765 * @instance: the relevant SCSI adapter
766 * @cmd: SCSI command
767 *
768 * cmd is added to the per-instance issue queue, with minor
769 * twiddling done to the host specific fields of cmd. If the
770 * main coroutine is not running, it is restarted.
771 */
772
773static int NCR5380_queue_command(struct Scsi_Host *instance,
774 struct scsi_cmnd *cmd)
775{
776 struct NCR5380_hostdata *hostdata = shost_priv(instance);
777 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
778 unsigned long flags;
779
780#if (NDEBUG & NDEBUG_NO_WRITE)
781 switch (cmd->cmnd[0]) {
782 case WRITE_6:
783 case WRITE_10:
784 shost_printk(KERN_DEBUG, instance, "WRITE attempted with NDEBUG_NO_WRITE set\n");
785 cmd->result = (DID_ERROR << 16);
786 cmd->scsi_done(cmd);
787 return 0;
788 }
789#endif /* (NDEBUG & NDEBUG_NO_WRITE) */
790
791 cmd->result = 0;
792
793 /*
794 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
795 * because also a timer int can trigger an abort or reset, which would
796 * alter queues and touch the lock.
797 */
798 if (!NCR5380_acquire_dma_irq(instance))
799 return SCSI_MLQUEUE_HOST_BUSY;
800
801 spin_lock_irqsave(&hostdata->lock, flags);
802
803 /*
804 * Insert the cmd into the issue queue. Note that REQUEST SENSE
805 * commands are added to the head of the queue since any command will
806 * clear the contingent allegiance condition that exists and the
807 * sense data is only guaranteed to be valid while the condition exists.
808 */
809
810 if (cmd->cmnd[0] == REQUEST_SENSE)
811 list_add(&ncmd->list, &hostdata->unissued);
812 else
813 list_add_tail(&ncmd->list, &hostdata->unissued);
814
815 spin_unlock_irqrestore(&hostdata->lock, flags);
816
817 dsprintk(NDEBUG_QUEUES, instance, "command %p added to %s of queue\n",
818 cmd, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
819
820 /* Kick off command processing */
821 queue_work(hostdata->work_q, &hostdata->main_task);
822 return 0;
823}
824
825static inline void maybe_release_dma_irq(struct Scsi_Host *instance)
826{
827 struct NCR5380_hostdata *hostdata = shost_priv(instance);
828
829 /* Caller does the locking needed to set & test these data atomically */
830 if (list_empty(&hostdata->disconnected) &&
831 list_empty(&hostdata->unissued) &&
832 list_empty(&hostdata->autosense) &&
833 !hostdata->connected &&
834 !hostdata->selecting)
835 NCR5380_release_dma_irq(instance);
836}
837
838/**
839 * dequeue_next_cmd - dequeue a command for processing
840 * @instance: the scsi host instance
841 *
842 * Priority is given to commands on the autosense queue. These commands
843 * need autosense because of a CHECK CONDITION result.
844 *
845 * Returns a command pointer if a command is found for a target that is
846 * not already busy. Otherwise returns NULL.
847 */
848
849static struct scsi_cmnd *dequeue_next_cmd(struct Scsi_Host *instance)
850{
851 struct NCR5380_hostdata *hostdata = shost_priv(instance);
852 struct NCR5380_cmd *ncmd;
853 struct scsi_cmnd *cmd;
854
855 if (hostdata->sensing || list_empty(&hostdata->autosense)) {
856 list_for_each_entry(ncmd, &hostdata->unissued, list) {
857 cmd = NCR5380_to_scmd(ncmd);
858 dsprintk(NDEBUG_QUEUES, instance, "dequeue: cmd=%p target=%d busy=0x%02x lun=%llu\n",
859 cmd, scmd_id(cmd), hostdata->busy[scmd_id(cmd)], cmd->device->lun);
860
861 if (
862#ifdef SUPPORT_TAGS
863 !is_lun_busy(cmd, 1)
864#else
865 !(hostdata->busy[scmd_id(cmd)] & (1 << cmd->device->lun))
866#endif
867 ) {
868 list_del(&ncmd->list);
869 dsprintk(NDEBUG_QUEUES, instance,
870 "dequeue: removed %p from issue queue\n", cmd);
871 return cmd;
872 }
873 }
874 } else {
875 /* Autosense processing begins here */
876 ncmd = list_first_entry(&hostdata->autosense,
877 struct NCR5380_cmd, list);
878 list_del(&ncmd->list);
879 cmd = NCR5380_to_scmd(ncmd);
880 dsprintk(NDEBUG_QUEUES, instance,
881 "dequeue: removed %p from autosense queue\n", cmd);
882 scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
883 hostdata->sensing = cmd;
884 return cmd;
885 }
886 return NULL;
887}
888
889static void requeue_cmd(struct Scsi_Host *instance, struct scsi_cmnd *cmd)
890{
891 struct NCR5380_hostdata *hostdata = shost_priv(instance);
892 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
893
894 if (hostdata->sensing == cmd) {
895 scsi_eh_restore_cmnd(cmd, &hostdata->ses);
896 list_add(&ncmd->list, &hostdata->autosense);
897 hostdata->sensing = NULL;
898 } else
899 list_add(&ncmd->list, &hostdata->unissued);
900}
901
902/**
903 * NCR5380_main - NCR state machines
904 *
905 * NCR5380_main is a coroutine that runs as long as more work can
906 * be done on the NCR5380 host adapters in a system. Both
907 * NCR5380_queue_command() and NCR5380_intr() will try to start it
908 * in case it is not running.
909 */
910
911static void NCR5380_main(struct work_struct *work)
912{
913 struct NCR5380_hostdata *hostdata =
914 container_of(work, struct NCR5380_hostdata, main_task);
915 struct Scsi_Host *instance = hostdata->host;
916 int done;
917
918 /*
919 * ++roman: Just disabling the NCR interrupt isn't sufficient here,
920 * because also a timer int can trigger an abort or reset, which can
921 * alter queues and touch the Falcon lock.
922 */
923
924 do {
925 done = 1;
926
927 spin_lock_irq(&hostdata->lock);
928 while (!hostdata->connected && !hostdata->selecting) {
929 struct scsi_cmnd *cmd = dequeue_next_cmd(instance);
930
931 if (!cmd)
932 break;
933
934 dsprintk(NDEBUG_MAIN, instance, "main: dequeued %p\n", cmd);
935
936 /*
937 * Attempt to establish an I_T_L nexus here.
938 * On success, instance->hostdata->connected is set.
939 * On failure, we must add the command back to the
940 * issue queue so we can keep trying.
941 */
942 /*
943 * REQUEST SENSE commands are issued without tagged
944 * queueing, even on SCSI-II devices because the
945 * contingent allegiance condition exists for the
946 * entire unit.
947 */
948 /* ++roman: ...and the standard also requires that
949 * REQUEST SENSE command are untagged.
950 */
951
952#ifdef SUPPORT_TAGS
953 cmd_get_tag(cmd, cmd->cmnd[0] != REQUEST_SENSE);
954#endif
955 if (!NCR5380_select(instance, cmd)) {
956 dsprintk(NDEBUG_MAIN, instance, "main: select complete\n");
957 maybe_release_dma_irq(instance);
958 } else {
959 dsprintk(NDEBUG_MAIN | NDEBUG_QUEUES, instance,
960 "main: select failed, returning %p to queue\n", cmd);
961 requeue_cmd(instance, cmd);
962#ifdef SUPPORT_TAGS
963 cmd_free_tag(cmd);
964#endif
965 }
966 }
967 if (hostdata->connected && !hostdata->dma_len) {
968 dsprintk(NDEBUG_MAIN, instance, "main: performing information transfer\n");
969 NCR5380_information_transfer(instance);
970 done = 0;
971 }
972 spin_unlock_irq(&hostdata->lock);
973 if (!done)
974 cond_resched();
975 } while (!done);
976}
977
978
979/*
980 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
981 *
982 * Purpose : Called by interrupt handler when DMA finishes or a phase
983 * mismatch occurs (which would finish the DMA transfer).
984 *
985 * Inputs : instance - this instance of the NCR5380.
986 */
987
988static void NCR5380_dma_complete(struct Scsi_Host *instance)
989{
990 struct NCR5380_hostdata *hostdata = shost_priv(instance);
991 int transferred;
992 unsigned char **data;
993 int *count;
994 int saved_data = 0, overrun = 0;
995 unsigned char p;
996
997 if (hostdata->read_overruns) {
998 p = hostdata->connected->SCp.phase;
999 if (p & SR_IO) {
1000 udelay(10);
1001 if ((NCR5380_read(BUS_AND_STATUS_REG) &
1002 (BASR_PHASE_MATCH|BASR_ACK)) ==
1003 (BASR_PHASE_MATCH|BASR_ACK)) {
1004 saved_data = NCR5380_read(INPUT_DATA_REG);
1005 overrun = 1;
1006 dsprintk(NDEBUG_DMA, instance, "read overrun handled\n");
1007 }
1008 }
1009 }
1010
1011#if defined(CONFIG_SUN3)
1012 if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) {
1013 pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n",
1014 instance->host_no);
1015 BUG();
1016 }
1017
1018 /* make sure we're not stuck in a data phase */
1019 if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) ==
1020 (BASR_PHASE_MATCH | BASR_ACK)) {
1021 pr_err("scsi%d: BASR %02x\n", instance->host_no,
1022 NCR5380_read(BUS_AND_STATUS_REG));
1023 pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n",
1024 instance->host_no);
1025 BUG();
1026 }
1027#endif
1028
1029 NCR5380_write(MODE_REG, MR_BASE);
1030 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1031 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1032
1033 transferred = hostdata->dma_len - NCR5380_dma_residual(instance);
1034 hostdata->dma_len = 0;
1035
1036 data = (unsigned char **)&hostdata->connected->SCp.ptr;
1037 count = &hostdata->connected->SCp.this_residual;
1038 *data += transferred;
1039 *count -= transferred;
1040
1041 if (hostdata->read_overruns) {
1042 int cnt, toPIO;
1043
1044 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
1045 cnt = toPIO = hostdata->read_overruns;
1046 if (overrun) {
1047 dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n");
1048 *(*data)++ = saved_data;
1049 (*count)--;
1050 cnt--;
1051 toPIO--;
1052 }
1053 dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
1054 NCR5380_transfer_pio(instance, &p, &cnt, data);
1055 *count -= toPIO - cnt;
1056 }
1057 }
1058}
1059
1060
1061/**
1062 * NCR5380_intr - generic NCR5380 irq handler
1063 * @irq: interrupt number
1064 * @dev_id: device info
1065 *
1066 * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
1067 * from the disconnected queue, and restarting NCR5380_main()
1068 * as required.
1069 *
1070 * The chip can assert IRQ in any of six different conditions. The IRQ flag
1071 * is then cleared by reading the Reset Parity/Interrupt Register (RPIR).
1072 * Three of these six conditions are latched in the Bus and Status Register:
1073 * - End of DMA (cleared by ending DMA Mode)
1074 * - Parity error (cleared by reading RPIR)
1075 * - Loss of BSY (cleared by reading RPIR)
1076 * Two conditions have flag bits that are not latched:
1077 * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode)
1078 * - Bus reset (non-maskable)
1079 * The remaining condition has no flag bit at all:
1080 * - Selection/reselection
1081 *
1082 * Hence, establishing the cause(s) of any interrupt is partly guesswork.
1083 * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor
1084 * claimed that "the design of the [DP8490] interrupt logic ensures
1085 * interrupts will not be lost (they can be on the DP5380)."
1086 * The L5380/53C80 datasheet from LOGIC Devices has more details.
1087 *
1088 * Checking for bus reset by reading RST is futile because of interrupt
1089 * latency, but a bus reset will reset chip logic. Checking for parity error
1090 * is unnecessary because that interrupt is never enabled. A Loss of BSY
1091 * condition will clear DMA Mode. We can tell when this occurs because the
1092 * the Busy Monitor interrupt is enabled together with DMA Mode.
1093 */
1094
1095static irqreturn_t NCR5380_intr(int irq, void *dev_id)
1096{
1097 struct Scsi_Host *instance = dev_id;
1098 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1099 int handled = 0;
1100 unsigned char basr;
1101 unsigned long flags;
1102
1103 spin_lock_irqsave(&hostdata->lock, flags);
1104
1105 basr = NCR5380_read(BUS_AND_STATUS_REG);
1106 if (basr & BASR_IRQ) {
1107 unsigned char mr = NCR5380_read(MODE_REG);
1108 unsigned char sr = NCR5380_read(STATUS_REG);
1109
1110 dsprintk(NDEBUG_INTR, instance, "IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n",
1111 irq, basr, sr, mr);
1112
1113 if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) {
1114 /* Probably End of DMA, Phase Mismatch or Loss of BSY.
1115 * We ack IRQ after clearing Mode Register. Workarounds
1116 * for End of DMA errata need to happen in DMA Mode.
1117 */
1118
1119 dsprintk(NDEBUG_INTR, instance, "interrupt in DMA mode\n");
1120
1121 if (hostdata->connected) {
1122 NCR5380_dma_complete(instance);
1123 queue_work(hostdata->work_q, &hostdata->main_task);
1124 } else {
1125 NCR5380_write(MODE_REG, MR_BASE);
1126 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1127 }
1128 } else if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) &&
1129 (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) {
1130 /* Probably reselected */
1131 NCR5380_write(SELECT_ENABLE_REG, 0);
1132 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1133
1134 dsprintk(NDEBUG_INTR, instance, "interrupt with SEL and IO\n");
1135
1136 if (!hostdata->connected) {
1137 NCR5380_reselect(instance);
1138 queue_work(hostdata->work_q, &hostdata->main_task);
1139 }
1140 if (!hostdata->connected)
1141 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1142 } else {
1143 /* Probably Bus Reset */
1144 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1145
1146 dsprintk(NDEBUG_INTR, instance, "unknown interrupt\n");
1147#ifdef SUN3_SCSI_VME
1148 dregs->csr |= CSR_DMA_ENABLE;
1149#endif
1150 }
1151 handled = 1;
1152 } else {
1153 shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n");
1154#ifdef SUN3_SCSI_VME
1155 dregs->csr |= CSR_DMA_ENABLE;
1156#endif
1157 }
1158
1159 spin_unlock_irqrestore(&hostdata->lock, flags);
1160
1161 return IRQ_RETVAL(handled);
1162}
1163
1164/*
1165 * Function : int NCR5380_select(struct Scsi_Host *instance,
1166 * struct scsi_cmnd *cmd)
1167 *
1168 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
1169 * including ARBITRATION, SELECTION, and initial message out for
1170 * IDENTIFY and queue messages.
1171 *
1172 * Inputs : instance - instantiation of the 5380 driver on which this
1173 * target lives, cmd - SCSI command to execute.
1174 *
1175 * Returns cmd if selection failed but should be retried,
1176 * NULL if selection failed and should not be retried, or
1177 * NULL if selection succeeded (hostdata->connected == cmd).
1178 *
1179 * Side effects :
1180 * If bus busy, arbitration failed, etc, NCR5380_select() will exit
1181 * with registers as they should have been on entry - ie
1182 * SELECT_ENABLE will be set appropriately, the NCR5380
1183 * will cease to drive any SCSI bus signals.
1184 *
1185 * If successful : I_T_L or I_T_L_Q nexus will be established,
1186 * instance->connected will be set to cmd.
1187 * SELECT interrupt will be disabled.
1188 *
1189 * If failed (no target) : cmd->scsi_done() will be called, and the
1190 * cmd->result host byte set to DID_BAD_TARGET.
1191 */
1192
1193static struct scsi_cmnd *NCR5380_select(struct Scsi_Host *instance,
1194 struct scsi_cmnd *cmd)
1195{
1196 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1197 unsigned char tmp[3], phase;
1198 unsigned char *data;
1199 int len;
1200 int err;
1201
1202 NCR5380_dprint(NDEBUG_ARBITRATION, instance);
1203 dsprintk(NDEBUG_ARBITRATION, instance, "starting arbitration, id = %d\n",
1204 instance->this_id);
1205
1206 /*
1207 * Arbitration and selection phases are slow and involve dropping the
1208 * lock, so we have to watch out for EH. An exception handler may
1209 * change 'selecting' to NULL. This function will then return NULL
1210 * so that the caller will forget about 'cmd'. (During information
1211 * transfer phases, EH may change 'connected' to NULL.)
1212 */
1213 hostdata->selecting = cmd;
1214
1215 /*
1216 * Set the phase bits to 0, otherwise the NCR5380 won't drive the
1217 * data bus during SELECTION.
1218 */
1219
1220 NCR5380_write(TARGET_COMMAND_REG, 0);
1221
1222 /*
1223 * Start arbitration.
1224 */
1225
1226 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
1227 NCR5380_write(MODE_REG, MR_ARBITRATE);
1228
1229 /* The chip now waits for BUS FREE phase. Then after the 800 ns
1230 * Bus Free Delay, arbitration will begin.
1231 */
1232
1233 spin_unlock_irq(&hostdata->lock);
1234 err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0,
1235 INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS,
1236 ICR_ARBITRATION_PROGRESS, HZ);
1237 spin_lock_irq(&hostdata->lock);
1238 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) {
1239 /* Reselection interrupt */
1240 goto out;
1241 }
1242 if (!hostdata->selecting) {
1243 /* Command was aborted */
1244 NCR5380_write(MODE_REG, MR_BASE);
1245 goto out;
1246 }
1247 if (err < 0) {
1248 NCR5380_write(MODE_REG, MR_BASE);
1249 shost_printk(KERN_ERR, instance,
1250 "select: arbitration timeout\n");
1251 goto out;
1252 }
1253 spin_unlock_irq(&hostdata->lock);
1254
1255 /* The SCSI-2 arbitration delay is 2.4 us */
1256 udelay(3);
1257
1258 /* Check for lost arbitration */
1259 if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
1260 (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
1261 (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
1262 NCR5380_write(MODE_REG, MR_BASE);
1263 dsprintk(NDEBUG_ARBITRATION, instance, "lost arbitration, deasserting MR_ARBITRATE\n");
1264 spin_lock_irq(&hostdata->lock);
1265 goto out;
1266 }
1267
1268 /* After/during arbitration, BSY should be asserted.
1269 * IBM DPES-31080 Version S31Q works now
1270 * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman)
1271 */
1272 NCR5380_write(INITIATOR_COMMAND_REG,
1273 ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY);
1274
1275 /*
1276 * Again, bus clear + bus settle time is 1.2us, however, this is
1277 * a minimum so we'll udelay ceil(1.2)
1278 */
1279
1280 if (hostdata->flags & FLAG_TOSHIBA_DELAY)
1281 udelay(15);
1282 else
1283 udelay(2);
1284
1285 spin_lock_irq(&hostdata->lock);
1286
1287 /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */
1288 if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE))
1289 goto out;
1290
1291 if (!hostdata->selecting) {
1292 NCR5380_write(MODE_REG, MR_BASE);
1293 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1294 goto out;
1295 }
1296
1297 dsprintk(NDEBUG_ARBITRATION, instance, "won arbitration\n");
1298
1299 /*
1300 * Now that we have won arbitration, start Selection process, asserting
1301 * the host and target ID's on the SCSI bus.
1302 */
1303
1304 NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask | (1 << scmd_id(cmd)));
1305
1306 /*
1307 * Raise ATN while SEL is true before BSY goes false from arbitration,
1308 * since this is the only way to guarantee that we'll get a MESSAGE OUT
1309 * phase immediately after selection.
1310 */
1311
1312 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY |
1313 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1314 NCR5380_write(MODE_REG, MR_BASE);
1315
1316 /*
1317 * Reselect interrupts must be turned off prior to the dropping of BSY,
1318 * otherwise we will trigger an interrupt.
1319 */
1320 NCR5380_write(SELECT_ENABLE_REG, 0);
1321
1322 spin_unlock_irq(&hostdata->lock);
1323
1324 /*
1325 * The initiator shall then wait at least two deskew delays and release
1326 * the BSY signal.
1327 */
1328 udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
1329
1330 /* Reset BSY */
1331 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA |
1332 ICR_ASSERT_ATN | ICR_ASSERT_SEL);
1333
1334 /*
1335 * Something weird happens when we cease to drive BSY - looks
1336 * like the board/chip is letting us do another read before the
1337 * appropriate propagation delay has expired, and we're confusing
1338 * a BSY signal from ourselves as the target's response to SELECTION.
1339 *
1340 * A small delay (the 'C++' frontend breaks the pipeline with an
1341 * unnecessary jump, making it work on my 386-33/Trantor T128, the
1342 * tighter 'C' code breaks and requires this) solves the problem -
1343 * the 1 us delay is arbitrary, and only used because this delay will
1344 * be the same on other platforms and since it works here, it should
1345 * work there.
1346 *
1347 * wingel suggests that this could be due to failing to wait
1348 * one deskew delay.
1349 */
1350
1351 udelay(1);
1352
1353 dsprintk(NDEBUG_SELECTION, instance, "selecting target %d\n", scmd_id(cmd));
1354
1355 /*
1356 * The SCSI specification calls for a 250 ms timeout for the actual
1357 * selection.
1358 */
1359
1360 err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY,
1361 msecs_to_jiffies(250));
1362
1363 if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
1364 spin_lock_irq(&hostdata->lock);
1365 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1366 NCR5380_reselect(instance);
1367 if (!hostdata->connected)
1368 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1369 shost_printk(KERN_ERR, instance, "reselection after won arbitration?\n");
1370 goto out;
1371 }
1372
1373 if (err < 0) {
1374 spin_lock_irq(&hostdata->lock);
1375 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1376 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1377 /* Can't touch cmd if it has been reclaimed by the scsi ML */
1378 if (hostdata->selecting) {
1379 cmd->result = DID_BAD_TARGET << 16;
1380 complete_cmd(instance, cmd);
1381 dsprintk(NDEBUG_SELECTION, instance, "target did not respond within 250ms\n");
1382 cmd = NULL;
1383 }
1384 goto out;
1385 }
1386
1387 /*
1388 * No less than two deskew delays after the initiator detects the
1389 * BSY signal is true, it shall release the SEL signal and may
1390 * change the DATA BUS. -wingel
1391 */
1392
1393 udelay(1);
1394
1395 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1396
1397 /*
1398 * Since we followed the SCSI spec, and raised ATN while SEL
1399 * was true but before BSY was false during selection, the information
1400 * transfer phase should be a MESSAGE OUT phase so that we can send the
1401 * IDENTIFY message.
1402 *
1403 * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
1404 * message (2 bytes) with a tag ID that we increment with every command
1405 * until it wraps back to 0.
1406 *
1407 * XXX - it turns out that there are some broken SCSI-II devices,
1408 * which claim to support tagged queuing but fail when more than
1409 * some number of commands are issued at once.
1410 */
1411
1412 /* Wait for start of REQ/ACK handshake */
1413
1414 err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
1415 spin_lock_irq(&hostdata->lock);
1416 if (err < 0) {
1417 shost_printk(KERN_ERR, instance, "select: REQ timeout\n");
1418 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1419 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
1420 goto out;
1421 }
1422 if (!hostdata->selecting) {
1423 do_abort(instance);
1424 goto out;
1425 }
1426
1427 dsprintk(NDEBUG_SELECTION, instance, "target %d selected, going into MESSAGE OUT phase.\n",
1428 scmd_id(cmd));
1429 tmp[0] = IDENTIFY(1, cmd->device->lun);
1430
1431#ifdef SUPPORT_TAGS
1432 if (cmd->tag != TAG_NONE) {
1433 tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
1434 tmp[2] = cmd->tag;
1435 len = 3;
1436 } else
1437 len = 1;
1438#else
1439 len = 1;
1440 cmd->tag = 0;
1441#endif /* SUPPORT_TAGS */
1442
1443 /* Send message(s) */
1444 data = tmp;
1445 phase = PHASE_MSGOUT;
1446 NCR5380_transfer_pio(instance, &phase, &len, &data);
1447 dsprintk(NDEBUG_SELECTION, instance, "nexus established.\n");
1448 /* XXX need to handle errors here */
1449
1450 hostdata->connected = cmd;
1451#ifndef SUPPORT_TAGS
1452 hostdata->busy[cmd->device->id] |= 1 << cmd->device->lun;
1453#endif
1454#ifdef SUN3_SCSI_VME
1455 dregs->csr |= CSR_INTR;
1456#endif
1457
1458 initialize_SCp(cmd);
1459
1460 cmd = NULL;
1461
1462out:
1463 if (!hostdata->selecting)
1464 return NULL;
1465 hostdata->selecting = NULL;
1466 return cmd;
1467}
1468
1469/*
1470 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
1471 * unsigned char *phase, int *count, unsigned char **data)
1472 *
1473 * Purpose : transfers data in given phase using polled I/O
1474 *
1475 * Inputs : instance - instance of driver, *phase - pointer to
1476 * what phase is expected, *count - pointer to number of
1477 * bytes to transfer, **data - pointer to data pointer.
1478 *
1479 * Returns : -1 when different phase is entered without transferring
1480 * maximum number of bytes, 0 if all bytes are transferred or exit
1481 * is in same phase.
1482 *
1483 * Also, *phase, *count, *data are modified in place.
1484 *
1485 * XXX Note : handling for bus free may be useful.
1486 */
1487
1488/*
1489 * Note : this code is not as quick as it could be, however it
1490 * IS 100% reliable, and for the actual data transfer where speed
1491 * counts, we will always do a pseudo DMA or DMA transfer.
1492 */
1493
1494static int NCR5380_transfer_pio(struct Scsi_Host *instance,
1495 unsigned char *phase, int *count,
1496 unsigned char **data)
1497{
1498 unsigned char p = *phase, tmp;
1499 int c = *count;
1500 unsigned char *d = *data;
1501
1502 /*
1503 * The NCR5380 chip will only drive the SCSI bus when the
1504 * phase specified in the appropriate bits of the TARGET COMMAND
1505 * REGISTER match the STATUS REGISTER
1506 */
1507
1508 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1509
1510 do {
1511 /*
1512 * Wait for assertion of REQ, after which the phase bits will be
1513 * valid
1514 */
1515
1516 if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0)
1517 break;
1518
1519 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ asserted\n");
1520
1521 /* Check for phase mismatch */
1522 if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) {
1523 dsprintk(NDEBUG_PIO, instance, "phase mismatch\n");
1524 NCR5380_dprint_phase(NDEBUG_PIO, instance);
1525 break;
1526 }
1527
1528 /* Do actual transfer from SCSI bus to / from memory */
1529 if (!(p & SR_IO))
1530 NCR5380_write(OUTPUT_DATA_REG, *d);
1531 else
1532 *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
1533
1534 ++d;
1535
1536 /*
1537 * The SCSI standard suggests that in MSGOUT phase, the initiator
1538 * should drop ATN on the last byte of the message phase
1539 * after REQ has been asserted for the handshake but before
1540 * the initiator raises ACK.
1541 */
1542
1543 if (!(p & SR_IO)) {
1544 if (!((p & SR_MSG) && c > 1)) {
1545 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1546 NCR5380_dprint(NDEBUG_PIO, instance);
1547 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1548 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1549 } else {
1550 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1551 ICR_ASSERT_DATA | ICR_ASSERT_ATN);
1552 NCR5380_dprint(NDEBUG_PIO, instance);
1553 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1554 ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1555 }
1556 } else {
1557 NCR5380_dprint(NDEBUG_PIO, instance);
1558 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
1559 }
1560
1561 if (NCR5380_poll_politely(instance,
1562 STATUS_REG, SR_REQ, 0, 5 * HZ) < 0)
1563 break;
1564
1565 dsprintk(NDEBUG_HANDSHAKE, instance, "REQ negated, handshake complete\n");
1566
1567/*
1568 * We have several special cases to consider during REQ/ACK handshaking :
1569 * 1. We were in MSGOUT phase, and we are on the last byte of the
1570 * message. ATN must be dropped as ACK is dropped.
1571 *
1572 * 2. We are in a MSGIN phase, and we are on the last byte of the
1573 * message. We must exit with ACK asserted, so that the calling
1574 * code may raise ATN before dropping ACK to reject the message.
1575 *
1576 * 3. ACK and ATN are clear and the target may proceed as normal.
1577 */
1578 if (!(p == PHASE_MSGIN && c == 1)) {
1579 if (p == PHASE_MSGOUT && c > 1)
1580 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1581 else
1582 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1583 }
1584 } while (--c);
1585
1586 dsprintk(NDEBUG_PIO, instance, "residual %d\n", c);
1587
1588 *count = c;
1589 *data = d;
1590 tmp = NCR5380_read(STATUS_REG);
1591 /* The phase read from the bus is valid if either REQ is (already)
1592 * asserted or if ACK hasn't been released yet. The latter applies if
1593 * we're in MSG IN, DATA IN or STATUS and all bytes have been received.
1594 */
1595 if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0))
1596 *phase = tmp & PHASE_MASK;
1597 else
1598 *phase = PHASE_UNKNOWN;
1599
1600 if (!c || (*phase == p))
1601 return 0;
1602 else
1603 return -1;
1604}
1605
1606/**
1607 * do_reset - issue a reset command
1608 * @instance: adapter to reset
1609 *
1610 * Issue a reset sequence to the NCR5380 and try and get the bus
1611 * back into sane shape.
1612 *
1613 * This clears the reset interrupt flag because there may be no handler for
1614 * it. When the driver is initialized, the NCR5380_intr() handler has not yet
1615 * been installed. And when in EH we may have released the ST DMA interrupt.
1616 */
1617
1618static void do_reset(struct Scsi_Host *instance)
1619{
1620 unsigned long flags;
1621
1622 local_irq_save(flags);
1623 NCR5380_write(TARGET_COMMAND_REG,
1624 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
1625 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
1626 udelay(50);
1627 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1628 (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
1629 local_irq_restore(flags);
1630}
1631
1632/**
1633 * do_abort - abort the currently established nexus by going to
1634 * MESSAGE OUT phase and sending an ABORT message.
1635 * @instance: relevant scsi host instance
1636 *
1637 * Returns 0 on success, -1 on failure.
1638 */
1639
1640static int do_abort(struct Scsi_Host *instance)
1641{
1642 unsigned char *msgptr, phase, tmp;
1643 int len;
1644 int rc;
1645
1646 /* Request message out phase */
1647 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1648
1649 /*
1650 * Wait for the target to indicate a valid phase by asserting
1651 * REQ. Once this happens, we'll have either a MSGOUT phase
1652 * and can immediately send the ABORT message, or we'll have some
1653 * other phase and will have to source/sink data.
1654 *
1655 * We really don't care what value was on the bus or what value
1656 * the target sees, so we just handshake.
1657 */
1658
1659 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ);
1660 if (rc < 0)
1661 goto timeout;
1662
1663 tmp = NCR5380_read(STATUS_REG) & PHASE_MASK;
1664
1665 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1666
1667 if (tmp != PHASE_MSGOUT) {
1668 NCR5380_write(INITIATOR_COMMAND_REG,
1669 ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
1670 rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ);
1671 if (rc < 0)
1672 goto timeout;
1673 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
1674 }
1675
1676 tmp = ABORT;
1677 msgptr = &tmp;
1678 len = 1;
1679 phase = PHASE_MSGOUT;
1680 NCR5380_transfer_pio(instance, &phase, &len, &msgptr);
1681
1682 /*
1683 * If we got here, and the command completed successfully,
1684 * we're about to go into bus free state.
1685 */
1686
1687 return len ? -1 : 0;
1688
1689timeout:
1690 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1691 return -1;
1692}
1693
1694
1695/*
1696 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
1697 * unsigned char *phase, int *count, unsigned char **data)
1698 *
1699 * Purpose : transfers data in given phase using either real
1700 * or pseudo DMA.
1701 *
1702 * Inputs : instance - instance of driver, *phase - pointer to
1703 * what phase is expected, *count - pointer to number of
1704 * bytes to transfer, **data - pointer to data pointer.
1705 *
1706 * Returns : -1 when different phase is entered without transferring
1707 * maximum number of bytes, 0 if all bytes or transferred or exit
1708 * is in same phase.
1709 *
1710 * Also, *phase, *count, *data are modified in place.
1711 */
1712
1713
1714static int NCR5380_transfer_dma(struct Scsi_Host *instance,
1715 unsigned char *phase, int *count,
1716 unsigned char **data)
1717{
1718 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1719 register int c = *count;
1720 register unsigned char p = *phase;
1721
1722#if defined(CONFIG_SUN3)
1723 /* sanity check */
1724 if (!sun3_dma_setup_done) {
1725 pr_err("scsi%d: transfer_dma without setup!\n",
1726 instance->host_no);
1727 BUG();
1728 }
1729 hostdata->dma_len = c;
1730
1731 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1732 (p & SR_IO) ? "receive" : "send", c, *data);
1733
1734 /* netbsd turns off ints here, why not be safe and do it too */
1735
1736 /* send start chain */
1737 sun3scsi_dma_start(c, *data);
1738
1739 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1740 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1741 MR_ENABLE_EOP_INTR);
1742 if (p & SR_IO) {
1743 NCR5380_write(INITIATOR_COMMAND_REG, 0);
1744 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1745 } else {
1746 NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA);
1747 NCR5380_write(START_DMA_SEND_REG, 0);
1748 }
1749
1750#ifdef SUN3_SCSI_VME
1751 dregs->csr |= CSR_DMA_ENABLE;
1752#endif
1753
1754 sun3_dma_active = 1;
1755
1756#else /* !defined(CONFIG_SUN3) */
1757 register unsigned char *d = *data;
1758 unsigned char tmp;
1759
1760 if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
1761 *phase = tmp;
1762 return -1;
1763 }
1764
1765 if (hostdata->read_overruns && (p & SR_IO))
1766 c -= hostdata->read_overruns;
1767
1768 dsprintk(NDEBUG_DMA, instance, "initializing DMA %s: length %d, address %p\n",
1769 (p & SR_IO) ? "receive" : "send", c, d);
1770
1771 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
1772 NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY |
1773 MR_ENABLE_EOP_INTR);
1774
1775 if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) {
1776 /* On the Medusa, it is a must to initialize the DMA before
1777 * starting the NCR. This is also the cleaner way for the TT.
1778 */
1779 hostdata->dma_len = (p & SR_IO) ?
1780 NCR5380_dma_read_setup(instance, d, c) :
1781 NCR5380_dma_write_setup(instance, d, c);
1782 }
1783
1784 if (p & SR_IO)
1785 NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
1786 else {
1787 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
1788 NCR5380_write(START_DMA_SEND_REG, 0);
1789 }
1790
1791 if (hostdata->flags & FLAG_LATE_DMA_SETUP) {
1792 /* On the Falcon, the DMA setup must be done after the last */
1793 /* NCR access, else the DMA setup gets trashed!
1794 */
1795 hostdata->dma_len = (p & SR_IO) ?
1796 NCR5380_dma_read_setup(instance, d, c) :
1797 NCR5380_dma_write_setup(instance, d, c);
1798 }
1799#endif /* !defined(CONFIG_SUN3) */
1800
1801 return 0;
1802}
1803
1804/*
1805 * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
1806 *
1807 * Purpose : run through the various SCSI phases and do as the target
1808 * directs us to. Operates on the currently connected command,
1809 * instance->connected.
1810 *
1811 * Inputs : instance, instance for which we are doing commands
1812 *
1813 * Side effects : SCSI things happen, the disconnected queue will be
1814 * modified if a command disconnects, *instance->connected will
1815 * change.
1816 *
1817 * XXX Note : we need to watch for bus free or a reset condition here
1818 * to recover from an unexpected bus free condition.
1819 */
1820
1821static void NCR5380_information_transfer(struct Scsi_Host *instance)
1822{
1823 struct NCR5380_hostdata *hostdata = shost_priv(instance);
1824 unsigned char msgout = NOP;
1825 int sink = 0;
1826 int len;
1827 int transfersize;
1828 unsigned char *data;
1829 unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
1830 struct scsi_cmnd *cmd;
1831
1832#ifdef SUN3_SCSI_VME
1833 dregs->csr |= CSR_INTR;
1834#endif
1835
1836 while ((cmd = hostdata->connected)) {
1837 struct NCR5380_cmd *ncmd = scsi_cmd_priv(cmd);
1838
1839 tmp = NCR5380_read(STATUS_REG);
1840 /* We only have a valid SCSI phase when REQ is asserted */
1841 if (tmp & SR_REQ) {
1842 phase = (tmp & PHASE_MASK);
1843 if (phase != old_phase) {
1844 old_phase = phase;
1845 NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
1846 }
1847#if defined(CONFIG_SUN3)
1848 if (phase == PHASE_CMDOUT) {
1849 void *d;
1850 unsigned long count;
1851
1852 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1853 count = cmd->SCp.buffer->length;
1854 d = sg_virt(cmd->SCp.buffer);
1855 } else {
1856 count = cmd->SCp.this_residual;
1857 d = cmd->SCp.ptr;
1858 }
1859 /* this command setup for dma yet? */
1860 if (sun3_dma_setup_done != cmd &&
1861 sun3scsi_dma_xfer_len(count, cmd) > 0) {
1862 sun3scsi_dma_setup(instance, d, count,
1863 rq_data_dir(cmd->request));
1864 sun3_dma_setup_done = cmd;
1865 }
1866#ifdef SUN3_SCSI_VME
1867 dregs->csr |= CSR_INTR;
1868#endif
1869 }
1870#endif /* CONFIG_SUN3 */
1871
1872 if (sink && (phase != PHASE_MSGOUT)) {
1873 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
1874
1875 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN |
1876 ICR_ASSERT_ACK);
1877 while (NCR5380_read(STATUS_REG) & SR_REQ)
1878 ;
1879 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1880 ICR_ASSERT_ATN);
1881 sink = 0;
1882 continue;
1883 }
1884
1885 switch (phase) {
1886 case PHASE_DATAOUT:
1887#if (NDEBUG & NDEBUG_NO_DATAOUT)
1888 shost_printk(KERN_DEBUG, instance, "NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n");
1889 sink = 1;
1890 do_abort(instance);
1891 cmd->result = DID_ERROR << 16;
1892 complete_cmd(instance, cmd);
1893 hostdata->connected = NULL;
1894 return;
1895#endif
1896 case PHASE_DATAIN:
1897 /*
1898 * If there is no room left in the current buffer in the
1899 * scatter-gather list, move onto the next one.
1900 */
1901
1902 if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
1903 ++cmd->SCp.buffer;
1904 --cmd->SCp.buffers_residual;
1905 cmd->SCp.this_residual = cmd->SCp.buffer->length;
1906 cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
1907 merge_contiguous_buffers(cmd);
1908 dsprintk(NDEBUG_INFORMATION, instance, "%d bytes and %d buffers left\n",
1909 cmd->SCp.this_residual,
1910 cmd->SCp.buffers_residual);
1911 }
1912
1913 /*
1914 * The preferred transfer method is going to be
1915 * PSEUDO-DMA for systems that are strictly PIO,
1916 * since we can let the hardware do the handshaking.
1917 *
1918 * For this to work, we need to know the transfersize
1919 * ahead of time, since the pseudo-DMA code will sit
1920 * in an unconditional loop.
1921 */
1922
1923#if !defined(CONFIG_SUN3)
1924 transfersize = 0;
1925 if (!cmd->device->borken)
1926#endif
1927 transfersize = NCR5380_dma_xfer_len(instance, cmd, phase);
1928
1929 if (transfersize > 0) {
1930 len = transfersize;
1931 cmd->SCp.phase = phase;
1932 if (NCR5380_transfer_dma(instance, &phase,
1933 &len, (unsigned char **)&cmd->SCp.ptr)) {
1934 /*
1935 * If the watchdog timer fires, all future
1936 * accesses to this device will use the
1937 * polled-IO.
1938 */
1939 scmd_printk(KERN_INFO, cmd,
1940 "switching to slow handshake\n");
1941 cmd->device->borken = 1;
1942 sink = 1;
1943 do_abort(instance);
1944 cmd->result = DID_ERROR << 16;
1945 /* XXX - need to source or sink data here, as appropriate */
1946 } else
1947 return;
1948 } else {
1949 /* Break up transfer into 3 ms chunks,
1950 * presuming 6 accesses per handshake.
1951 */
1952 transfersize = min((unsigned long)cmd->SCp.this_residual,
1953 hostdata->accesses_per_ms / 2);
1954 len = transfersize;
1955 NCR5380_transfer_pio(instance, &phase, &len,
1956 (unsigned char **)&cmd->SCp.ptr);
1957 cmd->SCp.this_residual -= transfersize - len;
1958 }
1959#if defined(CONFIG_SUN3)
1960 /* if we had intended to dma that command clear it */
1961 if (sun3_dma_setup_done == cmd)
1962 sun3_dma_setup_done = NULL;
1963#endif
1964 return;
1965 case PHASE_MSGIN:
1966 len = 1;
1967 data = &tmp;
1968 NCR5380_transfer_pio(instance, &phase, &len, &data);
1969 cmd->SCp.Message = tmp;
1970
1971 switch (tmp) {
1972 case ABORT:
1973 case COMMAND_COMPLETE:
1974 /* Accept message by clearing ACK */
1975 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1976 dsprintk(NDEBUG_QUEUES, instance,
1977 "COMMAND COMPLETE %p target %d lun %llu\n",
1978 cmd, scmd_id(cmd), cmd->device->lun);
1979
1980 hostdata->connected = NULL;
1981#ifdef SUPPORT_TAGS
1982 cmd_free_tag(cmd);
1983 if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
1984 u8 lun = cmd->device->lun;
1985 struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun];
1986
1987 dsprintk(NDEBUG_TAGS, instance,
1988 "QUEUE_FULL %p target %d lun %d nr_allocated %d\n",
1989 cmd, scmd_id(cmd), lun, ta->nr_allocated);
1990 if (ta->queue_size > ta->nr_allocated)
1991 ta->queue_size = ta->nr_allocated;
1992 }
1993#endif
1994
1995 cmd->result &= ~0xffff;
1996 cmd->result |= cmd->SCp.Status;
1997 cmd->result |= cmd->SCp.Message << 8;
1998
1999 if (cmd->cmnd[0] == REQUEST_SENSE)
2000 complete_cmd(instance, cmd);
2001 else {
2002 if (cmd->SCp.Status == SAM_STAT_CHECK_CONDITION ||
2003 cmd->SCp.Status == SAM_STAT_COMMAND_TERMINATED) {
2004 dsprintk(NDEBUG_QUEUES, instance, "autosense: adding cmd %p to tail of autosense queue\n",
2005 cmd);
2006 list_add_tail(&ncmd->list,
2007 &hostdata->autosense);
2008 } else
2009 complete_cmd(instance, cmd);
2010 }
2011
2012 /*
2013 * Restore phase bits to 0 so an interrupted selection,
2014 * arbitration can resume.
2015 */
2016 NCR5380_write(TARGET_COMMAND_REG, 0);
2017
2018 /* Enable reselect interrupts */
2019 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2020
2021 maybe_release_dma_irq(instance);
2022 return;
2023 case MESSAGE_REJECT:
2024 /* Accept message by clearing ACK */
2025 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2026 switch (hostdata->last_message) {
2027 case HEAD_OF_QUEUE_TAG:
2028 case ORDERED_QUEUE_TAG:
2029 case SIMPLE_QUEUE_TAG:
2030 /* The target obviously doesn't support tagged
2031 * queuing, even though it announced this ability in
2032 * its INQUIRY data ?!? (maybe only this LUN?) Ok,
2033 * clear 'tagged_supported' and lock the LUN, since
2034 * the command is treated as untagged further on.
2035 */
2036 cmd->device->tagged_supported = 0;
2037 hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
2038 cmd->tag = TAG_NONE;
2039 dsprintk(NDEBUG_TAGS, instance, "target %d lun %llu rejected QUEUE_TAG message; tagged queuing disabled\n",
2040 scmd_id(cmd), cmd->device->lun);
2041 break;
2042 }
2043 break;
2044 case DISCONNECT:
2045 /* Accept message by clearing ACK */
2046 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2047 hostdata->connected = NULL;
2048 list_add(&ncmd->list, &hostdata->disconnected);
2049 dsprintk(NDEBUG_INFORMATION | NDEBUG_QUEUES,
2050 instance, "connected command %p for target %d lun %llu moved to disconnected queue\n",
2051 cmd, scmd_id(cmd), cmd->device->lun);
2052
2053 /*
2054 * Restore phase bits to 0 so an interrupted selection,
2055 * arbitration can resume.
2056 */
2057 NCR5380_write(TARGET_COMMAND_REG, 0);
2058
2059 /* Enable reselect interrupts */
2060 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2061#ifdef SUN3_SCSI_VME
2062 dregs->csr |= CSR_DMA_ENABLE;
2063#endif
2064 return;
2065 /*
2066 * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
2067 * operation, in violation of the SCSI spec so we can safely
2068 * ignore SAVE/RESTORE pointers calls.
2069 *
2070 * Unfortunately, some disks violate the SCSI spec and
2071 * don't issue the required SAVE_POINTERS message before
2072 * disconnecting, and we have to break spec to remain
2073 * compatible.
2074 */
2075 case SAVE_POINTERS:
2076 case RESTORE_POINTERS:
2077 /* Accept message by clearing ACK */
2078 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2079 break;
2080 case EXTENDED_MESSAGE:
2081 /*
2082 * Start the message buffer with the EXTENDED_MESSAGE
2083 * byte, since spi_print_msg() wants the whole thing.
2084 */
2085 extended_msg[0] = EXTENDED_MESSAGE;
2086 /* Accept first byte by clearing ACK */
2087 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2088
2089 spin_unlock_irq(&hostdata->lock);
2090
2091 dsprintk(NDEBUG_EXTENDED, instance, "receiving extended message\n");
2092
2093 len = 2;
2094 data = extended_msg + 1;
2095 phase = PHASE_MSGIN;
2096 NCR5380_transfer_pio(instance, &phase, &len, &data);
2097 dsprintk(NDEBUG_EXTENDED, instance, "length %d, code 0x%02x\n",
2098 (int)extended_msg[1],
2099 (int)extended_msg[2]);
2100
2101 if (!len && extended_msg[1] > 0 &&
2102 extended_msg[1] <= sizeof(extended_msg) - 2) {
2103 /* Accept third byte by clearing ACK */
2104 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2105 len = extended_msg[1] - 1;
2106 data = extended_msg + 3;
2107 phase = PHASE_MSGIN;
2108
2109 NCR5380_transfer_pio(instance, &phase, &len, &data);
2110 dsprintk(NDEBUG_EXTENDED, instance, "message received, residual %d\n",
2111 len);
2112
2113 switch (extended_msg[2]) {
2114 case EXTENDED_SDTR:
2115 case EXTENDED_WDTR:
2116 case EXTENDED_MODIFY_DATA_POINTER:
2117 case EXTENDED_EXTENDED_IDENTIFY:
2118 tmp = 0;
2119 }
2120 } else if (len) {
2121 shost_printk(KERN_ERR, instance, "error receiving extended message\n");
2122 tmp = 0;
2123 } else {
2124 shost_printk(KERN_NOTICE, instance, "extended message code %02x length %d is too long\n",
2125 extended_msg[2], extended_msg[1]);
2126 tmp = 0;
2127 }
2128
2129 spin_lock_irq(&hostdata->lock);
2130 if (!hostdata->connected)
2131 return;
2132
2133 /* Fall through to reject message */
2134
2135 /*
2136 * If we get something weird that we aren't expecting,
2137 * reject it.
2138 */
2139 default:
2140 if (!tmp) {
2141 shost_printk(KERN_ERR, instance, "rejecting message ");
2142 spi_print_msg(extended_msg);
2143 printk("\n");
2144 } else if (tmp != EXTENDED_MESSAGE)
2145 scmd_printk(KERN_INFO, cmd,
2146 "rejecting unknown message %02x\n",
2147 tmp);
2148 else
2149 scmd_printk(KERN_INFO, cmd,
2150 "rejecting unknown extended message code %02x, length %d\n",
2151 extended_msg[1], extended_msg[0]);
2152
2153 msgout = MESSAGE_REJECT;
2154 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
2155 break;
2156 } /* switch (tmp) */
2157 break;
2158 case PHASE_MSGOUT:
2159 len = 1;
2160 data = &msgout;
2161 hostdata->last_message = msgout;
2162 NCR5380_transfer_pio(instance, &phase, &len, &data);
2163 if (msgout == ABORT) {
2164 hostdata->connected = NULL;
2165 cmd->result = DID_ERROR << 16;
2166 complete_cmd(instance, cmd);
2167 maybe_release_dma_irq(instance);
2168 NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
2169 return;
2170 }
2171 msgout = NOP;
2172 break;
2173 case PHASE_CMDOUT:
2174 len = cmd->cmd_len;
2175 data = cmd->cmnd;
2176 /*
2177 * XXX for performance reasons, on machines with a
2178 * PSEUDO-DMA architecture we should probably
2179 * use the dma transfer function.
2180 */
2181 NCR5380_transfer_pio(instance, &phase, &len, &data);
2182 break;
2183 case PHASE_STATIN:
2184 len = 1;
2185 data = &tmp;
2186 NCR5380_transfer_pio(instance, &phase, &len, &data);
2187 cmd->SCp.Status = tmp;
2188 break;
2189 default:
2190 shost_printk(KERN_ERR, instance, "unknown phase\n");
2191 NCR5380_dprint(NDEBUG_ANY, instance);
2192 } /* switch(phase) */
2193 } else {
2194 spin_unlock_irq(&hostdata->lock);
2195 NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
2196 spin_lock_irq(&hostdata->lock);
2197 }
2198 }
2199}
2200
2201/*
2202 * Function : void NCR5380_reselect (struct Scsi_Host *instance)
2203 *
2204 * Purpose : does reselection, initializing the instance->connected
2205 * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q
2206 * nexus has been reestablished,
2207 *
2208 * Inputs : instance - this instance of the NCR5380.
2209 */
2210
2211
2212/* it might eventually prove necessary to do a dma setup on
2213 reselection, but it doesn't seem to be needed now -- sam */
2214
2215static void NCR5380_reselect(struct Scsi_Host *instance)
2216{
2217 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2218 unsigned char target_mask;
2219 unsigned char lun;
2220#ifdef SUPPORT_TAGS
2221 unsigned char tag;
2222#endif
2223 unsigned char msg[3];
2224 int __maybe_unused len;
2225 unsigned char __maybe_unused *data, __maybe_unused phase;
2226 struct NCR5380_cmd *ncmd;
2227 struct scsi_cmnd *tmp;
2228
2229 /*
2230 * Disable arbitration, etc. since the host adapter obviously
2231 * lost, and tell an interrupted NCR5380_select() to restart.
2232 */
2233
2234 NCR5380_write(MODE_REG, MR_BASE);
2235
2236 target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
2237
2238 dsprintk(NDEBUG_RESELECTION, instance, "reselect\n");
2239
2240 /*
2241 * At this point, we have detected that our SCSI ID is on the bus,
2242 * SEL is true and BSY was false for at least one bus settle delay
2243 * (400 ns).
2244 *
2245 * We must assert BSY ourselves, until the target drops the SEL
2246 * signal.
2247 */
2248
2249 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
2250 if (NCR5380_poll_politely(instance,
2251 STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) {
2252 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2253 return;
2254 }
2255 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2256
2257 /*
2258 * Wait for target to go into MSGIN.
2259 */
2260
2261 if (NCR5380_poll_politely(instance,
2262 STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) {
2263 do_abort(instance);
2264 return;
2265 }
2266
2267#if defined(CONFIG_SUN3)
2268 /* acknowledge toggle to MSGIN */
2269 NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN));
2270
2271 /* peek at the byte without really hitting the bus */
2272 msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG);
2273#else
2274 len = 1;
2275 data = msg;
2276 phase = PHASE_MSGIN;
2277 NCR5380_transfer_pio(instance, &phase, &len, &data);
2278
2279 if (len) {
2280 do_abort(instance);
2281 return;
2282 }
2283#endif
2284
2285 if (!(msg[0] & 0x80)) {
2286 shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got ");
2287 spi_print_msg(msg);
2288 printk("\n");
2289 do_abort(instance);
2290 return;
2291 }
2292 lun = msg[0] & 0x07;
2293
2294#if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3)
2295 /* If the phase is still MSGIN, the target wants to send some more
2296 * messages. In case it supports tagged queuing, this is probably a
2297 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2298 */
2299 tag = TAG_NONE;
2300 if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) {
2301 /* Accept previous IDENTIFY message by clearing ACK */
2302 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2303 len = 2;
2304 data = msg + 1;
2305 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2306 msg[1] == SIMPLE_QUEUE_TAG)
2307 tag = msg[2];
2308 dsprintk(NDEBUG_TAGS, instance, "reselect: target mask %02x, lun %d sent tag %d\n",
2309 target_mask, lun, tag);
2310 }
2311#endif
2312
2313 /*
2314 * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
2315 * just reestablished, and remove it from the disconnected queue.
2316 */
2317
2318 tmp = NULL;
2319 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2320 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2321
2322 if (target_mask == (1 << scmd_id(cmd)) &&
2323 lun == (u8)cmd->device->lun
2324#ifdef SUPPORT_TAGS
2325 && (tag == cmd->tag)
2326#endif
2327 ) {
2328 list_del(&ncmd->list);
2329 tmp = cmd;
2330 break;
2331 }
2332 }
2333
2334 if (tmp) {
2335 dsprintk(NDEBUG_RESELECTION | NDEBUG_QUEUES, instance,
2336 "reselect: removed %p from disconnected queue\n", tmp);
2337 } else {
2338
2339#ifdef SUPPORT_TAGS
2340 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d tag %d not in disconnected queue.\n",
2341 target_mask, lun, tag);
2342#else
2343 shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n",
2344 target_mask, lun);
2345#endif
2346 /*
2347 * Since we have an established nexus that we can't do anything
2348 * with, we must abort it.
2349 */
2350 do_abort(instance);
2351 return;
2352 }
2353
2354#if defined(CONFIG_SUN3)
2355 /* engage dma setup for the command we just saw */
2356 {
2357 void *d;
2358 unsigned long count;
2359
2360 if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) {
2361 count = tmp->SCp.buffer->length;
2362 d = sg_virt(tmp->SCp.buffer);
2363 } else {
2364 count = tmp->SCp.this_residual;
2365 d = tmp->SCp.ptr;
2366 }
2367 /* setup this command for dma if not already */
2368 if (sun3_dma_setup_done != tmp &&
2369 sun3scsi_dma_xfer_len(count, tmp) > 0) {
2370 sun3scsi_dma_setup(instance, d, count,
2371 rq_data_dir(tmp->request));
2372 sun3_dma_setup_done = tmp;
2373 }
2374 }
2375
2376 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
2377#endif
2378
2379 /* Accept message by clearing ACK */
2380 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2381
2382#if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3)
2383 /* If the phase is still MSGIN, the target wants to send some more
2384 * messages. In case it supports tagged queuing, this is probably a
2385 * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
2386 */
2387 tag = TAG_NONE;
2388 if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
2389 /* Accept previous IDENTIFY message by clearing ACK */
2390 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
2391 len = 2;
2392 data = msg + 1;
2393 if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
2394 msg[1] == SIMPLE_QUEUE_TAG)
2395 tag = msg[2];
2396 dsprintk(NDEBUG_TAGS, instance, "reselect: target mask %02x, lun %d sent tag %d\n"
2397 target_mask, lun, tag);
2398 }
2399#endif
2400
2401 hostdata->connected = tmp;
2402 dsprintk(NDEBUG_RESELECTION, instance, "nexus established, target %d, lun %llu, tag %d\n",
2403 scmd_id(tmp), tmp->device->lun, tmp->tag);
2404}
2405
2406
2407/**
2408 * list_find_cmd - test for presence of a command in a linked list
2409 * @haystack: list of commands
2410 * @needle: command to search for
2411 */
2412
2413static bool list_find_cmd(struct list_head *haystack,
2414 struct scsi_cmnd *needle)
2415{
2416 struct NCR5380_cmd *ncmd;
2417
2418 list_for_each_entry(ncmd, haystack, list)
2419 if (NCR5380_to_scmd(ncmd) == needle)
2420 return true;
2421 return false;
2422}
2423
2424/**
2425 * list_remove_cmd - remove a command from linked list
2426 * @haystack: list of commands
2427 * @needle: command to remove
2428 */
2429
2430static bool list_del_cmd(struct list_head *haystack,
2431 struct scsi_cmnd *needle)
2432{
2433 if (list_find_cmd(haystack, needle)) {
2434 struct NCR5380_cmd *ncmd = scsi_cmd_priv(needle);
2435
2436 list_del(&ncmd->list);
2437 return true;
2438 }
2439 return false;
2440}
2441
2442/**
2443 * NCR5380_abort - scsi host eh_abort_handler() method
2444 * @cmd: the command to be aborted
2445 *
2446 * Try to abort a given command by removing it from queues and/or sending
2447 * the target an abort message. This may not succeed in causing a target
2448 * to abort the command. Nonetheless, the low-level driver must forget about
2449 * the command because the mid-layer reclaims it and it may be re-issued.
2450 *
2451 * The normal path taken by a command is as follows. For EH we trace this
2452 * same path to locate and abort the command.
2453 *
2454 * unissued -> selecting -> [unissued -> selecting ->]... connected ->
2455 * [disconnected -> connected ->]...
2456 * [autosense -> connected ->] done
2457 *
2458 * If cmd was not found at all then presumably it has already been completed,
2459 * in which case return SUCCESS to try to avoid further EH measures.
2460 *
2461 * If the command has not completed yet, we must not fail to find it.
2462 * We have no option but to forget the aborted command (even if it still
2463 * lacks sense data). The mid-layer may re-issue a command that is in error
2464 * recovery (see scsi_send_eh_cmnd), but the logic and data structures in
2465 * this driver are such that a command can appear on one queue only.
2466 *
2467 * The lock protects driver data structures, but EH handlers also use it
2468 * to serialize their own execution and prevent their own re-entry.
2469 */
2470
2471static int NCR5380_abort(struct scsi_cmnd *cmd)
2472{
2473 struct Scsi_Host *instance = cmd->device->host;
2474 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2475 unsigned long flags;
2476 int result = SUCCESS;
2477
2478 spin_lock_irqsave(&hostdata->lock, flags);
2479
2480#if (NDEBUG & NDEBUG_ANY)
2481 scmd_printk(KERN_INFO, cmd, __func__);
2482#endif
2483 NCR5380_dprint(NDEBUG_ANY, instance);
2484 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2485
2486 if (list_del_cmd(&hostdata->unissued, cmd)) {
2487 dsprintk(NDEBUG_ABORT, instance,
2488 "abort: removed %p from issue queue\n", cmd);
2489 cmd->result = DID_ABORT << 16;
2490 cmd->scsi_done(cmd); /* No tag or busy flag to worry about */
2491 goto out;
2492 }
2493
2494 if (hostdata->selecting == cmd) {
2495 dsprintk(NDEBUG_ABORT, instance,
2496 "abort: cmd %p == selecting\n", cmd);
2497 hostdata->selecting = NULL;
2498 cmd->result = DID_ABORT << 16;
2499 complete_cmd(instance, cmd);
2500 goto out;
2501 }
2502
2503 if (list_del_cmd(&hostdata->disconnected, cmd)) {
2504 dsprintk(NDEBUG_ABORT, instance,
2505 "abort: removed %p from disconnected list\n", cmd);
2506 /* Can't call NCR5380_select() and send ABORT because that
2507 * means releasing the lock. Need a bus reset.
2508 */
2509 set_host_byte(cmd, DID_ERROR);
2510 complete_cmd(instance, cmd);
2511 result = FAILED;
2512 goto out;
2513 }
2514
2515 if (hostdata->connected == cmd) {
2516 dsprintk(NDEBUG_ABORT, instance, "abort: cmd %p is connected\n", cmd);
2517 hostdata->connected = NULL;
2518 hostdata->dma_len = 0;
2519 if (do_abort(instance)) {
2520 set_host_byte(cmd, DID_ERROR);
2521 complete_cmd(instance, cmd);
2522 result = FAILED;
2523 goto out;
2524 }
2525 set_host_byte(cmd, DID_ABORT);
2526 complete_cmd(instance, cmd);
2527 goto out;
2528 }
2529
2530 if (list_del_cmd(&hostdata->autosense, cmd)) {
2531 dsprintk(NDEBUG_ABORT, instance,
2532 "abort: removed %p from sense queue\n", cmd);
2533 set_host_byte(cmd, DID_ERROR);
2534 complete_cmd(instance, cmd);
2535 }
2536
2537out:
2538 if (result == FAILED)
2539 dsprintk(NDEBUG_ABORT, instance, "abort: failed to abort %p\n", cmd);
2540 else
2541 dsprintk(NDEBUG_ABORT, instance, "abort: successfully aborted %p\n", cmd);
2542
2543 queue_work(hostdata->work_q, &hostdata->main_task);
2544 maybe_release_dma_irq(instance);
2545 spin_unlock_irqrestore(&hostdata->lock, flags);
2546
2547 return result;
2548}
2549
2550
2551/**
2552 * NCR5380_bus_reset - reset the SCSI bus
2553 * @cmd: SCSI command undergoing EH
2554 *
2555 * Returns SUCCESS
2556 */
2557
2558static int NCR5380_bus_reset(struct scsi_cmnd *cmd)
2559{
2560 struct Scsi_Host *instance = cmd->device->host;
2561 struct NCR5380_hostdata *hostdata = shost_priv(instance);
2562 int i;
2563 unsigned long flags;
2564 struct NCR5380_cmd *ncmd;
2565
2566 spin_lock_irqsave(&hostdata->lock, flags);
2567
2568#if (NDEBUG & NDEBUG_ANY)
2569 scmd_printk(KERN_INFO, cmd, __func__);
2570#endif
2571 NCR5380_dprint(NDEBUG_ANY, instance);
2572 NCR5380_dprint_phase(NDEBUG_ANY, instance);
2573
2574 do_reset(instance);
2575
2576 /* reset NCR registers */
2577 NCR5380_write(MODE_REG, MR_BASE);
2578 NCR5380_write(TARGET_COMMAND_REG, 0);
2579 NCR5380_write(SELECT_ENABLE_REG, 0);
2580
2581 /* After the reset, there are no more connected or disconnected commands
2582 * and no busy units; so clear the low-level status here to avoid
2583 * conflicts when the mid-level code tries to wake up the affected
2584 * commands!
2585 */
2586
2587 if (list_del_cmd(&hostdata->unissued, cmd)) {
2588 cmd->result = DID_RESET << 16;
2589 cmd->scsi_done(cmd);
2590 }
2591
2592 if (hostdata->selecting) {
2593 hostdata->selecting->result = DID_RESET << 16;
2594 complete_cmd(instance, hostdata->selecting);
2595 hostdata->selecting = NULL;
2596 }
2597
2598 list_for_each_entry(ncmd, &hostdata->disconnected, list) {
2599 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2600
2601 set_host_byte(cmd, DID_RESET);
2602 cmd->scsi_done(cmd);
2603 }
2604 INIT_LIST_HEAD(&hostdata->disconnected);
2605
2606 list_for_each_entry(ncmd, &hostdata->autosense, list) {
2607 struct scsi_cmnd *cmd = NCR5380_to_scmd(ncmd);
2608
2609 set_host_byte(cmd, DID_RESET);
2610 cmd->scsi_done(cmd);
2611 }
2612 INIT_LIST_HEAD(&hostdata->autosense);
2613
2614 if (hostdata->connected) {
2615 set_host_byte(hostdata->connected, DID_RESET);
2616 complete_cmd(instance, hostdata->connected);
2617 hostdata->connected = NULL;
2618 }
2619
2620#ifdef SUPPORT_TAGS
2621 free_all_tags(hostdata);
2622#endif
2623 for (i = 0; i < 8; ++i)
2624 hostdata->busy[i] = 0;
2625 hostdata->dma_len = 0;
2626
2627 queue_work(hostdata->work_q, &hostdata->main_task);
2628 maybe_release_dma_irq(instance);
2629 spin_unlock_irqrestore(&hostdata->lock, flags);
2630
2631 return SUCCESS;
2632}
diff --git a/drivers/scsi/atari_scsi.c b/drivers/scsi/atari_scsi.c
index 445c26724ba1..49b7b14e8913 100644
--- a/drivers/scsi/atari_scsi.c
+++ b/drivers/scsi/atari_scsi.c
@@ -87,9 +87,6 @@
87 87
88/* Definitions for the core NCR5380 driver. */ 88/* Definitions for the core NCR5380 driver. */
89 89
90#define SUPPORT_TAGS
91#define MAX_TAGS 32
92
93#define NCR5380_implementation_fields /* none */ 90#define NCR5380_implementation_fields /* none */
94 91
95#define NCR5380_read(reg) atari_scsi_reg_read(reg) 92#define NCR5380_read(reg) atari_scsi_reg_read(reg)
@@ -189,8 +186,6 @@ static int setup_cmd_per_lun = -1;
189module_param(setup_cmd_per_lun, int, 0); 186module_param(setup_cmd_per_lun, int, 0);
190static int setup_sg_tablesize = -1; 187static int setup_sg_tablesize = -1;
191module_param(setup_sg_tablesize, int, 0); 188module_param(setup_sg_tablesize, int, 0);
192static int setup_use_tagged_queuing = -1;
193module_param(setup_use_tagged_queuing, int, 0);
194static int setup_hostid = -1; 189static int setup_hostid = -1;
195module_param(setup_hostid, int, 0); 190module_param(setup_hostid, int, 0);
196static int setup_toshiba_delay = -1; 191static int setup_toshiba_delay = -1;
@@ -479,8 +474,7 @@ static int __init atari_scsi_setup(char *str)
479 setup_sg_tablesize = ints[3]; 474 setup_sg_tablesize = ints[3];
480 if (ints[0] >= 4) 475 if (ints[0] >= 4)
481 setup_hostid = ints[4]; 476 setup_hostid = ints[4];
482 if (ints[0] >= 5) 477 /* ints[5] (use_tagged_queuing) is ignored */
483 setup_use_tagged_queuing = ints[5];
484 /* ints[6] (use_pdma) is ignored */ 478 /* ints[6] (use_pdma) is ignored */
485 if (ints[0] >= 7) 479 if (ints[0] >= 7)
486 setup_toshiba_delay = ints[7]; 480 setup_toshiba_delay = ints[7];
@@ -853,9 +847,6 @@ static int __init atari_scsi_probe(struct platform_device *pdev)
853 instance->irq = irq->start; 847 instance->irq = irq->start;
854 848
855 host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP; 849 host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
856#ifdef SUPPORT_TAGS
857 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
858#endif
859 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; 850 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
860 851
861 error = NCR5380_init(instance, host_flags); 852 error = NCR5380_init(instance, host_flags);
diff --git a/drivers/scsi/mac_scsi.c b/drivers/scsi/mac_scsi.c
index 4de6589fdbfb..42feba7e350b 100644
--- a/drivers/scsi/mac_scsi.c
+++ b/drivers/scsi/mac_scsi.c
@@ -55,8 +55,6 @@ static int setup_sg_tablesize = -1;
55module_param(setup_sg_tablesize, int, 0); 55module_param(setup_sg_tablesize, int, 0);
56static int setup_use_pdma = -1; 56static int setup_use_pdma = -1;
57module_param(setup_use_pdma, int, 0); 57module_param(setup_use_pdma, int, 0);
58static int setup_use_tagged_queuing = -1;
59module_param(setup_use_tagged_queuing, int, 0);
60static int setup_hostid = -1; 58static int setup_hostid = -1;
61module_param(setup_hostid, int, 0); 59module_param(setup_hostid, int, 0);
62static int setup_toshiba_delay = -1; 60static int setup_toshiba_delay = -1;
@@ -95,8 +93,7 @@ static int __init mac_scsi_setup(char *str)
95 setup_sg_tablesize = ints[3]; 93 setup_sg_tablesize = ints[3];
96 if (ints[0] >= 4) 94 if (ints[0] >= 4)
97 setup_hostid = ints[4]; 95 setup_hostid = ints[4];
98 if (ints[0] >= 5) 96 /* ints[5] (use_tagged_queuing) is ignored */
99 setup_use_tagged_queuing = ints[5];
100 if (ints[0] >= 6) 97 if (ints[0] >= 6)
101 setup_use_pdma = ints[6]; 98 setup_use_pdma = ints[6];
102 if (ints[0] >= 7) 99 if (ints[0] >= 7)
@@ -382,9 +379,6 @@ static int __init mac_scsi_probe(struct platform_device *pdev)
382 } else 379 } else
383 host_flags |= FLAG_NO_PSEUDO_DMA; 380 host_flags |= FLAG_NO_PSEUDO_DMA;
384 381
385#ifdef SUPPORT_TAGS
386 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
387#endif
388 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; 382 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
389 383
390 error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP); 384 error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
diff --git a/drivers/scsi/sun3_scsi.c b/drivers/scsi/sun3_scsi.c
index f2fc788cb08b..3c4c07038948 100644
--- a/drivers/scsi/sun3_scsi.c
+++ b/drivers/scsi/sun3_scsi.c
@@ -41,9 +41,6 @@
41 41
42/* Definitions for the core NCR5380 driver. */ 42/* Definitions for the core NCR5380 driver. */
43 43
44/* #define SUPPORT_TAGS */
45/* #define MAX_TAGS 32 */
46
47#define NCR5380_implementation_fields /* none */ 44#define NCR5380_implementation_fields /* none */
48 45
49#define NCR5380_read(reg) sun3scsi_read(reg) 46#define NCR5380_read(reg) sun3scsi_read(reg)
@@ -75,10 +72,6 @@ static int setup_cmd_per_lun = -1;
75module_param(setup_cmd_per_lun, int, 0); 72module_param(setup_cmd_per_lun, int, 0);
76static int setup_sg_tablesize = -1; 73static int setup_sg_tablesize = -1;
77module_param(setup_sg_tablesize, int, 0); 74module_param(setup_sg_tablesize, int, 0);
78#ifdef SUPPORT_TAGS
79static int setup_use_tagged_queuing = -1;
80module_param(setup_use_tagged_queuing, int, 0);
81#endif
82static int setup_hostid = -1; 75static int setup_hostid = -1;
83module_param(setup_hostid, int, 0); 76module_param(setup_hostid, int, 0);
84 77
@@ -512,10 +505,6 @@ static int __init sun3_scsi_probe(struct platform_device *pdev)
512 instance->io_port = (unsigned long)ioaddr; 505 instance->io_port = (unsigned long)ioaddr;
513 instance->irq = irq->start; 506 instance->irq = irq->start;
514 507
515#ifdef SUPPORT_TAGS
516 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
517#endif
518
519 error = NCR5380_init(instance, host_flags); 508 error = NCR5380_init(instance, host_flags);
520 if (error) 509 if (error)
521 goto fail_init; 510 goto fail_init;