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