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>
100 #include <linux/io.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)
119 #define DANY( msg... ) DPRINTK( 0xffff, msg );
129 #undef LINKED /* Linked commands are currently broken! */
131 #if defined(OVERRIDE) && !defined(CONTROLLER)
132 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
136 #undef SEAGATE_USE_ASM
140 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
141 driver, and Mitsugu Suzuki for information on the ST-01
152 #define CMD_ATTN 0x08
153 #define CMD_START_ARB 0x10
154 #define CMD_EN_PARITY 0x20
155 #define CMD_INTR 0x40
156 #define CMD_DRVR_ENABLE 0x80
162 #define STAT_MSG 0x08
165 #define STAT_MSG 0x02
169 #define STAT_BSY 0x01
171 #define STAT_REQ 0x10
172 #define STAT_SEL 0x20
173 #define STAT_PARITY 0x40
174 #define STAT_ARB_CMPL 0x80
180 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
181 #define REQ_DATAOUT 0
182 #define REQ_DATAIN STAT_IO
183 #define REQ_CMDOUT STAT_CD
184 #define REQ_STATIN (STAT_CD | STAT_IO)
185 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
186 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
188 extern volatile int seagate_st0x_timeout;
191 #define BASE_CMD CMD_EN_PARITY
200 #define PHASE_BUS_FREE 1
201 #define PHASE_ARBITRATION 2
202 #define PHASE_SELECTION 4
203 #define PHASE_DATAIN 8
204 #define PHASE_DATAOUT 0x10
205 #define PHASE_CMDOUT 0x20
206 #define PHASE_MSGIN 0x40
207 #define PHASE_MSGOUT 0x80
208 #define PHASE_STATUSIN 0x100
209 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
210 #define PRINT_COMMAND 0x200
211 #define PHASE_EXIT 0x400
212 #define PHASE_RESELECT 0x800
213 #define DEBUG_FAST 0x1000
214 #define DEBUG_SG 0x2000
215 #define DEBUG_LINKED 0x4000
216 #define DEBUG_BORKEN 0x8000
219 * Control options - these are timeouts specified in .01 seconds.
223 #define ST0X_BUS_FREE_DELAY 25
224 #define ST0X_SELECTION_DELAY 25
226 #define SEAGATE 1 /* these determine the type of the controller */
229 #define ST0X_ID_STR "Seagate ST-01/ST-02"
230 #define FD_ID_STR "TMC-8XX/TMC-950"
232 static int internal_command (unsigned char target, unsigned char lun,
234 void *buff, int bufflen, int reselect);
236 static int incommand; /* set if arbitration has finished
237 and we are in some command phase. */
239 static unsigned int base_address = 0; /* Where the card ROM starts, used to
240 calculate memory mapped register
243 static void __iomem *st0x_cr_sr; /* control register write, status
244 register read. 256 bytes in
246 Read is status of SCSI BUS, as per
249 static void __iomem *st0x_dr; /* data register, read write 256
252 static volatile int st0x_aborted = 0; /* set when we are aborted, ie by a
255 static unsigned char controller_type = 0; /* set to SEAGATE for ST0x
256 boards or FD for TMC-8xx
258 static int irq = IRQ;
260 module_param(base_address, uint, 0);
261 module_param(controller_type, byte, 0);
262 module_param(irq, int, 0);
263 MODULE_LICENSE("GPL");
266 #define retcode(result) (((result) << 16) | (message << 8) | status)
267 #define STATUS ((u8) readb(st0x_cr_sr))
268 #define DATA ((u8) readb(st0x_dr))
269 #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
270 #define WRITE_DATA(d) { writeb((d), st0x_dr); }
273 static unsigned int seagate_bases[] = {
274 0xc8000, 0xca000, 0xcc000,
275 0xce000, 0xdc000, 0xde000
279 const unsigned char *signature;
285 static Signature __initdata signatures[] = {
286 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
287 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
290 * The following two lines are NOT mistakes. One detects ROM revision
291 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
292 * and this is not going to change, the "SEAGATE" and "SCSI" together
293 * are probably "good enough"
296 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
297 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
300 * However, future domain makes several incompatible SCSI boards, so specific
301 * signatures must be used.
304 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
305 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
306 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
307 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
308 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
309 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
310 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
311 {"FUTURE DOMAIN TMC-950", 5, 21, FD},
312 /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
313 {"IBM F1 V1.2009/22/93", 5, 25, FD},
316 #define NUM_SIGNATURES ARRAY_SIZE(signatures)
317 #endif /* n OVERRIDE */
320 * hostno stores the hostnumber, as told to us by the init routine.
323 static int hostno = -1;
324 static void seagate_reconnect_intr (int, void *);
325 static irqreturn_t do_seagate_reconnect_intr (int, void *);
326 static int seagate_st0x_bus_reset(struct scsi_cmnd *);
336 * Support for broken devices :
337 * The Seagate board has a handshaking problem. Namely, a lack
338 * thereof for slow devices. You can blast 600K/second through
339 * it if you are polling for each byte, more if you do a blind
340 * transfer. In the first case, with a fast device, REQ will
341 * transition high-low or high-low-high before your loop restarts
342 * and you'll have no problems. In the second case, the board
343 * will insert wait states for up to 13.2 usecs for REQ to
344 * transition low->high, and everything will work.
346 * However, there's nothing in the state machine that says
347 * you *HAVE* to see a high-low-high set of transitions before
348 * sending the next byte, and slow things like the Trantor CD ROMS
349 * will break because of this.
351 * So, we need to slow things down, which isn't as simple as it
352 * seems. We can't slow things down period, because then people
353 * who don't recompile their kernels will shoot me for ruining
354 * their performance. We need to do it on a case per case basis.
356 * The best for performance will be to, only for borken devices
357 * (this is stored on a per-target basis in the scsi_devices array)
359 * Wait for a low->high transition before continuing with that
360 * transfer. If we timeout, continue anyways. We don't need
361 * a long timeout, because REQ should only be asserted until the
362 * corresponding ACK is received and processed.
364 * Note that we can't use the system timer for this, because of
365 * resolution, and we *really* can't use the timer chip since
366 * gettimeofday() and the beeper routines use that. So,
367 * the best thing for us to do will be to calibrate a timing
368 * loop in the initialization code using the timer chip before
369 * gettimeofday() can screw with it.
371 * FIXME: this is broken (not borken :-). Empty loop costs less than
372 * loop with ISA access in it! -- pavel@ucw.cz
375 static int borken_calibration = 0;
377 static void __init borken_init (void)
379 register int count = 0, start = jiffies + 1, stop = start + 25;
381 /* FIXME: There may be a better approach, this is a straight port for
384 while (time_before (jiffies, start))
386 for (; time_before (jiffies, stop); ++count)
391 * Ok, we now have a count for .25 seconds. Convert to a
392 * count per second and divide by transfer rate in K. */
394 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
396 if (borken_calibration < 1)
397 borken_calibration = 1;
400 static inline void borken_wait (void)
404 for (count = borken_calibration; count && (STATUS & STAT_REQ); --count)
407 #if (DEBUG & DEBUG_BORKEN)
409 printk ("scsi%d : borken timeout\n", hostno);
413 #endif /* def SLOW_RATE */
415 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
416 * contains at least one ISA access, which takes more than 0.125
417 * usec. So if we loop 8 times time in usec, we are safe.
420 #define ULOOP( i ) for (clock = i*8;;)
421 #define TIMEOUT (!(clock--))
423 int __init seagate_st0x_detect (struct scsi_host_template * tpnt)
425 struct Scsi_Host *instance;
427 unsigned long cr, dr;
429 tpnt->proc_name = "seagate";
431 * First, we try for the manual override.
433 DANY ("Autodetecting ST0x / TMC-8xx\n");
436 printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
440 /* If the user specified the controller type from the command line,
441 controller_type will be non-zero, so don't try to detect one */
443 if (!controller_type) {
445 base_address = OVERRIDE;
446 controller_type = CONTROLLER;
448 DANY ("Base address overridden to %x, controller type is %s\n",
450 controller_type == SEAGATE ? "SEAGATE" : "FD");
453 * To detect this card, we simply look for the signature
454 * from the BIOS version notice in all the possible locations
455 * of the ROM's. This has a nice side effect of not trashing
456 * any register locations that might be used by something else.
458 * XXX - note that we probably should be probing the address
459 * space for the on-board RAM instead.
462 for (i = 0; i < ARRAY_SIZE(seagate_bases); ++i) {
463 void __iomem *p = ioremap(seagate_bases[i], 0x2000);
466 for (j = 0; j < NUM_SIGNATURES; ++j)
467 if (check_signature(p + signatures[j].offset, signatures[j].signature, signatures[j].length)) {
468 base_address = seagate_bases[i];
469 controller_type = signatures[j].type;
474 #endif /* OVERRIDE */
476 /* (! controller_type) */
477 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
478 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
481 printk(KERN_INFO "seagate: ST0x/TMC-8xx not detected.\n");
485 cr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
487 st0x_cr_sr = ioremap(cr, 0x100);
488 st0x_dr = ioremap(dr, 0x100);
490 DANY("%s detected. Base address = %x, cr = %x, dr = %x\n",
491 tpnt->name, base_address, cr, dr);
494 * At all times, we will use IRQ 5. Should also check for IRQ3
495 * if we lose our first interrupt.
497 instance = scsi_register (tpnt, 0);
498 if (instance == NULL)
501 hostno = instance->host_no;
502 if (request_irq (irq, do_seagate_reconnect_intr, IRQF_DISABLED, (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", instance)) {
503 printk(KERN_ERR "scsi%d : unable to allocate IRQ%d\n", hostno, irq);
507 instance->io_port = base_address;
509 printk(KERN_INFO "Calibrating borken timer... ");
511 printk(" %d cycles per transfer\n", borken_calibration);
513 printk (KERN_INFO "This is one second... ");
516 ULOOP (1 * 1000 * 1000) {
523 printk ("done, %s options:"
542 #ifdef SEAGATE_USE_ASM
558 static const char *seagate_st0x_info (struct Scsi_Host *shpnt)
560 static char buffer[64];
562 snprintf(buffer, 64, "%s at irq %d, address 0x%05X",
563 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
569 * These are our saved pointers for the outstanding command that is
570 * waiting for a reconnect
573 static unsigned char current_target, current_lun;
574 static unsigned char *current_cmnd, *current_data;
575 static int current_nobuffs;
576 static struct scatterlist *current_buffer;
577 static int current_bufflen;
581 * linked_connected indicates whether or not we are currently connected to
582 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
583 * using linked commands.
586 static int linked_connected = 0;
587 static unsigned char linked_target, linked_lun;
590 static void (*done_fn) (struct scsi_cmnd *) = NULL;
591 static struct scsi_cmnd *SCint = NULL;
594 * These control whether or not disconnect / reconnect will be attempted,
595 * or are being attempted.
598 #define NO_RECONNECT 0
599 #define RECONNECT_NOW 1
600 #define CAN_RECONNECT 2
603 * LINKED_RIGHT indicates that we are currently connected to the correct target
604 * for this command, LINKED_WRONG indicates that we are connected to the wrong
605 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
608 #define LINKED_RIGHT 3
609 #define LINKED_WRONG 4
612 * This determines if we are expecting to reconnect or not.
615 static int should_reconnect = 0;
618 * The seagate_reconnect_intr routine is called when a target reselects the
619 * host adapter. This occurs on the interrupt triggered by the target
623 static irqreturn_t do_seagate_reconnect_intr(int irq, void *dev_id)
626 struct Scsi_Host *dev = dev_id;
628 spin_lock_irqsave (dev->host_lock, flags);
629 seagate_reconnect_intr (irq, dev_id);
630 spin_unlock_irqrestore (dev->host_lock, flags);
634 static void seagate_reconnect_intr (int irq, void *dev_id)
637 struct scsi_cmnd *SCtmp;
639 DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n", hostno);
641 if (!should_reconnect)
642 printk(KERN_WARNING "scsi%d: unexpected interrupt.\n", hostno);
644 should_reconnect = 0;
646 DPRINTK (PHASE_RESELECT, "scsi%d : internal_command(%d, %08x, %08x, RECONNECT_NOW\n",
647 hostno, current_target, current_data, current_bufflen);
649 temp = internal_command (current_target, current_lun, current_cmnd, current_data, current_bufflen, RECONNECT_NOW);
651 if (msg_byte(temp) != DISCONNECT) {
653 DPRINTK(PHASE_RESELECT, "scsi%d : done_fn(%d,%08x)", hostno, hostno, temp);
655 panic ("SCint == NULL in seagate");
658 SCtmp->result = temp;
661 printk(KERN_ERR "done_fn() not defined.\n");
667 * The seagate_st0x_queue_command() function provides a queued interface
668 * to the seagate SCSI driver. Basically, it just passes control onto the
669 * seagate_command() function, after fixing it so that the done_fn()
670 * is set to the one passed to the function. We have to be very careful,
671 * because there are some commands on some devices that do not disconnect,
672 * and if we simply call the done_fn when the command is done then another
673 * command is started and queue_command is called again... We end up
674 * overflowing the kernel stack, and this tends not to be such a good idea.
677 static int recursion_depth = 0;
679 static int seagate_st0x_queue_command(struct scsi_cmnd * SCpnt,
680 void (*done) (struct scsi_cmnd *))
682 int result, reconnect;
683 struct scsi_cmnd *SCtmp;
685 DANY ("seagate: que_command");
687 current_target = SCpnt->device->id;
688 current_lun = SCpnt->device->lun;
689 current_cmnd = SCpnt->cmnd;
690 current_data = (unsigned char *) SCpnt->request_buffer;
691 current_bufflen = SCpnt->request_bufflen;
699 * Set linked command bit in control field of SCSI command.
702 current_cmnd[SCpnt->cmd_len] |= 0x01;
703 if (linked_connected) {
704 DPRINTK (DEBUG_LINKED, "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
705 if (linked_target == current_target && linked_lun == current_lun)
707 DPRINTK(DEBUG_LINKED, "correct\n");
708 reconnect = LINKED_RIGHT;
710 DPRINTK(DEBUG_LINKED, "incorrect\n");
711 reconnect = LINKED_WRONG;
715 reconnect = CAN_RECONNECT;
717 result = internal_command(SCint->device->id, SCint->device->lun, SCint->cmnd,
718 SCint->request_buffer, SCint->request_bufflen, reconnect);
719 if (msg_byte(result) == DISCONNECT)
723 SCtmp->result = result;
731 static int internal_command (unsigned char target, unsigned char lun,
732 const void *cmnd, void *buff, int bufflen, int reselect)
734 unsigned char *data = NULL;
735 struct scatterlist *buffer = NULL;
736 int clock, temp, nobuffs = 0, done = 0, len = 0;
738 int transfered = 0, phase = 0, newphase;
740 register unsigned char status_read;
741 unsigned char tmp_data, tmp_control, status = 0, message = 0;
742 unsigned transfersize = 0, underflow = 0;
744 int borken = (int) SCint->device->borken; /* Does the current target require
751 #if (DEBUG & PRINT_COMMAND)
752 printk("scsi%d : target = %d, command = ", hostno, target);
753 __scsi_print_command((unsigned char *) cmnd);
756 #if (DEBUG & PHASE_RESELECT)
759 printk("scsi%d : reconnecting\n", hostno);
763 printk("scsi%d : connected, can reconnect\n", hostno);
766 printk("scsi%d : connected to wrong target, can reconnect\n",
771 printk("scsi%d : allowed to reconnect\n", hostno);
774 printk("scsi%d : not allowed to reconnect\n", hostno);
778 if (target == (controller_type == SEAGATE ? 7 : 6))
779 return DID_BAD_TARGET;
782 * We work it differently depending on if this is is "the first time,"
783 * or a reconnect. If this is a reselect phase, then SEL will
784 * be asserted, and we must skip selection / arbitration phases.
789 DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
791 * At this point, we should find the logical or of our ID
792 * and the original target's ID on the BUS, with BSY, SEL,
793 * and I/O signals asserted.
795 * After ARBITRATION phase is completed, only SEL, BSY,
796 * and the target ID are asserted. A valid initiator ID
797 * is not on the bus until IO is asserted, so we must wait
802 if ((temp & STAT_IO) && !(temp & STAT_BSY))
805 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
806 return (DID_BAD_INTR << 16);
811 * After I/O is asserted by the target, we can read our ID
812 * and its ID off of the BUS.
815 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
816 DPRINTK (PHASE_RESELECT, "scsi%d : detected reconnect request to different target.\n\tData bus = %d\n", hostno, temp);
817 return (DID_BAD_INTR << 16);
820 if (!(temp & (1 << current_target))) {
821 printk(KERN_WARNING "scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno, temp);
822 return (DID_BAD_INTR << 16);
825 buffer = current_buffer;
826 cmnd = current_cmnd; /* WDE add */
827 data = current_data; /* WDE add */
828 len = current_bufflen; /* WDE add */
829 nobuffs = current_nobuffs;
832 * We have determined that we have been selected. At this
833 * point, we must respond to the reselection by asserting
838 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
840 WRITE_CONTROL (BASE_CMD | CMD_BSY);
844 * The target will drop SEL, and raise BSY, at which time
849 if (!(STATUS & STAT_SEL))
852 WRITE_CONTROL (BASE_CMD | CMD_INTR);
853 DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
854 return (DID_BAD_INTR << 16);
857 WRITE_CONTROL (BASE_CMD);
859 * At this point, we have connected with the target
860 * and can get on with our lives.
866 * This is a bletcherous hack, just as bad as the Unix #!
867 * interpreter stuff. If it turns out we are using the wrong
868 * I_T_L nexus, the easiest way to deal with it is to go into
869 * our INFORMATION TRANSFER PHASE code, send a ABORT
870 * message on MESSAGE OUT phase, and then loop back to here.
874 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
879 * On entry, we make sure that the BUS is in a BUS FREE
880 * phase, by insuring that both BSY and SEL are low for
881 * at least one bus settle delay. Several reads help
882 * eliminate wire glitch.
886 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
887 clock = jiffies + ST0X_BUS_FREE_DELAY;
889 while (((STATUS | STATUS | STATUS) & (STAT_BSY | STAT_SEL)) && (!st0x_aborted) && time_before (jiffies, clock))
892 if (time_after (jiffies, clock))
893 return retcode (DID_BUS_BUSY);
894 else if (st0x_aborted)
895 return retcode (st0x_aborted);
897 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
899 clock = jiffies + ST0X_SELECTION_DELAY;
902 * Arbitration/selection procedure :
904 * 2. Write HOST adapter address bit
905 * 3. Set start arbitration.
906 * 4. We get either ARBITRATION COMPLETE or SELECT at this
908 * 5. OR our ID and targets on bus.
909 * 6. Enable SCSI drivers and asserted SEL and ATTN
913 /* FIXME: verify host lock is always held here */
915 WRITE_DATA((controller_type == SEAGATE) ? 0x80 : 0x40);
916 WRITE_CONTROL(CMD_START_ARB);
918 ULOOP (ST0X_SELECTION_DELAY * 10000) {
919 status_read = STATUS;
920 if (status_read & STAT_ARB_CMPL)
922 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
924 if (TIMEOUT || (status_read & STAT_SEL)) {
925 printk(KERN_WARNING "scsi%d : arbitration lost or timeout.\n", hostno);
926 WRITE_CONTROL (BASE_CMD);
927 return retcode (DID_NO_CONNECT);
930 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
934 * When the SCSI device decides that we're gawking at it,
935 * it will respond by asserting BUSY on the bus.
937 * Note : the Seagate ST-01/02 product manual says that we
938 * should twiddle the DATA register before the control
939 * register. However, this does not work reliably so we do
940 * it the other way around.
942 * Probably could be a problem with arbitration too, we
943 * really should try this with a SCSI protocol or logic
944 * analyzer to see what is going on.
946 tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
947 tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
949 /* FIXME: verify host lock is always held here */
950 #ifdef OLDCNTDATASCEME
952 WRITE_CONTROL (tmp_control);
953 WRITE_DATA (tmp_data);
955 WRITE_DATA (tmp_data);
956 WRITE_CONTROL (tmp_control);
959 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
960 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
961 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
962 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
963 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
969 * If we have been aborted, and we have a
970 * command in progress, IE the target
971 * still has BSY asserted, then we will
972 * reset the bus, and notify the midlevel
973 * driver to expect sense.
976 WRITE_CONTROL (BASE_CMD);
977 if (STATUS & STAT_BSY) {
978 printk(KERN_WARNING "scsi%d : BST asserted after we've been aborted.\n", hostno);
979 seagate_st0x_bus_reset(NULL);
980 return retcode (DID_RESET);
982 return retcode (st0x_aborted);
984 if (STATUS & STAT_BSY)
987 DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n", hostno, target, STATUS);
988 return retcode (DID_NO_CONNECT);
992 /* Establish current pointers. Take into account scatter / gather */
994 if ((nobuffs = SCint->use_sg)) {
995 #if (DEBUG & DEBUG_SG)
998 printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno, nobuffs);
999 for (i = 0; i < nobuffs; ++i)
1000 printk("scsi%d : buffer %d address = %p length = %d\n",
1002 page_address(buffer[i].page) + buffer[i].offset,
1007 buffer = (struct scatterlist *) SCint->request_buffer;
1008 len = buffer->length;
1009 data = page_address(buffer->page) + buffer->offset;
1011 DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
1013 len = SCint->request_bufflen;
1014 data = (unsigned char *) SCint->request_buffer;
1017 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1027 } /* end of switch(reselect) */
1030 * There are several conditions under which we wish to send a message :
1031 * 1. When we are allowing disconnect / reconnect, and need to
1032 * establish the I_T_L nexus via an IDENTIFY with the DiscPriv bit
1035 * 2. When we are doing linked commands, are have the wrong I_T_L
1036 * nexus established and want to send an ABORT message.
1039 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1041 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT)|| (reselect == LINKED_WRONG))? CMD_ATTN : 0));
1043 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT))? CMD_ATTN : 0));
1047 * INFORMATION TRANSFER PHASE
1049 * The nasty looking read / write inline assembler loops we use for
1050 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1051 * the 'C' versions - since we're moving 1024 bytes of data, this
1054 * SJT: The nasty-looking assembler is gone, so it's slower.
1058 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1061 transfersize = SCint->transfersize;
1062 underflow = SCint->underflow;
1065 * Now, we poll the device for status information,
1066 * and handle any requests it makes. Note that since we are unsure
1067 * of how much data will be flowing across the system, etc and
1068 * cannot make reasonable timeouts, that we will instead have the
1069 * midlevel driver handle any timeouts that occur in this phase.
1072 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1074 if (status_read & STAT_PARITY) {
1075 printk(KERN_ERR "scsi%d : got parity error\n", hostno);
1076 st0x_aborted = DID_PARITY;
1079 if (status_read & STAT_REQ) {
1080 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1081 if ((newphase = (status_read & REQ_MASK)) != phase) {
1085 printk ("scsi%d : phase = DATA OUT\n", hostno);
1088 printk ("scsi%d : phase = DATA IN\n", hostno);
1092 ("scsi%d : phase = COMMAND OUT\n", hostno);
1095 printk ("scsi%d : phase = STATUS IN\n", hostno);
1099 ("scsi%d : phase = MESSAGE OUT\n", hostno);
1102 printk ("scsi%d : phase = MESSAGE IN\n", hostno);
1105 printk ("scsi%d : phase = UNKNOWN\n", hostno);
1106 st0x_aborted = DID_ERROR;
1110 switch (status_read & REQ_MASK) {
1113 * If we are in fast mode, then we simply splat
1114 * the data out in word-sized chunks as fast as
1120 printk("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
1121 st0x_aborted = DID_ERROR;
1127 if (fast && transfersize
1128 && !(len % transfersize)
1129 && (len >= transfersize)
1131 && !(transfersize % 4)
1134 DPRINTK (DEBUG_FAST,
1135 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1136 " len = %d, data = %08x\n",
1137 hostno, SCint->underflow,
1138 SCint->transfersize, len,
1141 /* SJT: Start. Fast Write */
1142 #ifdef SEAGATE_USE_ASM
1148 "movl %%eax, (%%edi)\n\t"
1152 "movb %%al, (%%edi)\n\t"
1156 /* input */ :"D" (st0x_dr),
1159 "c" (SCint->transfersize)
1163 #else /* SEAGATE_USE_ASM */
1164 memcpy_toio(st0x_dr, data, transfersize);
1165 #endif /* SEAGATE_USE_ASM */
1167 len -= transfersize;
1168 data += transfersize;
1169 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1172 * We loop as long as we are in a
1173 * data out phase, there is data to
1174 * send, and BSY is still active.
1177 /* SJT: Start. Slow Write. */
1178 #ifdef SEAGATE_USE_ASM
1180 int __dummy_1, __dummy_2;
1183 * We loop as long as we are in a data out phase, there is data to send,
1184 * and BSY is still active.
1186 /* Local variables : len = ecx , data = esi,
1187 st0x_cr_sr = ebx, st0x_dr = edi
1190 /* Test for any data here at all. */
1191 "orl %%ecx, %%ecx\n\t"
1192 "jz 2f\n\t" "cld\n\t"
1193 /* "movl st0x_cr_sr, %%ebx\n\t" */
1194 /* "movl st0x_dr, %%edi\n\t" */
1196 "movb (%%ebx), %%al\n\t"
1200 /* Test for data out phase - STATUS & REQ_MASK should be
1201 REQ_DATAOUT, which is 0. */
1202 "test $0xe, %%al\n\t"
1205 "test $0x10, %%al\n\t"
1208 "movb %%al, (%%edi)\n\t"
1209 "loop 1b\n\t" "2:\n"
1210 /* output */ :"=S" (data), "=c" (len),
1215 : "0" (data), "1" (len),
1220 #else /* SEAGATE_USE_ASM */
1225 if (!(stat & STAT_BSY)
1226 || ((stat & REQ_MASK) !=
1229 if (stat & STAT_REQ) {
1230 WRITE_DATA (*data++);
1234 #endif /* SEAGATE_USE_ASM */
1238 if (!len && nobuffs) {
1241 len = buffer->length;
1242 data = page_address(buffer->page) + buffer->offset;
1244 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1252 #if (DEBUG & (PHASE_DATAIN))
1255 for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN | STAT_REQ); --len) {
1259 #if (DEBUG & (PHASE_DATAIN))
1265 if (fast && transfersize
1266 && !(len % transfersize)
1267 && (len >= transfersize)
1269 && !(transfersize % 4)
1272 DPRINTK (DEBUG_FAST,
1273 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1274 " len = %d, data = %08x\n",
1275 hostno, SCint->underflow,
1276 SCint->transfersize, len,
1279 /* SJT: Start. Fast Read */
1280 #ifdef SEAGATE_USE_ASM
1285 "movl (%%esi), %%eax\n\t"
1289 "movb (%%esi), %%al\n\t"
1294 /* input */ :"S" (st0x_dr),
1297 "c" (SCint->transfersize)
1301 #else /* SEAGATE_USE_ASM */
1302 memcpy_fromio(data, st0x_dr, len);
1303 #endif /* SEAGATE_USE_ASM */
1305 len -= transfersize;
1306 data += transfersize;
1307 #if (DEBUG & PHASE_DATAIN)
1308 printk ("scsi%d: transfered += %d\n", hostno, transfersize);
1309 transfered += transfersize;
1312 DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
1315 #if (DEBUG & PHASE_DATAIN)
1316 printk ("scsi%d: transfered += %d\n", hostno, len);
1317 transfered += len; /* Assume we'll transfer it all, then
1318 subtract what we *didn't* transfer */
1322 * We loop as long as we are in a data in phase, there is room to read,
1323 * and BSY is still active
1327 #ifdef SEAGATE_USE_ASM
1329 int __dummy_3, __dummy_4;
1331 /* Dummy clobbering variables for the new gcc-2.95 */
1334 * We loop as long as we are in a data in phase, there is room to read,
1335 * and BSY is still active
1337 /* Local variables : ecx = len, edi = data
1338 esi = st0x_cr_sr, ebx = st0x_dr */
1340 /* Test for room to read */
1341 "orl %%ecx, %%ecx\n\t"
1342 "jz 2f\n\t" "cld\n\t"
1343 /* "movl st0x_cr_sr, %%esi\n\t" */
1344 /* "movl st0x_dr, %%ebx\n\t" */
1346 "movb (%%esi), %%al\n\t"
1350 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1351 = STAT_IO, which is 4. */
1352 "movb $0xe, %%ah\n\t"
1353 "andb %%al, %%ah\n\t"
1354 "cmpb $0x04, %%ah\n\t"
1357 "test $0x10, %%al\n\t"
1359 "movb (%%ebx), %%al\n\t"
1361 "loop 1b\n\t" "2:\n"
1362 /* output */ :"=D" (data), "=c" (len),
1367 : "0" (data), "1" (len),
1372 #else /* SEAGATE_USE_ASM */
1377 if (!(stat & STAT_BSY)
1378 || ((stat & REQ_MASK) !=
1381 if (stat & STAT_REQ) {
1386 #endif /* SEAGATE_USE_ASM */
1388 #if (DEBUG & PHASE_DATAIN)
1389 printk ("scsi%d: transfered -= %d\n", hostno, len);
1390 transfered -= len; /* Since we assumed all of Len got *
1391 transfered, correct our mistake */
1395 if (!len && nobuffs) {
1398 len = buffer->length;
1399 data = page_address(buffer->page) + buffer->offset;
1400 DPRINTK (DEBUG_SG, "scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno, len, data);
1405 while (((status_read = STATUS) & STAT_BSY) &&
1406 ((status_read & REQ_MASK) == REQ_CMDOUT))
1407 if (status_read & STAT_REQ) {
1408 WRITE_DATA (*(const unsigned char *) cmnd);
1409 cmnd = 1 + (const unsigned char *)cmnd;
1423 * We can only have sent a MSG OUT if we
1424 * requested to do this by raising ATTN.
1425 * So, we must drop ATTN.
1427 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1429 * If we are reconnecting, then we must
1430 * send an IDENTIFY message in response
1435 WRITE_DATA (IDENTIFY (1, lun));
1436 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
1441 linked_connected = 0;
1442 reselect = CAN_RECONNECT;
1444 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED, "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
1446 DPRINTK (DEBUG_LINKED, "correct\n");
1449 printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1454 switch (message = DATA) {
1456 DANY("seagate: deciding to disconnect\n");
1457 should_reconnect = 1;
1458 current_data = data; /* WDE add */
1459 current_buffer = buffer;
1460 current_bufflen = len; /* WDE add */
1461 current_nobuffs = nobuffs;
1463 linked_connected = 0;
1466 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
1470 case LINKED_CMD_COMPLETE:
1471 case LINKED_FLG_CMD_COMPLETE:
1473 case COMMAND_COMPLETE:
1475 * Note : we should check for underflow here.
1477 DPRINTK(PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
1481 DPRINTK(PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
1485 current_buffer = buffer;
1486 current_bufflen = len; /* WDE add */
1487 current_data = data; /* WDE mod */
1488 current_nobuffs = nobuffs;
1489 DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
1491 case RESTORE_POINTERS:
1492 buffer = current_buffer;
1493 cmnd = current_cmnd;
1494 data = current_data; /* WDE mod */
1495 len = current_bufflen;
1496 nobuffs = current_nobuffs;
1497 DPRINTK(PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
1502 * IDENTIFY distinguishes itself
1503 * from the other messages by
1504 * setting the high bit.
1506 * Note : we need to handle at
1507 * least one outstanding command
1508 * per LUN, and need to hash the
1509 * SCSI command for that I_T_L
1510 * nexus based on the known ID
1511 * (at this point) and LUN.
1514 if (message & 0x80) {
1515 DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n", hostno, target, message & 7);
1518 * We should go into a
1519 * MESSAGE OUT phase, and
1520 * send a MESSAGE_REJECT
1521 * if we run into a message
1522 * that we don't like. The
1523 * seagate driver needs
1525 * restructuring first
1528 DPRINTK (PHASE_MSGIN, "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
1533 printk(KERN_ERR "scsi%d : unknown phase.\n", hostno);
1534 st0x_aborted = DID_ERROR;
1535 } /* end of switch (status_read & REQ_MASK) */
1538 * I really don't care to deal with borken devices in
1539 * each single byte transfer case (ie, message in,
1540 * message out, status), so I'll do the wait here if
1547 } /* if(status_read & STAT_REQ) ends */
1548 } /* while(((status_read = STATUS)...) ends */
1550 DPRINTK(PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT, "scsi%d : Transfered %d bytes\n", hostno, transfered);
1552 #if (DEBUG & PHASE_EXIT)
1553 #if 0 /* Doesn't work for scatter/gather */
1554 printk("Buffer : \n");
1555 for(i = 0; i < 20; ++i)
1556 printk("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1559 printk("scsi%d : status = ", hostno);
1560 scsi_print_status(status);
1561 printk(" message = %02x\n", message);
1564 /* We shouldn't reach this until *after* BSY has been deasserted */
1570 * Fix the message byte so that unsuspecting high level drivers
1571 * don't puke when they see a LINKED COMMAND message in place of
1572 * the COMMAND COMPLETE they may be expecting. Shouldn't be
1573 * necessary, but it's better to be on the safe side.
1575 * A non LINKED* message byte will indicate that the command
1576 * completed, and we are now disconnected.
1580 case LINKED_CMD_COMPLETE:
1581 case LINKED_FLG_CMD_COMPLETE:
1582 message = COMMAND_COMPLETE;
1583 linked_target = current_target;
1584 linked_lun = current_lun;
1585 linked_connected = 1;
1586 DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established for linked command.\n", hostno);
1587 /* We also will need to adjust status to accommodate intermediate
1589 if ((status == INTERMEDIATE_GOOD) || (status == INTERMEDIATE_C_GOOD))
1593 * We should also handle what are "normal" termination
1594 * messages here (ABORT, BUS_DEVICE_RESET?, and
1595 * COMMAND_COMPLETE individually, and flake if things
1599 DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
1600 linked_connected = 0;
1605 if (should_reconnect) {
1606 DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n", hostno);
1607 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1609 WRITE_CONTROL (BASE_CMD);
1611 return retcode (st0x_aborted);
1612 } /* end of internal_command */
1614 static int seagate_st0x_abort(struct scsi_cmnd * SCpnt)
1616 st0x_aborted = DID_ABORT;
1624 * the seagate_st0x_reset function resets the SCSI bus
1626 * May be called with SCpnt = NULL
1629 static int seagate_st0x_bus_reset(struct scsi_cmnd * SCpnt)
1631 /* No timeouts - this command is going to fail because it was reset. */
1632 DANY ("scsi%d: Reseting bus... ", hostno);
1634 /* assert RESET signal on SCSI bus. */
1635 WRITE_CONTROL (BASE_CMD | CMD_RST);
1639 WRITE_CONTROL (BASE_CMD);
1640 st0x_aborted = DID_RESET;
1646 static int seagate_st0x_release(struct Scsi_Host *shost)
1649 free_irq(shost->irq, shost);
1650 release_region(shost->io_port, shost->n_io_port);
1654 static struct scsi_host_template driver_template = {
1655 .detect = seagate_st0x_detect,
1656 .release = seagate_st0x_release,
1657 .info = seagate_st0x_info,
1658 .queuecommand = seagate_st0x_queue_command,
1659 .eh_abort_handler = seagate_st0x_abort,
1660 .eh_bus_reset_handler = seagate_st0x_bus_reset,
1663 .sg_tablesize = SG_ALL,
1665 .use_clustering = DISABLE_CLUSTERING,
1667 #include "scsi_module.c"