Import 2.1.118
[davej-history.git] / drivers / scsi / seagate.c
blobf5a316d313fd2f6a8d7c96997b8743adaa9c0c9a
1 /*
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? :-) pavel@ucw.cz]
10 * This card does all the I/O via memory mapped I/O, so there is no need
11 * to check or allocate a region of the I/O address space.
14 /* 1996 - to use new read{b,w,l}, write{b,w,l}, and phys_to_virt
15 * macros, replaced assembler routines with C. There's probably a
16 * performance hit, but I only have a cdrom and can't tell. Define
17 * SEAGATE_USE_ASM if you want the old assembler code -- SJT
19 * 1998-jul-29 - created DPRINTK macros and made it work under
20 * linux 2.1.112, simplified some #defines etc. <pavel@ucw.cz>
24 * Configuration :
25 * To use without BIOS -DOVERRIDE=base_address -DCONTROLLER=FD or SEAGATE
26 * -DIRQ will override the default of 5.
27 * Note: You can now set these options from the kernel's "command line".
28 * The syntax is:
30 * st0x=ADDRESS,IRQ (for a Seagate controller)
31 * or:
32 * tmc8xx=ADDRESS,IRQ (for a TMC-8xx or TMC-950 controller)
33 * eg:
34 * tmc8xx=0xC8000,15
36 * will configure the driver for a TMC-8xx style controller using IRQ 15
37 * with a base address of 0xC8000.
39 * -DARBITRATE
40 * Will cause the host adapter to arbitrate for the
41 * bus for better SCSI-II compatibility, rather than just
42 * waiting for BUS FREE and then doing its thing. Should
43 * let us do one command per Lun when I integrate my
44 * reorganization changes into the distribution sources.
46 * -DDEBUG=65535
47 * Will activate debug code.
49 * -DFAST or -DFAST32
50 * Will use blind transfers where possible
52 * -DPARITY
53 * This will enable parity.
55 * -DSEAGATE_USE_ASM
56 * Will use older seagate assembly code. should be (very small amount)
57 * Faster.
59 * -DSLOW_RATE=50
60 * Will allow compatibility with broken devices that don't
61 * handshake fast enough (ie, some CD ROM's) for the Seagate
62 * code.
64 * 50 is some number, It will let you specify a default
65 * transfer rate if handshaking isn't working correctly.
67 * -DOLDCNTDATASCEME There is a new sceme to set the CONTROL
68 * and DATA reigsters which complies more closely
69 * with the SCSI2 standard. This hopefully eliminates
70 * the need to swap the order these registers are
71 * 'messed' with. It makes the following two options
72 * obsolete. To reenable the old sceme define this.
74 * The following to options are patches from the SCSI.HOWTO
76 * -DSWAPSTAT This will swap the definitions for STAT_MSG and STAT_CD.
78 * -DSWAPCNTDATA This will swap the order that seagate.c messes with
79 * the CONTROL an DATA registers.
82 #include <linux/module.h>
84 #include <asm/io.h>
85 #include <asm/system.h>
86 #include <asm/spinlock.h>
87 #include <linux/signal.h>
88 #include <linux/sched.h>
89 #include <linux/string.h>
90 #include <linux/config.h>
91 #include <linux/proc_fs.h>
92 #include <linux/init.h>
94 #include <linux/blk.h>
95 #include "scsi.h"
96 #include "hosts.h"
97 #include "seagate.h"
98 #include "constants.h"
99 #include <linux/stat.h>
100 #include <asm/uaccess.h>
101 #include "sd.h"
102 #include <scsi/scsi_ioctl.h>
103 #include <asm/delay.h>
105 #ifdef DEBUG
106 #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
107 #else
108 #define DPRINTK( when, msg... ) do { } while (0)
109 #endif
110 #define DANY( msg... ) DPRINTK( 0xffff, msg );
112 static struct proc_dir_entry proc_scsi_seagate =
114 PROC_SCSI_SEAGATE, 7, "seagate",
115 S_IFDIR | S_IRUGO | S_IXUGO, 2
118 #ifndef IRQ
119 #define IRQ 5
120 #endif
122 #ifdef FAST32
123 #define FAST
124 #endif
126 #undef LINKED /* Linked commands are currently broken! */
128 #if defined(OVERRIDE) && !defined(CONTROLLER)
129 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
130 #endif
133 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
134 driver, and Mitsugu Suzuki for information on the ST-01
135 SCSI host.
139 CONTROL defines
142 #define CMD_RST 0x01
143 #define CMD_SEL 0x02
144 #define CMD_BSY 0x04
145 #define CMD_ATTN 0x08
146 #define CMD_START_ARB 0x10
147 #define CMD_EN_PARITY 0x20
148 #define CMD_INTR 0x40
149 #define CMD_DRVR_ENABLE 0x80
152 STATUS
154 #ifdef SWAPSTAT
155 #define STAT_MSG 0x08
156 #define STAT_CD 0x02
157 #else
158 #define STAT_MSG 0x02
159 #define STAT_CD 0x08
160 #endif
162 #define STAT_BSY 0x01
163 #define STAT_IO 0x04
164 #define STAT_REQ 0x10
165 #define STAT_SEL 0x20
166 #define STAT_PARITY 0x40
167 #define STAT_ARB_CMPL 0x80
170 REQUESTS
173 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
174 #define REQ_DATAOUT 0
175 #define REQ_DATAIN STAT_IO
176 #define REQ_CMDOUT STAT_CD
177 #define REQ_STATIN (STAT_CD | STAT_IO)
178 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
179 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
181 extern volatile int seagate_st0x_timeout;
183 #ifdef PARITY
184 #define BASE_CMD CMD_EN_PARITY
185 #else
186 #define BASE_CMD 0
187 #endif
190 Debugging code
193 #define PHASE_BUS_FREE 1
194 #define PHASE_ARBITRATION 2
195 #define PHASE_SELECTION 4
196 #define PHASE_DATAIN 8
197 #define PHASE_DATAOUT 0x10
198 #define PHASE_CMDOUT 0x20
199 #define PHASE_MSGIN 0x40
200 #define PHASE_MSGOUT 0x80
201 #define PHASE_STATUSIN 0x100
202 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
203 #define PRINT_COMMAND 0x200
204 #define PHASE_EXIT 0x400
205 #define PHASE_RESELECT 0x800
206 #define DEBUG_FAST 0x1000
207 #define DEBUG_SG 0x2000
208 #define DEBUG_LINKED 0x4000
209 #define DEBUG_BORKEN 0x8000
212 * Control options - these are timeouts specified in .01 seconds.
215 /* 30, 20 work */
216 #define ST0X_BUS_FREE_DELAY 25
217 #define ST0X_SELECTION_DELAY 25
219 #define SEAGATE 1 /* these determine the type of the controller */
220 #define FD 2
222 #define ST0X_ID_STR "Seagate ST-01/ST-02"
223 #define FD_ID_STR "TMC-8XX/TMC-950"
226 static int internal_command (unsigned char target, unsigned char lun,
227 const void *cmnd,
228 void *buff, int bufflen, int reselect);
230 static int incommand; /* set if arbitration has finished
231 and we are in some command phase. */
233 static unsigned int base_address = 0; /* Where the card ROM starts, used to
234 calculate memory mapped register
235 location. */
237 static unsigned long st0x_cr_sr; /* control register write, status
238 register read. 256 bytes in
239 length.
240 Read is status of SCSI BUS, as per
241 STAT masks. */
243 static unsigned long st0x_dr; /* data register, read write 256
244 bytes in length. */
246 static volatile int st0x_aborted = 0; /* set when we are aborted, ie by a
247 time out, etc. */
249 static unsigned char controller_type = 0; /* set to SEAGATE for ST0x
250 boards or FD for TMC-8xx
251 boards */
252 static int irq = IRQ;
254 #define retcode(result) (((result) << 16) | (message << 8) | status)
255 #define STATUS ((u8) readb(st0x_cr_sr))
256 #define DATA ((u8) readb(st0x_dr))
257 #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
258 #define WRITE_DATA(d) { writeb((d), st0x_dr); }
260 void st0x_setup (char *str, int *ints)
262 controller_type = SEAGATE;
263 base_address = ints[1];
264 irq = ints[2];
267 void tmc8xx_setup (char *str, int *ints)
269 controller_type = FD;
270 base_address = ints[1];
271 irq = ints[2];
274 #ifndef OVERRIDE
275 static unsigned int seagate_bases[] =
277 0xc8000, 0xca000, 0xcc000,
278 0xce000, 0xdc000, 0xde000
281 typedef struct
283 const unsigned char *signature;
284 unsigned offset;
285 unsigned length;
286 unsigned char type;
288 Signature;
290 static const Signature __initdata signatures[] =
292 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
293 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
296 * The following two lines are NOT mistakes. One detects ROM revision
297 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
298 * and this is not going to change, the "SEAGATE" and "SCSI" together
299 * are probably "good enough"
302 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
303 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
306 * However, future domain makes several incompatible SCSI boards, so specific
307 * signatures must be used.
310 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
311 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
312 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
313 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
314 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
315 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
316 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
317 {"FUTURE DOMAIN TMC-950", 5, 21, FD},
320 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
321 #endif /* n OVERRIDE */
324 * hostno stores the hostnumber, as told to us by the init routine.
327 static int hostno = -1;
328 static void seagate_reconnect_intr (int, void *, struct pt_regs *);
329 static void do_seagate_reconnect_intr (int, void *, struct pt_regs *);
331 #ifdef FAST
332 static int fast = 1;
333 #else
334 #define fast 0
335 #endif
337 #ifdef SLOW_RATE
339 * Support for broken devices :
340 * The Seagate board has a handshaking problem. Namely, a lack
341 * thereof for slow devices. You can blast 600K/second through
342 * it if you are polling for each byte, more if you do a blind
343 * transfer. In the first case, with a fast device, REQ will
344 * transition high-low or high-low-high before your loop restarts
345 * and you'll have no problems. In the second case, the board
346 * will insert wait states for up to 13.2 usecs for REQ to
347 * transition low->high, and everything will work.
349 * However, there's nothing in the state machine that says
350 * you *HAVE* to see a high-low-high set of transitions before
351 * sending the next byte, and slow things like the Trantor CD ROMS
352 * will break because of this.
354 * So, we need to slow things down, which isn't as simple as it
355 * seems. We can't slow things down period, because then people
356 * who don't recompile their kernels will shoot me for ruining
357 * their performance. We need to do it on a case per case basis.
359 * The best for performance will be to, only for borken devices
360 * (this is stored on a per-target basis in the scsi_devices array)
362 * Wait for a low->high transition before continuing with that
363 * transfer. If we timeout, continue anyways. We don't need
364 * a long timeout, because REQ should only be asserted until the
365 * corresponding ACK is received and processed.
367 * Note that we can't use the system timer for this, because of
368 * resolution, and we *really* can't use the timer chip since
369 * gettimeofday() and the beeper routines use that. So,
370 * the best thing for us to do will be to calibrate a timing
371 * loop in the initialization code using the timer chip before
372 * gettimeofday() can screw with it.
374 * FIXME: this is broken (not borken :-). Empty loop costs less than
375 * loop with ISA access in it! -- pavel@ucw.cz
378 static int borken_calibration = 0;
379 static void __init borken_init (void)
381 register int count = 0, start = jiffies + 1, stop = start + 25;
383 while (jiffies < start) ;
384 for (; jiffies < stop; ++count) ;
387 * Ok, we now have a count for .25 seconds. Convert to a
388 * count per second and divide by transfer rate in K. */
390 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
392 if (borken_calibration < 1)
393 borken_calibration = 1;
396 static inline void borken_wait (void)
398 register int count;
400 for (count = borken_calibration; count && (STATUS & STAT_REQ); --count) ;
401 #if (DEBUG & DEBUG_BORKEN)
402 if (count)
403 printk ("scsi%d : borken timeout\n", hostno);
404 #endif
407 #endif /* def SLOW_RATE */
409 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
410 * contains at least one ISA access, which takes more than 0.125
411 * usec. So if we loop 8 times time in usec, we are safe.
414 #define ULOOP( i ) for (clock = i*8;;)
415 #define TIMEOUT (!(clock--))
417 int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
419 struct Scsi_Host *instance;
420 int i, j;
422 tpnt->proc_dir = &proc_scsi_seagate;
424 * First, we try for the manual override. */
425 DANY ("Autodetecting ST0x / TMC-8xx\n");
427 if (hostno != -1) {
428 printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
429 return 0;
432 /* If the user specified the controller type from the command line,
433 controller_type will be non-zero, so don't try to detect one */
435 if (!controller_type)
437 #ifdef OVERRIDE
438 base_address = OVERRIDE;
439 controller_type = CONTROLLER;
441 DANY("Base address overridden to %x, controller type is %s\n",
442 base_address, controller_type == SEAGATE ? "SEAGATE" : "FD");
443 #else /* OVERRIDE */
445 * To detect this card, we simply look for the signature
446 * from the BIOS version notice in all the possible locations
447 * of the ROM's. This has a nice side effect of not trashing
448 * any register locations that might be used by something else.
450 * XXX - note that we probably should be probing the address
451 * space for the on-board RAM instead.
454 for (i = 0; i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i)
456 for (j = 0; !base_address && j < NUM_SIGNATURES; ++j)
457 if (check_signature (seagate_bases[i] + signatures[j].offset,
458 signatures[j].signature, signatures[j].length))
460 base_address = seagate_bases[i];
461 controller_type = signatures[j].type;
463 #endif /* OVERRIDE */
464 } /* (! controller_type) */
466 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
467 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
469 if (!base_address) {
470 DANY ("ST0x / TMC-8xx not detected.\n");
471 return 0;
474 st0x_cr_sr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
475 st0x_dr = st0x_cr_sr + 0x200;
477 DANY ("%s detected. Base address = %x, cr = %x, dr = %x\n",
478 tpnt->name, base_address, st0x_cr_sr, st0x_dr);
481 * At all times, we will use IRQ 5. Should also check for IRQ3 if we
482 * loose our first interrupt.
484 instance = scsi_register (tpnt, 0);
485 hostno = instance->host_no;
486 if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT,
487 (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", NULL)) {
488 printk ("scsi%d : unable to allocate IRQ%d\n", hostno, irq);
489 return 0;
491 instance->irq = irq;
492 instance->io_port = base_address;
493 #ifdef SLOW_RATE
494 printk( "Calibrating borken timer... " );
495 borken_init ();
496 printk( " %d cycles per transfer\n", borken_calibration );
497 #endif
499 printk( "This is one second... " );
501 int clock;
502 ULOOP( 1*1000*1000 ) {
503 volatile int x = STATUS;
504 if (TIMEOUT) break;
508 printk ("done, %s options:"
509 #ifdef ARBITRATE
510 " ARBITRATE"
511 #endif
512 #ifdef DEBUG
513 " DEBUG"
514 #endif
515 #ifdef FAST
516 " FAST"
517 #ifdef FAST32
518 "32"
519 #endif
520 #endif
521 #ifdef LINKED
522 " LINKED"
523 #endif
524 #ifdef PARITY
525 " PARITY"
526 #endif
527 #ifdef SEAGATE_USE_ASM
528 " SEAGATE_USE_ASM"
529 #endif
530 #ifdef SLOW_RATE
531 " SLOW_RATE"
532 #endif
533 #ifdef SWAPSTAT
534 " SWAPSTAT"
535 #endif
536 #ifdef SWAPCNTDATA
537 " SWAPCNTDATA"
538 #endif
539 "\n", tpnt->name);
540 return 1;
543 const char *seagate_st0x_info (struct Scsi_Host *shpnt)
545 static char buffer[64];
547 sprintf (buffer, "%s at irq %d, address 0x%05X",
548 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
549 irq, base_address);
550 return buffer;
554 * These are our saved pointers for the outstanding command that is
555 * waiting for a reconnect
558 static unsigned char current_target, current_lun;
559 static unsigned char *current_cmnd, *current_data;
560 static int current_nobuffs;
561 static struct scatterlist *current_buffer;
562 static int current_bufflen;
564 #ifdef LINKED
566 * linked_connected indicates whether or not we are currently connected to
567 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
568 * using linked commands.
571 static int linked_connected = 0;
572 static unsigned char linked_target, linked_lun;
573 #endif
575 static void (*done_fn) (Scsi_Cmnd *) = NULL;
576 static Scsi_Cmnd *SCint = NULL;
579 * These control whether or not disconnect / reconnect will be attempted,
580 * or are being attempted.
583 #define NO_RECONNECT 0
584 #define RECONNECT_NOW 1
585 #define CAN_RECONNECT 2
588 * LINKED_RIGHT indicates that we are currently connected to the correct target
589 * for this command, LINKED_WRONG indicates that we are connected to the wrong
590 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
593 #define LINKED_RIGHT 3
594 #define LINKED_WRONG 4
597 * This determines if we are expecting to reconnect or not.
600 static int should_reconnect = 0;
603 * The seagate_reconnect_intr routine is called when a target reselects the
604 * host adapter. This occurs on the interrupt triggered by the target
605 * asserting SEL.
608 static void do_seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
610 unsigned long flags;
612 spin_lock_irqsave(&io_request_lock, flags);
613 seagate_reconnect_intr(irq, dev_id, regs);
614 spin_unlock_irqrestore(&io_request_lock, flags);
617 static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
619 int temp;
620 Scsi_Cmnd *SCtmp;
622 DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n", hostno);
624 if (!should_reconnect)
625 printk ("scsi%d: unexpected interrupt.\n", hostno);
626 else
628 should_reconnect = 0;
630 DPRINTK (PHASE_RESELECT, "scsi%d : internal_command("
631 "%d, %08x, %08x, RECONNECT_NOW\n", hostno,
632 current_target, current_data, current_bufflen);
634 temp = internal_command (current_target, current_lun, current_cmnd,
635 current_data, current_bufflen, RECONNECT_NOW);
637 if (msg_byte (temp) != DISCONNECT)
639 if (done_fn)
641 DPRINTK (PHASE_RESELECT, "scsi%d : done_fn(%d,%08x)", hostno,
642 hostno, temp);
643 if (!SCint)
644 panic ("SCint == NULL in seagate");
645 SCtmp = SCint;
646 SCint = NULL;
647 SCtmp->result = temp;
648 done_fn (SCtmp);
650 else
651 printk ("done_fn() not defined.\n");
657 * The seagate_st0x_queue_command() function provides a queued interface
658 * to the seagate SCSI driver. Basically, it just passes control onto the
659 * seagate_command() function, after fixing it so that the done_fn()
660 * is set to the one passed to the function. We have to be very careful,
661 * because there are some commands on some devices that do not disconnect,
662 * and if we simply call the done_fn when the command is done then another
663 * command is started and queue_command is called again... We end up
664 * overflowing the kernel stack, and this tends not to be such a good idea.
667 static int recursion_depth = 0;
669 int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
671 int result, reconnect;
672 Scsi_Cmnd *SCtmp;
674 DANY( "seagate: que_command" );
675 done_fn = done;
676 current_target = SCpnt->target;
677 current_lun = SCpnt->lun;
678 (const void *) current_cmnd = SCpnt->cmnd;
679 current_data = (unsigned char *) SCpnt->request_buffer;
680 current_bufflen = SCpnt->request_bufflen;
681 SCint = SCpnt;
682 if (recursion_depth) return 0;
683 recursion_depth++;
686 #ifdef LINKED
688 * Set linked command bit in control field of SCSI command.
691 current_cmnd[SCpnt->cmd_len] |= 0x01;
692 if (linked_connected)
694 DPRINTK (DEBUG_LINKED,
695 "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
696 if ((linked_target == current_target) && (linked_lun == current_lun))
698 DPRINTK (DEBUG_LINKED, "correct\n");
699 reconnect = LINKED_RIGHT;
701 else
703 DPRINTK (DEBUG_LINKED, "incorrect\n");
704 reconnect = LINKED_WRONG;
707 else
708 #endif /* LINKED */
709 reconnect = CAN_RECONNECT;
711 result = internal_command (SCint->target, SCint->lun, SCint->cmnd,
712 SCint->request_buffer, SCint->request_bufflen, reconnect);
713 if (msg_byte (result) == DISCONNECT) break;
714 SCtmp = SCint;
715 SCint = NULL;
716 SCtmp->result = result;
717 done_fn (SCtmp);
719 while (SCint);
720 recursion_depth--;
721 return 0;
724 int seagate_st0x_command (Scsi_Cmnd * SCpnt)
726 return internal_command (SCpnt->target, SCpnt->lun, SCpnt->cmnd,
727 SCpnt->request_buffer, SCpnt->request_bufflen,
728 (int) NO_RECONNECT);
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;
737 unsigned long flags;
739 #ifdef DEBUG
740 int transfered = 0, phase = 0, newphase;
741 #endif
743 register unsigned char status_read;
744 unsigned char tmp_data, tmp_control, status = 0, message = 0;
746 unsigned transfersize = 0, underflow = 0;
748 #ifdef SLOW_RATE
749 int borken = (int) SCint->device->borken; /* Does the current target require
750 Very Slow I/O ? */
751 #endif
753 incommand = 0;
754 st0x_aborted = 0;
756 #if (DEBUG & PRINT_COMMAND)
757 printk ("scsi%d : target = %d, command = ", hostno, target);
758 print_command ((unsigned char *) cmnd);
759 #endif
761 #if (DEBUG & PHASE_RESELECT)
762 switch (reselect)
764 case RECONNECT_NOW:
765 printk ("scsi%d : reconnecting\n", hostno);
766 break;
767 #ifdef LINKED
768 case LINKED_RIGHT:
769 printk ("scsi%d : connected, can reconnect\n", hostno);
770 break;
771 case LINKED_WRONG:
772 printk ("scsi%d : connected to wrong target, can reconnect\n", hostno);
773 break;
774 #endif
775 case CAN_RECONNECT:
776 printk ("scsi%d : allowed to reconnect\n", hostno);
777 break;
778 default:
779 printk ("scsi%d : not allowed to reconnect\n", hostno);
781 #endif
783 if (target == (controller_type == SEAGATE ? 7 : 6))
784 return DID_BAD_TARGET;
787 * We work it differently depending on if this is is "the first time,"
788 * or a reconnect. If this is a reselect phase, then SEL will
789 * be asserted, and we must skip selection / arbitration phases.
792 switch (reselect)
794 case RECONNECT_NOW:
795 DPRINTK ( PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
798 * At this point, we should find the logical or of our ID and the original
799 * target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
801 * After ARBITRATION phase is completed, only SEL, BSY, and the
802 * target ID are asserted. A valid initiator ID is not on the bus
803 * until IO is asserted, so we must wait for that.
805 ULOOP( 100*1000 ) {
806 temp = STATUS;
807 if ((temp & STAT_IO) && !(temp & STAT_BSY))
808 break;
810 if (TIMEOUT) {
811 DPRINTK (PHASE_RESELECT,
812 "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
813 return (DID_BAD_INTR << 16);
818 * After I/O is asserted by the target, we can read our ID and its
819 * ID off of the BUS.
822 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40)))
824 DPRINTK (PHASE_RESELECT,
825 "scsi%d : detected reconnect request to different target.\n"
826 "\tData bus = %d\n", hostno, temp);
827 return (DID_BAD_INTR << 16);
830 if (!(temp & (1 << current_target)))
832 printk ("scsi%d : Unexpected reselect interrupt. Data bus = %d\n",
833 hostno, temp);
834 return (DID_BAD_INTR << 16);
837 buffer = current_buffer;
838 cmnd = current_cmnd; /* WDE add */
839 data = current_data; /* WDE add */
840 len = current_bufflen; /* WDE add */
841 nobuffs = current_nobuffs;
844 * We have determined that we have been selected. At this point,
845 * we must respond to the reselection by asserting BSY ourselves
848 #if 1
849 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
850 #else
851 WRITE_CONTROL (BASE_CMD | CMD_BSY);
852 #endif
855 * The target will drop SEL, and raise BSY, at which time we must drop
856 * BSY.
859 ULOOP( 100*1000 ) {
860 if (!(STATUS & STAT_SEL)) break;
861 if (TIMEOUT) {
862 WRITE_CONTROL (BASE_CMD | CMD_INTR);
863 DPRINTK (PHASE_RESELECT,
864 "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
865 return (DID_BAD_INTR << 16);
869 WRITE_CONTROL (BASE_CMD);
872 * At this point, we have connected with the target and can get
873 * on with our lives.
875 break;
876 case CAN_RECONNECT:
878 #ifdef LINKED
880 * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
881 * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
882 * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT
883 * message on MESSAGE OUT phase, and then loop back to here.
886 connect_loop:
888 #endif
890 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
893 * BUS FREE PHASE
895 * On entry, we make sure that the BUS is in a BUS FREE
896 * phase, by insuring that both BSY and SEL are low for
897 * at least one bus settle delay. Several reads help
898 * eliminate wire glitch.
901 #ifndef ARBITRATE
902 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
903 clock = jiffies + ST0X_BUS_FREE_DELAY;
905 while (((STATUS | STATUS | STATUS) &
906 (STAT_BSY | STAT_SEL)) &&
907 (!st0x_aborted) && (jiffies < clock));
909 if (jiffies > clock)
910 return retcode (DID_BUS_BUSY);
911 else if (st0x_aborted)
912 return retcode (st0x_aborted);
913 #endif
915 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
917 clock = jiffies + ST0X_SELECTION_DELAY;
920 * Arbitration/selection procedure :
921 * 1. Disable drivers
922 * 2. Write HOST adapter address bit
923 * 3. Set start arbitration.
924 * 4. We get either ARBITRATION COMPLETE or SELECT at this
925 * point.
926 * 5. OR our ID and targets on bus.
927 * 6. Enable SCSI drivers and asserted SEL and ATTN
930 #ifdef ARBITRATE
931 save_flags (flags);
932 cli ();
933 WRITE_CONTROL (0);
934 WRITE_DATA ((controller_type == SEAGATE) ? 0x80 : 0x40);
935 WRITE_CONTROL (CMD_START_ARB);
936 restore_flags (flags);
938 ULOOP( ST0X_SELECTION_DELAY * 10000 ) {
939 status_read = STATUS;
940 if (status_read & STAT_ARB_CMPL) break;
941 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
942 break;
943 if (TIMEOUT || (status_read & STAT_SEL)) {
944 printk( "scsi%d : arbitration lost or timeout.\n", hostno );
945 WRITE_CONTROL (BASE_CMD);
946 return retcode (DID_NO_CONNECT);
950 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
951 #endif
954 * When the SCSI device decides that we're gawking at it, it will
955 * respond by asserting BUSY on the bus.
957 * Note : the Seagate ST-01/02 product manual says that we should
958 * twiddle the DATA register before the control register. However,
959 * this does not work reliably so we do it the other way around.
961 * Probably could be a problem with arbitration too, we really should
962 * try this with a SCSI protocol or logic analyzer to see what is
963 * going on.
965 tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
966 tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
968 save_flags(flags);
969 cli();
970 #ifdef OLDCNTDATASCEME
971 #ifdef SWAPCNTDATA
972 WRITE_CONTROL (tmp_control);
973 WRITE_DATA (tmp_data);
974 #else
975 WRITE_DATA (tmp_data);
976 WRITE_CONTROL (tmp_control);
977 #endif
978 #else
979 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
980 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
981 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
982 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
983 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
984 #endif
987 restore_flags (flags);
989 ULOOP( 250*1000 ) {
990 if (st0x_aborted) {
992 * If we have been aborted, and we have a command in progress, IE the
993 * target still has BSY asserted, then we will reset the bus, and
994 * notify the midlevel driver to expect sense.
997 WRITE_CONTROL (BASE_CMD);
998 if (STATUS & STAT_BSY) {
999 printk ("scsi%d : BST asserted after we've been aborted.\n", hostno);
1000 seagate_st0x_reset (NULL, 0);
1001 return retcode (DID_RESET);
1003 return retcode (st0x_aborted);
1005 if (STATUS & STAT_BSY) break;
1006 if (TIMEOUT) {
1007 DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n",
1008 hostno, target, STATUS);
1009 return retcode (DID_NO_CONNECT);
1013 /* Establish current pointers. Take into account scatter / gather */
1015 if ((nobuffs = SCint->use_sg))
1017 #if (DEBUG & DEBUG_SG)
1019 int i;
1021 printk ("scsi%d : scatter gather requested, using %d buffers.\n",
1022 hostno, nobuffs);
1023 for (i = 0; i < nobuffs; ++i)
1024 printk ("scsi%d : buffer %d address = %08x length = %d\n",
1025 hostno, i, buffer[i].address, buffer[i].length);
1027 #endif
1029 buffer = (struct scatterlist *) SCint->buffer;
1030 len = buffer->length;
1031 data = (unsigned char *) buffer->address;
1033 else
1035 DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
1036 buffer = NULL;
1037 len = SCint->request_bufflen;
1038 data = (unsigned char *) SCint->request_buffer;
1041 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n", hostno, len);
1043 break;
1044 #ifdef LINKED
1045 case LINKED_RIGHT:
1046 break;
1047 case LINKED_WRONG:
1048 break;
1049 #endif
1050 } /* end of switch(reselect) */
1053 * There are several conditions under which we wish to send a message :
1054 * 1. When we are allowing disconnect / reconnect, and need to establish
1055 * the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
1057 * 2. When we are doing linked commands, are have the wrong I_T_L nexus
1058 * established and want to send an ABORT message.
1061 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1062 #ifdef LINKED
1063 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1064 (((reselect == CAN_RECONNECT)
1065 || (reselect == LINKED_WRONG)
1066 )? CMD_ATTN : 0));
1067 #else
1068 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1069 (((reselect == CAN_RECONNECT)
1070 )? CMD_ATTN : 0));
1071 #endif
1074 * INFORMATION TRANSFER PHASE
1076 * The nasty looking read / write inline assembler loops we use for
1077 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1078 * the 'C' versions - since we're moving 1024 bytes of data, this
1079 * really adds up.
1081 * SJT: The nasty-looking assembler is gone, so it's slower.
1085 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1087 incommand = 1;
1088 transfersize = SCint->transfersize;
1089 underflow = SCint->underflow;
1092 * Now, we poll the device for status information,
1093 * and handle any requests it makes. Note that since we are unsure of
1094 * how much data will be flowing across the system, etc and cannot
1095 * make reasonable timeouts, that we will instead have the midlevel
1096 * driver handle any timeouts that occur in this phase.
1099 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done)
1101 #ifdef PARITY
1102 if (status_read & STAT_PARITY)
1104 printk ("scsi%d : got parity error\n", hostno);
1105 st0x_aborted = DID_PARITY;
1107 #endif
1109 if (status_read & STAT_REQ)
1111 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1112 if ((newphase = (status_read & REQ_MASK)) != phase)
1114 phase = newphase;
1115 switch (phase)
1117 case REQ_DATAOUT:
1118 printk ("scsi%d : phase = DATA OUT\n", hostno);
1119 break;
1120 case REQ_DATAIN:
1121 printk ("scsi%d : phase = DATA IN\n", hostno);
1122 break;
1123 case REQ_CMDOUT:
1124 printk ("scsi%d : phase = COMMAND OUT\n", hostno);
1125 break;
1126 case REQ_STATIN:
1127 printk ("scsi%d : phase = STATUS IN\n", hostno);
1128 break;
1129 case REQ_MSGOUT:
1130 printk ("scsi%d : phase = MESSAGE OUT\n", hostno);
1131 break;
1132 case REQ_MSGIN:
1133 printk ("scsi%d : phase = MESSAGE IN\n", hostno);
1134 break;
1135 default:
1136 printk ("scsi%d : phase = UNKNOWN\n", hostno);
1137 st0x_aborted = DID_ERROR;
1140 #endif
1141 switch (status_read & REQ_MASK)
1143 case REQ_DATAOUT:
1145 * If we are in fast mode, then we simply splat the data out
1146 * in word-sized chunks as fast as we can.
1149 if (!len)
1151 #if 0
1152 printk ("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
1153 st0x_aborted = DID_ERROR;
1154 fast = 0;
1155 #endif
1156 break;
1159 if (fast && transfersize && !(len % transfersize)
1160 && (len >= transfersize)
1161 #ifdef FAST32
1162 && !(transfersize % 4)
1163 #endif
1166 DPRINTK (DEBUG_FAST,
1167 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1168 " len = %d, data = %08x\n",
1169 hostno, SCint->underflow, SCint->transfersize, len, data);
1171 /* SJT: Start. Fast Write */
1172 #ifdef SEAGATE_USE_ASM
1173 __asm__(
1174 "cld\n\t"
1175 #ifdef FAST32
1176 "shr $2, %%ecx\n\t"
1177 "1:\t"
1178 "lodsl\n\t"
1179 "movl %%eax, (%%edi)\n\t"
1180 #else
1181 "1:\t"
1182 "lodsb\n\t"
1183 "movb %%al, (%%edi)\n\t"
1184 #endif
1185 "loop 1b;"
1186 /* output */ :
1187 /* input */ : "D" (phys_to_virt(st0x_dr)), "S" (data), "c" (SCint->transfersize)
1188 /* clobbered */ : "eax", "ecx", "esi" );
1189 #else /* SEAGATE_USE_ASM */
1191 #ifdef FAST32
1192 unsigned int *iop = phys_to_virt (st0x_dr);
1193 const unsigned int *dp = (unsigned int *) data;
1194 int xferlen = transfersize >> 2;
1195 #else
1196 unsigned char *iop = phys_to_virt (st0x_dr);
1197 const unsigned char *dp = data;
1198 int xferlen = transfersize;
1199 #endif
1200 for (; xferlen; --xferlen)
1201 *iop = *dp++;
1203 #endif /* SEAGATE_USE_ASM */
1204 /* SJT: End */
1205 len -= transfersize;
1206 data += transfersize;
1207 DPRINTK (DEBUG_FAST,
1208 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1209 hostno, len, data);
1211 else
1214 * We loop as long as we are in a data out phase, there is data to send,
1215 * and BSY is still active.
1218 /* SJT: Start. Slow Write. */
1219 #ifdef SEAGATE_USE_ASM
1221 * We loop as long as we are in a data out phase, there is data to send,
1222 * and BSY is still active.
1224 /* Local variables : len = ecx , data = esi,
1225 st0x_cr_sr = ebx, st0x_dr = edi
1227 __asm__ (
1228 /* Test for any data here at all. */
1229 "orl %%ecx, %%ecx\n\t"
1230 "jz 2f\n\t"
1231 "cld\n\t"
1232 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%ebx\n\t" */
1233 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%edi\n\t" */
1234 "1:\t"
1235 "movb (%%ebx), %%al\n\t"
1236 /* Test for BSY */
1237 "test $1, %%al\n\t"
1238 "jz 2f\n\t"
1239 /* Test for data out phase - STATUS & REQ_MASK should be
1240 REQ_DATAOUT, which is 0. */
1241 "test $0xe, %%al\n\t"
1242 "jnz 2f\n\t"
1243 /* Test for REQ */
1244 "test $0x10, %%al\n\t"
1245 "jz 1b\n\t"
1246 "lodsb\n\t"
1247 "movb %%al, (%%edi)\n\t"
1248 "loop 1b\n\t"
1249 "2:\n"
1250 /* output */ : "=S" (data), "=c" (len)
1251 /* input */ : "0" (data), "1" (len), "b" (phys_to_virt(st0x_cr_sr)), "D" (phys_to_virt(st0x_dr))
1252 /* clobbered */ : "eax", "ebx", "edi");
1253 #else /* SEAGATE_USE_ASM */
1254 while (len)
1256 unsigned char stat;
1258 stat = STATUS;
1259 if (!(stat & STAT_BSY) || ((stat & REQ_MASK) != REQ_DATAOUT))
1260 break;
1261 if (stat & STAT_REQ)
1263 WRITE_DATA (*data++);
1264 --len;
1267 #endif /* SEAGATE_USE_ASM */
1268 /* SJT: End. */
1271 if (!len && nobuffs)
1273 --nobuffs;
1274 ++buffer;
1275 len = buffer->length;
1276 data = (unsigned char *) buffer->address;
1277 DPRINTK (DEBUG_SG,
1278 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1279 hostno, len, data);
1281 break;
1283 case REQ_DATAIN:
1284 #ifdef SLOW_RATE
1285 if (borken)
1287 #if (DEBUG & (PHASE_DATAIN))
1288 transfered += len;
1289 #endif
1290 for (;
1291 len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN |
1292 STAT_REQ)
1293 ; --len)
1295 *data++ = DATA;
1296 borken_wait ();
1298 #if (DEBUG & (PHASE_DATAIN))
1299 transfered -= len;
1300 #endif
1302 else
1303 #endif
1305 if (fast && transfersize && !(len % transfersize) &&
1306 (len >= transfersize)
1307 #ifdef FAST32
1308 && !(transfersize % 4)
1309 #endif
1312 DPRINTK (DEBUG_FAST,
1313 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1314 " len = %d, data = %08x\n",
1315 hostno, SCint->underflow, SCint->transfersize, len, data);
1317 /* SJT: Start. Fast Read */
1318 #ifdef SEAGATE_USE_ASM
1319 __asm__(
1320 "cld\n\t"
1321 #ifdef FAST32
1322 "shr $2, %%ecx\n\t"
1323 "1:\t"
1324 "movl (%%esi), %%eax\n\t"
1325 "stosl\n\t"
1326 #else
1327 "1:\t"
1328 "movb (%%esi), %%al\n\t"
1329 "stosb\n\t"
1330 #endif
1331 "loop 1b\n\t"
1332 /* output */ :
1333 /* input */ : "S" (phys_to_virt(st0x_dr)), "D" (data), "c" (SCint->transfersize)
1334 /* clobbered */ : "eax", "ecx", "edi");
1335 #else /* SEAGATE_USE_ASM */
1337 #ifdef FAST32
1338 const unsigned int *iop = phys_to_virt (st0x_dr);
1339 unsigned int *dp = (unsigned int *) data;
1340 int xferlen = len >> 2;
1341 #else
1342 const unsigned char *iop = phys_to_virt (st0x_dr);
1343 unsigned char *dp = data;
1344 int xferlen = len;
1345 #endif
1346 for (; xferlen; --xferlen)
1347 *dp++ = *iop;
1349 #endif /* SEAGATE_USE_ASM */
1350 /* SJT: End */
1351 len -= transfersize;
1352 data += transfersize;
1353 #if (DEBUG & PHASE_DATAIN)
1354 printk ("scsi%d: transfered += %d\n", hostno, transfersize);
1355 transfered += transfersize;
1356 #endif
1358 DPRINTK (DEBUG_FAST,
1359 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1360 hostno, len, data);
1362 else
1365 #if (DEBUG & PHASE_DATAIN)
1366 printk ("scsi%d: transfered += %d\n", hostno, len);
1367 transfered += len; /* Assume we'll transfer it all, then
1368 subtract what we *didn't* transfer */
1369 #endif
1372 * We loop as long as we are in a data in phase, there is room to read,
1373 * and BSY is still active
1376 /* SJT: Start. */
1377 #ifdef SEAGATE_USE_ASM
1379 * We loop as long as we are in a data in phase, there is room to read,
1380 * and BSY is still active
1382 /* Local variables : ecx = len, edi = data
1383 esi = st0x_cr_sr, ebx = st0x_dr */
1384 __asm__ (
1385 /* Test for room to read */
1386 "orl %%ecx, %%ecx\n\t"
1387 "jz 2f\n\t"
1388 "cld\n\t"
1389 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%esi\n\t" */
1390 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%ebx\n\t" */
1391 "1:\t"
1392 "movb (%%esi), %%al\n\t"
1393 /* Test for BSY */
1394 "test $1, %%al\n\t"
1395 "jz 2f\n\t"
1396 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1397 = STAT_IO, which is 4. */
1398 "movb $0xe, %%ah\n\t"
1399 "andb %%al, %%ah\n\t"
1400 "cmpb $0x04, %%ah\n\t"
1401 "jne 2f\n\t"
1402 /* Test for REQ */
1403 "test $0x10, %%al\n\t"
1404 "jz 1b\n\t"
1405 "movb (%%ebx), %%al\n\t"
1406 "stosb\n\t"
1407 "loop 1b\n\t"
1408 "2:\n"
1409 /* output */ : "=D" (data), "=c" (len)
1410 /* input */ : "0" (data), "1" (len), "S" (phys_to_virt(st0x_cr_sr)), "b" (phys_to_virt(st0x_dr))
1411 /* clobbered */ : "eax","ebx", "esi");
1412 #else /* SEAGATE_USE_ASM */
1413 while (len)
1415 unsigned char stat;
1417 stat = STATUS;
1418 if (!(stat & STAT_BSY) || ((stat & REQ_MASK) != REQ_DATAIN))
1419 break;
1420 if (stat & STAT_REQ)
1422 *data++ = DATA;
1423 --len;
1426 #endif /* SEAGATE_USE_ASM */
1427 /* SJT: End. */
1428 #if (DEBUG & PHASE_DATAIN)
1429 printk ("scsi%d: transfered -= %d\n", hostno, len);
1430 transfered -= len; /* Since we assumed all of Len got *
1431 transfered, correct our mistake */
1432 #endif
1435 if (!len && nobuffs)
1437 --nobuffs;
1438 ++buffer;
1439 len = buffer->length;
1440 data = (unsigned char *) buffer->address;
1441 DPRINTK (DEBUG_SG,
1442 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1443 hostno, len, data);
1446 break;
1448 case REQ_CMDOUT:
1449 while (((status_read = STATUS) & STAT_BSY) &&
1450 ((status_read & REQ_MASK) == REQ_CMDOUT))
1451 if (status_read & STAT_REQ)
1453 WRITE_DATA (*(const unsigned char *) cmnd);
1454 cmnd = 1 + (const unsigned char *) cmnd;
1455 #ifdef SLOW_RATE
1456 if (borken)
1457 borken_wait ();
1458 #endif
1460 break;
1462 case REQ_STATIN:
1463 status = DATA;
1464 break;
1466 case REQ_MSGOUT:
1468 * We can only have sent a MSG OUT if we requested to do this
1469 * by raising ATTN. So, we must drop ATTN.
1472 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1474 * If we are reconnecting, then we must send an IDENTIFY message in
1475 * response to MSGOUT.
1477 switch (reselect)
1479 case CAN_RECONNECT:
1480 WRITE_DATA (IDENTIFY (1, lun));
1482 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
1483 break;
1484 #ifdef LINKED
1485 case LINKED_WRONG:
1486 WRITE_DATA (ABORT);
1487 linked_connected = 0;
1488 reselect = CAN_RECONNECT;
1489 goto connect_loop;
1490 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED,
1491 "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
1492 #endif /* LINKED */
1493 DPRINTK (DEBUG_LINKED, "correct\n");
1494 default:
1495 WRITE_DATA (NOP);
1496 printk ("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1498 break;
1500 case REQ_MSGIN:
1501 switch (message = DATA)
1503 case DISCONNECT:
1504 DANY ("seagate: deciding to disconnect\n");
1505 should_reconnect = 1;
1506 current_data = data; /* WDE add */
1507 current_buffer = buffer;
1508 current_bufflen = len; /* WDE add */
1509 current_nobuffs = nobuffs;
1510 #ifdef LINKED
1511 linked_connected = 0;
1512 #endif
1513 done = 1;
1514 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
1515 break;
1517 #ifdef LINKED
1518 case LINKED_CMD_COMPLETE:
1519 case LINKED_FLG_CMD_COMPLETE:
1520 #endif
1521 case COMMAND_COMPLETE:
1523 * Note : we should check for underflow here.
1525 DPRINTK (PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
1526 done = 1;
1527 break;
1528 case ABORT:
1529 DPRINTK (PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
1530 done = 1;
1531 break;
1532 case SAVE_POINTERS:
1533 current_buffer = buffer;
1534 current_bufflen = len; /* WDE add */
1535 current_data = data; /* WDE mod */
1536 current_nobuffs = nobuffs;
1537 DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
1538 break;
1539 case RESTORE_POINTERS:
1540 buffer = current_buffer;
1541 cmnd = current_cmnd;
1542 data = current_data; /* WDE mod */
1543 len = current_bufflen;
1544 nobuffs = current_nobuffs;
1545 DPRINTK (PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
1546 break;
1547 default:
1550 * IDENTIFY distinguishes itself from the other messages by setting the
1551 * high byte. [FIXME: should not this read "the high bit"? - pavel@ucw.cz]
1553 * Note : we need to handle at least one outstanding command per LUN,
1554 * and need to hash the SCSI command for that I_T_L nexus based on the
1555 * known ID (at this point) and LUN.
1558 if (message & 0x80)
1560 DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1561 hostno, target, message & 7);
1563 else
1567 * We should go into a MESSAGE OUT phase, and send a MESSAGE_REJECT
1568 * if we run into a message that we don't like. The seagate driver
1569 * needs some serious restructuring first though.
1572 DPRINTK (PHASE_MSGIN,
1573 "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
1576 break;
1578 default:
1579 printk ("scsi%d : unknown phase.\n", hostno);
1580 st0x_aborted = DID_ERROR;
1581 } /* end of switch (status_read &
1582 REQ_MASK) */
1584 #ifdef SLOW_RATE
1586 * I really don't care to deal with borken devices in each single
1587 * byte transfer case (ie, message in, message out, status), so
1588 * I'll do the wait here if necessary.
1590 if (borken)
1591 borken_wait ();
1592 #endif
1594 } /* if(status_read & STAT_REQ) ends */
1595 } /* while(((status_read = STATUS)...)
1596 ends */
1598 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT,
1599 "scsi%d : Transfered %d bytes\n", hostno, transfered);
1601 #if (DEBUG & PHASE_EXIT)
1602 #if 0 /* Doesn't work for scatter/gather */
1603 printk ("Buffer : \n");
1604 for (i = 0; i < 20; ++i)
1605 printk ("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1606 printk ("\n");
1607 #endif
1608 printk ("scsi%d : status = ", hostno);
1609 print_status (status);
1610 printk ("message = %02x\n", message);
1611 #endif
1613 /* We shouldn't reach this until *after* BSY has been deasserted */
1615 #ifdef LINKED
1616 else
1619 * Fix the message byte so that unsuspecting high level drivers don't
1620 * puke when they see a LINKED COMMAND message in place of the COMMAND
1621 * COMPLETE they may be expecting. Shouldn't be necessary, but it's
1622 * better to be on the safe side.
1624 * A non LINKED* message byte will indicate that the command completed,
1625 * and we are now disconnected.
1628 switch (message)
1630 case LINKED_CMD_COMPLETE:
1631 case LINKED_FLG_CMD_COMPLETE:
1632 message = COMMAND_COMPLETE;
1633 linked_target = current_target;
1634 linked_lun = current_lun;
1635 linked_connected = 1;
1636 DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established"
1637 "for linked command.\n", hostno);
1638 /* We also will need to adjust status to accommodate intermediate
1639 conditions. */
1640 if ((status == INTERMEDIATE_GOOD) ||
1641 (status == INTERMEDIATE_C_GOOD))
1642 status = GOOD;
1644 break;
1646 * We should also handle what are "normal" termination messages
1647 * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually,
1648 * and flake if things aren't right.
1650 default:
1651 DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
1652 linked_connected = 0;
1655 #endif /* LINKED */
1657 if (should_reconnect)
1659 DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command()"
1660 "with reconnect enabled.\n", hostno);
1661 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1663 else
1664 WRITE_CONTROL (BASE_CMD);
1666 return retcode (st0x_aborted);
1667 } /* end of internal_command */
1669 int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1671 st0x_aborted = DID_ABORT;
1672 return SCSI_ABORT_PENDING;
1674 #undef ULOOP
1675 #undef TIMEOUT
1678 * the seagate_st0x_reset function resets the SCSI bus
1681 int seagate_st0x_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1683 /* No timeouts - this command is going to fail because it was reset. */
1684 DANY ("scsi%d: Reseting bus... ", hostno );
1686 /* assert RESET signal on SCSI bus. */
1687 WRITE_CONTROL (BASE_CMD | CMD_RST);
1689 udelay( 20*1000 );
1691 WRITE_CONTROL (BASE_CMD);
1692 st0x_aborted = DID_RESET;
1694 DANY ("done.\n");
1695 return SCSI_RESET_WAKEUP;
1699 int seagate_st0x_biosparam (Disk * disk, kdev_t dev, int *ip)
1701 unsigned char buf[256 + sizeof (Scsi_Ioctl_Command)],
1702 cmd[6], *data, *page;
1703 Scsi_Ioctl_Command *sic = (Scsi_Ioctl_Command *) buf;
1704 int result, formatted_sectors, total_sectors;
1705 int cylinders, heads, sectors;
1706 int capacity;
1709 * Only SCSI-I CCS drives and later implement the necessary mode sense
1710 * pages.
1713 if (disk->device->scsi_level < 2)
1714 return -1;
1716 data = sic->data;
1718 cmd[0] = MODE_SENSE;
1719 cmd[1] = (disk->device->lun << 5) & 0xe5;
1720 cmd[2] = 0x04; /* Read page 4, rigid disk geometry
1721 page current values */
1722 cmd[3] = 0;
1723 cmd[4] = 255;
1724 cmd[5] = 0;
1727 * We are transferring 0 bytes in the out direction, and expect to get back
1728 * 24 bytes for each mode page.
1730 sic->inlen = 0;
1731 sic->outlen = 256;
1733 memcpy (data, cmd, 6);
1735 if (!(result = kernel_scsi_ioctl (disk->device, SCSI_IOCTL_SEND_COMMAND,
1736 sic)))
1739 * The mode page lies beyond the MODE SENSE header, with length 4, and
1740 * the BLOCK DESCRIPTOR, with length header[3].
1742 page = data + 4 + data[3];
1743 heads = (int) page[5];
1744 cylinders = (page[2] << 16) | (page[3] << 8) | page[4];
1746 cmd[2] = 0x03; /* Read page 3, format page current
1747 values */
1748 memcpy (data, cmd, 6);
1750 if (!(result = kernel_scsi_ioctl (disk->device, SCSI_IOCTL_SEND_COMMAND,
1751 sic)))
1753 page = data + 4 + data[3];
1754 sectors = (page[10] << 8) | page[11];
1756 * Get the total number of formatted sectors from the block descriptor,
1757 * so we can tell how many are being used for alternates.
1759 formatted_sectors = (data[4 + 1] << 16) | (data[4 + 2] << 8)
1760 | data[4 + 3];
1762 total_sectors = (heads * cylinders * sectors);
1765 * Adjust the real geometry by subtracting
1766 * (spare sectors / (heads * tracks)) cylinders from the number of cylinders.
1768 * It appears that the CE cylinder CAN be a partial cylinder.
1771 printk ("scsi%d : heads = %d cylinders = %d sectors = %d total = %d formatted = %d\n",
1772 hostno, heads, cylinders, sectors, total_sectors,
1773 formatted_sectors);
1775 if (!heads || !sectors || !cylinders)
1776 result = -1;
1777 else
1778 cylinders -= ((total_sectors - formatted_sectors) / (heads * sectors));
1781 * Now, we need to do a sanity check on the geometry to see if it is
1782 * BIOS compatible. The maximum BIOS geometry is 1024 cylinders *
1783 * 256 heads * 64 sectors.
1786 if ((cylinders > 1024) || (sectors > 64))
1788 /* The Seagate's seem to have some mapping. Multiply
1789 heads*sectors*cyl to get capacity. Then start rounding down.
1791 capacity = heads * sectors * cylinders;
1793 /* Old MFM Drives use this, so does the Seagate */
1794 sectors = 17;
1795 heads = 2;
1796 capacity = capacity / sectors;
1797 while (cylinders > 1024)
1799 heads *= 2; /* For some reason, they go in
1800 multiples */
1801 cylinders = capacity / heads;
1804 ip[0] = heads;
1805 ip[1] = sectors;
1806 ip[2] = cylinders;
1808 * There should be an alternate mapping for things the seagate doesn't
1809 * understand, but I couldn't say what it is with reasonable certainty.
1814 return result;
1817 #ifdef MODULE
1818 /* Eventually this will go into an include file, but this will be later */
1819 Scsi_Host_Template driver_template = SEAGATE_ST0X;
1821 #include "scsi_module.c"
1822 #endif