File: /usr/src/linux/drivers/scsi/seagate.c

1     /*
2      *    seagate.c Copyright (C) 1992, 1993 Drew Eckhardt
3      *      low level scsi driver for ST01/ST02, Future Domain TMC-885,
4      *      TMC-950 by Drew Eckhardt <drew@colorado.edu>
5      *
6      *      Note : TMC-880 boards don't work because they have two bits in
7      *              the status register flipped, I'll fix this "RSN"
8      *	[why do I have strong feeling that above message is from 1993? :-)
9      *	        pavel@ucw.cz]
10      *
11      *      This card does all the I/O via memory mapped I/O, so there is no need
12      *      to check or allocate a region of the I/O address space.
13      */
14     
15     /* 1996 - to use new read{b,w,l}, write{b,w,l}, and phys_to_virt
16      * macros, replaced assembler routines with C. There's probably a
17      * performance hit, but I only have a cdrom and can't tell. Define
18      * SEAGATE_USE_ASM if you want the old assembler code -- SJT
19      *
20      * 1998-jul-29 - created DPRINTK macros and made it work under 
21      * linux 2.1.112, simplified some #defines etc. <pavel@ucw.cz>
22      *
23      * Aug 2000 - aeb - deleted seagate_st0x_biosparam(). It would try to
24      * read the physical disk geometry, a bad mistake. Of course it doesnt
25      * matter much what geometry one invents, but on large disks it
26      * returned 256 (or more) heads, causing all kind of failures.
27      * Of course this means that people might see a different geometry now,
28      * so boot parameters may be necessary in some cases.
29      */
30     
31     /*
32      * Configuration :
33      * To use without BIOS -DOVERRIDE=base_address -DCONTROLLER=FD or SEAGATE
34      * -DIRQ will override the default of 5.
35      * Note: You can now set these options from the kernel's "command line".
36      * The syntax is:
37      *
38      *     st0x=ADDRESS,IRQ                (for a Seagate controller)
39      * or:
40      *     tmc8xx=ADDRESS,IRQ              (for a TMC-8xx or TMC-950 controller)
41      * eg:
42      *     tmc8xx=0xC8000,15
43      *
44      * will configure the driver for a TMC-8xx style controller using IRQ 15
45      * with a base address of 0xC8000.
46      *
47      * -DARBITRATE 
48      *      Will cause the host adapter to arbitrate for the
49      *      bus for better SCSI-II compatibility, rather than just
50      *      waiting for BUS FREE and then doing its thing.  Should
51      *      let us do one command per Lun when I integrate my
52      *      reorganization changes into the distribution sources.
53      *
54      * -DDEBUG=65535
55      *      Will activate debug code.
56      *
57      * -DFAST or -DFAST32 
58      *      Will use blind transfers where possible
59      *
60      * -DPARITY  
61      *      This will enable parity.
62      *
63      * -DSEAGATE_USE_ASM
64      *      Will use older seagate assembly code. should be (very small amount)
65      *      Faster.
66      *
67      * -DSLOW_RATE=50
68      *      Will allow compatibility with broken devices that don't
69      *      handshake fast enough (ie, some CD ROM's) for the Seagate
70      *      code.
71      *
72      *      50 is some number, It will let you specify a default
73      *      transfer rate if handshaking isn't working correctly.
74      *
75      * -DOLDCNTDATASCEME  There is a new sceme to set the CONTROL
76      *                    and DATA reigsters which complies more closely
77      *                    with the SCSI2 standard. This hopefully eliminates
78      *                    the need to swap the order these registers are
79      *                    'messed' with. It makes the following two options
80      *                    obsolete. To reenable the old sceme define this.
81      *
82      * The following to options are patches from the SCSI.HOWTO
83      *
84      * -DSWAPSTAT  This will swap the definitions for STAT_MSG and STAT_CD.
85      *
86      * -DSWAPCNTDATA  This will swap the order that seagate.c messes with
87      *                the CONTROL an DATA registers.
88      */
89     
90     #include <linux/module.h>
91     
92     #include <asm/io.h>
93     #include <asm/system.h>
94     #include <linux/spinlock.h>
95     #include <linux/signal.h>
96     #include <linux/sched.h>
97     #include <linux/string.h>
98     #include <linux/proc_fs.h>
99     #include <linux/init.h>
100     #include <linux/delay.h>
101     #include <linux/blk.h>
102     #include "scsi.h"
103     #include "hosts.h"
104     #include "seagate.h"
105     #include "constants.h"
106     #include <linux/stat.h>
107     #include <asm/uaccess.h>
108     #include "sd.h"
109     #include <scsi/scsi_ioctl.h>
110     
111     #ifdef DEBUG
112     #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
113     #else
114     #define DPRINTK( when, msg... ) do { } while (0)
115     #endif
116     #define DANY( msg... ) DPRINTK( 0xffff, msg );
117     
118     #ifndef IRQ
119     #define IRQ 5
120     #endif
121     
122     #ifdef FAST32
123     #define FAST
124     #endif
125     
126     #undef LINKED			/* Linked commands are currently broken! */
127     
128     #if defined(OVERRIDE) && !defined(CONTROLLER)
129     #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
130     #endif
131     
132     #ifndef __i386__
133     #undef SEAGATE_USE_ASM
134     #endif
135     
136     /*
137     	Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
138     		driver, and Mitsugu Suzuki for information on the ST-01
139     		SCSI host.
140     */
141     
142     /*
143     	CONTROL defines
144     */
145     
146     #define CMD_RST 		0x01
147     #define CMD_SEL 		0x02
148     #define CMD_BSY 		0x04
149     #define CMD_ATTN    		0x08
150     #define CMD_START_ARB		0x10
151     #define CMD_EN_PARITY		0x20
152     #define CMD_INTR		0x40
153     #define CMD_DRVR_ENABLE		0x80
154     
155     /*
156     	STATUS
157     */
158     #ifdef SWAPSTAT
159     #define STAT_MSG		0x08
160     #define STAT_CD			0x02
161     #else
162     #define STAT_MSG		0x02
163     #define STAT_CD			0x08
164     #endif
165     
166     #define STAT_BSY		0x01
167     #define STAT_IO			0x04
168     #define STAT_REQ		0x10
169     #define STAT_SEL		0x20
170     #define STAT_PARITY		0x40
171     #define STAT_ARB_CMPL		0x80
172     
173     /* 
174     	REQUESTS
175     */
176     
177     #define REQ_MASK (STAT_CD |  STAT_IO | STAT_MSG)
178     #define REQ_DATAOUT 0
179     #define REQ_DATAIN STAT_IO
180     #define REQ_CMDOUT STAT_CD
181     #define REQ_STATIN (STAT_CD | STAT_IO)
182     #define REQ_MSGOUT (STAT_MSG | STAT_CD)
183     #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
184     
185     extern volatile int seagate_st0x_timeout;
186     
187     #ifdef PARITY
188     #define BASE_CMD CMD_EN_PARITY
189     #else
190     #define BASE_CMD  0
191     #endif
192     
193     /*
194     	Debugging code
195     */
196     
197     #define PHASE_BUS_FREE 1
198     #define PHASE_ARBITRATION 2
199     #define PHASE_SELECTION 4
200     #define PHASE_DATAIN 8
201     #define PHASE_DATAOUT 0x10
202     #define PHASE_CMDOUT 0x20
203     #define PHASE_MSGIN 0x40
204     #define PHASE_MSGOUT 0x80
205     #define PHASE_STATUSIN 0x100
206     #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
207     #define PRINT_COMMAND 0x200
208     #define PHASE_EXIT 0x400
209     #define PHASE_RESELECT 0x800
210     #define DEBUG_FAST 0x1000
211     #define DEBUG_SG   0x2000
212     #define DEBUG_LINKED	0x4000
213     #define DEBUG_BORKEN	0x8000
214     
215     /* 
216      *	Control options - these are timeouts specified in .01 seconds.
217      */
218     
219     /* 30, 20 work */
220     #define ST0X_BUS_FREE_DELAY 25
221     #define ST0X_SELECTION_DELAY 25
222     
223     #define SEAGATE 1		/* these determine the type of the controller */
224     #define FD	2
225     
226     #define ST0X_ID_STR	"Seagate ST-01/ST-02"
227     #define FD_ID_STR	"TMC-8XX/TMC-950"
228     
229     static int internal_command (unsigned char target, unsigned char lun,
230     			     const void *cmnd,
231     			     void *buff, int bufflen, int reselect);
232     
233     static int incommand;		/* set if arbitration has finished
234     				   and we are in some command phase. */
235     
236     static unsigned int base_address = 0;	/* Where the card ROM starts, used to 
237     					   calculate memory mapped register
238     					   location.  */
239     
240     static unsigned long st0x_cr_sr;	/* control register write, status
241     					   register read.  256 bytes in
242     					   length.
243     					   Read is status of SCSI BUS, as per 
244     					   STAT masks.  */
245     
246     static unsigned long st0x_dr;	/* data register, read write 256
247     				   bytes in length.  */
248     
249     static volatile int st0x_aborted = 0;	/* set when we are aborted, ie by a
250     					   time out, etc.  */
251     
252     static unsigned char controller_type = 0;	/* set to SEAGATE for ST0x
253     						   boards or FD for TMC-8xx
254     						   boards */
255     static int irq = IRQ;
256     
257     MODULE_PARM (base_address, "i");
258     MODULE_PARM (controller_type, "b");
259     MODULE_PARM (irq, "i");
260     
261     #define retcode(result) (((result) << 16) | (message << 8) | status)
262     #define STATUS ((u8) isa_readb(st0x_cr_sr))
263     #define DATA ((u8) isa_readb(st0x_dr))
264     #define WRITE_CONTROL(d) { isa_writeb((d), st0x_cr_sr); }
265     #define WRITE_DATA(d) { isa_writeb((d), st0x_dr); }
266     
267     void
268     st0x_setup (char *str, int *ints)
269     {
270     	controller_type = SEAGATE;
271     	base_address = ints[1];
272     	irq = ints[2];
273     }
274     
275     void
276     tmc8xx_setup (char *str, int *ints)
277     {
278     	controller_type = FD;
279     	base_address = ints[1];
280     	irq = ints[2];
281     }
282     
283     #ifndef OVERRIDE
284     static unsigned int seagate_bases[] = {
285     	0xc8000, 0xca000, 0xcc000,
286     	0xce000, 0xdc000, 0xde000
287     };
288     
289     typedef struct {
290     	const unsigned char *signature;
291     	unsigned offset;
292     	unsigned length;
293     	unsigned char type;
294     } Signature;
295     
296     static Signature __initdata signatures[] = {
297     	{"ST01 v1.7  (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
298     	{"SCSI BIOS 2.00  (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
299     
300     /*
301      * The following two lines are NOT mistakes.  One detects ROM revision
302      * 3.0.0, the other 3.2.  Since seagate has only one type of SCSI adapter,
303      * and this is not going to change, the "SEAGATE" and "SCSI" together
304      * are probably "good enough"
305      */
306     
307     	{"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
308     	{"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
309     
310     /*
311      * However, future domain makes several incompatible SCSI boards, so specific
312      * signatures must be used.
313      */
314     
315     	{"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
316     	{"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
317     	{"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
318     	{"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
319     	{"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
320     	{"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
321     	{"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
322     	{"FUTURE DOMAIN TMC-950", 5, 21, FD},
323     	/* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
324     	{"IBM F1 V1.2009/22/93", 5, 25, FD},
325     };
326     
327     #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
328     #endif				/* n OVERRIDE */
329     
330     /*
331      * hostno stores the hostnumber, as told to us by the init routine.
332      */
333     
334     static int hostno = -1;
335     static void seagate_reconnect_intr (int, void *, struct pt_regs *);
336     static void do_seagate_reconnect_intr (int, void *, struct pt_regs *);
337     
338     #ifdef FAST
339     static int fast = 1;
340     #else
341     #define fast 0
342     #endif
343     
344     #ifdef SLOW_RATE
345     /*
346      * Support for broken devices :
347      * The Seagate board has a handshaking problem.  Namely, a lack
348      * thereof for slow devices.  You can blast 600K/second through
349      * it if you are polling for each byte, more if you do a blind
350      * transfer.  In the first case, with a fast device, REQ will
351      * transition high-low or high-low-high before your loop restarts
352      * and you'll have no problems.  In the second case, the board
353      * will insert wait states for up to 13.2 usecs for REQ to
354      * transition low->high, and everything will work.
355      *
356      * However, there's nothing in the state machine that says
357      * you *HAVE* to see a high-low-high set of transitions before
358      * sending the next byte, and slow things like the Trantor CD ROMS
359      * will break because of this.
360      *
361      * So, we need to slow things down, which isn't as simple as it
362      * seems.  We can't slow things down period, because then people
363      * who don't recompile their kernels will shoot me for ruining
364      * their performance.  We need to do it on a case per case basis.
365      *
366      * The best for performance will be to, only for borken devices
367      * (this is stored on a per-target basis in the scsi_devices array)
368      *
369      * Wait for a low->high transition before continuing with that
370      * transfer.  If we timeout, continue anyways.  We don't need
371      * a long timeout, because REQ should only be asserted until the
372      * corresponding ACK is received and processed.
373      *
374      * Note that we can't use the system timer for this, because of
375      * resolution, and we *really* can't use the timer chip since
376      * gettimeofday() and the beeper routines use that.  So,
377      * the best thing for us to do will be to calibrate a timing
378      * loop in the initialization code using the timer chip before
379      * gettimeofday() can screw with it.
380      *
381      * FIXME: this is broken (not borken :-). Empty loop costs less than
382      * loop with ISA access in it! -- pavel@ucw.cz
383      */
384     
385     static int borken_calibration = 0;
386     
387     static void __init borken_init (void)
388     {
389     	register int count = 0, start = jiffies + 1, stop = start + 25;
390     
391     	while (time_before (jiffies, start)) ;
392     	for (; time_before (jiffies, stop); ++count) ;
393     
394     /*
395      * Ok, we now have a count for .25 seconds.  Convert to a
396      * count per second and divide by transfer rate in K.  */
397     
398     	borken_calibration = (count * 4) / (SLOW_RATE * 1024);
399     
400     	if (borken_calibration < 1)
401     		borken_calibration = 1;
402     }
403     
404     static inline void borken_wait (void)
405     {
406     	register int count;
407     
408     	for (count = borken_calibration; count && (STATUS & STAT_REQ);
409     	     --count) ;
410     #if (DEBUG & DEBUG_BORKEN)
411     	if (count)
412     		printk ("scsi%d : borken timeout\n", hostno);
413     #endif
414     }
415     
416     #endif				/* def SLOW_RATE */
417     
418     /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
419      * contains at least one ISA access, which takes more than 0.125
420      * usec. So if we loop 8 times time in usec, we are safe.
421      */
422     
423     #define ULOOP( i ) for (clock = i*8;;)
424     #define TIMEOUT (!(clock--))
425     
426     int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
427     {
428     	struct Scsi_Host *instance;
429     	int i, j;
430     
431     	tpnt->proc_name = "seagate";
432     /*
433      *    First, we try for the manual override.
434      */
435     	DANY ("Autodetecting ST0x / TMC-8xx\n");
436     
437     	if (hostno != -1) {
438     		printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
439     		return 0;
440     	}
441     
442     /* If the user specified the controller type from the command line,
443        controller_type will be non-zero, so don't try to detect one */
444     
445     	if (!controller_type) {
446     #ifdef OVERRIDE
447     		base_address = OVERRIDE;
448     		controller_type = CONTROLLER;
449     
450     		DANY ("Base address overridden to %x, controller type is %s\n",
451     		      base_address,
452     		      controller_type == SEAGATE ? "SEAGATE" : "FD");
453     #else				/* OVERRIDE */
454     /*
455      * 	To detect this card, we simply look for the signature
456      *      from the BIOS version notice in all the possible locations
457      *      of the ROM's.  This has a nice side effect of not trashing
458      *      any register locations that might be used by something else.
459      *
460      * XXX - note that we probably should be probing the address
461      * space for the on-board RAM instead.
462      */
463     
464     		for (i = 0;
465     		     i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i)
466     
467     			for (j = 0; !base_address && j < NUM_SIGNATURES; ++j)
468     				if (isa_check_signature
469     				    (seagate_bases[i] + signatures[j].offset,
470     				     signatures[j].signature,
471     				     signatures[j].length)) {
472     					base_address = seagate_bases[i];
473     					controller_type = signatures[j].type;
474     				}
475     #endif				/* OVERRIDE */
476     	}
477     	/* (! controller_type) */
478     	tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
479     	tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
480     
481     	if (!base_address) {
482     		DANY ("ST0x / TMC-8xx not detected.\n");
483     		return 0;
484     	}
485     
486     	st0x_cr_sr =
487     	    base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
488     	st0x_dr = st0x_cr_sr + 0x200;
489     
490     	DANY ("%s detected. Base address = %x, cr = %x, dr = %x\n",
491     	      tpnt->name, base_address, st0x_cr_sr, st0x_dr);
492     
493     /*
494      *	At all times, we will use IRQ 5.  Should also check for IRQ3 if we
495      *      loose our first interrupt.
496      */
497     	instance = scsi_register (tpnt, 0);
498     	if (instance == NULL)
499     		return 0;
500     
501     	hostno = instance->host_no;
502     	if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT,
503     			 (controller_type == SEAGATE) ? "seagate" : "tmc-8xx",
504     			 NULL)) {
505     		printk ("scsi%d : unable to allocate IRQ%d\n", hostno, irq);
506     		return 0;
507     	}
508     	instance->irq = irq;
509     	instance->io_port = base_address;
510     #ifdef SLOW_RATE
511     	printk (KERN_INFO "Calibrating borken timer... ");
512     	borken_init ();
513     	printk (" %d cycles per transfer\n", borken_calibration);
514     #endif
515     
516     	printk (KERN_INFO "This is one second... ");
517     	{
518     		int clock;
519     		ULOOP (1 * 1000 * 1000) {
520     			STATUS;
521     			if (TIMEOUT)
522     				break;
523     		}
524     	}
525     
526     	printk ("done, %s options:"
527     #ifdef ARBITRATE
528     		" ARBITRATE"
529     #endif
530     #ifdef DEBUG
531     		" DEBUG"
532     #endif
533     #ifdef FAST
534     		" FAST"
535     #ifdef FAST32
536     		"32"
537     #endif
538     #endif
539     #ifdef LINKED
540     		" LINKED"
541     #endif
542     #ifdef PARITY
543     		" PARITY"
544     #endif
545     #ifdef SEAGATE_USE_ASM
546     		" SEAGATE_USE_ASM"
547     #endif
548     #ifdef SLOW_RATE
549     		" SLOW_RATE"
550     #endif
551     #ifdef SWAPSTAT
552     		" SWAPSTAT"
553     #endif
554     #ifdef SWAPCNTDATA
555     		" SWAPCNTDATA"
556     #endif
557     		"\n", tpnt->name);
558     	return 1;
559     }
560     
561     const char *
562     seagate_st0x_info (struct Scsi_Host *shpnt)
563     {
564     	static char buffer[64];
565     
566     	sprintf (buffer, "%s at irq %d, address 0x%05X",
567     		 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
568     		 irq, base_address);
569     	return buffer;
570     }
571     
572     /*
573      * These are our saved pointers for the outstanding command that is
574      * waiting for a reconnect
575      */
576     
577     static unsigned char current_target, current_lun;
578     static unsigned char *current_cmnd, *current_data;
579     static int current_nobuffs;
580     static struct scatterlist *current_buffer;
581     static int current_bufflen;
582     
583     #ifdef LINKED
584     /*
585      * linked_connected indicates whether or not we are currently connected to
586      * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
587      * using linked commands.
588      */
589     
590     static int linked_connected = 0;
591     static unsigned char linked_target, linked_lun;
592     #endif
593     
594     static void (*done_fn) (Scsi_Cmnd *) = NULL;
595     static Scsi_Cmnd *SCint = NULL;
596     
597     /*
598      * These control whether or not disconnect / reconnect will be attempted,
599      * or are being attempted.
600      */
601     
602     #define NO_RECONNECT    0
603     #define RECONNECT_NOW   1
604     #define CAN_RECONNECT   2
605     
606     /*
607      * LINKED_RIGHT indicates that we are currently connected to the correct target
608      * for this command, LINKED_WRONG indicates that we are connected to the wrong
609      * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
610      */
611     
612     #define LINKED_RIGHT    3
613     #define LINKED_WRONG    4
614     
615     /*
616      * This determines if we are expecting to reconnect or not.
617      */
618     
619     static int should_reconnect = 0;
620     
621     /*
622      * The seagate_reconnect_intr routine is called when a target reselects the
623      * host adapter.  This occurs on the interrupt triggered by the target
624      * asserting SEL.
625      */
626     
627     static void do_seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
628     {
629     	unsigned long flags;
630     
631     	spin_lock_irqsave (&io_request_lock, flags);
632     	seagate_reconnect_intr (irq, dev_id, regs);
633     	spin_unlock_irqrestore (&io_request_lock, flags);
634     }
635     
636     static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
637     {
638     	int temp;
639     	Scsi_Cmnd *SCtmp;
640     
641     	DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n",
642     		 hostno);
643     
644     	if (!should_reconnect)
645     		printk ("scsi%d: unexpected interrupt.\n", hostno);
646     	else {
647     		should_reconnect = 0;
648     
649     		DPRINTK (PHASE_RESELECT, "scsi%d : internal_command("
650     			 "%d, %08x, %08x, RECONNECT_NOW\n", hostno,
651     			 current_target, current_data, current_bufflen);
652     
653     		temp =
654     		    internal_command (current_target, current_lun, current_cmnd,
655     				      current_data, current_bufflen,
656     				      RECONNECT_NOW);
657     
658     		if (msg_byte (temp) != DISCONNECT) {
659     			if (done_fn) {
660     				DPRINTK (PHASE_RESELECT,
661     					 "scsi%d : done_fn(%d,%08x)", hostno,
662     					 hostno, temp);
663     				if (!SCint)
664     					panic ("SCint == NULL in seagate");
665     				SCtmp = SCint;
666     				SCint = NULL;
667     				SCtmp->result = temp;
668     				done_fn (SCtmp);
669     			} else
670     				printk ("done_fn() not defined.\n");
671     		}
672     	}
673     }
674     
675     /*
676      * The seagate_st0x_queue_command() function provides a queued interface
677      * to the seagate SCSI driver.  Basically, it just passes control onto the
678      * seagate_command() function, after fixing it so that the done_fn()
679      * is set to the one passed to the function.  We have to be very careful,
680      * because there are some commands on some devices that do not disconnect,
681      * and if we simply call the done_fn when the command is done then another
682      * command is started and queue_command is called again...  We end up
683      * overflowing the kernel stack, and this tends not to be such a good idea.
684      */
685     
686     static int recursion_depth = 0;
687     
688     int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
689     {
690     	int result, reconnect;
691     	Scsi_Cmnd *SCtmp;
692     
693     	DANY ("seagate: que_command");
694     	done_fn = done;
695     	current_target = SCpnt->target;
696     	current_lun = SCpnt->lun;
697     	(const void *) current_cmnd = SCpnt->cmnd;
698     	current_data = (unsigned char *) SCpnt->request_buffer;
699     	current_bufflen = SCpnt->request_bufflen;
700     	SCint = SCpnt;
701     	if (recursion_depth)
702     		return 0;
703     	recursion_depth++;
704     	do {
705     #ifdef LINKED
706     /*
707      * Set linked command bit in control field of SCSI command.
708      */
709     
710     		current_cmnd[SCpnt->cmd_len] |= 0x01;
711     		if (linked_connected) {
712     			DPRINTK (DEBUG_LINKED,
713     				 "scsi%d : using linked commands, current I_T_L nexus is ",
714     				 hostno);
715     			if ((linked_target == current_target)
716     			    && (linked_lun == current_lun)) {
717     				DPRINTK (DEBUG_LINKED, "correct\n");
718     				reconnect = LINKED_RIGHT;
719     			} else {
720     				DPRINTK (DEBUG_LINKED, "incorrect\n");
721     				reconnect = LINKED_WRONG;
722     			}
723     		} else
724     #endif				/* LINKED */
725     			reconnect = CAN_RECONNECT;
726     
727     		result =
728     		    internal_command (SCint->target, SCint->lun, SCint->cmnd,
729     				      SCint->request_buffer,
730     				      SCint->request_bufflen, reconnect);
731     		if (msg_byte (result) == DISCONNECT)
732     			break;
733     		SCtmp = SCint;
734     		SCint = NULL;
735     		SCtmp->result = result;
736     		done_fn (SCtmp);
737     	}
738     	while (SCint);
739     	recursion_depth--;
740     	return 0;
741     }
742     
743     int seagate_st0x_command (Scsi_Cmnd * SCpnt)
744     {
745     	return internal_command (SCpnt->target, SCpnt->lun, SCpnt->cmnd,
746     				 SCpnt->request_buffer, SCpnt->request_bufflen,
747     				 (int) NO_RECONNECT);
748     }
749     
750     static int internal_command (unsigned char target, unsigned char lun,
751     		  const void *cmnd, void *buff, int bufflen, int reselect)
752     {
753     	unsigned char *data = NULL;
754     	struct scatterlist *buffer = NULL;
755     	int clock, temp, nobuffs = 0, done = 0, len = 0;
756     	unsigned long flags;
757     
758     #ifdef DEBUG
759     	int transfered = 0, phase = 0, newphase;
760     #endif
761     
762     	register unsigned char status_read;
763     	unsigned char tmp_data, tmp_control, status = 0, message = 0;
764     
765     	unsigned transfersize = 0, underflow = 0;
766     
767     #ifdef SLOW_RATE
768     	int borken = (int) SCint->device->borken;	/* Does the current target require
769     							   Very Slow I/O ?  */
770     #endif
771     
772     	incommand = 0;
773     	st0x_aborted = 0;
774     
775     #if (DEBUG & PRINT_COMMAND)
776     	printk ("scsi%d : target = %d, command = ", hostno, target);
777     	print_command ((unsigned char *) cmnd);
778     #endif
779     
780     #if (DEBUG & PHASE_RESELECT)
781     	switch (reselect) {
782     	case RECONNECT_NOW:
783     		printk ("scsi%d : reconnecting\n", hostno);
784     		break;
785     #ifdef LINKED
786     	case LINKED_RIGHT:
787     		printk ("scsi%d : connected, can reconnect\n", hostno);
788     		break;
789     	case LINKED_WRONG:
790     		printk ("scsi%d : connected to wrong target, can reconnect\n",
791     			hostno);
792     		break;
793     #endif
794     	case CAN_RECONNECT:
795     		printk ("scsi%d : allowed to reconnect\n", hostno);
796     		break;
797     	default:
798     		printk ("scsi%d : not allowed to reconnect\n", hostno);
799     	}
800     #endif
801     
802     	if (target == (controller_type == SEAGATE ? 7 : 6))
803     		return DID_BAD_TARGET;
804     
805     /*
806      *	We work it differently depending on if this is is "the first time,"
807      *      or a reconnect.  If this is a reselect phase, then SEL will
808      *      be asserted, and we must skip selection / arbitration phases.
809      */
810     
811     	switch (reselect) {
812     	case RECONNECT_NOW:
813     		DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
814     
815     /*
816      *	At this point, we should find the logical or of our ID and the original
817      *      target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
818      *
819      *      After ARBITRATION phase is completed, only SEL, BSY, and the
820      *      target ID are asserted.  A valid initiator ID is not on the bus
821      *      until IO is asserted, so we must wait for that.
822      */
823     		ULOOP (100 * 1000) {
824     			temp = STATUS;
825     			if ((temp & STAT_IO) && !(temp & STAT_BSY))
826     				break;
827     
828     			if (TIMEOUT) {
829     				DPRINTK (PHASE_RESELECT,
830     					 "scsi%d : RESELECT timed out while waiting for IO .\n",
831     					 hostno);
832     				return (DID_BAD_INTR << 16);
833     			}
834     		}
835     
836     /*
837      *	After I/O is asserted by the target, we can read our ID and its
838      *      ID off of the BUS.
839      */
840     
841     		if (!
842     		    ((temp =
843     		      DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
844     			DPRINTK (PHASE_RESELECT,
845     				 "scsi%d : detected reconnect request to different target.\n"
846     				 "\tData bus = %d\n", hostno, temp);
847     			return (DID_BAD_INTR << 16);
848     		}
849     
850     		if (!(temp & (1 << current_target))) {
851     			printk
852     			    ("scsi%d : Unexpected reselect interrupt.  Data bus = %d\n",
853     			     hostno, temp);
854     			return (DID_BAD_INTR << 16);
855     		}
856     
857     		buffer = current_buffer;
858     		cmnd = current_cmnd;	/* WDE add */
859     		data = current_data;	/* WDE add */
860     		len = current_bufflen;	/* WDE add */
861     		nobuffs = current_nobuffs;
862     
863     /*
864      *	We have determined that we have been selected.  At this point,
865      *      we must respond to the reselection by asserting BSY ourselves
866      */
867     
868     #if 1
869     		WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
870     #else
871     		WRITE_CONTROL (BASE_CMD | CMD_BSY);
872     #endif
873     
874     /*
875      *	The target will drop SEL, and raise BSY, at which time we must drop
876      *      BSY.
877      */
878     
879     		ULOOP (100 * 1000) {
880     			if (!(STATUS & STAT_SEL))
881     				break;
882     			if (TIMEOUT) {
883     				WRITE_CONTROL (BASE_CMD | CMD_INTR);
884     				DPRINTK (PHASE_RESELECT,
885     					 "scsi%d : RESELECT timed out while waiting for SEL.\n",
886     					 hostno);
887     				return (DID_BAD_INTR << 16);
888     			}
889     		}
890     
891     		WRITE_CONTROL (BASE_CMD);
892     
893     /*
894      *	At this point, we have connected with the target and can get
895      *      on with our lives.
896      */
897     		break;
898     	case CAN_RECONNECT:
899     
900     #ifdef LINKED
901     /*
902      * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
903      * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
904      * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT
905      * message on MESSAGE OUT phase, and then loop back to here.
906      */
907     
908     	      connect_loop:
909     
910     #endif
911     
912     		DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n",
913     			 hostno);
914     
915     /*
916      *    BUS FREE PHASE
917      *
918      *      On entry, we make sure that the BUS is in a BUS FREE
919      *      phase, by insuring that both BSY and SEL are low for
920      *      at least one bus settle delay.  Several reads help
921      *      eliminate wire glitch.
922      */
923     
924     #ifndef ARBITRATE
925     #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
926     		clock = jiffies + ST0X_BUS_FREE_DELAY;
927     
928     		while (((STATUS | STATUS | STATUS) &
929     			(STAT_BSY | STAT_SEL)) &&
930     		       (!st0x_aborted) && time_before (jiffies, clock)) ;
931     
932     		if (time_after (jiffies, clock))
933     			return retcode (DID_BUS_BUSY);
934     		else if (st0x_aborted)
935     			return retcode (st0x_aborted);
936     #endif
937     
938     		DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n",
939     			 hostno);
940     
941     		clock = jiffies + ST0X_SELECTION_DELAY;
942     
943     /*
944      * Arbitration/selection procedure :
945      * 1.  Disable drivers
946      * 2.  Write HOST adapter address bit
947      * 3.  Set start arbitration.
948      * 4.  We get either ARBITRATION COMPLETE or SELECT at this
949      *     point.
950      * 5.  OR our ID and targets on bus.
951      * 6.  Enable SCSI drivers and asserted SEL and ATTN
952      */
953     
954     #ifdef ARBITRATE
955     		save_flags (flags);
956     		cli ();
957     		WRITE_CONTROL (0);
958     		WRITE_DATA ((controller_type == SEAGATE) ? 0x80 : 0x40);
959     		WRITE_CONTROL (CMD_START_ARB);
960     		restore_flags (flags);
961     
962     		ULOOP (ST0X_SELECTION_DELAY * 10000) {
963     			status_read = STATUS;
964     			if (status_read & STAT_ARB_CMPL)
965     				break;
966     			if (st0x_aborted)	/* FIXME: What? We are going to do something even after abort? */
967     				break;
968     			if (TIMEOUT || (status_read & STAT_SEL)) {
969     				printk
970     				    ("scsi%d : arbitration lost or timeout.\n",
971     				     hostno);
972     				WRITE_CONTROL (BASE_CMD);
973     				return retcode (DID_NO_CONNECT);
974     			}
975     		}
976     
977     		DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n",
978     			 hostno);
979     #endif
980     
981     /*
982      *    When the SCSI device decides that we're gawking at it, it will
983      *    respond by asserting BUSY on the bus.
984      *
985      *    Note : the Seagate ST-01/02 product manual says that we should
986      *    twiddle the DATA register before the control register.    However,
987      *    this does not work reliably so we do it the other way around.
988      *
989      *    Probably could be a problem with arbitration too, we really should
990      *    try this with a SCSI protocol or logic analyzer to see what is
991      *    going on.
992      */
993     		tmp_data =
994     		    (unsigned char) ((1 << target) |
995     				     (controller_type ==
996     				      SEAGATE ? 0x80 : 0x40));
997     		tmp_control =
998     		    BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN
999     							    : 0);
1000     
1001     		save_flags (flags);
1002     		cli ();
1003     #ifdef OLDCNTDATASCEME
1004     #ifdef SWAPCNTDATA
1005     		WRITE_CONTROL (tmp_control);
1006     		WRITE_DATA (tmp_data);
1007     #else
1008     		WRITE_DATA (tmp_data);
1009     		WRITE_CONTROL (tmp_control);
1010     #endif
1011     #else
1012     		tmp_control ^= CMD_BSY;	/* This is guesswork. What used to be in driver    */
1013     		WRITE_CONTROL (tmp_control);	/* could never work: it sent data into control     */
1014     		WRITE_DATA (tmp_data);	/* register and control info into data. Hopefully  */
1015     		tmp_control ^= CMD_BSY;	/* fixed, but order of first two may be wrong.     */
1016     		WRITE_CONTROL (tmp_control);	/* -- pavel@ucw.cz   */
1017     #endif
1018     
1019     		restore_flags (flags);
1020     
1021     		ULOOP (250 * 1000) {
1022     			if (st0x_aborted) {
1023     /*
1024      *	If we have been aborted, and we have a command in progress, IE the
1025      *      target still has BSY asserted, then we will reset the bus, and
1026      *      notify the midlevel driver to expect sense.
1027      */
1028     
1029     				WRITE_CONTROL (BASE_CMD);
1030     				if (STATUS & STAT_BSY) {
1031     					printk
1032     					    ("scsi%d : BST asserted after we've been aborted.\n",
1033     					     hostno);
1034     					seagate_st0x_reset (NULL, 0);
1035     					return retcode (DID_RESET);
1036     				}
1037     				return retcode (st0x_aborted);
1038     			}
1039     			if (STATUS & STAT_BSY)
1040     				break;
1041     			if (TIMEOUT) {
1042     				DPRINTK (PHASE_SELECTION,
1043     					 "scsi%d : NO CONNECT with target %d, stat = %x \n",
1044     					 hostno, target, STATUS);
1045     				return retcode (DID_NO_CONNECT);
1046     			}
1047     		}
1048     
1049     /* Establish current pointers.  Take into account scatter / gather */
1050     
1051     		if ((nobuffs = SCint->use_sg)) {
1052     #if (DEBUG & DEBUG_SG)
1053     			{
1054     				int i;
1055     
1056     				printk
1057     				    ("scsi%d : scatter gather requested, using %d buffers.\n",
1058     				     hostno, nobuffs);
1059     				for (i = 0; i < nobuffs; ++i)
1060     					printk
1061     					    ("scsi%d : buffer %d address = %08x length = %d\n",
1062     					     hostno, i, buffer[i].address,
1063     					     buffer[i].length);
1064     			}
1065     #endif
1066     
1067     			buffer = (struct scatterlist *) SCint->buffer;
1068     			len = buffer->length;
1069     			data = (unsigned char *) buffer->address;
1070     		} else {
1071     			DPRINTK (DEBUG_SG,
1072     				 "scsi%d : scatter gather not requested.\n",
1073     				 hostno);
1074     			buffer = NULL;
1075     			len = SCint->request_bufflen;
1076     			data = (unsigned char *) SCint->request_buffer;
1077     		}
1078     
1079     		DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1080     			 hostno, len);
1081     
1082     		break;
1083     #ifdef LINKED
1084     	case LINKED_RIGHT:
1085     		break;
1086     	case LINKED_WRONG:
1087     		break;
1088     #endif
1089     	}			/* end of switch(reselect) */
1090     
1091     /*
1092      *    There are several conditions under which we wish to send a message :
1093      *      1.  When we are allowing disconnect / reconnect, and need to establish
1094      *          the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
1095      *
1096      *      2.  When we are doing linked commands, are have the wrong I_T_L nexus
1097      *          established and want to send an ABORT message.
1098      */
1099     
1100     /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1101     #ifdef LINKED
1102     	WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1103     		       (((reselect == CAN_RECONNECT)
1104     			 || (reselect == LINKED_WRONG)
1105     			)? CMD_ATTN : 0));
1106     #else
1107     	WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1108     		       (((reselect == CAN_RECONNECT)
1109     			)? CMD_ATTN : 0));
1110     #endif
1111     
1112     /*
1113      *    INFORMATION TRANSFER PHASE
1114      *
1115      *      The nasty looking read / write inline assembler loops we use for
1116      *      DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1117      *      the 'C' versions - since we're moving 1024 bytes of data, this
1118      *      really adds up.
1119      *
1120      *      SJT: The nasty-looking assembler is gone, so it's slower.
1121      *
1122      */
1123     
1124     	DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1125     
1126     	incommand = 1;
1127     	transfersize = SCint->transfersize;
1128     	underflow = SCint->underflow;
1129     
1130     /*
1131      *	Now, we poll the device for status information,
1132      *      and handle any requests it makes.  Note that since we are unsure of
1133      *      how much data will be flowing across the system, etc and cannot
1134      *      make reasonable timeouts, that we will instead have the midlevel
1135      *      driver handle any timeouts that occur in this phase.
1136      */
1137     
1138     	while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1139     #ifdef PARITY
1140     		if (status_read & STAT_PARITY) {
1141     			printk ("scsi%d : got parity error\n", hostno);
1142     			st0x_aborted = DID_PARITY;
1143     		}
1144     #endif
1145     
1146     		if (status_read & STAT_REQ) {
1147     #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1148     			if ((newphase = (status_read & REQ_MASK)) != phase) {
1149     				phase = newphase;
1150     				switch (phase) {
1151     				case REQ_DATAOUT:
1152     					printk ("scsi%d : phase = DATA OUT\n",
1153     						hostno);
1154     					break;
1155     				case REQ_DATAIN:
1156     					printk ("scsi%d : phase = DATA IN\n",
1157     						hostno);
1158     					break;
1159     				case REQ_CMDOUT:
1160     					printk
1161     					    ("scsi%d : phase = COMMAND OUT\n",
1162     					     hostno);
1163     					break;
1164     				case REQ_STATIN:
1165     					printk ("scsi%d : phase = STATUS IN\n",
1166     						hostno);
1167     					break;
1168     				case REQ_MSGOUT:
1169     					printk
1170     					    ("scsi%d : phase = MESSAGE OUT\n",
1171     					     hostno);
1172     					break;
1173     				case REQ_MSGIN:
1174     					printk ("scsi%d : phase = MESSAGE IN\n",
1175     						hostno);
1176     					break;
1177     				default:
1178     					printk ("scsi%d : phase = UNKNOWN\n",
1179     						hostno);
1180     					st0x_aborted = DID_ERROR;
1181     				}
1182     			}
1183     #endif
1184     			switch (status_read & REQ_MASK) {
1185     			case REQ_DATAOUT:
1186     /*
1187      * If we are in fast mode, then we simply splat the data out
1188      * in word-sized chunks as fast as we can.
1189      */
1190     
1191     				if (!len) {
1192     #if 0
1193     					printk
1194     					    ("scsi%d: underflow to target %d lun %d \n",
1195     					     hostno, target, lun);
1196     					st0x_aborted = DID_ERROR;
1197     					fast = 0;
1198     #endif
1199     					break;
1200     				}
1201     
1202     				if (fast && transfersize
1203     				    && !(len % transfersize)
1204     				    && (len >= transfersize)
1205     #ifdef FAST32
1206     				    && !(transfersize % 4)
1207     #endif
1208     				    ) {
1209     					DPRINTK (DEBUG_FAST,
1210     						 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1211     						 "         len = %d, data = %08x\n",
1212     						 hostno, SCint->underflow,
1213     						 SCint->transfersize, len,
1214     						 data);
1215     
1216     /* SJT: Start. Fast Write */
1217     #ifdef SEAGATE_USE_ASM
1218     					__asm__ ("cld\n\t"
1219     #ifdef FAST32
1220     						 "shr $2, %%ecx\n\t"
1221     						 "1:\t"
1222     						 "lodsl\n\t"
1223     						 "movl %%eax, (%%edi)\n\t"
1224     #else
1225     						 "1:\t"
1226     						 "lodsb\n\t"
1227     						 "movb %%al, (%%edi)\n\t"
1228     #endif
1229     						 "loop 1b;"
1230     				      /* output */ :
1231     				      /* input */ :"D" (phys_to_virt (st0x_dr)),
1232     						 "S"
1233     						 (data),
1234     						 "c" (SCint->transfersize)
1235     /* clobbered */
1236     				      :	 "eax", "ecx",
1237     						 "esi");
1238     #else				/* SEAGATE_USE_ASM */
1239     					{
1240     #ifdef FAST32
1241     						unsigned int *iop =
1242     						    phys_to_virt (st0x_dr);
1243     						const unsigned int *dp =
1244     						    (unsigned int *) data;
1245     						int xferlen = transfersize >> 2;
1246     #else
1247     						unsigned char *iop =
1248     						    phys_to_virt (st0x_dr);
1249     						const unsigned char *dp = data;
1250     						int xferlen = transfersize;
1251     #endif
1252     						for (; xferlen; --xferlen)
1253     							*iop = *dp++;
1254     					}
1255     #endif				/* SEAGATE_USE_ASM */
1256     /* SJT: End */
1257     					len -= transfersize;
1258     					data += transfersize;
1259     					DPRINTK (DEBUG_FAST,
1260     						 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1261     						 hostno, len, data);
1262     				} else {
1263     /*
1264      *    We loop as long as we are in a data out phase, there is data to send,
1265      *      and BSY is still active.
1266      */
1267     
1268     /* SJT: Start. Slow Write. */
1269     #ifdef SEAGATE_USE_ASM
1270     
1271     					int __dummy_1, __dummy_2;
1272     
1273     /*
1274      *      We loop as long as we are in a data out phase, there is data to send, 
1275      *      and BSY is still active.
1276      */
1277     /* Local variables : len = ecx , data = esi, 
1278                          st0x_cr_sr = ebx, st0x_dr =  edi
1279     */
1280     					__asm__ (
1281     							/* Test for any data here at all. */
1282     							"orl %%ecx, %%ecx\n\t"
1283     							"jz 2f\n\t" "cld\n\t"
1284     /*                    "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%ebx\n\t"  */
1285     /*                    "movl " SYMBOL_NAME_STR(st0x_dr) ", %%edi\n\t"  */
1286     							"1:\t"
1287     							"movb (%%ebx), %%al\n\t"
1288     							/* Test for BSY */
1289     							"test $1, %%al\n\t"
1290     							"jz 2f\n\t"
1291     							/* Test for data out phase - STATUS & REQ_MASK should be 
1292     							   REQ_DATAOUT, which is 0. */
1293     							"test $0xe, %%al\n\t"
1294     							"jnz 2f\n\t"
1295     							/* Test for REQ */
1296     							"test $0x10, %%al\n\t"
1297     							"jz 1b\n\t"
1298     							"lodsb\n\t"
1299     							"movb %%al, (%%edi)\n\t"
1300     							"loop 1b\n\t" "2:\n"
1301     				      /* output */ :"=S" (data), "=c" (len),
1302     							"=b"
1303     							(__dummy_1),
1304     							"=D" (__dummy_2)
1305     /* input */
1306     				      :		"0" (data), "1" (len),
1307     							"2" (phys_to_virt
1308     							     (st0x_cr_sr)),
1309     							"3" (phys_to_virt
1310     							     (st0x_dr))
1311     /* clobbered */
1312     				      :		"eax");
1313     #else				/* SEAGATE_USE_ASM */
1314     					while (len) {
1315     						unsigned char stat;
1316     
1317     						stat = STATUS;
1318     						if (!(stat & STAT_BSY)
1319     						    || ((stat & REQ_MASK) !=
1320     							REQ_DATAOUT))
1321     							break;
1322     						if (stat & STAT_REQ) {
1323     							WRITE_DATA (*data++);
1324     							--len;
1325     						}
1326     					}
1327     #endif				/* SEAGATE_USE_ASM */
1328     /* SJT: End. */
1329     				}
1330     
1331     				if (!len && nobuffs) {
1332     					--nobuffs;
1333     					++buffer;
1334     					len = buffer->length;
1335     					data =
1336     					    (unsigned char *) buffer->address;
1337     					DPRINTK (DEBUG_SG,
1338     						 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1339     						 hostno, len, data);
1340     				}
1341     				break;
1342     
1343     			case REQ_DATAIN:
1344     #ifdef SLOW_RATE
1345     				if (borken) {
1346     #if (DEBUG & (PHASE_DATAIN))
1347     					transfered += len;
1348     #endif
1349     					for (;
1350     					     len
1351     					     && (STATUS & (REQ_MASK | STAT_REQ))
1352     					     == (REQ_DATAIN | STAT_REQ);
1353     					     --len) {
1354     						*data++ = DATA;
1355     						borken_wait ();
1356     					}
1357     #if (DEBUG & (PHASE_DATAIN))
1358     					transfered -= len;
1359     #endif
1360     				} else
1361     #endif
1362     
1363     					if (fast && transfersize
1364     					    && !(len % transfersize)
1365     					    && (len >= transfersize)
1366     #ifdef FAST32
1367     					    && !(transfersize % 4)
1368     #endif
1369     				    ) {
1370     					DPRINTK (DEBUG_FAST,
1371     						 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1372     						 "         len = %d, data = %08x\n",
1373     						 hostno, SCint->underflow,
1374     						 SCint->transfersize, len,
1375     						 data);
1376     
1377     /* SJT: Start. Fast Read */
1378     #ifdef SEAGATE_USE_ASM
1379     					__asm__ ("cld\n\t"
1380     #ifdef FAST32
1381     						 "shr $2, %%ecx\n\t"
1382     						 "1:\t"
1383     						 "movl (%%esi), %%eax\n\t"
1384     						 "stosl\n\t"
1385     #else
1386     						 "1:\t"
1387     						 "movb (%%esi), %%al\n\t"
1388     						 "stosb\n\t"
1389     #endif
1390     						 "loop 1b\n\t"
1391     				      /* output */ :
1392     				      /* input */ :"S" (phys_to_virt (st0x_dr)),
1393     						 "D"
1394     						 (data),
1395     						 "c" (SCint->transfersize)
1396     /* clobbered */
1397     				      :	 "eax", "ecx",
1398     						 "edi");
1399     #else				/* SEAGATE_USE_ASM */
1400     					{
1401     #ifdef FAST32
1402     						const unsigned int *iop =
1403     						    phys_to_virt (st0x_dr);
1404     						unsigned int *dp =
1405     						    (unsigned int *) data;
1406     						int xferlen = len >> 2;
1407     #else
1408     						const unsigned char *iop =
1409     						    phys_to_virt (st0x_dr);
1410     						unsigned char *dp = data;
1411     						int xferlen = len;
1412     #endif
1413     						for (; xferlen; --xferlen)
1414     							*dp++ = *iop;
1415     					}
1416     #endif				/* SEAGATE_USE_ASM */
1417     /* SJT: End */
1418     					len -= transfersize;
1419     					data += transfersize;
1420     #if (DEBUG & PHASE_DATAIN)
1421     					printk ("scsi%d: transfered += %d\n",
1422     						hostno, transfersize);
1423     					transfered += transfersize;
1424     #endif
1425     
1426     					DPRINTK (DEBUG_FAST,
1427     						 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1428     						 hostno, len, data);
1429     				} else {
1430     
1431     #if (DEBUG & PHASE_DATAIN)
1432     					printk ("scsi%d: transfered += %d\n",
1433     						hostno, len);
1434     					transfered += len;	/* Assume we'll transfer it all, then
1435     								   subtract what we *didn't* transfer */
1436     #endif
1437     
1438     /*
1439      *	We loop as long as we are in a data in phase, there is room to read,
1440      *      and BSY is still active
1441      */
1442     
1443     /* SJT: Start. */
1444     #ifdef SEAGATE_USE_ASM
1445     
1446     					int __dummy_3, __dummy_4;
1447     
1448     /* Dummy clobbering variables for the new gcc-2.95 */
1449     
1450     /*
1451      *      We loop as long as we are in a data in phase, there is room to read, 
1452      *      and BSY is still active
1453      */
1454     					/* Local variables : ecx = len, edi = data
1455     					   esi = st0x_cr_sr, ebx = st0x_dr */
1456     					__asm__ (
1457     							/* Test for room to read */
1458     							"orl %%ecx, %%ecx\n\t"
1459     							"jz 2f\n\t" "cld\n\t"
1460     /*                "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%esi\n\t"  */
1461     /*                "movl " SYMBOL_NAME_STR(st0x_dr) ", %%ebx\n\t"  */
1462     							"1:\t"
1463     							"movb (%%esi), %%al\n\t"
1464     							/* Test for BSY */
1465     							"test $1, %%al\n\t"
1466     							"jz 2f\n\t"
1467     							/* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN, 
1468     							   = STAT_IO, which is 4. */
1469     							"movb $0xe, %%ah\n\t"
1470     							"andb %%al, %%ah\n\t"
1471     							"cmpb $0x04, %%ah\n\t"
1472     							"jne 2f\n\t"
1473     							/* Test for REQ */
1474     							"test $0x10, %%al\n\t"
1475     							"jz 1b\n\t"
1476     							"movb (%%ebx), %%al\n\t"
1477     							"stosb\n\t"
1478     							"loop 1b\n\t" "2:\n"
1479     				      /* output */ :"=D" (data), "=c" (len),
1480     							"=S"
1481     							(__dummy_3),
1482     							"=b" (__dummy_4)
1483     /* input */
1484     				      :		"0" (data), "1" (len),
1485     							"2" (phys_to_virt
1486     							     (st0x_cr_sr)),
1487     							"3" (phys_to_virt
1488     							     (st0x_dr))
1489     /* clobbered */
1490     				      :		"eax");
1491     #else				/* SEAGATE_USE_ASM */
1492     					while (len) {
1493     						unsigned char stat;
1494     
1495     						stat = STATUS;
1496     						if (!(stat & STAT_BSY)
1497     						    || ((stat & REQ_MASK) !=
1498     							REQ_DATAIN))
1499     							break;
1500     						if (stat & STAT_REQ) {
1501     							*data++ = DATA;
1502     							--len;
1503     						}
1504     					}
1505     #endif				/* SEAGATE_USE_ASM */
1506     /* SJT: End. */
1507     #if (DEBUG & PHASE_DATAIN)
1508     					printk ("scsi%d: transfered -= %d\n",
1509     						hostno, len);
1510     					transfered -= len;	/* Since we assumed all of Len got  *
1511     								   transfered, correct our mistake */
1512     #endif
1513     				}
1514     
1515     				if (!len && nobuffs) {
1516     					--nobuffs;
1517     					++buffer;
1518     					len = buffer->length;
1519     					data =
1520     					    (unsigned char *) buffer->address;
1521     					DPRINTK (DEBUG_SG,
1522     						 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1523     						 hostno, len, data);
1524     				}
1525     
1526     				break;
1527     
1528     			case REQ_CMDOUT:
1529     				while (((status_read = STATUS) & STAT_BSY) &&
1530     				       ((status_read & REQ_MASK) == REQ_CMDOUT))
1531     					if (status_read & STAT_REQ) {
1532     						WRITE_DATA (*
1533     							    (const unsigned char
1534     							     *) cmnd);
1535     						cmnd =
1536     						    1 +
1537     						    (const unsigned char *)
1538     						    cmnd;
1539     #ifdef SLOW_RATE
1540     						if (borken)
1541     							borken_wait ();
1542     #endif
1543     					}
1544     				break;
1545     
1546     			case REQ_STATIN:
1547     				status = DATA;
1548     				break;
1549     
1550     			case REQ_MSGOUT:
1551     /*
1552      *	We can only have sent a MSG OUT if we requested to do this
1553      *      by raising ATTN.  So, we must drop ATTN.
1554      */
1555     
1556     				WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1557     /*
1558      *	If we are reconnecting, then we must send an IDENTIFY message in
1559      *      response  to MSGOUT.
1560      */
1561     				switch (reselect) {
1562     				case CAN_RECONNECT:
1563     					WRITE_DATA (IDENTIFY (1, lun));
1564     
1565     					DPRINTK (PHASE_RESELECT | PHASE_MSGOUT,
1566     						 "scsi%d : sent IDENTIFY message.\n",
1567     						 hostno);
1568     					break;
1569     #ifdef LINKED
1570     				case LINKED_WRONG:
1571     					WRITE_DATA (ABORT);
1572     					linked_connected = 0;
1573     					reselect = CAN_RECONNECT;
1574     					goto connect_loop;
1575     					DPRINTK (PHASE_MSGOUT | DEBUG_LINKED,
1576     						 "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n",
1577     						 hostno);
1578     #endif				/* LINKED */
1579     					DPRINTK (DEBUG_LINKED, "correct\n");
1580     				default:
1581     					WRITE_DATA (NOP);
1582     					printk
1583     					    ("scsi%d : target %d requested MSGOUT, sent NOP message.\n",
1584     					     hostno, target);
1585     				}
1586     				break;
1587     
1588     			case REQ_MSGIN:
1589     				switch (message = DATA) {
1590     				case DISCONNECT:
1591     					DANY ("seagate: deciding to disconnect\n");
1592     					should_reconnect = 1;
1593     					current_data = data;	/* WDE add */
1594     					current_buffer = buffer;
1595     					current_bufflen = len;	/* WDE add */
1596     					current_nobuffs = nobuffs;
1597     #ifdef LINKED
1598     					linked_connected = 0;
1599     #endif
1600     					done = 1;
1601     					DPRINTK ((PHASE_RESELECT | PHASE_MSGIN),
1602     						 "scsi%d : disconnected.\n",
1603     						 hostno);
1604     					break;
1605     
1606     #ifdef LINKED
1607     				case LINKED_CMD_COMPLETE:
1608     				case LINKED_FLG_CMD_COMPLETE:
1609     #endif
1610     				case COMMAND_COMPLETE:
1611     /*
1612      * Note : we should check for underflow here.
1613      */
1614     					DPRINTK (PHASE_MSGIN,
1615     						 "scsi%d : command complete.\n",
1616     						 hostno);
1617     					done = 1;
1618     					break;
1619     				case ABORT:
1620     					DPRINTK (PHASE_MSGIN,
1621     						 "scsi%d : abort message.\n",
1622     						 hostno);
1623     					done = 1;
1624     					break;
1625     				case SAVE_POINTERS:
1626     					current_buffer = buffer;
1627     					current_bufflen = len;	/* WDE add */
1628     					current_data = data;	/* WDE mod */
1629     					current_nobuffs = nobuffs;
1630     					DPRINTK (PHASE_MSGIN,
1631     						 "scsi%d : pointers saved.\n",
1632     						 hostno);
1633     					break;
1634     				case RESTORE_POINTERS:
1635     					buffer = current_buffer;
1636     					cmnd = current_cmnd;
1637     					data = current_data;	/* WDE mod */
1638     					len = current_bufflen;
1639     					nobuffs = current_nobuffs;
1640     					DPRINTK (PHASE_MSGIN,
1641     						 "scsi%d : pointers restored.\n",
1642     						 hostno);
1643     					break;
1644     				default:
1645     
1646     /*
1647      *	IDENTIFY distinguishes itself from the other messages by setting the
1648      *      high byte. [FIXME: should not this read "the high bit"? - pavel@ucw.cz]
1649      *
1650      *      Note : we need to handle at least one outstanding command per LUN,
1651      *      and need to hash the SCSI command for that I_T_L nexus based on the
1652      *      known ID (at this point) and LUN.
1653      */
1654     
1655     					if (message & 0x80) {
1656     						DPRINTK (PHASE_MSGIN,
1657     							 "scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1658     							 hostno, target,
1659     							 message & 7);
1660     					} else {
1661     
1662     /*
1663      *      We should go into a MESSAGE OUT phase, and send  a MESSAGE_REJECT
1664      *      if we run into a message that we don't like.  The seagate driver
1665      *      needs some serious restructuring first though.
1666      */
1667     
1668     						DPRINTK (PHASE_MSGIN,
1669     							 "scsi%d : unknown message %d from target %d.\n",
1670     							 hostno, message,
1671     							 target);
1672     					}
1673     				}
1674     				break;
1675     
1676     			default:
1677     				printk ("scsi%d : unknown phase.\n", hostno);
1678     				st0x_aborted = DID_ERROR;
1679     			}	/* end of switch (status_read &
1680     				   REQ_MASK) */
1681     
1682     #ifdef SLOW_RATE
1683     /*
1684      * I really don't care to deal with borken devices in each single
1685      * byte transfer case (ie, message in, message out, status), so
1686      * I'll do the wait here if necessary.
1687      */
1688     			if (borken)
1689     				borken_wait ();
1690     #endif
1691     
1692     		}		/* if(status_read & STAT_REQ) ends */
1693     	}			/* while(((status_read = STATUS)...)
1694     				   ends */
1695     
1696     	DPRINTK (PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT,
1697     		 "scsi%d : Transfered %d bytes\n", hostno, transfered);
1698     
1699     #if (DEBUG & PHASE_EXIT)
1700     #if 0				/* Doesn't work for scatter/gather */
1701     	printk ("Buffer : \n");
1702     	for (i = 0; i < 20; ++i)
1703     		printk ("%02x  ", ((unsigned char *) data)[i]);	/* WDE mod */
1704     	printk ("\n");
1705     #endif
1706     	printk ("scsi%d : status = ", hostno);
1707     	print_status (status);
1708     	printk ("message = %02x\n", message);
1709     #endif
1710     
1711     /* We shouldn't reach this until *after* BSY has been deasserted */
1712     
1713     #ifdef LINKED
1714     	else
1715     	{
1716     /*
1717      * Fix the message byte so that unsuspecting high level drivers don't
1718      * puke when they see a LINKED COMMAND message in place of the COMMAND
1719      * COMPLETE they may be expecting.  Shouldn't be necessary, but it's
1720      * better to be on the safe side.
1721      *
1722      * A non LINKED* message byte will indicate that the command completed,
1723      * and we are now disconnected.
1724      */
1725     
1726     		switch (message) {
1727     		case LINKED_CMD_COMPLETE:
1728     		case LINKED_FLG_CMD_COMPLETE:
1729     			message = COMMAND_COMPLETE;
1730     			linked_target = current_target;
1731     			linked_lun = current_lun;
1732     			linked_connected = 1;
1733     			DPRINTK (DEBUG_LINKED,
1734     				 "scsi%d : keeping I_T_L nexus established"
1735     				 "for linked command.\n", hostno);
1736     			/* We also will need to adjust status to accommodate intermediate
1737     			   conditions. */
1738     			if ((status == INTERMEDIATE_GOOD) ||
1739     			    (status == INTERMEDIATE_C_GOOD))
1740     				status = GOOD;
1741     
1742     			break;
1743     /*
1744      * We should also handle what are "normal" termination messages
1745      * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually,
1746      * and flake if things aren't right.
1747      */
1748     		default:
1749     			DPRINTK (DEBUG_LINKED,
1750     				 "scsi%d : closing I_T_L nexus.\n", hostno);
1751     			linked_connected = 0;
1752     		}
1753     	}
1754     #endif				/* LINKED */
1755     
1756     	if (should_reconnect) {
1757     		DPRINTK (PHASE_RESELECT,
1758     			 "scsi%d : exiting seagate_st0x_queue_command()"
1759     			 "with reconnect enabled.\n", hostno);
1760     		WRITE_CONTROL (BASE_CMD | CMD_INTR);
1761     	} else
1762     		WRITE_CONTROL (BASE_CMD);
1763     
1764     	return retcode (st0x_aborted);
1765     }				/* end of internal_command */
1766     
1767     static int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1768     {
1769     	st0x_aborted = DID_ABORT;
1770     	return SCSI_ABORT_PENDING;
1771     }
1772     
1773     #undef ULOOP
1774     #undef TIMEOUT
1775     
1776     /*
1777      * the seagate_st0x_reset function resets the SCSI bus 
1778      */
1779     
1780     static int seagate_st0x_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1781     {
1782     /* No timeouts - this command is going to fail because it was reset. */
1783     	DANY ("scsi%d: Reseting bus... ", hostno);
1784     
1785     /* assert  RESET signal on SCSI bus.  */
1786     	WRITE_CONTROL (BASE_CMD | CMD_RST);
1787     
1788     	udelay (20 * 1000);
1789     
1790     	WRITE_CONTROL (BASE_CMD);
1791     	st0x_aborted = DID_RESET;
1792     
1793     	DANY ("done.\n");
1794     	return SCSI_RESET_WAKEUP;
1795     }
1796     
1797     /* Eventually this will go into an include file, but this will be later */
1798     static Scsi_Host_Template driver_template = SEAGATE_ST0X;
1799     
1800     #include "scsi_module.c"
1801