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