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