xref: /openbmc/linux/drivers/scsi/initio.c (revision e1181b5b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**************************************************************************
3  * Initio 9100 device driver for Linux.
4  *
5  * Copyright (c) 1994-1998 Initio Corporation
6  * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
7  * Copyright (c) 2004 Christoph Hellwig <hch@lst.de>
8  * Copyright (c) 2007 Red Hat
9  *
10  *************************************************************************
11  *
12  * DESCRIPTION:
13  *
14  * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
15  * adapters
16  *
17  * 08/06/97 hc	- v1.01h
18  *		- Support inic-940 and inic-935
19  * 09/26/97 hc	- v1.01i
20  *		- Make correction from J.W. Schultz suggestion
21  * 10/13/97 hc	- Support reset function
22  * 10/21/97 hc	- v1.01j
23  *		- Support 32 LUN (SCSI 3)
24  * 01/14/98 hc	- v1.01k
25  *		- Fix memory allocation problem
26  * 03/04/98 hc	- v1.01l
27  *		- Fix tape rewind which will hang the system problem
28  *		- Set can_queue to initio_num_scb
29  * 06/25/98 hc	- v1.01m
30  *		- Get it work for kernel version >= 2.1.75
31  *		- Dynamic assign SCSI bus reset holding time in initio_init()
32  * 07/02/98 hc	- v1.01n
33  *		- Support 0002134A
34  * 08/07/98 hc  - v1.01o
35  *		- Change the initio_abort_srb routine to use scsi_done. <01>
36  * 09/07/98 hl  - v1.02
37  *              - Change the INI9100U define and proc_dir_entry to
38  *                reflect the newer Kernel 2.1.118, but the v1.o1o
39  *                should work with Kernel 2.1.118.
40  * 09/20/98 wh  - v1.02a
41  *              - Support Abort command.
42  *              - Handle reset routine.
43  * 09/21/98 hl  - v1.03
44  *              - remove comments.
45  * 12/09/98 bv	- v1.03a
46  *		- Removed unused code
47  * 12/13/98 bv	- v1.03b
48  *		- Remove cli() locking for kernels >= 2.1.95. This uses
49  *		  spinlocks to serialize access to the pSRB_head and
50  *		  pSRB_tail members of the HCS structure.
51  * 09/01/99 bv	- v1.03d
52  *		- Fixed a deadlock problem in SMP.
53  * 21/01/99 bv	- v1.03e
54  *		- Add support for the Domex 3192U PCI SCSI
55  *		  This is a slightly modified patch by
56  *		  Brian Macy <bmacy@sunshinecomputing.com>
57  * 22/02/99 bv	- v1.03f
58  *		- Didn't detect the INIC-950 in 2.0.x correctly.
59  *		  Now fixed.
60  * 05/07/99 bv	- v1.03g
61  *		- Changed the assumption that HZ = 100
62  * 10/17/03 mc	- v1.04
63  *		- added new DMA API support
64  * 06/01/04 jmd	- v1.04a
65  *		- Re-add reset_bus support
66  **************************************************************************/
67 
68 #include <linux/module.h>
69 #include <linux/errno.h>
70 #include <linux/delay.h>
71 #include <linux/pci.h>
72 #include <linux/init.h>
73 #include <linux/blkdev.h>
74 #include <linux/spinlock.h>
75 #include <linux/stat.h>
76 #include <linux/kernel.h>
77 #include <linux/proc_fs.h>
78 #include <linux/string.h>
79 #include <linux/interrupt.h>
80 #include <linux/ioport.h>
81 #include <linux/slab.h>
82 #include <linux/jiffies.h>
83 #include <linux/dma-mapping.h>
84 #include <asm/io.h>
85 
86 #include <scsi/scsi.h>
87 #include <scsi/scsi_cmnd.h>
88 #include <scsi/scsi_device.h>
89 #include <scsi/scsi_host.h>
90 #include <scsi/scsi_tcq.h>
91 
92 #include "initio.h"
93 
94 #define SENSE_SIZE		14
95 
96 #define i91u_MAXQUEUE		2
97 #define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
98 
99 #ifdef DEBUG_i91u
100 static unsigned int i91u_debug = DEBUG_DEFAULT;
101 #endif
102 
103 static int initio_tag_enable = 1;
104 
105 #ifdef DEBUG_i91u
106 static int setup_debug = 0;
107 #endif
108 
109 static void i91uSCBPost(u8 * pHcb, u8 * pScb);
110 
111 #define DEBUG_INTERRUPT 0
112 #define DEBUG_QUEUE     0
113 #define DEBUG_STATE     0
114 #define INT_DISC	0
115 
116 /*--- forward references ---*/
117 static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
118 static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
119 
120 static int tulip_main(struct initio_host * host);
121 
122 static int initio_next_state(struct initio_host * host);
123 static int initio_state_1(struct initio_host * host);
124 static int initio_state_2(struct initio_host * host);
125 static int initio_state_3(struct initio_host * host);
126 static int initio_state_4(struct initio_host * host);
127 static int initio_state_5(struct initio_host * host);
128 static int initio_state_6(struct initio_host * host);
129 static int initio_state_7(struct initio_host * host);
130 static int initio_xfer_data_in(struct initio_host * host);
131 static int initio_xfer_data_out(struct initio_host * host);
132 static int initio_xpad_in(struct initio_host * host);
133 static int initio_xpad_out(struct initio_host * host);
134 static int initio_status_msg(struct initio_host * host);
135 
136 static int initio_msgin(struct initio_host * host);
137 static int initio_msgin_sync(struct initio_host * host);
138 static int initio_msgin_accept(struct initio_host * host);
139 static int initio_msgout_reject(struct initio_host * host);
140 static int initio_msgin_extend(struct initio_host * host);
141 
142 static int initio_msgout_ide(struct initio_host * host);
143 static int initio_msgout_abort_targ(struct initio_host * host);
144 static int initio_msgout_abort_tag(struct initio_host * host);
145 
146 static int initio_bus_device_reset(struct initio_host * host);
147 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
148 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
149 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
150 static int int_initio_busfree(struct initio_host * host);
151 static int int_initio_scsi_rst(struct initio_host * host);
152 static int int_initio_bad_seq(struct initio_host * host);
153 static int int_initio_resel(struct initio_host * host);
154 static int initio_sync_done(struct initio_host * host);
155 static int wdtr_done(struct initio_host * host);
156 static int wait_tulip(struct initio_host * host);
157 static int initio_wait_done_disc(struct initio_host * host);
158 static int initio_wait_disc(struct initio_host * host);
159 static void tulip_scsi(struct initio_host * host);
160 static int initio_post_scsi_rst(struct initio_host * host);
161 
162 static void initio_se2_ew_en(unsigned long base);
163 static void initio_se2_ew_ds(unsigned long base);
164 static int initio_se2_rd_all(unsigned long base);
165 static void initio_se2_update_all(unsigned long base);	/* setup default pattern */
166 static void initio_read_eeprom(unsigned long base);
167 
168 /* ---- INTERNAL VARIABLES ---- */
169 
170 static NVRAM i91unvram;
171 static NVRAM *i91unvramp;
172 
173 static u8 i91udftNvRam[64] =
174 {
175 	/*----------- header -----------*/
176 	0x25, 0xc9,		/* Signature    */
177 	0x40,			/* Size         */
178 	0x01,			/* Revision     */
179 	/* -- Host Adapter Structure -- */
180 	0x95,			/* ModelByte0   */
181 	0x00,			/* ModelByte1   */
182 	0x00,			/* ModelInfo    */
183 	0x01,			/* NumOfCh      */
184 	NBC1_DEFAULT,		/* BIOSConfig1  */
185 	0,			/* BIOSConfig2  */
186 	0,			/* HAConfig1    */
187 	0,			/* HAConfig2    */
188 	/* SCSI channel 0 and target Structure  */
189 	7,			/* SCSIid       */
190 	NCC1_DEFAULT,		/* SCSIconfig1  */
191 	0,			/* SCSIconfig2  */
192 	0x10,			/* NumSCSItarget */
193 
194 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
195 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
196 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
197 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
198 
199 	/* SCSI channel 1 and target Structure  */
200 	7,			/* SCSIid       */
201 	NCC1_DEFAULT,		/* SCSIconfig1  */
202 	0,			/* SCSIconfig2  */
203 	0x10,			/* NumSCSItarget */
204 
205 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
206 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
207 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
208 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
209 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 	0, 0};			/*      - CheckSum -            */
211 
212 
213 static u8 initio_rate_tbl[8] =	/* fast 20      */
214 {
215 				/* nanosecond divide by 4 */
216 	12,			/* 50ns,  20M   */
217 	18,			/* 75ns,  13.3M */
218 	25,			/* 100ns, 10M   */
219 	31,			/* 125ns, 8M    */
220 	37,			/* 150ns, 6.6M  */
221 	43,			/* 175ns, 5.7M  */
222 	50,			/* 200ns, 5M    */
223 	62			/* 250ns, 4M    */
224 };
225 
226 static void initio_do_pause(unsigned amount)
227 {
228 	/* Pause for amount jiffies */
229 	unsigned long the_time = jiffies + amount;
230 
231 	while (time_before_eq(jiffies, the_time))
232 		cpu_relax();
233 }
234 
235 /*-- forward reference --*/
236 
237 /******************************************************************
238  Input: instruction for  Serial E2PROM
239 
240  EX: se2_rd(0 call se2_instr() to send address and read command
241 
242 	 StartBit  OP_Code   Address                Data
243 	 --------- --------  ------------------     -------
244 	 1         1 , 0     A5,A4,A3,A2,A1,A0      D15-D0
245 
246 		 +-----------------------------------------------------
247 		 |
248  CS -----+
249 			+--+  +--+  +--+  +--+  +--+
250 			^  |  ^  |  ^  |  ^  |  ^  |
251 			|  |  |  |  |  |  |  |  |  |
252  CLK -------+  +--+  +--+  +--+  +--+  +--
253  (leading edge trigger)
254 
255 		 +--1-----1--+
256 		 | SB    OP  |  OP    A5    A4
257  DI  ----+           +--0------------------
258  (address and cmd sent to nvram)
259 
260 	 -------------------------------------------+
261 												|
262  DO                                             +---
263  (data sent from nvram)
264 
265 
266 ******************************************************************/
267 
268 /**
269  *	initio_se2_instr	-	bitbang an instruction
270  *	@base: Base of InitIO controller
271  *	@instr: Instruction for serial E2PROM
272  *
273  *	Bitbang an instruction out to the serial E2Prom
274  */
275 
276 static void initio_se2_instr(unsigned long base, u8 instr)
277 {
278 	int i;
279 	u8 b;
280 
281 	outb(SE2CS | SE2DO, base + TUL_NVRAM);		/* cs+start bit */
282 	udelay(30);
283 	outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM);	/* +CLK */
284 	udelay(30);
285 
286 	for (i = 0; i < 8; i++) {
287 		if (instr & 0x80)
288 			b = SE2CS | SE2DO;		/* -CLK+dataBit */
289 		else
290 			b = SE2CS;			/* -CLK */
291 		outb(b, base + TUL_NVRAM);
292 		udelay(30);
293 		outb(b | SE2CLK, base + TUL_NVRAM);	/* +CLK */
294 		udelay(30);
295 		instr <<= 1;
296 	}
297 	outb(SE2CS, base + TUL_NVRAM);			/* -CLK */
298 	udelay(30);
299 }
300 
301 
302 /**
303  *	initio_se2_ew_en	-	Enable erase/write
304  *	@base: Base address of InitIO controller
305  *
306  *	Enable erase/write state of serial EEPROM
307  */
308 void initio_se2_ew_en(unsigned long base)
309 {
310 	initio_se2_instr(base, 0x30);	/* EWEN */
311 	outb(0, base + TUL_NVRAM);	/* -CS  */
312 	udelay(30);
313 }
314 
315 
316 /**
317  *	initio_se2_ew_ds	-	Disable erase/write
318  *	@base: Base address of InitIO controller
319  *
320  *	Disable erase/write state of serial EEPROM
321  */
322 void initio_se2_ew_ds(unsigned long base)
323 {
324 	initio_se2_instr(base, 0);	/* EWDS */
325 	outb(0, base + TUL_NVRAM);	/* -CS  */
326 	udelay(30);
327 }
328 
329 
330 /**
331  *	initio_se2_rd		-	read E2PROM word
332  *	@base: Base of InitIO controller
333  *	@addr: Address of word in E2PROM
334  *
335  *	Read a word from the NV E2PROM device
336  */
337 static u16 initio_se2_rd(unsigned long base, u8 addr)
338 {
339 	u8 instr, rb;
340 	u16 val = 0;
341 	int i;
342 
343 	instr = (u8) (addr | 0x80);
344 	initio_se2_instr(base, instr);	/* READ INSTR */
345 
346 	for (i = 15; i >= 0; i--) {
347 		outb(SE2CS | SE2CLK, base + TUL_NVRAM);	/* +CLK */
348 		udelay(30);
349 		outb(SE2CS, base + TUL_NVRAM);		/* -CLK */
350 
351 		/* sample data after the following edge of clock  */
352 		rb = inb(base + TUL_NVRAM);
353 		rb &= SE2DI;
354 		val += (rb << i);
355 		udelay(30);	/* 6/20/95 */
356 	}
357 
358 	outb(0, base + TUL_NVRAM);		/* no chip select */
359 	udelay(30);
360 	return val;
361 }
362 
363 /**
364  *	initio_se2_wr		-	read E2PROM word
365  *	@base: Base of InitIO controller
366  *	@addr: Address of word in E2PROM
367  *	@val: Value to write
368  *
369  *	Write a word to the NV E2PROM device. Used when recovering from
370  *	a problem with the NV.
371  */
372 static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
373 {
374 	u8 rb;
375 	u8 instr;
376 	int i;
377 
378 	instr = (u8) (addr | 0x40);
379 	initio_se2_instr(base, instr);	/* WRITE INSTR */
380 	for (i = 15; i >= 0; i--) {
381 		if (val & 0x8000)
382 			outb(SE2CS | SE2DO, base + TUL_NVRAM);	/* -CLK+dataBit 1 */
383 		else
384 			outb(SE2CS, base + TUL_NVRAM);		/* -CLK+dataBit 0 */
385 		udelay(30);
386 		outb(SE2CS | SE2CLK, base + TUL_NVRAM);		/* +CLK */
387 		udelay(30);
388 		val <<= 1;
389 	}
390 	outb(SE2CS, base + TUL_NVRAM);				/* -CLK */
391 	udelay(30);
392 	outb(0, base + TUL_NVRAM);				/* -CS  */
393 	udelay(30);
394 
395 	outb(SE2CS, base + TUL_NVRAM);				/* +CS  */
396 	udelay(30);
397 
398 	for (;;) {
399 		outb(SE2CS | SE2CLK, base + TUL_NVRAM);		/* +CLK */
400 		udelay(30);
401 		outb(SE2CS, base + TUL_NVRAM);			/* -CLK */
402 		udelay(30);
403 		if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
404 			break;	/* write complete */
405 	}
406 	outb(0, base + TUL_NVRAM);				/* -CS */
407 }
408 
409 /**
410  *	initio_se2_rd_all	-	read hostadapter NV configuration
411  *	@base: Base address of InitIO controller
412  *
413  *	Reads the E2PROM data into main memory. Ensures that the checksum
414  *	and header marker are valid. Returns 1 on success -1 on error.
415  */
416 
417 static int initio_se2_rd_all(unsigned long base)
418 {
419 	int i;
420 	u16 chksum = 0;
421 	u16 *np;
422 
423 	i91unvramp = &i91unvram;
424 	np = (u16 *) i91unvramp;
425 	for (i = 0; i < 32; i++)
426 		*np++ = initio_se2_rd(base, i);
427 
428 	/* Is signature "ini" ok ? */
429 	if (i91unvramp->NVM_Signature != INI_SIGNATURE)
430 		return -1;
431 	/* Is ckecksum ok ? */
432 	np = (u16 *) i91unvramp;
433 	for (i = 0; i < 31; i++)
434 		chksum += *np++;
435 	if (i91unvramp->NVM_CheckSum != chksum)
436 		return -1;
437 	return 1;
438 }
439 
440 /**
441  *	initio_se2_update_all		-	Update E2PROM
442  *	@base: Base of InitIO controller
443  *
444  *	Update the E2PROM by wrting any changes into the E2PROM
445  *	chip, rewriting the checksum.
446  */
447 static void initio_se2_update_all(unsigned long base)
448 {				/* setup default pattern */
449 	int i;
450 	u16 chksum = 0;
451 	u16 *np, *np1;
452 
453 	i91unvramp = &i91unvram;
454 	/* Calculate checksum first */
455 	np = (u16 *) i91udftNvRam;
456 	for (i = 0; i < 31; i++)
457 		chksum += *np++;
458 	*np = chksum;
459 	initio_se2_ew_en(base);	/* Enable write  */
460 
461 	np = (u16 *) i91udftNvRam;
462 	np1 = (u16 *) i91unvramp;
463 	for (i = 0; i < 32; i++, np++, np1++) {
464 		if (*np != *np1)
465 			initio_se2_wr(base, i, *np);
466 	}
467 	initio_se2_ew_ds(base);	/* Disable write   */
468 }
469 
470 /**
471  *	initio_read_eeprom		-	Retrieve configuration
472  *	@base: Base of InitIO Host Adapter
473  *
474  *	Retrieve the host adapter configuration data from E2Prom. If the
475  *	data is invalid then the defaults are used and are also restored
476  *	into the E2PROM. This forms the access point for the SCSI driver
477  *	into the E2PROM layer, the other functions for the E2PROM are all
478  *	internal use.
479  *
480  *	Must be called single threaded, uses a shared global area.
481  */
482 
483 static void initio_read_eeprom(unsigned long base)
484 {
485 	u8 gctrl;
486 
487 	i91unvramp = &i91unvram;
488 	/* Enable EEProm programming */
489 	gctrl = inb(base + TUL_GCTRL);
490 	outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
491 	if (initio_se2_rd_all(base) != 1) {
492 		initio_se2_update_all(base);	/* setup default pattern */
493 		initio_se2_rd_all(base);	/* load again  */
494 	}
495 	/* Disable EEProm programming */
496 	gctrl = inb(base + TUL_GCTRL);
497 	outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
498 }
499 
500 /**
501  *	initio_stop_bm		-	stop bus master
502  *	@host: InitIO we are stopping
503  *
504  *	Stop any pending DMA operation, aborting the DMA if necessary
505  */
506 
507 static void initio_stop_bm(struct initio_host * host)
508 {
509 
510 	if (inb(host->addr + TUL_XStatus) & XPEND) {	/* if DMA xfer is pending, abort DMA xfer */
511 		outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
512 		/* wait Abort DMA xfer done */
513 		while ((inb(host->addr + TUL_Int) & XABT) == 0)
514 			cpu_relax();
515 	}
516 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
517 }
518 
519 /**
520  *	initio_reset_scsi		-	Reset SCSI host controller
521  *	@host: InitIO host to reset
522  *	@seconds: Recovery time
523  *
524  *	Perform a full reset of the SCSI subsystem.
525  */
526 
527 static int initio_reset_scsi(struct initio_host * host, int seconds)
528 {
529 	outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
530 
531 	while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
532 		cpu_relax();
533 
534 	/* reset tulip chip */
535 	outb(0, host->addr + TUL_SSignal);
536 
537 	/* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
538 	/* SONY 5200 tape drive won't work if only stall for 1 sec */
539 	/* FIXME: this is a very long busy wait right now */
540 	initio_do_pause(seconds * HZ);
541 
542 	inb(host->addr + TUL_SInt);
543 	return SCSI_RESET_SUCCESS;
544 }
545 
546 /**
547  *	initio_init		-	set up an InitIO host adapter
548  *	@host: InitIO host adapter
549  *	@num_scbs: Number of SCBS
550  *	@bios_addr: BIOS address
551  *
552  *	Set up the host adapter and devices according to the configuration
553  *	retrieved from the E2PROM.
554  *
555  *	Locking: Calls E2PROM layer code which is not re-enterable so must
556  *	run single threaded for now.
557  */
558 
559 static void initio_init(struct initio_host * host, u8 *bios_addr)
560 {
561 	int i;
562 	u8 *flags;
563 	u8 *heads;
564 
565 	/* Get E2Prom configuration */
566 	initio_read_eeprom(host->addr);
567 	if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
568 		host->max_tar = 8;
569 	else
570 		host->max_tar = 16;
571 
572 	host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
573 
574 	host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
575 	host->idmask = ~(1 << host->scsi_id);
576 
577 #ifdef CHK_PARITY
578 	/* Enable parity error response */
579 	outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
580 #endif
581 
582 	/* Mask all the interrupt       */
583 	outb(0x1F, host->addr + TUL_Mask);
584 
585 	initio_stop_bm(host);
586 	/* --- Initialize the tulip --- */
587 	outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
588 
589 	/* program HBA's SCSI ID        */
590 	outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
591 
592 	/* Enable Initiator Mode ,phase latch,alternate sync period mode,
593 	   disable SCSI reset */
594 	if (host->config & HCC_EN_PAR)
595 		host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
596 	else
597 		host->sconf1 = (TSC_INITDEFAULT);
598 	outb(host->sconf1, host->addr + TUL_SConfig);
599 
600 	/* Enable HW reselect */
601 	outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
602 
603 	outb(0, host->addr + TUL_SPeriod);
604 
605 	/* selection time out = 250 ms */
606 	outb(153, host->addr + TUL_STimeOut);
607 
608 	/* Enable SCSI terminator */
609 	outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
610 		host->addr + TUL_XCtrl);
611 	outb(((host->config & HCC_AUTO_TERM) >> 4) |
612 		(inb(host->addr + TUL_GCTRL1) & 0xFE),
613 		host->addr + TUL_GCTRL1);
614 
615 	for (i = 0,
616 	     flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
617 	     heads = bios_addr + 0x180;
618 	     i < host->max_tar;
619 	     i++, flags++) {
620 		host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
621 		if (host->targets[i].flags & TCF_EN_255)
622 			host->targets[i].drv_flags = TCF_DRV_255_63;
623 		else
624 			host->targets[i].drv_flags = 0;
625 		host->targets[i].js_period = 0;
626 		host->targets[i].sconfig0 = host->sconf1;
627 		host->targets[i].heads = *heads++;
628 		if (host->targets[i].heads == 255)
629 			host->targets[i].drv_flags = TCF_DRV_255_63;
630 		else
631 			host->targets[i].drv_flags = 0;
632 		host->targets[i].sectors = *heads++;
633 		host->targets[i].flags &= ~TCF_BUSY;
634 		host->act_tags[i] = 0;
635 		host->max_tags[i] = 0xFF;
636 	}			/* for                          */
637 	printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
638 	       host->addr, host->pci_dev->irq,
639 	       host->bios_addr, host->scsi_id);
640 	/* Reset SCSI Bus */
641 	if (host->config & HCC_SCSI_RESET) {
642 		printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
643 		initio_reset_scsi(host, 10);
644 	}
645 	outb(0x17, host->addr + TUL_SCFG1);
646 	outb(0xE9, host->addr + TUL_SIntEnable);
647 }
648 
649 /**
650  *	initio_alloc_scb		-	Allocate an SCB
651  *	@host: InitIO host we are allocating for
652  *
653  *	Walk the SCB list for the controller and allocate a free SCB if
654  *	one exists.
655  */
656 static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
657 {
658 	struct scsi_ctrl_blk *scb;
659 	unsigned long flags;
660 
661 	spin_lock_irqsave(&host->avail_lock, flags);
662 	if ((scb = host->first_avail) != NULL) {
663 #if DEBUG_QUEUE
664 		printk("find scb at %p\n", scb);
665 #endif
666 		if ((host->first_avail = scb->next) == NULL)
667 			host->last_avail = NULL;
668 		scb->next = NULL;
669 		scb->status = SCB_RENT;
670 	}
671 	spin_unlock_irqrestore(&host->avail_lock, flags);
672 	return scb;
673 }
674 
675 /**
676  *	initio_release_scb		-	Release an SCB
677  *	@host: InitIO host that owns the SCB
678  *	@cmnd: SCB command block being returned
679  *
680  *	Return an allocated SCB to the host free list
681  */
682 
683 static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
684 {
685 	unsigned long flags;
686 
687 #if DEBUG_QUEUE
688 	printk("Release SCB %p; ", cmnd);
689 #endif
690 	spin_lock_irqsave(&(host->avail_lock), flags);
691 	cmnd->srb = NULL;
692 	cmnd->status = 0;
693 	cmnd->next = NULL;
694 	if (host->last_avail != NULL) {
695 		host->last_avail->next = cmnd;
696 		host->last_avail = cmnd;
697 	} else {
698 		host->first_avail = cmnd;
699 		host->last_avail = cmnd;
700 	}
701 	spin_unlock_irqrestore(&(host->avail_lock), flags);
702 }
703 
704 /***************************************************************************/
705 static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
706 {
707 
708 #if DEBUG_QUEUE
709 	printk("Append pend SCB %p; ", scbp);
710 #endif
711 	scbp->status = SCB_PEND;
712 	scbp->next = NULL;
713 	if (host->last_pending != NULL) {
714 		host->last_pending->next = scbp;
715 		host->last_pending = scbp;
716 	} else {
717 		host->first_pending = scbp;
718 		host->last_pending = scbp;
719 	}
720 }
721 
722 /***************************************************************************/
723 static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
724 {
725 
726 #if DEBUG_QUEUE
727 	printk("Push pend SCB %p; ", scbp);
728 #endif
729 	scbp->status = SCB_PEND;
730 	if ((scbp->next = host->first_pending) != NULL) {
731 		host->first_pending = scbp;
732 	} else {
733 		host->first_pending = scbp;
734 		host->last_pending = scbp;
735 	}
736 }
737 
738 static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
739 {
740 	struct scsi_ctrl_blk *first;
741 
742 
743 	first = host->first_pending;
744 	while (first != NULL) {
745 		if (first->opcode != ExecSCSI)
746 			return first;
747 		if (first->tagmsg == 0) {
748 			if ((host->act_tags[first->target] == 0) &&
749 			    !(host->targets[first->target].flags & TCF_BUSY))
750 				return first;
751 		} else {
752 			if ((host->act_tags[first->target] >=
753 			  host->max_tags[first->target]) |
754 			    (host->targets[first->target].flags & TCF_BUSY)) {
755 				first = first->next;
756 				continue;
757 			}
758 			return first;
759 		}
760 		first = first->next;
761 	}
762 	return first;
763 }
764 
765 static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
766 {
767 	struct scsi_ctrl_blk *tmp, *prev;
768 
769 #if DEBUG_QUEUE
770 	printk("unlink pend SCB %p; ", scb);
771 #endif
772 
773 	prev = tmp = host->first_pending;
774 	while (tmp != NULL) {
775 		if (scb == tmp) {	/* Unlink this SCB              */
776 			if (tmp == host->first_pending) {
777 				if ((host->first_pending = tmp->next) == NULL)
778 					host->last_pending = NULL;
779 			} else {
780 				prev->next = tmp->next;
781 				if (tmp == host->last_pending)
782 					host->last_pending = prev;
783 			}
784 			tmp->next = NULL;
785 			break;
786 		}
787 		prev = tmp;
788 		tmp = tmp->next;
789 	}
790 }
791 
792 static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
793 {
794 
795 #if DEBUG_QUEUE
796 	printk("append busy SCB %p; ", scbp);
797 #endif
798 	if (scbp->tagmsg)
799 		host->act_tags[scbp->target]++;
800 	else
801 		host->targets[scbp->target].flags |= TCF_BUSY;
802 	scbp->status = SCB_BUSY;
803 	scbp->next = NULL;
804 	if (host->last_busy != NULL) {
805 		host->last_busy->next = scbp;
806 		host->last_busy = scbp;
807 	} else {
808 		host->first_busy = scbp;
809 		host->last_busy = scbp;
810 	}
811 }
812 
813 /***************************************************************************/
814 static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
815 {
816 	struct scsi_ctrl_blk *tmp;
817 
818 
819 	if ((tmp = host->first_busy) != NULL) {
820 		if ((host->first_busy = tmp->next) == NULL)
821 			host->last_busy = NULL;
822 		tmp->next = NULL;
823 		if (tmp->tagmsg)
824 			host->act_tags[tmp->target]--;
825 		else
826 			host->targets[tmp->target].flags &= ~TCF_BUSY;
827 	}
828 #if DEBUG_QUEUE
829 	printk("Pop busy SCB %p; ", tmp);
830 #endif
831 	return tmp;
832 }
833 
834 /***************************************************************************/
835 static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
836 {
837 	struct scsi_ctrl_blk *tmp, *prev;
838 
839 #if DEBUG_QUEUE
840 	printk("unlink busy SCB %p; ", scb);
841 #endif
842 
843 	prev = tmp = host->first_busy;
844 	while (tmp != NULL) {
845 		if (scb == tmp) {	/* Unlink this SCB              */
846 			if (tmp == host->first_busy) {
847 				if ((host->first_busy = tmp->next) == NULL)
848 					host->last_busy = NULL;
849 			} else {
850 				prev->next = tmp->next;
851 				if (tmp == host->last_busy)
852 					host->last_busy = prev;
853 			}
854 			tmp->next = NULL;
855 			if (tmp->tagmsg)
856 				host->act_tags[tmp->target]--;
857 			else
858 				host->targets[tmp->target].flags &= ~TCF_BUSY;
859 			break;
860 		}
861 		prev = tmp;
862 		tmp = tmp->next;
863 	}
864 	return;
865 }
866 
867 struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
868 {
869 	struct scsi_ctrl_blk *tmp, *prev;
870 	u16 scbp_tarlun;
871 
872 
873 	prev = tmp = host->first_busy;
874 	while (tmp != NULL) {
875 		scbp_tarlun = (tmp->lun << 8) | (tmp->target);
876 		if (scbp_tarlun == tarlun) {	/* Unlink this SCB              */
877 			break;
878 		}
879 		prev = tmp;
880 		tmp = tmp->next;
881 	}
882 #if DEBUG_QUEUE
883 	printk("find busy SCB %p; ", tmp);
884 #endif
885 	return tmp;
886 }
887 
888 static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
889 {
890 #if DEBUG_QUEUE
891 	printk("append done SCB %p; ", scbp);
892 #endif
893 
894 	scbp->status = SCB_DONE;
895 	scbp->next = NULL;
896 	if (host->last_done != NULL) {
897 		host->last_done->next = scbp;
898 		host->last_done = scbp;
899 	} else {
900 		host->first_done = scbp;
901 		host->last_done = scbp;
902 	}
903 }
904 
905 struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
906 {
907 	struct scsi_ctrl_blk *tmp;
908 
909 	if ((tmp = host->first_done) != NULL) {
910 		if ((host->first_done = tmp->next) == NULL)
911 			host->last_done = NULL;
912 		tmp->next = NULL;
913 	}
914 #if DEBUG_QUEUE
915 	printk("find done SCB %p; ",tmp);
916 #endif
917 	return tmp;
918 }
919 
920 static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
921 {
922 	unsigned long flags;
923 	struct scsi_ctrl_blk *tmp, *prev;
924 
925 	spin_lock_irqsave(&host->semaph_lock, flags);
926 
927 	if ((host->semaph == 0) && (host->active == NULL)) {
928 		/* disable Jasmin SCSI Int        */
929 		outb(0x1F, host->addr + TUL_Mask);
930 		spin_unlock_irqrestore(&host->semaph_lock, flags);
931 		/* FIXME: synchronize_irq needed ? */
932 		tulip_main(host);
933 		spin_lock_irqsave(&host->semaph_lock, flags);
934 		host->semaph = 1;
935 		outb(0x0F, host->addr + TUL_Mask);
936 		spin_unlock_irqrestore(&host->semaph_lock, flags);
937 		return SCSI_ABORT_SNOOZE;
938 	}
939 	prev = tmp = host->first_pending;	/* Check Pend queue */
940 	while (tmp != NULL) {
941 		/* 07/27/98 */
942 		if (tmp->srb == srbp) {
943 			if (tmp == host->active) {
944 				spin_unlock_irqrestore(&host->semaph_lock, flags);
945 				return SCSI_ABORT_BUSY;
946 			} else if (tmp == host->first_pending) {
947 				if ((host->first_pending = tmp->next) == NULL)
948 					host->last_pending = NULL;
949 			} else {
950 				prev->next = tmp->next;
951 				if (tmp == host->last_pending)
952 					host->last_pending = prev;
953 			}
954 			tmp->hastat = HOST_ABORTED;
955 			tmp->flags |= SCF_DONE;
956 			if (tmp->flags & SCF_POST)
957 				(*tmp->post) ((u8 *) host, (u8 *) tmp);
958 			spin_unlock_irqrestore(&host->semaph_lock, flags);
959 			return SCSI_ABORT_SUCCESS;
960 		}
961 		prev = tmp;
962 		tmp = tmp->next;
963 	}
964 
965 	prev = tmp = host->first_busy;	/* Check Busy queue */
966 	while (tmp != NULL) {
967 		if (tmp->srb == srbp) {
968 			if (tmp == host->active) {
969 				spin_unlock_irqrestore(&host->semaph_lock, flags);
970 				return SCSI_ABORT_BUSY;
971 			} else if (tmp->tagmsg == 0) {
972 				spin_unlock_irqrestore(&host->semaph_lock, flags);
973 				return SCSI_ABORT_BUSY;
974 			} else {
975 				host->act_tags[tmp->target]--;
976 				if (tmp == host->first_busy) {
977 					if ((host->first_busy = tmp->next) == NULL)
978 						host->last_busy = NULL;
979 				} else {
980 					prev->next = tmp->next;
981 					if (tmp == host->last_busy)
982 						host->last_busy = prev;
983 				}
984 				tmp->next = NULL;
985 
986 
987 				tmp->hastat = HOST_ABORTED;
988 				tmp->flags |= SCF_DONE;
989 				if (tmp->flags & SCF_POST)
990 					(*tmp->post) ((u8 *) host, (u8 *) tmp);
991 				spin_unlock_irqrestore(&host->semaph_lock, flags);
992 				return SCSI_ABORT_SUCCESS;
993 			}
994 		}
995 		prev = tmp;
996 		tmp = tmp->next;
997 	}
998 	spin_unlock_irqrestore(&host->semaph_lock, flags);
999 	return SCSI_ABORT_NOT_RUNNING;
1000 }
1001 
1002 /***************************************************************************/
1003 static int initio_bad_seq(struct initio_host * host)
1004 {
1005 	struct scsi_ctrl_blk *scb;
1006 
1007 	printk("initio_bad_seg c=%d\n", host->index);
1008 
1009 	if ((scb = host->active) != NULL) {
1010 		initio_unlink_busy_scb(host, scb);
1011 		scb->hastat = HOST_BAD_PHAS;
1012 		scb->tastat = 0;
1013 		initio_append_done_scb(host, scb);
1014 	}
1015 	initio_stop_bm(host);
1016 	initio_reset_scsi(host, 8);	/* 7/29/98 */
1017 	return initio_post_scsi_rst(host);
1018 }
1019 
1020 
1021 /************************************************************************/
1022 static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
1023 {
1024 	unsigned long flags;
1025 
1026 	scb->mode = 0;
1027 
1028 	scb->sgidx = 0;
1029 	scb->sgmax = scb->sglen;
1030 
1031 	spin_lock_irqsave(&host->semaph_lock, flags);
1032 
1033 	initio_append_pend_scb(host, scb);	/* Append this SCB to Pending queue */
1034 
1035 /* VVVVV 07/21/98 */
1036 	if (host->semaph == 1) {
1037 		/* Disable Jasmin SCSI Int */
1038 		outb(0x1F, host->addr + TUL_Mask);
1039 		host->semaph = 0;
1040 		spin_unlock_irqrestore(&host->semaph_lock, flags);
1041 
1042 		tulip_main(host);
1043 
1044 		spin_lock_irqsave(&host->semaph_lock, flags);
1045 		host->semaph = 1;
1046 		outb(0x0F, host->addr + TUL_Mask);
1047 	}
1048 	spin_unlock_irqrestore(&host->semaph_lock, flags);
1049 	return;
1050 }
1051 
1052 /***************************************************************************/
1053 static int initio_isr(struct initio_host * host)
1054 {
1055 	if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1056 		if (host->semaph == 1) {
1057 			outb(0x1F, host->addr + TUL_Mask);
1058 			/* Disable Tulip SCSI Int */
1059 			host->semaph = 0;
1060 
1061 			tulip_main(host);
1062 
1063 			host->semaph = 1;
1064 			outb(0x0F, host->addr + TUL_Mask);
1065 			return 1;
1066 		}
1067 	}
1068 	return 0;
1069 }
1070 
1071 static int tulip_main(struct initio_host * host)
1072 {
1073 	struct scsi_ctrl_blk *scb;
1074 
1075 	for (;;) {
1076 		tulip_scsi(host);	/* Call tulip_scsi              */
1077 
1078 		/* Walk the list of completed SCBs */
1079 		while ((scb = initio_find_done_scb(host)) != NULL) {	/* find done entry */
1080 			if (scb->tastat == INI_QUEUE_FULL) {
1081 				host->max_tags[scb->target] =
1082 				    host->act_tags[scb->target] - 1;
1083 				scb->tastat = 0;
1084 				initio_append_pend_scb(host, scb);
1085 				continue;
1086 			}
1087 			if (!(scb->mode & SCM_RSENS)) {		/* not in auto req. sense mode */
1088 				if (scb->tastat == 2) {
1089 
1090 					/* clr sync. nego flag */
1091 
1092 					if (scb->flags & SCF_SENSE) {
1093 						u8 len;
1094 						len = scb->senselen;
1095 						if (len == 0)
1096 							len = 1;
1097 						scb->buflen = scb->senselen;
1098 						scb->bufptr = scb->senseptr;
1099 						scb->flags &= ~(SCF_SG | SCF_DIR);	/* for xfer_data_in */
1100 						/* so, we won't report wrong direction in xfer_data_in,
1101 						   and won't report HOST_DO_DU in state_6 */
1102 						scb->mode = SCM_RSENS;
1103 						scb->ident &= 0xBF;	/* Disable Disconnect */
1104 						scb->tagmsg = 0;
1105 						scb->tastat = 0;
1106 						scb->cdblen = 6;
1107 						scb->cdb[0] = SCSICMD_RequestSense;
1108 						scb->cdb[1] = 0;
1109 						scb->cdb[2] = 0;
1110 						scb->cdb[3] = 0;
1111 						scb->cdb[4] = len;
1112 						scb->cdb[5] = 0;
1113 						initio_push_pend_scb(host, scb);
1114 						break;
1115 					}
1116 				}
1117 			} else {	/* in request sense mode */
1118 
1119 				if (scb->tastat == 2) {		/* check contition status again after sending
1120 									   requset sense cmd 0x3 */
1121 					scb->hastat = HOST_BAD_PHAS;
1122 				}
1123 				scb->tastat = 2;
1124 			}
1125 			scb->flags |= SCF_DONE;
1126 			if (scb->flags & SCF_POST) {
1127 				/* FIXME: only one post method and lose casts */
1128 				(*scb->post) ((u8 *) host, (u8 *) scb);
1129 			}
1130 		}		/* while */
1131 		/* find_active: */
1132 		if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
1133 			continue;
1134 		if (host->active)	/* return to OS and wait for xfer_done_ISR/Selected_ISR */
1135 			return 1;	/* return to OS, enable interrupt */
1136 		/* Check pending SCB            */
1137 		if (initio_find_first_pend_scb(host) == NULL)
1138 			return 1;	/* return to OS, enable interrupt */
1139 	}			/* End of for loop */
1140 	/* statement won't reach here */
1141 }
1142 
1143 static void tulip_scsi(struct initio_host * host)
1144 {
1145 	struct scsi_ctrl_blk *scb;
1146 	struct target_control *active_tc;
1147 
1148 	/* make sure to service interrupt asap */
1149 	if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1150 		host->phase = host->jsstatus0 & TSS_PH_MASK;
1151 		host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1152 		host->jsint = inb(host->addr + TUL_SInt);
1153 		if (host->jsint & TSS_SCSIRST_INT) {	/* SCSI bus reset detected      */
1154 			int_initio_scsi_rst(host);
1155 			return;
1156 		}
1157 		if (host->jsint & TSS_RESEL_INT) {	/* if selected/reselected interrupt */
1158 			if (int_initio_resel(host) == 0)
1159 				initio_next_state(host);
1160 			return;
1161 		}
1162 		if (host->jsint & TSS_SEL_TIMEOUT) {
1163 			int_initio_busfree(host);
1164 			return;
1165 		}
1166 		if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
1167 			int_initio_busfree(host);	/* unexpected bus free or sel timeout */
1168 			return;
1169 		}
1170 		if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) {	/* func complete or Bus service */
1171 			if ((scb = host->active) != NULL)
1172 				initio_next_state(host);
1173 			return;
1174 		}
1175 	}
1176 	if (host->active != NULL)
1177 		return;
1178 
1179 	if ((scb = initio_find_first_pend_scb(host)) == NULL)
1180 		return;
1181 
1182 	/* program HBA's SCSI ID & target SCSI ID */
1183 	outb((host->scsi_id << 4) | (scb->target & 0x0F),
1184 		host->addr + TUL_SScsiId);
1185 	if (scb->opcode == ExecSCSI) {
1186 		active_tc = &host->targets[scb->target];
1187 
1188 		if (scb->tagmsg)
1189 			active_tc->drv_flags |= TCF_DRV_EN_TAG;
1190 		else
1191 			active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
1192 
1193 		outb(active_tc->js_period, host->addr + TUL_SPeriod);
1194 		if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {	/* do wdtr negotiation          */
1195 			initio_select_atn_stop(host, scb);
1196 		} else {
1197 			if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {	/* do sync negotiation          */
1198 				initio_select_atn_stop(host, scb);
1199 			} else {
1200 				if (scb->tagmsg)
1201 					initio_select_atn3(host, scb);
1202 				else
1203 					initio_select_atn(host, scb);
1204 			}
1205 		}
1206 		if (scb->flags & SCF_POLL) {
1207 			while (wait_tulip(host) != -1) {
1208 				if (initio_next_state(host) == -1)
1209 					break;
1210 			}
1211 		}
1212 	} else if (scb->opcode == BusDevRst) {
1213 		initio_select_atn_stop(host, scb);
1214 		scb->next_state = 8;
1215 		if (scb->flags & SCF_POLL) {
1216 			while (wait_tulip(host) != -1) {
1217 				if (initio_next_state(host) == -1)
1218 					break;
1219 			}
1220 		}
1221 	} else if (scb->opcode == AbortCmd) {
1222 		if (initio_abort_srb(host, scb->srb) != 0) {
1223 			initio_unlink_pend_scb(host, scb);
1224 			initio_release_scb(host, scb);
1225 		} else {
1226 			scb->opcode = BusDevRst;
1227 			initio_select_atn_stop(host, scb);
1228 			scb->next_state = 8;
1229 		}
1230 	} else {
1231 		initio_unlink_pend_scb(host, scb);
1232 		scb->hastat = 0x16;	/* bad command */
1233 		initio_append_done_scb(host, scb);
1234 	}
1235 	return;
1236 }
1237 
1238 /**
1239  *	initio_next_state		-	Next SCSI state
1240  *	@host: InitIO host we are processing
1241  *
1242  *	Progress the active command block along the state machine
1243  *	until we hit a state which we must wait for activity to occur.
1244  *
1245  *	Returns zero or a negative code.
1246  */
1247 
1248 static int initio_next_state(struct initio_host * host)
1249 {
1250 	int next;
1251 
1252 	next = host->active->next_state;
1253 	for (;;) {
1254 		switch (next) {
1255 		case 1:
1256 			next = initio_state_1(host);
1257 			break;
1258 		case 2:
1259 			next = initio_state_2(host);
1260 			break;
1261 		case 3:
1262 			next = initio_state_3(host);
1263 			break;
1264 		case 4:
1265 			next = initio_state_4(host);
1266 			break;
1267 		case 5:
1268 			next = initio_state_5(host);
1269 			break;
1270 		case 6:
1271 			next = initio_state_6(host);
1272 			break;
1273 		case 7:
1274 			next = initio_state_7(host);
1275 			break;
1276 		case 8:
1277 			return initio_bus_device_reset(host);
1278 		default:
1279 			return initio_bad_seq(host);
1280 		}
1281 		if (next <= 0)
1282 			return next;
1283 	}
1284 }
1285 
1286 
1287 /**
1288  *	initio_state_1		-	SCSI state machine
1289  *	@host: InitIO host we are controlling
1290  *
1291  *	Perform SCSI state processing for Select/Attention/Stop
1292  */
1293 
1294 static int initio_state_1(struct initio_host * host)
1295 {
1296 	struct scsi_ctrl_blk *scb = host->active;
1297 	struct target_control *active_tc = host->active_tc;
1298 #if DEBUG_STATE
1299 	printk("-s1-");
1300 #endif
1301 
1302 	/* Move the SCB from pending to busy */
1303 	initio_unlink_pend_scb(host, scb);
1304 	initio_append_busy_scb(host, scb);
1305 
1306 	outb(active_tc->sconfig0, host->addr + TUL_SConfig );
1307 	/* ATN on */
1308 	if (host->phase == MSG_OUT) {
1309 		outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1310 		outb(scb->ident, host->addr + TUL_SFifo);
1311 
1312 		if (scb->tagmsg) {
1313 			outb(scb->tagmsg, host->addr + TUL_SFifo);
1314 			outb(scb->tagid, host->addr + TUL_SFifo);
1315 		}
1316 		if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1317 			active_tc->flags |= TCF_WDTR_DONE;
1318 			outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1319 			outb(2, host->addr + TUL_SFifo);	/* Extended msg length */
1320 			outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* Sync request */
1321 			outb(1, host->addr + TUL_SFifo);	/* Start from 16 bits */
1322 		} else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1323 			active_tc->flags |= TCF_SYNC_DONE;
1324 			outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1325 			outb(3, host->addr + TUL_SFifo);	/* extended msg length */
1326 			outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* sync request */
1327 			outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1328 			outb(MAX_OFFSET, host->addr + TUL_SFifo);	/* REQ/ACK offset */
1329 		}
1330 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1331 		if (wait_tulip(host) == -1)
1332 			return -1;
1333 	}
1334 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1335 	outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1336 	/* Into before CDB xfer */
1337 	return 3;
1338 }
1339 
1340 
1341 /**
1342  *	initio_state_2		-	SCSI state machine
1343  *	@host: InitIO host we are controlling
1344  *
1345  * state after selection with attention
1346  * state after selection with attention3
1347  */
1348 
1349 static int initio_state_2(struct initio_host * host)
1350 {
1351 	struct scsi_ctrl_blk *scb = host->active;
1352 	struct target_control *active_tc = host->active_tc;
1353 #if DEBUG_STATE
1354 	printk("-s2-");
1355 #endif
1356 
1357 	initio_unlink_pend_scb(host, scb);
1358 	initio_append_busy_scb(host, scb);
1359 
1360 	outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1361 
1362 	if (host->jsstatus1 & TSS_CMD_PH_CMP)
1363 		return 4;
1364 
1365 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1366 	outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1367 	/* Into before CDB xfer */
1368 	return 3;
1369 }
1370 
1371 /**
1372  *	initio_state_3		-	SCSI state machine
1373  *	@host: InitIO host we are controlling
1374  *
1375  * state before CDB xfer is done
1376  */
1377 
1378 static int initio_state_3(struct initio_host * host)
1379 {
1380 	struct scsi_ctrl_blk *scb = host->active;
1381 	struct target_control *active_tc = host->active_tc;
1382 	int i;
1383 
1384 #if DEBUG_STATE
1385 	printk("-s3-");
1386 #endif
1387 	for (;;) {
1388 		switch (host->phase) {
1389 		case CMD_OUT:	/* Command out phase            */
1390 			for (i = 0; i < (int) scb->cdblen; i++)
1391 				outb(scb->cdb[i], host->addr + TUL_SFifo);
1392 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1393 			if (wait_tulip(host) == -1)
1394 				return -1;
1395 			if (host->phase == CMD_OUT)
1396 				return initio_bad_seq(host);
1397 			return 4;
1398 
1399 		case MSG_IN:	/* Message in phase             */
1400 			scb->next_state = 3;
1401 			if (initio_msgin(host) == -1)
1402 				return -1;
1403 			break;
1404 
1405 		case STATUS_IN:	/* Status phase                 */
1406 			if (initio_status_msg(host) == -1)
1407 				return -1;
1408 			break;
1409 
1410 		case MSG_OUT:	/* Message out phase            */
1411 			if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1412 				outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
1413 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1414 				if (wait_tulip(host) == -1)
1415 					return -1;
1416 			} else {
1417 				active_tc->flags |= TCF_SYNC_DONE;
1418 
1419 				outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1420 				outb(3, host->addr + TUL_SFifo);	/* ext. msg len */
1421 				outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* sync request */
1422 				outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1423 				outb(MAX_OFFSET, host->addr + TUL_SFifo);	/* REQ/ACK offset */
1424 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1425 				if (wait_tulip(host) == -1)
1426 					return -1;
1427 				outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1428 				outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
1429 
1430 			}
1431 			break;
1432 		default:
1433 			return initio_bad_seq(host);
1434 		}
1435 	}
1436 }
1437 
1438 /**
1439  *	initio_state_4		-	SCSI state machine
1440  *	@host: InitIO host we are controlling
1441  *
1442  *	SCSI state machine. State 4
1443  */
1444 
1445 static int initio_state_4(struct initio_host * host)
1446 {
1447 	struct scsi_ctrl_blk *scb = host->active;
1448 
1449 #if DEBUG_STATE
1450 	printk("-s4-");
1451 #endif
1452 	if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1453 		return 6;	/* Go to state 6 (After data) */
1454 	}
1455 	for (;;) {
1456 		if (scb->buflen == 0)
1457 			return 6;
1458 
1459 		switch (host->phase) {
1460 
1461 		case STATUS_IN:	/* Status phase                 */
1462 			if ((scb->flags & SCF_DIR) != 0)	/* if direction bit set then report data underrun */
1463 				scb->hastat = HOST_DO_DU;
1464 			if ((initio_status_msg(host)) == -1)
1465 				return -1;
1466 			break;
1467 
1468 		case MSG_IN:	/* Message in phase             */
1469 			scb->next_state = 0x4;
1470 			if (initio_msgin(host) == -1)
1471 				return -1;
1472 			break;
1473 
1474 		case MSG_OUT:	/* Message out phase            */
1475 			if (host->jsstatus0 & TSS_PAR_ERROR) {
1476 				scb->buflen = 0;
1477 				scb->hastat = HOST_DO_DU;
1478 				if (initio_msgout_ide(host) == -1)
1479 					return -1;
1480 				return 6;
1481 			} else {
1482 				outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
1483 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1484 				if (wait_tulip(host) == -1)
1485 					return -1;
1486 			}
1487 			break;
1488 
1489 		case DATA_IN:	/* Data in phase                */
1490 			return initio_xfer_data_in(host);
1491 
1492 		case DATA_OUT:	/* Data out phase               */
1493 			return initio_xfer_data_out(host);
1494 
1495 		default:
1496 			return initio_bad_seq(host);
1497 		}
1498 	}
1499 }
1500 
1501 
1502 /**
1503  *	initio_state_5		-	SCSI state machine
1504  *	@host: InitIO host we are controlling
1505  *
1506  *	State after dma xfer done or phase change before xfer done
1507  */
1508 
1509 static int initio_state_5(struct initio_host * host)
1510 {
1511 	struct scsi_ctrl_blk *scb = host->active;
1512 	long cnt, xcnt;		/* cannot use unsigned !! code: if (xcnt < 0) */
1513 
1514 #if DEBUG_STATE
1515 	printk("-s5-");
1516 #endif
1517 	/*------ get remaining count -------*/
1518 	cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
1519 
1520 	if (inb(host->addr + TUL_XCmd) & 0x20) {
1521 		/* ----------------------- DATA_IN ----------------------------- */
1522 		/* check scsi parity error */
1523 		if (host->jsstatus0 & TSS_PAR_ERROR)
1524 			scb->hastat = HOST_DO_DU;
1525 		if (inb(host->addr + TUL_XStatus) & XPEND) {	/* DMA xfer pending, Send STOP  */
1526 			/* tell Hardware  scsi xfer has been terminated */
1527 			outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
1528 			/* wait until DMA xfer not pending */
1529 			while (inb(host->addr + TUL_XStatus) & XPEND)
1530 				cpu_relax();
1531 		}
1532 	} else {
1533 		/*-------- DATA OUT -----------*/
1534 		if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1535 			if (host->active_tc->js_period & TSC_WIDE_SCSI)
1536 				cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
1537 			else
1538 				cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
1539 		}
1540 		if (inb(host->addr + TUL_XStatus) & XPEND) {	/* if DMA xfer is pending, abort DMA xfer */
1541 			outb(TAX_X_ABT, host->addr + TUL_XCmd);
1542 			/* wait Abort DMA xfer done */
1543 			while ((inb(host->addr + TUL_Int) & XABT) == 0)
1544 				cpu_relax();
1545 		}
1546 		if ((cnt == 1) && (host->phase == DATA_OUT)) {
1547 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1548 			if (wait_tulip(host) == -1)
1549 				return -1;
1550 			cnt = 0;
1551 		} else {
1552 			if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1553 				outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1554 		}
1555 	}
1556 	if (cnt == 0) {
1557 		scb->buflen = 0;
1558 		return 6;	/* After Data */
1559 	}
1560 	/* Update active data pointer */
1561 	xcnt = (long) scb->buflen - cnt;	/* xcnt== bytes already xferred */
1562 	scb->buflen = (u32) cnt;		/* cnt == bytes left to be xferred */
1563 	if (scb->flags & SCF_SG) {
1564 		struct sg_entry *sgp;
1565 		unsigned long i;
1566 
1567 		sgp = &scb->sglist[scb->sgidx];
1568 		for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1569 			xcnt -= (long) sgp->len;
1570 			if (xcnt < 0) {		/* this sgp xfer half done */
1571 				xcnt += (long) sgp->len;	/* xcnt == bytes xferred in this sgp */
1572 				sgp->data += (u32) xcnt;	/* new ptr to be xfer */
1573 				sgp->len -= (u32) xcnt;	/* new len to be xfer */
1574 				scb->bufptr += ((u32) (i - scb->sgidx) << 3);
1575 				/* new SG table ptr */
1576 				scb->sglen = (u8) (scb->sgmax - i);
1577 				/* new SG table len */
1578 				scb->sgidx = (u16) i;
1579 				/* for next disc and come in this loop */
1580 				return 4;	/* Go to state 4                */
1581 			}
1582 			/* else (xcnt >= 0 , i.e. this sgp already xferred */
1583 		}		/* for */
1584 		return 6;	/* Go to state 6                */
1585 	} else {
1586 		scb->bufptr += (u32) xcnt;
1587 	}
1588 	return 4;		/* Go to state 4                */
1589 }
1590 
1591 /**
1592  *	initio_state_6		-	SCSI state machine
1593  *	@host: InitIO host we are controlling
1594  *
1595  *	State after Data phase
1596  */
1597 
1598 static int initio_state_6(struct initio_host * host)
1599 {
1600 	struct scsi_ctrl_blk *scb = host->active;
1601 
1602 #if DEBUG_STATE
1603 	printk("-s6-");
1604 #endif
1605 	for (;;) {
1606 		switch (host->phase) {
1607 		case STATUS_IN:	/* Status phase                 */
1608 			if ((initio_status_msg(host)) == -1)
1609 				return -1;
1610 			break;
1611 
1612 		case MSG_IN:	/* Message in phase             */
1613 			scb->next_state = 6;
1614 			if ((initio_msgin(host)) == -1)
1615 				return -1;
1616 			break;
1617 
1618 		case MSG_OUT:	/* Message out phase            */
1619 			outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
1620 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1621 			if (wait_tulip(host) == -1)
1622 				return -1;
1623 			break;
1624 
1625 		case DATA_IN:	/* Data in phase                */
1626 			return initio_xpad_in(host);
1627 
1628 		case DATA_OUT:	/* Data out phase               */
1629 			return initio_xpad_out(host);
1630 
1631 		default:
1632 			return initio_bad_seq(host);
1633 		}
1634 	}
1635 }
1636 
1637 /**
1638  *	initio_state_7		-	SCSI state machine
1639  *	@host: InitIO host we are controlling
1640  *
1641  */
1642 
1643 static int initio_state_7(struct initio_host * host)
1644 {
1645 	int cnt, i;
1646 
1647 #if DEBUG_STATE
1648 	printk("-s7-");
1649 #endif
1650 	/* flush SCSI FIFO */
1651 	cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
1652 	if (cnt) {
1653 		for (i = 0; i < cnt; i++)
1654 			inb(host->addr + TUL_SFifo);
1655 	}
1656 	switch (host->phase) {
1657 	case DATA_IN:		/* Data in phase                */
1658 	case DATA_OUT:		/* Data out phase               */
1659 		return initio_bad_seq(host);
1660 	default:
1661 		return 6;	/* Go to state 6                */
1662 	}
1663 }
1664 
1665 /**
1666  *	initio_xfer_data_in	-	Commence data input
1667  *	@host: InitIO host in use
1668  *
1669  *	Commence a block of data transfer. The transfer itself will
1670  *	be managed by the controller and we will get a completion (or
1671  *	failure) interrupt.
1672  */
1673 static int initio_xfer_data_in(struct initio_host * host)
1674 {
1675 	struct scsi_ctrl_blk *scb = host->active;
1676 
1677 	if ((scb->flags & SCF_DIR) == SCF_DOUT)
1678 		return 6;	/* wrong direction */
1679 
1680 	outl(scb->buflen, host->addr + TUL_SCnt0);
1681 	outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd);	/* 7/25/95 */
1682 
1683 	if (scb->flags & SCF_SG) {	/* S/G xfer */
1684 		outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1685 		outl(scb->bufptr, host->addr + TUL_XAddH);
1686 		outb(TAX_SG_IN, host->addr + TUL_XCmd);
1687 	} else {
1688 		outl(scb->buflen, host->addr + TUL_XCntH);
1689 		outl(scb->bufptr, host->addr + TUL_XAddH);
1690 		outb(TAX_X_IN, host->addr + TUL_XCmd);
1691 	}
1692 	scb->next_state = 0x5;
1693 	return 0;		/* return to OS, wait xfer done , let jas_isr come in */
1694 }
1695 
1696 /**
1697  *	initio_xfer_data_out	-	Commence data output
1698  *	@host: InitIO host in use
1699  *
1700  *	Commence a block of data transfer. The transfer itself will
1701  *	be managed by the controller and we will get a completion (or
1702  *	failure) interrupt.
1703  */
1704 
1705 static int initio_xfer_data_out(struct initio_host * host)
1706 {
1707 	struct scsi_ctrl_blk *scb = host->active;
1708 
1709 	if ((scb->flags & SCF_DIR) == SCF_DIN)
1710 		return 6;	/* wrong direction */
1711 
1712 	outl(scb->buflen, host->addr + TUL_SCnt0);
1713 	outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1714 
1715 	if (scb->flags & SCF_SG) {	/* S/G xfer */
1716 		outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1717 		outl(scb->bufptr, host->addr + TUL_XAddH);
1718 		outb(TAX_SG_OUT, host->addr + TUL_XCmd);
1719 	} else {
1720 		outl(scb->buflen, host->addr + TUL_XCntH);
1721 		outl(scb->bufptr, host->addr + TUL_XAddH);
1722 		outb(TAX_X_OUT, host->addr + TUL_XCmd);
1723 	}
1724 
1725 	scb->next_state = 0x5;
1726 	return 0;		/* return to OS, wait xfer done , let jas_isr come in */
1727 }
1728 
1729 int initio_xpad_in(struct initio_host * host)
1730 {
1731 	struct scsi_ctrl_blk *scb = host->active;
1732 	struct target_control *active_tc = host->active_tc;
1733 
1734 	if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1735 		scb->hastat = HOST_DO_DU;	/* over run             */
1736 	for (;;) {
1737 		if (active_tc->js_period & TSC_WIDE_SCSI)
1738 			outl(2, host->addr + TUL_SCnt0);
1739 		else
1740 			outl(1, host->addr + TUL_SCnt0);
1741 
1742 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1743 		if (wait_tulip(host) == -1)
1744 			return -1;
1745 		if (host->phase != DATA_IN) {
1746 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1747 			return 6;
1748 		}
1749 		inb(host->addr + TUL_SFifo);
1750 	}
1751 }
1752 
1753 int initio_xpad_out(struct initio_host * host)
1754 {
1755 	struct scsi_ctrl_blk *scb = host->active;
1756 	struct target_control *active_tc = host->active_tc;
1757 
1758 	if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1759 		scb->hastat = HOST_DO_DU;	/* over run             */
1760 	for (;;) {
1761 		if (active_tc->js_period & TSC_WIDE_SCSI)
1762 			outl(2, host->addr + TUL_SCnt0);
1763 		else
1764 			outl(1, host->addr + TUL_SCnt0);
1765 
1766 		outb(0, host->addr + TUL_SFifo);
1767 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1768 		if ((wait_tulip(host)) == -1)
1769 			return -1;
1770 		if (host->phase != DATA_OUT) {	/* Disable wide CPU to allow read 16 bits */
1771 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1772 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1773 			return 6;
1774 		}
1775 	}
1776 }
1777 
1778 int initio_status_msg(struct initio_host * host)
1779 {				/* status & MSG_IN */
1780 	struct scsi_ctrl_blk *scb = host->active;
1781 	u8 msg;
1782 
1783 	outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1784 	if (wait_tulip(host) == -1)
1785 		return -1;
1786 
1787 	/* get status */
1788 	scb->tastat = inb(host->addr + TUL_SFifo);
1789 
1790 	if (host->phase == MSG_OUT) {
1791 		if (host->jsstatus0 & TSS_PAR_ERROR)
1792 			outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1793 		else
1794 			outb(NOP, host->addr + TUL_SFifo);
1795 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1796 		return wait_tulip(host);
1797 	}
1798 	if (host->phase == MSG_IN) {
1799 		msg = inb(host->addr + TUL_SFifo);
1800 		if (host->jsstatus0 & TSS_PAR_ERROR) {	/* Parity error                 */
1801 			if ((initio_msgin_accept(host)) == -1)
1802 				return -1;
1803 			if (host->phase != MSG_OUT)
1804 				return initio_bad_seq(host);
1805 			outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1806 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1807 			return wait_tulip(host);
1808 		}
1809 		if (msg == 0) {	/* Command complete             */
1810 
1811 			if ((scb->tastat & 0x18) == 0x10)	/* No link support              */
1812 				return initio_bad_seq(host);
1813 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1814 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1815 			return initio_wait_done_disc(host);
1816 
1817 		}
1818 		if (msg == LINKED_CMD_COMPLETE ||
1819 		    msg == LINKED_FLG_CMD_COMPLETE) {
1820 			if ((scb->tastat & 0x18) == 0x10)
1821 				return initio_msgin_accept(host);
1822 		}
1823 	}
1824 	return initio_bad_seq(host);
1825 }
1826 
1827 
1828 /* scsi bus free */
1829 int int_initio_busfree(struct initio_host * host)
1830 {
1831 	struct scsi_ctrl_blk *scb = host->active;
1832 
1833 	if (scb != NULL) {
1834 		if (scb->status & SCB_SELECT) {		/* selection timeout */
1835 			initio_unlink_pend_scb(host, scb);
1836 			scb->hastat = HOST_SEL_TOUT;
1837 			initio_append_done_scb(host, scb);
1838 		} else {	/* Unexpected bus free          */
1839 			initio_unlink_busy_scb(host, scb);
1840 			scb->hastat = HOST_BUS_FREE;
1841 			initio_append_done_scb(host, scb);
1842 		}
1843 		host->active = NULL;
1844 		host->active_tc = NULL;
1845 	}
1846 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);		/* Flush SCSI FIFO  */
1847 	outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1848 	outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect       */
1849 	return -1;
1850 }
1851 
1852 
1853 /**
1854  *	int_initio_scsi_rst	-	SCSI reset occurred
1855  *	@host: Host seeing the reset
1856  *
1857  *	A SCSI bus reset has occurred. Clean up any pending transfer
1858  *	the hardware is doing by DMA and then abort all active and
1859  *	disconnected commands. The mid layer should sort the rest out
1860  *	for us
1861  */
1862 
1863 static int int_initio_scsi_rst(struct initio_host * host)
1864 {
1865 	struct scsi_ctrl_blk *scb;
1866 	int i;
1867 
1868 	/* if DMA xfer is pending, abort DMA xfer */
1869 	if (inb(host->addr + TUL_XStatus) & 0x01) {
1870 		outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
1871 		/* wait Abort DMA xfer done */
1872 		while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1873 			cpu_relax();
1874 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1875 	}
1876 	/* Abort all active & disconnected scb */
1877 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
1878 		scb->hastat = HOST_BAD_PHAS;
1879 		initio_append_done_scb(host, scb);
1880 	}
1881 	host->active = NULL;
1882 	host->active_tc = NULL;
1883 
1884 	/* clr sync nego. done flag */
1885 	for (i = 0; i < host->max_tar; i++)
1886 		host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1887 	return -1;
1888 }
1889 
1890 /**
1891  *	int_initio_scsi_resel	-	Reselection occurred
1892  *	@host: InitIO host adapter
1893  *
1894  *	A SCSI reselection event has been signalled and the interrupt
1895  *	is now being processed. Work out which command block needs attention
1896  *	and continue processing that command.
1897  */
1898 
1899 int int_initio_resel(struct initio_host * host)
1900 {
1901 	struct scsi_ctrl_blk *scb;
1902 	struct target_control *active_tc;
1903 	u8 tag, msg = 0;
1904 	u8 tar, lun;
1905 
1906 	if ((scb = host->active) != NULL) {
1907 		/* FIXME: Why check and not just clear ? */
1908 		if (scb->status & SCB_SELECT)		/* if waiting for selection complete */
1909 			scb->status &= ~SCB_SELECT;
1910 		host->active = NULL;
1911 	}
1912 	/* --------- get target id---------------------- */
1913 	tar = inb(host->addr + TUL_SBusId);
1914 	/* ------ get LUN from Identify message----------- */
1915 	lun = inb(host->addr + TUL_SIdent) & 0x0F;
1916 	/* 07/22/98 from 0x1F -> 0x0F */
1917 	active_tc = &host->targets[tar];
1918 	host->active_tc = active_tc;
1919 	outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1920 	outb(active_tc->js_period, host->addr + TUL_SPeriod);
1921 
1922 	/* ------------- tag queueing ? ------------------- */
1923 	if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1924 		if ((initio_msgin_accept(host)) == -1)
1925 			return -1;
1926 		if (host->phase != MSG_IN)
1927 			goto no_tag;
1928 		outl(1, host->addr + TUL_SCnt0);
1929 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1930 		if (wait_tulip(host) == -1)
1931 			return -1;
1932 		msg = inb(host->addr + TUL_SFifo);	/* Read Tag Message    */
1933 
1934 		if (msg < SIMPLE_QUEUE_TAG || msg > ORDERED_QUEUE_TAG)
1935 			/* Is simple Tag      */
1936 			goto no_tag;
1937 
1938 		if (initio_msgin_accept(host) == -1)
1939 			return -1;
1940 
1941 		if (host->phase != MSG_IN)
1942 			goto no_tag;
1943 
1944 		outl(1, host->addr + TUL_SCnt0);
1945 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1946 		if (wait_tulip(host) == -1)
1947 			return -1;
1948 		tag = inb(host->addr + TUL_SFifo);	/* Read Tag ID       */
1949 		scb = host->scb + tag;
1950 		if (scb->target != tar || scb->lun != lun) {
1951 			return initio_msgout_abort_tag(host);
1952 		}
1953 		if (scb->status != SCB_BUSY) {	/* 03/24/95             */
1954 			return initio_msgout_abort_tag(host);
1955 		}
1956 		host->active = scb;
1957 		if ((initio_msgin_accept(host)) == -1)
1958 			return -1;
1959 	} else {		/* No tag               */
1960 	      no_tag:
1961 		if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1962 			return initio_msgout_abort_targ(host);
1963 		}
1964 		host->active = scb;
1965 		if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1966 			if ((initio_msgin_accept(host)) == -1)
1967 				return -1;
1968 		}
1969 	}
1970 	return 0;
1971 }
1972 
1973 /**
1974  *	int_initio_bad_seq		-	out of phase
1975  *	@host: InitIO host flagging event
1976  *
1977  *	We have ended up out of phase somehow. Reset the host controller
1978  *	and throw all our toys out of the pram. Let the midlayer clean up
1979  */
1980 
1981 static int int_initio_bad_seq(struct initio_host * host)
1982 {				/* target wrong phase           */
1983 	struct scsi_ctrl_blk *scb;
1984 	int i;
1985 
1986 	initio_reset_scsi(host, 10);
1987 
1988 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
1989 		scb->hastat = HOST_BAD_PHAS;
1990 		initio_append_done_scb(host, scb);
1991 	}
1992 	for (i = 0; i < host->max_tar; i++)
1993 		host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1994 	return -1;
1995 }
1996 
1997 
1998 /**
1999  *	initio_msgout_abort_targ		-	abort a tag
2000  *	@host: InitIO host
2001  *
2002  *	Abort when the target/lun does not match or when our SCB is not
2003  *	busy. Used by untagged commands.
2004  */
2005 
2006 static int initio_msgout_abort_targ(struct initio_host * host)
2007 {
2008 
2009 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2010 	if (initio_msgin_accept(host) == -1)
2011 		return -1;
2012 	if (host->phase != MSG_OUT)
2013 		return initio_bad_seq(host);
2014 
2015 	outb(ABORT_TASK_SET, host->addr + TUL_SFifo);
2016 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2017 
2018 	return initio_wait_disc(host);
2019 }
2020 
2021 /**
2022  *	initio_msgout_abort_tag		-	abort a tag
2023  *	@host: InitIO host
2024  *
2025  *	Abort when the target/lun does not match or when our SCB is not
2026  *	busy. Used for tagged commands.
2027  */
2028 
2029 static int initio_msgout_abort_tag(struct initio_host * host)
2030 {
2031 
2032 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2033 	if (initio_msgin_accept(host) == -1)
2034 		return -1;
2035 	if (host->phase != MSG_OUT)
2036 		return initio_bad_seq(host);
2037 
2038 	outb(ABORT_TASK, host->addr + TUL_SFifo);
2039 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2040 
2041 	return initio_wait_disc(host);
2042 
2043 }
2044 
2045 /**
2046  *	initio_msgin		-	Message in
2047  *	@host: InitIO Host
2048  *
2049  *	Process incoming message
2050  */
2051 static int initio_msgin(struct initio_host * host)
2052 {
2053 	struct target_control *active_tc;
2054 
2055 	for (;;) {
2056 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2057 
2058 		outl(1, host->addr + TUL_SCnt0);
2059 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2060 		if (wait_tulip(host) == -1)
2061 			return -1;
2062 
2063 		switch (inb(host->addr + TUL_SFifo)) {
2064 		case DISCONNECT:	/* Disconnect msg */
2065 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2066 			return initio_wait_disc(host);
2067 		case SAVE_POINTERS:
2068 		case RESTORE_POINTERS:
2069 		case NOP:
2070 			initio_msgin_accept(host);
2071 			break;
2072 		case MESSAGE_REJECT:	/* Clear ATN first              */
2073 			outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2074 				host->addr + TUL_SSignal);
2075 			active_tc = host->active_tc;
2076 			if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)	/* do sync nego */
2077 				outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2078 					host->addr + TUL_SSignal);
2079 			initio_msgin_accept(host);
2080 			break;
2081 		case EXTENDED_MESSAGE:	/* extended msg */
2082 			initio_msgin_extend(host);
2083 			break;
2084 		case IGNORE_WIDE_RESIDUE:
2085 			initio_msgin_accept(host);
2086 			break;
2087 		case COMMAND_COMPLETE:
2088 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2089 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2090 			return initio_wait_done_disc(host);
2091 		default:
2092 			initio_msgout_reject(host);
2093 			break;
2094 		}
2095 		if (host->phase != MSG_IN)
2096 			return host->phase;
2097 	}
2098 	/* statement won't reach here */
2099 }
2100 
2101 static int initio_msgout_reject(struct initio_host * host)
2102 {
2103 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2104 
2105 	if (initio_msgin_accept(host) == -1)
2106 		return -1;
2107 
2108 	if (host->phase == MSG_OUT) {
2109 		outb(MESSAGE_REJECT, host->addr + TUL_SFifo);		/* Msg reject           */
2110 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2111 		return wait_tulip(host);
2112 	}
2113 	return host->phase;
2114 }
2115 
2116 static int initio_msgout_ide(struct initio_host * host)
2117 {
2118 	outb(INITIATOR_ERROR, host->addr + TUL_SFifo);		/* Initiator Detected Error */
2119 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2120 	return wait_tulip(host);
2121 }
2122 
2123 static int initio_msgin_extend(struct initio_host * host)
2124 {
2125 	u8 len, idx;
2126 
2127 	if (initio_msgin_accept(host) != MSG_IN)
2128 		return host->phase;
2129 
2130 	/* Get extended msg length      */
2131 	outl(1, host->addr + TUL_SCnt0);
2132 	outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2133 	if (wait_tulip(host) == -1)
2134 		return -1;
2135 
2136 	len = inb(host->addr + TUL_SFifo);
2137 	host->msg[0] = len;
2138 	for (idx = 1; len != 0; len--) {
2139 
2140 		if ((initio_msgin_accept(host)) != MSG_IN)
2141 			return host->phase;
2142 		outl(1, host->addr + TUL_SCnt0);
2143 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2144 		if (wait_tulip(host) == -1)
2145 			return -1;
2146 		host->msg[idx++] = inb(host->addr + TUL_SFifo);
2147 	}
2148 	if (host->msg[1] == 1) {		/* if it's synchronous data transfer request */
2149 		u8 r;
2150 		if (host->msg[0] != 3)	/* if length is not right */
2151 			return initio_msgout_reject(host);
2152 		if (host->active_tc->flags & TCF_NO_SYNC_NEGO) {	/* Set OFFSET=0 to do async, nego back */
2153 			host->msg[3] = 0;
2154 		} else {
2155 			if (initio_msgin_sync(host) == 0 &&
2156 			    (host->active_tc->flags & TCF_SYNC_DONE)) {
2157 				initio_sync_done(host);
2158 				return initio_msgin_accept(host);
2159 			}
2160 		}
2161 
2162 		r = inb(host->addr + TUL_SSignal);
2163 		outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2164 			host->addr + TUL_SSignal);
2165 		if (initio_msgin_accept(host) != MSG_OUT)
2166 			return host->phase;
2167 		/* sync msg out */
2168 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2169 
2170 		initio_sync_done(host);
2171 
2172 		outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2173 		outb(3, host->addr + TUL_SFifo);
2174 		outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
2175 		outb(host->msg[2], host->addr + TUL_SFifo);
2176 		outb(host->msg[3], host->addr + TUL_SFifo);
2177 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2178 		return wait_tulip(host);
2179 	}
2180 	if (host->msg[0] != 2 || host->msg[1] != 3)
2181 		return initio_msgout_reject(host);
2182 	/* if it's WIDE DATA XFER REQ   */
2183 	if (host->active_tc->flags & TCF_NO_WDTR) {
2184 		host->msg[2] = 0;
2185 	} else {
2186 		if (host->msg[2] > 2)	/* > 32 bits            */
2187 			return initio_msgout_reject(host);
2188 		if (host->msg[2] == 2) {		/* == 32                */
2189 			host->msg[2] = 1;
2190 		} else {
2191 			if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2192 				wdtr_done(host);
2193 				if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2194 					outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2195 				return initio_msgin_accept(host);
2196 			}
2197 		}
2198 	}
2199 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2200 
2201 	if (initio_msgin_accept(host) != MSG_OUT)
2202 		return host->phase;
2203 	/* WDTR msg out                 */
2204 	outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2205 	outb(2, host->addr + TUL_SFifo);
2206 	outb(EXTENDED_WDTR, host->addr + TUL_SFifo);
2207 	outb(host->msg[2], host->addr + TUL_SFifo);
2208 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2209 	return wait_tulip(host);
2210 }
2211 
2212 static int initio_msgin_sync(struct initio_host * host)
2213 {
2214 	char default_period;
2215 
2216 	default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2217 	if (host->msg[3] > MAX_OFFSET) {
2218 		host->msg[3] = MAX_OFFSET;
2219 		if (host->msg[2] < default_period) {
2220 			host->msg[2] = default_period;
2221 			return 1;
2222 		}
2223 		if (host->msg[2] >= 59)	/* Change to async              */
2224 			host->msg[3] = 0;
2225 		return 1;
2226 	}
2227 	/* offset requests asynchronous transfers ? */
2228 	if (host->msg[3] == 0) {
2229 		return 0;
2230 	}
2231 	if (host->msg[2] < default_period) {
2232 		host->msg[2] = default_period;
2233 		return 1;
2234 	}
2235 	if (host->msg[2] >= 59) {
2236 		host->msg[3] = 0;
2237 		return 1;
2238 	}
2239 	return 0;
2240 }
2241 
2242 static int wdtr_done(struct initio_host * host)
2243 {
2244 	host->active_tc->flags &= ~TCF_SYNC_DONE;
2245 	host->active_tc->flags |= TCF_WDTR_DONE;
2246 
2247 	host->active_tc->js_period = 0;
2248 	if (host->msg[2])	/* if 16 bit */
2249 		host->active_tc->js_period |= TSC_WIDE_SCSI;
2250 	host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2251 	outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2252 	outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2253 
2254 	return 1;
2255 }
2256 
2257 static int initio_sync_done(struct initio_host * host)
2258 {
2259 	int i;
2260 
2261 	host->active_tc->flags |= TCF_SYNC_DONE;
2262 
2263 	if (host->msg[3]) {
2264 		host->active_tc->js_period |= host->msg[3];
2265 		for (i = 0; i < 8; i++) {
2266 			if (initio_rate_tbl[i] >= host->msg[2])	/* pick the big one */
2267 				break;
2268 		}
2269 		host->active_tc->js_period |= (i << 4);
2270 		host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
2271 	}
2272 	outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2273 	outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2274 
2275 	return -1;
2276 }
2277 
2278 
2279 static int initio_post_scsi_rst(struct initio_host * host)
2280 {
2281 	struct scsi_ctrl_blk *scb;
2282 	struct target_control *active_tc;
2283 	int i;
2284 
2285 	host->active = NULL;
2286 	host->active_tc = NULL;
2287 	host->flags = 0;
2288 
2289 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
2290 		scb->hastat = HOST_BAD_PHAS;
2291 		initio_append_done_scb(host, scb);
2292 	}
2293 	/* clear sync done flag         */
2294 	active_tc = &host->targets[0];
2295 	for (i = 0; i < host->max_tar; active_tc++, i++) {
2296 		active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
2297 		/* Initialize the sync. xfer register values to an asyn xfer */
2298 		active_tc->js_period = 0;
2299 		active_tc->sconfig0 = host->sconf1;
2300 		host->act_tags[0] = 0;	/* 07/22/98 */
2301 		host->targets[i].flags &= ~TCF_BUSY;	/* 07/22/98 */
2302 	}			/* for */
2303 
2304 	return -1;
2305 }
2306 
2307 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
2308 {
2309 	scb->status |= SCB_SELECT;
2310 	scb->next_state = 0x1;
2311 	host->active = scb;
2312 	host->active_tc = &host->targets[scb->target];
2313 	outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
2314 }
2315 
2316 
2317 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
2318 {
2319 	int i;
2320 
2321 	scb->status |= SCB_SELECT;
2322 	scb->next_state = 0x2;
2323 
2324 	outb(scb->ident, host->addr + TUL_SFifo);
2325 	for (i = 0; i < (int) scb->cdblen; i++)
2326 		outb(scb->cdb[i], host->addr + TUL_SFifo);
2327 	host->active_tc = &host->targets[scb->target];
2328 	host->active = scb;
2329 	outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
2330 }
2331 
2332 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
2333 {
2334 	int i;
2335 
2336 	scb->status |= SCB_SELECT;
2337 	scb->next_state = 0x2;
2338 
2339 	outb(scb->ident, host->addr + TUL_SFifo);
2340 	outb(scb->tagmsg, host->addr + TUL_SFifo);
2341 	outb(scb->tagid, host->addr + TUL_SFifo);
2342 	for (i = 0; i < scb->cdblen; i++)
2343 		outb(scb->cdb[i], host->addr + TUL_SFifo);
2344 	host->active_tc = &host->targets[scb->target];
2345 	host->active = scb;
2346 	outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
2347 }
2348 
2349 /**
2350  *	initio_bus_device_reset	-	 SCSI Bus Device Reset
2351  *	@host: InitIO host to reset
2352  *
2353  *	Perform a device reset and abort all pending SCBs for the
2354  *	victim device
2355  */
2356 int initio_bus_device_reset(struct initio_host * host)
2357 {
2358 	struct scsi_ctrl_blk *scb = host->active;
2359 	struct target_control *active_tc = host->active_tc;
2360 	struct scsi_ctrl_blk *tmp, *prev;
2361 	u8 tar;
2362 
2363 	if (host->phase != MSG_OUT)
2364 		return int_initio_bad_seq(host);	/* Unexpected phase */
2365 
2366 	initio_unlink_pend_scb(host, scb);
2367 	initio_release_scb(host, scb);
2368 
2369 
2370 	tar = scb->target;	/* target                       */
2371 	active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
2372 	/* clr sync. nego & WDTR flags  07/22/98 */
2373 
2374 	/* abort all SCB with same target */
2375 	prev = tmp = host->first_busy;	/* Check Busy queue */
2376 	while (tmp != NULL) {
2377 		if (tmp->target == tar) {
2378 			/* unlink it */
2379 			if (tmp == host->first_busy) {
2380 				if ((host->first_busy = tmp->next) == NULL)
2381 					host->last_busy = NULL;
2382 			} else {
2383 				prev->next = tmp->next;
2384 				if (tmp == host->last_busy)
2385 					host->last_busy = prev;
2386 			}
2387 			tmp->hastat = HOST_ABORTED;
2388 			initio_append_done_scb(host, tmp);
2389 		}
2390 		/* Previous haven't change      */
2391 		else {
2392 			prev = tmp;
2393 		}
2394 		tmp = tmp->next;
2395 	}
2396 	outb(TARGET_RESET, host->addr + TUL_SFifo);
2397 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2398 	return initio_wait_disc(host);
2399 
2400 }
2401 
2402 static int initio_msgin_accept(struct initio_host * host)
2403 {
2404 	outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2405 	return wait_tulip(host);
2406 }
2407 
2408 static int wait_tulip(struct initio_host * host)
2409 {
2410 
2411 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2412 		 & TSS_INT_PENDING))
2413 			cpu_relax();
2414 
2415 	host->jsint = inb(host->addr + TUL_SInt);
2416 	host->phase = host->jsstatus0 & TSS_PH_MASK;
2417 	host->jsstatus1 = inb(host->addr + TUL_SStatus1);
2418 
2419 	if (host->jsint & TSS_RESEL_INT)	/* if SCSI bus reset detected */
2420 		return int_initio_resel(host);
2421 	if (host->jsint & TSS_SEL_TIMEOUT)	/* if selected/reselected timeout interrupt */
2422 		return int_initio_busfree(host);
2423 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected   */
2424 		return int_initio_scsi_rst(host);
2425 
2426 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
2427 		if (host->flags & HCF_EXPECT_DONE_DISC) {
2428 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2429 			initio_unlink_busy_scb(host, host->active);
2430 			host->active->hastat = 0;
2431 			initio_append_done_scb(host, host->active);
2432 			host->active = NULL;
2433 			host->active_tc = NULL;
2434 			host->flags &= ~HCF_EXPECT_DONE_DISC;
2435 			outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2436 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
2437 			return -1;
2438 		}
2439 		if (host->flags & HCF_EXPECT_DISC) {
2440 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2441 			host->active = NULL;
2442 			host->active_tc = NULL;
2443 			host->flags &= ~HCF_EXPECT_DISC;
2444 			outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2445 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
2446 			return -1;
2447 		}
2448 		return int_initio_busfree(host);
2449 	}
2450 	/* The old code really does the below. Can probably be removed */
2451 	if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2452 		return host->phase;
2453 	return host->phase;
2454 }
2455 
2456 static int initio_wait_disc(struct initio_host * host)
2457 {
2458 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2459 		cpu_relax();
2460 
2461 	host->jsint = inb(host->addr + TUL_SInt);
2462 
2463 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected */
2464 		return int_initio_scsi_rst(host);
2465 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection */
2466 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2467 		outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2468 		outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
2469 		host->active = NULL;
2470 		return -1;
2471 	}
2472 	return initio_bad_seq(host);
2473 }
2474 
2475 static int initio_wait_done_disc(struct initio_host * host)
2476 {
2477 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2478 		 & TSS_INT_PENDING))
2479 		 cpu_relax();
2480 
2481 	host->jsint = inb(host->addr + TUL_SInt);
2482 
2483 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected */
2484 		return int_initio_scsi_rst(host);
2485 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection */
2486 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);		/* Flush SCSI FIFO */
2487 		outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2488 		outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);		/* Enable HW reselect */
2489 		initio_unlink_busy_scb(host, host->active);
2490 
2491 		initio_append_done_scb(host, host->active);
2492 		host->active = NULL;
2493 		return -1;
2494 	}
2495 	return initio_bad_seq(host);
2496 }
2497 
2498 /**
2499  *	i91u_intr		-	IRQ handler
2500  *	@irqno: IRQ number
2501  *	@dev_id: IRQ identifier
2502  *
2503  *	Take the relevant locks and then invoke the actual isr processing
2504  *	code under the lock.
2505  */
2506 
2507 static irqreturn_t i91u_intr(int irqno, void *dev_id)
2508 {
2509 	struct Scsi_Host *dev = dev_id;
2510 	unsigned long flags;
2511 	int r;
2512 
2513 	spin_lock_irqsave(dev->host_lock, flags);
2514 	r = initio_isr((struct initio_host *)dev->hostdata);
2515 	spin_unlock_irqrestore(dev->host_lock, flags);
2516 	if (r)
2517 		return IRQ_HANDLED;
2518 	else
2519 		return IRQ_NONE;
2520 }
2521 
2522 
2523 /**
2524  *	initio_build_scb		-	Build the mappings and SCB
2525  *	@host: InitIO host taking the command
2526  *	@cblk: Firmware command block
2527  *	@cmnd: SCSI midlayer command block
2528  *
2529  *	Translate the abstract SCSI command into a firmware command block
2530  *	suitable for feeding to the InitIO host controller. This also requires
2531  *	we build the scatter gather lists and ensure they are mapped properly.
2532  */
2533 
2534 static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
2535 {				/* Create corresponding SCB     */
2536 	struct scatterlist *sglist;
2537 	struct sg_entry *sg;		/* Pointer to SG list           */
2538 	int i, nseg;
2539 	long total_len;
2540 	dma_addr_t dma_addr;
2541 
2542 	/* Fill in the command headers */
2543 	cblk->post = i91uSCBPost;	/* i91u's callback routine      */
2544 	cblk->srb = cmnd;
2545 	cblk->opcode = ExecSCSI;
2546 	cblk->flags = SCF_POST;	/* After SCSI done, call post routine */
2547 	cblk->target = cmnd->device->id;
2548 	cblk->lun = cmnd->device->lun;
2549 	cblk->ident = cmnd->device->lun | DISC_ALLOW;
2550 
2551 	cblk->flags |= SCF_SENSE;	/* Turn on auto request sense   */
2552 
2553 	/* Map the sense buffer into bus memory */
2554 	dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
2555 				  SENSE_SIZE, DMA_FROM_DEVICE);
2556 	cblk->senseptr = (u32)dma_addr;
2557 	cblk->senselen = SENSE_SIZE;
2558 	cmnd->SCp.ptr = (char *)(unsigned long)dma_addr;
2559 	cblk->cdblen = cmnd->cmd_len;
2560 
2561 	/* Clear the returned status */
2562 	cblk->hastat = 0;
2563 	cblk->tastat = 0;
2564 	/* Command the command */
2565 	memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
2566 
2567 	/* Set up tags */
2568 	if (cmnd->device->tagged_supported) {	/* Tag Support                  */
2569 		cblk->tagmsg = SIMPLE_QUEUE_TAG;	/* Do simple tag only   */
2570 	} else {
2571 		cblk->tagmsg = 0;	/* No tag support               */
2572 	}
2573 
2574 	/* todo handle map_sg error */
2575 	nseg = scsi_dma_map(cmnd);
2576 	BUG_ON(nseg < 0);
2577 	if (nseg) {
2578 		dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2579 					  sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2580 					  DMA_BIDIRECTIONAL);
2581 		cblk->bufptr = (u32)dma_addr;
2582 		cmnd->SCp.dma_handle = dma_addr;
2583 
2584 		cblk->sglen = nseg;
2585 
2586 		cblk->flags |= SCF_SG;	/* Turn on SG list flag       */
2587 		total_len = 0;
2588 		sg = &cblk->sglist[0];
2589 		scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
2590 			sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
2591 			sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2592 			total_len += sg_dma_len(sglist);
2593 			++sg;
2594 		}
2595 
2596 		cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2597 			total_len : scsi_bufflen(cmnd);
2598 	} else {	/* No data transfer required */
2599 		cblk->buflen = 0;
2600 		cblk->sglen = 0;
2601 	}
2602 }
2603 
2604 /**
2605  *	i91u_queuecommand	-	Queue a new command if possible
2606  *	@cmd: SCSI command block from the mid layer
2607  *	@done: Completion handler
2608  *
2609  *	Attempts to queue a new command with the host adapter. Will return
2610  *	zero if successful or indicate a host busy condition if not (which
2611  *	will cause the mid layer to call us again later with the command)
2612  */
2613 
2614 static int i91u_queuecommand_lck(struct scsi_cmnd *cmd,
2615 		void (*done)(struct scsi_cmnd *))
2616 {
2617 	struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2618 	struct scsi_ctrl_blk *cmnd;
2619 
2620 	cmd->scsi_done = done;
2621 
2622 	cmnd = initio_alloc_scb(host);
2623 	if (!cmnd)
2624 		return SCSI_MLQUEUE_HOST_BUSY;
2625 
2626 	initio_build_scb(host, cmnd, cmd);
2627 	initio_exec_scb(host, cmnd);
2628 	return 0;
2629 }
2630 
2631 static DEF_SCSI_QCMD(i91u_queuecommand)
2632 
2633 /**
2634  *	i91u_bus_reset		-	reset the SCSI bus
2635  *	@cmnd: Command block we want to trigger the reset for
2636  *
2637  *	Initiate a SCSI bus reset sequence
2638  */
2639 
2640 static int i91u_bus_reset(struct scsi_cmnd * cmnd)
2641 {
2642 	struct initio_host *host;
2643 
2644 	host = (struct initio_host *) cmnd->device->host->hostdata;
2645 
2646 	spin_lock_irq(cmnd->device->host->host_lock);
2647 	initio_reset_scsi(host, 0);
2648 	spin_unlock_irq(cmnd->device->host->host_lock);
2649 
2650 	return SUCCESS;
2651 }
2652 
2653 /**
2654  *	i91u_biospararm			-	return the "logical geometry
2655  *	@sdev: SCSI device
2656  *	@dev; Matching block device
2657  *	@capacity: Sector size of drive
2658  *	@info_array: Return space for BIOS geometry
2659  *
2660  *	Map the device geometry in a manner compatible with the host
2661  *	controller BIOS behaviour.
2662  *
2663  *	FIXME: limited to 2^32 sector devices.
2664  */
2665 
2666 static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2667 		sector_t capacity, int *info_array)
2668 {
2669 	struct initio_host *host;		/* Point to Host adapter control block */
2670 	struct target_control *tc;
2671 
2672 	host = (struct initio_host *) sdev->host->hostdata;
2673 	tc = &host->targets[sdev->id];
2674 
2675 	if (tc->heads) {
2676 		info_array[0] = tc->heads;
2677 		info_array[1] = tc->sectors;
2678 		info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
2679 	} else {
2680 		if (tc->drv_flags & TCF_DRV_255_63) {
2681 			info_array[0] = 255;
2682 			info_array[1] = 63;
2683 			info_array[2] = (unsigned long)capacity / 255 / 63;
2684 		} else {
2685 			info_array[0] = 64;
2686 			info_array[1] = 32;
2687 			info_array[2] = (unsigned long)capacity >> 11;
2688 		}
2689 	}
2690 
2691 #if defined(DEBUG_BIOSPARAM)
2692 	if (i91u_debug & debug_biosparam) {
2693 		printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2694 		       info_array[0], info_array[1], info_array[2]);
2695 		printk("WARNING: check, if the bios geometry is correct.\n");
2696 	}
2697 #endif
2698 
2699 	return 0;
2700 }
2701 
2702 /**
2703  *	i91u_unmap_scb		-	Unmap a command
2704  *	@pci_dev: PCI device the command is for
2705  *	@cmnd: The command itself
2706  *
2707  *	Unmap any PCI mapping/IOMMU resources allocated when the command
2708  *	was mapped originally as part of initio_build_scb
2709  */
2710 
2711 static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
2712 {
2713 	/* auto sense buffer */
2714 	if (cmnd->SCp.ptr) {
2715 		dma_unmap_single(&pci_dev->dev,
2716 				 (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
2717 				 SENSE_SIZE, DMA_FROM_DEVICE);
2718 		cmnd->SCp.ptr = NULL;
2719 	}
2720 
2721 	/* request buffer */
2722 	if (scsi_sg_count(cmnd)) {
2723 		dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
2724 				 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2725 				 DMA_BIDIRECTIONAL);
2726 
2727 		scsi_dma_unmap(cmnd);
2728 	}
2729 }
2730 
2731 /**
2732  *	i91uSCBPost		-	SCSI callback
2733  *	@host: Pointer to host adapter control block.
2734  *	@cmnd: Pointer to SCSI control block.
2735  *
2736  *	This is callback routine be called when tulip finish one
2737  *	SCSI command.
2738  */
2739 
2740 static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
2741 {
2742 	struct scsi_cmnd *cmnd;	/* Pointer to SCSI request block */
2743 	struct initio_host *host;
2744 	struct scsi_ctrl_blk *cblk;
2745 
2746 	host = (struct initio_host *) host_mem;
2747 	cblk = (struct scsi_ctrl_blk *) cblk_mem;
2748 	if ((cmnd = cblk->srb) == NULL) {
2749 		printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2750 		WARN_ON(1);
2751 		initio_release_scb(host, cblk);	/* Release SCB for current channel */
2752 		return;
2753 	}
2754 
2755 	/*
2756 	 *	Remap the firmware error status into a mid layer one
2757 	 */
2758 	switch (cblk->hastat) {
2759 	case 0x0:
2760 	case 0xa:		/* Linked command complete without error and linked normally */
2761 	case 0xb:		/* Linked command complete without error interrupt generated */
2762 		cblk->hastat = 0;
2763 		break;
2764 
2765 	case 0x11:		/* Selection time out-The initiator selection or target
2766 				   reselection was not complete within the SCSI Time out period */
2767 		cblk->hastat = DID_TIME_OUT;
2768 		break;
2769 
2770 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2771 				   phase sequence was requested by the target. The host adapter
2772 				   will generate a SCSI Reset Condition, notifying the host with
2773 				   a SCRD interrupt */
2774 		cblk->hastat = DID_RESET;
2775 		break;
2776 
2777 	case 0x1a:		/* SCB Aborted. 07/21/98 */
2778 		cblk->hastat = DID_ABORT;
2779 		break;
2780 
2781 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
2782 				   than was allocated by the Data Length field or the sum of the
2783 				   Scatter / Gather Data Length fields. */
2784 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2785 	case 0x16:		/* Invalid SCB Operation Code. */
2786 
2787 	default:
2788 		printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2789 		cblk->hastat = DID_ERROR;	/* Couldn't find any better */
2790 		break;
2791 	}
2792 
2793 	cmnd->result = cblk->tastat | (cblk->hastat << 16);
2794 	i91u_unmap_scb(host->pci_dev, cmnd);
2795 	cmnd->scsi_done(cmnd);	/* Notify system DONE           */
2796 	initio_release_scb(host, cblk);	/* Release SCB for current channel */
2797 }
2798 
2799 static struct scsi_host_template initio_template = {
2800 	.proc_name		= "INI9100U",
2801 	.name			= "Initio INI-9X00U/UW SCSI device driver",
2802 	.queuecommand		= i91u_queuecommand,
2803 	.eh_bus_reset_handler	= i91u_bus_reset,
2804 	.bios_param		= i91u_biosparam,
2805 	.can_queue		= MAX_TARGETS * i91u_MAXQUEUE,
2806 	.this_id		= 1,
2807 	.sg_tablesize		= SG_ALL,
2808 };
2809 
2810 static int initio_probe_one(struct pci_dev *pdev,
2811 	const struct pci_device_id *id)
2812 {
2813 	struct Scsi_Host *shost;
2814 	struct initio_host *host;
2815 	u32 reg;
2816 	u16 bios_seg;
2817 	struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
2818 	int num_scb, i, error;
2819 
2820 	error = pci_enable_device(pdev);
2821 	if (error)
2822 		return error;
2823 
2824 	pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2825 	bios_seg = (u16) (reg & 0xFF);
2826 	if (((reg & 0xFF00) >> 8) == 0xFF)
2827 		reg = 0;
2828 	bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2829 
2830 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2831 		printk(KERN_WARNING  "i91u: Could not set 32 bit DMA mask\n");
2832 		error = -ENODEV;
2833 		goto out_disable_device;
2834 	}
2835 	shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2836 	if (!shost) {
2837 		printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2838 		error = -ENOMEM;
2839 		goto out_disable_device;
2840 	}
2841 	host = (struct initio_host *)shost->hostdata;
2842 	memset(host, 0, sizeof(struct initio_host));
2843 	host->addr = pci_resource_start(pdev, 0);
2844 	host->bios_addr = bios_seg;
2845 
2846 	if (!request_region(host->addr, 256, "i91u")) {
2847 		printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2848 		error = -ENODEV;
2849 		goto out_host_put;
2850 	}
2851 
2852 	if (initio_tag_enable)	/* 1.01i */
2853 		num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2854 	else
2855 		num_scb = MAX_TARGETS + 3;	/* 1-tape, 1-CD_ROM, 1- extra */
2856 
2857 	for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2858 		i = num_scb * sizeof(struct scsi_ctrl_blk);
2859 		if ((scb = kzalloc(i, GFP_DMA)) != NULL)
2860 			break;
2861 	}
2862 
2863 	if (!scb) {
2864 		printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2865 		error = -ENOMEM;
2866 		goto out_release_region;
2867 	}
2868 
2869 	host->pci_dev = pdev;
2870 
2871 	host->semaph = 1;
2872 	spin_lock_init(&host->semaph_lock);
2873 	host->num_scbs = num_scb;
2874 	host->scb = scb;
2875 	host->next_pending = scb;
2876 	host->next_avail = scb;
2877 	for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2878 		tmp->tagid = i;
2879 		if (i != 0)
2880 			prev->next = tmp;
2881 		prev = tmp;
2882 	}
2883 	prev->next = NULL;
2884 	host->scb_end = tmp;
2885 	host->first_avail = scb;
2886 	host->last_avail = prev;
2887 	spin_lock_init(&host->avail_lock);
2888 
2889 	initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
2890 
2891 	host->jsstatus0 = 0;
2892 
2893 	shost->io_port = host->addr;
2894 	shost->n_io_port = 0xff;
2895 	shost->can_queue = num_scb;		/* 03/05/98                      */
2896 	shost->unique_id = host->addr;
2897 	shost->max_id = host->max_tar;
2898 	shost->max_lun = 32;	/* 10/21/97                     */
2899 	shost->irq = pdev->irq;
2900 	shost->this_id = host->scsi_id;	/* Assign HCS index           */
2901 	shost->base = host->addr;
2902 	shost->sg_tablesize = TOTAL_SG_ENTRY;
2903 
2904 	error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
2905 	if (error < 0) {
2906 		printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2907 		goto out_free_scbs;
2908 	}
2909 
2910 	pci_set_drvdata(pdev, shost);
2911 
2912 	error = scsi_add_host(shost, &pdev->dev);
2913 	if (error)
2914 		goto out_free_irq;
2915 	scsi_scan_host(shost);
2916 	return 0;
2917 out_free_irq:
2918 	free_irq(pdev->irq, shost);
2919 out_free_scbs:
2920 	kfree(host->scb);
2921 out_release_region:
2922 	release_region(host->addr, 256);
2923 out_host_put:
2924 	scsi_host_put(shost);
2925 out_disable_device:
2926 	pci_disable_device(pdev);
2927 	return error;
2928 }
2929 
2930 /**
2931  *	initio_remove_one	-	control shutdown
2932  *	@pdev:	PCI device being released
2933  *
2934  *	Release the resources assigned to this adapter after it has
2935  *	finished being used.
2936  */
2937 
2938 static void initio_remove_one(struct pci_dev *pdev)
2939 {
2940 	struct Scsi_Host *host = pci_get_drvdata(pdev);
2941 	struct initio_host *s = (struct initio_host *)host->hostdata;
2942 	scsi_remove_host(host);
2943 	free_irq(pdev->irq, host);
2944 	release_region(s->addr, 256);
2945 	scsi_host_put(host);
2946 	pci_disable_device(pdev);
2947 }
2948 
2949 MODULE_LICENSE("GPL");
2950 
2951 static struct pci_device_id initio_pci_tbl[] = {
2952 	{PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2953 	{PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2954 	{PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2955 	{PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2956 	{PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2957 	{0,}
2958 };
2959 MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
2960 
2961 static struct pci_driver initio_pci_driver = {
2962 	.name		= "initio",
2963 	.id_table	= initio_pci_tbl,
2964 	.probe		= initio_probe_one,
2965 	.remove		= initio_remove_one,
2966 };
2967 module_pci_driver(initio_pci_driver);
2968 
2969 MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
2970 MODULE_AUTHOR("Initio Corporation");
2971 MODULE_LICENSE("GPL");
2972