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