Import 2.3.18pre1
[davej-history.git] / drivers / scsi / seagate.c
blob27c9e1aa1cdcca0b617ddd386b32cfd140045449
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 <linux/spinlock.h>
87 #include <linux/signal.h>
88 #include <linux/sched.h>
89 #include <linux/string.h>
90 #include <linux/proc_fs.h>
91 #include <linux/init.h>
93 #include <linux/blk.h>
94 #include "scsi.h"
95 #include "hosts.h"
96 #include "seagate.h"
97 #include "constants.h"
98 #include <linux/stat.h>
99 #include <asm/uaccess.h>
100 #include "sd.h"
101 #include <scsi/scsi_ioctl.h>
102 #include <asm/delay.h>
104 #ifdef DEBUG
105 #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
106 #else
107 #define DPRINTK( when, msg... ) do { } while (0)
108 #endif
109 #define DANY( msg... ) DPRINTK( 0xffff, msg );
111 static struct proc_dir_entry proc_scsi_seagate =
113 PROC_SCSI_SEAGATE, 7, "seagate",
114 S_IFDIR | S_IRUGO | S_IXUGO, 2
117 #ifndef IRQ
118 #define IRQ 5
119 #endif
121 #ifdef FAST32
122 #define FAST
123 #endif
125 #undef LINKED /* Linked commands are currently broken! */
127 #if defined(OVERRIDE) && !defined(CONTROLLER)
128 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
129 #endif
132 Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
133 driver, and Mitsugu Suzuki for information on the ST-01
134 SCSI host.
138 CONTROL defines
141 #define CMD_RST 0x01
142 #define CMD_SEL 0x02
143 #define CMD_BSY 0x04
144 #define CMD_ATTN 0x08
145 #define CMD_START_ARB 0x10
146 #define CMD_EN_PARITY 0x20
147 #define CMD_INTR 0x40
148 #define CMD_DRVR_ENABLE 0x80
151 STATUS
153 #ifdef SWAPSTAT
154 #define STAT_MSG 0x08
155 #define STAT_CD 0x02
156 #else
157 #define STAT_MSG 0x02
158 #define STAT_CD 0x08
159 #endif
161 #define STAT_BSY 0x01
162 #define STAT_IO 0x04
163 #define STAT_REQ 0x10
164 #define STAT_SEL 0x20
165 #define STAT_PARITY 0x40
166 #define STAT_ARB_CMPL 0x80
169 REQUESTS
172 #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
173 #define REQ_DATAOUT 0
174 #define REQ_DATAIN STAT_IO
175 #define REQ_CMDOUT STAT_CD
176 #define REQ_STATIN (STAT_CD | STAT_IO)
177 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
178 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
180 extern volatile int seagate_st0x_timeout;
182 #ifdef PARITY
183 #define BASE_CMD CMD_EN_PARITY
184 #else
185 #define BASE_CMD 0
186 #endif
189 Debugging code
192 #define PHASE_BUS_FREE 1
193 #define PHASE_ARBITRATION 2
194 #define PHASE_SELECTION 4
195 #define PHASE_DATAIN 8
196 #define PHASE_DATAOUT 0x10
197 #define PHASE_CMDOUT 0x20
198 #define PHASE_MSGIN 0x40
199 #define PHASE_MSGOUT 0x80
200 #define PHASE_STATUSIN 0x100
201 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
202 #define PRINT_COMMAND 0x200
203 #define PHASE_EXIT 0x400
204 #define PHASE_RESELECT 0x800
205 #define DEBUG_FAST 0x1000
206 #define DEBUG_SG 0x2000
207 #define DEBUG_LINKED 0x4000
208 #define DEBUG_BORKEN 0x8000
211 * Control options - these are timeouts specified in .01 seconds.
214 /* 30, 20 work */
215 #define ST0X_BUS_FREE_DELAY 25
216 #define ST0X_SELECTION_DELAY 25
218 #define SEAGATE 1 /* these determine the type of the controller */
219 #define FD 2
221 #define ST0X_ID_STR "Seagate ST-01/ST-02"
222 #define FD_ID_STR "TMC-8XX/TMC-950"
225 static int internal_command (unsigned char target, unsigned char lun,
226 const void *cmnd,
227 void *buff, int bufflen, int reselect);
229 static int incommand; /* set if arbitration has finished
230 and we are in some command phase. */
232 static unsigned int base_address = 0; /* Where the card ROM starts, used to
233 calculate memory mapped register
234 location. */
236 static unsigned long st0x_cr_sr; /* control register write, status
237 register read. 256 bytes in
238 length.
239 Read is status of SCSI BUS, as per
240 STAT masks. */
242 static unsigned long st0x_dr; /* data register, read write 256
243 bytes in length. */
245 static volatile int st0x_aborted = 0; /* set when we are aborted, ie by a
246 time out, etc. */
248 static unsigned char controller_type = 0; /* set to SEAGATE for ST0x
249 boards or FD for TMC-8xx
250 boards */
251 static int irq = IRQ;
253 #define retcode(result) (((result) << 16) | (message << 8) | status)
254 #define STATUS ((u8) readb(st0x_cr_sr))
255 #define DATA ((u8) readb(st0x_dr))
256 #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
257 #define WRITE_DATA(d) { writeb((d), st0x_dr); }
259 void st0x_setup (char *str, int *ints)
261 controller_type = SEAGATE;
262 base_address = ints[1];
263 irq = ints[2];
266 void tmc8xx_setup (char *str, int *ints)
268 controller_type = FD;
269 base_address = ints[1];
270 irq = ints[2];
273 #ifndef OVERRIDE
274 static unsigned int seagate_bases[] =
276 0xc8000, 0xca000, 0xcc000,
277 0xce000, 0xdc000, 0xde000
280 typedef struct
282 const unsigned char *signature;
283 unsigned offset;
284 unsigned length;
285 unsigned char type;
287 Signature;
289 static const Signature __initdata signatures[] =
291 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
292 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
295 * The following two lines are NOT mistakes. One detects ROM revision
296 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
297 * and this is not going to change, the "SEAGATE" and "SCSI" together
298 * are probably "good enough"
301 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
302 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
305 * However, future domain makes several incompatible SCSI boards, so specific
306 * signatures must be used.
309 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
310 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
311 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
312 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
313 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
314 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
315 {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
316 {"FUTURE DOMAIN TMC-950", 5, 21, FD},
319 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
320 #endif /* n OVERRIDE */
323 * hostno stores the hostnumber, as told to us by the init routine.
326 static int hostno = -1;
327 static void seagate_reconnect_intr (int, void *, struct pt_regs *);
328 static void do_seagate_reconnect_intr (int, void *, struct pt_regs *);
330 #ifdef FAST
331 static int fast = 1;
332 #else
333 #define fast 0
334 #endif
336 #ifdef SLOW_RATE
338 * Support for broken devices :
339 * The Seagate board has a handshaking problem. Namely, a lack
340 * thereof for slow devices. You can blast 600K/second through
341 * it if you are polling for each byte, more if you do a blind
342 * transfer. In the first case, with a fast device, REQ will
343 * transition high-low or high-low-high before your loop restarts
344 * and you'll have no problems. In the second case, the board
345 * will insert wait states for up to 13.2 usecs for REQ to
346 * transition low->high, and everything will work.
348 * However, there's nothing in the state machine that says
349 * you *HAVE* to see a high-low-high set of transitions before
350 * sending the next byte, and slow things like the Trantor CD ROMS
351 * will break because of this.
353 * So, we need to slow things down, which isn't as simple as it
354 * seems. We can't slow things down period, because then people
355 * who don't recompile their kernels will shoot me for ruining
356 * their performance. We need to do it on a case per case basis.
358 * The best for performance will be to, only for borken devices
359 * (this is stored on a per-target basis in the scsi_devices array)
361 * Wait for a low->high transition before continuing with that
362 * transfer. If we timeout, continue anyways. We don't need
363 * a long timeout, because REQ should only be asserted until the
364 * corresponding ACK is received and processed.
366 * Note that we can't use the system timer for this, because of
367 * resolution, and we *really* can't use the timer chip since
368 * gettimeofday() and the beeper routines use that. So,
369 * the best thing for us to do will be to calibrate a timing
370 * loop in the initialization code using the timer chip before
371 * gettimeofday() can screw with it.
373 * FIXME: this is broken (not borken :-). Empty loop costs less than
374 * loop with ISA access in it! -- pavel@ucw.cz
377 static int borken_calibration = 0;
378 static void __init borken_init (void)
380 register int count = 0, start = jiffies + 1, stop = start + 25;
382 while (time_before(jiffies, start)) ;
383 for (; time_before(jiffies, stop); ++count) ;
386 * Ok, we now have a count for .25 seconds. Convert to a
387 * count per second and divide by transfer rate in K. */
389 borken_calibration = (count * 4) / (SLOW_RATE * 1024);
391 if (borken_calibration < 1)
392 borken_calibration = 1;
395 static inline void borken_wait (void)
397 register int count;
399 for (count = borken_calibration; count && (STATUS & STAT_REQ); --count) ;
400 #if (DEBUG & DEBUG_BORKEN)
401 if (count)
402 printk ("scsi%d : borken timeout\n", hostno);
403 #endif
406 #endif /* def SLOW_RATE */
408 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
409 * contains at least one ISA access, which takes more than 0.125
410 * usec. So if we loop 8 times time in usec, we are safe.
413 #define ULOOP( i ) for (clock = i*8;;)
414 #define TIMEOUT (!(clock--))
416 int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
418 struct Scsi_Host *instance;
419 int i, j;
421 tpnt->proc_dir = &proc_scsi_seagate;
423 * First, we try for the manual override. */
424 DANY ("Autodetecting ST0x / TMC-8xx\n");
426 if (hostno != -1) {
427 printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
428 return 0;
431 /* If the user specified the controller type from the command line,
432 controller_type will be non-zero, so don't try to detect one */
434 if (!controller_type)
436 #ifdef OVERRIDE
437 base_address = OVERRIDE;
438 controller_type = CONTROLLER;
440 DANY("Base address overridden to %x, controller type is %s\n",
441 base_address, controller_type == SEAGATE ? "SEAGATE" : "FD");
442 #else /* OVERRIDE */
444 * To detect this card, we simply look for the signature
445 * from the BIOS version notice in all the possible locations
446 * of the ROM's. This has a nice side effect of not trashing
447 * any register locations that might be used by something else.
449 * XXX - note that we probably should be probing the address
450 * space for the on-board RAM instead.
453 for (i = 0; i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i)
455 for (j = 0; !base_address && j < NUM_SIGNATURES; ++j)
456 if (check_signature (seagate_bases[i] + signatures[j].offset,
457 signatures[j].signature, signatures[j].length))
459 base_address = seagate_bases[i];
460 controller_type = signatures[j].type;
462 #endif /* OVERRIDE */
463 } /* (! controller_type) */
465 tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
466 tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
468 if (!base_address) {
469 DANY ("ST0x / TMC-8xx not detected.\n");
470 return 0;
473 st0x_cr_sr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
474 st0x_dr = st0x_cr_sr + 0x200;
476 DANY ("%s detected. Base address = %x, cr = %x, dr = %x\n",
477 tpnt->name, base_address, st0x_cr_sr, st0x_dr);
480 * At all times, we will use IRQ 5. Should also check for IRQ3 if we
481 * loose our first interrupt.
483 instance = scsi_register (tpnt, 0);
484 hostno = instance->host_no;
485 if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT,
486 (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", NULL)) {
487 printk ("scsi%d : unable to allocate IRQ%d\n", hostno, irq);
488 return 0;
490 instance->irq = irq;
491 instance->io_port = base_address;
492 #ifdef SLOW_RATE
493 printk( "Calibrating borken timer... " );
494 borken_init ();
495 printk( " %d cycles per transfer\n", borken_calibration );
496 #endif
498 printk( "This is one second... " );
500 int clock;
501 ULOOP( 1*1000*1000 ) {
502 volatile int x = STATUS;
503 if (TIMEOUT) break;
507 printk ("done, %s options:"
508 #ifdef ARBITRATE
509 " ARBITRATE"
510 #endif
511 #ifdef DEBUG
512 " DEBUG"
513 #endif
514 #ifdef FAST
515 " FAST"
516 #ifdef FAST32
517 "32"
518 #endif
519 #endif
520 #ifdef LINKED
521 " LINKED"
522 #endif
523 #ifdef PARITY
524 " PARITY"
525 #endif
526 #ifdef SEAGATE_USE_ASM
527 " SEAGATE_USE_ASM"
528 #endif
529 #ifdef SLOW_RATE
530 " SLOW_RATE"
531 #endif
532 #ifdef SWAPSTAT
533 " SWAPSTAT"
534 #endif
535 #ifdef SWAPCNTDATA
536 " SWAPCNTDATA"
537 #endif
538 "\n", tpnt->name);
539 return 1;
542 const char *seagate_st0x_info (struct Scsi_Host *shpnt)
544 static char buffer[64];
546 sprintf (buffer, "%s at irq %d, address 0x%05X",
547 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
548 irq, base_address);
549 return buffer;
553 * These are our saved pointers for the outstanding command that is
554 * waiting for a reconnect
557 static unsigned char current_target, current_lun;
558 static unsigned char *current_cmnd, *current_data;
559 static int current_nobuffs;
560 static struct scatterlist *current_buffer;
561 static int current_bufflen;
563 #ifdef LINKED
565 * linked_connected indicates whether or not we are currently connected to
566 * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
567 * using linked commands.
570 static int linked_connected = 0;
571 static unsigned char linked_target, linked_lun;
572 #endif
574 static void (*done_fn) (Scsi_Cmnd *) = NULL;
575 static Scsi_Cmnd *SCint = NULL;
578 * These control whether or not disconnect / reconnect will be attempted,
579 * or are being attempted.
582 #define NO_RECONNECT 0
583 #define RECONNECT_NOW 1
584 #define CAN_RECONNECT 2
587 * LINKED_RIGHT indicates that we are currently connected to the correct target
588 * for this command, LINKED_WRONG indicates that we are connected to the wrong
589 * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
592 #define LINKED_RIGHT 3
593 #define LINKED_WRONG 4
596 * This determines if we are expecting to reconnect or not.
599 static int should_reconnect = 0;
602 * The seagate_reconnect_intr routine is called when a target reselects the
603 * host adapter. This occurs on the interrupt triggered by the target
604 * asserting SEL.
607 static void do_seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
609 unsigned long flags;
611 spin_lock_irqsave(&io_request_lock, flags);
612 seagate_reconnect_intr(irq, dev_id, regs);
613 spin_unlock_irqrestore(&io_request_lock, flags);
616 static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
618 int temp;
619 Scsi_Cmnd *SCtmp;
621 DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n", hostno);
623 if (!should_reconnect)
624 printk ("scsi%d: unexpected interrupt.\n", hostno);
625 else
627 should_reconnect = 0;
629 DPRINTK (PHASE_RESELECT, "scsi%d : internal_command("
630 "%d, %08x, %08x, RECONNECT_NOW\n", hostno,
631 current_target, current_data, current_bufflen);
633 temp = internal_command (current_target, current_lun, current_cmnd,
634 current_data, current_bufflen, RECONNECT_NOW);
636 if (msg_byte (temp) != DISCONNECT)
638 if (done_fn)
640 DPRINTK (PHASE_RESELECT, "scsi%d : done_fn(%d,%08x)", hostno,
641 hostno, temp);
642 if (!SCint)
643 panic ("SCint == NULL in seagate");
644 SCtmp = SCint;
645 SCint = NULL;
646 SCtmp->result = temp;
647 done_fn (SCtmp);
649 else
650 printk ("done_fn() not defined.\n");
656 * The seagate_st0x_queue_command() function provides a queued interface
657 * to the seagate SCSI driver. Basically, it just passes control onto the
658 * seagate_command() function, after fixing it so that the done_fn()
659 * is set to the one passed to the function. We have to be very careful,
660 * because there are some commands on some devices that do not disconnect,
661 * and if we simply call the done_fn when the command is done then another
662 * command is started and queue_command is called again... We end up
663 * overflowing the kernel stack, and this tends not to be such a good idea.
666 static int recursion_depth = 0;
668 int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
670 int result, reconnect;
671 Scsi_Cmnd *SCtmp;
673 DANY( "seagate: que_command" );
674 done_fn = done;
675 current_target = SCpnt->target;
676 current_lun = SCpnt->lun;
677 (const void *) current_cmnd = SCpnt->cmnd;
678 current_data = (unsigned char *) SCpnt->request_buffer;
679 current_bufflen = SCpnt->request_bufflen;
680 SCint = SCpnt;
681 if (recursion_depth) return 0;
682 recursion_depth++;
685 #ifdef LINKED
687 * Set linked command bit in control field of SCSI command.
690 current_cmnd[SCpnt->cmd_len] |= 0x01;
691 if (linked_connected)
693 DPRINTK (DEBUG_LINKED,
694 "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
695 if ((linked_target == current_target) && (linked_lun == current_lun))
697 DPRINTK (DEBUG_LINKED, "correct\n");
698 reconnect = LINKED_RIGHT;
700 else
702 DPRINTK (DEBUG_LINKED, "incorrect\n");
703 reconnect = LINKED_WRONG;
706 else
707 #endif /* LINKED */
708 reconnect = CAN_RECONNECT;
710 result = internal_command (SCint->target, SCint->lun, SCint->cmnd,
711 SCint->request_buffer, SCint->request_bufflen, reconnect);
712 if (msg_byte (result) == DISCONNECT) break;
713 SCtmp = SCint;
714 SCint = NULL;
715 SCtmp->result = result;
716 done_fn (SCtmp);
718 while (SCint);
719 recursion_depth--;
720 return 0;
723 int seagate_st0x_command (Scsi_Cmnd * SCpnt)
725 return internal_command (SCpnt->target, SCpnt->lun, SCpnt->cmnd,
726 SCpnt->request_buffer, SCpnt->request_bufflen,
727 (int) NO_RECONNECT);
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;
736 unsigned long flags;
738 #ifdef DEBUG
739 int transfered = 0, phase = 0, newphase;
740 #endif
742 register unsigned char status_read;
743 unsigned char tmp_data, tmp_control, status = 0, message = 0;
745 unsigned transfersize = 0, underflow = 0;
747 #ifdef SLOW_RATE
748 int borken = (int) SCint->device->borken; /* Does the current target require
749 Very Slow I/O ? */
750 #endif
752 incommand = 0;
753 st0x_aborted = 0;
755 #if (DEBUG & PRINT_COMMAND)
756 printk ("scsi%d : target = %d, command = ", hostno, target);
757 print_command ((unsigned char *) cmnd);
758 #endif
760 #if (DEBUG & PHASE_RESELECT)
761 switch (reselect)
763 case RECONNECT_NOW:
764 printk ("scsi%d : reconnecting\n", hostno);
765 break;
766 #ifdef LINKED
767 case LINKED_RIGHT:
768 printk ("scsi%d : connected, can reconnect\n", hostno);
769 break;
770 case LINKED_WRONG:
771 printk ("scsi%d : connected to wrong target, can reconnect\n", hostno);
772 break;
773 #endif
774 case CAN_RECONNECT:
775 printk ("scsi%d : allowed to reconnect\n", hostno);
776 break;
777 default:
778 printk ("scsi%d : not allowed to reconnect\n", hostno);
780 #endif
782 if (target == (controller_type == SEAGATE ? 7 : 6))
783 return DID_BAD_TARGET;
786 * We work it differently depending on if this is is "the first time,"
787 * or a reconnect. If this is a reselect phase, then SEL will
788 * be asserted, and we must skip selection / arbitration phases.
791 switch (reselect)
793 case RECONNECT_NOW:
794 DPRINTK ( PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
797 * At this point, we should find the logical or of our ID and the original
798 * target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
800 * After ARBITRATION phase is completed, only SEL, BSY, and the
801 * target ID are asserted. A valid initiator ID is not on the bus
802 * until IO is asserted, so we must wait for that.
804 ULOOP( 100*1000 ) {
805 temp = STATUS;
806 if ((temp & STAT_IO) && !(temp & STAT_BSY))
807 break;
809 if (TIMEOUT) {
810 DPRINTK (PHASE_RESELECT,
811 "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
812 return (DID_BAD_INTR << 16);
817 * After I/O is asserted by the target, we can read our ID and its
818 * ID off of the BUS.
821 if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40)))
823 DPRINTK (PHASE_RESELECT,
824 "scsi%d : detected reconnect request to different target.\n"
825 "\tData bus = %d\n", hostno, temp);
826 return (DID_BAD_INTR << 16);
829 if (!(temp & (1 << current_target)))
831 printk ("scsi%d : Unexpected reselect interrupt. Data bus = %d\n",
832 hostno, temp);
833 return (DID_BAD_INTR << 16);
836 buffer = current_buffer;
837 cmnd = current_cmnd; /* WDE add */
838 data = current_data; /* WDE add */
839 len = current_bufflen; /* WDE add */
840 nobuffs = current_nobuffs;
843 * We have determined that we have been selected. At this point,
844 * we must respond to the reselection by asserting BSY ourselves
847 #if 1
848 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
849 #else
850 WRITE_CONTROL (BASE_CMD | CMD_BSY);
851 #endif
854 * The target will drop SEL, and raise BSY, at which time we must drop
855 * BSY.
858 ULOOP( 100*1000 ) {
859 if (!(STATUS & STAT_SEL)) break;
860 if (TIMEOUT) {
861 WRITE_CONTROL (BASE_CMD | CMD_INTR);
862 DPRINTK (PHASE_RESELECT,
863 "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
864 return (DID_BAD_INTR << 16);
868 WRITE_CONTROL (BASE_CMD);
871 * At this point, we have connected with the target and can get
872 * on with our lives.
874 break;
875 case CAN_RECONNECT:
877 #ifdef LINKED
879 * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
880 * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
881 * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT
882 * message on MESSAGE OUT phase, and then loop back to here.
885 connect_loop:
887 #endif
889 DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
892 * BUS FREE PHASE
894 * On entry, we make sure that the BUS is in a BUS FREE
895 * phase, by insuring that both BSY and SEL are low for
896 * at least one bus settle delay. Several reads help
897 * eliminate wire glitch.
900 #ifndef ARBITRATE
901 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
902 clock = jiffies + ST0X_BUS_FREE_DELAY;
904 while (((STATUS | STATUS | STATUS) &
905 (STAT_BSY | STAT_SEL)) &&
906 (!st0x_aborted) && time_before(jiffies, clock));
908 if (time_after(jiffies, clock))
909 return retcode (DID_BUS_BUSY);
910 else if (st0x_aborted)
911 return retcode (st0x_aborted);
912 #endif
914 DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
916 clock = jiffies + ST0X_SELECTION_DELAY;
919 * Arbitration/selection procedure :
920 * 1. Disable drivers
921 * 2. Write HOST adapter address bit
922 * 3. Set start arbitration.
923 * 4. We get either ARBITRATION COMPLETE or SELECT at this
924 * point.
925 * 5. OR our ID and targets on bus.
926 * 6. Enable SCSI drivers and asserted SEL and ATTN
929 #ifdef ARBITRATE
930 save_flags (flags);
931 cli ();
932 WRITE_CONTROL (0);
933 WRITE_DATA ((controller_type == SEAGATE) ? 0x80 : 0x40);
934 WRITE_CONTROL (CMD_START_ARB);
935 restore_flags (flags);
937 ULOOP( ST0X_SELECTION_DELAY * 10000 ) {
938 status_read = STATUS;
939 if (status_read & STAT_ARB_CMPL) break;
940 if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
941 break;
942 if (TIMEOUT || (status_read & STAT_SEL)) {
943 printk( "scsi%d : arbitration lost or timeout.\n", hostno );
944 WRITE_CONTROL (BASE_CMD);
945 return retcode (DID_NO_CONNECT);
949 DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
950 #endif
953 * When the SCSI device decides that we're gawking at it, it will
954 * respond by asserting BUSY on the bus.
956 * Note : the Seagate ST-01/02 product manual says that we should
957 * twiddle the DATA register before the control register. However,
958 * this does not work reliably so we do it the other way around.
960 * Probably could be a problem with arbitration too, we really should
961 * try this with a SCSI protocol or logic analyzer to see what is
962 * going on.
964 tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
965 tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
967 save_flags(flags);
968 cli();
969 #ifdef OLDCNTDATASCEME
970 #ifdef SWAPCNTDATA
971 WRITE_CONTROL (tmp_control);
972 WRITE_DATA (tmp_data);
973 #else
974 WRITE_DATA (tmp_data);
975 WRITE_CONTROL (tmp_control);
976 #endif
977 #else
978 tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
979 WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
980 WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
981 tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
982 WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
983 #endif
986 restore_flags (flags);
988 ULOOP( 250*1000 ) {
989 if (st0x_aborted) {
991 * If we have been aborted, and we have a command in progress, IE the
992 * target still has BSY asserted, then we will reset the bus, and
993 * notify the midlevel driver to expect sense.
996 WRITE_CONTROL (BASE_CMD);
997 if (STATUS & STAT_BSY) {
998 printk ("scsi%d : BST asserted after we've been aborted.\n", hostno);
999 seagate_st0x_reset (NULL, 0);
1000 return retcode (DID_RESET);
1002 return retcode (st0x_aborted);
1004 if (STATUS & STAT_BSY) break;
1005 if (TIMEOUT) {
1006 DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n",
1007 hostno, target, STATUS);
1008 return retcode (DID_NO_CONNECT);
1012 /* Establish current pointers. Take into account scatter / gather */
1014 if ((nobuffs = SCint->use_sg))
1016 #if (DEBUG & DEBUG_SG)
1018 int i;
1020 printk ("scsi%d : scatter gather requested, using %d buffers.\n",
1021 hostno, nobuffs);
1022 for (i = 0; i < nobuffs; ++i)
1023 printk ("scsi%d : buffer %d address = %08x length = %d\n",
1024 hostno, i, buffer[i].address, buffer[i].length);
1026 #endif
1028 buffer = (struct scatterlist *) SCint->buffer;
1029 len = buffer->length;
1030 data = (unsigned char *) buffer->address;
1032 else
1034 DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
1035 buffer = NULL;
1036 len = SCint->request_bufflen;
1037 data = (unsigned char *) SCint->request_buffer;
1040 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n", hostno, len);
1042 break;
1043 #ifdef LINKED
1044 case LINKED_RIGHT:
1045 break;
1046 case LINKED_WRONG:
1047 break;
1048 #endif
1049 } /* end of switch(reselect) */
1052 * There are several conditions under which we wish to send a message :
1053 * 1. When we are allowing disconnect / reconnect, and need to establish
1054 * the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
1056 * 2. When we are doing linked commands, are have the wrong I_T_L nexus
1057 * established and want to send an ABORT message.
1060 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1061 #ifdef LINKED
1062 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1063 (((reselect == CAN_RECONNECT)
1064 || (reselect == LINKED_WRONG)
1065 )? CMD_ATTN : 0));
1066 #else
1067 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1068 (((reselect == CAN_RECONNECT)
1069 )? CMD_ATTN : 0));
1070 #endif
1073 * INFORMATION TRANSFER PHASE
1075 * The nasty looking read / write inline assembler loops we use for
1076 * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1077 * the 'C' versions - since we're moving 1024 bytes of data, this
1078 * really adds up.
1080 * SJT: The nasty-looking assembler is gone, so it's slower.
1084 DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1086 incommand = 1;
1087 transfersize = SCint->transfersize;
1088 underflow = SCint->underflow;
1091 * Now, we poll the device for status information,
1092 * and handle any requests it makes. Note that since we are unsure of
1093 * how much data will be flowing across the system, etc and cannot
1094 * make reasonable timeouts, that we will instead have the midlevel
1095 * driver handle any timeouts that occur in this phase.
1098 while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done)
1100 #ifdef PARITY
1101 if (status_read & STAT_PARITY)
1103 printk ("scsi%d : got parity error\n", hostno);
1104 st0x_aborted = DID_PARITY;
1106 #endif
1108 if (status_read & STAT_REQ)
1110 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1111 if ((newphase = (status_read & REQ_MASK)) != phase)
1113 phase = newphase;
1114 switch (phase)
1116 case REQ_DATAOUT:
1117 printk ("scsi%d : phase = DATA OUT\n", hostno);
1118 break;
1119 case REQ_DATAIN:
1120 printk ("scsi%d : phase = DATA IN\n", hostno);
1121 break;
1122 case REQ_CMDOUT:
1123 printk ("scsi%d : phase = COMMAND OUT\n", hostno);
1124 break;
1125 case REQ_STATIN:
1126 printk ("scsi%d : phase = STATUS IN\n", hostno);
1127 break;
1128 case REQ_MSGOUT:
1129 printk ("scsi%d : phase = MESSAGE OUT\n", hostno);
1130 break;
1131 case REQ_MSGIN:
1132 printk ("scsi%d : phase = MESSAGE IN\n", hostno);
1133 break;
1134 default:
1135 printk ("scsi%d : phase = UNKNOWN\n", hostno);
1136 st0x_aborted = DID_ERROR;
1139 #endif
1140 switch (status_read & REQ_MASK)
1142 case REQ_DATAOUT:
1144 * If we are in fast mode, then we simply splat the data out
1145 * in word-sized chunks as fast as we can.
1148 if (!len)
1150 #if 0
1151 printk ("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
1152 st0x_aborted = DID_ERROR;
1153 fast = 0;
1154 #endif
1155 break;
1158 if (fast && transfersize && !(len % transfersize)
1159 && (len >= transfersize)
1160 #ifdef FAST32
1161 && !(transfersize % 4)
1162 #endif
1165 DPRINTK (DEBUG_FAST,
1166 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1167 " len = %d, data = %08x\n",
1168 hostno, SCint->underflow, SCint->transfersize, len, data);
1170 /* SJT: Start. Fast Write */
1171 #ifdef SEAGATE_USE_ASM
1172 __asm__(
1173 "cld\n\t"
1174 #ifdef FAST32
1175 "shr $2, %%ecx\n\t"
1176 "1:\t"
1177 "lodsl\n\t"
1178 "movl %%eax, (%%edi)\n\t"
1179 #else
1180 "1:\t"
1181 "lodsb\n\t"
1182 "movb %%al, (%%edi)\n\t"
1183 #endif
1184 "loop 1b;"
1185 /* output */ :
1186 /* input */ : "D" (phys_to_virt(st0x_dr)), "S" (data), "c" (SCint->transfersize)
1187 /* clobbered */ : "eax", "ecx", "esi" );
1188 #else /* SEAGATE_USE_ASM */
1190 #ifdef FAST32
1191 unsigned int *iop = phys_to_virt (st0x_dr);
1192 const unsigned int *dp = (unsigned int *) data;
1193 int xferlen = transfersize >> 2;
1194 #else
1195 unsigned char *iop = phys_to_virt (st0x_dr);
1196 const unsigned char *dp = data;
1197 int xferlen = transfersize;
1198 #endif
1199 for (; xferlen; --xferlen)
1200 *iop = *dp++;
1202 #endif /* SEAGATE_USE_ASM */
1203 /* SJT: End */
1204 len -= transfersize;
1205 data += transfersize;
1206 DPRINTK (DEBUG_FAST,
1207 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1208 hostno, len, data);
1210 else
1213 * We loop as long as we are in a data out phase, there is data to send,
1214 * and BSY is still active.
1217 /* SJT: Start. Slow Write. */
1218 #ifdef SEAGATE_USE_ASM
1220 * We loop as long as we are in a data out phase, there is data to send,
1221 * and BSY is still active.
1223 /* Local variables : len = ecx , data = esi,
1224 st0x_cr_sr = ebx, st0x_dr = edi
1226 __asm__ (
1227 /* Test for any data here at all. */
1228 "orl %%ecx, %%ecx\n\t"
1229 "jz 2f\n\t"
1230 "cld\n\t"
1231 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%ebx\n\t" */
1232 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%edi\n\t" */
1233 "1:\t"
1234 "movb (%%ebx), %%al\n\t"
1235 /* Test for BSY */
1236 "test $1, %%al\n\t"
1237 "jz 2f\n\t"
1238 /* Test for data out phase - STATUS & REQ_MASK should be
1239 REQ_DATAOUT, which is 0. */
1240 "test $0xe, %%al\n\t"
1241 "jnz 2f\n\t"
1242 /* Test for REQ */
1243 "test $0x10, %%al\n\t"
1244 "jz 1b\n\t"
1245 "lodsb\n\t"
1246 "movb %%al, (%%edi)\n\t"
1247 "loop 1b\n\t"
1248 "2:\n"
1249 /* output */ : "=S" (data), "=c" (len)
1250 /* input */ : "0" (data), "1" (len), "b" (phys_to_virt(st0x_cr_sr)), "D" (phys_to_virt(st0x_dr))
1251 /* clobbered */ : "eax", "ebx", "edi");
1252 #else /* SEAGATE_USE_ASM */
1253 while (len)
1255 unsigned char stat;
1257 stat = STATUS;
1258 if (!(stat & STAT_BSY) || ((stat & REQ_MASK) != REQ_DATAOUT))
1259 break;
1260 if (stat & STAT_REQ)
1262 WRITE_DATA (*data++);
1263 --len;
1266 #endif /* SEAGATE_USE_ASM */
1267 /* SJT: End. */
1270 if (!len && nobuffs)
1272 --nobuffs;
1273 ++buffer;
1274 len = buffer->length;
1275 data = (unsigned char *) buffer->address;
1276 DPRINTK (DEBUG_SG,
1277 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1278 hostno, len, data);
1280 break;
1282 case REQ_DATAIN:
1283 #ifdef SLOW_RATE
1284 if (borken)
1286 #if (DEBUG & (PHASE_DATAIN))
1287 transfered += len;
1288 #endif
1289 for (;
1290 len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN |
1291 STAT_REQ)
1292 ; --len)
1294 *data++ = DATA;
1295 borken_wait ();
1297 #if (DEBUG & (PHASE_DATAIN))
1298 transfered -= len;
1299 #endif
1301 else
1302 #endif
1304 if (fast && transfersize && !(len % transfersize) &&
1305 (len >= transfersize)
1306 #ifdef FAST32
1307 && !(transfersize % 4)
1308 #endif
1311 DPRINTK (DEBUG_FAST,
1312 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1313 " len = %d, data = %08x\n",
1314 hostno, SCint->underflow, SCint->transfersize, len, data);
1316 /* SJT: Start. Fast Read */
1317 #ifdef SEAGATE_USE_ASM
1318 __asm__(
1319 "cld\n\t"
1320 #ifdef FAST32
1321 "shr $2, %%ecx\n\t"
1322 "1:\t"
1323 "movl (%%esi), %%eax\n\t"
1324 "stosl\n\t"
1325 #else
1326 "1:\t"
1327 "movb (%%esi), %%al\n\t"
1328 "stosb\n\t"
1329 #endif
1330 "loop 1b\n\t"
1331 /* output */ :
1332 /* input */ : "S" (phys_to_virt(st0x_dr)), "D" (data), "c" (SCint->transfersize)
1333 /* clobbered */ : "eax", "ecx", "edi");
1334 #else /* SEAGATE_USE_ASM */
1336 #ifdef FAST32
1337 const unsigned int *iop = phys_to_virt (st0x_dr);
1338 unsigned int *dp = (unsigned int *) data;
1339 int xferlen = len >> 2;
1340 #else
1341 const unsigned char *iop = phys_to_virt (st0x_dr);
1342 unsigned char *dp = data;
1343 int xferlen = len;
1344 #endif
1345 for (; xferlen; --xferlen)
1346 *dp++ = *iop;
1348 #endif /* SEAGATE_USE_ASM */
1349 /* SJT: End */
1350 len -= transfersize;
1351 data += transfersize;
1352 #if (DEBUG & PHASE_DATAIN)
1353 printk ("scsi%d: transfered += %d\n", hostno, transfersize);
1354 transfered += transfersize;
1355 #endif
1357 DPRINTK (DEBUG_FAST,
1358 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1359 hostno, len, data);
1361 else
1364 #if (DEBUG & PHASE_DATAIN)
1365 printk ("scsi%d: transfered += %d\n", hostno, len);
1366 transfered += len; /* Assume we'll transfer it all, then
1367 subtract what we *didn't* transfer */
1368 #endif
1371 * We loop as long as we are in a data in phase, there is room to read,
1372 * and BSY is still active
1375 /* SJT: Start. */
1376 #ifdef SEAGATE_USE_ASM
1378 * We loop as long as we are in a data in phase, there is room to read,
1379 * and BSY is still active
1381 /* Local variables : ecx = len, edi = data
1382 esi = st0x_cr_sr, ebx = st0x_dr */
1383 __asm__ (
1384 /* Test for room to read */
1385 "orl %%ecx, %%ecx\n\t"
1386 "jz 2f\n\t"
1387 "cld\n\t"
1388 /* "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%esi\n\t" */
1389 /* "movl " SYMBOL_NAME_STR(st0x_dr) ", %%ebx\n\t" */
1390 "1:\t"
1391 "movb (%%esi), %%al\n\t"
1392 /* Test for BSY */
1393 "test $1, %%al\n\t"
1394 "jz 2f\n\t"
1395 /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1396 = STAT_IO, which is 4. */
1397 "movb $0xe, %%ah\n\t"
1398 "andb %%al, %%ah\n\t"
1399 "cmpb $0x04, %%ah\n\t"
1400 "jne 2f\n\t"
1401 /* Test for REQ */
1402 "test $0x10, %%al\n\t"
1403 "jz 1b\n\t"
1404 "movb (%%ebx), %%al\n\t"
1405 "stosb\n\t"
1406 "loop 1b\n\t"
1407 "2:\n"
1408 /* output */ : "=D" (data), "=c" (len)
1409 /* input */ : "0" (data), "1" (len), "S" (phys_to_virt(st0x_cr_sr)), "b" (phys_to_virt(st0x_dr))
1410 /* clobbered */ : "eax","ebx", "esi");
1411 #else /* SEAGATE_USE_ASM */
1412 while (len)
1414 unsigned char stat;
1416 stat = STATUS;
1417 if (!(stat & STAT_BSY) || ((stat & REQ_MASK) != REQ_DATAIN))
1418 break;
1419 if (stat & STAT_REQ)
1421 *data++ = DATA;
1422 --len;
1425 #endif /* SEAGATE_USE_ASM */
1426 /* SJT: End. */
1427 #if (DEBUG & PHASE_DATAIN)
1428 printk ("scsi%d: transfered -= %d\n", hostno, len);
1429 transfered -= len; /* Since we assumed all of Len got *
1430 transfered, correct our mistake */
1431 #endif
1434 if (!len && nobuffs)
1436 --nobuffs;
1437 ++buffer;
1438 len = buffer->length;
1439 data = (unsigned char *) buffer->address;
1440 DPRINTK (DEBUG_SG,
1441 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1442 hostno, len, data);
1445 break;
1447 case REQ_CMDOUT:
1448 while (((status_read = STATUS) & STAT_BSY) &&
1449 ((status_read & REQ_MASK) == REQ_CMDOUT))
1450 if (status_read & STAT_REQ)
1452 WRITE_DATA (*(const unsigned char *) cmnd);
1453 cmnd = 1 + (const unsigned char *) cmnd;
1454 #ifdef SLOW_RATE
1455 if (borken)
1456 borken_wait ();
1457 #endif
1459 break;
1461 case REQ_STATIN:
1462 status = DATA;
1463 break;
1465 case REQ_MSGOUT:
1467 * We can only have sent a MSG OUT if we requested to do this
1468 * by raising ATTN. So, we must drop ATTN.
1471 WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1473 * If we are reconnecting, then we must send an IDENTIFY message in
1474 * response to MSGOUT.
1476 switch (reselect)
1478 case CAN_RECONNECT:
1479 WRITE_DATA (IDENTIFY (1, lun));
1481 DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
1482 break;
1483 #ifdef LINKED
1484 case LINKED_WRONG:
1485 WRITE_DATA (ABORT);
1486 linked_connected = 0;
1487 reselect = CAN_RECONNECT;
1488 goto connect_loop;
1489 DPRINTK (PHASE_MSGOUT | DEBUG_LINKED,
1490 "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
1491 #endif /* LINKED */
1492 DPRINTK (DEBUG_LINKED, "correct\n");
1493 default:
1494 WRITE_DATA (NOP);
1495 printk ("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
1497 break;
1499 case REQ_MSGIN:
1500 switch (message = DATA)
1502 case DISCONNECT:
1503 DANY ("seagate: deciding to disconnect\n");
1504 should_reconnect = 1;
1505 current_data = data; /* WDE add */
1506 current_buffer = buffer;
1507 current_bufflen = len; /* WDE add */
1508 current_nobuffs = nobuffs;
1509 #ifdef LINKED
1510 linked_connected = 0;
1511 #endif
1512 done = 1;
1513 DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
1514 break;
1516 #ifdef LINKED
1517 case LINKED_CMD_COMPLETE:
1518 case LINKED_FLG_CMD_COMPLETE:
1519 #endif
1520 case COMMAND_COMPLETE:
1522 * Note : we should check for underflow here.
1524 DPRINTK (PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
1525 done = 1;
1526 break;
1527 case ABORT:
1528 DPRINTK (PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
1529 done = 1;
1530 break;
1531 case SAVE_POINTERS:
1532 current_buffer = buffer;
1533 current_bufflen = len; /* WDE add */
1534 current_data = data; /* WDE mod */
1535 current_nobuffs = nobuffs;
1536 DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
1537 break;
1538 case RESTORE_POINTERS:
1539 buffer = current_buffer;
1540 cmnd = current_cmnd;
1541 data = current_data; /* WDE mod */
1542 len = current_bufflen;
1543 nobuffs = current_nobuffs;
1544 DPRINTK (PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
1545 break;
1546 default:
1549 * IDENTIFY distinguishes itself from the other messages by setting the
1550 * high byte. [FIXME: should not this read "the high bit"? - pavel@ucw.cz]
1552 * Note : we need to handle at least one outstanding command per LUN,
1553 * and need to hash the SCSI command for that I_T_L nexus based on the
1554 * known ID (at this point) and LUN.
1557 if (message & 0x80)
1559 DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1560 hostno, target, message & 7);
1562 else
1566 * We should go into a MESSAGE OUT phase, and send a MESSAGE_REJECT
1567 * if we run into a message that we don't like. The seagate driver
1568 * needs some serious restructuring first though.
1571 DPRINTK (PHASE_MSGIN,
1572 "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
1575 break;
1577 default:
1578 printk ("scsi%d : unknown phase.\n", hostno);
1579 st0x_aborted = DID_ERROR;
1580 } /* end of switch (status_read &
1581 REQ_MASK) */
1583 #ifdef SLOW_RATE
1585 * I really don't care to deal with borken devices in each single
1586 * byte transfer case (ie, message in, message out, status), so
1587 * I'll do the wait here if necessary.
1589 if (borken)
1590 borken_wait ();
1591 #endif
1593 } /* if(status_read & STAT_REQ) ends */
1594 } /* while(((status_read = STATUS)...)
1595 ends */
1597 DPRINTK (PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT,
1598 "scsi%d : Transfered %d bytes\n", hostno, transfered);
1600 #if (DEBUG & PHASE_EXIT)
1601 #if 0 /* Doesn't work for scatter/gather */
1602 printk ("Buffer : \n");
1603 for (i = 0; i < 20; ++i)
1604 printk ("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
1605 printk ("\n");
1606 #endif
1607 printk ("scsi%d : status = ", hostno);
1608 print_status (status);
1609 printk ("message = %02x\n", message);
1610 #endif
1612 /* We shouldn't reach this until *after* BSY has been deasserted */
1614 #ifdef LINKED
1615 else
1618 * Fix the message byte so that unsuspecting high level drivers don't
1619 * puke when they see a LINKED COMMAND message in place of the COMMAND
1620 * COMPLETE they may be expecting. Shouldn't be necessary, but it's
1621 * better to be on the safe side.
1623 * A non LINKED* message byte will indicate that the command completed,
1624 * and we are now disconnected.
1627 switch (message)
1629 case LINKED_CMD_COMPLETE:
1630 case LINKED_FLG_CMD_COMPLETE:
1631 message = COMMAND_COMPLETE;
1632 linked_target = current_target;
1633 linked_lun = current_lun;
1634 linked_connected = 1;
1635 DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established"
1636 "for linked command.\n", hostno);
1637 /* We also will need to adjust status to accommodate intermediate
1638 conditions. */
1639 if ((status == INTERMEDIATE_GOOD) ||
1640 (status == INTERMEDIATE_C_GOOD))
1641 status = GOOD;
1643 break;
1645 * We should also handle what are "normal" termination messages
1646 * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually,
1647 * and flake if things aren't right.
1649 default:
1650 DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
1651 linked_connected = 0;
1654 #endif /* LINKED */
1656 if (should_reconnect)
1658 DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command()"
1659 "with reconnect enabled.\n", hostno);
1660 WRITE_CONTROL (BASE_CMD | CMD_INTR);
1662 else
1663 WRITE_CONTROL (BASE_CMD);
1665 return retcode (st0x_aborted);
1666 } /* end of internal_command */
1668 int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1670 st0x_aborted = DID_ABORT;
1671 return SCSI_ABORT_PENDING;
1673 #undef ULOOP
1674 #undef TIMEOUT
1677 * the seagate_st0x_reset function resets the SCSI bus
1680 int seagate_st0x_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1682 /* No timeouts - this command is going to fail because it was reset. */
1683 DANY ("scsi%d: Reseting bus... ", hostno );
1685 /* assert RESET signal on SCSI bus. */
1686 WRITE_CONTROL (BASE_CMD | CMD_RST);
1688 udelay( 20*1000 );
1690 WRITE_CONTROL (BASE_CMD);
1691 st0x_aborted = DID_RESET;
1693 DANY ("done.\n");
1694 return SCSI_RESET_WAKEUP;
1698 int seagate_st0x_biosparam (Disk * disk, kdev_t dev, int *ip)
1700 unsigned char buf[256 + sizeof (Scsi_Ioctl_Command)],
1701 cmd[6], *data, *page;
1702 Scsi_Ioctl_Command *sic = (Scsi_Ioctl_Command *) buf;
1703 int result, formatted_sectors, total_sectors;
1704 int cylinders, heads, sectors;
1705 int capacity;
1708 * Only SCSI-I CCS drives and later implement the necessary mode sense
1709 * pages.
1712 if (disk->device->scsi_level < 2)
1713 return -1;
1715 data = sic->data;
1717 cmd[0] = MODE_SENSE;
1718 cmd[1] = (disk->device->lun << 5) & 0xe5;
1719 cmd[2] = 0x04; /* Read page 4, rigid disk geometry
1720 page current values */
1721 cmd[3] = 0;
1722 cmd[4] = 255;
1723 cmd[5] = 0;
1726 * We are transferring 0 bytes in the out direction, and expect to get back
1727 * 24 bytes for each mode page.
1729 sic->inlen = 0;
1730 sic->outlen = 256;
1732 memcpy (data, cmd, 6);
1734 if (!(result = kernel_scsi_ioctl (disk->device, SCSI_IOCTL_SEND_COMMAND,
1735 sic)))
1738 * The mode page lies beyond the MODE SENSE header, with length 4, and
1739 * the BLOCK DESCRIPTOR, with length header[3].
1741 page = data + 4 + data[3];
1742 heads = (int) page[5];
1743 cylinders = (page[2] << 16) | (page[3] << 8) | page[4];
1745 cmd[2] = 0x03; /* Read page 3, format page current
1746 values */
1747 memcpy (data, cmd, 6);
1749 if (!(result = kernel_scsi_ioctl (disk->device, SCSI_IOCTL_SEND_COMMAND,
1750 sic)))
1752 page = data + 4 + data[3];
1753 sectors = (page[10] << 8) | page[11];
1755 * Get the total number of formatted sectors from the block descriptor,
1756 * so we can tell how many are being used for alternates.
1758 formatted_sectors = (data[4 + 1] << 16) | (data[4 + 2] << 8)
1759 | data[4 + 3];
1761 total_sectors = (heads * cylinders * sectors);
1764 * Adjust the real geometry by subtracting
1765 * (spare sectors / (heads * tracks)) cylinders from the number of cylinders.
1767 * It appears that the CE cylinder CAN be a partial cylinder.
1770 printk ("scsi%d : heads = %d cylinders = %d sectors = %d total = %d formatted = %d\n",
1771 hostno, heads, cylinders, sectors, total_sectors,
1772 formatted_sectors);
1774 if (!heads || !sectors || !cylinders)
1775 result = -1;
1776 else
1777 cylinders -= ((total_sectors - formatted_sectors) / (heads * sectors));
1780 * Now, we need to do a sanity check on the geometry to see if it is
1781 * BIOS compatible. The maximum BIOS geometry is 1024 cylinders *
1782 * 256 heads * 64 sectors.
1785 if ((cylinders > 1024) || (sectors > 64))
1787 /* The Seagate's seem to have some mapping. Multiply
1788 heads*sectors*cyl to get capacity. Then start rounding down.
1790 capacity = heads * sectors * cylinders;
1792 /* Old MFM Drives use this, so does the Seagate */
1793 sectors = 17;
1794 heads = 2;
1795 capacity = capacity / sectors;
1796 while (cylinders > 1024)
1798 heads *= 2; /* For some reason, they go in
1799 multiples */
1800 cylinders = capacity / heads;
1803 ip[0] = heads;
1804 ip[1] = sectors;
1805 ip[2] = cylinders;
1807 * There should be an alternate mapping for things the seagate doesn't
1808 * understand, but I couldn't say what it is with reasonable certainty.
1813 return result;
1816 #ifdef MODULE
1817 /* Eventually this will go into an include file, but this will be later */
1818 Scsi_Host_Template driver_template = SEAGATE_ST0X;
1820 #include "scsi_module.c"
1821 #endif