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/blkdev.h>
98 #include <linux/stat.h>
99 #include <linux/delay.h>
102 #include <asm/system.h>
103 #include <asm/uaccess.h>
105 #include <scsi/scsi_cmnd.h>
106 #include <scsi/scsi_device.h>
107 #include <scsi/scsi.h>
109 #include <scsi/scsi_dbg.h>
110 #include <scsi/scsi_host.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 *);
324 static irqreturn_t do_seagate_reconnect_intr (int, void *);
325 static int seagate_st0x_bus_reset(struct scsi_cmnd *);
335 * Support for broken devices :
336 * The Seagate board has a handshaking problem. Namely, a lack
337 * thereof for slow devices. You can blast 600K/second through
338 * it if you are polling for each byte, more if you do a blind
339 * transfer. In the first case, with a fast device, REQ will
340 * transition high-low or high-low-high before your loop restarts
341 * and you'll have no problems. In the second case, the board
342 * will insert wait states for up to 13.2 usecs for REQ to
343 * transition low->high, and everything will work.
345 * However, there's nothing in the state machine that says
346 * you *HAVE* to see a high-low-high set of transitions before
347 * sending the next byte, and slow things like the Trantor CD ROMS
348 * will break because of this.
350 * So, we need to slow things down, which isn't as simple as it
351 * seems. We can't slow things down period, because then people
352 * who don't recompile their kernels will shoot me for ruining
353 * their performance. We need to do it on a case per case basis.
355 * The best for performance will be to, only for borken devices
356 * (this is stored on a per-target basis in the scsi_devices array)
358 * Wait for a low->high transition before continuing with that
359 * transfer. If we timeout, continue anyways. We don't need
360 * a long timeout, because REQ should only be asserted until the
361 * corresponding ACK is received and processed.
363 * Note that we can't use the system timer for this, because of
364 * resolution, and we *really* can't use the timer chip since
365 * gettimeofday() and the beeper routines use that. So,
366 * the best thing for us to do will be to calibrate a timing
367 * loop in the initialization code using the timer chip before
368 * gettimeofday() can screw with it.
370 * FIXME: this is broken (not borken :-). Empty loop costs less than
371 * loop with ISA access in it! -- pavel@ucw.cz
374 static int borken_calibration = 0;
376 static void __init borken_init (void)
378 register int count = 0, start = jiffies + 1, stop = start + 25;
380 /* FIXME: There may be a better approach, this is a straight port for
383 while (time_before (jiffies, start))
385 for (; time_before (jiffies, stop); ++count)
390 * Ok, we now have a count for .25 seconds. Convert to a
391 * count per second and divide by transfer rate in K. */
393 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
395 if (borken_calibration < 1)
396 borken_calibration = 1;
399 static inline void borken_wait (void)
403 for (count = borken_calibration; count && (STATUS & STAT_REQ); --count)
406 #if (DEBUG & DEBUG_BORKEN)
408 printk ("scsi%d : borken timeout\n", hostno);
412 #endif /* def SLOW_RATE */
414 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
415 * contains at least one ISA access, which takes more than 0.125
416 * usec. So if we loop 8 times time in usec, we are safe.
419 #define ULOOP( i ) for (clock = i*8;;)
420 #define TIMEOUT (!(clock--))
422 int __init seagate_st0x_detect (struct scsi_host_template * tpnt)
424 struct Scsi_Host *instance;
426 unsigned long cr, dr;
428 tpnt->proc_name = "seagate";
430 * First, we try for the manual override.
432 DANY ("Autodetecting ST0x / TMC-8xx\n");
435 printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
439 /* If the user specified the controller type from the command line,
440 controller_type will be non-zero, so don't try to detect one */
442 if (!controller_type) {
444 base_address = OVERRIDE;
445 controller_type = CONTROLLER;
447 DANY ("Base address overridden to %x, controller type is %s\n",
449 controller_type == SEAGATE ? "SEAGATE" : "FD");
452 * To detect this card, we simply look for the signature
453 * from the BIOS version notice in all the possible locations
454 * of the ROM's. This has a nice side effect of not trashing
455 * any register locations that might be used by something else.
457 * XXX - note that we probably should be probing the address
458 * space for the on-board RAM instead.
461 for (i = 0; i < ARRAY_SIZE(seagate_bases); ++i) {
462 void __iomem *p = ioremap(seagate_bases[i], 0x2000);
465 for (j = 0; j < NUM_SIGNATURES; ++j)
466 if (check_signature(p + signatures[j].offset, signatures[j].signature, signatures[j].length)) {
467 base_address = seagate_bases[i];
468 controller_type = signatures[j].type;
473 #endif /* OVERRIDE */
475 /* (! controller_type) */
476 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
477 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
480 printk(KERN_INFO "seagate: ST0x/TMC-8xx not detected.\n");
484 cr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
486 st0x_cr_sr = ioremap(cr, 0x100);
487 st0x_dr = ioremap(dr, 0x100);
489 DANY("%s detected. Base address = %x, cr = %x, dr = %x\n",
490 tpnt->name, base_address, cr, dr);
493 * At all times, we will use IRQ 5. Should also check for IRQ3
494 * if we lose our first interrupt.
496 instance = scsi_register (tpnt, 0);
497 if (instance == NULL)
500 hostno = instance->host_no;
501 if (request_irq (irq, do_seagate_reconnect_intr, IRQF_DISABLED, (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", instance)) {
502 printk(KERN_ERR "scsi%d : unable to allocate IRQ%d\n", hostno, irq);
506 instance->io_port = base_address;
508 printk(KERN_INFO "Calibrating borken timer... ");
510 printk(" %d cycles per transfer\n", borken_calibration);
512 printk (KERN_INFO "This is one second... ");
515 ULOOP (1 * 1000 * 1000) {
522 printk ("done, %s options:"
541 #ifdef SEAGATE_USE_ASM
557 static const char *seagate_st0x_info (struct Scsi_Host *shpnt)
559 static char buffer[64];
561 snprintf(buffer, 64, "%s at irq %d, address 0x%05X",
562 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
568 * These are our saved pointers for the outstanding command that is
569 * waiting for a reconnect
572 static unsigned char current_target, current_lun;
573 static unsigned char *current_cmnd, *current_data;
574 static int current_nobuffs;
575 static struct scatterlist *current_buffer;
576 static int current_bufflen;
580 * linked_connected indicates whether or not we are currently connected to
581 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
582 * using linked commands.
585 static int linked_connected = 0;
586 static unsigned char linked_target, linked_lun;
589 static void (*done_fn) (struct scsi_cmnd *) = NULL;
590 static struct scsi_cmnd *SCint = NULL;
593 * These control whether or not disconnect / reconnect will be attempted,
594 * or are being attempted.
597 #define NO_RECONNECT 0
598 #define RECONNECT_NOW 1
599 #define CAN_RECONNECT 2
602 * LINKED_RIGHT indicates that we are currently connected to the correct target
603 * for this command, LINKED_WRONG indicates that we are connected to the wrong
604 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
607 #define LINKED_RIGHT 3
608 #define LINKED_WRONG 4
611 * This determines if we are expecting to reconnect or not.
614 static int should_reconnect = 0;
617 * The seagate_reconnect_intr routine is called when a target reselects the
618 * host adapter. This occurs on the interrupt triggered by the target
622 static irqreturn_t do_seagate_reconnect_intr(int irq, void *dev_id)
625 struct Scsi_Host *dev = dev_id;
627 spin_lock_irqsave (dev->host_lock, flags);
628 seagate_reconnect_intr (irq, dev_id);
629 spin_unlock_irqrestore (dev->host_lock, flags);
633 static void seagate_reconnect_intr (int irq, void *dev_id)
636 struct scsi_cmnd *SCtmp;
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(struct scsi_cmnd * SCpnt,
679 void (*done) (struct scsi_cmnd *))
681 int result, reconnect;
682 struct scsi_cmnd *SCtmp;
684 DANY ("seagate: que_command");
686 current_target = SCpnt->device->id;
687 current_lun = SCpnt->device->lun;
688 current_cmnd = SCpnt->cmnd;
689 current_data = (unsigned char *) SCpnt->request_buffer;
690 current_bufflen = SCpnt->request_bufflen;
698 * Set linked command bit in control field of SCSI command.
701 current_cmnd[SCpnt->cmd_len] |= 0x01;
702 if (linked_connected) {
703 DPRINTK (DEBUG_LINKED, "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
704 if (linked_target == current_target && linked_lun == current_lun)
706 DPRINTK(DEBUG_LINKED, "correct\n");
707 reconnect = LINKED_RIGHT;
709 DPRINTK(DEBUG_LINKED, "incorrect\n");
710 reconnect = LINKED_WRONG;
714 reconnect = CAN_RECONNECT;
716 result = internal_command(SCint->device->id, SCint->device->lun, SCint->cmnd,
717 SCint->request_buffer, SCint->request_bufflen, reconnect);
718 if (msg_byte(result) == DISCONNECT)
722 SCtmp->result = result;
730 static int internal_command (unsigned char target, unsigned char lun,
731 const void *cmnd, void *buff, int bufflen, int reselect)
733 unsigned char *data = NULL;
734 struct scatterlist *buffer = NULL;
735 int clock, temp, nobuffs = 0, done = 0, len = 0;
737 int transfered = 0, phase = 0, newphase;
739 register unsigned char status_read;
740 unsigned char tmp_data, tmp_control, status = 0, message = 0;
741 unsigned transfersize = 0, underflow = 0;
743 int borken = (int) SCint->device->borken; /* Does the current target require
750 #if (DEBUG & PRINT_COMMAND)
751 printk("scsi%d : target = %d, command = ", hostno, target);
752 __scsi_print_command((unsigned char *) cmnd);
755 #if (DEBUG & PHASE_RESELECT)
758 printk("scsi%d : reconnecting\n", hostno);
762 printk("scsi%d : connected, can reconnect\n", hostno);
765 printk("scsi%d : connected to wrong target, can reconnect\n",
770 printk("scsi%d : allowed to reconnect\n", hostno);
773 printk("scsi%d : not allowed to reconnect\n", hostno);
777 if (target == (controller_type == SEAGATE ? 7 : 6))
778 return DID_BAD_TARGET;
781 * We work it differently depending on if this is is "the first time,"
782 * or a reconnect. If this is a reselect phase, then SEL will
783 * be asserted, and we must skip selection / arbitration phases.
788 DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
790 * At this point, we should find the logical or of our ID
791 * and the original target's ID on the BUS, with BSY, SEL,
792 * and I/O signals asserted.
794 * After ARBITRATION phase is completed, only SEL, BSY,
795 * and the target ID are asserted. A valid initiator ID
796 * is not on the bus until IO is asserted, so we must wait
801 if ((temp & STAT_IO) && !(temp & STAT_BSY))
804 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
805 return (DID_BAD_INTR << 16);
810 * After I/O is asserted by the target, we can read our ID
811 * and its ID off of the BUS.
814 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
815 DPRINTK (PHASE_RESELECT, "scsi%d : detected reconnect request to different target.\n\tData bus = %d\n", hostno, temp);
816 return (DID_BAD_INTR << 16);
819 if (!(temp & (1 << current_target))) {
820 printk(KERN_WARNING "scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno, temp);
821 return (DID_BAD_INTR << 16);
824 buffer = current_buffer;
825 cmnd = current_cmnd; /* WDE add */
826 data = current_data; /* WDE add */
827 len = current_bufflen; /* WDE add */
828 nobuffs = current_nobuffs;
831 * We have determined that we have been selected. At this
832 * point, we must respond to the reselection by asserting
837 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
839 WRITE_CONTROL (BASE_CMD | CMD_BSY);
843 * The target will drop SEL, and raise BSY, at which time
848 if (!(STATUS & STAT_SEL))
851 WRITE_CONTROL (BASE_CMD | CMD_INTR);
852 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
853 return (DID_BAD_INTR << 16);
856 WRITE_CONTROL (BASE_CMD);
858 * At this point, we have connected with the target
859 * and can get on with our lives.
865 * This is a bletcherous hack, just as bad as the Unix #!
866 * interpreter stuff. If it turns out we are using the wrong
867 * I_T_L nexus, the easiest way to deal with it is to go into
868 * our INFORMATION TRANSFER PHASE code, send a ABORT
869 * message on MESSAGE OUT phase, and then loop back to here.
873 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
878 * On entry, we make sure that the BUS is in a BUS FREE
879 * phase, by insuring that both BSY and SEL are low for
880 * at least one bus settle delay. Several reads help
881 * eliminate wire glitch.
885 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
886 clock = jiffies + ST0X_BUS_FREE_DELAY;
888 while (((STATUS | STATUS | STATUS) & (STAT_BSY | STAT_SEL)) && (!st0x_aborted) && time_before (jiffies, clock))
891 if (time_after (jiffies, clock))
892 return retcode (DID_BUS_BUSY);
893 else if (st0x_aborted)
894 return retcode (st0x_aborted);
896 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
898 clock = jiffies + ST0X_SELECTION_DELAY;
901 * Arbitration/selection procedure :
903 * 2. Write HOST adapter address bit
904 * 3. Set start arbitration.
905 * 4. We get either ARBITRATION COMPLETE or SELECT at this
907 * 5. OR our ID and targets on bus.
908 * 6. Enable SCSI drivers and asserted SEL and ATTN
912 /* FIXME: verify host lock is always held here */
914 WRITE_DATA((controller_type == SEAGATE) ? 0x80 : 0x40);
915 WRITE_CONTROL(CMD_START_ARB);
917 ULOOP (ST0X_SELECTION_DELAY * 10000) {
918 status_read = STATUS;
919 if (status_read & STAT_ARB_CMPL)
921 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
923 if (TIMEOUT || (status_read & STAT_SEL)) {
924 printk(KERN_WARNING "scsi%d : arbitration lost or timeout.\n", hostno);
925 WRITE_CONTROL (BASE_CMD);
926 return retcode (DID_NO_CONNECT);
929 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
933 * When the SCSI device decides that we're gawking at it,
934 * it will respond by asserting BUSY on the bus.
936 * Note : the Seagate ST-01/02 product manual says that we
937 * should twiddle the DATA register before the control
938 * register. However, this does not work reliably so we do
939 * it the other way around.
941 * Probably could be a problem with arbitration too, we
942 * really should try this with a SCSI protocol or logic
943 * analyzer to see what is going on.
945 tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
946 tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
948 /* FIXME: verify host lock is always held here */
949 #ifdef OLDCNTDATASCEME
951 WRITE_CONTROL (tmp_control);
952 WRITE_DATA (tmp_data);
954 WRITE_DATA (tmp_data);
955 WRITE_CONTROL (tmp_control);
958 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
959 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
960 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
961 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
962 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
968 * If we have been aborted, and we have a
969 * command in progress, IE the target
970 * still has BSY asserted, then we will
971 * reset the bus, and notify the midlevel
972 * driver to expect sense.
975 WRITE_CONTROL (BASE_CMD);
976 if (STATUS & STAT_BSY) {
977 printk(KERN_WARNING "scsi%d : BST asserted after we've been aborted.\n", hostno);
978 seagate_st0x_bus_reset(NULL);
979 return retcode (DID_RESET);
981 return retcode (st0x_aborted);
983 if (STATUS & STAT_BSY)
986 DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n", hostno, target, STATUS);
987 return retcode (DID_NO_CONNECT);
991 /* Establish current pointers. Take into account scatter / gather */
993 if ((nobuffs = SCint->use_sg)) {
994 #if (DEBUG & DEBUG_SG)
997 printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno, nobuffs);
998 for (i = 0; i < nobuffs; ++i)
999 printk("scsi%d : buffer %d address = %p length = %d\n",
1001 page_address(buffer[i].page) + buffer[i].offset,
1006 buffer = (struct scatterlist *) SCint->request_buffer;
1007 len = buffer->length;
1008 data = page_address(buffer->page) + buffer->offset;
1010 DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
1012 len = SCint->request_bufflen;
1013 data = (unsigned char *) SCint->request_buffer;
1016 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1026 } /* end of switch(reselect) */
1029 * There are several conditions under which we wish to send a message :
1030 * 1. When we are allowing disconnect / reconnect, and need to
1031 * establish the I_T_L nexus via an IDENTIFY with the DiscPriv bit
1034 * 2. When we are doing linked commands, are have the wrong I_T_L
1035 * nexus established and want to send an ABORT message.
1038 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1040 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT)|| (reselect == LINKED_WRONG))? CMD_ATTN : 0));
1042 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT))? CMD_ATTN : 0));
1046 * INFORMATION TRANSFER PHASE
1048 * The nasty looking read / write inline assembler loops we use for
1049 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1050 * the 'C' versions - since we're moving 1024 bytes of data, this
1053 * SJT: The nasty-looking assembler is gone, so it's slower.
1057 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1060 transfersize = SCint->transfersize;
1061 underflow = SCint->underflow;
1064 * Now, we poll the device for status information,
1065 * and handle any requests it makes. Note that since we are unsure
1066 * of how much data will be flowing across the system, etc and
1067 * cannot make reasonable timeouts, that we will instead have the
1068 * midlevel driver handle any timeouts that occur in this phase.
1071 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1073 if (status_read & STAT_PARITY) {
1074 printk(KERN_ERR "scsi%d : got parity error\n", hostno);
1075 st0x_aborted = DID_PARITY;
1078 if (status_read & STAT_REQ) {
1079 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1080 if ((newphase = (status_read & REQ_MASK)) != phase) {
1084 printk ("scsi%d : phase = DATA OUT\n", hostno);
1087 printk ("scsi%d : phase = DATA IN\n", hostno);
1091 ("scsi%d : phase = COMMAND OUT\n", hostno);
1094 printk ("scsi%d : phase = STATUS IN\n", hostno);
1098 ("scsi%d : phase = MESSAGE OUT\n", hostno);
1101 printk ("scsi%d : phase = MESSAGE IN\n", hostno);
1104 printk ("scsi%d : phase = UNKNOWN\n", hostno);
1105 st0x_aborted = DID_ERROR;
1109 switch (status_read & REQ_MASK) {
1112 * If we are in fast mode, then we simply splat
1113 * the data out in word-sized chunks as fast as
1119 printk("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
1120 st0x_aborted = DID_ERROR;
1126 if (fast && transfersize
1127 && !(len % transfersize)
1128 && (len >= transfersize)
1130 && !(transfersize % 4)
1133 DPRINTK (DEBUG_FAST,
1134 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1135 " len = %d, data = %08x\n",
1136 hostno, SCint->underflow,
1137 SCint->transfersize, len,
1140 /* SJT: Start. Fast Write */
1141 #ifdef SEAGATE_USE_ASM
1147 "movl %%eax, (%%edi)\n\t"
1151 "movb %%al, (%%edi)\n\t"
1155 /* input */ :"D" (st0x_dr),
1158 "c" (SCint->transfersize)
1162 #else /* SEAGATE_USE_ASM */
1163 memcpy_toio(st0x_dr, data, transfersize);
1164 #endif /* SEAGATE_USE_ASM */
1166 len -= transfersize;
1167 data += transfersize;
1168 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1171 * We loop as long as we are in a
1172 * data out phase, there is data to
1173 * send, and BSY is still active.
1176 /* SJT: Start. Slow Write. */
1177 #ifdef SEAGATE_USE_ASM
1179 int __dummy_1, __dummy_2;
1182 * We loop as long as we are in a data out phase, there is data to send,
1183 * and BSY is still active.
1185 /* Local variables : len = ecx , data = esi,
1186 st0x_cr_sr = ebx, st0x_dr = edi
1189 /* Test for any data here at all. */
1190 "orl %%ecx, %%ecx\n\t"
1191 "jz 2f\n\t" "cld\n\t"
1192 /* "movl st0x_cr_sr, %%ebx\n\t" */
1193 /* "movl st0x_dr, %%edi\n\t" */
1195 "movb (%%ebx), %%al\n\t"
1199 /* Test for data out phase - STATUS & REQ_MASK should be
1200 REQ_DATAOUT, which is 0. */
1201 "test $0xe, %%al\n\t"
1204 "test $0x10, %%al\n\t"
1207 "movb %%al, (%%edi)\n\t"
1208 "loop 1b\n\t" "2:\n"
1209 /* output */ :"=S" (data), "=c" (len),
1214 : "0" (data), "1" (len),
1219 #else /* SEAGATE_USE_ASM */
1224 if (!(stat & STAT_BSY)
1225 || ((stat & REQ_MASK) !=
1228 if (stat & STAT_REQ) {
1229 WRITE_DATA (*data++);
1233 #endif /* SEAGATE_USE_ASM */
1237 if (!len && nobuffs) {
1240 len = buffer->length;
1241 data = page_address(buffer->page) + buffer->offset;
1243 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1251 #if (DEBUG & (PHASE_DATAIN))
1254 for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN | STAT_REQ); --len) {
1258 #if (DEBUG & (PHASE_DATAIN))
1264 if (fast && transfersize
1265 && !(len % transfersize)
1266 && (len >= transfersize)
1268 && !(transfersize % 4)
1271 DPRINTK (DEBUG_FAST,
1272 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1273 " len = %d, data = %08x\n",
1274 hostno, SCint->underflow,
1275 SCint->transfersize, len,
1278 /* SJT: Start. Fast Read */
1279 #ifdef SEAGATE_USE_ASM
1284 "movl (%%esi), %%eax\n\t"
1288 "movb (%%esi), %%al\n\t"
1293 /* input */ :"S" (st0x_dr),
1296 "c" (SCint->transfersize)
1300 #else /* SEAGATE_USE_ASM */
1301 memcpy_fromio(data, st0x_dr, len);
1302 #endif /* SEAGATE_USE_ASM */
1304 len -= transfersize;
1305 data += transfersize;
1306 #if (DEBUG & PHASE_DATAIN)
1307 printk ("scsi%d: transfered += %d\n", hostno, transfersize);
1308 transfered += transfersize;
1311 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1314 #if (DEBUG & PHASE_DATAIN)
1315 printk ("scsi%d: transfered += %d\n", hostno, len);
1316 transfered += len; /* Assume we'll transfer it all, then
1317 subtract what we *didn't* transfer */
1321 * We loop as long as we are in a data in phase, there is room to read,
1322 * and BSY is still active
1326 #ifdef SEAGATE_USE_ASM
1328 int __dummy_3, __dummy_4;
1330 /* Dummy clobbering variables for the new gcc-2.95 */
1333 * We loop as long as we are in a data in phase, there is room to read,
1334 * and BSY is still active
1336 /* Local variables : ecx = len, edi = data
1337 esi = st0x_cr_sr, ebx = st0x_dr */
1339 /* Test for room to read */
1340 "orl %%ecx, %%ecx\n\t"
1341 "jz 2f\n\t" "cld\n\t"
1342 /* "movl st0x_cr_sr, %%esi\n\t" */
1343 /* "movl st0x_dr, %%ebx\n\t" */
1345 "movb (%%esi), %%al\n\t"
1349 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1350 = STAT_IO, which is 4. */
1351 "movb $0xe, %%ah\n\t"
1352 "andb %%al, %%ah\n\t"
1353 "cmpb $0x04, %%ah\n\t"
1356 "test $0x10, %%al\n\t"
1358 "movb (%%ebx), %%al\n\t"
1360 "loop 1b\n\t" "2:\n"
1361 /* output */ :"=D" (data), "=c" (len),
1366 : "0" (data), "1" (len),
1371 #else /* SEAGATE_USE_ASM */
1376 if (!(stat & STAT_BSY)
1377 || ((stat & REQ_MASK) !=
1380 if (stat & STAT_REQ) {
1385 #endif /* SEAGATE_USE_ASM */
1387 #if (DEBUG & PHASE_DATAIN)
1388 printk ("scsi%d: transfered -= %d\n", hostno, len);
1389 transfered -= len; /* Since we assumed all of Len got *
1390 transfered, correct our mistake */
1394 if (!len && nobuffs) {
1397 len = buffer->length;
1398 data = page_address(buffer->page) + buffer->offset;
1399 DPRINTK (DEBUG_SG, "scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno, len, data);
1404 while (((status_read = STATUS) & STAT_BSY) &&
1405 ((status_read & REQ_MASK) == REQ_CMDOUT))
1406 if (status_read & STAT_REQ) {
1407 WRITE_DATA (*(const unsigned char *) cmnd);
1408 cmnd = 1 + (const unsigned char *)cmnd;
1422 * We can only have sent a MSG OUT if we
1423 * requested to do this by raising ATTN.
1424 * So, we must drop ATTN.
1426 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1428 * If we are reconnecting, then we must
1429 * send an IDENTIFY message in response
1434 WRITE_DATA (IDENTIFY (1, lun));
1435 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
1440 linked_connected = 0;
1441 reselect = CAN_RECONNECT;
1443 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED, "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
1445 DPRINTK (DEBUG_LINKED, "correct\n");
1448 printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1453 switch (message = DATA) {
1455 DANY("seagate: deciding to disconnect\n");
1456 should_reconnect = 1;
1457 current_data = data; /* WDE add */
1458 current_buffer = buffer;
1459 current_bufflen = len; /* WDE add */
1460 current_nobuffs = nobuffs;
1462 linked_connected = 0;
1465 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
1469 case LINKED_CMD_COMPLETE:
1470 case LINKED_FLG_CMD_COMPLETE:
1472 case COMMAND_COMPLETE:
1474 * Note : we should check for underflow here.
1476 DPRINTK(PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
1480 DPRINTK(PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
1484 current_buffer = buffer;
1485 current_bufflen = len; /* WDE add */
1486 current_data = data; /* WDE mod */
1487 current_nobuffs = nobuffs;
1488 DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
1490 case RESTORE_POINTERS:
1491 buffer = current_buffer;
1492 cmnd = current_cmnd;
1493 data = current_data; /* WDE mod */
1494 len = current_bufflen;
1495 nobuffs = current_nobuffs;
1496 DPRINTK(PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
1501 * IDENTIFY distinguishes itself
1502 * from the other messages by
1503 * setting the high bit.
1505 * Note : we need to handle at
1506 * least one outstanding command
1507 * per LUN, and need to hash the
1508 * SCSI command for that I_T_L
1509 * nexus based on the known ID
1510 * (at this point) and LUN.
1513 if (message & 0x80) {
1514 DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n", hostno, target, message & 7);
1517 * We should go into a
1518 * MESSAGE OUT phase, and
1519 * send a MESSAGE_REJECT
1520 * if we run into a message
1521 * that we don't like. The
1522 * seagate driver needs
1524 * restructuring first
1527 DPRINTK (PHASE_MSGIN, "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
1532 printk(KERN_ERR "scsi%d : unknown phase.\n", hostno);
1533 st0x_aborted = DID_ERROR;
1534 } /* end of switch (status_read & REQ_MASK) */
1537 * I really don't care to deal with borken devices in
1538 * each single byte transfer case (ie, message in,
1539 * message out, status), so I'll do the wait here if
1546 } /* if(status_read & STAT_REQ) ends */
1547 } /* while(((status_read = STATUS)...) ends */
1549 DPRINTK(PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT, "scsi%d : Transfered %d bytes\n", hostno, transfered);
1551 #if (DEBUG & PHASE_EXIT)
1552 #if 0 /* Doesn't work for scatter/gather */
1553 printk("Buffer : \n");
1554 for(i = 0; i < 20; ++i)
1555 printk("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1558 printk("scsi%d : status = ", hostno);
1559 scsi_print_status(status);
1560 printk(" message = %02x\n", message);
1563 /* We shouldn't reach this until *after* BSY has been deasserted */
1569 * Fix the message byte so that unsuspecting high level drivers
1570 * don't puke when they see a LINKED COMMAND message in place of
1571 * the COMMAND COMPLETE they may be expecting. Shouldn't be
1572 * necessary, but it's better to be on the safe side.
1574 * A non LINKED* message byte will indicate that the command
1575 * completed, and we are now disconnected.
1579 case LINKED_CMD_COMPLETE:
1580 case LINKED_FLG_CMD_COMPLETE:
1581 message = COMMAND_COMPLETE;
1582 linked_target = current_target;
1583 linked_lun = current_lun;
1584 linked_connected = 1;
1585 DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established for linked command.\n", hostno);
1586 /* We also will need to adjust status to accommodate intermediate
1588 if ((status == INTERMEDIATE_GOOD) || (status == INTERMEDIATE_C_GOOD))
1592 * We should also handle what are "normal" termination
1593 * messages here (ABORT, BUS_DEVICE_RESET?, and
1594 * COMMAND_COMPLETE individually, and flake if things
1598 DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
1599 linked_connected = 0;
1604 if (should_reconnect) {
1605 DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n", hostno);
1606 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1608 WRITE_CONTROL (BASE_CMD);
1610 return retcode (st0x_aborted);
1611 } /* end of internal_command */
1613 static int seagate_st0x_abort(struct scsi_cmnd * SCpnt)
1615 st0x_aborted = DID_ABORT;
1623 * the seagate_st0x_reset function resets the SCSI bus
1625 * May be called with SCpnt = NULL
1628 static int seagate_st0x_bus_reset(struct scsi_cmnd * SCpnt)
1630 /* No timeouts - this command is going to fail because it was reset. */
1631 DANY ("scsi%d: Reseting bus... ", hostno);
1633 /* assert RESET signal on SCSI bus. */
1634 WRITE_CONTROL (BASE_CMD | CMD_RST);
1638 WRITE_CONTROL (BASE_CMD);
1639 st0x_aborted = DID_RESET;
1645 static int seagate_st0x_release(struct Scsi_Host *shost)
1648 free_irq(shost->irq, shost);
1649 release_region(shost->io_port, shost->n_io_port);
1653 static struct scsi_host_template driver_template = {
1654 .detect = seagate_st0x_detect,
1655 .release = seagate_st0x_release,
1656 .info = seagate_st0x_info,
1657 .queuecommand = seagate_st0x_queue_command,
1658 .eh_abort_handler = seagate_st0x_abort,
1659 .eh_bus_reset_handler = seagate_st0x_bus_reset,
1662 .sg_tablesize = SG_ALL,
1664 .use_clustering = DISABLE_CLUSTERING,
1666 #include "scsi_module.c"