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