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