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>
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? :-)
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.
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
20 * 1998-jul-29 - created DPRINTK macros and made it work under
21 * linux 2.1.112, simplified some #defines etc. <pavel@ucw.cz>
23 * Aug 2000 - aeb - deleted seagate_st0x_biosparam(). It would try to
24 * read the physical disk geometry, a bad mistake. Of course it doesnt
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.
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".
38 * st0x=ADDRESS,IRQ (for a Seagate controller)
40 * tmc8xx=ADDRESS,IRQ (for a TMC-8xx or TMC-950 controller)
44 * will configure the driver for a TMC-8xx style controller using IRQ 15
45 * with a base address of 0xC8000.
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.
55 * Will activate debug code.
58 * Will use blind transfers where possible
61 * This will enable parity.
64 * Will use older seagate assembly code. should be (very small amount)
68 * Will allow compatibility with broken devices that don't
69 * handshake fast enough (ie, some CD ROM's) for the Seagate
72 * 50 is some number, It will let you specify a default
73 * transfer rate if handshaking isn't working correctly.
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.
82 * The following to options are patches from the SCSI.HOWTO
84 * -DSWAPSTAT This will swap the definitions for STAT_MSG and STAT_CD.
86 * -DSWAPCNTDATA This will swap the order that seagate.c messes with
87 * the CONTROL an DATA registers.
90 #include <linux/module.h>
93 #include <asm/system.h>
94 #include <linux/spinlock.h>
95 #include <linux/signal.h>
96 #include <linux/sched.h>
97 #include <linux/string.h>
98 #include <linux/proc_fs.h>
99 #include <linux/init.h>
100 #include <linux/delay.h>
101 #include <linux/blk.h>
105 #include "constants.h"
106 #include <linux/stat.h>
107 #include <asm/uaccess.h>
109 #include <scsi/scsi_ioctl.h>
112 #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
114 #define DPRINTK( when, msg... ) do { } while (0)
116 #define DANY( msg... ) DPRINTK( 0xffff, msg );
126 #undef LINKED /* Linked commands are currently broken! */
128 #if defined(OVERRIDE) && !defined(CONTROLLER)
129 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
133 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
134 driver, and Mitsugu Suzuki for information on the ST-01
145 #define CMD_ATTN 0x08
146 #define CMD_START_ARB 0x10
147 #define CMD_EN_PARITY 0x20
148 #define CMD_INTR 0x40
149 #define CMD_DRVR_ENABLE 0x80
155 #define STAT_MSG 0x08
158 #define STAT_MSG 0x02
162 #define STAT_BSY 0x01
164 #define STAT_REQ 0x10
165 #define STAT_SEL 0x20
166 #define STAT_PARITY 0x40
167 #define STAT_ARB_CMPL 0x80
173 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
174 #define REQ_DATAOUT 0
175 #define REQ_DATAIN STAT_IO
176 #define REQ_CMDOUT STAT_CD
177 #define REQ_STATIN (STAT_CD | STAT_IO)
178 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
179 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
181 extern volatile int seagate_st0x_timeout
;
184 #define BASE_CMD CMD_EN_PARITY
193 #define PHASE_BUS_FREE 1
194 #define PHASE_ARBITRATION 2
195 #define PHASE_SELECTION 4
196 #define PHASE_DATAIN 8
197 #define PHASE_DATAOUT 0x10
198 #define PHASE_CMDOUT 0x20
199 #define PHASE_MSGIN 0x40
200 #define PHASE_MSGOUT 0x80
201 #define PHASE_STATUSIN 0x100
202 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
203 #define PRINT_COMMAND 0x200
204 #define PHASE_EXIT 0x400
205 #define PHASE_RESELECT 0x800
206 #define DEBUG_FAST 0x1000
207 #define DEBUG_SG 0x2000
208 #define DEBUG_LINKED 0x4000
209 #define DEBUG_BORKEN 0x8000
212 * Control options - these are timeouts specified in .01 seconds.
216 #define ST0X_BUS_FREE_DELAY 25
217 #define ST0X_SELECTION_DELAY 25
219 #define SEAGATE 1 /* these determine the type of the controller */
222 #define ST0X_ID_STR "Seagate ST-01/ST-02"
223 #define FD_ID_STR "TMC-8XX/TMC-950"
226 static int internal_command (unsigned char target
, unsigned char lun
,
228 void *buff
, int bufflen
, int reselect
);
230 static int incommand
; /* set if arbitration has finished
231 and we are in some command phase. */
233 static unsigned int base_address
= 0; /* Where the card ROM starts, used to
234 calculate memory mapped register
237 static unsigned long st0x_cr_sr
; /* control register write, status
238 register read. 256 bytes in
240 Read is status of SCSI BUS, as per
243 static unsigned long st0x_dr
; /* data register, read write 256
246 static volatile int st0x_aborted
= 0; /* set when we are aborted, ie by a
249 static unsigned char controller_type
= 0; /* set to SEAGATE for ST0x
250 boards or FD for TMC-8xx
252 static int irq
= IRQ
;
254 MODULE_PARM(base_address
, "i");
255 MODULE_PARM(controller_type
, "b");
256 MODULE_PARM(irq
, "i");
258 #define retcode(result) (((result) << 16) | (message << 8) | status)
259 #define STATUS ((u8) isa_readb(st0x_cr_sr))
260 #define DATA ((u8) isa_readb(st0x_dr))
261 #define WRITE_CONTROL(d) { isa_writeb((d), st0x_cr_sr); }
262 #define WRITE_DATA(d) { isa_writeb((d), st0x_dr); }
264 void st0x_setup (char *str
, int *ints
)
266 controller_type
= SEAGATE
;
267 base_address
= ints
[1];
271 void tmc8xx_setup (char *str
, int *ints
)
273 controller_type
= FD
;
274 base_address
= ints
[1];
279 static unsigned int seagate_bases
[] =
281 0xc8000, 0xca000, 0xcc000,
282 0xce000, 0xdc000, 0xde000
287 const unsigned char *signature
;
294 static const Signature __initdata signatures
[] =
296 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE
},
297 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE
},
300 * The following two lines are NOT mistakes. One detects ROM revision
301 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
302 * and this is not going to change, the "SEAGATE" and "SCSI" together
303 * are probably "good enough"
306 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE
},
307 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE
},
310 * However, future domain makes several incompatible SCSI boards, so specific
311 * signatures must be used.
314 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD
},
315 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD
},
316 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD
},
317 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD
},
318 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD
},
319 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD
},
320 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD
},
321 {"FUTURE DOMAIN TMC-950", 5, 21, FD
},
322 /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
323 {"IBM F1 V1.2009/22/93", 5, 25, FD
},
326 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
327 #endif /* n OVERRIDE */
330 * hostno stores the hostnumber, as told to us by the init routine.
333 static int hostno
= -1;
334 static void seagate_reconnect_intr (int, void *, struct pt_regs
*);
335 static void do_seagate_reconnect_intr (int, void *, struct pt_regs
*);
345 * Support for broken devices :
346 * The Seagate board has a handshaking problem. Namely, a lack
347 * thereof for slow devices. You can blast 600K/second through
348 * it if you are polling for each byte, more if you do a blind
349 * transfer. In the first case, with a fast device, REQ will
350 * transition high-low or high-low-high before your loop restarts
351 * and you'll have no problems. In the second case, the board
352 * will insert wait states for up to 13.2 usecs for REQ to
353 * transition low->high, and everything will work.
355 * However, there's nothing in the state machine that says
356 * you *HAVE* to see a high-low-high set of transitions before
357 * sending the next byte, and slow things like the Trantor CD ROMS
358 * will break because of this.
360 * So, we need to slow things down, which isn't as simple as it
361 * seems. We can't slow things down period, because then people
362 * who don't recompile their kernels will shoot me for ruining
363 * their performance. We need to do it on a case per case basis.
365 * The best for performance will be to, only for borken devices
366 * (this is stored on a per-target basis in the scsi_devices array)
368 * Wait for a low->high transition before continuing with that
369 * transfer. If we timeout, continue anyways. We don't need
370 * a long timeout, because REQ should only be asserted until the
371 * corresponding ACK is received and processed.
373 * Note that we can't use the system timer for this, because of
374 * resolution, and we *really* can't use the timer chip since
375 * gettimeofday() and the beeper routines use that. So,
376 * the best thing for us to do will be to calibrate a timing
377 * loop in the initialization code using the timer chip before
378 * gettimeofday() can screw with it.
380 * FIXME: this is broken (not borken :-). Empty loop costs less than
381 * loop with ISA access in it! -- pavel@ucw.cz
384 static int borken_calibration
= 0;
385 static void __init
borken_init (void)
387 register int count
= 0, start
= jiffies
+ 1, stop
= start
+ 25;
389 while (time_before(jiffies
, start
)) ;
390 for (; time_before(jiffies
, stop
); ++count
) ;
393 * Ok, we now have a count for .25 seconds. Convert to a
394 * count per second and divide by transfer rate in K. */
396 borken_calibration
= (count
* 4) / (SLOW_RATE
* 1024);
398 if (borken_calibration
< 1)
399 borken_calibration
= 1;
402 static inline void borken_wait (void)
406 for (count
= borken_calibration
; count
&& (STATUS
& STAT_REQ
); --count
) ;
407 #if (DEBUG & DEBUG_BORKEN)
409 printk ("scsi%d : borken timeout\n", hostno
);
413 #endif /* def SLOW_RATE */
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.
420 #define ULOOP( i ) for (clock = i*8;;)
421 #define TIMEOUT (!(clock--))
423 int __init
seagate_st0x_detect (Scsi_Host_Template
* tpnt
)
425 struct Scsi_Host
*instance
;
428 tpnt
->proc_name
= "seagate";
430 * First, we try for the manual override. */
431 DANY ("Autodetecting ST0x / TMC-8xx\n");
434 printk (KERN_ERR
"seagate_st0x_detect() called twice?!\n");
438 /* If the user specified the controller type from the command line,
439 controller_type will be non-zero, so don't try to detect one */
441 if (!controller_type
)
444 base_address
= OVERRIDE
;
445 controller_type
= CONTROLLER
;
447 DANY("Base address overridden to %x, controller type is %s\n",
448 base_address
, controller_type
== SEAGATE
? "SEAGATE" : "FD");
451 * To detect this card, we simply look for the signature
452 * from the BIOS version notice in all the possible locations
453 * of the ROM's. This has a nice side effect of not trashing
454 * any register locations that might be used by something else.
456 * XXX - note that we probably should be probing the address
457 * space for the on-board RAM instead.
460 for (i
= 0; i
< (sizeof (seagate_bases
) / sizeof (unsigned int)); ++i
)
462 for (j
= 0; !base_address
&& j
< NUM_SIGNATURES
; ++j
)
463 if (isa_check_signature (seagate_bases
[i
] + signatures
[j
].offset
,
464 signatures
[j
].signature
, signatures
[j
].length
))
466 base_address
= seagate_bases
[i
];
467 controller_type
= signatures
[j
].type
;
469 #endif /* OVERRIDE */
470 } /* (! controller_type) */
472 tpnt
->this_id
= (controller_type
== SEAGATE
) ? 7 : 6;
473 tpnt
->name
= (controller_type
== SEAGATE
) ? ST0X_ID_STR
: FD_ID_STR
;
476 DANY ("ST0x / TMC-8xx not detected.\n");
480 st0x_cr_sr
= base_address
+ (controller_type
== SEAGATE
? 0x1a00 : 0x1c00);
481 st0x_dr
= st0x_cr_sr
+ 0x200;
483 DANY ("%s detected. Base address = %x, cr = %x, dr = %x\n",
484 tpnt
->name
, base_address
, st0x_cr_sr
, st0x_dr
);
487 * At all times, we will use IRQ 5. Should also check for IRQ3 if we
488 * loose our first interrupt.
490 instance
= scsi_register (tpnt
, 0);
494 hostno
= instance
->host_no
;
495 if (request_irq (irq
, do_seagate_reconnect_intr
, SA_INTERRUPT
,
496 (controller_type
== SEAGATE
) ? "seagate" : "tmc-8xx", NULL
)) {
497 printk ("scsi%d : unable to allocate IRQ%d\n", hostno
, irq
);
501 instance
->io_port
= base_address
;
503 printk( "Calibrating borken timer... " );
505 printk( " %d cycles per transfer\n", borken_calibration
);
508 printk( "This is one second... " );
511 ULOOP( 1*1000*1000 ) {
517 printk ("done, %s options:"
536 #ifdef SEAGATE_USE_ASM
552 const char *seagate_st0x_info (struct Scsi_Host
*shpnt
)
554 static char buffer
[64];
556 sprintf (buffer
, "%s at irq %d, address 0x%05X",
557 (controller_type
== SEAGATE
) ? ST0X_ID_STR
: FD_ID_STR
,
563 * These are our saved pointers for the outstanding command that is
564 * waiting for a reconnect
567 static unsigned char current_target
, current_lun
;
568 static unsigned char *current_cmnd
, *current_data
;
569 static int current_nobuffs
;
570 static struct scatterlist
*current_buffer
;
571 static int current_bufflen
;
575 * linked_connected indicates whether or not we are currently connected to
576 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
577 * using linked commands.
580 static int linked_connected
= 0;
581 static unsigned char linked_target
, linked_lun
;
584 static void (*done_fn
) (Scsi_Cmnd
*) = NULL
;
585 static Scsi_Cmnd
*SCint
= NULL
;
588 * These control whether or not disconnect / reconnect will be attempted,
589 * or are being attempted.
592 #define NO_RECONNECT 0
593 #define RECONNECT_NOW 1
594 #define CAN_RECONNECT 2
597 * LINKED_RIGHT indicates that we are currently connected to the correct target
598 * for this command, LINKED_WRONG indicates that we are connected to the wrong
599 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
602 #define LINKED_RIGHT 3
603 #define LINKED_WRONG 4
606 * This determines if we are expecting to reconnect or not.
609 static int should_reconnect
= 0;
612 * The seagate_reconnect_intr routine is called when a target reselects the
613 * host adapter. This occurs on the interrupt triggered by the target
617 static void do_seagate_reconnect_intr (int irq
, void *dev_id
, struct pt_regs
*regs
)
621 spin_lock_irqsave(&io_request_lock
, flags
);
622 seagate_reconnect_intr(irq
, dev_id
, regs
);
623 spin_unlock_irqrestore(&io_request_lock
, flags
);
626 static void seagate_reconnect_intr (int irq
, void *dev_id
, struct pt_regs
*regs
)
631 DPRINTK (PHASE_RESELECT
, "scsi%d : seagate_reconnect_intr() called\n", hostno
);
633 if (!should_reconnect
)
634 printk ("scsi%d: unexpected interrupt.\n", hostno
);
637 should_reconnect
= 0;
639 DPRINTK (PHASE_RESELECT
, "scsi%d : internal_command("
640 "%d, %08x, %08x, RECONNECT_NOW\n", hostno
,
641 current_target
, current_data
, current_bufflen
);
643 temp
= internal_command (current_target
, current_lun
, current_cmnd
,
644 current_data
, current_bufflen
, RECONNECT_NOW
);
646 if (msg_byte (temp
) != DISCONNECT
)
650 DPRINTK (PHASE_RESELECT
, "scsi%d : done_fn(%d,%08x)", hostno
,
653 panic ("SCint == NULL in seagate");
656 SCtmp
->result
= temp
;
660 printk ("done_fn() not defined.\n");
666 * The seagate_st0x_queue_command() function provides a queued interface
667 * to the seagate SCSI driver. Basically, it just passes control onto the
668 * seagate_command() function, after fixing it so that the done_fn()
669 * is set to the one passed to the function. We have to be very careful,
670 * because there are some commands on some devices that do not disconnect,
671 * and if we simply call the done_fn when the command is done then another
672 * command is started and queue_command is called again... We end up
673 * overflowing the kernel stack, and this tends not to be such a good idea.
676 static int recursion_depth
= 0;
678 int seagate_st0x_queue_command (Scsi_Cmnd
* SCpnt
, void (*done
) (Scsi_Cmnd
*))
680 int result
, reconnect
;
683 DANY( "seagate: que_command" );
685 current_target
= SCpnt
->target
;
686 current_lun
= SCpnt
->lun
;
687 (const void *) current_cmnd
= SCpnt
->cmnd
;
688 current_data
= (unsigned char *) SCpnt
->request_buffer
;
689 current_bufflen
= SCpnt
->request_bufflen
;
691 if (recursion_depth
) return 0;
697 * Set linked command bit in control field of SCSI command.
700 current_cmnd
[SCpnt
->cmd_len
] |= 0x01;
701 if (linked_connected
)
703 DPRINTK (DEBUG_LINKED
,
704 "scsi%d : using linked commands, current I_T_L nexus is ", hostno
);
705 if ((linked_target
== current_target
) && (linked_lun
== current_lun
))
707 DPRINTK (DEBUG_LINKED
, "correct\n");
708 reconnect
= LINKED_RIGHT
;
712 DPRINTK (DEBUG_LINKED
, "incorrect\n");
713 reconnect
= LINKED_WRONG
;
718 reconnect
= CAN_RECONNECT
;
720 result
= internal_command (SCint
->target
, SCint
->lun
, SCint
->cmnd
,
721 SCint
->request_buffer
, SCint
->request_bufflen
, reconnect
);
722 if (msg_byte (result
) == DISCONNECT
) break;
725 SCtmp
->result
= result
;
733 int seagate_st0x_command (Scsi_Cmnd
* SCpnt
)
735 return internal_command (SCpnt
->target
, SCpnt
->lun
, SCpnt
->cmnd
,
736 SCpnt
->request_buffer
, SCpnt
->request_bufflen
,
740 static int internal_command (unsigned char target
, unsigned char lun
,
741 const void *cmnd
, void *buff
, int bufflen
, int reselect
)
743 unsigned char *data
= NULL
;
744 struct scatterlist
*buffer
= NULL
;
745 int clock
, temp
, nobuffs
= 0, done
= 0, len
= 0;
749 int transfered
= 0, phase
= 0, newphase
;
752 register unsigned char status_read
;
753 unsigned char tmp_data
, tmp_control
, status
= 0, message
= 0;
755 unsigned transfersize
= 0, underflow
= 0;
758 int borken
= (int) SCint
->device
->borken
; /* Does the current target require
765 #if (DEBUG & PRINT_COMMAND)
766 printk ("scsi%d : target = %d, command = ", hostno
, target
);
767 print_command ((unsigned char *) cmnd
);
770 #if (DEBUG & PHASE_RESELECT)
774 printk ("scsi%d : reconnecting\n", hostno
);
778 printk ("scsi%d : connected, can reconnect\n", hostno
);
781 printk ("scsi%d : connected to wrong target, can reconnect\n", hostno
);
785 printk ("scsi%d : allowed to reconnect\n", hostno
);
788 printk ("scsi%d : not allowed to reconnect\n", hostno
);
792 if (target
== (controller_type
== SEAGATE
? 7 : 6))
793 return DID_BAD_TARGET
;
796 * We work it differently depending on if this is is "the first time,"
797 * or a reconnect. If this is a reselect phase, then SEL will
798 * be asserted, and we must skip selection / arbitration phases.
804 DPRINTK ( PHASE_RESELECT
, "scsi%d : phase RESELECT \n", hostno
);
807 * At this point, we should find the logical or of our ID and the original
808 * target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
810 * After ARBITRATION phase is completed, only SEL, BSY, and the
811 * target ID are asserted. A valid initiator ID is not on the bus
812 * until IO is asserted, so we must wait for that.
816 if ((temp
& STAT_IO
) && !(temp
& STAT_BSY
))
820 DPRINTK (PHASE_RESELECT
,
821 "scsi%d : RESELECT timed out while waiting for IO .\n", hostno
);
822 return (DID_BAD_INTR
<< 16);
827 * After I/O is asserted by the target, we can read our ID and its
831 if (!((temp
= DATA
) & (controller_type
== SEAGATE
? 0x80 : 0x40)))
833 DPRINTK (PHASE_RESELECT
,
834 "scsi%d : detected reconnect request to different target.\n"
835 "\tData bus = %d\n", hostno
, temp
);
836 return (DID_BAD_INTR
<< 16);
839 if (!(temp
& (1 << current_target
)))
841 printk ("scsi%d : Unexpected reselect interrupt. Data bus = %d\n",
843 return (DID_BAD_INTR
<< 16);
846 buffer
= current_buffer
;
847 cmnd
= current_cmnd
; /* WDE add */
848 data
= current_data
; /* WDE add */
849 len
= current_bufflen
; /* WDE add */
850 nobuffs
= current_nobuffs
;
853 * We have determined that we have been selected. At this point,
854 * we must respond to the reselection by asserting BSY ourselves
858 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
| CMD_BSY
);
860 WRITE_CONTROL (BASE_CMD
| CMD_BSY
);
864 * The target will drop SEL, and raise BSY, at which time we must drop
869 if (!(STATUS
& STAT_SEL
)) break;
871 WRITE_CONTROL (BASE_CMD
| CMD_INTR
);
872 DPRINTK (PHASE_RESELECT
,
873 "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno
);
874 return (DID_BAD_INTR
<< 16);
878 WRITE_CONTROL (BASE_CMD
);
881 * At this point, we have connected with the target and can get
889 * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
890 * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
891 * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT
892 * message on MESSAGE OUT phase, and then loop back to here.
899 DPRINTK (PHASE_BUS_FREE
, "scsi%d : phase = BUS FREE \n", hostno
);
904 * On entry, we make sure that the BUS is in a BUS FREE
905 * phase, by insuring that both BSY and SEL are low for
906 * at least one bus settle delay. Several reads help
907 * eliminate wire glitch.
911 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
912 clock
= jiffies
+ ST0X_BUS_FREE_DELAY
;
914 while (((STATUS
| STATUS
| STATUS
) &
915 (STAT_BSY
| STAT_SEL
)) &&
916 (!st0x_aborted
) && time_before(jiffies
, clock
));
918 if (time_after(jiffies
, clock
))
919 return retcode (DID_BUS_BUSY
);
920 else if (st0x_aborted
)
921 return retcode (st0x_aborted
);
924 DPRINTK (PHASE_SELECTION
, "scsi%d : phase = SELECTION\n", hostno
);
926 clock
= jiffies
+ ST0X_SELECTION_DELAY
;
929 * Arbitration/selection procedure :
931 * 2. Write HOST adapter address bit
932 * 3. Set start arbitration.
933 * 4. We get either ARBITRATION COMPLETE or SELECT at this
935 * 5. OR our ID and targets on bus.
936 * 6. Enable SCSI drivers and asserted SEL and ATTN
943 WRITE_DATA ((controller_type
== SEAGATE
) ? 0x80 : 0x40);
944 WRITE_CONTROL (CMD_START_ARB
);
945 restore_flags (flags
);
947 ULOOP( ST0X_SELECTION_DELAY
* 10000 ) {
948 status_read
= STATUS
;
949 if (status_read
& STAT_ARB_CMPL
) break;
950 if (st0x_aborted
) /* FIXME: What? We are going to do something even after abort? */
952 if (TIMEOUT
|| (status_read
& STAT_SEL
)) {
953 printk( "scsi%d : arbitration lost or timeout.\n", hostno
);
954 WRITE_CONTROL (BASE_CMD
);
955 return retcode (DID_NO_CONNECT
);
959 DPRINTK (PHASE_SELECTION
, "scsi%d : arbitration complete\n", hostno
);
963 * When the SCSI device decides that we're gawking at it, it will
964 * respond by asserting BUSY on the bus.
966 * Note : the Seagate ST-01/02 product manual says that we should
967 * twiddle the DATA register before the control register. However,
968 * this does not work reliably so we do it the other way around.
970 * Probably could be a problem with arbitration too, we really should
971 * try this with a SCSI protocol or logic analyzer to see what is
974 tmp_data
= (unsigned char) ((1 << target
) | (controller_type
== SEAGATE
? 0x80 : 0x40));
975 tmp_control
= BASE_CMD
| CMD_DRVR_ENABLE
| CMD_SEL
| (reselect
? CMD_ATTN
: 0);
979 #ifdef OLDCNTDATASCEME
981 WRITE_CONTROL (tmp_control
);
982 WRITE_DATA (tmp_data
);
984 WRITE_DATA (tmp_data
);
985 WRITE_CONTROL (tmp_control
);
988 tmp_control
^= CMD_BSY
; /* This is guesswork. What used to be in driver */
989 WRITE_CONTROL (tmp_control
); /* could never work: it sent data into control */
990 WRITE_DATA (tmp_data
); /* register and control info into data. Hopefully */
991 tmp_control
^= CMD_BSY
; /* fixed, but order of first two may be wrong. */
992 WRITE_CONTROL (tmp_control
); /* -- pavel@ucw.cz */
996 restore_flags (flags
);
1001 * If we have been aborted, and we have a command in progress, IE the
1002 * target still has BSY asserted, then we will reset the bus, and
1003 * notify the midlevel driver to expect sense.
1006 WRITE_CONTROL (BASE_CMD
);
1007 if (STATUS
& STAT_BSY
) {
1008 printk ("scsi%d : BST asserted after we've been aborted.\n", hostno
);
1009 seagate_st0x_reset (NULL
, 0);
1010 return retcode (DID_RESET
);
1012 return retcode (st0x_aborted
);
1014 if (STATUS
& STAT_BSY
) break;
1016 DPRINTK (PHASE_SELECTION
, "scsi%d : NO CONNECT with target %d, stat = %x \n",
1017 hostno
, target
, STATUS
);
1018 return retcode (DID_NO_CONNECT
);
1022 /* Establish current pointers. Take into account scatter / gather */
1024 if ((nobuffs
= SCint
->use_sg
))
1026 #if (DEBUG & DEBUG_SG)
1030 printk ("scsi%d : scatter gather requested, using %d buffers.\n",
1032 for (i
= 0; i
< nobuffs
; ++i
)
1033 printk ("scsi%d : buffer %d address = %08x length = %d\n",
1034 hostno
, i
, buffer
[i
].address
, buffer
[i
].length
);
1038 buffer
= (struct scatterlist
*) SCint
->buffer
;
1039 len
= buffer
->length
;
1040 data
= (unsigned char *) buffer
->address
;
1044 DPRINTK (DEBUG_SG
, "scsi%d : scatter gather not requested.\n", hostno
);
1046 len
= SCint
->request_bufflen
;
1047 data
= (unsigned char *) SCint
->request_buffer
;
1050 DPRINTK (PHASE_DATAIN
| PHASE_DATAOUT
, "scsi%d : len = %d\n", hostno
, len
);
1059 } /* end of switch(reselect) */
1062 * There are several conditions under which we wish to send a message :
1063 * 1. When we are allowing disconnect / reconnect, and need to establish
1064 * the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
1066 * 2. When we are doing linked commands, are have the wrong I_T_L nexus
1067 * established and want to send an ABORT message.
1070 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1072 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
|
1073 (((reselect
== CAN_RECONNECT
)
1074 || (reselect
== LINKED_WRONG
)
1077 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
|
1078 (((reselect
== CAN_RECONNECT
)
1083 * INFORMATION TRANSFER PHASE
1085 * The nasty looking read / write inline assembler loops we use for
1086 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1087 * the 'C' versions - since we're moving 1024 bytes of data, this
1090 * SJT: The nasty-looking assembler is gone, so it's slower.
1094 DPRINTK (PHASE_ETC
, "scsi%d : phase = INFORMATION TRANSFER\n", hostno
);
1097 transfersize
= SCint
->transfersize
;
1098 underflow
= SCint
->underflow
;
1101 * Now, we poll the device for status information,
1102 * and handle any requests it makes. Note that since we are unsure of
1103 * how much data will be flowing across the system, etc and cannot
1104 * make reasonable timeouts, that we will instead have the midlevel
1105 * driver handle any timeouts that occur in this phase.
1108 while (((status_read
= STATUS
) & STAT_BSY
) && !st0x_aborted
&& !done
)
1111 if (status_read
& STAT_PARITY
)
1113 printk ("scsi%d : got parity error\n", hostno
);
1114 st0x_aborted
= DID_PARITY
;
1118 if (status_read
& STAT_REQ
)
1120 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1121 if ((newphase
= (status_read
& REQ_MASK
)) != phase
)
1127 printk ("scsi%d : phase = DATA OUT\n", hostno
);
1130 printk ("scsi%d : phase = DATA IN\n", hostno
);
1133 printk ("scsi%d : phase = COMMAND OUT\n", hostno
);
1136 printk ("scsi%d : phase = STATUS IN\n", hostno
);
1139 printk ("scsi%d : phase = MESSAGE OUT\n", hostno
);
1142 printk ("scsi%d : phase = MESSAGE IN\n", hostno
);
1145 printk ("scsi%d : phase = UNKNOWN\n", hostno
);
1146 st0x_aborted
= DID_ERROR
;
1150 switch (status_read
& REQ_MASK
)
1154 * If we are in fast mode, then we simply splat the data out
1155 * in word-sized chunks as fast as we can.
1161 printk ("scsi%d: underflow to target %d lun %d \n", hostno
, target
, lun
);
1162 st0x_aborted
= DID_ERROR
;
1168 if (fast
&& transfersize
&& !(len
% transfersize
)
1169 && (len
>= transfersize
)
1171 && !(transfersize
% 4)
1175 DPRINTK (DEBUG_FAST
,
1176 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1177 " len = %d, data = %08x\n",
1178 hostno
, SCint
->underflow
, SCint
->transfersize
, len
, data
);
1180 /* SJT: Start. Fast Write */
1181 #ifdef SEAGATE_USE_ASM
1188 "movl %%eax, (%%edi)\n\t"
1192 "movb %%al, (%%edi)\n\t"
1196 /* input */ : "D" (phys_to_virt(st0x_dr
)), "S" (data
), "c" (SCint
->transfersize
)
1197 /* clobbered */ : "eax", "ecx", "esi" );
1198 #else /* SEAGATE_USE_ASM */
1201 unsigned int *iop
= phys_to_virt (st0x_dr
);
1202 const unsigned int *dp
= (unsigned int *) data
;
1203 int xferlen
= transfersize
>> 2;
1205 unsigned char *iop
= phys_to_virt (st0x_dr
);
1206 const unsigned char *dp
= data
;
1207 int xferlen
= transfersize
;
1209 for (; xferlen
; --xferlen
)
1212 #endif /* SEAGATE_USE_ASM */
1214 len
-= transfersize
;
1215 data
+= transfersize
;
1216 DPRINTK (DEBUG_FAST
,
1217 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1223 * We loop as long as we are in a data out phase, there is data to send,
1224 * and BSY is still active.
1227 /* SJT: Start. Slow Write. */
1228 #ifdef SEAGATE_USE_ASM
1230 int __dummy_1
,__dummy_2
;
1233 * We loop as long as we are in a data out phase, there is data to send,
1234 * and BSY is still active.
1236 /* Local variables : len = ecx , data = esi,
1237 st0x_cr_sr = ebx, st0x_dr = edi
1240 /* Test for any data here at all. */
1241 "orl %%ecx, %%ecx\n\t"
1244 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%ebx\n\t" */
1245 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%edi\n\t" */
1247 "movb (%%ebx), %%al\n\t"
1251 /* Test for data out phase - STATUS & REQ_MASK should be
1252 REQ_DATAOUT, which is 0. */
1253 "test $0xe, %%al\n\t"
1256 "test $0x10, %%al\n\t"
1259 "movb %%al, (%%edi)\n\t"
1262 /* output */ : "=S" (data
), "=c" (len
) ,"=b" (__dummy_1
) ,"=D" (__dummy_2
)
1263 /* input */ : "0" (data
), "1" (len
), "2" (phys_to_virt(st0x_cr_sr
)), "3" (phys_to_virt(st0x_dr
))
1264 /* clobbered */ : "eax");
1265 #else /* SEAGATE_USE_ASM */
1271 if (!(stat
& STAT_BSY
) || ((stat
& REQ_MASK
) != REQ_DATAOUT
))
1273 if (stat
& STAT_REQ
)
1275 WRITE_DATA (*data
++);
1279 #endif /* SEAGATE_USE_ASM */
1283 if (!len
&& nobuffs
)
1287 len
= buffer
->length
;
1288 data
= (unsigned char *) buffer
->address
;
1290 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1299 #if (DEBUG & (PHASE_DATAIN))
1303 len
&& (STATUS
& (REQ_MASK
| STAT_REQ
)) == (REQ_DATAIN
|
1310 #if (DEBUG & (PHASE_DATAIN))
1317 if (fast
&& transfersize
&& !(len
% transfersize
) &&
1318 (len
>= transfersize
)
1320 && !(transfersize
% 4)
1324 DPRINTK (DEBUG_FAST
,
1325 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1326 " len = %d, data = %08x\n",
1327 hostno
, SCint
->underflow
, SCint
->transfersize
, len
, data
);
1329 /* SJT: Start. Fast Read */
1330 #ifdef SEAGATE_USE_ASM
1336 "movl (%%esi), %%eax\n\t"
1340 "movb (%%esi), %%al\n\t"
1345 /* input */ : "S" (phys_to_virt(st0x_dr
)), "D" (data
), "c" (SCint
->transfersize
)
1346 /* clobbered */ : "eax", "ecx", "edi");
1347 #else /* SEAGATE_USE_ASM */
1350 const unsigned int *iop
= phys_to_virt (st0x_dr
);
1351 unsigned int *dp
= (unsigned int *) data
;
1352 int xferlen
= len
>> 2;
1354 const unsigned char *iop
= phys_to_virt (st0x_dr
);
1355 unsigned char *dp
= data
;
1358 for (; xferlen
; --xferlen
)
1361 #endif /* SEAGATE_USE_ASM */
1363 len
-= transfersize
;
1364 data
+= transfersize
;
1365 #if (DEBUG & PHASE_DATAIN)
1366 printk ("scsi%d: transfered += %d\n", hostno
, transfersize
);
1367 transfered
+= transfersize
;
1370 DPRINTK (DEBUG_FAST
,
1371 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1377 #if (DEBUG & PHASE_DATAIN)
1378 printk ("scsi%d: transfered += %d\n", hostno
, len
);
1379 transfered
+= len
; /* Assume we'll transfer it all, then
1380 subtract what we *didn't* transfer */
1384 * We loop as long as we are in a data in phase, there is room to read,
1385 * and BSY is still active
1389 #ifdef SEAGATE_USE_ASM
1391 int __dummy_3
,__dummy_4
;
1393 /* Dummy clobbering variables for the new gcc-2.95 */
1396 * We loop as long as we are in a data in phase, there is room to read,
1397 * and BSY is still active
1399 /* Local variables : ecx = len, edi = data
1400 esi = st0x_cr_sr, ebx = st0x_dr */
1402 /* Test for room to read */
1403 "orl %%ecx, %%ecx\n\t"
1406 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%esi\n\t" */
1407 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%ebx\n\t" */
1409 "movb (%%esi), %%al\n\t"
1413 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1414 = STAT_IO, which is 4. */
1415 "movb $0xe, %%ah\n\t"
1416 "andb %%al, %%ah\n\t"
1417 "cmpb $0x04, %%ah\n\t"
1420 "test $0x10, %%al\n\t"
1422 "movb (%%ebx), %%al\n\t"
1426 /* output */ : "=D" (data
), "=c" (len
) ,"=S" (__dummy_3
) ,"=b" (__dummy_4
)
1427 /* input */ : "0" (data
), "1" (len
), "2" (phys_to_virt(st0x_cr_sr
)), "3" (phys_to_virt(st0x_dr
))
1428 /* clobbered */ : "eax" );
1429 #else /* SEAGATE_USE_ASM */
1435 if (!(stat
& STAT_BSY
) || ((stat
& REQ_MASK
) != REQ_DATAIN
))
1437 if (stat
& STAT_REQ
)
1443 #endif /* SEAGATE_USE_ASM */
1445 #if (DEBUG & PHASE_DATAIN)
1446 printk ("scsi%d: transfered -= %d\n", hostno
, len
);
1447 transfered
-= len
; /* Since we assumed all of Len got *
1448 transfered, correct our mistake */
1452 if (!len
&& nobuffs
)
1456 len
= buffer
->length
;
1457 data
= (unsigned char *) buffer
->address
;
1459 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1466 while (((status_read
= STATUS
) & STAT_BSY
) &&
1467 ((status_read
& REQ_MASK
) == REQ_CMDOUT
))
1468 if (status_read
& STAT_REQ
)
1470 WRITE_DATA (*(const unsigned char *) cmnd
);
1471 cmnd
= 1 + (const unsigned char *) cmnd
;
1485 * We can only have sent a MSG OUT if we requested to do this
1486 * by raising ATTN. So, we must drop ATTN.
1489 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
);
1491 * If we are reconnecting, then we must send an IDENTIFY message in
1492 * response to MSGOUT.
1497 WRITE_DATA (IDENTIFY (1, lun
));
1499 DPRINTK (PHASE_RESELECT
| PHASE_MSGOUT
, "scsi%d : sent IDENTIFY message.\n", hostno
);
1504 linked_connected
= 0;
1505 reselect
= CAN_RECONNECT
;
1507 DPRINTK (PHASE_MSGOUT
| DEBUG_LINKED
,
1508 "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno
);
1510 DPRINTK (DEBUG_LINKED
, "correct\n");
1513 printk ("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno
, target
);
1518 switch (message
= DATA
)
1521 DANY ("seagate: deciding to disconnect\n");
1522 should_reconnect
= 1;
1523 current_data
= data
; /* WDE add */
1524 current_buffer
= buffer
;
1525 current_bufflen
= len
; /* WDE add */
1526 current_nobuffs
= nobuffs
;
1528 linked_connected
= 0;
1531 DPRINTK ((PHASE_RESELECT
| PHASE_MSGIN
), "scsi%d : disconnected.\n", hostno
);
1535 case LINKED_CMD_COMPLETE
:
1536 case LINKED_FLG_CMD_COMPLETE
:
1538 case COMMAND_COMPLETE
:
1540 * Note : we should check for underflow here.
1542 DPRINTK (PHASE_MSGIN
, "scsi%d : command complete.\n", hostno
);
1546 DPRINTK (PHASE_MSGIN
, "scsi%d : abort message.\n", hostno
);
1550 current_buffer
= buffer
;
1551 current_bufflen
= len
; /* WDE add */
1552 current_data
= data
; /* WDE mod */
1553 current_nobuffs
= nobuffs
;
1554 DPRINTK (PHASE_MSGIN
, "scsi%d : pointers saved.\n", hostno
);
1556 case RESTORE_POINTERS
:
1557 buffer
= current_buffer
;
1558 cmnd
= current_cmnd
;
1559 data
= current_data
; /* WDE mod */
1560 len
= current_bufflen
;
1561 nobuffs
= current_nobuffs
;
1562 DPRINTK (PHASE_MSGIN
, "scsi%d : pointers restored.\n", hostno
);
1567 * IDENTIFY distinguishes itself from the other messages by setting the
1568 * high byte. [FIXME: should not this read "the high bit"? - pavel@ucw.cz]
1570 * Note : we need to handle at least one outstanding command per LUN,
1571 * and need to hash the SCSI command for that I_T_L nexus based on the
1572 * known ID (at this point) and LUN.
1577 DPRINTK (PHASE_MSGIN
, "scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1578 hostno
, target
, message
& 7);
1584 * We should go into a MESSAGE OUT phase, and send a MESSAGE_REJECT
1585 * if we run into a message that we don't like. The seagate driver
1586 * needs some serious restructuring first though.
1589 DPRINTK (PHASE_MSGIN
,
1590 "scsi%d : unknown message %d from target %d.\n", hostno
, message
, target
);
1596 printk ("scsi%d : unknown phase.\n", hostno
);
1597 st0x_aborted
= DID_ERROR
;
1598 } /* end of switch (status_read &
1603 * I really don't care to deal with borken devices in each single
1604 * byte transfer case (ie, message in, message out, status), so
1605 * I'll do the wait here if necessary.
1611 } /* if(status_read & STAT_REQ) ends */
1612 } /* while(((status_read = STATUS)...)
1615 DPRINTK (PHASE_DATAIN
| PHASE_DATAOUT
| PHASE_EXIT
,
1616 "scsi%d : Transfered %d bytes\n", hostno
, transfered
);
1618 #if (DEBUG & PHASE_EXIT)
1619 #if 0 /* Doesn't work for scatter/gather */
1620 printk ("Buffer : \n");
1621 for (i
= 0; i
< 20; ++i
)
1622 printk ("%02x ", ((unsigned char *) data
)[i
]); /* WDE mod */
1625 printk ("scsi%d : status = ", hostno
);
1626 print_status (status
);
1627 printk ("message = %02x\n", message
);
1630 /* We shouldn't reach this until *after* BSY has been deasserted */
1636 * Fix the message byte so that unsuspecting high level drivers don't
1637 * puke when they see a LINKED COMMAND message in place of the COMMAND
1638 * COMPLETE they may be expecting. Shouldn't be necessary, but it's
1639 * better to be on the safe side.
1641 * A non LINKED* message byte will indicate that the command completed,
1642 * and we are now disconnected.
1647 case LINKED_CMD_COMPLETE
:
1648 case LINKED_FLG_CMD_COMPLETE
:
1649 message
= COMMAND_COMPLETE
;
1650 linked_target
= current_target
;
1651 linked_lun
= current_lun
;
1652 linked_connected
= 1;
1653 DPRINTK (DEBUG_LINKED
, "scsi%d : keeping I_T_L nexus established"
1654 "for linked command.\n", hostno
);
1655 /* We also will need to adjust status to accommodate intermediate
1657 if ((status
== INTERMEDIATE_GOOD
) ||
1658 (status
== INTERMEDIATE_C_GOOD
))
1663 * We should also handle what are "normal" termination messages
1664 * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually,
1665 * and flake if things aren't right.
1668 DPRINTK (DEBUG_LINKED
, "scsi%d : closing I_T_L nexus.\n", hostno
);
1669 linked_connected
= 0;
1674 if (should_reconnect
)
1676 DPRINTK (PHASE_RESELECT
, "scsi%d : exiting seagate_st0x_queue_command()"
1677 "with reconnect enabled.\n", hostno
);
1678 WRITE_CONTROL (BASE_CMD
| CMD_INTR
);
1681 WRITE_CONTROL (BASE_CMD
);
1683 return retcode (st0x_aborted
);
1684 } /* end of internal_command */
1686 int seagate_st0x_abort (Scsi_Cmnd
* SCpnt
)
1688 st0x_aborted
= DID_ABORT
;
1689 return SCSI_ABORT_PENDING
;
1695 * the seagate_st0x_reset function resets the SCSI bus
1698 int seagate_st0x_reset (Scsi_Cmnd
* SCpnt
, unsigned int reset_flags
)
1700 /* No timeouts - this command is going to fail because it was reset. */
1701 DANY ("scsi%d: Reseting bus... ", hostno
);
1703 /* assert RESET signal on SCSI bus. */
1704 WRITE_CONTROL (BASE_CMD
| CMD_RST
);
1708 WRITE_CONTROL (BASE_CMD
);
1709 st0x_aborted
= DID_RESET
;
1712 return SCSI_RESET_WAKEUP
;
1716 /* Eventually this will go into an include file, but this will be later */
1717 static Scsi_Host_Template driver_template
= SEAGATE_ST0X
;
1719 #include "scsi_module.c"