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