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>
5  *
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? :-)
9  *	        pavel@ucw.cz]
10  *
11  *      This card does all the I/O via memory mapped I/O, so there is no need
12  *      to check or allocate a region of the I/O address space.
13  */
14 
15 /* 1996 - to use new read{b,w,l}, write{b,w,l}, and phys_to_virt
16  * macros, replaced assembler routines with C. There's probably a
17  * performance hit, but I only have a cdrom and can't tell. Define
18  * SEAGATE_USE_ASM if you want the old assembler code -- SJT
19  *
20  * 1998-jul-29 - created DPRINTK macros and made it work under
21  * linux 2.1.112, simplified some #defines etc. <pavel@ucw.cz>
22  *
23  * Aug 2000 - aeb - deleted seagate_st0x_biosparam(). It would try to
24  * read the physical disk geometry, a bad mistake. Of course it doesn't
25  * matter much what geometry one invents, but on large disks it
26  * returned 256 (or more) heads, causing all kind of failures.
27  * Of course this means that people might see a different geometry now,
28  * so boot parameters may be necessary in some cases.
29  */
30 
31 /*
32  * Configuration :
33  * To use without BIOS -DOVERRIDE=base_address -DCONTROLLER=FD or SEAGATE
34  * -DIRQ will override the default of 5.
35  * Note: You can now set these options from the kernel's "command line".
36  * The syntax is:
37  *
38  *     st0x=ADDRESS,IRQ                (for a Seagate controller)
39  * or:
40  *     tmc8xx=ADDRESS,IRQ              (for a TMC-8xx or TMC-950 controller)
41  * eg:
42  *     tmc8xx=0xC8000,15
43  *
44  * will configure the driver for a TMC-8xx style controller using IRQ 15
45  * with a base address of 0xC8000.
46  *
47  * -DARBITRATE
48  *      Will cause the host adapter to arbitrate for the
49  *      bus for better SCSI-II compatibility, rather than just
50  *      waiting for BUS FREE and then doing its thing.  Should
51  *      let us do one command per Lun when I integrate my
52  *      reorganization changes into the distribution sources.
53  *
54  * -DDEBUG=65535
55  *      Will activate debug code.
56  *
57  * -DFAST or -DFAST32
58  *      Will use blind transfers where possible
59  *
60  * -DPARITY
61  *      This will enable parity.
62  *
63  * -DSEAGATE_USE_ASM
64  *      Will use older seagate assembly code. should be (very small amount)
65  *      Faster.
66  *
67  * -DSLOW_RATE=50
68  *      Will allow compatibility with broken devices that don't
69  *      handshake fast enough (ie, some CD ROM's) for the Seagate
70  *      code.
71  *
72  *      50 is some number, It will let you specify a default
73  *      transfer rate if handshaking isn't working correctly.
74  *
75  * -DOLDCNTDATASCEME  There is a new sceme to set the CONTROL
76  *                    and DATA reigsters which complies more closely
77  *                    with the SCSI2 standard. This hopefully eliminates
78  *                    the need to swap the order these registers are
79  *                    'messed' with. It makes the following two options
80  *                    obsolete. To reenable the old sceme define this.
81  *
82  * The following to options are patches from the SCSI.HOWTO
83  *
84  * -DSWAPSTAT  This will swap the definitions for STAT_MSG and STAT_CD.
85  *
86  * -DSWAPCNTDATA  This will swap the order that seagate.c messes with
87  *                the CONTROL an DATA registers.
88  */
89 
90 #include <linux/module.h>
91 
92 #include <asm/io.h>
93 #include <asm/system.h>
94 #include <linux/spinlock.h>
95 #include <linux/signal.h>
96 #include <linux/sched.h>
97 #include <linux/string.h>
98 #include <linux/proc_fs.h>
99 #include <linux/init.h>
100 #include <linux/delay.h>
101 #include <linux/blk.h>
102 #include "scsi.h"
103 #include "hosts.h"
104 #include "seagate.h"
105 #include "constants.h"
106 #include <linux/stat.h>
107 #include <asm/uaccess.h>
108 #include "sd.h"
109 #include <scsi/scsi_ioctl.h>
110 
111 #ifdef DEBUG
112 #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
113 #else
114 #define DPRINTK( when, msg... ) do { } while (0)
115 #endif
116 #define DANY( msg... ) DPRINTK( 0xffff, msg );
117 
118 #ifndef IRQ
119 #define IRQ 5
120 #endif
121 
122 #ifdef FAST32
123 #define FAST
124 #endif
125 
126 #undef LINKED			/* Linked commands are currently broken! */
127 
128 #if defined(OVERRIDE) && !defined(CONTROLLER)
129 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
130 #endif
131 
132 #ifndef __i386__
133 #undef SEAGATE_USE_ASM
134 #endif
135 
136 /*
137 	Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
138 		driver, and Mitsugu Suzuki for information on the ST-01
139 		SCSI host.
140 */
141 
142 /*
143 	CONTROL defines
144 */
145 
146 #define CMD_RST 		0x01
147 #define CMD_SEL 		0x02
148 #define CMD_BSY 		0x04
149 #define CMD_ATTN    		0x08
150 #define CMD_START_ARB		0x10
151 #define CMD_EN_PARITY		0x20
152 #define CMD_INTR		0x40
153 #define CMD_DRVR_ENABLE		0x80
154 
155 /*
156 	STATUS
157 */
158 #ifdef SWAPSTAT
159 #define STAT_MSG		0x08
160 #define STAT_CD			0x02
161 #else
162 #define STAT_MSG		0x02
163 #define STAT_CD			0x08
164 #endif
165 
166 #define STAT_BSY		0x01
167 #define STAT_IO			0x04
168 #define STAT_REQ		0x10
169 #define STAT_SEL		0x20
170 #define STAT_PARITY		0x40
171 #define STAT_ARB_CMPL		0x80
172 
173 /*
174 	REQUESTS
175 */
176 
177 #define REQ_MASK (STAT_CD |  STAT_IO | STAT_MSG)
178 #define REQ_DATAOUT 0
179 #define REQ_DATAIN STAT_IO
180 #define REQ_CMDOUT STAT_CD
181 #define REQ_STATIN (STAT_CD | STAT_IO)
182 #define REQ_MSGOUT (STAT_MSG | STAT_CD)
183 #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
184 
185 extern volatile int seagate_st0x_timeout;
186 
187 #ifdef PARITY
188 #define BASE_CMD CMD_EN_PARITY
189 #else
190 #define BASE_CMD  0
191 #endif
192 
193 /*
194 	Debugging code
195 */
196 
197 #define PHASE_BUS_FREE 1
198 #define PHASE_ARBITRATION 2
199 #define PHASE_SELECTION 4
200 #define PHASE_DATAIN 8
201 #define PHASE_DATAOUT 0x10
202 #define PHASE_CMDOUT 0x20
203 #define PHASE_MSGIN 0x40
204 #define PHASE_MSGOUT 0x80
205 #define PHASE_STATUSIN 0x100
206 #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
207 #define PRINT_COMMAND 0x200
208 #define PHASE_EXIT 0x400
209 #define PHASE_RESELECT 0x800
210 #define DEBUG_FAST 0x1000
211 #define DEBUG_SG   0x2000
212 #define DEBUG_LINKED	0x4000
213 #define DEBUG_BORKEN	0x8000
214 
215 /*
216  *	Control options - these are timeouts specified in .01 seconds.
217  */
218 
219 /* 30, 20 work */
220 #define ST0X_BUS_FREE_DELAY 25
221 #define ST0X_SELECTION_DELAY 25
222 
223 #define SEAGATE 1		/* these determine the type of the controller */
224 #define FD	2
225 
226 #define ST0X_ID_STR	"Seagate ST-01/ST-02"
227 #define FD_ID_STR	"TMC-8XX/TMC-950"
228 
229 static int internal_command (unsigned char target, unsigned char lun,
230 			     const void *cmnd,
231 			     void *buff, int bufflen, int reselect);
232 
233 static int incommand;		/* set if arbitration has finished
234 				   and we are in some command phase. */
235 
236 static unsigned int base_address = 0;	/* Where the card ROM starts, used to
237 					   calculate memory mapped register
238 					   location.  */
239 
240 static unsigned long st0x_cr_sr;	/* control register write, status
241 					   register read.  256 bytes in
242 					   length.
243 					   Read is status of SCSI BUS, as per
244 					   STAT masks.  */
245 
246 static unsigned long st0x_dr;	/* data register, read write 256
247 				   bytes in length.  */
248 
249 static volatile int st0x_aborted = 0;	/* set when we are aborted, ie by a
250 					   time out, etc.  */
251 
252 static unsigned char controller_type = 0;	/* set to SEAGATE for ST0x
253 						   boards or FD for TMC-8xx
254 						   boards */
255 static int irq = IRQ;
256 
257 MODULE_PARM (base_address, "i");
258 MODULE_PARM (controller_type, "b");
259 MODULE_PARM (irq, "i");
260 MODULE_LICENSE("GPL");
261 
262 
263 #define retcode(result) (((result) << 16) | (message << 8) | status)
264 #define STATUS ((u8) isa_readb(st0x_cr_sr))
265 #define DATA ((u8) isa_readb(st0x_dr))
266 #define WRITE_CONTROL(d) { isa_writeb((d), st0x_cr_sr); }
267 #define WRITE_DATA(d) { isa_writeb((d), st0x_dr); }
268 
269 void
st0x_setup(char * str,int * ints)270 st0x_setup (char *str, int *ints)
271 {
272 	controller_type = SEAGATE;
273 	base_address = ints[1];
274 	irq = ints[2];
275 }
276 
277 void
tmc8xx_setup(char * str,int * ints)278 tmc8xx_setup (char *str, int *ints)
279 {
280 	controller_type = FD;
281 	base_address = ints[1];
282 	irq = ints[2];
283 }
284 
285 #ifndef OVERRIDE
286 static unsigned int seagate_bases[] = {
287 	0xc8000, 0xca000, 0xcc000,
288 	0xce000, 0xdc000, 0xde000
289 };
290 
291 typedef struct {
292 	const unsigned char *signature;
293 	unsigned offset;
294 	unsigned length;
295 	unsigned char type;
296 } Signature;
297 
298 static Signature __initdata signatures[] = {
299 	{"ST01 v1.7  (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
300 	{"SCSI BIOS 2.00  (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
301 
302 /*
303  * The following two lines are NOT mistakes.  One detects ROM revision
304  * 3.0.0, the other 3.2.  Since seagate has only one type of SCSI adapter,
305  * and this is not going to change, the "SEAGATE" and "SCSI" together
306  * are probably "good enough"
307  */
308 
309 	{"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
310 	{"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
311 
312 /*
313  * However, future domain makes several incompatible SCSI boards, so specific
314  * signatures must be used.
315  */
316 
317 	{"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
318 	{"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
319 	{"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
320 	{"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
321 	{"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
322 	{"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
323 	{"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
324 	{"FUTURE DOMAIN TMC-950", 5, 21, FD},
325 	/* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
326 	{"IBM F1 V1.2009/22/93", 5, 25, FD},
327 	/* Added for Dell Latitude XP 4100CX */
328 	{"Future Domain Corp. V1.0008/18/9364/32", 5, 38, FD},
329 };
330 
331 #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
332 #endif				/* n OVERRIDE */
333 
334 /*
335  * hostno stores the hostnumber, as told to us by the init routine.
336  */
337 
338 static int hostno = -1;
339 static void seagate_reconnect_intr (int, void *, struct pt_regs *);
340 static void do_seagate_reconnect_intr (int, void *, struct pt_regs *);
341 
342 #ifdef FAST
343 static int fast = 1;
344 #else
345 #define fast 0
346 #endif
347 
348 #ifdef SLOW_RATE
349 /*
350  * Support for broken devices :
351  * The Seagate board has a handshaking problem.  Namely, a lack
352  * thereof for slow devices.  You can blast 600K/second through
353  * it if you are polling for each byte, more if you do a blind
354  * transfer.  In the first case, with a fast device, REQ will
355  * transition high-low or high-low-high before your loop restarts
356  * and you'll have no problems.  In the second case, the board
357  * will insert wait states for up to 13.2 usecs for REQ to
358  * transition low->high, and everything will work.
359  *
360  * However, there's nothing in the state machine that says
361  * you *HAVE* to see a high-low-high set of transitions before
362  * sending the next byte, and slow things like the Trantor CD ROMS
363  * will break because of this.
364  *
365  * So, we need to slow things down, which isn't as simple as it
366  * seems.  We can't slow things down period, because then people
367  * who don't recompile their kernels will shoot me for ruining
368  * their performance.  We need to do it on a case per case basis.
369  *
370  * The best for performance will be to, only for borken devices
371  * (this is stored on a per-target basis in the scsi_devices array)
372  *
373  * Wait for a low->high transition before continuing with that
374  * transfer.  If we timeout, continue anyways.  We don't need
375  * a long timeout, because REQ should only be asserted until the
376  * corresponding ACK is received and processed.
377  *
378  * Note that we can't use the system timer for this, because of
379  * resolution, and we *really* can't use the timer chip since
380  * gettimeofday() and the beeper routines use that.  So,
381  * the best thing for us to do will be to calibrate a timing
382  * loop in the initialization code using the timer chip before
383  * gettimeofday() can screw with it.
384  *
385  * FIXME: this is broken (not borken :-). Empty loop costs less than
386  * loop with ISA access in it! -- pavel@ucw.cz
387  */
388 
389 static int borken_calibration = 0;
390 
borken_init(void)391 static void __init borken_init (void)
392 {
393 	register int count = 0, start = jiffies + 1, stop = start + 25;
394 
395 	while (time_before (jiffies, start)) ;
396 	for (; time_before (jiffies, stop); ++count) ;
397 
398 /*
399  * Ok, we now have a count for .25 seconds.  Convert to a
400  * count per second and divide by transfer rate in K.  */
401 
402 	borken_calibration = (count * 4) / (SLOW_RATE * 1024);
403 
404 	if (borken_calibration < 1)
405 		borken_calibration = 1;
406 }
407 
borken_wait(void)408 static inline void borken_wait (void)
409 {
410 	register int count;
411 
412 	for (count = borken_calibration; count && (STATUS & STAT_REQ);
413 	     --count) ;
414 #if (DEBUG & DEBUG_BORKEN)
415 	if (count)
416 		printk ("scsi%d : borken timeout\n", hostno);
417 #endif
418 }
419 
420 #endif				/* def SLOW_RATE */
421 
422 /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
423  * contains at least one ISA access, which takes more than 0.125
424  * usec. So if we loop 8 times time in usec, we are safe.
425  */
426 
427 #define ULOOP( i ) for (clock = i*8;;)
428 #define TIMEOUT (!(clock--))
429 
seagate_st0x_detect(Scsi_Host_Template * tpnt)430 int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
431 {
432 	struct Scsi_Host *instance;
433 	int i, j;
434 
435 	tpnt->proc_name = "seagate";
436 /*
437  *    First, we try for the manual override.
438  */
439 	DANY ("Autodetecting ST0x / TMC-8xx\n");
440 
441 	if (hostno != -1) {
442 		printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
443 		return 0;
444 	}
445 
446 /* If the user specified the controller type from the command line,
447    controller_type will be non-zero, so don't try to detect one */
448 
449 	if (!controller_type) {
450 #ifdef OVERRIDE
451 		base_address = OVERRIDE;
452 		controller_type = CONTROLLER;
453 
454 		DANY ("Base address overridden to %x, controller type is %s\n",
455 		      base_address,
456 		      controller_type == SEAGATE ? "SEAGATE" : "FD");
457 #else				/* OVERRIDE */
458 /*
459  * 	To detect this card, we simply look for the signature
460  *      from the BIOS version notice in all the possible locations
461  *      of the ROM's.  This has a nice side effect of not trashing
462  *      any register locations that might be used by something else.
463  *
464  * XXX - note that we probably should be probing the address
465  * space for the on-board RAM instead.
466  */
467 
468 		for (i = 0;
469 		     i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i)
470 
471 			for (j = 0; !base_address && j < NUM_SIGNATURES; ++j)
472 				if (isa_check_signature
473 				    (seagate_bases[i] + signatures[j].offset,
474 				     signatures[j].signature,
475 				     signatures[j].length)) {
476 					base_address = seagate_bases[i];
477 					controller_type = signatures[j].type;
478 				}
479 #endif				/* OVERRIDE */
480 	}
481 	/* (! controller_type) */
482 	tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
483 	tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
484 
485 	if (!base_address) {
486 		DANY ("ST0x / TMC-8xx not detected.\n");
487 		return 0;
488 	}
489 
490 	st0x_cr_sr =
491 	    base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
492 	st0x_dr = st0x_cr_sr + 0x200;
493 
494 	DANY ("%s detected. Base address = %x, cr = %x, dr = %x\n",
495 	      tpnt->name, base_address, st0x_cr_sr, st0x_dr);
496 
497 /*
498  *	At all times, we will use IRQ 5.  Should also check for IRQ3 if we
499  *      loose our first interrupt.
500  */
501 	instance = scsi_register (tpnt, 0);
502 	if (instance == NULL)
503 		return 0;
504 
505 	hostno = instance->host_no;
506 	if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT,
507 			 (controller_type == SEAGATE) ? "seagate" : "tmc-8xx",
508 			 NULL)) {
509 		printk ("scsi%d : unable to allocate IRQ%d\n", hostno, irq);
510 		return 0;
511 	}
512 	instance->irq = irq;
513 	instance->io_port = base_address;
514 #ifdef SLOW_RATE
515 	printk (KERN_INFO "Calibrating borken timer... ");
516 	borken_init ();
517 	printk (" %d cycles per transfer\n", borken_calibration);
518 #endif
519 
520 	printk (KERN_INFO "This is one second... ");
521 	{
522 		int clock;
523 		ULOOP (1 * 1000 * 1000) {
524 			STATUS;
525 			if (TIMEOUT)
526 				break;
527 		}
528 	}
529 
530 	printk ("done, %s options:"
531 #ifdef ARBITRATE
532 		" ARBITRATE"
533 #endif
534 #ifdef DEBUG
535 		" DEBUG"
536 #endif
537 #ifdef FAST
538 		" FAST"
539 #ifdef FAST32
540 		"32"
541 #endif
542 #endif
543 #ifdef LINKED
544 		" LINKED"
545 #endif
546 #ifdef PARITY
547 		" PARITY"
548 #endif
549 #ifdef SEAGATE_USE_ASM
550 		" SEAGATE_USE_ASM"
551 #endif
552 #ifdef SLOW_RATE
553 		" SLOW_RATE"
554 #endif
555 #ifdef SWAPSTAT
556 		" SWAPSTAT"
557 #endif
558 #ifdef SWAPCNTDATA
559 		" SWAPCNTDATA"
560 #endif
561 		"\n", tpnt->name);
562 	return 1;
563 }
564 
565 const char *
seagate_st0x_info(struct Scsi_Host * shpnt)566 seagate_st0x_info (struct Scsi_Host *shpnt)
567 {
568 	static char buffer[64];
569 
570 	sprintf (buffer, "%s at irq %d, address 0x%05X",
571 		 (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
572 		 irq, base_address);
573 	return buffer;
574 }
575 
576 /*
577  * These are our saved pointers for the outstanding command that is
578  * waiting for a reconnect
579  */
580 
581 static unsigned char current_target, current_lun;
582 static unsigned char *current_cmnd, *current_data;
583 static int current_nobuffs;
584 static struct scatterlist *current_buffer;
585 static int current_bufflen;
586 
587 #ifdef LINKED
588 /*
589  * linked_connected indicates whether or not we are currently connected to
590  * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
591  * using linked commands.
592  */
593 
594 static int linked_connected = 0;
595 static unsigned char linked_target, linked_lun;
596 #endif
597 
598 static void (*done_fn) (Scsi_Cmnd *) = NULL;
599 static Scsi_Cmnd *SCint = NULL;
600 
601 /*
602  * These control whether or not disconnect / reconnect will be attempted,
603  * or are being attempted.
604  */
605 
606 #define NO_RECONNECT    0
607 #define RECONNECT_NOW   1
608 #define CAN_RECONNECT   2
609 
610 /*
611  * LINKED_RIGHT indicates that we are currently connected to the correct target
612  * for this command, LINKED_WRONG indicates that we are connected to the wrong
613  * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
614  */
615 
616 #define LINKED_RIGHT    3
617 #define LINKED_WRONG    4
618 
619 /*
620  * This determines if we are expecting to reconnect or not.
621  */
622 
623 static int should_reconnect = 0;
624 
625 /*
626  * The seagate_reconnect_intr routine is called when a target reselects the
627  * host adapter.  This occurs on the interrupt triggered by the target
628  * asserting SEL.
629  */
630 
do_seagate_reconnect_intr(int irq,void * dev_id,struct pt_regs * regs)631 static void do_seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
632 {
633 	unsigned long flags;
634 
635 	spin_lock_irqsave (&io_request_lock, flags);
636 	seagate_reconnect_intr (irq, dev_id, regs);
637 	spin_unlock_irqrestore (&io_request_lock, flags);
638 }
639 
seagate_reconnect_intr(int irq,void * dev_id,struct pt_regs * regs)640 static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
641 {
642 	int temp;
643 	Scsi_Cmnd *SCtmp;
644 
645 	DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n",
646 		 hostno);
647 
648 	if (!should_reconnect)
649 		printk ("scsi%d: unexpected interrupt.\n", hostno);
650 	else {
651 		should_reconnect = 0;
652 
653 		DPRINTK (PHASE_RESELECT, "scsi%d : internal_command("
654 			 "%d, %08x, %08x, RECONNECT_NOW\n", hostno,
655 			 current_target, current_data, current_bufflen);
656 
657 		temp =
658 		    internal_command (current_target, current_lun, current_cmnd,
659 				      current_data, current_bufflen,
660 				      RECONNECT_NOW);
661 
662 		if (msg_byte (temp) != DISCONNECT) {
663 			if (done_fn) {
664 				DPRINTK (PHASE_RESELECT,
665 					 "scsi%d : done_fn(%d,%08x)", hostno,
666 					 hostno, temp);
667 				if (!SCint)
668 					panic ("SCint == NULL in seagate");
669 				SCtmp = SCint;
670 				SCint = NULL;
671 				SCtmp->result = temp;
672 				done_fn (SCtmp);
673 			} else
674 				printk ("done_fn() not defined.\n");
675 		}
676 	}
677 }
678 
679 /*
680  * The seagate_st0x_queue_command() function provides a queued interface
681  * to the seagate SCSI driver.  Basically, it just passes control onto the
682  * seagate_command() function, after fixing it so that the done_fn()
683  * is set to the one passed to the function.  We have to be very careful,
684  * because there are some commands on some devices that do not disconnect,
685  * and if we simply call the done_fn when the command is done then another
686  * command is started and queue_command is called again...  We end up
687  * overflowing the kernel stack, and this tends not to be such a good idea.
688  */
689 
690 static int recursion_depth = 0;
691 
seagate_st0x_queue_command(Scsi_Cmnd * SCpnt,void (* done)(Scsi_Cmnd *))692 int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
693 {
694 	int result, reconnect;
695 	Scsi_Cmnd *SCtmp;
696 
697 	DANY ("seagate: que_command");
698 	done_fn = done;
699 	current_target = SCpnt->target;
700 	current_lun = SCpnt->lun;
701 	current_cmnd = SCpnt->cmnd;
702 	current_data = (unsigned char *) SCpnt->request_buffer;
703 	current_bufflen = SCpnt->request_bufflen;
704 	SCint = SCpnt;
705 	if (recursion_depth)
706 		return 0;
707 	recursion_depth++;
708 	do {
709 #ifdef LINKED
710 /*
711  * Set linked command bit in control field of SCSI command.
712  */
713 
714 		current_cmnd[SCpnt->cmd_len] |= 0x01;
715 		if (linked_connected) {
716 			DPRINTK (DEBUG_LINKED,
717 				 "scsi%d : using linked commands, current I_T_L nexus is ",
718 				 hostno);
719 			if ((linked_target == current_target)
720 			    && (linked_lun == current_lun)) {
721 				DPRINTK (DEBUG_LINKED, "correct\n");
722 				reconnect = LINKED_RIGHT;
723 			} else {
724 				DPRINTK (DEBUG_LINKED, "incorrect\n");
725 				reconnect = LINKED_WRONG;
726 			}
727 		} else
728 #endif				/* LINKED */
729 			reconnect = CAN_RECONNECT;
730 
731 		result =
732 		    internal_command (SCint->target, SCint->lun, SCint->cmnd,
733 				      SCint->request_buffer,
734 				      SCint->request_bufflen, reconnect);
735 		if (msg_byte (result) == DISCONNECT)
736 			break;
737 		SCtmp = SCint;
738 		SCint = NULL;
739 		SCtmp->result = result;
740 		done_fn (SCtmp);
741 	}
742 	while (SCint);
743 	recursion_depth--;
744 	return 0;
745 }
746 
seagate_st0x_command(Scsi_Cmnd * SCpnt)747 int seagate_st0x_command (Scsi_Cmnd * SCpnt)
748 {
749 	return internal_command (SCpnt->target, SCpnt->lun, SCpnt->cmnd,
750 				 SCpnt->request_buffer, SCpnt->request_bufflen,
751 				 (int) NO_RECONNECT);
752 }
753 
internal_command(unsigned char target,unsigned char lun,const void * cmnd,void * buff,int bufflen,int reselect)754 static int internal_command (unsigned char target, unsigned char lun,
755 		  const void *cmnd, void *buff, int bufflen, int reselect)
756 {
757 	unsigned char *data = NULL;
758 	struct scatterlist *buffer = NULL;
759 	int clock, temp, nobuffs = 0, done = 0, len = 0;
760 	unsigned long flags;
761 
762 #ifdef DEBUG
763 	int transfered = 0, phase = 0, newphase;
764 #endif
765 
766 	register unsigned char status_read;
767 	unsigned char tmp_data, tmp_control, status = 0, message = 0;
768 
769 	unsigned transfersize = 0, underflow = 0;
770 
771 #ifdef SLOW_RATE
772 	int borken = (int) SCint->device->borken;	/* Does the current target require
773 							   Very Slow I/O ?  */
774 #endif
775 
776 	incommand = 0;
777 	st0x_aborted = 0;
778 
779 #if (DEBUG & PRINT_COMMAND)
780 	printk ("scsi%d : target = %d, command = ", hostno, target);
781 	print_command ((unsigned char *) cmnd);
782 #endif
783 
784 #if (DEBUG & PHASE_RESELECT)
785 	switch (reselect) {
786 	case RECONNECT_NOW:
787 		printk ("scsi%d : reconnecting\n", hostno);
788 		break;
789 #ifdef LINKED
790 	case LINKED_RIGHT:
791 		printk ("scsi%d : connected, can reconnect\n", hostno);
792 		break;
793 	case LINKED_WRONG:
794 		printk ("scsi%d : connected to wrong target, can reconnect\n",
795 			hostno);
796 		break;
797 #endif
798 	case CAN_RECONNECT:
799 		printk ("scsi%d : allowed to reconnect\n", hostno);
800 		break;
801 	default:
802 		printk ("scsi%d : not allowed to reconnect\n", hostno);
803 	}
804 #endif
805 
806 	if (target == (controller_type == SEAGATE ? 7 : 6))
807 		return DID_BAD_TARGET;
808 
809 /*
810  *	We work it differently depending on if this is is "the first time,"
811  *      or a reconnect.  If this is a reselect phase, then SEL will
812  *      be asserted, and we must skip selection / arbitration phases.
813  */
814 
815 	switch (reselect) {
816 	case RECONNECT_NOW:
817 		DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
818 
819 /*
820  *	At this point, we should find the logical or of our ID and the original
821  *      target's ID on the BUS, with BSY, SEL, and I/O signals asserted.
822  *
823  *      After ARBITRATION phase is completed, only SEL, BSY, and the
824  *      target ID are asserted.  A valid initiator ID is not on the bus
825  *      until IO is asserted, so we must wait for that.
826  */
827 		ULOOP (100 * 1000) {
828 			temp = STATUS;
829 			if ((temp & STAT_IO) && !(temp & STAT_BSY))
830 				break;
831 
832 			if (TIMEOUT) {
833 				DPRINTK (PHASE_RESELECT,
834 					 "scsi%d : RESELECT timed out while waiting for IO .\n",
835 					 hostno);
836 				return (DID_BAD_INTR << 16);
837 			}
838 		}
839 
840 /*
841  *	After I/O is asserted by the target, we can read our ID and its
842  *      ID off of the BUS.
843  */
844 
845 		if (!
846 		    ((temp =
847 		      DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
848 			DPRINTK (PHASE_RESELECT,
849 				 "scsi%d : detected reconnect request to different target.\n"
850 				 "\tData bus = %d\n", hostno, temp);
851 			return (DID_BAD_INTR << 16);
852 		}
853 
854 		if (!(temp & (1 << current_target))) {
855 			printk
856 			    ("scsi%d : Unexpected reselect interrupt.  Data bus = %d\n",
857 			     hostno, temp);
858 			return (DID_BAD_INTR << 16);
859 		}
860 
861 		buffer = current_buffer;
862 		cmnd = current_cmnd;	/* WDE add */
863 		data = current_data;	/* WDE add */
864 		len = current_bufflen;	/* WDE add */
865 		nobuffs = current_nobuffs;
866 
867 /*
868  *	We have determined that we have been selected.  At this point,
869  *      we must respond to the reselection by asserting BSY ourselves
870  */
871 
872 #if 1
873 		WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
874 #else
875 		WRITE_CONTROL (BASE_CMD | CMD_BSY);
876 #endif
877 
878 /*
879  *	The target will drop SEL, and raise BSY, at which time we must drop
880  *      BSY.
881  */
882 
883 		ULOOP (100 * 1000) {
884 			if (!(STATUS & STAT_SEL))
885 				break;
886 			if (TIMEOUT) {
887 				WRITE_CONTROL (BASE_CMD | CMD_INTR);
888 				DPRINTK (PHASE_RESELECT,
889 					 "scsi%d : RESELECT timed out while waiting for SEL.\n",
890 					 hostno);
891 				return (DID_BAD_INTR << 16);
892 			}
893 		}
894 
895 		WRITE_CONTROL (BASE_CMD);
896 
897 /*
898  *	At this point, we have connected with the target and can get
899  *      on with our lives.
900  */
901 		break;
902 	case CAN_RECONNECT:
903 
904 #ifdef LINKED
905 /*
906  * This is a bletcherous hack, just as bad as the Unix #! interpreter stuff.
907  * If it turns out we are using the wrong I_T_L nexus, the easiest way to deal
908  * with it is to go into our INFORMATION TRANSFER PHASE code, send a ABORT
909  * message on MESSAGE OUT phase, and then loop back to here.
910  */
911 
912 	      connect_loop:
913 
914 #endif
915 
916 		DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n",
917 			 hostno);
918 
919 /*
920  *    BUS FREE PHASE
921  *
922  *      On entry, we make sure that the BUS is in a BUS FREE
923  *      phase, by insuring that both BSY and SEL are low for
924  *      at least one bus settle delay.  Several reads help
925  *      eliminate wire glitch.
926  */
927 
928 #ifndef ARBITRATE
929 #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
930 		clock = jiffies + ST0X_BUS_FREE_DELAY;
931 
932 		while (((STATUS | STATUS | STATUS) &
933 			(STAT_BSY | STAT_SEL)) &&
934 		       (!st0x_aborted) && time_before (jiffies, clock)) ;
935 
936 		if (time_after (jiffies, clock))
937 			return retcode (DID_BUS_BUSY);
938 		else if (st0x_aborted)
939 			return retcode (st0x_aborted);
940 #endif
941 
942 		DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n",
943 			 hostno);
944 
945 		clock = jiffies + ST0X_SELECTION_DELAY;
946 
947 /*
948  * Arbitration/selection procedure :
949  * 1.  Disable drivers
950  * 2.  Write HOST adapter address bit
951  * 3.  Set start arbitration.
952  * 4.  We get either ARBITRATION COMPLETE or SELECT at this
953  *     point.
954  * 5.  OR our ID and targets on bus.
955  * 6.  Enable SCSI drivers and asserted SEL and ATTN
956  */
957 
958 #ifdef ARBITRATE
959 		save_flags (flags);
960 		cli ();
961 		WRITE_CONTROL (0);
962 		WRITE_DATA ((controller_type == SEAGATE) ? 0x80 : 0x40);
963 		WRITE_CONTROL (CMD_START_ARB);
964 		restore_flags (flags);
965 
966 		ULOOP (ST0X_SELECTION_DELAY * 10000) {
967 			status_read = STATUS;
968 			if (status_read & STAT_ARB_CMPL)
969 				break;
970 			if (st0x_aborted)	/* FIXME: What? We are going to do something even after abort? */
971 				break;
972 			if (TIMEOUT || (status_read & STAT_SEL)) {
973 				printk
974 				    ("scsi%d : arbitration lost or timeout.\n",
975 				     hostno);
976 				WRITE_CONTROL (BASE_CMD);
977 				return retcode (DID_NO_CONNECT);
978 			}
979 		}
980 
981 		DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n",
982 			 hostno);
983 #endif
984 
985 /*
986  *    When the SCSI device decides that we're gawking at it, it will
987  *    respond by asserting BUSY on the bus.
988  *
989  *    Note : the Seagate ST-01/02 product manual says that we should
990  *    twiddle the DATA register before the control register.    However,
991  *    this does not work reliably so we do it the other way around.
992  *
993  *    Probably could be a problem with arbitration too, we really should
994  *    try this with a SCSI protocol or logic analyzer to see what is
995  *    going on.
996  */
997 		tmp_data =
998 		    (unsigned char) ((1 << target) |
999 				     (controller_type ==
1000 				      SEAGATE ? 0x80 : 0x40));
1001 		tmp_control =
1002 		    BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN
1003 							    : 0);
1004 
1005 		save_flags (flags);
1006 		cli ();
1007 #ifdef OLDCNTDATASCEME
1008 #ifdef SWAPCNTDATA
1009 		WRITE_CONTROL (tmp_control);
1010 		WRITE_DATA (tmp_data);
1011 #else
1012 		WRITE_DATA (tmp_data);
1013 		WRITE_CONTROL (tmp_control);
1014 #endif
1015 #else
1016 		tmp_control ^= CMD_BSY;	/* This is guesswork. What used to be in driver    */
1017 		WRITE_CONTROL (tmp_control);	/* could never work: it sent data into control     */
1018 		WRITE_DATA (tmp_data);	/* register and control info into data. Hopefully  */
1019 		tmp_control ^= CMD_BSY;	/* fixed, but order of first two may be wrong.     */
1020 		WRITE_CONTROL (tmp_control);	/* -- pavel@ucw.cz   */
1021 #endif
1022 
1023 		restore_flags (flags);
1024 
1025 		ULOOP (250 * 1000) {
1026 			if (st0x_aborted) {
1027 /*
1028  *	If we have been aborted, and we have a command in progress, IE the
1029  *      target still has BSY asserted, then we will reset the bus, and
1030  *      notify the midlevel driver to expect sense.
1031  */
1032 
1033 				WRITE_CONTROL (BASE_CMD);
1034 				if (STATUS & STAT_BSY) {
1035 					printk
1036 					    ("scsi%d : BST asserted after we've been aborted.\n",
1037 					     hostno);
1038 					seagate_st0x_reset (NULL, 0);
1039 					return retcode (DID_RESET);
1040 				}
1041 				return retcode (st0x_aborted);
1042 			}
1043 			if (STATUS & STAT_BSY)
1044 				break;
1045 			if (TIMEOUT) {
1046 				DPRINTK (PHASE_SELECTION,
1047 					 "scsi%d : NO CONNECT with target %d, stat = %x \n",
1048 					 hostno, target, STATUS);
1049 				return retcode (DID_NO_CONNECT);
1050 			}
1051 		}
1052 
1053 /* Establish current pointers.  Take into account scatter / gather */
1054 
1055 		if ((nobuffs = SCint->use_sg)) {
1056 #if (DEBUG & DEBUG_SG)
1057 			{
1058 				int i;
1059 
1060 				printk
1061 				    ("scsi%d : scatter gather requested, using %d buffers.\n",
1062 				     hostno, nobuffs);
1063 				for (i = 0; i < nobuffs; ++i)
1064 					printk
1065 					    ("scsi%d : buffer %d address = %08x length = %d\n",
1066 					     hostno, i, buffer[i].address,
1067 					     buffer[i].length);
1068 			}
1069 #endif
1070 
1071 			buffer = (struct scatterlist *) SCint->buffer;
1072 			len = buffer->length;
1073 			data = (unsigned char *) buffer->address;
1074 		} else {
1075 			DPRINTK (DEBUG_SG,
1076 				 "scsi%d : scatter gather not requested.\n",
1077 				 hostno);
1078 			buffer = NULL;
1079 			len = SCint->request_bufflen;
1080 			data = (unsigned char *) SCint->request_buffer;
1081 		}
1082 
1083 		DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
1084 			 hostno, len);
1085 
1086 		break;
1087 #ifdef LINKED
1088 	case LINKED_RIGHT:
1089 		break;
1090 	case LINKED_WRONG:
1091 		break;
1092 #endif
1093 	}			/* end of switch(reselect) */
1094 
1095 /*
1096  *    There are several conditions under which we wish to send a message :
1097  *      1.  When we are allowing disconnect / reconnect, and need to establish
1098  *          the I_T_L nexus via an IDENTIFY with the DiscPriv bit set.
1099  *
1100  *      2.  When we are doing linked commands, are have the wrong I_T_L nexus
1101  *          established and want to send an ABORT message.
1102  */
1103 
1104 /* GCC does not like an ifdef inside a macro, so do it the hard way. */
1105 #ifdef LINKED
1106 	WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1107 		       (((reselect == CAN_RECONNECT)
1108 			 || (reselect == LINKED_WRONG)
1109 			)? CMD_ATTN : 0));
1110 #else
1111 	WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE |
1112 		       (((reselect == CAN_RECONNECT)
1113 			)? CMD_ATTN : 0));
1114 #endif
1115 
1116 /*
1117  *    INFORMATION TRANSFER PHASE
1118  *
1119  *      The nasty looking read / write inline assembler loops we use for
1120  *      DATAIN and DATAOUT phases are approximately 4-5 times as fast as
1121  *      the 'C' versions - since we're moving 1024 bytes of data, this
1122  *      really adds up.
1123  *
1124  *      SJT: The nasty-looking assembler is gone, so it's slower.
1125  *
1126  */
1127 
1128 	DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
1129 
1130 	incommand = 1;
1131 	transfersize = SCint->transfersize;
1132 	underflow = SCint->underflow;
1133 
1134 /*
1135  *	Now, we poll the device for status information,
1136  *      and handle any requests it makes.  Note that since we are unsure of
1137  *      how much data will be flowing across the system, etc and cannot
1138  *      make reasonable timeouts, that we will instead have the midlevel
1139  *      driver handle any timeouts that occur in this phase.
1140  */
1141 
1142 	while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
1143 #ifdef PARITY
1144 		if (status_read & STAT_PARITY) {
1145 			printk ("scsi%d : got parity error\n", hostno);
1146 			st0x_aborted = DID_PARITY;
1147 		}
1148 #endif
1149 
1150 		if (status_read & STAT_REQ) {
1151 #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
1152 			if ((newphase = (status_read & REQ_MASK)) != phase) {
1153 				phase = newphase;
1154 				switch (phase) {
1155 				case REQ_DATAOUT:
1156 					printk ("scsi%d : phase = DATA OUT\n",
1157 						hostno);
1158 					break;
1159 				case REQ_DATAIN:
1160 					printk ("scsi%d : phase = DATA IN\n",
1161 						hostno);
1162 					break;
1163 				case REQ_CMDOUT:
1164 					printk
1165 					    ("scsi%d : phase = COMMAND OUT\n",
1166 					     hostno);
1167 					break;
1168 				case REQ_STATIN:
1169 					printk ("scsi%d : phase = STATUS IN\n",
1170 						hostno);
1171 					break;
1172 				case REQ_MSGOUT:
1173 					printk
1174 					    ("scsi%d : phase = MESSAGE OUT\n",
1175 					     hostno);
1176 					break;
1177 				case REQ_MSGIN:
1178 					printk ("scsi%d : phase = MESSAGE IN\n",
1179 						hostno);
1180 					break;
1181 				default:
1182 					printk ("scsi%d : phase = UNKNOWN\n",
1183 						hostno);
1184 					st0x_aborted = DID_ERROR;
1185 				}
1186 			}
1187 #endif
1188 			switch (status_read & REQ_MASK) {
1189 			case REQ_DATAOUT:
1190 /*
1191  * If we are in fast mode, then we simply splat the data out
1192  * in word-sized chunks as fast as we can.
1193  */
1194 
1195 				if (!len) {
1196 #if 0
1197 					printk
1198 					    ("scsi%d: underflow to target %d lun %d \n",
1199 					     hostno, target, lun);
1200 					st0x_aborted = DID_ERROR;
1201 					fast = 0;
1202 #endif
1203 					break;
1204 				}
1205 
1206 				if (fast && transfersize
1207 				    && !(len % transfersize)
1208 				    && (len >= transfersize)
1209 #ifdef FAST32
1210 				    && !(transfersize % 4)
1211 #endif
1212 				    ) {
1213 					DPRINTK (DEBUG_FAST,
1214 						 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1215 						 "         len = %d, data = %08x\n",
1216 						 hostno, SCint->underflow,
1217 						 SCint->transfersize, len,
1218 						 data);
1219 
1220 /* SJT: Start. Fast Write */
1221 #ifdef SEAGATE_USE_ASM
1222 					__asm__ ("cld\n\t"
1223 #ifdef FAST32
1224 						 "shr $2, %%ecx\n\t"
1225 						 "1:\t"
1226 						 "lodsl\n\t"
1227 						 "movl %%eax, (%%edi)\n\t"
1228 #else
1229 						 "1:\t"
1230 						 "lodsb\n\t"
1231 						 "movb %%al, (%%edi)\n\t"
1232 #endif
1233 						 "loop 1b;"
1234 				      /* output */ :
1235 				      /* input */ :"D" (phys_to_virt (st0x_dr)),
1236 						 "S"
1237 						 (data),
1238 						 "c" (SCint->transfersize)
1239 /* clobbered */
1240 				      :	 "eax", "ecx",
1241 						 "esi");
1242 #else				/* SEAGATE_USE_ASM */
1243 					{
1244 #ifdef FAST32
1245 						unsigned int *iop =
1246 						    phys_to_virt (st0x_dr);
1247 						const unsigned int *dp =
1248 						    (unsigned int *) data;
1249 						int xferlen = transfersize >> 2;
1250 #else
1251 						unsigned char *iop =
1252 						    phys_to_virt (st0x_dr);
1253 						const unsigned char *dp = data;
1254 						int xferlen = transfersize;
1255 #endif
1256 						for (; xferlen; --xferlen)
1257 							*iop = *dp++;
1258 					}
1259 #endif				/* SEAGATE_USE_ASM */
1260 /* SJT: End */
1261 					len -= transfersize;
1262 					data += transfersize;
1263 					DPRINTK (DEBUG_FAST,
1264 						 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1265 						 hostno, len, data);
1266 				} else {
1267 /*
1268  *    We loop as long as we are in a data out phase, there is data to send,
1269  *      and BSY is still active.
1270  */
1271 
1272 /* SJT: Start. Slow Write. */
1273 #ifdef SEAGATE_USE_ASM
1274 
1275 					int __dummy_1, __dummy_2;
1276 
1277 /*
1278  *      We loop as long as we are in a data out phase, there is data to send,
1279  *      and BSY is still active.
1280  */
1281 /* Local variables : len = ecx , data = esi,
1282                      st0x_cr_sr = ebx, st0x_dr =  edi
1283 */
1284 					__asm__ (
1285 							/* Test for any data here at all. */
1286 							"orl %%ecx, %%ecx\n\t"
1287 							"jz 2f\n\t" "cld\n\t"
1288 /*                    "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%ebx\n\t"  */
1289 /*                    "movl " SYMBOL_NAME_STR(st0x_dr) ", %%edi\n\t"  */
1290 							"1:\t"
1291 							"movb (%%ebx), %%al\n\t"
1292 							/* Test for BSY */
1293 							"test $1, %%al\n\t"
1294 							"jz 2f\n\t"
1295 							/* Test for data out phase - STATUS & REQ_MASK should be
1296 							   REQ_DATAOUT, which is 0. */
1297 							"test $0xe, %%al\n\t"
1298 							"jnz 2f\n\t"
1299 							/* Test for REQ */
1300 							"test $0x10, %%al\n\t"
1301 							"jz 1b\n\t"
1302 							"lodsb\n\t"
1303 							"movb %%al, (%%edi)\n\t"
1304 							"loop 1b\n\t" "2:\n"
1305 				      /* output */ :"=S" (data), "=c" (len),
1306 							"=b"
1307 							(__dummy_1),
1308 							"=D" (__dummy_2)
1309 /* input */
1310 				      :		"0" (data), "1" (len),
1311 							"2" (phys_to_virt
1312 							     (st0x_cr_sr)),
1313 							"3" (phys_to_virt
1314 							     (st0x_dr))
1315 /* clobbered */
1316 				      :		"eax");
1317 #else				/* SEAGATE_USE_ASM */
1318 					while (len) {
1319 						unsigned char stat;
1320 
1321 						stat = STATUS;
1322 						if (!(stat & STAT_BSY)
1323 						    || ((stat & REQ_MASK) !=
1324 							REQ_DATAOUT))
1325 							break;
1326 						if (stat & STAT_REQ) {
1327 							WRITE_DATA (*data++);
1328 							--len;
1329 						}
1330 					}
1331 #endif				/* SEAGATE_USE_ASM */
1332 /* SJT: End. */
1333 				}
1334 
1335 				if (!len && nobuffs) {
1336 					--nobuffs;
1337 					++buffer;
1338 					len = buffer->length;
1339 					data =
1340 					    (unsigned char *) buffer->address;
1341 					DPRINTK (DEBUG_SG,
1342 						 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1343 						 hostno, len, data);
1344 				}
1345 				break;
1346 
1347 			case REQ_DATAIN:
1348 #ifdef SLOW_RATE
1349 				if (borken) {
1350 #if (DEBUG & (PHASE_DATAIN))
1351 					transfered += len;
1352 #endif
1353 					for (;
1354 					     len
1355 					     && (STATUS & (REQ_MASK | STAT_REQ))
1356 					     == (REQ_DATAIN | STAT_REQ);
1357 					     --len) {
1358 						*data++ = DATA;
1359 						borken_wait ();
1360 					}
1361 #if (DEBUG & (PHASE_DATAIN))
1362 					transfered -= len;
1363 #endif
1364 				} else
1365 #endif
1366 
1367 					if (fast && transfersize
1368 					    && !(len % transfersize)
1369 					    && (len >= transfersize)
1370 #ifdef FAST32
1371 					    && !(transfersize % 4)
1372 #endif
1373 				    ) {
1374 					DPRINTK (DEBUG_FAST,
1375 						 "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
1376 						 "         len = %d, data = %08x\n",
1377 						 hostno, SCint->underflow,
1378 						 SCint->transfersize, len,
1379 						 data);
1380 
1381 /* SJT: Start. Fast Read */
1382 #ifdef SEAGATE_USE_ASM
1383 					__asm__ ("cld\n\t"
1384 #ifdef FAST32
1385 						 "shr $2, %%ecx\n\t"
1386 						 "1:\t"
1387 						 "movl (%%esi), %%eax\n\t"
1388 						 "stosl\n\t"
1389 #else
1390 						 "1:\t"
1391 						 "movb (%%esi), %%al\n\t"
1392 						 "stosb\n\t"
1393 #endif
1394 						 "loop 1b\n\t"
1395 				      /* output */ :
1396 				      /* input */ :"S" (phys_to_virt (st0x_dr)),
1397 						 "D"
1398 						 (data),
1399 						 "c" (SCint->transfersize)
1400 /* clobbered */
1401 				      :	 "eax", "ecx",
1402 						 "edi");
1403 #else				/* SEAGATE_USE_ASM */
1404 					{
1405 #ifdef FAST32
1406 						const unsigned int *iop =
1407 						    phys_to_virt (st0x_dr);
1408 						unsigned int *dp =
1409 						    (unsigned int *) data;
1410 						int xferlen = len >> 2;
1411 #else
1412 						const unsigned char *iop =
1413 						    phys_to_virt (st0x_dr);
1414 						unsigned char *dp = data;
1415 						int xferlen = len;
1416 #endif
1417 						for (; xferlen; --xferlen)
1418 							*dp++ = *iop;
1419 					}
1420 #endif				/* SEAGATE_USE_ASM */
1421 /* SJT: End */
1422 					len -= transfersize;
1423 					data += transfersize;
1424 #if (DEBUG & PHASE_DATAIN)
1425 					printk ("scsi%d: transfered += %d\n",
1426 						hostno, transfersize);
1427 					transfered += transfersize;
1428 #endif
1429 
1430 					DPRINTK (DEBUG_FAST,
1431 						 "scsi%d : FAST transfer complete len = %d data = %08x\n",
1432 						 hostno, len, data);
1433 				} else {
1434 
1435 #if (DEBUG & PHASE_DATAIN)
1436 					printk ("scsi%d: transfered += %d\n",
1437 						hostno, len);
1438 					transfered += len;	/* Assume we'll transfer it all, then
1439 								   subtract what we *didn't* transfer */
1440 #endif
1441 
1442 /*
1443  *	We loop as long as we are in a data in phase, there is room to read,
1444  *      and BSY is still active
1445  */
1446 
1447 /* SJT: Start. */
1448 #ifdef SEAGATE_USE_ASM
1449 
1450 					int __dummy_3, __dummy_4;
1451 
1452 /* Dummy clobbering variables for the new gcc-2.95 */
1453 
1454 /*
1455  *      We loop as long as we are in a data in phase, there is room to read,
1456  *      and BSY is still active
1457  */
1458 					/* Local variables : ecx = len, edi = data
1459 					   esi = st0x_cr_sr, ebx = st0x_dr */
1460 					__asm__ (
1461 							/* Test for room to read */
1462 							"orl %%ecx, %%ecx\n\t"
1463 							"jz 2f\n\t" "cld\n\t"
1464 /*                "movl " SYMBOL_NAME_STR(st0x_cr_sr) ", %%esi\n\t"  */
1465 /*                "movl " SYMBOL_NAME_STR(st0x_dr) ", %%ebx\n\t"  */
1466 							"1:\t"
1467 							"movb (%%esi), %%al\n\t"
1468 							/* Test for BSY */
1469 							"test $1, %%al\n\t"
1470 							"jz 2f\n\t"
1471 							/* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
1472 							   = STAT_IO, which is 4. */
1473 							"movb $0xe, %%ah\n\t"
1474 							"andb %%al, %%ah\n\t"
1475 							"cmpb $0x04, %%ah\n\t"
1476 							"jne 2f\n\t"
1477 							/* Test for REQ */
1478 							"test $0x10, %%al\n\t"
1479 							"jz 1b\n\t"
1480 							"movb (%%ebx), %%al\n\t"
1481 							"stosb\n\t"
1482 							"loop 1b\n\t" "2:\n"
1483 				      /* output */ :"=D" (data), "=c" (len),
1484 							"=S"
1485 							(__dummy_3),
1486 							"=b" (__dummy_4)
1487 /* input */
1488 				      :		"0" (data), "1" (len),
1489 							"2" (phys_to_virt
1490 							     (st0x_cr_sr)),
1491 							"3" (phys_to_virt
1492 							     (st0x_dr))
1493 /* clobbered */
1494 				      :		"eax");
1495 #else				/* SEAGATE_USE_ASM */
1496 					while (len) {
1497 						unsigned char stat;
1498 
1499 						stat = STATUS;
1500 						if (!(stat & STAT_BSY)
1501 						    || ((stat & REQ_MASK) !=
1502 							REQ_DATAIN))
1503 							break;
1504 						if (stat & STAT_REQ) {
1505 							*data++ = DATA;
1506 							--len;
1507 						}
1508 					}
1509 #endif				/* SEAGATE_USE_ASM */
1510 /* SJT: End. */
1511 #if (DEBUG & PHASE_DATAIN)
1512 					printk ("scsi%d: transfered -= %d\n",
1513 						hostno, len);
1514 					transfered -= len;	/* Since we assumed all of Len got  *
1515 								   transfered, correct our mistake */
1516 #endif
1517 				}
1518 
1519 				if (!len && nobuffs) {
1520 					--nobuffs;
1521 					++buffer;
1522 					len = buffer->length;
1523 					data =
1524 					    (unsigned char *) buffer->address;
1525 					DPRINTK (DEBUG_SG,
1526 						 "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
1527 						 hostno, len, data);
1528 				}
1529 
1530 				break;
1531 
1532 			case REQ_CMDOUT:
1533 				while (((status_read = STATUS) & STAT_BSY) &&
1534 				       ((status_read & REQ_MASK) == REQ_CMDOUT))
1535 					if (status_read & STAT_REQ) {
1536 						WRITE_DATA (*
1537 							    (const unsigned char
1538 							     *) cmnd);
1539 						cmnd =
1540 						    1 +
1541 						    (const unsigned char *)
1542 						    cmnd;
1543 #ifdef SLOW_RATE
1544 						if (borken)
1545 							borken_wait ();
1546 #endif
1547 					}
1548 				break;
1549 
1550 			case REQ_STATIN:
1551 				status = DATA;
1552 				break;
1553 
1554 			case REQ_MSGOUT:
1555 /*
1556  *	We can only have sent a MSG OUT if we requested to do this
1557  *      by raising ATTN.  So, we must drop ATTN.
1558  */
1559 
1560 				WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
1561 /*
1562  *	If we are reconnecting, then we must send an IDENTIFY message in
1563  *      response  to MSGOUT.
1564  */
1565 				switch (reselect) {
1566 				case CAN_RECONNECT:
1567 					WRITE_DATA (IDENTIFY (1, lun));
1568 
1569 					DPRINTK (PHASE_RESELECT | PHASE_MSGOUT,
1570 						 "scsi%d : sent IDENTIFY message.\n",
1571 						 hostno);
1572 					break;
1573 #ifdef LINKED
1574 				case LINKED_WRONG:
1575 					WRITE_DATA (ABORT);
1576 					linked_connected = 0;
1577 					reselect = CAN_RECONNECT;
1578 					goto connect_loop;
1579 					DPRINTK (PHASE_MSGOUT | DEBUG_LINKED,
1580 						 "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n",
1581 						 hostno);
1582 #endif				/* LINKED */
1583 					DPRINTK (DEBUG_LINKED, "correct\n");
1584 				default:
1585 					WRITE_DATA (NOP);
1586 					printk
1587 					    ("scsi%d : target %d requested MSGOUT, sent NOP message.\n",
1588 					     hostno, target);
1589 				}
1590 				break;
1591 
1592 			case REQ_MSGIN:
1593 				switch (message = DATA) {
1594 				case DISCONNECT:
1595 					DANY ("seagate: deciding to disconnect\n");
1596 					should_reconnect = 1;
1597 					current_data = data;	/* WDE add */
1598 					current_buffer = buffer;
1599 					current_bufflen = len;	/* WDE add */
1600 					current_nobuffs = nobuffs;
1601 #ifdef LINKED
1602 					linked_connected = 0;
1603 #endif
1604 					done = 1;
1605 					DPRINTK ((PHASE_RESELECT | PHASE_MSGIN),
1606 						 "scsi%d : disconnected.\n",
1607 						 hostno);
1608 					break;
1609 
1610 #ifdef LINKED
1611 				case LINKED_CMD_COMPLETE:
1612 				case LINKED_FLG_CMD_COMPLETE:
1613 #endif
1614 				case COMMAND_COMPLETE:
1615 /*
1616  * Note : we should check for underflow here.
1617  */
1618 					DPRINTK (PHASE_MSGIN,
1619 						 "scsi%d : command complete.\n",
1620 						 hostno);
1621 					done = 1;
1622 					break;
1623 				case ABORT:
1624 					DPRINTK (PHASE_MSGIN,
1625 						 "scsi%d : abort message.\n",
1626 						 hostno);
1627 					done = 1;
1628 					break;
1629 				case SAVE_POINTERS:
1630 					current_buffer = buffer;
1631 					current_bufflen = len;	/* WDE add */
1632 					current_data = data;	/* WDE mod */
1633 					current_nobuffs = nobuffs;
1634 					DPRINTK (PHASE_MSGIN,
1635 						 "scsi%d : pointers saved.\n",
1636 						 hostno);
1637 					break;
1638 				case RESTORE_POINTERS:
1639 					buffer = current_buffer;
1640 					cmnd = current_cmnd;
1641 					data = current_data;	/* WDE mod */
1642 					len = current_bufflen;
1643 					nobuffs = current_nobuffs;
1644 					DPRINTK (PHASE_MSGIN,
1645 						 "scsi%d : pointers restored.\n",
1646 						 hostno);
1647 					break;
1648 				default:
1649 
1650 /*
1651  *	IDENTIFY distinguishes itself from the other messages by setting the
1652  *      high byte. [FIXME: should not this read "the high bit"? - pavel@ucw.cz]
1653  *
1654  *      Note : we need to handle at least one outstanding command per LUN,
1655  *      and need to hash the SCSI command for that I_T_L nexus based on the
1656  *      known ID (at this point) and LUN.
1657  */
1658 
1659 					if (message & 0x80) {
1660 						DPRINTK (PHASE_MSGIN,
1661 							 "scsi%d : IDENTIFY message received from id %d, lun %d.\n",
1662 							 hostno, target,
1663 							 message & 7);
1664 					} else {
1665 
1666 /*
1667  *      We should go into a MESSAGE OUT phase, and send  a MESSAGE_REJECT
1668  *      if we run into a message that we don't like.  The seagate driver
1669  *      needs some serious restructuring first though.
1670  */
1671 
1672 						DPRINTK (PHASE_MSGIN,
1673 							 "scsi%d : unknown message %d from target %d.\n",
1674 							 hostno, message,
1675 							 target);
1676 					}
1677 				}
1678 				break;
1679 
1680 			default:
1681 				printk ("scsi%d : unknown phase.\n", hostno);
1682 				st0x_aborted = DID_ERROR;
1683 			}	/* end of switch (status_read &
1684 				   REQ_MASK) */
1685 
1686 #ifdef SLOW_RATE
1687 /*
1688  * I really don't care to deal with borken devices in each single
1689  * byte transfer case (ie, message in, message out, status), so
1690  * I'll do the wait here if necessary.
1691  */
1692 			if (borken)
1693 				borken_wait ();
1694 #endif
1695 
1696 		}		/* if(status_read & STAT_REQ) ends */
1697 	}			/* while(((status_read = STATUS)...)
1698 				   ends */
1699 
1700 	DPRINTK (PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT,
1701 		 "scsi%d : Transfered %d bytes\n", hostno, transfered);
1702 
1703 #if (DEBUG & PHASE_EXIT)
1704 #if 0				/* Doesn't work for scatter/gather */
1705 	printk ("Buffer : \n");
1706 	for (i = 0; i < 20; ++i)
1707 		printk ("%02x  ", ((unsigned char *) data)[i]);	/* WDE mod */
1708 	printk ("\n");
1709 #endif
1710 	printk ("scsi%d : status = ", hostno);
1711 	print_status (status);
1712 	printk ("message = %02x\n", message);
1713 #endif
1714 
1715 /* We shouldn't reach this until *after* BSY has been deasserted */
1716 
1717 #ifdef LINKED
1718 	else
1719 	{
1720 /*
1721  * Fix the message byte so that unsuspecting high level drivers don't
1722  * puke when they see a LINKED COMMAND message in place of the COMMAND
1723  * COMPLETE they may be expecting.  Shouldn't be necessary, but it's
1724  * better to be on the safe side.
1725  *
1726  * A non LINKED* message byte will indicate that the command completed,
1727  * and we are now disconnected.
1728  */
1729 
1730 		switch (message) {
1731 		case LINKED_CMD_COMPLETE:
1732 		case LINKED_FLG_CMD_COMPLETE:
1733 			message = COMMAND_COMPLETE;
1734 			linked_target = current_target;
1735 			linked_lun = current_lun;
1736 			linked_connected = 1;
1737 			DPRINTK (DEBUG_LINKED,
1738 				 "scsi%d : keeping I_T_L nexus established"
1739 				 "for linked command.\n", hostno);
1740 			/* We also will need to adjust status to accommodate intermediate
1741 			   conditions. */
1742 			if ((status == INTERMEDIATE_GOOD) ||
1743 			    (status == INTERMEDIATE_C_GOOD))
1744 				status = GOOD;
1745 
1746 			break;
1747 /*
1748  * We should also handle what are "normal" termination messages
1749  * here (ABORT, BUS_DEVICE_RESET?, and COMMAND_COMPLETE individually,
1750  * and flake if things aren't right.
1751  */
1752 		default:
1753 			DPRINTK (DEBUG_LINKED,
1754 				 "scsi%d : closing I_T_L nexus.\n", hostno);
1755 			linked_connected = 0;
1756 		}
1757 	}
1758 #endif				/* LINKED */
1759 
1760 	if (should_reconnect) {
1761 		DPRINTK (PHASE_RESELECT,
1762 			 "scsi%d : exiting seagate_st0x_queue_command()"
1763 			 "with reconnect enabled.\n", hostno);
1764 		WRITE_CONTROL (BASE_CMD | CMD_INTR);
1765 	} else
1766 		WRITE_CONTROL (BASE_CMD);
1767 
1768 	return retcode (st0x_aborted);
1769 }				/* end of internal_command */
1770 
seagate_st0x_abort(Scsi_Cmnd * SCpnt)1771 static int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
1772 {
1773 	st0x_aborted = DID_ABORT;
1774 	return SCSI_ABORT_PENDING;
1775 }
1776 
1777 #undef ULOOP
1778 #undef TIMEOUT
1779 
1780 /*
1781  * the seagate_st0x_reset function resets the SCSI bus
1782  */
1783 
seagate_st0x_reset(Scsi_Cmnd * SCpnt,unsigned int reset_flags)1784 static int seagate_st0x_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
1785 {
1786 /* No timeouts - this command is going to fail because it was reset. */
1787 	DANY ("scsi%d: Reseting bus... ", hostno);
1788 
1789 /* assert  RESET signal on SCSI bus.  */
1790 	WRITE_CONTROL (BASE_CMD | CMD_RST);
1791 
1792 	udelay (20 * 1000);
1793 
1794 	WRITE_CONTROL (BASE_CMD);
1795 	st0x_aborted = DID_RESET;
1796 
1797 	DANY ("done.\n");
1798 	return SCSI_RESET_WAKEUP;
1799 }
1800 
1801 /* Eventually this will go into an include file, but this will be later */
1802 static Scsi_Host_Template driver_template = SEAGATE_ST0X;
1803 
1804 #include "scsi_module.c"
1805