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