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_dbg.h>
107 #include <scsi/scsi_host.h>
110 #include <scsi/scsi_ioctl.h>
113 #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
115 #define DPRINTK( when, msg... ) do { } while (0)
117 #define DANY( msg... ) DPRINTK( 0xffff, msg );
127 #undef LINKED /* Linked commands are currently broken! */
129 #if defined(OVERRIDE) && !defined(CONTROLLER)
130 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
134 #undef SEAGATE_USE_ASM
138 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
139 driver, and Mitsugu Suzuki for information on the ST-01
150 #define CMD_ATTN 0x08
151 #define CMD_START_ARB 0x10
152 #define CMD_EN_PARITY 0x20
153 #define CMD_INTR 0x40
154 #define CMD_DRVR_ENABLE 0x80
160 #define STAT_MSG 0x08
163 #define STAT_MSG 0x02
167 #define STAT_BSY 0x01
169 #define STAT_REQ 0x10
170 #define STAT_SEL 0x20
171 #define STAT_PARITY 0x40
172 #define STAT_ARB_CMPL 0x80
178 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
179 #define REQ_DATAOUT 0
180 #define REQ_DATAIN STAT_IO
181 #define REQ_CMDOUT STAT_CD
182 #define REQ_STATIN (STAT_CD | STAT_IO)
183 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
184 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
186 extern volatile int seagate_st0x_timeout;
189 #define BASE_CMD CMD_EN_PARITY
198 #define PHASE_BUS_FREE 1
199 #define PHASE_ARBITRATION 2
200 #define PHASE_SELECTION 4
201 #define PHASE_DATAIN 8
202 #define PHASE_DATAOUT 0x10
203 #define PHASE_CMDOUT 0x20
204 #define PHASE_MSGIN 0x40
205 #define PHASE_MSGOUT 0x80
206 #define PHASE_STATUSIN 0x100
207 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
208 #define PRINT_COMMAND 0x200
209 #define PHASE_EXIT 0x400
210 #define PHASE_RESELECT 0x800
211 #define DEBUG_FAST 0x1000
212 #define DEBUG_SG 0x2000
213 #define DEBUG_LINKED 0x4000
214 #define DEBUG_BORKEN 0x8000
217 * Control options - these are timeouts specified in .01 seconds.
221 #define ST0X_BUS_FREE_DELAY 25
222 #define ST0X_SELECTION_DELAY 25
224 #define SEAGATE 1 /* these determine the type of the controller */
227 #define ST0X_ID_STR "Seagate ST-01/ST-02"
228 #define FD_ID_STR "TMC-8XX/TMC-950"
230 static int internal_command (unsigned char target, unsigned char lun,
232 void *buff, int bufflen, int reselect);
234 static int incommand; /* set if arbitration has finished
235 and we are in some command phase. */
237 static unsigned int base_address = 0; /* Where the card ROM starts, used to
238 calculate memory mapped register
241 static void __iomem *st0x_cr_sr; /* control register write, status
242 register read. 256 bytes in
244 Read is status of SCSI BUS, as per
247 static void __iomem *st0x_dr; /* data register, read write 256
250 static volatile int st0x_aborted = 0; /* set when we are aborted, ie by a
253 static unsigned char controller_type = 0; /* set to SEAGATE for ST0x
254 boards or FD for TMC-8xx
256 static int irq = IRQ;
258 module_param(base_address, uint, 0);
259 module_param(controller_type, byte, 0);
260 module_param(irq, int, 0);
261 MODULE_LICENSE("GPL");
264 #define retcode(result) (((result) << 16) | (message << 8) | status)
265 #define STATUS ((u8) readb(st0x_cr_sr))
266 #define DATA ((u8) readb(st0x_dr))
267 #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
268 #define WRITE_DATA(d) { writeb((d), st0x_dr); }
271 static unsigned int seagate_bases[] = {
272 0xc8000, 0xca000, 0xcc000,
273 0xce000, 0xdc000, 0xde000
277 const unsigned char *signature;
283 static Signature __initdata signatures[] = {
284 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
285 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
288 * The following two lines are NOT mistakes. One detects ROM revision
289 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
290 * and this is not going to change, the "SEAGATE" and "SCSI" together
291 * are probably "good enough"
294 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
295 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
298 * However, future domain makes several incompatible SCSI boards, so specific
299 * signatures must be used.
302 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
303 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
304 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
305 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
306 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
307 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
308 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
309 {"FUTURE DOMAIN TMC-950", 5, 21, FD},
310 /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
311 {"IBM F1 V1.2009/22/93", 5, 25, FD},
314 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
315 #endif /* n OVERRIDE */
318 * hostno stores the hostnumber, as told to us by the init routine.
321 static int hostno = -1;
322 static void seagate_reconnect_intr (int, void *, struct pt_regs *);
323 static irqreturn_t do_seagate_reconnect_intr (int, void *, struct pt_regs *);
333 * Support for broken devices :
334 * The Seagate board has a handshaking problem. Namely, a lack
335 * thereof for slow devices. You can blast 600K/second through
336 * it if you are polling for each byte, more if you do a blind
337 * transfer. In the first case, with a fast device, REQ will
338 * transition high-low or high-low-high before your loop restarts
339 * and you'll have no problems. In the second case, the board
340 * will insert wait states for up to 13.2 usecs for REQ to
341 * transition low->high, and everything will work.
343 * However, there's nothing in the state machine that says
344 * you *HAVE* to see a high-low-high set of transitions before
345 * sending the next byte, and slow things like the Trantor CD ROMS
346 * will break because of this.
348 * So, we need to slow things down, which isn't as simple as it
349 * seems. We can't slow things down period, because then people
350 * who don't recompile their kernels will shoot me for ruining
351 * their performance. We need to do it on a case per case basis.
353 * The best for performance will be to, only for borken devices
354 * (this is stored on a per-target basis in the scsi_devices array)
356 * Wait for a low->high transition before continuing with that
357 * transfer. If we timeout, continue anyways. We don't need
358 * a long timeout, because REQ should only be asserted until the
359 * corresponding ACK is received and processed.
361 * Note that we can't use the system timer for this, because of
362 * resolution, and we *really* can't use the timer chip since
363 * gettimeofday() and the beeper routines use that. So,
364 * the best thing for us to do will be to calibrate a timing
365 * loop in the initialization code using the timer chip before
366 * gettimeofday() can screw with it.
368 * FIXME: this is broken (not borken :-). Empty loop costs less than
369 * loop with ISA access in it! -- pavel@ucw.cz
372 static int borken_calibration = 0;
374 static void __init borken_init (void)
376 register int count = 0, start = jiffies + 1, stop = start + 25;
378 /* FIXME: There may be a better approach, this is a straight port for
381 while (time_before (jiffies, start))
383 for (; time_before (jiffies, stop); ++count)
388 * Ok, we now have a count for .25 seconds. Convert to a
389 * count per second and divide by transfer rate in K. */
391 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
393 if (borken_calibration < 1)
394 borken_calibration = 1;
397 static inline void borken_wait (void)
401 for (count = borken_calibration; count && (STATUS & STAT_REQ); --count)
404 #if (DEBUG & DEBUG_BORKEN)
406 printk ("scsi%d : borken timeout\n", hostno);
410 #endif /* def SLOW_RATE */
412 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
413 * contains at least one ISA access, which takes more than 0.125
414 * usec. So if we loop 8 times time in usec, we are safe.
417 #define ULOOP( i ) for (clock = i*8;;)
418 #define TIMEOUT (!(clock--))
420 int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
422 struct Scsi_Host *instance;
424 unsigned long cr, dr;
426 tpnt->proc_name = "seagate";
428 * First, we try for the manual override.
430 DANY ("Autodetecting ST0x / TMC-8xx\n");
433 printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
437 /* If the user specified the controller type from the command line,
438 controller_type will be non-zero, so don't try to detect one */
440 if (!controller_type) {
442 base_address = OVERRIDE;
443 controller_type = CONTROLLER;
445 DANY ("Base address overridden to %x, controller type is %s\n",
447 controller_type == SEAGATE ? "SEAGATE" : "FD");
450 * To detect this card, we simply look for the signature
451 * from the BIOS version notice in all the possible locations
452 * of the ROM's. This has a nice side effect of not trashing
453 * any register locations that might be used by something else.
455 * XXX - note that we probably should be probing the address
456 * space for the on-board RAM instead.
459 for (i = 0; i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i) {
460 void __iomem *p = ioremap(seagate_bases[i], 0x2000);
463 for (j = 0; j < NUM_SIGNATURES; ++j)
464 if (check_signature(p + signatures[j].offset, signatures[j].signature, signatures[j].length)) {
465 base_address = seagate_bases[i];
466 controller_type = signatures[j].type;
471 #endif /* OVERRIDE */
473 /* (! controller_type) */
474 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
475 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
478 printk(KERN_INFO "seagate: ST0x/TMC-8xx not detected.\n");
482 cr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
484 st0x_cr_sr = ioremap(cr, 0x100);
485 st0x_dr = ioremap(dr, 0x100);
487 DANY("%s detected. Base address = %x, cr = %x, dr = %x\n",
488 tpnt->name, base_address, cr, dr);
491 * At all times, we will use IRQ 5. Should also check for IRQ3
492 * if we lose our first interrupt.
494 instance = scsi_register (tpnt, 0);
495 if (instance == NULL)
498 hostno = instance->host_no;
499 if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT, (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", instance)) {
500 printk(KERN_ERR "scsi%d : unable to allocate IRQ%d\n", hostno, irq);
504 instance->io_port = base_address;
506 printk(KERN_INFO "Calibrating borken timer... ");
508 printk(" %d cycles per transfer\n", borken_calibration);
510 printk (KERN_INFO "This is one second... ");
513 ULOOP (1 * 1000 * 1000) {
520 printk ("done, %s options:"
539 #ifdef SEAGATE_USE_ASM
555 static const char *seagate_st0x_info (struct Scsi_Host *shpnt)
557 static char buffer[64];
559 snprintf(buffer, 64, "%s at irq %d, address 0x%05X",
560 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
566 * These are our saved pointers for the outstanding command that is
567 * waiting for a reconnect
570 static unsigned char current_target, current_lun;
571 static unsigned char *current_cmnd, *current_data;
572 static int current_nobuffs;
573 static struct scatterlist *current_buffer;
574 static int current_bufflen;
578 * linked_connected indicates whether or not we are currently connected to
579 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
580 * using linked commands.
583 static int linked_connected = 0;
584 static unsigned char linked_target, linked_lun;
587 static void (*done_fn) (Scsi_Cmnd *) = NULL;
588 static Scsi_Cmnd *SCint = NULL;
591 * These control whether or not disconnect / reconnect will be attempted,
592 * or are being attempted.
595 #define NO_RECONNECT 0
596 #define RECONNECT_NOW 1
597 #define CAN_RECONNECT 2
600 * LINKED_RIGHT indicates that we are currently connected to the correct target
601 * for this command, LINKED_WRONG indicates that we are connected to the wrong
602 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
605 #define LINKED_RIGHT 3
606 #define LINKED_WRONG 4
609 * This determines if we are expecting to reconnect or not.
612 static int should_reconnect = 0;
615 * The seagate_reconnect_intr routine is called when a target reselects the
616 * host adapter. This occurs on the interrupt triggered by the target
620 static irqreturn_t do_seagate_reconnect_intr(int irq, void *dev_id,
621 struct pt_regs *regs)
624 struct Scsi_Host *dev = dev_id;
626 spin_lock_irqsave (dev->host_lock, flags);
627 seagate_reconnect_intr (irq, dev_id, regs);
628 spin_unlock_irqrestore (dev->host_lock, flags);
632 static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
637 DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n", hostno);
639 if (!should_reconnect)
640 printk(KERN_WARNING "scsi%d: unexpected interrupt.\n", hostno);
642 should_reconnect = 0;
644 DPRINTK (PHASE_RESELECT, "scsi%d : internal_command(%d, %08x, %08x, RECONNECT_NOW\n",
645 hostno, current_target, current_data, current_bufflen);
647 temp = internal_command (current_target, current_lun, current_cmnd, current_data, current_bufflen, RECONNECT_NOW);
649 if (msg_byte(temp) != DISCONNECT) {
651 DPRINTK(PHASE_RESELECT, "scsi%d : done_fn(%d,%08x)", hostno, hostno, temp);
653 panic ("SCint == NULL in seagate");
656 SCtmp->result = temp;
659 printk(KERN_ERR "done_fn() not defined.\n");
665 * The seagate_st0x_queue_command() function provides a queued interface
666 * to the seagate SCSI driver. Basically, it just passes control onto the
667 * seagate_command() function, after fixing it so that the done_fn()
668 * is set to the one passed to the function. We have to be very careful,
669 * because there are some commands on some devices that do not disconnect,
670 * and if we simply call the done_fn when the command is done then another
671 * command is started and queue_command is called again... We end up
672 * overflowing the kernel stack, and this tends not to be such a good idea.
675 static int recursion_depth = 0;
677 static int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
679 int result, reconnect;
682 DANY ("seagate: que_command");
684 current_target = SCpnt->device->id;
685 current_lun = SCpnt->device->lun;
686 current_cmnd = SCpnt->cmnd;
687 current_data = (unsigned char *) SCpnt->request_buffer;
688 current_bufflen = SCpnt->request_bufflen;
696 * Set linked command bit in control field of SCSI command.
699 current_cmnd[SCpnt->cmd_len] |= 0x01;
700 if (linked_connected) {
701 DPRINTK (DEBUG_LINKED, "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
702 if (linked_target == current_target && linked_lun == current_lun)
704 DPRINTK(DEBUG_LINKED, "correct\n");
705 reconnect = LINKED_RIGHT;
707 DPRINTK(DEBUG_LINKED, "incorrect\n");
708 reconnect = LINKED_WRONG;
712 reconnect = CAN_RECONNECT;
714 result = internal_command(SCint->device->id, SCint->device->lun, SCint->cmnd,
715 SCint->request_buffer, SCint->request_bufflen, reconnect);
716 if (msg_byte(result) == DISCONNECT)
720 SCtmp->result = result;
728 static int internal_command (unsigned char target, unsigned char lun,
729 const void *cmnd, void *buff, int bufflen, int reselect)
731 unsigned char *data = NULL;
732 struct scatterlist *buffer = NULL;
733 int clock, temp, nobuffs = 0, done = 0, len = 0;
735 int transfered = 0, phase = 0, newphase;
737 register unsigned char status_read;
738 unsigned char tmp_data, tmp_control, status = 0, message = 0;
739 unsigned transfersize = 0, underflow = 0;
741 int borken = (int) SCint->device->borken; /* Does the current target require
748 #if (DEBUG & PRINT_COMMAND)
749 printk("scsi%d : target = %d, command = ", hostno, target);
750 __scsi_print_command((unsigned char *) cmnd);
753 #if (DEBUG & PHASE_RESELECT)
756 printk("scsi%d : reconnecting\n", hostno);
760 printk("scsi%d : connected, can reconnect\n", hostno);
763 printk("scsi%d : connected to wrong target, can reconnect\n",
768 printk("scsi%d : allowed to reconnect\n", hostno);
771 printk("scsi%d : not allowed to reconnect\n", hostno);
775 if (target == (controller_type == SEAGATE ? 7 : 6))
776 return DID_BAD_TARGET;
779 * We work it differently depending on if this is is "the first time,"
780 * or a reconnect. If this is a reselect phase, then SEL will
781 * be asserted, and we must skip selection / arbitration phases.
786 DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
788 * At this point, we should find the logical or of our ID
789 * and the original target's ID on the BUS, with BSY, SEL,
790 * and I/O signals asserted.
792 * After ARBITRATION phase is completed, only SEL, BSY,
793 * and the target ID are asserted. A valid initiator ID
794 * is not on the bus until IO is asserted, so we must wait
799 if ((temp & STAT_IO) && !(temp & STAT_BSY))
802 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
803 return (DID_BAD_INTR << 16);
808 * After I/O is asserted by the target, we can read our ID
809 * and its ID off of the BUS.
812 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
813 DPRINTK (PHASE_RESELECT, "scsi%d : detected reconnect request to different target.\n\tData bus = %d\n", hostno, temp);
814 return (DID_BAD_INTR << 16);
817 if (!(temp & (1 << current_target))) {
818 printk(KERN_WARNING "scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno, temp);
819 return (DID_BAD_INTR << 16);
822 buffer = current_buffer;
823 cmnd = current_cmnd; /* WDE add */
824 data = current_data; /* WDE add */
825 len = current_bufflen; /* WDE add */
826 nobuffs = current_nobuffs;
829 * We have determined that we have been selected. At this
830 * point, we must respond to the reselection by asserting
835 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
837 WRITE_CONTROL (BASE_CMD | CMD_BSY);
841 * The target will drop SEL, and raise BSY, at which time
846 if (!(STATUS & STAT_SEL))
849 WRITE_CONTROL (BASE_CMD | CMD_INTR);
850 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
851 return (DID_BAD_INTR << 16);
854 WRITE_CONTROL (BASE_CMD);
856 * At this point, we have connected with the target
857 * and can get on with our lives.
863 * This is a bletcherous hack, just as bad as the Unix #!
864 * interpreter stuff. If it turns out we are using the wrong
865 * I_T_L nexus, the easiest way to deal with it is to go into
866 * our INFORMATION TRANSFER PHASE code, send a ABORT
867 * message on MESSAGE OUT phase, and then loop back to here.
871 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
876 * On entry, we make sure that the BUS is in a BUS FREE
877 * phase, by insuring that both BSY and SEL are low for
878 * at least one bus settle delay. Several reads help
879 * eliminate wire glitch.
883 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
884 clock = jiffies + ST0X_BUS_FREE_DELAY;
886 while (((STATUS | STATUS | STATUS) & (STAT_BSY | STAT_SEL)) && (!st0x_aborted) && time_before (jiffies, clock))
889 if (time_after (jiffies, clock))
890 return retcode (DID_BUS_BUSY);
891 else if (st0x_aborted)
892 return retcode (st0x_aborted);
894 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
896 clock = jiffies + ST0X_SELECTION_DELAY;
899 * Arbitration/selection procedure :
901 * 2. Write HOST adapter address bit
902 * 3. Set start arbitration.
903 * 4. We get either ARBITRATION COMPLETE or SELECT at this
905 * 5. OR our ID and targets on bus.
906 * 6. Enable SCSI drivers and asserted SEL and ATTN
910 /* FIXME: verify host lock is always held here */
912 WRITE_DATA((controller_type == SEAGATE) ? 0x80 : 0x40);
913 WRITE_CONTROL(CMD_START_ARB);
915 ULOOP (ST0X_SELECTION_DELAY * 10000) {
916 status_read = STATUS;
917 if (status_read & STAT_ARB_CMPL)
919 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
921 if (TIMEOUT || (status_read & STAT_SEL)) {
922 printk(KERN_WARNING "scsi%d : arbitration lost or timeout.\n", hostno);
923 WRITE_CONTROL (BASE_CMD);
924 return retcode (DID_NO_CONNECT);
927 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
931 * When the SCSI device decides that we're gawking at it,
932 * it will respond by asserting BUSY on the bus.
934 * Note : the Seagate ST-01/02 product manual says that we
935 * should twiddle the DATA register before the control
936 * register. However, this does not work reliably so we do
937 * it the other way around.
939 * Probably could be a problem with arbitration too, we
940 * really should try this with a SCSI protocol or logic
941 * analyzer to see what is going on.
943 tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
944 tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
946 /* FIXME: verify host lock is always held here */
947 #ifdef OLDCNTDATASCEME
949 WRITE_CONTROL (tmp_control);
950 WRITE_DATA (tmp_data);
952 WRITE_DATA (tmp_data);
953 WRITE_CONTROL (tmp_control);
956 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
957 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
958 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
959 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
960 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
966 * If we have been aborted, and we have a
967 * command in progress, IE the target
968 * still has BSY asserted, then we will
969 * reset the bus, and notify the midlevel
970 * driver to expect sense.
973 WRITE_CONTROL (BASE_CMD);
974 if (STATUS & STAT_BSY) {
975 printk(KERN_WARNING "scsi%d : BST asserted after we've been aborted.\n", hostno);
976 seagate_st0x_bus_reset(NULL);
977 return retcode (DID_RESET);
979 return retcode (st0x_aborted);
981 if (STATUS & STAT_BSY)
984 DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n", hostno, target, STATUS);
985 return retcode (DID_NO_CONNECT);
989 /* Establish current pointers. Take into account scatter / gather */
991 if ((nobuffs = SCint->use_sg)) {
992 #if (DEBUG & DEBUG_SG)
995 printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno, nobuffs);
996 for (i = 0; i < nobuffs; ++i)
997 printk("scsi%d : buffer %d address = %p length = %d\n",
999 page_address(buffer[i].page) + buffer[i].offset,
1004 buffer = (struct scatterlist *) SCint->buffer;
1005 len = buffer->length;
1006 data = page_address(buffer->page) + buffer->offset;
1008 DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
1010 len = SCint->request_bufflen;
1011 data = (unsigned char *) SCint->request_buffer;
1014 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1024 } /* end of switch(reselect) */
1027 * There are several conditions under which we wish to send a message :
1028 * 1. When we are allowing disconnect / reconnect, and need to
1029 * establish the I_T_L nexus via an IDENTIFY with the DiscPriv bit
1032 * 2. When we are doing linked commands, are have the wrong I_T_L
1033 * nexus established and want to send an ABORT message.
1036 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1038 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT)|| (reselect == LINKED_WRONG))? CMD_ATTN : 0));
1040 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT))? CMD_ATTN : 0));
1044 * INFORMATION TRANSFER PHASE
1046 * The nasty looking read / write inline assembler loops we use for
1047 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1048 * the 'C' versions - since we're moving 1024 bytes of data, this
1051 * SJT: The nasty-looking assembler is gone, so it's slower.
1055 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1058 transfersize = SCint->transfersize;
1059 underflow = SCint->underflow;
1062 * Now, we poll the device for status information,
1063 * and handle any requests it makes. Note that since we are unsure
1064 * of how much data will be flowing across the system, etc and
1065 * cannot make reasonable timeouts, that we will instead have the
1066 * midlevel driver handle any timeouts that occur in this phase.
1069 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1071 if (status_read & STAT_PARITY) {
1072 printk(KERN_ERR "scsi%d : got parity error\n", hostno);
1073 st0x_aborted = DID_PARITY;
1076 if (status_read & STAT_REQ) {
1077 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1078 if ((newphase = (status_read & REQ_MASK)) != phase) {
1082 printk ("scsi%d : phase = DATA OUT\n", hostno);
1085 printk ("scsi%d : phase = DATA IN\n", hostno);
1089 ("scsi%d : phase = COMMAND OUT\n", hostno);
1092 printk ("scsi%d : phase = STATUS IN\n", hostno);
1096 ("scsi%d : phase = MESSAGE OUT\n", hostno);
1099 printk ("scsi%d : phase = MESSAGE IN\n", hostno);
1102 printk ("scsi%d : phase = UNKNOWN\n", hostno);
1103 st0x_aborted = DID_ERROR;
1107 switch (status_read & REQ_MASK) {
1110 * If we are in fast mode, then we simply splat
1111 * the data out in word-sized chunks as fast as
1117 printk("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
1118 st0x_aborted = DID_ERROR;
1124 if (fast && transfersize
1125 && !(len % transfersize)
1126 && (len >= transfersize)
1128 && !(transfersize % 4)
1131 DPRINTK (DEBUG_FAST,
1132 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1133 " len = %d, data = %08x\n",
1134 hostno, SCint->underflow,
1135 SCint->transfersize, len,
1138 /* SJT: Start. Fast Write */
1139 #ifdef SEAGATE_USE_ASM
1145 "movl %%eax, (%%edi)\n\t"
1149 "movb %%al, (%%edi)\n\t"
1153 /* input */ :"D" (st0x_dr),
1156 "c" (SCint->transfersize)
1160 #else /* SEAGATE_USE_ASM */
1161 memcpy_toio(st0x_dr, data, transfersize);
1162 #endif /* SEAGATE_USE_ASM */
1164 len -= transfersize;
1165 data += transfersize;
1166 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1169 * We loop as long as we are in a
1170 * data out phase, there is data to
1171 * send, and BSY is still active.
1174 /* SJT: Start. Slow Write. */
1175 #ifdef SEAGATE_USE_ASM
1177 int __dummy_1, __dummy_2;
1180 * We loop as long as we are in a data out phase, there is data to send,
1181 * and BSY is still active.
1183 /* Local variables : len = ecx , data = esi,
1184 st0x_cr_sr = ebx, st0x_dr = edi
1187 /* Test for any data here at all. */
1188 "orl %%ecx, %%ecx\n\t"
1189 "jz 2f\n\t" "cld\n\t"
1190 /* "movl st0x_cr_sr, %%ebx\n\t" */
1191 /* "movl st0x_dr, %%edi\n\t" */
1193 "movb (%%ebx), %%al\n\t"
1197 /* Test for data out phase - STATUS & REQ_MASK should be
1198 REQ_DATAOUT, which is 0. */
1199 "test $0xe, %%al\n\t"
1202 "test $0x10, %%al\n\t"
1205 "movb %%al, (%%edi)\n\t"
1206 "loop 1b\n\t" "2:\n"
1207 /* output */ :"=S" (data), "=c" (len),
1212 : "0" (data), "1" (len),
1217 #else /* SEAGATE_USE_ASM */
1222 if (!(stat & STAT_BSY)
1223 || ((stat & REQ_MASK) !=
1226 if (stat & STAT_REQ) {
1227 WRITE_DATA (*data++);
1231 #endif /* SEAGATE_USE_ASM */
1235 if (!len && nobuffs) {
1238 len = buffer->length;
1239 data = page_address(buffer->page) + buffer->offset;
1241 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1249 #if (DEBUG & (PHASE_DATAIN))
1252 for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN | STAT_REQ); --len) {
1256 #if (DEBUG & (PHASE_DATAIN))
1262 if (fast && transfersize
1263 && !(len % transfersize)
1264 && (len >= transfersize)
1266 && !(transfersize % 4)
1269 DPRINTK (DEBUG_FAST,
1270 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1271 " len = %d, data = %08x\n",
1272 hostno, SCint->underflow,
1273 SCint->transfersize, len,
1276 /* SJT: Start. Fast Read */
1277 #ifdef SEAGATE_USE_ASM
1282 "movl (%%esi), %%eax\n\t"
1286 "movb (%%esi), %%al\n\t"
1291 /* input */ :"S" (st0x_dr),
1294 "c" (SCint->transfersize)
1298 #else /* SEAGATE_USE_ASM */
1299 memcpy_fromio(data, st0x_dr, len);
1300 #endif /* SEAGATE_USE_ASM */
1302 len -= transfersize;
1303 data += transfersize;
1304 #if (DEBUG & PHASE_DATAIN)
1305 printk ("scsi%d: transfered += %d\n", hostno, transfersize);
1306 transfered += transfersize;
1309 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1312 #if (DEBUG & PHASE_DATAIN)
1313 printk ("scsi%d: transfered += %d\n", hostno, len);
1314 transfered += len; /* Assume we'll transfer it all, then
1315 subtract what we *didn't* transfer */
1319 * We loop as long as we are in a data in phase, there is room to read,
1320 * and BSY is still active
1324 #ifdef SEAGATE_USE_ASM
1326 int __dummy_3, __dummy_4;
1328 /* Dummy clobbering variables for the new gcc-2.95 */
1331 * We loop as long as we are in a data in phase, there is room to read,
1332 * and BSY is still active
1334 /* Local variables : ecx = len, edi = data
1335 esi = st0x_cr_sr, ebx = st0x_dr */
1337 /* Test for room to read */
1338 "orl %%ecx, %%ecx\n\t"
1339 "jz 2f\n\t" "cld\n\t"
1340 /* "movl st0x_cr_sr, %%esi\n\t" */
1341 /* "movl st0x_dr, %%ebx\n\t" */
1343 "movb (%%esi), %%al\n\t"
1347 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1348 = STAT_IO, which is 4. */
1349 "movb $0xe, %%ah\n\t"
1350 "andb %%al, %%ah\n\t"
1351 "cmpb $0x04, %%ah\n\t"
1354 "test $0x10, %%al\n\t"
1356 "movb (%%ebx), %%al\n\t"
1358 "loop 1b\n\t" "2:\n"
1359 /* output */ :"=D" (data), "=c" (len),
1364 : "0" (data), "1" (len),
1369 #else /* SEAGATE_USE_ASM */
1374 if (!(stat & STAT_BSY)
1375 || ((stat & REQ_MASK) !=
1378 if (stat & STAT_REQ) {
1383 #endif /* SEAGATE_USE_ASM */
1385 #if (DEBUG & PHASE_DATAIN)
1386 printk ("scsi%d: transfered -= %d\n", hostno, len);
1387 transfered -= len; /* Since we assumed all of Len got *
1388 transfered, correct our mistake */
1392 if (!len && nobuffs) {
1395 len = buffer->length;
1396 data = page_address(buffer->page) + buffer->offset;
1397 DPRINTK (DEBUG_SG, "scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno, len, data);
1402 while (((status_read = STATUS) & STAT_BSY) &&
1403 ((status_read & REQ_MASK) == REQ_CMDOUT))
1404 if (status_read & STAT_REQ) {
1405 WRITE_DATA (*(const unsigned char *) cmnd);
1406 cmnd = 1 + (const unsigned char *)cmnd;
1420 * We can only have sent a MSG OUT if we
1421 * requested to do this by raising ATTN.
1422 * So, we must drop ATTN.
1424 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1426 * If we are reconnecting, then we must
1427 * send an IDENTIFY message in response
1432 WRITE_DATA (IDENTIFY (1, lun));
1433 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
1438 linked_connected = 0;
1439 reselect = CAN_RECONNECT;
1441 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED, "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
1443 DPRINTK (DEBUG_LINKED, "correct\n");
1446 printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1451 switch (message = DATA) {
1453 DANY("seagate: deciding to disconnect\n");
1454 should_reconnect = 1;
1455 current_data = data; /* WDE add */
1456 current_buffer = buffer;
1457 current_bufflen = len; /* WDE add */
1458 current_nobuffs = nobuffs;
1460 linked_connected = 0;
1463 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
1467 case LINKED_CMD_COMPLETE:
1468 case LINKED_FLG_CMD_COMPLETE:
1470 case COMMAND_COMPLETE:
1472 * Note : we should check for underflow here.
1474 DPRINTK(PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
1478 DPRINTK(PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
1482 current_buffer = buffer;
1483 current_bufflen = len; /* WDE add */
1484 current_data = data; /* WDE mod */
1485 current_nobuffs = nobuffs;
1486 DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
1488 case RESTORE_POINTERS:
1489 buffer = current_buffer;
1490 cmnd = current_cmnd;
1491 data = current_data; /* WDE mod */
1492 len = current_bufflen;
1493 nobuffs = current_nobuffs;
1494 DPRINTK(PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
1499 * IDENTIFY distinguishes itself
1500 * from the other messages by
1501 * setting the high bit.
1503 * Note : we need to handle at
1504 * least one outstanding command
1505 * per LUN, and need to hash the
1506 * SCSI command for that I_T_L
1507 * nexus based on the known ID
1508 * (at this point) and LUN.
1511 if (message & 0x80) {
1512 DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n", hostno, target, message & 7);
1515 * We should go into a
1516 * MESSAGE OUT phase, and
1517 * send a MESSAGE_REJECT
1518 * if we run into a message
1519 * that we don't like. The
1520 * seagate driver needs
1522 * restructuring first
1525 DPRINTK (PHASE_MSGIN, "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
1530 printk(KERN_ERR "scsi%d : unknown phase.\n", hostno);
1531 st0x_aborted = DID_ERROR;
1532 } /* end of switch (status_read & REQ_MASK) */
1535 * I really don't care to deal with borken devices in
1536 * each single byte transfer case (ie, message in,
1537 * message out, status), so I'll do the wait here if
1544 } /* if(status_read & STAT_REQ) ends */
1545 } /* while(((status_read = STATUS)...) ends */
1547 DPRINTK(PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT, "scsi%d : Transfered %d bytes\n", hostno, transfered);
1549 #if (DEBUG & PHASE_EXIT)
1550 #if 0 /* Doesn't work for scatter/gather */
1551 printk("Buffer : \n");
1552 for(i = 0; i < 20; ++i)
1553 printk("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1556 printk("scsi%d : status = ", hostno);
1557 scsi_print_status(status);
1558 printk(" message = %02x\n", message);
1561 /* We shouldn't reach this until *after* BSY has been deasserted */
1567 * Fix the message byte so that unsuspecting high level drivers
1568 * don't puke when they see a LINKED COMMAND message in place of
1569 * the COMMAND COMPLETE they may be expecting. Shouldn't be
1570 * necessary, but it's better to be on the safe side.
1572 * A non LINKED* message byte will indicate that the command
1573 * completed, and we are now disconnected.
1577 case LINKED_CMD_COMPLETE:
1578 case LINKED_FLG_CMD_COMPLETE:
1579 message = COMMAND_COMPLETE;
1580 linked_target = current_target;
1581 linked_lun = current_lun;
1582 linked_connected = 1;
1583 DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established for linked command.\n", hostno);
1584 /* We also will need to adjust status to accommodate intermediate
1586 if ((status == INTERMEDIATE_GOOD) || (status == INTERMEDIATE_C_GOOD))
1590 * We should also handle what are "normal" termination
1591 * messages here (ABORT, BUS_DEVICE_RESET?, and
1592 * COMMAND_COMPLETE individually, and flake if things
1596 DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
1597 linked_connected = 0;
1602 if (should_reconnect) {
1603 DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n", hostno);
1604 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1606 WRITE_CONTROL (BASE_CMD);
1608 return retcode (st0x_aborted);
1609 } /* end of internal_command */
1611 static int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1613 st0x_aborted = DID_ABORT;
1621 * the seagate_st0x_reset function resets the SCSI bus
1623 * May be called with SCpnt = NULL
1626 static int seagate_st0x_bus_reset(Scsi_Cmnd * SCpnt)
1628 /* No timeouts - this command is going to fail because it was reset. */
1629 DANY ("scsi%d: Reseting bus... ", hostno);
1631 /* assert RESET signal on SCSI bus. */
1632 WRITE_CONTROL (BASE_CMD | CMD_RST);
1636 WRITE_CONTROL (BASE_CMD);
1637 st0x_aborted = DID_RESET;
1643 static int seagate_st0x_host_reset(Scsi_Cmnd *SCpnt)
1648 static int seagate_st0x_device_reset(Scsi_Cmnd *SCpnt)
1653 static int seagate_st0x_release(struct Scsi_Host *shost)
1656 free_irq(shost->irq, shost);
1657 release_region(shost->io_port, shost->n_io_port);
1661 static Scsi_Host_Template driver_template = {
1662 .detect = seagate_st0x_detect,
1663 .release = seagate_st0x_release,
1664 .info = seagate_st0x_info,
1665 .queuecommand = seagate_st0x_queue_command,
1666 .eh_abort_handler = seagate_st0x_abort,
1667 .eh_bus_reset_handler = seagate_st0x_bus_reset,
1668 .eh_host_reset_handler = seagate_st0x_host_reset,
1669 .eh_device_reset_handler = seagate_st0x_device_reset,
1672 .sg_tablesize = SG_ALL,
1674 .use_clustering = DISABLE_CLUSTERING,
1676 #include "scsi_module.c"