~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/scsi/atari_NCR5380.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.