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>
100 #include <linux/delay.h>
103 #include <asm/system.h>
104 #include <asm/uaccess.h>
107 #include <scsi/scsi_dbg.h>
108 #include <scsi/scsi_host.h>
111 #include <scsi/scsi_ioctl.h>
114 #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
116 #define DPRINTK( when, msg... ) do { } while (0)
118 #define DANY( msg... ) DPRINTK( 0xffff, msg );
128 #undef LINKED /* Linked commands are currently broken! */
130 #if defined(OVERRIDE) && !defined(CONTROLLER)
131 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
135 #undef SEAGATE_USE_ASM
139 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
140 driver, and Mitsugu Suzuki for information on the ST-01
151 #define CMD_ATTN 0x08
152 #define CMD_START_ARB 0x10
153 #define CMD_EN_PARITY 0x20
154 #define CMD_INTR 0x40
155 #define CMD_DRVR_ENABLE 0x80
161 #define STAT_MSG 0x08
164 #define STAT_MSG 0x02
168 #define STAT_BSY 0x01
170 #define STAT_REQ 0x10
171 #define STAT_SEL 0x20
172 #define STAT_PARITY 0x40
173 #define STAT_ARB_CMPL 0x80
179 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
180 #define REQ_DATAOUT 0
181 #define REQ_DATAIN STAT_IO
182 #define REQ_CMDOUT STAT_CD
183 #define REQ_STATIN (STAT_CD | STAT_IO)
184 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
185 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
187 extern volatile int seagate_st0x_timeout;
190 #define BASE_CMD CMD_EN_PARITY
199 #define PHASE_BUS_FREE 1
200 #define PHASE_ARBITRATION 2
201 #define PHASE_SELECTION 4
202 #define PHASE_DATAIN 8
203 #define PHASE_DATAOUT 0x10
204 #define PHASE_CMDOUT 0x20
205 #define PHASE_MSGIN 0x40
206 #define PHASE_MSGOUT 0x80
207 #define PHASE_STATUSIN 0x100
208 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
209 #define PRINT_COMMAND 0x200
210 #define PHASE_EXIT 0x400
211 #define PHASE_RESELECT 0x800
212 #define DEBUG_FAST 0x1000
213 #define DEBUG_SG 0x2000
214 #define DEBUG_LINKED 0x4000
215 #define DEBUG_BORKEN 0x8000
218 * Control options - these are timeouts specified in .01 seconds.
222 #define ST0X_BUS_FREE_DELAY 25
223 #define ST0X_SELECTION_DELAY 25
225 #define SEAGATE 1 /* these determine the type of the controller */
228 #define ST0X_ID_STR "Seagate ST-01/ST-02"
229 #define FD_ID_STR "TMC-8XX/TMC-950"
231 static int internal_command (unsigned char target, unsigned char lun,
233 void *buff, int bufflen, int reselect);
235 static int incommand; /* set if arbitration has finished
236 and we are in some command phase. */
238 static unsigned int base_address = 0; /* Where the card ROM starts, used to
239 calculate memory mapped register
242 static void __iomem *st0x_cr_sr; /* control register write, status
243 register read. 256 bytes in
245 Read is status of SCSI BUS, as per
248 static void __iomem *st0x_dr; /* data register, read write 256
251 static volatile int st0x_aborted = 0; /* set when we are aborted, ie by a
254 static unsigned char controller_type = 0; /* set to SEAGATE for ST0x
255 boards or FD for TMC-8xx
257 static int irq = IRQ;
259 module_param(base_address, uint, 0);
260 module_param(controller_type, byte, 0);
261 module_param(irq, int, 0);
262 MODULE_LICENSE("GPL");
265 #define retcode(result) (((result) << 16) | (message << 8) | status)
266 #define STATUS ((u8) readb(st0x_cr_sr))
267 #define DATA ((u8) readb(st0x_dr))
268 #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
269 #define WRITE_DATA(d) { writeb((d), st0x_dr); }
272 static unsigned int seagate_bases[] = {
273 0xc8000, 0xca000, 0xcc000,
274 0xce000, 0xdc000, 0xde000
278 const unsigned char *signature;
284 static Signature __initdata signatures[] = {
285 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
286 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
289 * The following two lines are NOT mistakes. One detects ROM revision
290 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
291 * and this is not going to change, the "SEAGATE" and "SCSI" together
292 * are probably "good enough"
295 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
296 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
299 * However, future domain makes several incompatible SCSI boards, so specific
300 * signatures must be used.
303 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
304 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
305 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
306 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
307 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
308 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
309 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
310 {"FUTURE DOMAIN TMC-950", 5, 21, FD},
311 /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
312 {"IBM F1 V1.2009/22/93", 5, 25, FD},
315 #define NUM_SIGNATURES ARRAY_SIZE(signatures)
316 #endif /* n OVERRIDE */
319 * hostno stores the hostnumber, as told to us by the init routine.
322 static int hostno = -1;
323 static void seagate_reconnect_intr (int, void *, struct pt_regs *);
324 static irqreturn_t do_seagate_reconnect_intr (int, void *, struct pt_regs *);
334 * Support for broken devices :
335 * The Seagate board has a handshaking problem. Namely, a lack
336 * thereof for slow devices. You can blast 600K/second through
337 * it if you are polling for each byte, more if you do a blind
338 * transfer. In the first case, with a fast device, REQ will
339 * transition high-low or high-low-high before your loop restarts
340 * and you'll have no problems. In the second case, the board
341 * will insert wait states for up to 13.2 usecs for REQ to
342 * transition low->high, and everything will work.
344 * However, there's nothing in the state machine that says
345 * you *HAVE* to see a high-low-high set of transitions before
346 * sending the next byte, and slow things like the Trantor CD ROMS
347 * will break because of this.
349 * So, we need to slow things down, which isn't as simple as it
350 * seems. We can't slow things down period, because then people
351 * who don't recompile their kernels will shoot me for ruining
352 * their performance. We need to do it on a case per case basis.
354 * The best for performance will be to, only for borken devices
355 * (this is stored on a per-target basis in the scsi_devices array)
357 * Wait for a low->high transition before continuing with that
358 * transfer. If we timeout, continue anyways. We don't need
359 * a long timeout, because REQ should only be asserted until the
360 * corresponding ACK is received and processed.
362 * Note that we can't use the system timer for this, because of
363 * resolution, and we *really* can't use the timer chip since
364 * gettimeofday() and the beeper routines use that. So,
365 * the best thing for us to do will be to calibrate a timing
366 * loop in the initialization code using the timer chip before
367 * gettimeofday() can screw with it.
369 * FIXME: this is broken (not borken :-). Empty loop costs less than
370 * loop with ISA access in it! -- pavel@ucw.cz
373 static int borken_calibration = 0;
375 static void __init borken_init (void)
377 register int count = 0, start = jiffies + 1, stop = start + 25;
379 /* FIXME: There may be a better approach, this is a straight port for
382 while (time_before (jiffies, start))
384 for (; time_before (jiffies, stop); ++count)
389 * Ok, we now have a count for .25 seconds. Convert to a
390 * count per second and divide by transfer rate in K. */
392 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
394 if (borken_calibration < 1)
395 borken_calibration = 1;
398 static inline void borken_wait (void)
402 for (count = borken_calibration; count && (STATUS & STAT_REQ); --count)
405 #if (DEBUG & DEBUG_BORKEN)
407 printk ("scsi%d : borken timeout\n", hostno);
411 #endif /* def SLOW_RATE */
413 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
414 * contains at least one ISA access, which takes more than 0.125
415 * usec. So if we loop 8 times time in usec, we are safe.
418 #define ULOOP( i ) for (clock = i*8;;)
419 #define TIMEOUT (!(clock--))
421 int __init seagate_st0x_detect (struct scsi_host_template * tpnt)
423 struct Scsi_Host *instance;
425 unsigned long cr, dr;
427 tpnt->proc_name = "seagate";
429 * 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) {
443 base_address = OVERRIDE;
444 controller_type = CONTROLLER;
446 DANY ("Base address overridden to %x, controller type is %s\n",
448 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 < ARRAY_SIZE(seagate_bases); ++i) {
461 void __iomem *p = ioremap(seagate_bases[i], 0x2000);
464 for (j = 0; j < NUM_SIGNATURES; ++j)
465 if (check_signature(p + signatures[j].offset, signatures[j].signature, signatures[j].length)) {
466 base_address = seagate_bases[i];
467 controller_type = signatures[j].type;
472 #endif /* OVERRIDE */
474 /* (! controller_type) */
475 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
476 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
479 printk(KERN_INFO "seagate: ST0x/TMC-8xx not detected.\n");
483 cr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
485 st0x_cr_sr = ioremap(cr, 0x100);
486 st0x_dr = ioremap(dr, 0x100);
488 DANY("%s detected. Base address = %x, cr = %x, dr = %x\n",
489 tpnt->name, base_address, cr, dr);
492 * At all times, we will use IRQ 5. Should also check for IRQ3
493 * if we lose our first interrupt.
495 instance = scsi_register (tpnt, 0);
496 if (instance == NULL)
499 hostno = instance->host_no;
500 if (request_irq (irq, do_seagate_reconnect_intr, IRQF_DISABLED, (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", instance)) {
501 printk(KERN_ERR "scsi%d : unable to allocate IRQ%d\n", hostno, irq);
505 instance->io_port = base_address;
507 printk(KERN_INFO "Calibrating borken timer... ");
509 printk(" %d cycles per transfer\n", borken_calibration);
511 printk (KERN_INFO "This is one second... ");
514 ULOOP (1 * 1000 * 1000) {
521 printk ("done, %s options:"
540 #ifdef SEAGATE_USE_ASM
556 static const char *seagate_st0x_info (struct Scsi_Host *shpnt)
558 static char buffer[64];
560 snprintf(buffer, 64, "%s at irq %d, address 0x%05X",
561 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
567 * These are our saved pointers for the outstanding command that is
568 * waiting for a reconnect
571 static unsigned char current_target, current_lun;
572 static unsigned char *current_cmnd, *current_data;
573 static int current_nobuffs;
574 static struct scatterlist *current_buffer;
575 static int current_bufflen;
579 * linked_connected indicates whether or not we are currently connected to
580 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
581 * using linked commands.
584 static int linked_connected = 0;
585 static unsigned char linked_target, linked_lun;
588 static void (*done_fn) (Scsi_Cmnd *) = NULL;
589 static Scsi_Cmnd *SCint = NULL;
592 * These control whether or not disconnect / reconnect will be attempted,
593 * or are being attempted.
596 #define NO_RECONNECT 0
597 #define RECONNECT_NOW 1
598 #define CAN_RECONNECT 2
601 * LINKED_RIGHT indicates that we are currently connected to the correct target
602 * for this command, LINKED_WRONG indicates that we are connected to the wrong
603 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
606 #define LINKED_RIGHT 3
607 #define LINKED_WRONG 4
610 * This determines if we are expecting to reconnect or not.
613 static int should_reconnect = 0;
616 * The seagate_reconnect_intr routine is called when a target reselects the
617 * host adapter. This occurs on the interrupt triggered by the target
621 static irqreturn_t do_seagate_reconnect_intr(int irq, void *dev_id,
622 struct pt_regs *regs)
625 struct Scsi_Host *dev = dev_id;
627 spin_lock_irqsave (dev->host_lock, flags);
628 seagate_reconnect_intr (irq, dev_id, regs);
629 spin_unlock_irqrestore (dev->host_lock, flags);
633 static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
638 DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n", hostno);
640 if (!should_reconnect)
641 printk(KERN_WARNING "scsi%d: unexpected interrupt.\n", hostno);
643 should_reconnect = 0;
645 DPRINTK (PHASE_RESELECT, "scsi%d : internal_command(%d, %08x, %08x, RECONNECT_NOW\n",
646 hostno, current_target, current_data, current_bufflen);
648 temp = internal_command (current_target, current_lun, current_cmnd, current_data, current_bufflen, RECONNECT_NOW);
650 if (msg_byte(temp) != DISCONNECT) {
652 DPRINTK(PHASE_RESELECT, "scsi%d : done_fn(%d,%08x)", hostno, hostno, temp);
654 panic ("SCint == NULL in seagate");
657 SCtmp->result = temp;
660 printk(KERN_ERR "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 static 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->device->id;
686 current_lun = SCpnt->device->lun;
687 current_cmnd = SCpnt->cmnd;
688 current_data = (unsigned char *) SCpnt->request_buffer;
689 current_bufflen = SCpnt->request_bufflen;
697 * Set linked command bit in control field of SCSI command.
700 current_cmnd[SCpnt->cmd_len] |= 0x01;
701 if (linked_connected) {
702 DPRINTK (DEBUG_LINKED, "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
703 if (linked_target == current_target && linked_lun == current_lun)
705 DPRINTK(DEBUG_LINKED, "correct\n");
706 reconnect = LINKED_RIGHT;
708 DPRINTK(DEBUG_LINKED, "incorrect\n");
709 reconnect = LINKED_WRONG;
713 reconnect = CAN_RECONNECT;
715 result = internal_command(SCint->device->id, SCint->device->lun, SCint->cmnd,
716 SCint->request_buffer, SCint->request_bufflen, reconnect);
717 if (msg_byte(result) == DISCONNECT)
721 SCtmp->result = result;
729 static int internal_command (unsigned char target, unsigned char lun,
730 const void *cmnd, void *buff, int bufflen, int reselect)
732 unsigned char *data = NULL;
733 struct scatterlist *buffer = NULL;
734 int clock, temp, nobuffs = 0, done = 0, len = 0;
736 int transfered = 0, phase = 0, newphase;
738 register unsigned char status_read;
739 unsigned char tmp_data, tmp_control, status = 0, message = 0;
740 unsigned transfersize = 0, underflow = 0;
742 int borken = (int) SCint->device->borken; /* Does the current target require
749 #if (DEBUG & PRINT_COMMAND)
750 printk("scsi%d : target = %d, command = ", hostno, target);
751 __scsi_print_command((unsigned char *) cmnd);
754 #if (DEBUG & PHASE_RESELECT)
757 printk("scsi%d : reconnecting\n", hostno);
761 printk("scsi%d : connected, can reconnect\n", hostno);
764 printk("scsi%d : connected to wrong target, can reconnect\n",
769 printk("scsi%d : allowed to reconnect\n", hostno);
772 printk("scsi%d : not allowed to reconnect\n", hostno);
776 if (target == (controller_type == SEAGATE ? 7 : 6))
777 return DID_BAD_TARGET;
780 * We work it differently depending on if this is is "the first time,"
781 * or a reconnect. If this is a reselect phase, then SEL will
782 * be asserted, and we must skip selection / arbitration phases.
787 DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
789 * At this point, we should find the logical or of our ID
790 * and the original target's ID on the BUS, with BSY, SEL,
791 * and I/O signals asserted.
793 * After ARBITRATION phase is completed, only SEL, BSY,
794 * and the target ID are asserted. A valid initiator ID
795 * is not on the bus until IO is asserted, so we must wait
800 if ((temp & STAT_IO) && !(temp & STAT_BSY))
803 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
804 return (DID_BAD_INTR << 16);
809 * After I/O is asserted by the target, we can read our ID
810 * and its ID off of the BUS.
813 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
814 DPRINTK (PHASE_RESELECT, "scsi%d : detected reconnect request to different target.\n\tData bus = %d\n", hostno, temp);
815 return (DID_BAD_INTR << 16);
818 if (!(temp & (1 << current_target))) {
819 printk(KERN_WARNING "scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno, temp);
820 return (DID_BAD_INTR << 16);
823 buffer = current_buffer;
824 cmnd = current_cmnd; /* WDE add */
825 data = current_data; /* WDE add */
826 len = current_bufflen; /* WDE add */
827 nobuffs = current_nobuffs;
830 * We have determined that we have been selected. At this
831 * point, we must respond to the reselection by asserting
836 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
838 WRITE_CONTROL (BASE_CMD | CMD_BSY);
842 * The target will drop SEL, and raise BSY, at which time
847 if (!(STATUS & STAT_SEL))
850 WRITE_CONTROL (BASE_CMD | CMD_INTR);
851 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
852 return (DID_BAD_INTR << 16);
855 WRITE_CONTROL (BASE_CMD);
857 * At this point, we have connected with the target
858 * and can get on with our lives.
864 * This is a bletcherous hack, just as bad as the Unix #!
865 * interpreter stuff. If it turns out we are using the wrong
866 * I_T_L nexus, the easiest way to deal with it is to go into
867 * our INFORMATION TRANSFER PHASE code, send a ABORT
868 * message on MESSAGE OUT phase, and then loop back to here.
872 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
877 * On entry, we make sure that the BUS is in a BUS FREE
878 * phase, by insuring that both BSY and SEL are low for
879 * at least one bus settle delay. Several reads help
880 * eliminate wire glitch.
884 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
885 clock = jiffies + ST0X_BUS_FREE_DELAY;
887 while (((STATUS | STATUS | STATUS) & (STAT_BSY | STAT_SEL)) && (!st0x_aborted) && time_before (jiffies, clock))
890 if (time_after (jiffies, clock))
891 return retcode (DID_BUS_BUSY);
892 else if (st0x_aborted)
893 return retcode (st0x_aborted);
895 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
897 clock = jiffies + ST0X_SELECTION_DELAY;
900 * Arbitration/selection procedure :
902 * 2. Write HOST adapter address bit
903 * 3. Set start arbitration.
904 * 4. We get either ARBITRATION COMPLETE or SELECT at this
906 * 5. OR our ID and targets on bus.
907 * 6. Enable SCSI drivers and asserted SEL and ATTN
911 /* FIXME: verify host lock is always held here */
913 WRITE_DATA((controller_type == SEAGATE) ? 0x80 : 0x40);
914 WRITE_CONTROL(CMD_START_ARB);
916 ULOOP (ST0X_SELECTION_DELAY * 10000) {
917 status_read = STATUS;
918 if (status_read & STAT_ARB_CMPL)
920 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
922 if (TIMEOUT || (status_read & STAT_SEL)) {
923 printk(KERN_WARNING "scsi%d : arbitration lost or timeout.\n", hostno);
924 WRITE_CONTROL (BASE_CMD);
925 return retcode (DID_NO_CONNECT);
928 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
932 * When the SCSI device decides that we're gawking at it,
933 * it will respond by asserting BUSY on the bus.
935 * Note : the Seagate ST-01/02 product manual says that we
936 * should twiddle the DATA register before the control
937 * register. However, this does not work reliably so we do
938 * it the other way around.
940 * Probably could be a problem with arbitration too, we
941 * really should try this with a SCSI protocol or logic
942 * analyzer to see what is going on.
944 tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
945 tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
947 /* FIXME: verify host lock is always held here */
948 #ifdef OLDCNTDATASCEME
950 WRITE_CONTROL (tmp_control);
951 WRITE_DATA (tmp_data);
953 WRITE_DATA (tmp_data);
954 WRITE_CONTROL (tmp_control);
957 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
958 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
959 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
960 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
961 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
967 * If we have been aborted, and we have a
968 * command in progress, IE the target
969 * still has BSY asserted, then we will
970 * reset the bus, and notify the midlevel
971 * driver to expect sense.
974 WRITE_CONTROL (BASE_CMD);
975 if (STATUS & STAT_BSY) {
976 printk(KERN_WARNING "scsi%d : BST asserted after we've been aborted.\n", hostno);
977 seagate_st0x_bus_reset(NULL);
978 return retcode (DID_RESET);
980 return retcode (st0x_aborted);
982 if (STATUS & STAT_BSY)
985 DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n", hostno, target, STATUS);
986 return retcode (DID_NO_CONNECT);
990 /* Establish current pointers. Take into account scatter / gather */
992 if ((nobuffs = SCint->use_sg)) {
993 #if (DEBUG & DEBUG_SG)
996 printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno, nobuffs);
997 for (i = 0; i < nobuffs; ++i)
998 printk("scsi%d : buffer %d address = %p length = %d\n",
1000 page_address(buffer[i].page) + buffer[i].offset,
1005 buffer = (struct scatterlist *) SCint->request_buffer;
1006 len = buffer->length;
1007 data = page_address(buffer->page) + buffer->offset;
1009 DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
1011 len = SCint->request_bufflen;
1012 data = (unsigned char *) SCint->request_buffer;
1015 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1025 } /* end of switch(reselect) */
1028 * There are several conditions under which we wish to send a message :
1029 * 1. When we are allowing disconnect / reconnect, and need to
1030 * establish the I_T_L nexus via an IDENTIFY with the DiscPriv bit
1033 * 2. When we are doing linked commands, are have the wrong I_T_L
1034 * nexus established and want to send an ABORT message.
1037 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1039 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT)|| (reselect == LINKED_WRONG))? CMD_ATTN : 0));
1041 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT))? CMD_ATTN : 0));
1045 * INFORMATION TRANSFER PHASE
1047 * The nasty looking read / write inline assembler loops we use for
1048 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1049 * the 'C' versions - since we're moving 1024 bytes of data, this
1052 * SJT: The nasty-looking assembler is gone, so it's slower.
1056 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1059 transfersize = SCint->transfersize;
1060 underflow = SCint->underflow;
1063 * Now, we poll the device for status information,
1064 * and handle any requests it makes. Note that since we are unsure
1065 * of how much data will be flowing across the system, etc and
1066 * cannot make reasonable timeouts, that we will instead have the
1067 * midlevel driver handle any timeouts that occur in this phase.
1070 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1072 if (status_read & STAT_PARITY) {
1073 printk(KERN_ERR "scsi%d : got parity error\n", hostno);
1074 st0x_aborted = DID_PARITY;
1077 if (status_read & STAT_REQ) {
1078 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1079 if ((newphase = (status_read & REQ_MASK)) != phase) {
1083 printk ("scsi%d : phase = DATA OUT\n", hostno);
1086 printk ("scsi%d : phase = DATA IN\n", hostno);
1090 ("scsi%d : phase = COMMAND OUT\n", hostno);
1093 printk ("scsi%d : phase = STATUS IN\n", hostno);
1097 ("scsi%d : phase = MESSAGE OUT\n", hostno);
1100 printk ("scsi%d : phase = MESSAGE IN\n", hostno);
1103 printk ("scsi%d : phase = UNKNOWN\n", hostno);
1104 st0x_aborted = DID_ERROR;
1108 switch (status_read & REQ_MASK) {
1111 * If we are in fast mode, then we simply splat
1112 * the data out in word-sized chunks as fast as
1118 printk("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
1119 st0x_aborted = DID_ERROR;
1125 if (fast && transfersize
1126 && !(len % transfersize)
1127 && (len >= transfersize)
1129 && !(transfersize % 4)
1132 DPRINTK (DEBUG_FAST,
1133 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1134 " len = %d, data = %08x\n",
1135 hostno, SCint->underflow,
1136 SCint->transfersize, len,
1139 /* SJT: Start. Fast Write */
1140 #ifdef SEAGATE_USE_ASM
1146 "movl %%eax, (%%edi)\n\t"
1150 "movb %%al, (%%edi)\n\t"
1154 /* input */ :"D" (st0x_dr),
1157 "c" (SCint->transfersize)
1161 #else /* SEAGATE_USE_ASM */
1162 memcpy_toio(st0x_dr, data, transfersize);
1163 #endif /* SEAGATE_USE_ASM */
1165 len -= transfersize;
1166 data += transfersize;
1167 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1170 * We loop as long as we are in a
1171 * data out phase, there is data to
1172 * send, and BSY is still active.
1175 /* SJT: Start. Slow Write. */
1176 #ifdef SEAGATE_USE_ASM
1178 int __dummy_1, __dummy_2;
1181 * We loop as long as we are in a data out phase, there is data to send,
1182 * and BSY is still active.
1184 /* Local variables : len = ecx , data = esi,
1185 st0x_cr_sr = ebx, st0x_dr = edi
1188 /* Test for any data here at all. */
1189 "orl %%ecx, %%ecx\n\t"
1190 "jz 2f\n\t" "cld\n\t"
1191 /* "movl st0x_cr_sr, %%ebx\n\t" */
1192 /* "movl st0x_dr, %%edi\n\t" */
1194 "movb (%%ebx), %%al\n\t"
1198 /* Test for data out phase - STATUS & REQ_MASK should be
1199 REQ_DATAOUT, which is 0. */
1200 "test $0xe, %%al\n\t"
1203 "test $0x10, %%al\n\t"
1206 "movb %%al, (%%edi)\n\t"
1207 "loop 1b\n\t" "2:\n"
1208 /* output */ :"=S" (data), "=c" (len),
1213 : "0" (data), "1" (len),
1218 #else /* SEAGATE_USE_ASM */
1223 if (!(stat & STAT_BSY)
1224 || ((stat & REQ_MASK) !=
1227 if (stat & STAT_REQ) {
1228 WRITE_DATA (*data++);
1232 #endif /* SEAGATE_USE_ASM */
1236 if (!len && nobuffs) {
1239 len = buffer->length;
1240 data = page_address(buffer->page) + buffer->offset;
1242 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1250 #if (DEBUG & (PHASE_DATAIN))
1253 for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN | STAT_REQ); --len) {
1257 #if (DEBUG & (PHASE_DATAIN))
1263 if (fast && transfersize
1264 && !(len % transfersize)
1265 && (len >= transfersize)
1267 && !(transfersize % 4)
1270 DPRINTK (DEBUG_FAST,
1271 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1272 " len = %d, data = %08x\n",
1273 hostno, SCint->underflow,
1274 SCint->transfersize, len,
1277 /* SJT: Start. Fast Read */
1278 #ifdef SEAGATE_USE_ASM
1283 "movl (%%esi), %%eax\n\t"
1287 "movb (%%esi), %%al\n\t"
1292 /* input */ :"S" (st0x_dr),
1295 "c" (SCint->transfersize)
1299 #else /* SEAGATE_USE_ASM */
1300 memcpy_fromio(data, st0x_dr, len);
1301 #endif /* SEAGATE_USE_ASM */
1303 len -= transfersize;
1304 data += transfersize;
1305 #if (DEBUG & PHASE_DATAIN)
1306 printk ("scsi%d: transfered += %d\n", hostno, transfersize);
1307 transfered += transfersize;
1310 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1313 #if (DEBUG & PHASE_DATAIN)
1314 printk ("scsi%d: transfered += %d\n", hostno, len);
1315 transfered += len; /* Assume we'll transfer it all, then
1316 subtract what we *didn't* transfer */
1320 * We loop as long as we are in a data in phase, there is room to read,
1321 * and BSY is still active
1325 #ifdef SEAGATE_USE_ASM
1327 int __dummy_3, __dummy_4;
1329 /* Dummy clobbering variables for the new gcc-2.95 */
1332 * We loop as long as we are in a data in phase, there is room to read,
1333 * and BSY is still active
1335 /* Local variables : ecx = len, edi = data
1336 esi = st0x_cr_sr, ebx = st0x_dr */
1338 /* Test for room to read */
1339 "orl %%ecx, %%ecx\n\t"
1340 "jz 2f\n\t" "cld\n\t"
1341 /* "movl st0x_cr_sr, %%esi\n\t" */
1342 /* "movl st0x_dr, %%ebx\n\t" */
1344 "movb (%%esi), %%al\n\t"
1348 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1349 = STAT_IO, which is 4. */
1350 "movb $0xe, %%ah\n\t"
1351 "andb %%al, %%ah\n\t"
1352 "cmpb $0x04, %%ah\n\t"
1355 "test $0x10, %%al\n\t"
1357 "movb (%%ebx), %%al\n\t"
1359 "loop 1b\n\t" "2:\n"
1360 /* output */ :"=D" (data), "=c" (len),
1365 : "0" (data), "1" (len),
1370 #else /* SEAGATE_USE_ASM */
1375 if (!(stat & STAT_BSY)
1376 || ((stat & REQ_MASK) !=
1379 if (stat & STAT_REQ) {
1384 #endif /* SEAGATE_USE_ASM */
1386 #if (DEBUG & PHASE_DATAIN)
1387 printk ("scsi%d: transfered -= %d\n", hostno, len);
1388 transfered -= len; /* Since we assumed all of Len got *
1389 transfered, correct our mistake */
1393 if (!len && nobuffs) {
1396 len = buffer->length;
1397 data = page_address(buffer->page) + buffer->offset;
1398 DPRINTK (DEBUG_SG, "scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno, len, data);
1403 while (((status_read = STATUS) & STAT_BSY) &&
1404 ((status_read & REQ_MASK) == REQ_CMDOUT))
1405 if (status_read & STAT_REQ) {
1406 WRITE_DATA (*(const unsigned char *) cmnd);
1407 cmnd = 1 + (const unsigned char *)cmnd;
1421 * We can only have sent a MSG OUT if we
1422 * requested to do this by raising ATTN.
1423 * So, we must drop ATTN.
1425 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1427 * If we are reconnecting, then we must
1428 * send an IDENTIFY message in response
1433 WRITE_DATA (IDENTIFY (1, lun));
1434 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
1439 linked_connected = 0;
1440 reselect = CAN_RECONNECT;
1442 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED, "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
1444 DPRINTK (DEBUG_LINKED, "correct\n");
1447 printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1452 switch (message = DATA) {
1454 DANY("seagate: deciding to disconnect\n");
1455 should_reconnect = 1;
1456 current_data = data; /* WDE add */
1457 current_buffer = buffer;
1458 current_bufflen = len; /* WDE add */
1459 current_nobuffs = nobuffs;
1461 linked_connected = 0;
1464 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
1468 case LINKED_CMD_COMPLETE:
1469 case LINKED_FLG_CMD_COMPLETE:
1471 case COMMAND_COMPLETE:
1473 * Note : we should check for underflow here.
1475 DPRINTK(PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
1479 DPRINTK(PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
1483 current_buffer = buffer;
1484 current_bufflen = len; /* WDE add */
1485 current_data = data; /* WDE mod */
1486 current_nobuffs = nobuffs;
1487 DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
1489 case RESTORE_POINTERS:
1490 buffer = current_buffer;
1491 cmnd = current_cmnd;
1492 data = current_data; /* WDE mod */
1493 len = current_bufflen;
1494 nobuffs = current_nobuffs;
1495 DPRINTK(PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
1500 * IDENTIFY distinguishes itself
1501 * from the other messages by
1502 * setting the high bit.
1504 * Note : we need to handle at
1505 * least one outstanding command
1506 * per LUN, and need to hash the
1507 * SCSI command for that I_T_L
1508 * nexus based on the known ID
1509 * (at this point) and LUN.
1512 if (message & 0x80) {
1513 DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n", hostno, target, message & 7);
1516 * We should go into a
1517 * MESSAGE OUT phase, and
1518 * send a MESSAGE_REJECT
1519 * if we run into a message
1520 * that we don't like. The
1521 * seagate driver needs
1523 * restructuring first
1526 DPRINTK (PHASE_MSGIN, "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
1531 printk(KERN_ERR "scsi%d : unknown phase.\n", hostno);
1532 st0x_aborted = DID_ERROR;
1533 } /* end of switch (status_read & REQ_MASK) */
1536 * I really don't care to deal with borken devices in
1537 * each single byte transfer case (ie, message in,
1538 * message out, status), so I'll do the wait here if
1545 } /* if(status_read & STAT_REQ) ends */
1546 } /* while(((status_read = STATUS)...) ends */
1548 DPRINTK(PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT, "scsi%d : Transfered %d bytes\n", hostno, transfered);
1550 #if (DEBUG & PHASE_EXIT)
1551 #if 0 /* Doesn't work for scatter/gather */
1552 printk("Buffer : \n");
1553 for(i = 0; i < 20; ++i)
1554 printk("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1557 printk("scsi%d : status = ", hostno);
1558 scsi_print_status(status);
1559 printk(" message = %02x\n", message);
1562 /* We shouldn't reach this until *after* BSY has been deasserted */
1568 * Fix the message byte so that unsuspecting high level drivers
1569 * don't puke when they see a LINKED COMMAND message in place of
1570 * the COMMAND COMPLETE they may be expecting. Shouldn't be
1571 * necessary, but it's better to be on the safe side.
1573 * A non LINKED* message byte will indicate that the command
1574 * completed, and we are now disconnected.
1578 case LINKED_CMD_COMPLETE:
1579 case LINKED_FLG_CMD_COMPLETE:
1580 message = COMMAND_COMPLETE;
1581 linked_target = current_target;
1582 linked_lun = current_lun;
1583 linked_connected = 1;
1584 DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established for linked command.\n", hostno);
1585 /* We also will need to adjust status to accommodate intermediate
1587 if ((status == INTERMEDIATE_GOOD) || (status == INTERMEDIATE_C_GOOD))
1591 * We should also handle what are "normal" termination
1592 * messages here (ABORT, BUS_DEVICE_RESET?, and
1593 * COMMAND_COMPLETE individually, and flake if things
1597 DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
1598 linked_connected = 0;
1603 if (should_reconnect) {
1604 DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n", hostno);
1605 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1607 WRITE_CONTROL (BASE_CMD);
1609 return retcode (st0x_aborted);
1610 } /* end of internal_command */
1612 static int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1614 st0x_aborted = DID_ABORT;
1622 * the seagate_st0x_reset function resets the SCSI bus
1624 * May be called with SCpnt = NULL
1627 static int seagate_st0x_bus_reset(Scsi_Cmnd * SCpnt)
1629 /* No timeouts - this command is going to fail because it was reset. */
1630 DANY ("scsi%d: Reseting bus... ", hostno);
1632 /* assert RESET signal on SCSI bus. */
1633 WRITE_CONTROL (BASE_CMD | CMD_RST);
1637 WRITE_CONTROL (BASE_CMD);
1638 st0x_aborted = DID_RESET;
1644 static int seagate_st0x_release(struct Scsi_Host *shost)
1647 free_irq(shost->irq, shost);
1648 release_region(shost->io_port, shost->n_io_port);
1652 static struct scsi_host_template driver_template = {
1653 .detect = seagate_st0x_detect,
1654 .release = seagate_st0x_release,
1655 .info = seagate_st0x_info,
1656 .queuecommand = seagate_st0x_queue_command,
1657 .eh_abort_handler = seagate_st0x_abort,
1658 .eh_bus_reset_handler = seagate_st0x_bus_reset,
1661 .sg_tablesize = SG_ALL,
1663 .use_clustering = DISABLE_CLUSTERING,
1665 #include "scsi_module.c"