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