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>
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 #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 unsigned long st0x_cr_sr; /* control register write, status
241 register read. 256 bytes in
243 Read is status of SCSI BUS, as per
246 static unsigned long 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_PARM (base_address, "i");
258 MODULE_PARM (controller_type, "b");
259 MODULE_PARM (irq, "i");
260 MODULE_LICENSE("GPL");
263 #define retcode(result) (((result) << 16) | (message << 8) | status)
264 #define STATUS ((u8) isa_readb(st0x_cr_sr))
265 #define DATA ((u8) isa_readb(st0x_dr))
266 #define WRITE_CONTROL(d) { isa_writeb((d), st0x_cr_sr); }
267 #define WRITE_DATA(d) { isa_writeb((d), st0x_dr); }
270 st0x_setup (char *str, int *ints)
272 controller_type = SEAGATE;
273 base_address = ints[1];
278 tmc8xx_setup (char *str, int *ints)
280 controller_type = FD;
281 base_address = ints[1];
286 static unsigned int seagate_bases[] = {
287 0xc8000, 0xca000, 0xcc000,
288 0xce000, 0xdc000, 0xde000
292 const unsigned char *signature;
298 static Signature __initdata signatures[] = {
299 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
300 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
303 * The following two lines are NOT mistakes. One detects ROM revision
304 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
305 * and this is not going to change, the "SEAGATE" and "SCSI" together
306 * are probably "good enough"
309 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
310 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
313 * However, future domain makes several incompatible SCSI boards, so specific
314 * signatures must be used.
317 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
318 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
319 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
320 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
321 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
322 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
323 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
324 {"FUTURE DOMAIN TMC-950", 5, 21, FD},
325 /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
326 {"IBM F1 V1.2009/22/93", 5, 25, FD},
329 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
330 #endif /* n OVERRIDE */
333 * hostno stores the hostnumber, as told to us by the init routine.
336 static int hostno = -1;
337 static void seagate_reconnect_intr (int, void *, struct pt_regs *);
338 static void do_seagate_reconnect_intr (int, void *, struct pt_regs *);
348 * Support for broken devices :
349 * The Seagate board has a handshaking problem. Namely, a lack
350 * thereof for slow devices. You can blast 600K/second through
351 * it if you are polling for each byte, more if you do a blind
352 * transfer. In the first case, with a fast device, REQ will
353 * transition high-low or high-low-high before your loop restarts
354 * and you'll have no problems. In the second case, the board
355 * will insert wait states for up to 13.2 usecs for REQ to
356 * transition low->high, and everything will work.
358 * However, there's nothing in the state machine that says
359 * you *HAVE* to see a high-low-high set of transitions before
360 * sending the next byte, and slow things like the Trantor CD ROMS
361 * will break because of this.
363 * So, we need to slow things down, which isn't as simple as it
364 * seems. We can't slow things down period, because then people
365 * who don't recompile their kernels will shoot me for ruining
366 * their performance. We need to do it on a case per case basis.
368 * The best for performance will be to, only for borken devices
369 * (this is stored on a per-target basis in the scsi_devices array)
371 * Wait for a low->high transition before continuing with that
372 * transfer. If we timeout, continue anyways. We don't need
373 * a long timeout, because REQ should only be asserted until the
374 * corresponding ACK is received and processed.
376 * Note that we can't use the system timer for this, because of
377 * resolution, and we *really* can't use the timer chip since
378 * gettimeofday() and the beeper routines use that. So,
379 * the best thing for us to do will be to calibrate a timing
380 * loop in the initialization code using the timer chip before
381 * gettimeofday() can screw with it.
383 * FIXME: this is broken (not borken :-). Empty loop costs less than
384 * loop with ISA access in it! -- pavel@ucw.cz
387 static int borken_calibration = 0;
389 static void __init borken_init (void)
391 register int count = 0, start = jiffies + 1, stop = start + 25;
393 while (time_before (jiffies, start)) ;
394 for (; time_before (jiffies, stop); ++count) ;
397 * Ok, we now have a count for .25 seconds. Convert to a
398 * count per second and divide by transfer rate in K. */
400 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
402 if (borken_calibration < 1)
403 borken_calibration = 1;
406 static inline void borken_wait (void)
410 for (count = borken_calibration; count && (STATUS & STAT_REQ);
412 #if (DEBUG & DEBUG_BORKEN)
414 printk ("scsi%d : borken timeout\n", hostno);
418 #endif /* def SLOW_RATE */
420 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
421 * contains at least one ISA access, which takes more than 0.125
422 * usec. So if we loop 8 times time in usec, we are safe.
425 #define ULOOP( i ) for (clock = i*8;;)
426 #define TIMEOUT (!(clock--))
428 int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
430 struct Scsi_Host *instance;
433 tpnt->proc_name = "seagate";
435 * First, we try for the manual override.
437 DANY ("Autodetecting ST0x / TMC-8xx\n");
440 printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
444 /* If the user specified the controller type from the command line,
445 controller_type will be non-zero, so don't try to detect one */
447 if (!controller_type) {
449 base_address = OVERRIDE;
450 controller_type = CONTROLLER;
452 DANY ("Base address overridden to %x, controller type is %s\n",
454 controller_type == SEAGATE ? "SEAGATE" : "FD");
457 * To detect this card, we simply look for the signature
458 * from the BIOS version notice in all the possible locations
459 * of the ROM's. This has a nice side effect of not trashing
460 * any register locations that might be used by something else.
462 * XXX - note that we probably should be probing the address
463 * space for the on-board RAM instead.
467 i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i)
469 for (j = 0; !base_address && j < NUM_SIGNATURES; ++j)
470 if (isa_check_signature
471 (seagate_bases[i] + signatures[j].offset,
472 signatures[j].signature,
473 signatures[j].length)) {
474 base_address = seagate_bases[i];
475 controller_type = signatures[j].type;
477 #endif /* OVERRIDE */
479 /* (! controller_type) */
480 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
481 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
484 DANY ("ST0x / TMC-8xx not detected.\n");
489 base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
490 st0x_dr = st0x_cr_sr + 0x200;
492 DANY ("%s detected. Base address = %x, cr = %x, dr = %x\n",
493 tpnt->name, base_address, st0x_cr_sr, st0x_dr);
496 * At all times, we will use IRQ 5. Should also check for IRQ3 if we
497 * loose our first interrupt.
499 instance = scsi_register (tpnt, 0);
500 if (instance == NULL)
503 hostno = instance->host_no;
504 if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT,
505 (controller_type == SEAGATE) ? "seagate" : "tmc-8xx",
507 printk ("scsi%d : unable to allocate IRQ%d\n", hostno, irq);
511 instance->io_port = base_address;
513 printk (KERN_INFO "Calibrating borken timer... ");
515 printk (" %d cycles per transfer\n", borken_calibration);
518 printk (KERN_INFO "This is one second... ");
521 ULOOP (1 * 1000 * 1000) {
528 printk ("done, %s options:"
547 #ifdef SEAGATE_USE_ASM
564 seagate_st0x_info (struct Scsi_Host *shpnt)
566 static char buffer[64];
568 sprintf (buffer, "%s at irq %d, address 0x%05X",
569 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
575 * These are our saved pointers for the outstanding command that is
576 * waiting for a reconnect
579 static unsigned char current_target, current_lun;
580 static unsigned char *current_cmnd, *current_data;
581 static int current_nobuffs;
582 static struct scatterlist *current_buffer;
583 static int current_bufflen;
587 * linked_connected indicates whether or not we are currently connected to
588 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
589 * using linked commands.
592 static int linked_connected = 0;
593 static unsigned char linked_target, linked_lun;
596 static void (*done_fn) (Scsi_Cmnd *) = NULL;
597 static Scsi_Cmnd *SCint = NULL;
600 * These control whether or not disconnect / reconnect will be attempted,
601 * or are being attempted.
604 #define NO_RECONNECT 0
605 #define RECONNECT_NOW 1
606 #define CAN_RECONNECT 2
609 * LINKED_RIGHT indicates that we are currently connected to the correct target
610 * for this command, LINKED_WRONG indicates that we are connected to the wrong
611 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
614 #define LINKED_RIGHT 3
615 #define LINKED_WRONG 4
618 * This determines if we are expecting to reconnect or not.
621 static int should_reconnect = 0;
624 * The seagate_reconnect_intr routine is called when a target reselects the
625 * host adapter. This occurs on the interrupt triggered by the target
629 static void do_seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
633 spin_lock_irqsave (&io_request_lock, flags);
634 seagate_reconnect_intr (irq, dev_id, regs);
635 spin_unlock_irqrestore (&io_request_lock, flags);
638 static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
643 DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n",
646 if (!should_reconnect)
647 printk ("scsi%d: unexpected interrupt.\n", hostno);
649 should_reconnect = 0;
651 DPRINTK (PHASE_RESELECT, "scsi%d : internal_command("
652 "%d, %08x, %08x, RECONNECT_NOW\n", hostno,
653 current_target, current_data, current_bufflen);
656 internal_command (current_target, current_lun, current_cmnd,
657 current_data, current_bufflen,
660 if (msg_byte (temp) != DISCONNECT) {
662 DPRINTK (PHASE_RESELECT,
663 "scsi%d : done_fn(%d,%08x)", hostno,
666 panic ("SCint == NULL in seagate");
669 SCtmp->result = temp;
672 printk ("done_fn() not defined.\n");
678 * The seagate_st0x_queue_command() function provides a queued interface
679 * to the seagate SCSI driver. Basically, it just passes control onto the
680 * seagate_command() function, after fixing it so that the done_fn()
681 * is set to the one passed to the function. We have to be very careful,
682 * because there are some commands on some devices that do not disconnect,
683 * and if we simply call the done_fn when the command is done then another
684 * command is started and queue_command is called again... We end up
685 * overflowing the kernel stack, and this tends not to be such a good idea.
688 static int recursion_depth = 0;
690 int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
692 int result, reconnect;
695 DANY ("seagate: que_command");
697 current_target = SCpnt->target;
698 current_lun = SCpnt->lun;
699 (const void *) current_cmnd = SCpnt->cmnd;
700 current_data = (unsigned char *) SCpnt->request_buffer;
701 current_bufflen = SCpnt->request_bufflen;
709 * Set linked command bit in control field of SCSI command.
712 current_cmnd[SCpnt->cmd_len] |= 0x01;
713 if (linked_connected) {
714 DPRINTK (DEBUG_LINKED,
715 "scsi%d : using linked commands, current I_T_L nexus is ",
717 if ((linked_target == current_target)
718 && (linked_lun == current_lun)) {
719 DPRINTK (DEBUG_LINKED, "correct\n");
720 reconnect = LINKED_RIGHT;
722 DPRINTK (DEBUG_LINKED, "incorrect\n");
723 reconnect = LINKED_WRONG;
727 reconnect = CAN_RECONNECT;
730 internal_command (SCint->target, SCint->lun, SCint->cmnd,
731 SCint->request_buffer,
732 SCint->request_bufflen, reconnect);
733 if (msg_byte (result) == DISCONNECT)
737 SCtmp->result = result;
745 int seagate_st0x_command (Scsi_Cmnd * SCpnt)
747 return internal_command (SCpnt->target, SCpnt->lun, SCpnt->cmnd,
748 SCpnt->request_buffer, SCpnt->request_bufflen,
752 static int internal_command (unsigned char target, unsigned char lun,
753 const void *cmnd, void *buff, int bufflen, int reselect)
755 unsigned char *data = NULL;
756 struct scatterlist *buffer = NULL;
757 int clock, temp, nobuffs = 0, done = 0, len = 0;
761 int transfered = 0, phase = 0, newphase;
764 register unsigned char status_read;
765 unsigned char tmp_data, tmp_control, status = 0, message = 0;
767 unsigned transfersize = 0, underflow = 0;
770 int borken = (int) SCint->device->borken; /* Does the current target require
777 #if (DEBUG & PRINT_COMMAND)
778 printk ("scsi%d : target = %d, command = ", hostno, target);
779 print_command ((unsigned char *) cmnd);
782 #if (DEBUG & PHASE_RESELECT)
785 printk ("scsi%d : reconnecting\n", hostno);
789 printk ("scsi%d : connected, can reconnect\n", hostno);
792 printk ("scsi%d : connected to wrong target, can reconnect\n",
797 printk ("scsi%d : allowed to reconnect\n", hostno);
800 printk ("scsi%d : not allowed to reconnect\n", hostno);
804 if (target == (controller_type == SEAGATE ? 7 : 6))
805 return DID_BAD_TARGET;
808 * We work it differently depending on if this is is "the first time,"
809 * or a reconnect. If this is a reselect phase, then SEL will
810 * be asserted, and we must skip selection / arbitration phases.
815 DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
818 * At this point, we should find the logical or of our ID and the original
819 * target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
821 * After ARBITRATION phase is completed, only SEL, BSY, and the
822 * target ID are asserted. A valid initiator ID is not on the bus
823 * until IO is asserted, so we must wait for that.
827 if ((temp & STAT_IO) && !(temp & STAT_BSY))
831 DPRINTK (PHASE_RESELECT,
832 "scsi%d : RESELECT timed out while waiting for IO .\n",
834 return (DID_BAD_INTR << 16);
839 * After I/O is asserted by the target, we can read our ID and its
845 DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
846 DPRINTK (PHASE_RESELECT,
847 "scsi%d : detected reconnect request to different target.\n"
848 "\tData bus = %d\n", hostno, temp);
849 return (DID_BAD_INTR << 16);
852 if (!(temp & (1 << current_target))) {
854 ("scsi%d : Unexpected reselect interrupt. Data bus = %d\n",
856 return (DID_BAD_INTR << 16);
859 buffer = current_buffer;
860 cmnd = current_cmnd; /* WDE add */
861 data = current_data; /* WDE add */
862 len = current_bufflen; /* WDE add */
863 nobuffs = current_nobuffs;
866 * We have determined that we have been selected. At this point,
867 * we must respond to the reselection by asserting BSY ourselves
871 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
873 WRITE_CONTROL (BASE_CMD | CMD_BSY);
877 * The target will drop SEL, and raise BSY, at which time we must drop
882 if (!(STATUS & STAT_SEL))
885 WRITE_CONTROL (BASE_CMD | CMD_INTR);
886 DPRINTK (PHASE_RESELECT,
887 "scsi%d : RESELECT timed out while waiting for SEL.\n",
889 return (DID_BAD_INTR << 16);
893 WRITE_CONTROL (BASE_CMD);
896 * At this point, we have connected with the target and can get
904 * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
905 * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
906 * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT
907 * message on MESSAGE OUT phase, and then loop back to here.
914 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n",
920 * On entry, we make sure that the BUS is in a BUS FREE
921 * phase, by insuring that both BSY and SEL are low for
922 * at least one bus settle delay. Several reads help
923 * eliminate wire glitch.
927 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
928 clock = jiffies + ST0X_BUS_FREE_DELAY;
930 while (((STATUS | STATUS | STATUS) &
931 (STAT_BSY | STAT_SEL)) &&
932 (!st0x_aborted) && time_before (jiffies, clock)) ;
934 if (time_after (jiffies, clock))
935 return retcode (DID_BUS_BUSY);
936 else if (st0x_aborted)
937 return retcode (st0x_aborted);
940 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n",
943 clock = jiffies + ST0X_SELECTION_DELAY;
946 * Arbitration/selection procedure :
948 * 2. Write HOST adapter address bit
949 * 3. Set start arbitration.
950 * 4. We get either ARBITRATION COMPLETE or SELECT at this
952 * 5. OR our ID and targets on bus.
953 * 6. Enable SCSI drivers and asserted SEL and ATTN
960 WRITE_DATA ((controller_type == SEAGATE) ? 0x80 : 0x40);
961 WRITE_CONTROL (CMD_START_ARB);
962 restore_flags (flags);
964 ULOOP (ST0X_SELECTION_DELAY * 10000) {
965 status_read = STATUS;
966 if (status_read & STAT_ARB_CMPL)
968 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
970 if (TIMEOUT || (status_read & STAT_SEL)) {
972 ("scsi%d : arbitration lost or timeout.\n",
974 WRITE_CONTROL (BASE_CMD);
975 return retcode (DID_NO_CONNECT);
979 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n",
984 * When the SCSI device decides that we're gawking at it, it will
985 * respond by asserting BUSY on the bus.
987 * Note : the Seagate ST-01/02 product manual says that we should
988 * twiddle the DATA register before the control register. However,
989 * this does not work reliably so we do it the other way around.
991 * Probably could be a problem with arbitration too, we really should
992 * try this with a SCSI protocol or logic analyzer to see what is
996 (unsigned char) ((1 << target) |
998 SEAGATE ? 0x80 : 0x40));
1000 BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN
1005 #ifdef OLDCNTDATASCEME
1007 WRITE_CONTROL (tmp_control);
1008 WRITE_DATA (tmp_data);
1010 WRITE_DATA (tmp_data);
1011 WRITE_CONTROL (tmp_control);
1014 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
1015 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
1016 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
1017 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
1018 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
1021 restore_flags (flags);
1023 ULOOP (250 * 1000) {
1026 * If we have been aborted, and we have a command in progress, IE the
1027 * target still has BSY asserted, then we will reset the bus, and
1028 * notify the midlevel driver to expect sense.
1031 WRITE_CONTROL (BASE_CMD);
1032 if (STATUS & STAT_BSY) {
1034 ("scsi%d : BST asserted after we've been aborted.\n",
1036 seagate_st0x_reset (NULL, 0);
1037 return retcode (DID_RESET);
1039 return retcode (st0x_aborted);
1041 if (STATUS & STAT_BSY)
1044 DPRINTK (PHASE_SELECTION,
1045 "scsi%d : NO CONNECT with target %d, stat = %x \n",
1046 hostno, target, STATUS);
1047 return retcode (DID_NO_CONNECT);
1051 /* Establish current pointers. Take into account scatter / gather */
1053 if ((nobuffs = SCint->use_sg)) {
1054 #if (DEBUG & DEBUG_SG)
1059 ("scsi%d : scatter gather requested, using %d buffers.\n",
1061 for (i = 0; i < nobuffs; ++i)
1063 ("scsi%d : buffer %d address = %08x length = %d\n",
1064 hostno, i, buffer[i].address,
1069 buffer = (struct scatterlist *) SCint->buffer;
1070 len = buffer->length;
1071 data = (unsigned char *) buffer->address;
1074 "scsi%d : scatter gather not requested.\n",
1077 len = SCint->request_bufflen;
1078 data = (unsigned char *) SCint->request_buffer;
1081 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1091 } /* end of switch(reselect) */
1094 * There are several conditions under which we wish to send a message :
1095 * 1. When we are allowing disconnect / reconnect, and need to establish
1096 * the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
1098 * 2. When we are doing linked commands, are have the wrong I_T_L nexus
1099 * established and want to send an ABORT message.
1102 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1104 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1105 (((reselect == CAN_RECONNECT)
1106 || (reselect == LINKED_WRONG)
1109 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1110 (((reselect == CAN_RECONNECT)
1115 * INFORMATION TRANSFER PHASE
1117 * The nasty looking read / write inline assembler loops we use for
1118 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1119 * the 'C' versions - since we're moving 1024 bytes of data, this
1122 * SJT: The nasty-looking assembler is gone, so it's slower.
1126 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1129 transfersize = SCint->transfersize;
1130 underflow = SCint->underflow;
1133 * Now, we poll the device for status information,
1134 * and handle any requests it makes. Note that since we are unsure of
1135 * how much data will be flowing across the system, etc and cannot
1136 * make reasonable timeouts, that we will instead have the midlevel
1137 * driver handle any timeouts that occur in this phase.
1140 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1142 if (status_read & STAT_PARITY) {
1143 printk ("scsi%d : got parity error\n", hostno);
1144 st0x_aborted = DID_PARITY;
1148 if (status_read & STAT_REQ) {
1149 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1150 if ((newphase = (status_read & REQ_MASK)) != phase) {
1154 printk ("scsi%d : phase = DATA OUT\n",
1158 printk ("scsi%d : phase = DATA IN\n",
1163 ("scsi%d : phase = COMMAND OUT\n",
1167 printk ("scsi%d : phase = STATUS IN\n",
1172 ("scsi%d : phase = MESSAGE OUT\n",
1176 printk ("scsi%d : phase = MESSAGE IN\n",
1180 printk ("scsi%d : phase = UNKNOWN\n",
1182 st0x_aborted = DID_ERROR;
1186 switch (status_read & REQ_MASK) {
1189 * If we are in fast mode, then we simply splat the data out
1190 * in word-sized chunks as fast as we can.
1196 ("scsi%d: underflow to target %d lun %d \n",
1197 hostno, target, lun);
1198 st0x_aborted = DID_ERROR;
1204 if (fast && transfersize
1205 && !(len % transfersize)
1206 && (len >= transfersize)
1208 && !(transfersize % 4)
1211 DPRINTK (DEBUG_FAST,
1212 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1213 " len = %d, data = %08x\n",
1214 hostno, SCint->underflow,
1215 SCint->transfersize, len,
1218 /* SJT: Start. Fast Write */
1219 #ifdef SEAGATE_USE_ASM
1225 "movl %%eax, (%%edi)\n\t"
1229 "movb %%al, (%%edi)\n\t"
1233 /* input */ :"D" (phys_to_virt (st0x_dr)),
1236 "c" (SCint->transfersize)
1240 #else /* SEAGATE_USE_ASM */
1244 phys_to_virt (st0x_dr);
1245 const unsigned int *dp =
1246 (unsigned int *) data;
1247 int xferlen = transfersize >> 2;
1249 unsigned char *iop =
1250 phys_to_virt (st0x_dr);
1251 const unsigned char *dp = data;
1252 int xferlen = transfersize;
1254 for (; xferlen; --xferlen)
1257 #endif /* SEAGATE_USE_ASM */
1259 len -= transfersize;
1260 data += transfersize;
1261 DPRINTK (DEBUG_FAST,
1262 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1266 * We loop as long as we are in a data out phase, there is data to send,
1267 * and BSY is still active.
1270 /* SJT: Start. Slow Write. */
1271 #ifdef SEAGATE_USE_ASM
1273 int __dummy_1, __dummy_2;
1276 * We loop as long as we are in a data out phase, there is data to send,
1277 * and BSY is still active.
1279 /* Local variables : len = ecx , data = esi,
1280 st0x_cr_sr = ebx, st0x_dr = edi
1283 /* Test for any data here at all. */
1284 "orl %%ecx, %%ecx\n\t"
1285 "jz 2f\n\t" "cld\n\t"
1286 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%ebx\n\t" */
1287 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%edi\n\t" */
1289 "movb (%%ebx), %%al\n\t"
1293 /* Test for data out phase - STATUS & REQ_MASK should be
1294 REQ_DATAOUT, which is 0. */
1295 "test $0xe, %%al\n\t"
1298 "test $0x10, %%al\n\t"
1301 "movb %%al, (%%edi)\n\t"
1302 "loop 1b\n\t" "2:\n"
1303 /* output */ :"=S" (data), "=c" (len),
1308 : "0" (data), "1" (len),
1315 #else /* SEAGATE_USE_ASM */
1320 if (!(stat & STAT_BSY)
1321 || ((stat & REQ_MASK) !=
1324 if (stat & STAT_REQ) {
1325 WRITE_DATA (*data++);
1329 #endif /* SEAGATE_USE_ASM */
1333 if (!len && nobuffs) {
1336 len = buffer->length;
1338 (unsigned char *) buffer->address;
1340 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1348 #if (DEBUG & (PHASE_DATAIN))
1353 && (STATUS & (REQ_MASK | STAT_REQ))
1354 == (REQ_DATAIN | STAT_REQ);
1359 #if (DEBUG & (PHASE_DATAIN))
1365 if (fast && transfersize
1366 && !(len % transfersize)
1367 && (len >= transfersize)
1369 && !(transfersize % 4)
1372 DPRINTK (DEBUG_FAST,
1373 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1374 " len = %d, data = %08x\n",
1375 hostno, SCint->underflow,
1376 SCint->transfersize, len,
1379 /* SJT: Start. Fast Read */
1380 #ifdef SEAGATE_USE_ASM
1385 "movl (%%esi), %%eax\n\t"
1389 "movb (%%esi), %%al\n\t"
1394 /* input */ :"S" (phys_to_virt (st0x_dr)),
1397 "c" (SCint->transfersize)
1401 #else /* SEAGATE_USE_ASM */
1404 const unsigned int *iop =
1405 phys_to_virt (st0x_dr);
1407 (unsigned int *) data;
1408 int xferlen = len >> 2;
1410 const unsigned char *iop =
1411 phys_to_virt (st0x_dr);
1412 unsigned char *dp = data;
1415 for (; xferlen; --xferlen)
1418 #endif /* SEAGATE_USE_ASM */
1420 len -= transfersize;
1421 data += transfersize;
1422 #if (DEBUG & PHASE_DATAIN)
1423 printk ("scsi%d: transfered += %d\n",
1424 hostno, transfersize);
1425 transfered += transfersize;
1428 DPRINTK (DEBUG_FAST,
1429 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1433 #if (DEBUG & PHASE_DATAIN)
1434 printk ("scsi%d: transfered += %d\n",
1436 transfered += len; /* Assume we'll transfer it all, then
1437 subtract what we *didn't* transfer */
1441 * We loop as long as we are in a data in phase, there is room to read,
1442 * and BSY is still active
1446 #ifdef SEAGATE_USE_ASM
1448 int __dummy_3, __dummy_4;
1450 /* Dummy clobbering variables for the new gcc-2.95 */
1453 * We loop as long as we are in a data in phase, there is room to read,
1454 * and BSY is still active
1456 /* Local variables : ecx = len, edi = data
1457 esi = st0x_cr_sr, ebx = st0x_dr */
1459 /* Test for room to read */
1460 "orl %%ecx, %%ecx\n\t"
1461 "jz 2f\n\t" "cld\n\t"
1462 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%esi\n\t" */
1463 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%ebx\n\t" */
1465 "movb (%%esi), %%al\n\t"
1469 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1470 = STAT_IO, which is 4. */
1471 "movb $0xe, %%ah\n\t"
1472 "andb %%al, %%ah\n\t"
1473 "cmpb $0x04, %%ah\n\t"
1476 "test $0x10, %%al\n\t"
1478 "movb (%%ebx), %%al\n\t"
1480 "loop 1b\n\t" "2:\n"
1481 /* output */ :"=D" (data), "=c" (len),
1486 : "0" (data), "1" (len),
1493 #else /* SEAGATE_USE_ASM */
1498 if (!(stat & STAT_BSY)
1499 || ((stat & REQ_MASK) !=
1502 if (stat & STAT_REQ) {
1507 #endif /* SEAGATE_USE_ASM */
1509 #if (DEBUG & PHASE_DATAIN)
1510 printk ("scsi%d: transfered -= %d\n",
1512 transfered -= len; /* Since we assumed all of Len got *
1513 transfered, correct our mistake */
1517 if (!len && nobuffs) {
1520 len = buffer->length;
1522 (unsigned char *) buffer->address;
1524 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1531 while (((status_read = STATUS) & STAT_BSY) &&
1532 ((status_read & REQ_MASK) == REQ_CMDOUT))
1533 if (status_read & STAT_REQ) {
1535 (const unsigned char
1539 (const unsigned char *)
1554 * We can only have sent a MSG OUT if we requested to do this
1555 * by raising ATTN. So, we must drop ATTN.
1558 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1560 * If we are reconnecting, then we must send an IDENTIFY message in
1561 * response to MSGOUT.
1565 WRITE_DATA (IDENTIFY (1, lun));
1567 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT,
1568 "scsi%d : sent IDENTIFY message.\n",
1574 linked_connected = 0;
1575 reselect = CAN_RECONNECT;
1577 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED,
1578 "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n",
1581 DPRINTK (DEBUG_LINKED, "correct\n");
1585 ("scsi%d : target %d requested MSGOUT, sent NOP message.\n",
1591 switch (message = DATA) {
1593 DANY ("seagate: deciding to disconnect\n");
1594 should_reconnect = 1;
1595 current_data = data; /* WDE add */
1596 current_buffer = buffer;
1597 current_bufflen = len; /* WDE add */
1598 current_nobuffs = nobuffs;
1600 linked_connected = 0;
1603 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN),
1604 "scsi%d : disconnected.\n",
1609 case LINKED_CMD_COMPLETE:
1610 case LINKED_FLG_CMD_COMPLETE:
1612 case COMMAND_COMPLETE:
1614 * Note : we should check for underflow here.
1616 DPRINTK (PHASE_MSGIN,
1617 "scsi%d : command complete.\n",
1622 DPRINTK (PHASE_MSGIN,
1623 "scsi%d : abort message.\n",
1628 current_buffer = buffer;
1629 current_bufflen = len; /* WDE add */
1630 current_data = data; /* WDE mod */
1631 current_nobuffs = nobuffs;
1632 DPRINTK (PHASE_MSGIN,
1633 "scsi%d : pointers saved.\n",
1636 case RESTORE_POINTERS:
1637 buffer = current_buffer;
1638 cmnd = current_cmnd;
1639 data = current_data; /* WDE mod */
1640 len = current_bufflen;
1641 nobuffs = current_nobuffs;
1642 DPRINTK (PHASE_MSGIN,
1643 "scsi%d : pointers restored.\n",
1649 * IDENTIFY distinguishes itself from the other messages by setting the
1650 * high byte. [FIXME: should not this read "the high bit"? - pavel@ucw.cz]
1652 * Note : we need to handle at least one outstanding command per LUN,
1653 * and need to hash the SCSI command for that I_T_L nexus based on the
1654 * known ID (at this point) and LUN.
1657 if (message & 0x80) {
1658 DPRINTK (PHASE_MSGIN,
1659 "scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1665 * We should go into a MESSAGE OUT phase, and send a MESSAGE_REJECT
1666 * if we run into a message that we don't like. The seagate driver
1667 * needs some serious restructuring first though.
1670 DPRINTK (PHASE_MSGIN,
1671 "scsi%d : unknown message %d from target %d.\n",
1679 printk ("scsi%d : unknown phase.\n", hostno);
1680 st0x_aborted = DID_ERROR;
1681 } /* end of switch (status_read &
1686 * I really don't care to deal with borken devices in each single
1687 * byte transfer case (ie, message in, message out, status), so
1688 * I'll do the wait here if necessary.
1694 } /* if(status_read & STAT_REQ) ends */
1695 } /* while(((status_read = STATUS)...)
1698 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT,
1699 "scsi%d : Transfered %d bytes\n", hostno, transfered);
1701 #if (DEBUG & PHASE_EXIT)
1702 #if 0 /* Doesn't work for scatter/gather */
1703 printk ("Buffer : \n");
1704 for (i = 0; i < 20; ++i)
1705 printk ("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1708 printk ("scsi%d : status = ", hostno);
1709 print_status (status);
1710 printk ("message = %02x\n", message);
1713 /* We shouldn't reach this until *after* BSY has been deasserted */
1719 * Fix the message byte so that unsuspecting high level drivers don't
1720 * puke when they see a LINKED COMMAND message in place of the COMMAND
1721 * COMPLETE they may be expecting. Shouldn't be necessary, but it's
1722 * better to be on the safe side.
1724 * A non LINKED* message byte will indicate that the command completed,
1725 * and we are now disconnected.
1729 case LINKED_CMD_COMPLETE:
1730 case LINKED_FLG_CMD_COMPLETE:
1731 message = COMMAND_COMPLETE;
1732 linked_target = current_target;
1733 linked_lun = current_lun;
1734 linked_connected = 1;
1735 DPRINTK (DEBUG_LINKED,
1736 "scsi%d : keeping I_T_L nexus established"
1737 "for linked command.\n", hostno);
1738 /* We also will need to adjust status to accommodate intermediate
1740 if ((status == INTERMEDIATE_GOOD) ||
1741 (status == INTERMEDIATE_C_GOOD))
1746 * We should also handle what are "normal" termination messages
1747 * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually,
1748 * and flake if things aren't right.
1751 DPRINTK (DEBUG_LINKED,
1752 "scsi%d : closing I_T_L nexus.\n", hostno);
1753 linked_connected = 0;
1758 if (should_reconnect) {
1759 DPRINTK (PHASE_RESELECT,
1760 "scsi%d : exiting seagate_st0x_queue_command()"
1761 "with reconnect enabled.\n", hostno);
1762 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1764 WRITE_CONTROL (BASE_CMD);
1766 return retcode (st0x_aborted);
1767 } /* end of internal_command */
1769 static int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1771 st0x_aborted = DID_ABORT;
1772 return SCSI_ABORT_PENDING;
1779 * the seagate_st0x_reset function resets the SCSI bus
1782 static int seagate_st0x_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1784 /* No timeouts - this command is going to fail because it was reset. */
1785 DANY ("scsi%d: Reseting bus... ", hostno);
1787 /* assert RESET signal on SCSI bus. */
1788 WRITE_CONTROL (BASE_CMD | CMD_RST);
1792 WRITE_CONTROL (BASE_CMD);
1793 st0x_aborted = DID_RESET;
1796 return SCSI_RESET_WAKEUP;
1799 /* Eventually this will go into an include file, but this will be later */
1800 static Scsi_Host_Template driver_template = SEAGATE_ST0X;
1802 #include "scsi_module.c"