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