xref: /linux-6.15/drivers/scsi/initio.c (revision 78e40ac8)
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 <[email protected]>
7  * Copyright (c) 2004 Christoph Hellwig <[email protected]>
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 <[email protected]>
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;
870 	u16 scbp_tarlun;
871 
872 
873 	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 		tmp = tmp->next;
880 	}
881 #if DEBUG_QUEUE
882 	printk("find busy SCB %p; ", tmp);
883 #endif
884 	return tmp;
885 }
886 
887 static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
888 {
889 #if DEBUG_QUEUE
890 	printk("append done SCB %p; ", scbp);
891 #endif
892 
893 	scbp->status = SCB_DONE;
894 	scbp->next = NULL;
895 	if (host->last_done != NULL) {
896 		host->last_done->next = scbp;
897 		host->last_done = scbp;
898 	} else {
899 		host->first_done = scbp;
900 		host->last_done = scbp;
901 	}
902 }
903 
904 struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
905 {
906 	struct scsi_ctrl_blk *tmp;
907 
908 	if ((tmp = host->first_done) != NULL) {
909 		if ((host->first_done = tmp->next) == NULL)
910 			host->last_done = NULL;
911 		tmp->next = NULL;
912 	}
913 #if DEBUG_QUEUE
914 	printk("find done SCB %p; ",tmp);
915 #endif
916 	return tmp;
917 }
918 
919 static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
920 {
921 	unsigned long flags;
922 	struct scsi_ctrl_blk *tmp, *prev;
923 
924 	spin_lock_irqsave(&host->semaph_lock, flags);
925 
926 	if ((host->semaph == 0) && (host->active == NULL)) {
927 		/* disable Jasmin SCSI Int        */
928 		outb(0x1F, host->addr + TUL_Mask);
929 		spin_unlock_irqrestore(&host->semaph_lock, flags);
930 		/* FIXME: synchronize_irq needed ? */
931 		tulip_main(host);
932 		spin_lock_irqsave(&host->semaph_lock, flags);
933 		host->semaph = 1;
934 		outb(0x0F, host->addr + TUL_Mask);
935 		spin_unlock_irqrestore(&host->semaph_lock, flags);
936 		return SCSI_ABORT_SNOOZE;
937 	}
938 	prev = tmp = host->first_pending;	/* Check Pend queue */
939 	while (tmp != NULL) {
940 		/* 07/27/98 */
941 		if (tmp->srb == srbp) {
942 			if (tmp == host->active) {
943 				spin_unlock_irqrestore(&host->semaph_lock, flags);
944 				return SCSI_ABORT_BUSY;
945 			} else if (tmp == host->first_pending) {
946 				if ((host->first_pending = tmp->next) == NULL)
947 					host->last_pending = NULL;
948 			} else {
949 				prev->next = tmp->next;
950 				if (tmp == host->last_pending)
951 					host->last_pending = prev;
952 			}
953 			tmp->hastat = HOST_ABORTED;
954 			tmp->flags |= SCF_DONE;
955 			if (tmp->flags & SCF_POST)
956 				(*tmp->post) ((u8 *) host, (u8 *) tmp);
957 			spin_unlock_irqrestore(&host->semaph_lock, flags);
958 			return SCSI_ABORT_SUCCESS;
959 		}
960 		prev = tmp;
961 		tmp = tmp->next;
962 	}
963 
964 	prev = tmp = host->first_busy;	/* Check Busy queue */
965 	while (tmp != NULL) {
966 		if (tmp->srb == srbp) {
967 			if (tmp == host->active) {
968 				spin_unlock_irqrestore(&host->semaph_lock, flags);
969 				return SCSI_ABORT_BUSY;
970 			} else if (tmp->tagmsg == 0) {
971 				spin_unlock_irqrestore(&host->semaph_lock, flags);
972 				return SCSI_ABORT_BUSY;
973 			} else {
974 				host->act_tags[tmp->target]--;
975 				if (tmp == host->first_busy) {
976 					if ((host->first_busy = tmp->next) == NULL)
977 						host->last_busy = NULL;
978 				} else {
979 					prev->next = tmp->next;
980 					if (tmp == host->last_busy)
981 						host->last_busy = prev;
982 				}
983 				tmp->next = NULL;
984 
985 
986 				tmp->hastat = HOST_ABORTED;
987 				tmp->flags |= SCF_DONE;
988 				if (tmp->flags & SCF_POST)
989 					(*tmp->post) ((u8 *) host, (u8 *) tmp);
990 				spin_unlock_irqrestore(&host->semaph_lock, flags);
991 				return SCSI_ABORT_SUCCESS;
992 			}
993 		}
994 		prev = tmp;
995 		tmp = tmp->next;
996 	}
997 	spin_unlock_irqrestore(&host->semaph_lock, flags);
998 	return SCSI_ABORT_NOT_RUNNING;
999 }
1000 
1001 /***************************************************************************/
1002 static int initio_bad_seq(struct initio_host * host)
1003 {
1004 	struct scsi_ctrl_blk *scb;
1005 
1006 	printk("initio_bad_seg c=%d\n", host->index);
1007 
1008 	if ((scb = host->active) != NULL) {
1009 		initio_unlink_busy_scb(host, scb);
1010 		scb->hastat = HOST_BAD_PHAS;
1011 		scb->tastat = 0;
1012 		initio_append_done_scb(host, scb);
1013 	}
1014 	initio_stop_bm(host);
1015 	initio_reset_scsi(host, 8);	/* 7/29/98 */
1016 	return initio_post_scsi_rst(host);
1017 }
1018 
1019 
1020 /************************************************************************/
1021 static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
1022 {
1023 	unsigned long flags;
1024 
1025 	scb->mode = 0;
1026 
1027 	scb->sgidx = 0;
1028 	scb->sgmax = scb->sglen;
1029 
1030 	spin_lock_irqsave(&host->semaph_lock, flags);
1031 
1032 	initio_append_pend_scb(host, scb);	/* Append this SCB to Pending queue */
1033 
1034 /* VVVVV 07/21/98 */
1035 	if (host->semaph == 1) {
1036 		/* Disable Jasmin SCSI Int */
1037 		outb(0x1F, host->addr + TUL_Mask);
1038 		host->semaph = 0;
1039 		spin_unlock_irqrestore(&host->semaph_lock, flags);
1040 
1041 		tulip_main(host);
1042 
1043 		spin_lock_irqsave(&host->semaph_lock, flags);
1044 		host->semaph = 1;
1045 		outb(0x0F, host->addr + TUL_Mask);
1046 	}
1047 	spin_unlock_irqrestore(&host->semaph_lock, flags);
1048 	return;
1049 }
1050 
1051 /***************************************************************************/
1052 static int initio_isr(struct initio_host * host)
1053 {
1054 	if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
1055 		if (host->semaph == 1) {
1056 			outb(0x1F, host->addr + TUL_Mask);
1057 			/* Disable Tulip SCSI Int */
1058 			host->semaph = 0;
1059 
1060 			tulip_main(host);
1061 
1062 			host->semaph = 1;
1063 			outb(0x0F, host->addr + TUL_Mask);
1064 			return 1;
1065 		}
1066 	}
1067 	return 0;
1068 }
1069 
1070 static int tulip_main(struct initio_host * host)
1071 {
1072 	struct scsi_ctrl_blk *scb;
1073 
1074 	for (;;) {
1075 		tulip_scsi(host);	/* Call tulip_scsi              */
1076 
1077 		/* Walk the list of completed SCBs */
1078 		while ((scb = initio_find_done_scb(host)) != NULL) {	/* find done entry */
1079 			if (scb->tastat == INI_QUEUE_FULL) {
1080 				host->max_tags[scb->target] =
1081 				    host->act_tags[scb->target] - 1;
1082 				scb->tastat = 0;
1083 				initio_append_pend_scb(host, scb);
1084 				continue;
1085 			}
1086 			if (!(scb->mode & SCM_RSENS)) {		/* not in auto req. sense mode */
1087 				if (scb->tastat == 2) {
1088 
1089 					/* clr sync. nego flag */
1090 
1091 					if (scb->flags & SCF_SENSE) {
1092 						u8 len;
1093 						len = scb->senselen;
1094 						if (len == 0)
1095 							len = 1;
1096 						scb->buflen = scb->senselen;
1097 						scb->bufptr = scb->senseptr;
1098 						scb->flags &= ~(SCF_SG | SCF_DIR);	/* for xfer_data_in */
1099 						/* so, we won't report wrong direction in xfer_data_in,
1100 						   and won't report HOST_DO_DU in state_6 */
1101 						scb->mode = SCM_RSENS;
1102 						scb->ident &= 0xBF;	/* Disable Disconnect */
1103 						scb->tagmsg = 0;
1104 						scb->tastat = 0;
1105 						scb->cdblen = 6;
1106 						scb->cdb[0] = SCSICMD_RequestSense;
1107 						scb->cdb[1] = 0;
1108 						scb->cdb[2] = 0;
1109 						scb->cdb[3] = 0;
1110 						scb->cdb[4] = len;
1111 						scb->cdb[5] = 0;
1112 						initio_push_pend_scb(host, scb);
1113 						break;
1114 					}
1115 				}
1116 			} else {	/* in request sense mode */
1117 
1118 				if (scb->tastat == 2) {		/* check contition status again after sending
1119 									   requset sense cmd 0x3 */
1120 					scb->hastat = HOST_BAD_PHAS;
1121 				}
1122 				scb->tastat = 2;
1123 			}
1124 			scb->flags |= SCF_DONE;
1125 			if (scb->flags & SCF_POST) {
1126 				/* FIXME: only one post method and lose casts */
1127 				(*scb->post) ((u8 *) host, (u8 *) scb);
1128 			}
1129 		}		/* while */
1130 		/* find_active: */
1131 		if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
1132 			continue;
1133 		if (host->active)	/* return to OS and wait for xfer_done_ISR/Selected_ISR */
1134 			return 1;	/* return to OS, enable interrupt */
1135 		/* Check pending SCB            */
1136 		if (initio_find_first_pend_scb(host) == NULL)
1137 			return 1;	/* return to OS, enable interrupt */
1138 	}			/* End of for loop */
1139 	/* statement won't reach here */
1140 }
1141 
1142 static void tulip_scsi(struct initio_host * host)
1143 {
1144 	struct scsi_ctrl_blk *scb;
1145 	struct target_control *active_tc;
1146 
1147 	/* make sure to service interrupt asap */
1148 	if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
1149 		host->phase = host->jsstatus0 & TSS_PH_MASK;
1150 		host->jsstatus1 = inb(host->addr + TUL_SStatus1);
1151 		host->jsint = inb(host->addr + TUL_SInt);
1152 		if (host->jsint & TSS_SCSIRST_INT) {	/* SCSI bus reset detected      */
1153 			int_initio_scsi_rst(host);
1154 			return;
1155 		}
1156 		if (host->jsint & TSS_RESEL_INT) {	/* if selected/reselected interrupt */
1157 			if (int_initio_resel(host) == 0)
1158 				initio_next_state(host);
1159 			return;
1160 		}
1161 		if (host->jsint & TSS_SEL_TIMEOUT) {
1162 			int_initio_busfree(host);
1163 			return;
1164 		}
1165 		if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
1166 			int_initio_busfree(host);	/* unexpected bus free or sel timeout */
1167 			return;
1168 		}
1169 		if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) {	/* func complete or Bus service */
1170 			if ((scb = host->active) != NULL)
1171 				initio_next_state(host);
1172 			return;
1173 		}
1174 	}
1175 	if (host->active != NULL)
1176 		return;
1177 
1178 	if ((scb = initio_find_first_pend_scb(host)) == NULL)
1179 		return;
1180 
1181 	/* program HBA's SCSI ID & target SCSI ID */
1182 	outb((host->scsi_id << 4) | (scb->target & 0x0F),
1183 		host->addr + TUL_SScsiId);
1184 	if (scb->opcode == ExecSCSI) {
1185 		active_tc = &host->targets[scb->target];
1186 
1187 		if (scb->tagmsg)
1188 			active_tc->drv_flags |= TCF_DRV_EN_TAG;
1189 		else
1190 			active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
1191 
1192 		outb(active_tc->js_period, host->addr + TUL_SPeriod);
1193 		if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {	/* do wdtr negotiation          */
1194 			initio_select_atn_stop(host, scb);
1195 		} else {
1196 			if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {	/* do sync negotiation          */
1197 				initio_select_atn_stop(host, scb);
1198 			} else {
1199 				if (scb->tagmsg)
1200 					initio_select_atn3(host, scb);
1201 				else
1202 					initio_select_atn(host, scb);
1203 			}
1204 		}
1205 		if (scb->flags & SCF_POLL) {
1206 			while (wait_tulip(host) != -1) {
1207 				if (initio_next_state(host) == -1)
1208 					break;
1209 			}
1210 		}
1211 	} else if (scb->opcode == BusDevRst) {
1212 		initio_select_atn_stop(host, scb);
1213 		scb->next_state = 8;
1214 		if (scb->flags & SCF_POLL) {
1215 			while (wait_tulip(host) != -1) {
1216 				if (initio_next_state(host) == -1)
1217 					break;
1218 			}
1219 		}
1220 	} else if (scb->opcode == AbortCmd) {
1221 		if (initio_abort_srb(host, scb->srb) != 0) {
1222 			initio_unlink_pend_scb(host, scb);
1223 			initio_release_scb(host, scb);
1224 		} else {
1225 			scb->opcode = BusDevRst;
1226 			initio_select_atn_stop(host, scb);
1227 			scb->next_state = 8;
1228 		}
1229 	} else {
1230 		initio_unlink_pend_scb(host, scb);
1231 		scb->hastat = 0x16;	/* bad command */
1232 		initio_append_done_scb(host, scb);
1233 	}
1234 	return;
1235 }
1236 
1237 /**
1238  *	initio_next_state		-	Next SCSI state
1239  *	@host: InitIO host we are processing
1240  *
1241  *	Progress the active command block along the state machine
1242  *	until we hit a state which we must wait for activity to occur.
1243  *
1244  *	Returns zero or a negative code.
1245  */
1246 
1247 static int initio_next_state(struct initio_host * host)
1248 {
1249 	int next;
1250 
1251 	next = host->active->next_state;
1252 	for (;;) {
1253 		switch (next) {
1254 		case 1:
1255 			next = initio_state_1(host);
1256 			break;
1257 		case 2:
1258 			next = initio_state_2(host);
1259 			break;
1260 		case 3:
1261 			next = initio_state_3(host);
1262 			break;
1263 		case 4:
1264 			next = initio_state_4(host);
1265 			break;
1266 		case 5:
1267 			next = initio_state_5(host);
1268 			break;
1269 		case 6:
1270 			next = initio_state_6(host);
1271 			break;
1272 		case 7:
1273 			next = initio_state_7(host);
1274 			break;
1275 		case 8:
1276 			return initio_bus_device_reset(host);
1277 		default:
1278 			return initio_bad_seq(host);
1279 		}
1280 		if (next <= 0)
1281 			return next;
1282 	}
1283 }
1284 
1285 
1286 /**
1287  *	initio_state_1		-	SCSI state machine
1288  *	@host: InitIO host we are controlling
1289  *
1290  *	Perform SCSI state processing for Select/Attention/Stop
1291  */
1292 
1293 static int initio_state_1(struct initio_host * host)
1294 {
1295 	struct scsi_ctrl_blk *scb = host->active;
1296 	struct target_control *active_tc = host->active_tc;
1297 #if DEBUG_STATE
1298 	printk("-s1-");
1299 #endif
1300 
1301 	/* Move the SCB from pending to busy */
1302 	initio_unlink_pend_scb(host, scb);
1303 	initio_append_busy_scb(host, scb);
1304 
1305 	outb(active_tc->sconfig0, host->addr + TUL_SConfig );
1306 	/* ATN on */
1307 	if (host->phase == MSG_OUT) {
1308 		outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1309 		outb(scb->ident, host->addr + TUL_SFifo);
1310 
1311 		if (scb->tagmsg) {
1312 			outb(scb->tagmsg, host->addr + TUL_SFifo);
1313 			outb(scb->tagid, host->addr + TUL_SFifo);
1314 		}
1315 		if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
1316 			active_tc->flags |= TCF_WDTR_DONE;
1317 			outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1318 			outb(2, host->addr + TUL_SFifo);	/* Extended msg length */
1319 			outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* Sync request */
1320 			outb(1, host->addr + TUL_SFifo);	/* Start from 16 bits */
1321 		} else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
1322 			active_tc->flags |= TCF_SYNC_DONE;
1323 			outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1324 			outb(3, host->addr + TUL_SFifo);	/* extended msg length */
1325 			outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* sync request */
1326 			outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1327 			outb(MAX_OFFSET, host->addr + TUL_SFifo);	/* REQ/ACK offset */
1328 		}
1329 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1330 		if (wait_tulip(host) == -1)
1331 			return -1;
1332 	}
1333 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1334 	outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1335 	/* Into before CDB xfer */
1336 	return 3;
1337 }
1338 
1339 
1340 /**
1341  *	initio_state_2		-	SCSI state machine
1342  *	@host: InitIO host we are controlling
1343  *
1344  * state after selection with attention
1345  * state after selection with attention3
1346  */
1347 
1348 static int initio_state_2(struct initio_host * host)
1349 {
1350 	struct scsi_ctrl_blk *scb = host->active;
1351 	struct target_control *active_tc = host->active_tc;
1352 #if DEBUG_STATE
1353 	printk("-s2-");
1354 #endif
1355 
1356 	initio_unlink_pend_scb(host, scb);
1357 	initio_append_busy_scb(host, scb);
1358 
1359 	outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1360 
1361 	if (host->jsstatus1 & TSS_CMD_PH_CMP)
1362 		return 4;
1363 
1364 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1365 	outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
1366 	/* Into before CDB xfer */
1367 	return 3;
1368 }
1369 
1370 /**
1371  *	initio_state_3		-	SCSI state machine
1372  *	@host: InitIO host we are controlling
1373  *
1374  * state before CDB xfer is done
1375  */
1376 
1377 static int initio_state_3(struct initio_host * host)
1378 {
1379 	struct scsi_ctrl_blk *scb = host->active;
1380 	struct target_control *active_tc = host->active_tc;
1381 	int i;
1382 
1383 #if DEBUG_STATE
1384 	printk("-s3-");
1385 #endif
1386 	for (;;) {
1387 		switch (host->phase) {
1388 		case CMD_OUT:	/* Command out phase            */
1389 			for (i = 0; i < (int) scb->cdblen; i++)
1390 				outb(scb->cdb[i], host->addr + TUL_SFifo);
1391 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1392 			if (wait_tulip(host) == -1)
1393 				return -1;
1394 			if (host->phase == CMD_OUT)
1395 				return initio_bad_seq(host);
1396 			return 4;
1397 
1398 		case MSG_IN:	/* Message in phase             */
1399 			scb->next_state = 3;
1400 			if (initio_msgin(host) == -1)
1401 				return -1;
1402 			break;
1403 
1404 		case STATUS_IN:	/* Status phase                 */
1405 			if (initio_status_msg(host) == -1)
1406 				return -1;
1407 			break;
1408 
1409 		case MSG_OUT:	/* Message out phase            */
1410 			if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
1411 				outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
1412 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1413 				if (wait_tulip(host) == -1)
1414 					return -1;
1415 			} else {
1416 				active_tc->flags |= TCF_SYNC_DONE;
1417 
1418 				outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
1419 				outb(3, host->addr + TUL_SFifo);	/* ext. msg len */
1420 				outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* sync request */
1421 				outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
1422 				outb(MAX_OFFSET, host->addr + TUL_SFifo);	/* REQ/ACK offset */
1423 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1424 				if (wait_tulip(host) == -1)
1425 					return -1;
1426 				outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1427 				outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
1428 
1429 			}
1430 			break;
1431 		default:
1432 			return initio_bad_seq(host);
1433 		}
1434 	}
1435 }
1436 
1437 /**
1438  *	initio_state_4		-	SCSI state machine
1439  *	@host: InitIO host we are controlling
1440  *
1441  *	SCSI state machine. State 4
1442  */
1443 
1444 static int initio_state_4(struct initio_host * host)
1445 {
1446 	struct scsi_ctrl_blk *scb = host->active;
1447 
1448 #if DEBUG_STATE
1449 	printk("-s4-");
1450 #endif
1451 	if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
1452 		return 6;	/* Go to state 6 (After data) */
1453 	}
1454 	for (;;) {
1455 		if (scb->buflen == 0)
1456 			return 6;
1457 
1458 		switch (host->phase) {
1459 
1460 		case STATUS_IN:	/* Status phase                 */
1461 			if ((scb->flags & SCF_DIR) != 0)	/* if direction bit set then report data underrun */
1462 				scb->hastat = HOST_DO_DU;
1463 			if ((initio_status_msg(host)) == -1)
1464 				return -1;
1465 			break;
1466 
1467 		case MSG_IN:	/* Message in phase             */
1468 			scb->next_state = 0x4;
1469 			if (initio_msgin(host) == -1)
1470 				return -1;
1471 			break;
1472 
1473 		case MSG_OUT:	/* Message out phase            */
1474 			if (host->jsstatus0 & TSS_PAR_ERROR) {
1475 				scb->buflen = 0;
1476 				scb->hastat = HOST_DO_DU;
1477 				if (initio_msgout_ide(host) == -1)
1478 					return -1;
1479 				return 6;
1480 			} else {
1481 				outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
1482 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1483 				if (wait_tulip(host) == -1)
1484 					return -1;
1485 			}
1486 			break;
1487 
1488 		case DATA_IN:	/* Data in phase                */
1489 			return initio_xfer_data_in(host);
1490 
1491 		case DATA_OUT:	/* Data out phase               */
1492 			return initio_xfer_data_out(host);
1493 
1494 		default:
1495 			return initio_bad_seq(host);
1496 		}
1497 	}
1498 }
1499 
1500 
1501 /**
1502  *	initio_state_5		-	SCSI state machine
1503  *	@host: InitIO host we are controlling
1504  *
1505  *	State after dma xfer done or phase change before xfer done
1506  */
1507 
1508 static int initio_state_5(struct initio_host * host)
1509 {
1510 	struct scsi_ctrl_blk *scb = host->active;
1511 	long cnt, xcnt;		/* cannot use unsigned !! code: if (xcnt < 0) */
1512 
1513 #if DEBUG_STATE
1514 	printk("-s5-");
1515 #endif
1516 	/*------ get remaining count -------*/
1517 	cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
1518 
1519 	if (inb(host->addr + TUL_XCmd) & 0x20) {
1520 		/* ----------------------- DATA_IN ----------------------------- */
1521 		/* check scsi parity error */
1522 		if (host->jsstatus0 & TSS_PAR_ERROR)
1523 			scb->hastat = HOST_DO_DU;
1524 		if (inb(host->addr + TUL_XStatus) & XPEND) {	/* DMA xfer pending, Send STOP  */
1525 			/* tell Hardware  scsi xfer has been terminated */
1526 			outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
1527 			/* wait until DMA xfer not pending */
1528 			while (inb(host->addr + TUL_XStatus) & XPEND)
1529 				cpu_relax();
1530 		}
1531 	} else {
1532 		/*-------- DATA OUT -----------*/
1533 		if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
1534 			if (host->active_tc->js_period & TSC_WIDE_SCSI)
1535 				cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
1536 			else
1537 				cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
1538 		}
1539 		if (inb(host->addr + TUL_XStatus) & XPEND) {	/* if DMA xfer is pending, abort DMA xfer */
1540 			outb(TAX_X_ABT, host->addr + TUL_XCmd);
1541 			/* wait Abort DMA xfer done */
1542 			while ((inb(host->addr + TUL_Int) & XABT) == 0)
1543 				cpu_relax();
1544 		}
1545 		if ((cnt == 1) && (host->phase == DATA_OUT)) {
1546 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1547 			if (wait_tulip(host) == -1)
1548 				return -1;
1549 			cnt = 0;
1550 		} else {
1551 			if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
1552 				outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1553 		}
1554 	}
1555 	if (cnt == 0) {
1556 		scb->buflen = 0;
1557 		return 6;	/* After Data */
1558 	}
1559 	/* Update active data pointer */
1560 	xcnt = (long) scb->buflen - cnt;	/* xcnt== bytes already xferred */
1561 	scb->buflen = (u32) cnt;		/* cnt == bytes left to be xferred */
1562 	if (scb->flags & SCF_SG) {
1563 		struct sg_entry *sgp;
1564 		unsigned long i;
1565 
1566 		sgp = &scb->sglist[scb->sgidx];
1567 		for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
1568 			xcnt -= (long) sgp->len;
1569 			if (xcnt < 0) {		/* this sgp xfer half done */
1570 				xcnt += (long) sgp->len;	/* xcnt == bytes xferred in this sgp */
1571 				sgp->data += (u32) xcnt;	/* new ptr to be xfer */
1572 				sgp->len -= (u32) xcnt;	/* new len to be xfer */
1573 				scb->bufptr += ((u32) (i - scb->sgidx) << 3);
1574 				/* new SG table ptr */
1575 				scb->sglen = (u8) (scb->sgmax - i);
1576 				/* new SG table len */
1577 				scb->sgidx = (u16) i;
1578 				/* for next disc and come in this loop */
1579 				return 4;	/* Go to state 4                */
1580 			}
1581 			/* else (xcnt >= 0 , i.e. this sgp already xferred */
1582 		}		/* for */
1583 		return 6;	/* Go to state 6                */
1584 	} else {
1585 		scb->bufptr += (u32) xcnt;
1586 	}
1587 	return 4;		/* Go to state 4                */
1588 }
1589 
1590 /**
1591  *	initio_state_6		-	SCSI state machine
1592  *	@host: InitIO host we are controlling
1593  *
1594  *	State after Data phase
1595  */
1596 
1597 static int initio_state_6(struct initio_host * host)
1598 {
1599 	struct scsi_ctrl_blk *scb = host->active;
1600 
1601 #if DEBUG_STATE
1602 	printk("-s6-");
1603 #endif
1604 	for (;;) {
1605 		switch (host->phase) {
1606 		case STATUS_IN:	/* Status phase                 */
1607 			if ((initio_status_msg(host)) == -1)
1608 				return -1;
1609 			break;
1610 
1611 		case MSG_IN:	/* Message in phase             */
1612 			scb->next_state = 6;
1613 			if ((initio_msgin(host)) == -1)
1614 				return -1;
1615 			break;
1616 
1617 		case MSG_OUT:	/* Message out phase            */
1618 			outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
1619 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1620 			if (wait_tulip(host) == -1)
1621 				return -1;
1622 			break;
1623 
1624 		case DATA_IN:	/* Data in phase                */
1625 			return initio_xpad_in(host);
1626 
1627 		case DATA_OUT:	/* Data out phase               */
1628 			return initio_xpad_out(host);
1629 
1630 		default:
1631 			return initio_bad_seq(host);
1632 		}
1633 	}
1634 }
1635 
1636 /**
1637  *	initio_state_7		-	SCSI state machine
1638  *	@host: InitIO host we are controlling
1639  *
1640  */
1641 
1642 static int initio_state_7(struct initio_host * host)
1643 {
1644 	int cnt, i;
1645 
1646 #if DEBUG_STATE
1647 	printk("-s7-");
1648 #endif
1649 	/* flush SCSI FIFO */
1650 	cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
1651 	if (cnt) {
1652 		for (i = 0; i < cnt; i++)
1653 			inb(host->addr + TUL_SFifo);
1654 	}
1655 	switch (host->phase) {
1656 	case DATA_IN:		/* Data in phase                */
1657 	case DATA_OUT:		/* Data out phase               */
1658 		return initio_bad_seq(host);
1659 	default:
1660 		return 6;	/* Go to state 6                */
1661 	}
1662 }
1663 
1664 /**
1665  *	initio_xfer_data_in	-	Commence data input
1666  *	@host: InitIO host in use
1667  *
1668  *	Commence a block of data transfer. The transfer itself will
1669  *	be managed by the controller and we will get a completion (or
1670  *	failure) interrupt.
1671  */
1672 static int initio_xfer_data_in(struct initio_host * host)
1673 {
1674 	struct scsi_ctrl_blk *scb = host->active;
1675 
1676 	if ((scb->flags & SCF_DIR) == SCF_DOUT)
1677 		return 6;	/* wrong direction */
1678 
1679 	outl(scb->buflen, host->addr + TUL_SCnt0);
1680 	outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd);	/* 7/25/95 */
1681 
1682 	if (scb->flags & SCF_SG) {	/* S/G xfer */
1683 		outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1684 		outl(scb->bufptr, host->addr + TUL_XAddH);
1685 		outb(TAX_SG_IN, host->addr + TUL_XCmd);
1686 	} else {
1687 		outl(scb->buflen, host->addr + TUL_XCntH);
1688 		outl(scb->bufptr, host->addr + TUL_XAddH);
1689 		outb(TAX_X_IN, host->addr + TUL_XCmd);
1690 	}
1691 	scb->next_state = 0x5;
1692 	return 0;		/* return to OS, wait xfer done , let jas_isr come in */
1693 }
1694 
1695 /**
1696  *	initio_xfer_data_out	-	Commence data output
1697  *	@host: InitIO host in use
1698  *
1699  *	Commence a block of data transfer. The transfer itself will
1700  *	be managed by the controller and we will get a completion (or
1701  *	failure) interrupt.
1702  */
1703 
1704 static int initio_xfer_data_out(struct initio_host * host)
1705 {
1706 	struct scsi_ctrl_blk *scb = host->active;
1707 
1708 	if ((scb->flags & SCF_DIR) == SCF_DIN)
1709 		return 6;	/* wrong direction */
1710 
1711 	outl(scb->buflen, host->addr + TUL_SCnt0);
1712 	outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
1713 
1714 	if (scb->flags & SCF_SG) {	/* S/G xfer */
1715 		outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
1716 		outl(scb->bufptr, host->addr + TUL_XAddH);
1717 		outb(TAX_SG_OUT, host->addr + TUL_XCmd);
1718 	} else {
1719 		outl(scb->buflen, host->addr + TUL_XCntH);
1720 		outl(scb->bufptr, host->addr + TUL_XAddH);
1721 		outb(TAX_X_OUT, host->addr + TUL_XCmd);
1722 	}
1723 
1724 	scb->next_state = 0x5;
1725 	return 0;		/* return to OS, wait xfer done , let jas_isr come in */
1726 }
1727 
1728 int initio_xpad_in(struct initio_host * host)
1729 {
1730 	struct scsi_ctrl_blk *scb = host->active;
1731 	struct target_control *active_tc = host->active_tc;
1732 
1733 	if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1734 		scb->hastat = HOST_DO_DU;	/* over run             */
1735 	for (;;) {
1736 		if (active_tc->js_period & TSC_WIDE_SCSI)
1737 			outl(2, host->addr + TUL_SCnt0);
1738 		else
1739 			outl(1, host->addr + TUL_SCnt0);
1740 
1741 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1742 		if (wait_tulip(host) == -1)
1743 			return -1;
1744 		if (host->phase != DATA_IN) {
1745 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1746 			return 6;
1747 		}
1748 		inb(host->addr + TUL_SFifo);
1749 	}
1750 }
1751 
1752 int initio_xpad_out(struct initio_host * host)
1753 {
1754 	struct scsi_ctrl_blk *scb = host->active;
1755 	struct target_control *active_tc = host->active_tc;
1756 
1757 	if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
1758 		scb->hastat = HOST_DO_DU;	/* over run             */
1759 	for (;;) {
1760 		if (active_tc->js_period & TSC_WIDE_SCSI)
1761 			outl(2, host->addr + TUL_SCnt0);
1762 		else
1763 			outl(1, host->addr + TUL_SCnt0);
1764 
1765 		outb(0, host->addr + TUL_SFifo);
1766 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1767 		if ((wait_tulip(host)) == -1)
1768 			return -1;
1769 		if (host->phase != DATA_OUT) {	/* Disable wide CPU to allow read 16 bits */
1770 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
1771 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1772 			return 6;
1773 		}
1774 	}
1775 }
1776 
1777 int initio_status_msg(struct initio_host * host)
1778 {				/* status & MSG_IN */
1779 	struct scsi_ctrl_blk *scb = host->active;
1780 	u8 msg;
1781 
1782 	outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
1783 	if (wait_tulip(host) == -1)
1784 		return -1;
1785 
1786 	/* get status */
1787 	scb->tastat = inb(host->addr + TUL_SFifo);
1788 
1789 	if (host->phase == MSG_OUT) {
1790 		if (host->jsstatus0 & TSS_PAR_ERROR)
1791 			outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1792 		else
1793 			outb(NOP, host->addr + TUL_SFifo);
1794 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1795 		return wait_tulip(host);
1796 	}
1797 	if (host->phase == MSG_IN) {
1798 		msg = inb(host->addr + TUL_SFifo);
1799 		if (host->jsstatus0 & TSS_PAR_ERROR) {	/* Parity error                 */
1800 			if ((initio_msgin_accept(host)) == -1)
1801 				return -1;
1802 			if (host->phase != MSG_OUT)
1803 				return initio_bad_seq(host);
1804 			outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
1805 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
1806 			return wait_tulip(host);
1807 		}
1808 		if (msg == 0) {	/* Command complete             */
1809 
1810 			if ((scb->tastat & 0x18) == 0x10)	/* No link support              */
1811 				return initio_bad_seq(host);
1812 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1813 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
1814 			return initio_wait_done_disc(host);
1815 
1816 		}
1817 		if (msg == LINKED_CMD_COMPLETE ||
1818 		    msg == LINKED_FLG_CMD_COMPLETE) {
1819 			if ((scb->tastat & 0x18) == 0x10)
1820 				return initio_msgin_accept(host);
1821 		}
1822 	}
1823 	return initio_bad_seq(host);
1824 }
1825 
1826 
1827 /* scsi bus free */
1828 int int_initio_busfree(struct initio_host * host)
1829 {
1830 	struct scsi_ctrl_blk *scb = host->active;
1831 
1832 	if (scb != NULL) {
1833 		if (scb->status & SCB_SELECT) {		/* selection timeout */
1834 			initio_unlink_pend_scb(host, scb);
1835 			scb->hastat = HOST_SEL_TOUT;
1836 			initio_append_done_scb(host, scb);
1837 		} else {	/* Unexpected bus free          */
1838 			initio_unlink_busy_scb(host, scb);
1839 			scb->hastat = HOST_BUS_FREE;
1840 			initio_append_done_scb(host, scb);
1841 		}
1842 		host->active = NULL;
1843 		host->active_tc = NULL;
1844 	}
1845 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);		/* Flush SCSI FIFO  */
1846 	outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
1847 	outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect       */
1848 	return -1;
1849 }
1850 
1851 
1852 /**
1853  *	int_initio_scsi_rst	-	SCSI reset occurred
1854  *	@host: Host seeing the reset
1855  *
1856  *	A SCSI bus reset has occurred. Clean up any pending transfer
1857  *	the hardware is doing by DMA and then abort all active and
1858  *	disconnected commands. The mid layer should sort the rest out
1859  *	for us
1860  */
1861 
1862 static int int_initio_scsi_rst(struct initio_host * host)
1863 {
1864 	struct scsi_ctrl_blk *scb;
1865 	int i;
1866 
1867 	/* if DMA xfer is pending, abort DMA xfer */
1868 	if (inb(host->addr + TUL_XStatus) & 0x01) {
1869 		outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
1870 		/* wait Abort DMA xfer done */
1871 		while ((inb(host->addr + TUL_Int) & 0x04) == 0)
1872 			cpu_relax();
1873 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
1874 	}
1875 	/* Abort all active & disconnected scb */
1876 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
1877 		scb->hastat = HOST_BAD_PHAS;
1878 		initio_append_done_scb(host, scb);
1879 	}
1880 	host->active = NULL;
1881 	host->active_tc = NULL;
1882 
1883 	/* clr sync nego. done flag */
1884 	for (i = 0; i < host->max_tar; i++)
1885 		host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1886 	return -1;
1887 }
1888 
1889 /**
1890  *	int_initio_scsi_resel	-	Reselection occurred
1891  *	@host: InitIO host adapter
1892  *
1893  *	A SCSI reselection event has been signalled and the interrupt
1894  *	is now being processed. Work out which command block needs attention
1895  *	and continue processing that command.
1896  */
1897 
1898 int int_initio_resel(struct initio_host * host)
1899 {
1900 	struct scsi_ctrl_blk *scb;
1901 	struct target_control *active_tc;
1902 	u8 tag, msg = 0;
1903 	u8 tar, lun;
1904 
1905 	if ((scb = host->active) != NULL) {
1906 		/* FIXME: Why check and not just clear ? */
1907 		if (scb->status & SCB_SELECT)		/* if waiting for selection complete */
1908 			scb->status &= ~SCB_SELECT;
1909 		host->active = NULL;
1910 	}
1911 	/* --------- get target id---------------------- */
1912 	tar = inb(host->addr + TUL_SBusId);
1913 	/* ------ get LUN from Identify message----------- */
1914 	lun = inb(host->addr + TUL_SIdent) & 0x0F;
1915 	/* 07/22/98 from 0x1F -> 0x0F */
1916 	active_tc = &host->targets[tar];
1917 	host->active_tc = active_tc;
1918 	outb(active_tc->sconfig0, host->addr + TUL_SConfig);
1919 	outb(active_tc->js_period, host->addr + TUL_SPeriod);
1920 
1921 	/* ------------- tag queueing ? ------------------- */
1922 	if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
1923 		if ((initio_msgin_accept(host)) == -1)
1924 			return -1;
1925 		if (host->phase != MSG_IN)
1926 			goto no_tag;
1927 		outl(1, host->addr + TUL_SCnt0);
1928 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1929 		if (wait_tulip(host) == -1)
1930 			return -1;
1931 		msg = inb(host->addr + TUL_SFifo);	/* Read Tag Message    */
1932 
1933 		if (msg < SIMPLE_QUEUE_TAG || msg > ORDERED_QUEUE_TAG)
1934 			/* Is simple Tag      */
1935 			goto no_tag;
1936 
1937 		if (initio_msgin_accept(host) == -1)
1938 			return -1;
1939 
1940 		if (host->phase != MSG_IN)
1941 			goto no_tag;
1942 
1943 		outl(1, host->addr + TUL_SCnt0);
1944 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
1945 		if (wait_tulip(host) == -1)
1946 			return -1;
1947 		tag = inb(host->addr + TUL_SFifo);	/* Read Tag ID       */
1948 		scb = host->scb + tag;
1949 		if (scb->target != tar || scb->lun != lun) {
1950 			return initio_msgout_abort_tag(host);
1951 		}
1952 		if (scb->status != SCB_BUSY) {	/* 03/24/95             */
1953 			return initio_msgout_abort_tag(host);
1954 		}
1955 		host->active = scb;
1956 		if ((initio_msgin_accept(host)) == -1)
1957 			return -1;
1958 	} else {		/* No tag               */
1959 	      no_tag:
1960 		if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
1961 			return initio_msgout_abort_targ(host);
1962 		}
1963 		host->active = scb;
1964 		if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
1965 			if ((initio_msgin_accept(host)) == -1)
1966 				return -1;
1967 		}
1968 	}
1969 	return 0;
1970 }
1971 
1972 /**
1973  *	int_initio_bad_seq		-	out of phase
1974  *	@host: InitIO host flagging event
1975  *
1976  *	We have ended up out of phase somehow. Reset the host controller
1977  *	and throw all our toys out of the pram. Let the midlayer clean up
1978  */
1979 
1980 static int int_initio_bad_seq(struct initio_host * host)
1981 {				/* target wrong phase           */
1982 	struct scsi_ctrl_blk *scb;
1983 	int i;
1984 
1985 	initio_reset_scsi(host, 10);
1986 
1987 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
1988 		scb->hastat = HOST_BAD_PHAS;
1989 		initio_append_done_scb(host, scb);
1990 	}
1991 	for (i = 0; i < host->max_tar; i++)
1992 		host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
1993 	return -1;
1994 }
1995 
1996 
1997 /**
1998  *	initio_msgout_abort_targ		-	abort a tag
1999  *	@host: InitIO host
2000  *
2001  *	Abort when the target/lun does not match or when our SCB is not
2002  *	busy. Used by untagged commands.
2003  */
2004 
2005 static int initio_msgout_abort_targ(struct initio_host * host)
2006 {
2007 
2008 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2009 	if (initio_msgin_accept(host) == -1)
2010 		return -1;
2011 	if (host->phase != MSG_OUT)
2012 		return initio_bad_seq(host);
2013 
2014 	outb(ABORT_TASK_SET, host->addr + TUL_SFifo);
2015 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2016 
2017 	return initio_wait_disc(host);
2018 }
2019 
2020 /**
2021  *	initio_msgout_abort_tag		-	abort a tag
2022  *	@host: InitIO host
2023  *
2024  *	Abort when the target/lun does not match or when our SCB is not
2025  *	busy. Used for tagged commands.
2026  */
2027 
2028 static int initio_msgout_abort_tag(struct initio_host * host)
2029 {
2030 
2031 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2032 	if (initio_msgin_accept(host) == -1)
2033 		return -1;
2034 	if (host->phase != MSG_OUT)
2035 		return initio_bad_seq(host);
2036 
2037 	outb(ABORT_TASK, host->addr + TUL_SFifo);
2038 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2039 
2040 	return initio_wait_disc(host);
2041 
2042 }
2043 
2044 /**
2045  *	initio_msgin		-	Message in
2046  *	@host: InitIO Host
2047  *
2048  *	Process incoming message
2049  */
2050 static int initio_msgin(struct initio_host * host)
2051 {
2052 	struct target_control *active_tc;
2053 
2054 	for (;;) {
2055 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2056 
2057 		outl(1, host->addr + TUL_SCnt0);
2058 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2059 		if (wait_tulip(host) == -1)
2060 			return -1;
2061 
2062 		switch (inb(host->addr + TUL_SFifo)) {
2063 		case DISCONNECT:	/* Disconnect msg */
2064 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2065 			return initio_wait_disc(host);
2066 		case SAVE_POINTERS:
2067 		case RESTORE_POINTERS:
2068 		case NOP:
2069 			initio_msgin_accept(host);
2070 			break;
2071 		case MESSAGE_REJECT:	/* Clear ATN first              */
2072 			outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
2073 				host->addr + TUL_SSignal);
2074 			active_tc = host->active_tc;
2075 			if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)	/* do sync nego */
2076 				outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
2077 					host->addr + TUL_SSignal);
2078 			initio_msgin_accept(host);
2079 			break;
2080 		case EXTENDED_MESSAGE:	/* extended msg */
2081 			initio_msgin_extend(host);
2082 			break;
2083 		case IGNORE_WIDE_RESIDUE:
2084 			initio_msgin_accept(host);
2085 			break;
2086 		case COMMAND_COMPLETE:
2087 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2088 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2089 			return initio_wait_done_disc(host);
2090 		default:
2091 			initio_msgout_reject(host);
2092 			break;
2093 		}
2094 		if (host->phase != MSG_IN)
2095 			return host->phase;
2096 	}
2097 	/* statement won't reach here */
2098 }
2099 
2100 static int initio_msgout_reject(struct initio_host * host)
2101 {
2102 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2103 
2104 	if (initio_msgin_accept(host) == -1)
2105 		return -1;
2106 
2107 	if (host->phase == MSG_OUT) {
2108 		outb(MESSAGE_REJECT, host->addr + TUL_SFifo);		/* Msg reject           */
2109 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2110 		return wait_tulip(host);
2111 	}
2112 	return host->phase;
2113 }
2114 
2115 static int initio_msgout_ide(struct initio_host * host)
2116 {
2117 	outb(INITIATOR_ERROR, host->addr + TUL_SFifo);		/* Initiator Detected Error */
2118 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2119 	return wait_tulip(host);
2120 }
2121 
2122 static int initio_msgin_extend(struct initio_host * host)
2123 {
2124 	u8 len, idx;
2125 
2126 	if (initio_msgin_accept(host) != MSG_IN)
2127 		return host->phase;
2128 
2129 	/* Get extended msg length      */
2130 	outl(1, host->addr + TUL_SCnt0);
2131 	outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2132 	if (wait_tulip(host) == -1)
2133 		return -1;
2134 
2135 	len = inb(host->addr + TUL_SFifo);
2136 	host->msg[0] = len;
2137 	for (idx = 1; len != 0; len--) {
2138 
2139 		if ((initio_msgin_accept(host)) != MSG_IN)
2140 			return host->phase;
2141 		outl(1, host->addr + TUL_SCnt0);
2142 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
2143 		if (wait_tulip(host) == -1)
2144 			return -1;
2145 		host->msg[idx++] = inb(host->addr + TUL_SFifo);
2146 	}
2147 	if (host->msg[1] == 1) {		/* if it's synchronous data transfer request */
2148 		u8 r;
2149 		if (host->msg[0] != 3)	/* if length is not right */
2150 			return initio_msgout_reject(host);
2151 		if (host->active_tc->flags & TCF_NO_SYNC_NEGO) {	/* Set OFFSET=0 to do async, nego back */
2152 			host->msg[3] = 0;
2153 		} else {
2154 			if (initio_msgin_sync(host) == 0 &&
2155 			    (host->active_tc->flags & TCF_SYNC_DONE)) {
2156 				initio_sync_done(host);
2157 				return initio_msgin_accept(host);
2158 			}
2159 		}
2160 
2161 		r = inb(host->addr + TUL_SSignal);
2162 		outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
2163 			host->addr + TUL_SSignal);
2164 		if (initio_msgin_accept(host) != MSG_OUT)
2165 			return host->phase;
2166 		/* sync msg out */
2167 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
2168 
2169 		initio_sync_done(host);
2170 
2171 		outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2172 		outb(3, host->addr + TUL_SFifo);
2173 		outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
2174 		outb(host->msg[2], host->addr + TUL_SFifo);
2175 		outb(host->msg[3], host->addr + TUL_SFifo);
2176 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2177 		return wait_tulip(host);
2178 	}
2179 	if (host->msg[0] != 2 || host->msg[1] != 3)
2180 		return initio_msgout_reject(host);
2181 	/* if it's WIDE DATA XFER REQ   */
2182 	if (host->active_tc->flags & TCF_NO_WDTR) {
2183 		host->msg[2] = 0;
2184 	} else {
2185 		if (host->msg[2] > 2)	/* > 32 bits            */
2186 			return initio_msgout_reject(host);
2187 		if (host->msg[2] == 2) {		/* == 32                */
2188 			host->msg[2] = 1;
2189 		} else {
2190 			if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
2191 				wdtr_done(host);
2192 				if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
2193 					outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2194 				return initio_msgin_accept(host);
2195 			}
2196 		}
2197 	}
2198 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
2199 
2200 	if (initio_msgin_accept(host) != MSG_OUT)
2201 		return host->phase;
2202 	/* WDTR msg out                 */
2203 	outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
2204 	outb(2, host->addr + TUL_SFifo);
2205 	outb(EXTENDED_WDTR, host->addr + TUL_SFifo);
2206 	outb(host->msg[2], host->addr + TUL_SFifo);
2207 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2208 	return wait_tulip(host);
2209 }
2210 
2211 static int initio_msgin_sync(struct initio_host * host)
2212 {
2213 	char default_period;
2214 
2215 	default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
2216 	if (host->msg[3] > MAX_OFFSET) {
2217 		host->msg[3] = MAX_OFFSET;
2218 		if (host->msg[2] < default_period) {
2219 			host->msg[2] = default_period;
2220 			return 1;
2221 		}
2222 		if (host->msg[2] >= 59)	/* Change to async              */
2223 			host->msg[3] = 0;
2224 		return 1;
2225 	}
2226 	/* offset requests asynchronous transfers ? */
2227 	if (host->msg[3] == 0) {
2228 		return 0;
2229 	}
2230 	if (host->msg[2] < default_period) {
2231 		host->msg[2] = default_period;
2232 		return 1;
2233 	}
2234 	if (host->msg[2] >= 59) {
2235 		host->msg[3] = 0;
2236 		return 1;
2237 	}
2238 	return 0;
2239 }
2240 
2241 static int wdtr_done(struct initio_host * host)
2242 {
2243 	host->active_tc->flags &= ~TCF_SYNC_DONE;
2244 	host->active_tc->flags |= TCF_WDTR_DONE;
2245 
2246 	host->active_tc->js_period = 0;
2247 	if (host->msg[2])	/* if 16 bit */
2248 		host->active_tc->js_period |= TSC_WIDE_SCSI;
2249 	host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
2250 	outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2251 	outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2252 
2253 	return 1;
2254 }
2255 
2256 static int initio_sync_done(struct initio_host * host)
2257 {
2258 	int i;
2259 
2260 	host->active_tc->flags |= TCF_SYNC_DONE;
2261 
2262 	if (host->msg[3]) {
2263 		host->active_tc->js_period |= host->msg[3];
2264 		for (i = 0; i < 8; i++) {
2265 			if (initio_rate_tbl[i] >= host->msg[2])	/* pick the big one */
2266 				break;
2267 		}
2268 		host->active_tc->js_period |= (i << 4);
2269 		host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
2270 	}
2271 	outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
2272 	outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
2273 
2274 	return -1;
2275 }
2276 
2277 
2278 static int initio_post_scsi_rst(struct initio_host * host)
2279 {
2280 	struct scsi_ctrl_blk *scb;
2281 	struct target_control *active_tc;
2282 	int i;
2283 
2284 	host->active = NULL;
2285 	host->active_tc = NULL;
2286 	host->flags = 0;
2287 
2288 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
2289 		scb->hastat = HOST_BAD_PHAS;
2290 		initio_append_done_scb(host, scb);
2291 	}
2292 	/* clear sync done flag         */
2293 	active_tc = &host->targets[0];
2294 	for (i = 0; i < host->max_tar; active_tc++, i++) {
2295 		active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
2296 		/* Initialize the sync. xfer register values to an asyn xfer */
2297 		active_tc->js_period = 0;
2298 		active_tc->sconfig0 = host->sconf1;
2299 		host->act_tags[0] = 0;	/* 07/22/98 */
2300 		host->targets[i].flags &= ~TCF_BUSY;	/* 07/22/98 */
2301 	}			/* for */
2302 
2303 	return -1;
2304 }
2305 
2306 static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
2307 {
2308 	scb->status |= SCB_SELECT;
2309 	scb->next_state = 0x1;
2310 	host->active = scb;
2311 	host->active_tc = &host->targets[scb->target];
2312 	outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
2313 }
2314 
2315 
2316 static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
2317 {
2318 	int i;
2319 
2320 	scb->status |= SCB_SELECT;
2321 	scb->next_state = 0x2;
2322 
2323 	outb(scb->ident, host->addr + TUL_SFifo);
2324 	for (i = 0; i < (int) scb->cdblen; i++)
2325 		outb(scb->cdb[i], host->addr + TUL_SFifo);
2326 	host->active_tc = &host->targets[scb->target];
2327 	host->active = scb;
2328 	outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
2329 }
2330 
2331 static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
2332 {
2333 	int i;
2334 
2335 	scb->status |= SCB_SELECT;
2336 	scb->next_state = 0x2;
2337 
2338 	outb(scb->ident, host->addr + TUL_SFifo);
2339 	outb(scb->tagmsg, host->addr + TUL_SFifo);
2340 	outb(scb->tagid, host->addr + TUL_SFifo);
2341 	for (i = 0; i < scb->cdblen; i++)
2342 		outb(scb->cdb[i], host->addr + TUL_SFifo);
2343 	host->active_tc = &host->targets[scb->target];
2344 	host->active = scb;
2345 	outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
2346 }
2347 
2348 /**
2349  *	initio_bus_device_reset	-	 SCSI Bus Device Reset
2350  *	@host: InitIO host to reset
2351  *
2352  *	Perform a device reset and abort all pending SCBs for the
2353  *	victim device
2354  */
2355 int initio_bus_device_reset(struct initio_host * host)
2356 {
2357 	struct scsi_ctrl_blk *scb = host->active;
2358 	struct target_control *active_tc = host->active_tc;
2359 	struct scsi_ctrl_blk *tmp, *prev;
2360 	u8 tar;
2361 
2362 	if (host->phase != MSG_OUT)
2363 		return int_initio_bad_seq(host);	/* Unexpected phase */
2364 
2365 	initio_unlink_pend_scb(host, scb);
2366 	initio_release_scb(host, scb);
2367 
2368 
2369 	tar = scb->target;	/* target                       */
2370 	active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
2371 	/* clr sync. nego & WDTR flags  07/22/98 */
2372 
2373 	/* abort all SCB with same target */
2374 	prev = tmp = host->first_busy;	/* Check Busy queue */
2375 	while (tmp != NULL) {
2376 		if (tmp->target == tar) {
2377 			/* unlink it */
2378 			if (tmp == host->first_busy) {
2379 				if ((host->first_busy = tmp->next) == NULL)
2380 					host->last_busy = NULL;
2381 			} else {
2382 				prev->next = tmp->next;
2383 				if (tmp == host->last_busy)
2384 					host->last_busy = prev;
2385 			}
2386 			tmp->hastat = HOST_ABORTED;
2387 			initio_append_done_scb(host, tmp);
2388 		}
2389 		/* Previous haven't change      */
2390 		else {
2391 			prev = tmp;
2392 		}
2393 		tmp = tmp->next;
2394 	}
2395 	outb(TARGET_RESET, host->addr + TUL_SFifo);
2396 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
2397 	return initio_wait_disc(host);
2398 
2399 }
2400 
2401 static int initio_msgin_accept(struct initio_host * host)
2402 {
2403 	outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
2404 	return wait_tulip(host);
2405 }
2406 
2407 static int wait_tulip(struct initio_host * host)
2408 {
2409 
2410 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2411 		 & TSS_INT_PENDING))
2412 			cpu_relax();
2413 
2414 	host->jsint = inb(host->addr + TUL_SInt);
2415 	host->phase = host->jsstatus0 & TSS_PH_MASK;
2416 	host->jsstatus1 = inb(host->addr + TUL_SStatus1);
2417 
2418 	if (host->jsint & TSS_RESEL_INT)	/* if SCSI bus reset detected */
2419 		return int_initio_resel(host);
2420 	if (host->jsint & TSS_SEL_TIMEOUT)	/* if selected/reselected timeout interrupt */
2421 		return int_initio_busfree(host);
2422 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected   */
2423 		return int_initio_scsi_rst(host);
2424 
2425 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
2426 		if (host->flags & HCF_EXPECT_DONE_DISC) {
2427 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2428 			initio_unlink_busy_scb(host, host->active);
2429 			host->active->hastat = 0;
2430 			initio_append_done_scb(host, host->active);
2431 			host->active = NULL;
2432 			host->active_tc = NULL;
2433 			host->flags &= ~HCF_EXPECT_DONE_DISC;
2434 			outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2435 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
2436 			return -1;
2437 		}
2438 		if (host->flags & HCF_EXPECT_DISC) {
2439 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2440 			host->active = NULL;
2441 			host->active_tc = NULL;
2442 			host->flags &= ~HCF_EXPECT_DISC;
2443 			outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2444 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
2445 			return -1;
2446 		}
2447 		return int_initio_busfree(host);
2448 	}
2449 	/* The old code really does the below. Can probably be removed */
2450 	if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
2451 		return host->phase;
2452 	return host->phase;
2453 }
2454 
2455 static int initio_wait_disc(struct initio_host * host)
2456 {
2457 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
2458 		cpu_relax();
2459 
2460 	host->jsint = inb(host->addr + TUL_SInt);
2461 
2462 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected */
2463 		return int_initio_scsi_rst(host);
2464 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection */
2465 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
2466 		outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2467 		outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
2468 		host->active = NULL;
2469 		return -1;
2470 	}
2471 	return initio_bad_seq(host);
2472 }
2473 
2474 static int initio_wait_done_disc(struct initio_host * host)
2475 {
2476 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
2477 		 & TSS_INT_PENDING))
2478 		 cpu_relax();
2479 
2480 	host->jsint = inb(host->addr + TUL_SInt);
2481 
2482 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected */
2483 		return int_initio_scsi_rst(host);
2484 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection */
2485 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);		/* Flush SCSI FIFO */
2486 		outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
2487 		outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);		/* Enable HW reselect */
2488 		initio_unlink_busy_scb(host, host->active);
2489 
2490 		initio_append_done_scb(host, host->active);
2491 		host->active = NULL;
2492 		return -1;
2493 	}
2494 	return initio_bad_seq(host);
2495 }
2496 
2497 /**
2498  *	i91u_intr		-	IRQ handler
2499  *	@irqno: IRQ number
2500  *	@dev_id: IRQ identifier
2501  *
2502  *	Take the relevant locks and then invoke the actual isr processing
2503  *	code under the lock.
2504  */
2505 
2506 static irqreturn_t i91u_intr(int irqno, void *dev_id)
2507 {
2508 	struct Scsi_Host *dev = dev_id;
2509 	unsigned long flags;
2510 	int r;
2511 
2512 	spin_lock_irqsave(dev->host_lock, flags);
2513 	r = initio_isr((struct initio_host *)dev->hostdata);
2514 	spin_unlock_irqrestore(dev->host_lock, flags);
2515 	if (r)
2516 		return IRQ_HANDLED;
2517 	else
2518 		return IRQ_NONE;
2519 }
2520 
2521 
2522 /**
2523  *	initio_build_scb		-	Build the mappings and SCB
2524  *	@host: InitIO host taking the command
2525  *	@cblk: Firmware command block
2526  *	@cmnd: SCSI midlayer command block
2527  *
2528  *	Translate the abstract SCSI command into a firmware command block
2529  *	suitable for feeding to the InitIO host controller. This also requires
2530  *	we build the scatter gather lists and ensure they are mapped properly.
2531  */
2532 
2533 static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
2534 {				/* Create corresponding SCB     */
2535 	struct scatterlist *sglist;
2536 	struct sg_entry *sg;		/* Pointer to SG list           */
2537 	int i, nseg;
2538 	long total_len;
2539 	dma_addr_t dma_addr;
2540 
2541 	/* Fill in the command headers */
2542 	cblk->post = i91uSCBPost;	/* i91u's callback routine      */
2543 	cblk->srb = cmnd;
2544 	cblk->opcode = ExecSCSI;
2545 	cblk->flags = SCF_POST;	/* After SCSI done, call post routine */
2546 	cblk->target = cmnd->device->id;
2547 	cblk->lun = cmnd->device->lun;
2548 	cblk->ident = cmnd->device->lun | DISC_ALLOW;
2549 
2550 	cblk->flags |= SCF_SENSE;	/* Turn on auto request sense   */
2551 
2552 	/* Map the sense buffer into bus memory */
2553 	dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
2554 				  SENSE_SIZE, DMA_FROM_DEVICE);
2555 	cblk->senseptr = (u32)dma_addr;
2556 	cblk->senselen = SENSE_SIZE;
2557 	cmnd->SCp.ptr = (char *)(unsigned long)dma_addr;
2558 	cblk->cdblen = cmnd->cmd_len;
2559 
2560 	/* Clear the returned status */
2561 	cblk->hastat = 0;
2562 	cblk->tastat = 0;
2563 	/* Command the command */
2564 	memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
2565 
2566 	/* Set up tags */
2567 	if (cmnd->device->tagged_supported) {	/* Tag Support                  */
2568 		cblk->tagmsg = SIMPLE_QUEUE_TAG;	/* Do simple tag only   */
2569 	} else {
2570 		cblk->tagmsg = 0;	/* No tag support               */
2571 	}
2572 
2573 	/* todo handle map_sg error */
2574 	nseg = scsi_dma_map(cmnd);
2575 	BUG_ON(nseg < 0);
2576 	if (nseg) {
2577 		dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
2578 					  sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2579 					  DMA_BIDIRECTIONAL);
2580 		cblk->bufptr = (u32)dma_addr;
2581 		cmnd->SCp.dma_handle = dma_addr;
2582 
2583 		cblk->sglen = nseg;
2584 
2585 		cblk->flags |= SCF_SG;	/* Turn on SG list flag       */
2586 		total_len = 0;
2587 		sg = &cblk->sglist[0];
2588 		scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
2589 			sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
2590 			sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2591 			total_len += sg_dma_len(sglist);
2592 			++sg;
2593 		}
2594 
2595 		cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2596 			total_len : scsi_bufflen(cmnd);
2597 	} else {	/* No data transfer required */
2598 		cblk->buflen = 0;
2599 		cblk->sglen = 0;
2600 	}
2601 }
2602 
2603 /**
2604  *	i91u_queuecommand	-	Queue a new command if possible
2605  *	@cmd: SCSI command block from the mid layer
2606  *	@done: Completion handler
2607  *
2608  *	Attempts to queue a new command with the host adapter. Will return
2609  *	zero if successful or indicate a host busy condition if not (which
2610  *	will cause the mid layer to call us again later with the command)
2611  */
2612 
2613 static int i91u_queuecommand_lck(struct scsi_cmnd *cmd,
2614 		void (*done)(struct scsi_cmnd *))
2615 {
2616 	struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
2617 	struct scsi_ctrl_blk *cmnd;
2618 
2619 	cmd->scsi_done = done;
2620 
2621 	cmnd = initio_alloc_scb(host);
2622 	if (!cmnd)
2623 		return SCSI_MLQUEUE_HOST_BUSY;
2624 
2625 	initio_build_scb(host, cmnd, cmd);
2626 	initio_exec_scb(host, cmnd);
2627 	return 0;
2628 }
2629 
2630 static DEF_SCSI_QCMD(i91u_queuecommand)
2631 
2632 /**
2633  *	i91u_bus_reset		-	reset the SCSI bus
2634  *	@cmnd: Command block we want to trigger the reset for
2635  *
2636  *	Initiate a SCSI bus reset sequence
2637  */
2638 
2639 static int i91u_bus_reset(struct scsi_cmnd * cmnd)
2640 {
2641 	struct initio_host *host;
2642 
2643 	host = (struct initio_host *) cmnd->device->host->hostdata;
2644 
2645 	spin_lock_irq(cmnd->device->host->host_lock);
2646 	initio_reset_scsi(host, 0);
2647 	spin_unlock_irq(cmnd->device->host->host_lock);
2648 
2649 	return SUCCESS;
2650 }
2651 
2652 /**
2653  *	i91u_biospararm			-	return the "logical geometry
2654  *	@sdev: SCSI device
2655  *	@dev; Matching block device
2656  *	@capacity: Sector size of drive
2657  *	@info_array: Return space for BIOS geometry
2658  *
2659  *	Map the device geometry in a manner compatible with the host
2660  *	controller BIOS behaviour.
2661  *
2662  *	FIXME: limited to 2^32 sector devices.
2663  */
2664 
2665 static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
2666 		sector_t capacity, int *info_array)
2667 {
2668 	struct initio_host *host;		/* Point to Host adapter control block */
2669 	struct target_control *tc;
2670 
2671 	host = (struct initio_host *) sdev->host->hostdata;
2672 	tc = &host->targets[sdev->id];
2673 
2674 	if (tc->heads) {
2675 		info_array[0] = tc->heads;
2676 		info_array[1] = tc->sectors;
2677 		info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
2678 	} else {
2679 		if (tc->drv_flags & TCF_DRV_255_63) {
2680 			info_array[0] = 255;
2681 			info_array[1] = 63;
2682 			info_array[2] = (unsigned long)capacity / 255 / 63;
2683 		} else {
2684 			info_array[0] = 64;
2685 			info_array[1] = 32;
2686 			info_array[2] = (unsigned long)capacity >> 11;
2687 		}
2688 	}
2689 
2690 #if defined(DEBUG_BIOSPARAM)
2691 	if (i91u_debug & debug_biosparam) {
2692 		printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
2693 		       info_array[0], info_array[1], info_array[2]);
2694 		printk("WARNING: check, if the bios geometry is correct.\n");
2695 	}
2696 #endif
2697 
2698 	return 0;
2699 }
2700 
2701 /**
2702  *	i91u_unmap_scb		-	Unmap a command
2703  *	@pci_dev: PCI device the command is for
2704  *	@cmnd: The command itself
2705  *
2706  *	Unmap any PCI mapping/IOMMU resources allocated when the command
2707  *	was mapped originally as part of initio_build_scb
2708  */
2709 
2710 static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
2711 {
2712 	/* auto sense buffer */
2713 	if (cmnd->SCp.ptr) {
2714 		dma_unmap_single(&pci_dev->dev,
2715 				 (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
2716 				 SENSE_SIZE, DMA_FROM_DEVICE);
2717 		cmnd->SCp.ptr = NULL;
2718 	}
2719 
2720 	/* request buffer */
2721 	if (scsi_sg_count(cmnd)) {
2722 		dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
2723 				 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
2724 				 DMA_BIDIRECTIONAL);
2725 
2726 		scsi_dma_unmap(cmnd);
2727 	}
2728 }
2729 
2730 /**
2731  *	i91uSCBPost		-	SCSI callback
2732  *	@host: Pointer to host adapter control block.
2733  *	@cmnd: Pointer to SCSI control block.
2734  *
2735  *	This is callback routine be called when tulip finish one
2736  *	SCSI command.
2737  */
2738 
2739 static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
2740 {
2741 	struct scsi_cmnd *cmnd;	/* Pointer to SCSI request block */
2742 	struct initio_host *host;
2743 	struct scsi_ctrl_blk *cblk;
2744 
2745 	host = (struct initio_host *) host_mem;
2746 	cblk = (struct scsi_ctrl_blk *) cblk_mem;
2747 	if ((cmnd = cblk->srb) == NULL) {
2748 		printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
2749 		WARN_ON(1);
2750 		initio_release_scb(host, cblk);	/* Release SCB for current channel */
2751 		return;
2752 	}
2753 
2754 	/*
2755 	 *	Remap the firmware error status into a mid layer one
2756 	 */
2757 	switch (cblk->hastat) {
2758 	case 0x0:
2759 	case 0xa:		/* Linked command complete without error and linked normally */
2760 	case 0xb:		/* Linked command complete without error interrupt generated */
2761 		cblk->hastat = 0;
2762 		break;
2763 
2764 	case 0x11:		/* Selection time out-The initiator selection or target
2765 				   reselection was not complete within the SCSI Time out period */
2766 		cblk->hastat = DID_TIME_OUT;
2767 		break;
2768 
2769 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
2770 				   phase sequence was requested by the target. The host adapter
2771 				   will generate a SCSI Reset Condition, notifying the host with
2772 				   a SCRD interrupt */
2773 		cblk->hastat = DID_RESET;
2774 		break;
2775 
2776 	case 0x1a:		/* SCB Aborted. 07/21/98 */
2777 		cblk->hastat = DID_ABORT;
2778 		break;
2779 
2780 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
2781 				   than was allocated by the Data Length field or the sum of the
2782 				   Scatter / Gather Data Length fields. */
2783 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
2784 	case 0x16:		/* Invalid SCB Operation Code. */
2785 
2786 	default:
2787 		printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
2788 		cblk->hastat = DID_ERROR;	/* Couldn't find any better */
2789 		break;
2790 	}
2791 
2792 	cmnd->result = cblk->tastat | (cblk->hastat << 16);
2793 	i91u_unmap_scb(host->pci_dev, cmnd);
2794 	cmnd->scsi_done(cmnd);	/* Notify system DONE           */
2795 	initio_release_scb(host, cblk);	/* Release SCB for current channel */
2796 }
2797 
2798 static struct scsi_host_template initio_template = {
2799 	.proc_name		= "INI9100U",
2800 	.name			= "Initio INI-9X00U/UW SCSI device driver",
2801 	.queuecommand		= i91u_queuecommand,
2802 	.eh_bus_reset_handler	= i91u_bus_reset,
2803 	.bios_param		= i91u_biosparam,
2804 	.can_queue		= MAX_TARGETS * i91u_MAXQUEUE,
2805 	.this_id		= 1,
2806 	.sg_tablesize		= SG_ALL,
2807 };
2808 
2809 static int initio_probe_one(struct pci_dev *pdev,
2810 	const struct pci_device_id *id)
2811 {
2812 	struct Scsi_Host *shost;
2813 	struct initio_host *host;
2814 	u32 reg;
2815 	u16 bios_seg;
2816 	struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
2817 	int num_scb, i, error;
2818 
2819 	error = pci_enable_device(pdev);
2820 	if (error)
2821 		return error;
2822 
2823 	pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
2824 	bios_seg = (u16) (reg & 0xFF);
2825 	if (((reg & 0xFF00) >> 8) == 0xFF)
2826 		reg = 0;
2827 	bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
2828 
2829 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
2830 		printk(KERN_WARNING  "i91u: Could not set 32 bit DMA mask\n");
2831 		error = -ENODEV;
2832 		goto out_disable_device;
2833 	}
2834 	shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
2835 	if (!shost) {
2836 		printk(KERN_WARNING "initio: Could not allocate host structure.\n");
2837 		error = -ENOMEM;
2838 		goto out_disable_device;
2839 	}
2840 	host = (struct initio_host *)shost->hostdata;
2841 	memset(host, 0, sizeof(struct initio_host));
2842 	host->addr = pci_resource_start(pdev, 0);
2843 	host->bios_addr = bios_seg;
2844 
2845 	if (!request_region(host->addr, 256, "i91u")) {
2846 		printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
2847 		error = -ENODEV;
2848 		goto out_host_put;
2849 	}
2850 
2851 	if (initio_tag_enable)	/* 1.01i */
2852 		num_scb = MAX_TARGETS * i91u_MAXQUEUE;
2853 	else
2854 		num_scb = MAX_TARGETS + 3;	/* 1-tape, 1-CD_ROM, 1- extra */
2855 
2856 	for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
2857 		i = num_scb * sizeof(struct scsi_ctrl_blk);
2858 		if ((scb = kzalloc(i, GFP_DMA)) != NULL)
2859 			break;
2860 	}
2861 
2862 	if (!scb) {
2863 		printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
2864 		error = -ENOMEM;
2865 		goto out_release_region;
2866 	}
2867 
2868 	host->pci_dev = pdev;
2869 
2870 	host->semaph = 1;
2871 	spin_lock_init(&host->semaph_lock);
2872 	host->num_scbs = num_scb;
2873 	host->scb = scb;
2874 	host->next_pending = scb;
2875 	host->next_avail = scb;
2876 	for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
2877 		tmp->tagid = i;
2878 		if (i != 0)
2879 			prev->next = tmp;
2880 		prev = tmp;
2881 	}
2882 	prev->next = NULL;
2883 	host->scb_end = tmp;
2884 	host->first_avail = scb;
2885 	host->last_avail = prev;
2886 	spin_lock_init(&host->avail_lock);
2887 
2888 	initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
2889 
2890 	host->jsstatus0 = 0;
2891 
2892 	shost->io_port = host->addr;
2893 	shost->n_io_port = 0xff;
2894 	shost->can_queue = num_scb;		/* 03/05/98                      */
2895 	shost->unique_id = host->addr;
2896 	shost->max_id = host->max_tar;
2897 	shost->max_lun = 32;	/* 10/21/97                     */
2898 	shost->irq = pdev->irq;
2899 	shost->this_id = host->scsi_id;	/* Assign HCS index           */
2900 	shost->base = host->addr;
2901 	shost->sg_tablesize = TOTAL_SG_ENTRY;
2902 
2903 	error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
2904 	if (error < 0) {
2905 		printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
2906 		goto out_free_scbs;
2907 	}
2908 
2909 	pci_set_drvdata(pdev, shost);
2910 
2911 	error = scsi_add_host(shost, &pdev->dev);
2912 	if (error)
2913 		goto out_free_irq;
2914 	scsi_scan_host(shost);
2915 	return 0;
2916 out_free_irq:
2917 	free_irq(pdev->irq, shost);
2918 out_free_scbs:
2919 	kfree(host->scb);
2920 out_release_region:
2921 	release_region(host->addr, 256);
2922 out_host_put:
2923 	scsi_host_put(shost);
2924 out_disable_device:
2925 	pci_disable_device(pdev);
2926 	return error;
2927 }
2928 
2929 /**
2930  *	initio_remove_one	-	control shutdown
2931  *	@pdev:	PCI device being released
2932  *
2933  *	Release the resources assigned to this adapter after it has
2934  *	finished being used.
2935  */
2936 
2937 static void initio_remove_one(struct pci_dev *pdev)
2938 {
2939 	struct Scsi_Host *host = pci_get_drvdata(pdev);
2940 	struct initio_host *s = (struct initio_host *)host->hostdata;
2941 	scsi_remove_host(host);
2942 	free_irq(pdev->irq, host);
2943 	release_region(s->addr, 256);
2944 	scsi_host_put(host);
2945 	pci_disable_device(pdev);
2946 }
2947 
2948 MODULE_LICENSE("GPL");
2949 
2950 static struct pci_device_id initio_pci_tbl[] = {
2951 	{PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2952 	{PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2953 	{PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2954 	{PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2955 	{PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2956 	{0,}
2957 };
2958 MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
2959 
2960 static struct pci_driver initio_pci_driver = {
2961 	.name		= "initio",
2962 	.id_table	= initio_pci_tbl,
2963 	.probe		= initio_probe_one,
2964 	.remove		= initio_remove_one,
2965 };
2966 module_pci_driver(initio_pci_driver);
2967 
2968 MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
2969 MODULE_AUTHOR("Initio Corporation");
2970 MODULE_LICENSE("GPL");
2971