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 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.
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>
91 #include <linux/interrupt.h>
92 #include <linux/spinlock.h>
93 #include <linux/signal.h>
94 #include <linux/string.h>
95 #include <linux/proc_fs.h>
96 #include <linux/init.h>
97 #include <linux/delay.h>
98 #include <linux/blkdev.h>
99 #include <linux/stat.h>
102 #include <asm/system.h>
103 #include <asm/uaccess.h>
106 #include <scsi/scsi_host.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 #undef SEAGATE_USE_ASM
137 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
138 driver, and Mitsugu Suzuki for information on the ST-01
149 #define CMD_ATTN 0x08
150 #define CMD_START_ARB 0x10
151 #define CMD_EN_PARITY 0x20
152 #define CMD_INTR 0x40
153 #define CMD_DRVR_ENABLE 0x80
159 #define STAT_MSG 0x08
162 #define STAT_MSG 0x02
166 #define STAT_BSY 0x01
168 #define STAT_REQ 0x10
169 #define STAT_SEL 0x20
170 #define STAT_PARITY 0x40
171 #define STAT_ARB_CMPL 0x80
177 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
178 #define REQ_DATAOUT 0
179 #define REQ_DATAIN STAT_IO
180 #define REQ_CMDOUT STAT_CD
181 #define REQ_STATIN (STAT_CD | STAT_IO)
182 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
183 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
185 extern volatile int seagate_st0x_timeout
;
188 #define BASE_CMD CMD_EN_PARITY
197 #define PHASE_BUS_FREE 1
198 #define PHASE_ARBITRATION 2
199 #define PHASE_SELECTION 4
200 #define PHASE_DATAIN 8
201 #define PHASE_DATAOUT 0x10
202 #define PHASE_CMDOUT 0x20
203 #define PHASE_MSGIN 0x40
204 #define PHASE_MSGOUT 0x80
205 #define PHASE_STATUSIN 0x100
206 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
207 #define PRINT_COMMAND 0x200
208 #define PHASE_EXIT 0x400
209 #define PHASE_RESELECT 0x800
210 #define DEBUG_FAST 0x1000
211 #define DEBUG_SG 0x2000
212 #define DEBUG_LINKED 0x4000
213 #define DEBUG_BORKEN 0x8000
216 * Control options - these are timeouts specified in .01 seconds.
220 #define ST0X_BUS_FREE_DELAY 25
221 #define ST0X_SELECTION_DELAY 25
223 #define SEAGATE 1 /* these determine the type of the controller */
226 #define ST0X_ID_STR "Seagate ST-01/ST-02"
227 #define FD_ID_STR "TMC-8XX/TMC-950"
229 static int internal_command (unsigned char target
, unsigned char lun
,
231 void *buff
, int bufflen
, int reselect
);
233 static int incommand
; /* set if arbitration has finished
234 and we are in some command phase. */
236 static unsigned int base_address
= 0; /* Where the card ROM starts, used to
237 calculate memory mapped register
240 static void __iomem
*st0x_cr_sr
; /* control register write, status
241 register read. 256 bytes in
243 Read is status of SCSI BUS, as per
246 static void __iomem
*st0x_dr
; /* data register, read write 256
249 static volatile int st0x_aborted
= 0; /* set when we are aborted, ie by a
252 static unsigned char controller_type
= 0; /* set to SEAGATE for ST0x
253 boards or FD for TMC-8xx
255 static int irq
= IRQ
;
257 module_param(base_address
, uint
, 0);
258 module_param(controller_type
, byte
, 0);
259 module_param(irq
, int, 0);
260 MODULE_LICENSE("GPL");
263 #define retcode(result) (((result) << 16) | (message << 8) | status)
264 #define STATUS ((u8) readb(st0x_cr_sr))
265 #define DATA ((u8) readb(st0x_dr))
266 #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
267 #define WRITE_DATA(d) { writeb((d), st0x_dr); }
270 static unsigned int seagate_bases
[] = {
271 0xc8000, 0xca000, 0xcc000,
272 0xce000, 0xdc000, 0xde000
276 const unsigned char *signature
;
282 static Signature __initdata signatures
[] = {
283 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE
},
284 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE
},
287 * The following two lines are NOT mistakes. One detects ROM revision
288 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
289 * and this is not going to change, the "SEAGATE" and "SCSI" together
290 * are probably "good enough"
293 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE
},
294 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE
},
297 * However, future domain makes several incompatible SCSI boards, so specific
298 * signatures must be used.
301 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD
},
302 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD
},
303 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD
},
304 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD
},
305 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD
},
306 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD
},
307 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD
},
308 {"FUTURE DOMAIN TMC-950", 5, 21, FD
},
309 /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
310 {"IBM F1 V1.2009/22/93", 5, 25, FD
},
313 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
314 #endif /* n OVERRIDE */
317 * hostno stores the hostnumber, as told to us by the init routine.
320 static int hostno
= -1;
321 static void seagate_reconnect_intr (int, void *, struct pt_regs
*);
322 static irqreturn_t
do_seagate_reconnect_intr (int, void *, struct pt_regs
*);
332 * Support for broken devices :
333 * The Seagate board has a handshaking problem. Namely, a lack
334 * thereof for slow devices. You can blast 600K/second through
335 * it if you are polling for each byte, more if you do a blind
336 * transfer. In the first case, with a fast device, REQ will
337 * transition high-low or high-low-high before your loop restarts
338 * and you'll have no problems. In the second case, the board
339 * will insert wait states for up to 13.2 usecs for REQ to
340 * transition low->high, and everything will work.
342 * However, there's nothing in the state machine that says
343 * you *HAVE* to see a high-low-high set of transitions before
344 * sending the next byte, and slow things like the Trantor CD ROMS
345 * will break because of this.
347 * So, we need to slow things down, which isn't as simple as it
348 * seems. We can't slow things down period, because then people
349 * who don't recompile their kernels will shoot me for ruining
350 * their performance. We need to do it on a case per case basis.
352 * The best for performance will be to, only for borken devices
353 * (this is stored on a per-target basis in the scsi_devices array)
355 * Wait for a low->high transition before continuing with that
356 * transfer. If we timeout, continue anyways. We don't need
357 * a long timeout, because REQ should only be asserted until the
358 * corresponding ACK is received and processed.
360 * Note that we can't use the system timer for this, because of
361 * resolution, and we *really* can't use the timer chip since
362 * gettimeofday() and the beeper routines use that. So,
363 * the best thing for us to do will be to calibrate a timing
364 * loop in the initialization code using the timer chip before
365 * gettimeofday() can screw with it.
367 * FIXME: this is broken (not borken :-). Empty loop costs less than
368 * loop with ISA access in it! -- pavel@ucw.cz
371 static int borken_calibration
= 0;
373 static void __init
borken_init (void)
375 register int count
= 0, start
= jiffies
+ 1, stop
= start
+ 25;
377 /* FIXME: There may be a better approach, this is a straight port for
380 while (time_before (jiffies
, start
))
382 for (; time_before (jiffies
, stop
); ++count
)
387 * Ok, we now have a count for .25 seconds. Convert to a
388 * count per second and divide by transfer rate in K. */
390 borken_calibration
= (count
* 4) / (SLOW_RATE
* 1024);
392 if (borken_calibration
< 1)
393 borken_calibration
= 1;
396 static inline void borken_wait (void)
400 for (count
= borken_calibration
; count
&& (STATUS
& STAT_REQ
); --count
)
403 #if (DEBUG & DEBUG_BORKEN)
405 printk ("scsi%d : borken timeout\n", hostno
);
409 #endif /* def SLOW_RATE */
411 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
412 * contains at least one ISA access, which takes more than 0.125
413 * usec. So if we loop 8 times time in usec, we are safe.
416 #define ULOOP( i ) for (clock = i*8;;)
417 #define TIMEOUT (!(clock--))
419 int __init
seagate_st0x_detect (Scsi_Host_Template
* tpnt
)
421 struct Scsi_Host
*instance
;
423 unsigned long cr
, dr
;
425 tpnt
->proc_name
= "seagate";
427 * First, we try for the manual override.
429 DANY ("Autodetecting ST0x / TMC-8xx\n");
432 printk (KERN_ERR
"seagate_st0x_detect() called twice?!\n");
436 /* If the user specified the controller type from the command line,
437 controller_type will be non-zero, so don't try to detect one */
439 if (!controller_type
) {
441 base_address
= OVERRIDE
;
442 controller_type
= CONTROLLER
;
444 DANY ("Base address overridden to %x, controller type is %s\n",
446 controller_type
== SEAGATE
? "SEAGATE" : "FD");
449 * To detect this card, we simply look for the signature
450 * from the BIOS version notice in all the possible locations
451 * of the ROM's. This has a nice side effect of not trashing
452 * any register locations that might be used by something else.
454 * XXX - note that we probably should be probing the address
455 * space for the on-board RAM instead.
458 for (i
= 0; i
< (sizeof (seagate_bases
) / sizeof (unsigned int)); ++i
) {
459 void __iomem
*p
= ioremap(seagate_bases
[i
], 0x2000);
462 for (j
= 0; j
< NUM_SIGNATURES
; ++j
)
463 if (check_signature(p
+ signatures
[j
].offset
, signatures
[j
].signature
, signatures
[j
].length
)) {
464 base_address
= seagate_bases
[i
];
465 controller_type
= signatures
[j
].type
;
470 #endif /* OVERRIDE */
472 /* (! controller_type) */
473 tpnt
->this_id
= (controller_type
== SEAGATE
) ? 7 : 6;
474 tpnt
->name
= (controller_type
== SEAGATE
) ? ST0X_ID_STR
: FD_ID_STR
;
477 printk(KERN_INFO
"seagate: ST0x/TMC-8xx not detected.\n");
481 cr
= base_address
+ (controller_type
== SEAGATE
? 0x1a00 : 0x1c00);
483 st0x_cr_sr
= ioremap(cr
, 0x100);
484 st0x_dr
= ioremap(dr
, 0x100);
486 DANY("%s detected. Base address = %x, cr = %x, dr = %x\n",
487 tpnt
->name
, base_address
, cr
, dr
);
490 * At all times, we will use IRQ 5. Should also check for IRQ3
491 * if we lose our first interrupt.
493 instance
= scsi_register (tpnt
, 0);
494 if (instance
== NULL
)
497 hostno
= instance
->host_no
;
498 if (request_irq (irq
, do_seagate_reconnect_intr
, SA_INTERRUPT
, (controller_type
== SEAGATE
) ? "seagate" : "tmc-8xx", instance
)) {
499 printk(KERN_ERR
"scsi%d : unable to allocate IRQ%d\n", hostno
, irq
);
503 instance
->io_port
= base_address
;
505 printk(KERN_INFO
"Calibrating borken timer... ");
507 printk(" %d cycles per transfer\n", borken_calibration
);
509 printk (KERN_INFO
"This is one second... ");
512 ULOOP (1 * 1000 * 1000) {
519 printk ("done, %s options:"
538 #ifdef SEAGATE_USE_ASM
554 static const char *seagate_st0x_info (struct Scsi_Host
*shpnt
)
556 static char buffer
[64];
558 snprintf(buffer
, 64, "%s at irq %d, address 0x%05X",
559 (controller_type
== SEAGATE
) ? ST0X_ID_STR
: FD_ID_STR
,
565 * These are our saved pointers for the outstanding command that is
566 * waiting for a reconnect
569 static unsigned char current_target
, current_lun
;
570 static unsigned char *current_cmnd
, *current_data
;
571 static int current_nobuffs
;
572 static struct scatterlist
*current_buffer
;
573 static int current_bufflen
;
577 * linked_connected indicates whether or not we are currently connected to
578 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
579 * using linked commands.
582 static int linked_connected
= 0;
583 static unsigned char linked_target
, linked_lun
;
586 static void (*done_fn
) (Scsi_Cmnd
*) = NULL
;
587 static Scsi_Cmnd
*SCint
= NULL
;
590 * These control whether or not disconnect / reconnect will be attempted,
591 * or are being attempted.
594 #define NO_RECONNECT 0
595 #define RECONNECT_NOW 1
596 #define CAN_RECONNECT 2
599 * LINKED_RIGHT indicates that we are currently connected to the correct target
600 * for this command, LINKED_WRONG indicates that we are connected to the wrong
601 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
604 #define LINKED_RIGHT 3
605 #define LINKED_WRONG 4
608 * This determines if we are expecting to reconnect or not.
611 static int should_reconnect
= 0;
614 * The seagate_reconnect_intr routine is called when a target reselects the
615 * host adapter. This occurs on the interrupt triggered by the target
619 static irqreturn_t
do_seagate_reconnect_intr(int irq
, void *dev_id
,
620 struct pt_regs
*regs
)
623 struct Scsi_Host
*dev
= dev_id
;
625 spin_lock_irqsave (dev
->host_lock
, flags
);
626 seagate_reconnect_intr (irq
, dev_id
, regs
);
627 spin_unlock_irqrestore (dev
->host_lock
, flags
);
631 static void seagate_reconnect_intr (int irq
, void *dev_id
, struct pt_regs
*regs
)
636 DPRINTK (PHASE_RESELECT
, "scsi%d : seagate_reconnect_intr() called\n", hostno
);
638 if (!should_reconnect
)
639 printk(KERN_WARNING
"scsi%d: unexpected interrupt.\n", hostno
);
641 should_reconnect
= 0;
643 DPRINTK (PHASE_RESELECT
, "scsi%d : internal_command(%d, %08x, %08x, RECONNECT_NOW\n",
644 hostno
, current_target
, current_data
, current_bufflen
);
646 temp
= internal_command (current_target
, current_lun
, current_cmnd
, current_data
, current_bufflen
, RECONNECT_NOW
);
648 if (msg_byte(temp
) != DISCONNECT
) {
650 DPRINTK(PHASE_RESELECT
, "scsi%d : done_fn(%d,%08x)", hostno
, hostno
, temp
);
652 panic ("SCint == NULL in seagate");
655 SCtmp
->result
= temp
;
658 printk(KERN_ERR
"done_fn() not defined.\n");
664 * The seagate_st0x_queue_command() function provides a queued interface
665 * to the seagate SCSI driver. Basically, it just passes control onto the
666 * seagate_command() function, after fixing it so that the done_fn()
667 * is set to the one passed to the function. We have to be very careful,
668 * because there are some commands on some devices that do not disconnect,
669 * and if we simply call the done_fn when the command is done then another
670 * command is started and queue_command is called again... We end up
671 * overflowing the kernel stack, and this tends not to be such a good idea.
674 static int recursion_depth
= 0;
676 static int seagate_st0x_queue_command (Scsi_Cmnd
* SCpnt
, void (*done
) (Scsi_Cmnd
*))
678 int result
, reconnect
;
681 DANY ("seagate: que_command");
683 current_target
= SCpnt
->device
->id
;
684 current_lun
= SCpnt
->device
->lun
;
685 current_cmnd
= SCpnt
->cmnd
;
686 current_data
= (unsigned char *) SCpnt
->request_buffer
;
687 current_bufflen
= SCpnt
->request_bufflen
;
695 * Set linked command bit in control field of SCSI command.
698 current_cmnd
[SCpnt
->cmd_len
] |= 0x01;
699 if (linked_connected
) {
700 DPRINTK (DEBUG_LINKED
, "scsi%d : using linked commands, current I_T_L nexus is ", hostno
);
701 if (linked_target
== current_target
&& linked_lun
== current_lun
)
703 DPRINTK(DEBUG_LINKED
, "correct\n");
704 reconnect
= LINKED_RIGHT
;
706 DPRINTK(DEBUG_LINKED
, "incorrect\n");
707 reconnect
= LINKED_WRONG
;
711 reconnect
= CAN_RECONNECT
;
713 result
= internal_command(SCint
->device
->id
, SCint
->device
->lun
, SCint
->cmnd
,
714 SCint
->request_buffer
, SCint
->request_bufflen
, reconnect
);
715 if (msg_byte(result
) == DISCONNECT
)
719 SCtmp
->result
= result
;
727 static int internal_command (unsigned char target
, unsigned char lun
,
728 const void *cmnd
, void *buff
, int bufflen
, int reselect
)
730 unsigned char *data
= NULL
;
731 struct scatterlist
*buffer
= NULL
;
732 int clock
, temp
, nobuffs
= 0, done
= 0, len
= 0;
734 int transfered
= 0, phase
= 0, newphase
;
736 register unsigned char status_read
;
737 unsigned char tmp_data
, tmp_control
, status
= 0, message
= 0;
738 unsigned transfersize
= 0, underflow
= 0;
740 int borken
= (int) SCint
->device
->borken
; /* Does the current target require
747 #if (DEBUG & PRINT_COMMAND)
748 printk("scsi%d : target = %d, command = ", hostno
, target
);
749 print_command((unsigned char *) cmnd
);
752 #if (DEBUG & PHASE_RESELECT)
755 printk("scsi%d : reconnecting\n", hostno
);
759 printk("scsi%d : connected, can reconnect\n", hostno
);
762 printk("scsi%d : connected to wrong target, can reconnect\n",
767 printk("scsi%d : allowed to reconnect\n", hostno
);
770 printk("scsi%d : not allowed to reconnect\n", hostno
);
774 if (target
== (controller_type
== SEAGATE
? 7 : 6))
775 return DID_BAD_TARGET
;
778 * We work it differently depending on if this is is "the first time,"
779 * or a reconnect. If this is a reselect phase, then SEL will
780 * be asserted, and we must skip selection / arbitration phases.
785 DPRINTK (PHASE_RESELECT
, "scsi%d : phase RESELECT \n", hostno
);
787 * At this point, we should find the logical or of our ID
788 * and the original target's ID on the BUS, with BSY, SEL,
789 * and I/O signals asserted.
791 * After ARBITRATION phase is completed, only SEL, BSY,
792 * and the target ID are asserted. A valid initiator ID
793 * is not on the bus until IO is asserted, so we must wait
798 if ((temp
& STAT_IO
) && !(temp
& STAT_BSY
))
801 DPRINTK (PHASE_RESELECT
, "scsi%d : RESELECT timed out while waiting for IO .\n", hostno
);
802 return (DID_BAD_INTR
<< 16);
807 * After I/O is asserted by the target, we can read our ID
808 * and its ID off of the BUS.
811 if (!((temp
= DATA
) & (controller_type
== SEAGATE
? 0x80 : 0x40))) {
812 DPRINTK (PHASE_RESELECT
, "scsi%d : detected reconnect request to different target.\n\tData bus = %d\n", hostno
, temp
);
813 return (DID_BAD_INTR
<< 16);
816 if (!(temp
& (1 << current_target
))) {
817 printk(KERN_WARNING
"scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno
, temp
);
818 return (DID_BAD_INTR
<< 16);
821 buffer
= current_buffer
;
822 cmnd
= current_cmnd
; /* WDE add */
823 data
= current_data
; /* WDE add */
824 len
= current_bufflen
; /* WDE add */
825 nobuffs
= current_nobuffs
;
828 * We have determined that we have been selected. At this
829 * point, we must respond to the reselection by asserting
834 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
| CMD_BSY
);
836 WRITE_CONTROL (BASE_CMD
| CMD_BSY
);
840 * The target will drop SEL, and raise BSY, at which time
845 if (!(STATUS
& STAT_SEL
))
848 WRITE_CONTROL (BASE_CMD
| CMD_INTR
);
849 DPRINTK (PHASE_RESELECT
, "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno
);
850 return (DID_BAD_INTR
<< 16);
853 WRITE_CONTROL (BASE_CMD
);
855 * At this point, we have connected with the target
856 * and can get on with our lives.
862 * This is a bletcherous hack, just as bad as the Unix #!
863 * interpreter stuff. If it turns out we are using the wrong
864 * I_T_L nexus, the easiest way to deal with it is to go into
865 * our INFORMATION TRANSFER PHASE code, send a ABORT
866 * message on MESSAGE OUT phase, and then loop back to here.
870 DPRINTK (PHASE_BUS_FREE
, "scsi%d : phase = BUS FREE \n", hostno
);
875 * On entry, we make sure that the BUS is in a BUS FREE
876 * phase, by insuring that both BSY and SEL are low for
877 * at least one bus settle delay. Several reads help
878 * eliminate wire glitch.
882 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
883 clock
= jiffies
+ ST0X_BUS_FREE_DELAY
;
885 while (((STATUS
| STATUS
| STATUS
) & (STAT_BSY
| STAT_SEL
)) && (!st0x_aborted
) && time_before (jiffies
, clock
))
888 if (time_after (jiffies
, clock
))
889 return retcode (DID_BUS_BUSY
);
890 else if (st0x_aborted
)
891 return retcode (st0x_aborted
);
893 DPRINTK (PHASE_SELECTION
, "scsi%d : phase = SELECTION\n", hostno
);
895 clock
= jiffies
+ ST0X_SELECTION_DELAY
;
898 * Arbitration/selection procedure :
900 * 2. Write HOST adapter address bit
901 * 3. Set start arbitration.
902 * 4. We get either ARBITRATION COMPLETE or SELECT at this
904 * 5. OR our ID and targets on bus.
905 * 6. Enable SCSI drivers and asserted SEL and ATTN
909 /* FIXME: verify host lock is always held here */
911 WRITE_DATA((controller_type
== SEAGATE
) ? 0x80 : 0x40);
912 WRITE_CONTROL(CMD_START_ARB
);
914 ULOOP (ST0X_SELECTION_DELAY
* 10000) {
915 status_read
= STATUS
;
916 if (status_read
& STAT_ARB_CMPL
)
918 if (st0x_aborted
) /* FIXME: What? We are going to do something even after abort? */
920 if (TIMEOUT
|| (status_read
& STAT_SEL
)) {
921 printk(KERN_WARNING
"scsi%d : arbitration lost or timeout.\n", hostno
);
922 WRITE_CONTROL (BASE_CMD
);
923 return retcode (DID_NO_CONNECT
);
926 DPRINTK (PHASE_SELECTION
, "scsi%d : arbitration complete\n", hostno
);
930 * When the SCSI device decides that we're gawking at it,
931 * it will respond by asserting BUSY on the bus.
933 * Note : the Seagate ST-01/02 product manual says that we
934 * should twiddle the DATA register before the control
935 * register. However, this does not work reliably so we do
936 * it the other way around.
938 * Probably could be a problem with arbitration too, we
939 * really should try this with a SCSI protocol or logic
940 * analyzer to see what is going on.
942 tmp_data
= (unsigned char) ((1 << target
) | (controller_type
== SEAGATE
? 0x80 : 0x40));
943 tmp_control
= BASE_CMD
| CMD_DRVR_ENABLE
| CMD_SEL
| (reselect
? CMD_ATTN
: 0);
945 /* FIXME: verify host lock is always held here */
946 #ifdef OLDCNTDATASCEME
948 WRITE_CONTROL (tmp_control
);
949 WRITE_DATA (tmp_data
);
951 WRITE_DATA (tmp_data
);
952 WRITE_CONTROL (tmp_control
);
955 tmp_control
^= CMD_BSY
; /* This is guesswork. What used to be in driver */
956 WRITE_CONTROL (tmp_control
); /* could never work: it sent data into control */
957 WRITE_DATA (tmp_data
); /* register and control info into data. Hopefully */
958 tmp_control
^= CMD_BSY
; /* fixed, but order of first two may be wrong. */
959 WRITE_CONTROL (tmp_control
); /* -- pavel@ucw.cz */
965 * If we have been aborted, and we have a
966 * command in progress, IE the target
967 * still has BSY asserted, then we will
968 * reset the bus, and notify the midlevel
969 * driver to expect sense.
972 WRITE_CONTROL (BASE_CMD
);
973 if (STATUS
& STAT_BSY
) {
974 printk(KERN_WARNING
"scsi%d : BST asserted after we've been aborted.\n", hostno
);
975 seagate_st0x_bus_reset(NULL
);
976 return retcode (DID_RESET
);
978 return retcode (st0x_aborted
);
980 if (STATUS
& STAT_BSY
)
983 DPRINTK (PHASE_SELECTION
, "scsi%d : NO CONNECT with target %d, stat = %x \n", hostno
, target
, STATUS
);
984 return retcode (DID_NO_CONNECT
);
988 /* Establish current pointers. Take into account scatter / gather */
990 if ((nobuffs
= SCint
->use_sg
)) {
991 #if (DEBUG & DEBUG_SG)
994 printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno
, nobuffs
);
995 for (i
= 0; i
< nobuffs
; ++i
)
996 printk("scsi%d : buffer %d address = %p length = %d\n",
998 page_address(buffer
[i
].page
) + buffer
[i
].offset
,
1003 buffer
= (struct scatterlist
*) SCint
->buffer
;
1004 len
= buffer
->length
;
1005 data
= page_address(buffer
->page
) + buffer
->offset
;
1007 DPRINTK (DEBUG_SG
, "scsi%d : scatter gather not requested.\n", hostno
);
1009 len
= SCint
->request_bufflen
;
1010 data
= (unsigned char *) SCint
->request_buffer
;
1013 DPRINTK (PHASE_DATAIN
| PHASE_DATAOUT
, "scsi%d : len = %d\n",
1023 } /* end of switch(reselect) */
1026 * There are several conditions under which we wish to send a message :
1027 * 1. When we are allowing disconnect / reconnect, and need to
1028 * establish the I_T_L nexus via an IDENTIFY with the DiscPriv bit
1031 * 2. When we are doing linked commands, are have the wrong I_T_L
1032 * nexus established and want to send an ABORT message.
1035 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1037 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
| (((reselect
== CAN_RECONNECT
)|| (reselect
== LINKED_WRONG
))? CMD_ATTN
: 0));
1039 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
| (((reselect
== CAN_RECONNECT
))? CMD_ATTN
: 0));
1043 * INFORMATION TRANSFER PHASE
1045 * The nasty looking read / write inline assembler loops we use for
1046 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1047 * the 'C' versions - since we're moving 1024 bytes of data, this
1050 * SJT: The nasty-looking assembler is gone, so it's slower.
1054 DPRINTK (PHASE_ETC
, "scsi%d : phase = INFORMATION TRANSFER\n", hostno
);
1057 transfersize
= SCint
->transfersize
;
1058 underflow
= SCint
->underflow
;
1061 * Now, we poll the device for status information,
1062 * and handle any requests it makes. Note that since we are unsure
1063 * of how much data will be flowing across the system, etc and
1064 * cannot make reasonable timeouts, that we will instead have the
1065 * midlevel driver handle any timeouts that occur in this phase.
1068 while (((status_read
= STATUS
) & STAT_BSY
) && !st0x_aborted
&& !done
) {
1070 if (status_read
& STAT_PARITY
) {
1071 printk(KERN_ERR
"scsi%d : got parity error\n", hostno
);
1072 st0x_aborted
= DID_PARITY
;
1075 if (status_read
& STAT_REQ
) {
1076 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1077 if ((newphase
= (status_read
& REQ_MASK
)) != phase
) {
1081 printk ("scsi%d : phase = DATA OUT\n", hostno
);
1084 printk ("scsi%d : phase = DATA IN\n", hostno
);
1088 ("scsi%d : phase = COMMAND OUT\n", hostno
);
1091 printk ("scsi%d : phase = STATUS IN\n", hostno
);
1095 ("scsi%d : phase = MESSAGE OUT\n", hostno
);
1098 printk ("scsi%d : phase = MESSAGE IN\n", hostno
);
1101 printk ("scsi%d : phase = UNKNOWN\n", hostno
);
1102 st0x_aborted
= DID_ERROR
;
1106 switch (status_read
& REQ_MASK
) {
1109 * If we are in fast mode, then we simply splat
1110 * the data out in word-sized chunks as fast as
1116 printk("scsi%d: underflow to target %d lun %d \n", hostno
, target
, lun
);
1117 st0x_aborted
= DID_ERROR
;
1123 if (fast
&& transfersize
1124 && !(len
% transfersize
)
1125 && (len
>= transfersize
)
1127 && !(transfersize
% 4)
1130 DPRINTK (DEBUG_FAST
,
1131 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1132 " len = %d, data = %08x\n",
1133 hostno
, SCint
->underflow
,
1134 SCint
->transfersize
, len
,
1137 /* SJT: Start. Fast Write */
1138 #ifdef SEAGATE_USE_ASM
1144 "movl %%eax, (%%edi)\n\t"
1148 "movb %%al, (%%edi)\n\t"
1152 /* input */ :"D" (st0x_dr
),
1155 "c" (SCint
->transfersize
)
1159 #else /* SEAGATE_USE_ASM */
1160 memcpy_toio(st0x_dr
, data
, transfersize
);
1161 #endif /* SEAGATE_USE_ASM */
1163 len
-= transfersize
;
1164 data
+= transfersize
;
1165 DPRINTK (DEBUG_FAST
, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno
, len
, data
);
1168 * We loop as long as we are in a
1169 * data out phase, there is data to
1170 * send, and BSY is still active.
1173 /* SJT: Start. Slow Write. */
1174 #ifdef SEAGATE_USE_ASM
1176 int __dummy_1
, __dummy_2
;
1179 * We loop as long as we are in a data out phase, there is data to send,
1180 * and BSY is still active.
1182 /* Local variables : len = ecx , data = esi,
1183 st0x_cr_sr = ebx, st0x_dr = edi
1186 /* Test for any data here at all. */
1187 "orl %%ecx, %%ecx\n\t"
1188 "jz 2f\n\t" "cld\n\t"
1189 /* "movl st0x_cr_sr, %%ebx\n\t" */
1190 /* "movl st0x_dr, %%edi\n\t" */
1192 "movb (%%ebx), %%al\n\t"
1196 /* Test for data out phase - STATUS & REQ_MASK should be
1197 REQ_DATAOUT, which is 0. */
1198 "test $0xe, %%al\n\t"
1201 "test $0x10, %%al\n\t"
1204 "movb %%al, (%%edi)\n\t"
1205 "loop 1b\n\t" "2:\n"
1206 /* output */ :"=S" (data
), "=c" (len
),
1211 : "0" (data
), "1" (len
),
1216 #else /* SEAGATE_USE_ASM */
1221 if (!(stat
& STAT_BSY
)
1222 || ((stat
& REQ_MASK
) !=
1225 if (stat
& STAT_REQ
) {
1226 WRITE_DATA (*data
++);
1230 #endif /* SEAGATE_USE_ASM */
1234 if (!len
&& nobuffs
) {
1237 len
= buffer
->length
;
1238 data
= page_address(buffer
->page
) + buffer
->offset
;
1240 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1248 #if (DEBUG & (PHASE_DATAIN))
1251 for (; len
&& (STATUS
& (REQ_MASK
| STAT_REQ
)) == (REQ_DATAIN
| STAT_REQ
); --len
) {
1255 #if (DEBUG & (PHASE_DATAIN))
1261 if (fast
&& transfersize
1262 && !(len
% transfersize
)
1263 && (len
>= transfersize
)
1265 && !(transfersize
% 4)
1268 DPRINTK (DEBUG_FAST
,
1269 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1270 " len = %d, data = %08x\n",
1271 hostno
, SCint
->underflow
,
1272 SCint
->transfersize
, len
,
1275 /* SJT: Start. Fast Read */
1276 #ifdef SEAGATE_USE_ASM
1281 "movl (%%esi), %%eax\n\t"
1285 "movb (%%esi), %%al\n\t"
1290 /* input */ :"S" (st0x_dr
),
1293 "c" (SCint
->transfersize
)
1297 #else /* SEAGATE_USE_ASM */
1298 memcpy_fromio(data
, st0x_dr
, len
);
1299 #endif /* SEAGATE_USE_ASM */
1301 len
-= transfersize
;
1302 data
+= transfersize
;
1303 #if (DEBUG & PHASE_DATAIN)
1304 printk ("scsi%d: transfered += %d\n", hostno
, transfersize
);
1305 transfered
+= transfersize
;
1308 DPRINTK (DEBUG_FAST
, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno
, len
, data
);
1311 #if (DEBUG & PHASE_DATAIN)
1312 printk ("scsi%d: transfered += %d\n", hostno
, len
);
1313 transfered
+= len
; /* Assume we'll transfer it all, then
1314 subtract what we *didn't* transfer */
1318 * We loop as long as we are in a data in phase, there is room to read,
1319 * and BSY is still active
1323 #ifdef SEAGATE_USE_ASM
1325 int __dummy_3
, __dummy_4
;
1327 /* Dummy clobbering variables for the new gcc-2.95 */
1330 * We loop as long as we are in a data in phase, there is room to read,
1331 * and BSY is still active
1333 /* Local variables : ecx = len, edi = data
1334 esi = st0x_cr_sr, ebx = st0x_dr */
1336 /* Test for room to read */
1337 "orl %%ecx, %%ecx\n\t"
1338 "jz 2f\n\t" "cld\n\t"
1339 /* "movl st0x_cr_sr, %%esi\n\t" */
1340 /* "movl st0x_dr, %%ebx\n\t" */
1342 "movb (%%esi), %%al\n\t"
1346 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1347 = STAT_IO, which is 4. */
1348 "movb $0xe, %%ah\n\t"
1349 "andb %%al, %%ah\n\t"
1350 "cmpb $0x04, %%ah\n\t"
1353 "test $0x10, %%al\n\t"
1355 "movb (%%ebx), %%al\n\t"
1357 "loop 1b\n\t" "2:\n"
1358 /* output */ :"=D" (data
), "=c" (len
),
1363 : "0" (data
), "1" (len
),
1368 #else /* SEAGATE_USE_ASM */
1373 if (!(stat
& STAT_BSY
)
1374 || ((stat
& REQ_MASK
) !=
1377 if (stat
& STAT_REQ
) {
1382 #endif /* SEAGATE_USE_ASM */
1384 #if (DEBUG & PHASE_DATAIN)
1385 printk ("scsi%d: transfered -= %d\n", hostno
, len
);
1386 transfered
-= len
; /* Since we assumed all of Len got *
1387 transfered, correct our mistake */
1391 if (!len
&& nobuffs
) {
1394 len
= buffer
->length
;
1395 data
= page_address(buffer
->page
) + buffer
->offset
;
1396 DPRINTK (DEBUG_SG
, "scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno
, len
, data
);
1401 while (((status_read
= STATUS
) & STAT_BSY
) &&
1402 ((status_read
& REQ_MASK
) == REQ_CMDOUT
))
1403 if (status_read
& STAT_REQ
) {
1404 WRITE_DATA (*(const unsigned char *) cmnd
);
1405 cmnd
= 1 + (const unsigned char *)cmnd
;
1419 * We can only have sent a MSG OUT if we
1420 * requested to do this by raising ATTN.
1421 * So, we must drop ATTN.
1423 WRITE_CONTROL (BASE_CMD
| CMD_DRVR_ENABLE
);
1425 * If we are reconnecting, then we must
1426 * send an IDENTIFY message in response
1431 WRITE_DATA (IDENTIFY (1, lun
));
1432 DPRINTK (PHASE_RESELECT
| PHASE_MSGOUT
, "scsi%d : sent IDENTIFY message.\n", hostno
);
1437 linked_connected
= 0;
1438 reselect
= CAN_RECONNECT
;
1440 DPRINTK (PHASE_MSGOUT
| DEBUG_LINKED
, "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno
);
1442 DPRINTK (DEBUG_LINKED
, "correct\n");
1445 printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno
, target
);
1450 switch (message
= DATA
) {
1452 DANY("seagate: deciding to disconnect\n");
1453 should_reconnect
= 1;
1454 current_data
= data
; /* WDE add */
1455 current_buffer
= buffer
;
1456 current_bufflen
= len
; /* WDE add */
1457 current_nobuffs
= nobuffs
;
1459 linked_connected
= 0;
1462 DPRINTK ((PHASE_RESELECT
| PHASE_MSGIN
), "scsi%d : disconnected.\n", hostno
);
1466 case LINKED_CMD_COMPLETE
:
1467 case LINKED_FLG_CMD_COMPLETE
:
1469 case COMMAND_COMPLETE
:
1471 * Note : we should check for underflow here.
1473 DPRINTK(PHASE_MSGIN
, "scsi%d : command complete.\n", hostno
);
1477 DPRINTK(PHASE_MSGIN
, "scsi%d : abort message.\n", hostno
);
1481 current_buffer
= buffer
;
1482 current_bufflen
= len
; /* WDE add */
1483 current_data
= data
; /* WDE mod */
1484 current_nobuffs
= nobuffs
;
1485 DPRINTK (PHASE_MSGIN
, "scsi%d : pointers saved.\n", hostno
);
1487 case RESTORE_POINTERS
:
1488 buffer
= current_buffer
;
1489 cmnd
= current_cmnd
;
1490 data
= current_data
; /* WDE mod */
1491 len
= current_bufflen
;
1492 nobuffs
= current_nobuffs
;
1493 DPRINTK(PHASE_MSGIN
, "scsi%d : pointers restored.\n", hostno
);
1498 * IDENTIFY distinguishes itself
1499 * from the other messages by
1500 * setting the high bit.
1502 * Note : we need to handle at
1503 * least one outstanding command
1504 * per LUN, and need to hash the
1505 * SCSI command for that I_T_L
1506 * nexus based on the known ID
1507 * (at this point) and LUN.
1510 if (message
& 0x80) {
1511 DPRINTK (PHASE_MSGIN
, "scsi%d : IDENTIFY message received from id %d, lun %d.\n", hostno
, target
, message
& 7);
1514 * We should go into a
1515 * MESSAGE OUT phase, and
1516 * send a MESSAGE_REJECT
1517 * if we run into a message
1518 * that we don't like. The
1519 * seagate driver needs
1521 * restructuring first
1524 DPRINTK (PHASE_MSGIN
, "scsi%d : unknown message %d from target %d.\n", hostno
, message
, target
);
1529 printk(KERN_ERR
"scsi%d : unknown phase.\n", hostno
);
1530 st0x_aborted
= DID_ERROR
;
1531 } /* end of switch (status_read & REQ_MASK) */
1534 * I really don't care to deal with borken devices in
1535 * each single byte transfer case (ie, message in,
1536 * message out, status), so I'll do the wait here if
1543 } /* if(status_read & STAT_REQ) ends */
1544 } /* while(((status_read = STATUS)...) ends */
1546 DPRINTK(PHASE_DATAIN
| PHASE_DATAOUT
| PHASE_EXIT
, "scsi%d : Transfered %d bytes\n", hostno
, transfered
);
1548 #if (DEBUG & PHASE_EXIT)
1549 #if 0 /* Doesn't work for scatter/gather */
1550 printk("Buffer : \n");
1551 for(i
= 0; i
< 20; ++i
)
1552 printk("%02x ", ((unsigned char *) data
)[i
]); /* WDE mod */
1555 printk("scsi%d : status = ", hostno
);
1556 print_status(status
);
1557 printk(" message = %02x\n", message
);
1560 /* We shouldn't reach this until *after* BSY has been deasserted */
1566 * Fix the message byte so that unsuspecting high level drivers
1567 * don't puke when they see a LINKED COMMAND message in place of
1568 * the COMMAND COMPLETE they may be expecting. Shouldn't be
1569 * necessary, but it's better to be on the safe side.
1571 * A non LINKED* message byte will indicate that the command
1572 * completed, and we are now disconnected.
1576 case LINKED_CMD_COMPLETE
:
1577 case LINKED_FLG_CMD_COMPLETE
:
1578 message
= COMMAND_COMPLETE
;
1579 linked_target
= current_target
;
1580 linked_lun
= current_lun
;
1581 linked_connected
= 1;
1582 DPRINTK (DEBUG_LINKED
, "scsi%d : keeping I_T_L nexus established for linked command.\n", hostno
);
1583 /* We also will need to adjust status to accommodate intermediate
1585 if ((status
== INTERMEDIATE_GOOD
) || (status
== INTERMEDIATE_C_GOOD
))
1589 * We should also handle what are "normal" termination
1590 * messages here (ABORT, BUS_DEVICE_RESET?, and
1591 * COMMAND_COMPLETE individually, and flake if things
1595 DPRINTK (DEBUG_LINKED
, "scsi%d : closing I_T_L nexus.\n", hostno
);
1596 linked_connected
= 0;
1601 if (should_reconnect
) {
1602 DPRINTK (PHASE_RESELECT
, "scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n", hostno
);
1603 WRITE_CONTROL (BASE_CMD
| CMD_INTR
);
1605 WRITE_CONTROL (BASE_CMD
);
1607 return retcode (st0x_aborted
);
1608 } /* end of internal_command */
1610 static int seagate_st0x_abort (Scsi_Cmnd
* SCpnt
)
1612 st0x_aborted
= DID_ABORT
;
1620 * the seagate_st0x_reset function resets the SCSI bus
1622 * May be called with SCpnt = NULL
1625 static int seagate_st0x_bus_reset(Scsi_Cmnd
* SCpnt
)
1627 /* No timeouts - this command is going to fail because it was reset. */
1628 DANY ("scsi%d: Reseting bus... ", hostno
);
1630 /* assert RESET signal on SCSI bus. */
1631 WRITE_CONTROL (BASE_CMD
| CMD_RST
);
1635 WRITE_CONTROL (BASE_CMD
);
1636 st0x_aborted
= DID_RESET
;
1642 static int seagate_st0x_host_reset(Scsi_Cmnd
*SCpnt
)
1647 static int seagate_st0x_device_reset(Scsi_Cmnd
*SCpnt
)
1652 static int seagate_st0x_release(struct Scsi_Host
*shost
)
1655 free_irq(shost
->irq
, shost
);
1656 release_region(shost
->io_port
, shost
->n_io_port
);
1660 static Scsi_Host_Template driver_template
= {
1661 .detect
= seagate_st0x_detect
,
1662 .release
= seagate_st0x_release
,
1663 .info
= seagate_st0x_info
,
1664 .queuecommand
= seagate_st0x_queue_command
,
1665 .eh_abort_handler
= seagate_st0x_abort
,
1666 .eh_bus_reset_handler
= seagate_st0x_bus_reset
,
1667 .eh_host_reset_handler
= seagate_st0x_host_reset
,
1668 .eh_device_reset_handler
= seagate_st0x_device_reset
,
1671 .sg_tablesize
= SG_ALL
,
1673 .use_clustering
= DISABLE_CLUSTERING
,
1675 #include "scsi_module.c"