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