xref: /openbmc/linux/drivers/scsi/initio.c (revision 4ba116af)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /**************************************************************************
31da177e4SLinus Torvalds  * Initio 9100 device driver for Linux.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 1994-1998 Initio Corporation
61da177e4SLinus Torvalds  * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
772d39feaSAlan Cox  * Copyright (c) 2004 Christoph Hellwig <hch@lst.de>
8fa195afeSAlan Cox  * Copyright (c) 2007 Red Hat
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *************************************************************************
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * DESCRIPTION:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
151da177e4SLinus Torvalds  * adapters
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * 08/06/97 hc	- v1.01h
181da177e4SLinus Torvalds  *		- Support inic-940 and inic-935
191da177e4SLinus Torvalds  * 09/26/97 hc	- v1.01i
201da177e4SLinus Torvalds  *		- Make correction from J.W. Schultz suggestion
211da177e4SLinus Torvalds  * 10/13/97 hc	- Support reset function
221da177e4SLinus Torvalds  * 10/21/97 hc	- v1.01j
231da177e4SLinus Torvalds  *		- Support 32 LUN (SCSI 3)
241da177e4SLinus Torvalds  * 01/14/98 hc	- v1.01k
251da177e4SLinus Torvalds  *		- Fix memory allocation problem
261da177e4SLinus Torvalds  * 03/04/98 hc	- v1.01l
271da177e4SLinus Torvalds  *		- Fix tape rewind which will hang the system problem
2872d39feaSAlan Cox  *		- Set can_queue to initio_num_scb
291da177e4SLinus Torvalds  * 06/25/98 hc	- v1.01m
301da177e4SLinus Torvalds  *		- Get it work for kernel version >= 2.1.75
3172d39feaSAlan Cox  *		- Dynamic assign SCSI bus reset holding time in initio_init()
321da177e4SLinus Torvalds  * 07/02/98 hc	- v1.01n
331da177e4SLinus Torvalds  *		- Support 0002134A
341da177e4SLinus Torvalds  * 08/07/98 hc  - v1.01o
3572d39feaSAlan Cox  *		- Change the initio_abort_srb routine to use scsi_done. <01>
361da177e4SLinus Torvalds  * 09/07/98 hl  - v1.02
371da177e4SLinus Torvalds  *              - Change the INI9100U define and proc_dir_entry to
381da177e4SLinus Torvalds  *                reflect the newer Kernel 2.1.118, but the v1.o1o
391da177e4SLinus Torvalds  *                should work with Kernel 2.1.118.
401da177e4SLinus Torvalds  * 09/20/98 wh  - v1.02a
411da177e4SLinus Torvalds  *              - Support Abort command.
421da177e4SLinus Torvalds  *              - Handle reset routine.
431da177e4SLinus Torvalds  * 09/21/98 hl  - v1.03
441da177e4SLinus Torvalds  *              - remove comments.
451da177e4SLinus Torvalds  * 12/09/98 bv	- v1.03a
461da177e4SLinus Torvalds  *		- Removed unused code
471da177e4SLinus Torvalds  * 12/13/98 bv	- v1.03b
481da177e4SLinus Torvalds  *		- Remove cli() locking for kernels >= 2.1.95. This uses
491da177e4SLinus Torvalds  *		  spinlocks to serialize access to the pSRB_head and
501da177e4SLinus Torvalds  *		  pSRB_tail members of the HCS structure.
511da177e4SLinus Torvalds  * 09/01/99 bv	- v1.03d
521da177e4SLinus Torvalds  *		- Fixed a deadlock problem in SMP.
531da177e4SLinus Torvalds  * 21/01/99 bv	- v1.03e
541da177e4SLinus Torvalds  *		- Add support for the Domex 3192U PCI SCSI
551da177e4SLinus Torvalds  *		  This is a slightly modified patch by
561da177e4SLinus Torvalds  *		  Brian Macy <bmacy@sunshinecomputing.com>
571da177e4SLinus Torvalds  * 22/02/99 bv	- v1.03f
581da177e4SLinus Torvalds  *		- Didn't detect the INIC-950 in 2.0.x correctly.
591da177e4SLinus Torvalds  *		  Now fixed.
601da177e4SLinus Torvalds  * 05/07/99 bv	- v1.03g
611da177e4SLinus Torvalds  *		- Changed the assumption that HZ = 100
621da177e4SLinus Torvalds  * 10/17/03 mc	- v1.04
631da177e4SLinus Torvalds  *		- added new DMA API support
641da177e4SLinus Torvalds  * 06/01/04 jmd	- v1.04a
651da177e4SLinus Torvalds  *		- Re-add reset_bus support
661da177e4SLinus Torvalds  **************************************************************************/
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <linux/module.h>
691da177e4SLinus Torvalds #include <linux/errno.h>
701da177e4SLinus Torvalds #include <linux/delay.h>
711da177e4SLinus Torvalds #include <linux/pci.h>
721da177e4SLinus Torvalds #include <linux/init.h>
731da177e4SLinus Torvalds #include <linux/blkdev.h>
741da177e4SLinus Torvalds #include <linux/spinlock.h>
751da177e4SLinus Torvalds #include <linux/stat.h>
761da177e4SLinus Torvalds #include <linux/kernel.h>
771da177e4SLinus Torvalds #include <linux/proc_fs.h>
781da177e4SLinus Torvalds #include <linux/string.h>
791da177e4SLinus Torvalds #include <linux/interrupt.h>
801da177e4SLinus Torvalds #include <linux/ioport.h>
811da177e4SLinus Torvalds #include <linux/slab.h>
821da177e4SLinus Torvalds #include <linux/jiffies.h>
83910638aeSMatthias Gehre #include <linux/dma-mapping.h>
841da177e4SLinus Torvalds #include <asm/io.h>
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #include <scsi/scsi.h>
871da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
881da177e4SLinus Torvalds #include <scsi/scsi_device.h>
891da177e4SLinus Torvalds #include <scsi/scsi_host.h>
901da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #include "initio.h"
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define SENSE_SIZE		14
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #define i91u_MAXQUEUE		2
971da177e4SLinus Torvalds #define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #ifdef DEBUG_i91u
1001da177e4SLinus Torvalds static unsigned int i91u_debug = DEBUG_DEFAULT;
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
10372d39feaSAlan Cox static int initio_tag_enable = 1;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #ifdef DEBUG_i91u
1061da177e4SLinus Torvalds static int setup_debug = 0;
1071da177e4SLinus Torvalds #endif
1081da177e4SLinus Torvalds 
10972d39feaSAlan Cox static void i91uSCBPost(u8 * pHcb, u8 * pScb);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define DEBUG_INTERRUPT 0
1121da177e4SLinus Torvalds #define DEBUG_QUEUE     0
1131da177e4SLinus Torvalds #define DEBUG_STATE     0
1141da177e4SLinus Torvalds #define INT_DISC	0
1151da177e4SLinus Torvalds 
11672d39feaSAlan Cox /*--- forward references ---*/
11772d39feaSAlan Cox static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
11872d39feaSAlan Cox static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
1191da177e4SLinus Torvalds 
12072d39feaSAlan Cox static int tulip_main(struct initio_host * host);
1211da177e4SLinus Torvalds 
12272d39feaSAlan Cox static int initio_next_state(struct initio_host * host);
12372d39feaSAlan Cox static int initio_state_1(struct initio_host * host);
12472d39feaSAlan Cox static int initio_state_2(struct initio_host * host);
12572d39feaSAlan Cox static int initio_state_3(struct initio_host * host);
12672d39feaSAlan Cox static int initio_state_4(struct initio_host * host);
12772d39feaSAlan Cox static int initio_state_5(struct initio_host * host);
12872d39feaSAlan Cox static int initio_state_6(struct initio_host * host);
12972d39feaSAlan Cox static int initio_state_7(struct initio_host * host);
13072d39feaSAlan Cox static int initio_xfer_data_in(struct initio_host * host);
13172d39feaSAlan Cox static int initio_xfer_data_out(struct initio_host * host);
13272d39feaSAlan Cox static int initio_xpad_in(struct initio_host * host);
13372d39feaSAlan Cox static int initio_xpad_out(struct initio_host * host);
13472d39feaSAlan Cox static int initio_status_msg(struct initio_host * host);
1351da177e4SLinus Torvalds 
13672d39feaSAlan Cox static int initio_msgin(struct initio_host * host);
13772d39feaSAlan Cox static int initio_msgin_sync(struct initio_host * host);
13872d39feaSAlan Cox static int initio_msgin_accept(struct initio_host * host);
13972d39feaSAlan Cox static int initio_msgout_reject(struct initio_host * host);
14072d39feaSAlan Cox static int initio_msgin_extend(struct initio_host * host);
1411da177e4SLinus Torvalds 
14272d39feaSAlan Cox static int initio_msgout_ide(struct initio_host * host);
14372d39feaSAlan Cox static int initio_msgout_abort_targ(struct initio_host * host);
14472d39feaSAlan Cox static int initio_msgout_abort_tag(struct initio_host * host);
1451da177e4SLinus Torvalds 
14672d39feaSAlan Cox static int initio_bus_device_reset(struct initio_host * host);
14772d39feaSAlan Cox static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
14872d39feaSAlan Cox static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
14972d39feaSAlan Cox static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
15072d39feaSAlan Cox static int int_initio_busfree(struct initio_host * host);
15172d39feaSAlan Cox static int int_initio_scsi_rst(struct initio_host * host);
15272d39feaSAlan Cox static int int_initio_bad_seq(struct initio_host * host);
15372d39feaSAlan Cox static int int_initio_resel(struct initio_host * host);
15472d39feaSAlan Cox static int initio_sync_done(struct initio_host * host);
15572d39feaSAlan Cox static int wdtr_done(struct initio_host * host);
15672d39feaSAlan Cox static int wait_tulip(struct initio_host * host);
15772d39feaSAlan Cox static int initio_wait_done_disc(struct initio_host * host);
15872d39feaSAlan Cox static int initio_wait_disc(struct initio_host * host);
15972d39feaSAlan Cox static void tulip_scsi(struct initio_host * host);
16072d39feaSAlan Cox static int initio_post_scsi_rst(struct initio_host * host);
1611da177e4SLinus Torvalds 
16272d39feaSAlan Cox static void initio_se2_ew_en(unsigned long base);
16372d39feaSAlan Cox static void initio_se2_ew_ds(unsigned long base);
16472d39feaSAlan Cox static int initio_se2_rd_all(unsigned long base);
16572d39feaSAlan Cox static void initio_se2_update_all(unsigned long base);	/* setup default pattern */
16672d39feaSAlan Cox static void initio_read_eeprom(unsigned long base);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* ---- INTERNAL VARIABLES ---- */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static NVRAM i91unvram;
1711da177e4SLinus Torvalds static NVRAM *i91unvramp;
1721da177e4SLinus Torvalds 
17372d39feaSAlan Cox static u8 i91udftNvRam[64] =
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	/*----------- header -----------*/
1761da177e4SLinus Torvalds 	0x25, 0xc9,		/* Signature    */
1771da177e4SLinus Torvalds 	0x40,			/* Size         */
1781da177e4SLinus Torvalds 	0x01,			/* Revision     */
1791da177e4SLinus Torvalds 	/* -- Host Adapter Structure -- */
1801da177e4SLinus Torvalds 	0x95,			/* ModelByte0   */
1811da177e4SLinus Torvalds 	0x00,			/* ModelByte1   */
1821da177e4SLinus Torvalds 	0x00,			/* ModelInfo    */
1831da177e4SLinus Torvalds 	0x01,			/* NumOfCh      */
1841da177e4SLinus Torvalds 	NBC1_DEFAULT,		/* BIOSConfig1  */
1851da177e4SLinus Torvalds 	0,			/* BIOSConfig2  */
1861da177e4SLinus Torvalds 	0,			/* HAConfig1    */
1871da177e4SLinus Torvalds 	0,			/* HAConfig2    */
1881da177e4SLinus Torvalds 	/* SCSI channel 0 and target Structure  */
1891da177e4SLinus Torvalds 	7,			/* SCSIid       */
1901da177e4SLinus Torvalds 	NCC1_DEFAULT,		/* SCSIconfig1  */
1911da177e4SLinus Torvalds 	0,			/* SCSIconfig2  */
1921da177e4SLinus Torvalds 	0x10,			/* NumSCSItarget */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
1951da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
1961da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
1971da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	/* SCSI channel 1 and target Structure  */
2001da177e4SLinus Torvalds 	7,			/* SCSIid       */
2011da177e4SLinus Torvalds 	NCC1_DEFAULT,		/* SCSIconfig1  */
2021da177e4SLinus Torvalds 	0,			/* SCSIconfig2  */
2031da177e4SLinus Torvalds 	0x10,			/* NumSCSItarget */
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
2061da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
2071da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
2081da177e4SLinus Torvalds 	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
2091da177e4SLinus Torvalds 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2101da177e4SLinus Torvalds 	0, 0};			/*      - CheckSum -            */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 
21372d39feaSAlan Cox static u8 initio_rate_tbl[8] =	/* fast 20      */
2141da177e4SLinus Torvalds {
21525985edcSLucas De Marchi 				/* nanosecond divide by 4 */
2161da177e4SLinus Torvalds 	12,			/* 50ns,  20M   */
2171da177e4SLinus Torvalds 	18,			/* 75ns,  13.3M */
2181da177e4SLinus Torvalds 	25,			/* 100ns, 10M   */
2191da177e4SLinus Torvalds 	31,			/* 125ns, 8M    */
2201da177e4SLinus Torvalds 	37,			/* 150ns, 6.6M  */
2211da177e4SLinus Torvalds 	43,			/* 175ns, 5.7M  */
2221da177e4SLinus Torvalds 	50,			/* 200ns, 5M    */
2231da177e4SLinus Torvalds 	62			/* 250ns, 4M    */
2241da177e4SLinus Torvalds };
2251da177e4SLinus Torvalds 
initio_do_pause(unsigned amount)22672d39feaSAlan Cox static void initio_do_pause(unsigned amount)
22772d39feaSAlan Cox {
22872d39feaSAlan Cox 	/* Pause for amount jiffies */
2291da177e4SLinus Torvalds 	unsigned long the_time = jiffies + amount;
2301da177e4SLinus Torvalds 
23172d39feaSAlan Cox 	while (time_before_eq(jiffies, the_time))
23272d39feaSAlan Cox 		cpu_relax();
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds /*-- forward reference --*/
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /******************************************************************
2381da177e4SLinus Torvalds  Input: instruction for  Serial E2PROM
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds  EX: se2_rd(0 call se2_instr() to send address and read command
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	 StartBit  OP_Code   Address                Data
2431da177e4SLinus Torvalds 	 --------- --------  ------------------     -------
2441da177e4SLinus Torvalds 	 1         1 , 0     A5,A4,A3,A2,A1,A0      D15-D0
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 		 +-----------------------------------------------------
2471da177e4SLinus Torvalds 		 |
2481da177e4SLinus Torvalds  CS -----+
2491da177e4SLinus Torvalds 			+--+  +--+  +--+  +--+  +--+
2501da177e4SLinus Torvalds 			^  |  ^  |  ^  |  ^  |  ^  |
2511da177e4SLinus Torvalds 			|  |  |  |  |  |  |  |  |  |
2521da177e4SLinus Torvalds  CLK -------+  +--+  +--+  +--+  +--+  +--
2531da177e4SLinus Torvalds  (leading edge trigger)
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 		 +--1-----1--+
2561da177e4SLinus Torvalds 		 | SB    OP  |  OP    A5    A4
2571da177e4SLinus Torvalds  DI  ----+           +--0------------------
2581da177e4SLinus Torvalds  (address and cmd sent to nvram)
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	 -------------------------------------------+
2611da177e4SLinus Torvalds 												|
2621da177e4SLinus Torvalds  DO                                             +---
2631da177e4SLinus Torvalds  (data sent from nvram)
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds ******************************************************************/
26772d39feaSAlan Cox 
26872d39feaSAlan Cox /**
26972d39feaSAlan Cox  *	initio_se2_instr	-	bitbang an instruction
27072d39feaSAlan Cox  *	@base: Base of InitIO controller
27172d39feaSAlan Cox  *	@instr: Instruction for serial E2PROM
27272d39feaSAlan Cox  *
27372d39feaSAlan Cox  *	Bitbang an instruction out to the serial E2Prom
27472d39feaSAlan Cox  */
27572d39feaSAlan Cox 
initio_se2_instr(unsigned long base,u8 instr)27672d39feaSAlan Cox static void initio_se2_instr(unsigned long base, u8 instr)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	int i;
27972d39feaSAlan Cox 	u8 b;
2801da177e4SLinus Torvalds 
28172d39feaSAlan Cox 	outb(SE2CS | SE2DO, base + TUL_NVRAM);		/* cs+start bit */
28272d39feaSAlan Cox 	udelay(30);
28372d39feaSAlan Cox 	outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM);	/* +CLK */
28472d39feaSAlan Cox 	udelay(30);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	for (i = 0; i < 8; i++) {
2871da177e4SLinus Torvalds 		if (instr & 0x80)
2881da177e4SLinus Torvalds 			b = SE2CS | SE2DO;		/* -CLK+dataBit */
2891da177e4SLinus Torvalds 		else
2901da177e4SLinus Torvalds 			b = SE2CS;			/* -CLK */
29172d39feaSAlan Cox 		outb(b, base + TUL_NVRAM);
29272d39feaSAlan Cox 		udelay(30);
29372d39feaSAlan Cox 		outb(b | SE2CLK, base + TUL_NVRAM);	/* +CLK */
29472d39feaSAlan Cox 		udelay(30);
2951da177e4SLinus Torvalds 		instr <<= 1;
2961da177e4SLinus Torvalds 	}
29772d39feaSAlan Cox 	outb(SE2CS, base + TUL_NVRAM);			/* -CLK */
29872d39feaSAlan Cox 	udelay(30);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 
30272d39feaSAlan Cox /**
30372d39feaSAlan Cox  *	initio_se2_ew_en	-	Enable erase/write
30472d39feaSAlan Cox  *	@base: Base address of InitIO controller
30572d39feaSAlan Cox  *
30672d39feaSAlan Cox  *	Enable erase/write state of serial EEPROM
30772d39feaSAlan Cox  */
initio_se2_ew_en(unsigned long base)30872d39feaSAlan Cox void initio_se2_ew_en(unsigned long base)
3091da177e4SLinus Torvalds {
31072d39feaSAlan Cox 	initio_se2_instr(base, 0x30);	/* EWEN */
31172d39feaSAlan Cox 	outb(0, base + TUL_NVRAM);	/* -CS  */
31272d39feaSAlan Cox 	udelay(30);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 
31672d39feaSAlan Cox /**
31772d39feaSAlan Cox  *	initio_se2_ew_ds	-	Disable erase/write
31872d39feaSAlan Cox  *	@base: Base address of InitIO controller
31972d39feaSAlan Cox  *
32072d39feaSAlan Cox  *	Disable erase/write state of serial EEPROM
32172d39feaSAlan Cox  */
initio_se2_ew_ds(unsigned long base)32272d39feaSAlan Cox void initio_se2_ew_ds(unsigned long base)
3231da177e4SLinus Torvalds {
32472d39feaSAlan Cox 	initio_se2_instr(base, 0);	/* EWDS */
32572d39feaSAlan Cox 	outb(0, base + TUL_NVRAM);	/* -CS  */
32672d39feaSAlan Cox 	udelay(30);
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 
33072d39feaSAlan Cox /**
33172d39feaSAlan Cox  *	initio_se2_rd		-	read E2PROM word
33272d39feaSAlan Cox  *	@base: Base of InitIO controller
33372d39feaSAlan Cox  *	@addr: Address of word in E2PROM
33472d39feaSAlan Cox  *
33572d39feaSAlan Cox  *	Read a word from the NV E2PROM device
33672d39feaSAlan Cox  */
initio_se2_rd(unsigned long base,u8 addr)33772d39feaSAlan Cox static u16 initio_se2_rd(unsigned long base, u8 addr)
3381da177e4SLinus Torvalds {
33972d39feaSAlan Cox 	u8 instr, rb;
34072d39feaSAlan Cox 	u16 val = 0;
3411da177e4SLinus Torvalds 	int i;
3421da177e4SLinus Torvalds 
34372d39feaSAlan Cox 	instr = (u8) (addr | 0x80);
34472d39feaSAlan Cox 	initio_se2_instr(base, instr);	/* READ INSTR */
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	for (i = 15; i >= 0; i--) {
34772d39feaSAlan Cox 		outb(SE2CS | SE2CLK, base + TUL_NVRAM);	/* +CLK */
34872d39feaSAlan Cox 		udelay(30);
34972d39feaSAlan Cox 		outb(SE2CS, base + TUL_NVRAM);		/* -CLK */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		/* sample data after the following edge of clock  */
35272d39feaSAlan Cox 		rb = inb(base + TUL_NVRAM);
35372d39feaSAlan Cox 		rb &= SE2DI;
35472d39feaSAlan Cox 		val += (rb << i);
35572d39feaSAlan Cox 		udelay(30);	/* 6/20/95 */
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 
35872d39feaSAlan Cox 	outb(0, base + TUL_NVRAM);		/* no chip select */
35972d39feaSAlan Cox 	udelay(30);
36072d39feaSAlan Cox 	return val;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
36372d39feaSAlan Cox /**
36472d39feaSAlan Cox  *	initio_se2_wr		-	read E2PROM word
36572d39feaSAlan Cox  *	@base: Base of InitIO controller
36672d39feaSAlan Cox  *	@addr: Address of word in E2PROM
36772d39feaSAlan Cox  *	@val: Value to write
36872d39feaSAlan Cox  *
36972d39feaSAlan Cox  *	Write a word to the NV E2PROM device. Used when recovering from
37072d39feaSAlan Cox  *	a problem with the NV.
37172d39feaSAlan Cox  */
initio_se2_wr(unsigned long base,u8 addr,u16 val)37272d39feaSAlan Cox static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
3731da177e4SLinus Torvalds {
37472d39feaSAlan Cox 	u8 rb;
37572d39feaSAlan Cox 	u8 instr;
3761da177e4SLinus Torvalds 	int i;
3771da177e4SLinus Torvalds 
37872d39feaSAlan Cox 	instr = (u8) (addr | 0x40);
37972d39feaSAlan Cox 	initio_se2_instr(base, instr);	/* WRITE INSTR */
3801da177e4SLinus Torvalds 	for (i = 15; i >= 0; i--) {
38172d39feaSAlan Cox 		if (val & 0x8000)
38272d39feaSAlan Cox 			outb(SE2CS | SE2DO, base + TUL_NVRAM);	/* -CLK+dataBit 1 */
3831da177e4SLinus Torvalds 		else
38472d39feaSAlan Cox 			outb(SE2CS, base + TUL_NVRAM);		/* -CLK+dataBit 0 */
38572d39feaSAlan Cox 		udelay(30);
38672d39feaSAlan Cox 		outb(SE2CS | SE2CLK, base + TUL_NVRAM);		/* +CLK */
38772d39feaSAlan Cox 		udelay(30);
38872d39feaSAlan Cox 		val <<= 1;
3891da177e4SLinus Torvalds 	}
39072d39feaSAlan Cox 	outb(SE2CS, base + TUL_NVRAM);				/* -CLK */
39172d39feaSAlan Cox 	udelay(30);
39272d39feaSAlan Cox 	outb(0, base + TUL_NVRAM);				/* -CS  */
39372d39feaSAlan Cox 	udelay(30);
3941da177e4SLinus Torvalds 
39572d39feaSAlan Cox 	outb(SE2CS, base + TUL_NVRAM);				/* +CS  */
39672d39feaSAlan Cox 	udelay(30);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	for (;;) {
39972d39feaSAlan Cox 		outb(SE2CS | SE2CLK, base + TUL_NVRAM);		/* +CLK */
40072d39feaSAlan Cox 		udelay(30);
40172d39feaSAlan Cox 		outb(SE2CS, base + TUL_NVRAM);			/* -CLK */
40272d39feaSAlan Cox 		udelay(30);
40372d39feaSAlan Cox 		if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
4041da177e4SLinus Torvalds 			break;	/* write complete */
4051da177e4SLinus Torvalds 	}
40672d39feaSAlan Cox 	outb(0, base + TUL_NVRAM);				/* -CS */
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
40972d39feaSAlan Cox /**
41072d39feaSAlan Cox  *	initio_se2_rd_all	-	read hostadapter NV configuration
41172d39feaSAlan Cox  *	@base: Base address of InitIO controller
41272d39feaSAlan Cox  *
41372d39feaSAlan Cox  *	Reads the E2PROM data into main memory. Ensures that the checksum
41472d39feaSAlan Cox  *	and header marker are valid. Returns 1 on success -1 on error.
41572d39feaSAlan Cox  */
4161da177e4SLinus Torvalds 
initio_se2_rd_all(unsigned long base)41772d39feaSAlan Cox static int initio_se2_rd_all(unsigned long base)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	int i;
42072d39feaSAlan Cox 	u16 chksum = 0;
42172d39feaSAlan Cox 	u16 *np;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	i91unvramp = &i91unvram;
42472d39feaSAlan Cox 	np = (u16 *) i91unvramp;
42572d39feaSAlan Cox 	for (i = 0; i < 32; i++)
42672d39feaSAlan Cox 		*np++ = initio_se2_rd(base, i);
4271da177e4SLinus Torvalds 
42872d39feaSAlan Cox 	/* Is signature "ini" ok ? */
4291da177e4SLinus Torvalds 	if (i91unvramp->NVM_Signature != INI_SIGNATURE)
4301da177e4SLinus Torvalds 		return -1;
43172d39feaSAlan Cox 	/* Is ckecksum ok ? */
43272d39feaSAlan Cox 	np = (u16 *) i91unvramp;
4331da177e4SLinus Torvalds 	for (i = 0; i < 31; i++)
4341da177e4SLinus Torvalds 		chksum += *np++;
43572d39feaSAlan Cox 	if (i91unvramp->NVM_CheckSum != chksum)
4361da177e4SLinus Torvalds 		return -1;
4371da177e4SLinus Torvalds 	return 1;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
44072d39feaSAlan Cox /**
44172d39feaSAlan Cox  *	initio_se2_update_all		-	Update E2PROM
44272d39feaSAlan Cox  *	@base: Base of InitIO controller
44372d39feaSAlan Cox  *
44472d39feaSAlan Cox  *	Update the E2PROM by wrting any changes into the E2PROM
44572d39feaSAlan Cox  *	chip, rewriting the checksum.
44672d39feaSAlan Cox  */
initio_se2_update_all(unsigned long base)44772d39feaSAlan Cox static void initio_se2_update_all(unsigned long base)
4481da177e4SLinus Torvalds {				/* setup default pattern */
4491da177e4SLinus Torvalds 	int i;
45072d39feaSAlan Cox 	u16 chksum = 0;
45172d39feaSAlan Cox 	u16 *np, *np1;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	i91unvramp = &i91unvram;
4541da177e4SLinus Torvalds 	/* Calculate checksum first */
45572d39feaSAlan Cox 	np = (u16 *) i91udftNvRam;
4561da177e4SLinus Torvalds 	for (i = 0; i < 31; i++)
4571da177e4SLinus Torvalds 		chksum += *np++;
45872d39feaSAlan Cox 	*np = chksum;
45972d39feaSAlan Cox 	initio_se2_ew_en(base);	/* Enable write  */
4601da177e4SLinus Torvalds 
46172d39feaSAlan Cox 	np = (u16 *) i91udftNvRam;
46272d39feaSAlan Cox 	np1 = (u16 *) i91unvramp;
4631da177e4SLinus Torvalds 	for (i = 0; i < 32; i++, np++, np1++) {
46472d39feaSAlan Cox 		if (*np != *np1)
46572d39feaSAlan Cox 			initio_se2_wr(base, i, *np);
4661da177e4SLinus Torvalds 	}
46772d39feaSAlan Cox 	initio_se2_ew_ds(base);	/* Disable write   */
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
47072d39feaSAlan Cox /**
47172d39feaSAlan Cox  *	initio_read_eeprom		-	Retrieve configuration
47272d39feaSAlan Cox  *	@base: Base of InitIO Host Adapter
47372d39feaSAlan Cox  *
47472d39feaSAlan Cox  *	Retrieve the host adapter configuration data from E2Prom. If the
47572d39feaSAlan Cox  *	data is invalid then the defaults are used and are also restored
47672d39feaSAlan Cox  *	into the E2PROM. This forms the access point for the SCSI driver
47772d39feaSAlan Cox  *	into the E2PROM layer, the other functions for the E2PROM are all
47872d39feaSAlan Cox  *	internal use.
47972d39feaSAlan Cox  *
48072d39feaSAlan Cox  *	Must be called single threaded, uses a shared global area.
48172d39feaSAlan Cox  */
4821da177e4SLinus Torvalds 
initio_read_eeprom(unsigned long base)48372d39feaSAlan Cox static void initio_read_eeprom(unsigned long base)
4841da177e4SLinus Torvalds {
48572d39feaSAlan Cox 	u8 gctrl;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	i91unvramp = &i91unvram;
48872d39feaSAlan Cox 	/* Enable EEProm programming */
48972d39feaSAlan Cox 	gctrl = inb(base + TUL_GCTRL);
49072d39feaSAlan Cox 	outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
49172d39feaSAlan Cox 	if (initio_se2_rd_all(base) != 1) {
49272d39feaSAlan Cox 		initio_se2_update_all(base);	/* setup default pattern */
49372d39feaSAlan Cox 		initio_se2_rd_all(base);	/* load again  */
4941da177e4SLinus Torvalds 	}
49572d39feaSAlan Cox 	/* Disable EEProm programming */
49672d39feaSAlan Cox 	gctrl = inb(base + TUL_GCTRL);
49772d39feaSAlan Cox 	outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
50072d39feaSAlan Cox /**
50172d39feaSAlan Cox  *	initio_stop_bm		-	stop bus master
50272d39feaSAlan Cox  *	@host: InitIO we are stopping
50372d39feaSAlan Cox  *
5043ad2f3fbSDaniel Mack  *	Stop any pending DMA operation, aborting the DMA if necessary
50572d39feaSAlan Cox  */
5061da177e4SLinus Torvalds 
initio_stop_bm(struct initio_host * host)50772d39feaSAlan Cox static void initio_stop_bm(struct initio_host * host)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 
51072d39feaSAlan Cox 	if (inb(host->addr + TUL_XStatus) & XPEND) {	/* if DMA xfer is pending, abort DMA xfer */
51172d39feaSAlan Cox 		outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
5121da177e4SLinus Torvalds 		/* wait Abort DMA xfer done */
51372d39feaSAlan Cox 		while ((inb(host->addr + TUL_Int) & XABT) == 0)
51472d39feaSAlan Cox 			cpu_relax();
5151da177e4SLinus Torvalds 	}
51672d39feaSAlan Cox 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
51972d39feaSAlan Cox /**
52072d39feaSAlan Cox  *	initio_reset_scsi		-	Reset SCSI host controller
52172d39feaSAlan Cox  *	@host: InitIO host to reset
52272d39feaSAlan Cox  *	@seconds: Recovery time
52372d39feaSAlan Cox  *
52472d39feaSAlan Cox  *	Perform a full reset of the SCSI subsystem.
52572d39feaSAlan Cox  */
52672d39feaSAlan Cox 
initio_reset_scsi(struct initio_host * host,int seconds)52772d39feaSAlan Cox static int initio_reset_scsi(struct initio_host * host, int seconds)
5281da177e4SLinus Torvalds {
52972d39feaSAlan Cox 	outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
5301da177e4SLinus Torvalds 
53172d39feaSAlan Cox 	while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
53272d39feaSAlan Cox 		cpu_relax();
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	/* reset tulip chip */
53572d39feaSAlan Cox 	outb(0, host->addr + TUL_SSignal);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	/* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
5381da177e4SLinus Torvalds 	/* SONY 5200 tape drive won't work if only stall for 1 sec */
53972d39feaSAlan Cox 	/* FIXME: this is a very long busy wait right now */
54072d39feaSAlan Cox 	initio_do_pause(seconds * HZ);
5411da177e4SLinus Torvalds 
54272d39feaSAlan Cox 	inb(host->addr + TUL_SInt);
54372d39feaSAlan Cox 	return SCSI_RESET_SUCCESS;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
54672d39feaSAlan Cox /**
54772d39feaSAlan Cox  *	initio_init		-	set up an InitIO host adapter
54872d39feaSAlan Cox  *	@host: InitIO host adapter
54972d39feaSAlan Cox  *	@bios_addr: BIOS address
55072d39feaSAlan Cox  *
55172d39feaSAlan Cox  *	Set up the host adapter and devices according to the configuration
55272d39feaSAlan Cox  *	retrieved from the E2PROM.
55372d39feaSAlan Cox  *
55472d39feaSAlan Cox  *	Locking: Calls E2PROM layer code which is not re-enterable so must
55572d39feaSAlan Cox  *	run single threaded for now.
55672d39feaSAlan Cox  */
55772d39feaSAlan Cox 
initio_init(struct initio_host * host,u8 * bios_addr)55872d39feaSAlan Cox static void initio_init(struct initio_host * host, u8 *bios_addr)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	int i;
56172d39feaSAlan Cox 	u8 *flags;
56272d39feaSAlan Cox 	u8 *heads;
5631da177e4SLinus Torvalds 
56472d39feaSAlan Cox 	/* Get E2Prom configuration */
56572d39feaSAlan Cox 	initio_read_eeprom(host->addr);
5661da177e4SLinus Torvalds 	if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
56772d39feaSAlan Cox 		host->max_tar = 8;
5681da177e4SLinus Torvalds 	else
56972d39feaSAlan Cox 		host->max_tar = 16;
5701da177e4SLinus Torvalds 
57172d39feaSAlan Cox 	host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
5721da177e4SLinus Torvalds 
57372d39feaSAlan Cox 	host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
57472d39feaSAlan Cox 	host->idmask = ~(1 << host->scsi_id);
5751da177e4SLinus Torvalds 
57644456d37SOlaf Hering #ifdef CHK_PARITY
5771da177e4SLinus Torvalds 	/* Enable parity error response */
57872d39feaSAlan Cox 	outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
5791da177e4SLinus Torvalds #endif
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	/* Mask all the interrupt       */
58272d39feaSAlan Cox 	outb(0x1F, host->addr + TUL_Mask);
5831da177e4SLinus Torvalds 
58472d39feaSAlan Cox 	initio_stop_bm(host);
5851da177e4SLinus Torvalds 	/* --- Initialize the tulip --- */
58672d39feaSAlan Cox 	outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	/* program HBA's SCSI ID        */
58972d39feaSAlan Cox 	outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	/* Enable Initiator Mode ,phase latch,alternate sync period mode,
5921da177e4SLinus Torvalds 	   disable SCSI reset */
59372d39feaSAlan Cox 	if (host->config & HCC_EN_PAR)
59472d39feaSAlan Cox 		host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
5951da177e4SLinus Torvalds 	else
59672d39feaSAlan Cox 		host->sconf1 = (TSC_INITDEFAULT);
59772d39feaSAlan Cox 	outb(host->sconf1, host->addr + TUL_SConfig);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/* Enable HW reselect */
60072d39feaSAlan Cox 	outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
6011da177e4SLinus Torvalds 
60272d39feaSAlan Cox 	outb(0, host->addr + TUL_SPeriod);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	/* selection time out = 250 ms */
60572d39feaSAlan Cox 	outb(153, host->addr + TUL_STimeOut);
6061da177e4SLinus Torvalds 
60772d39feaSAlan Cox 	/* Enable SCSI terminator */
60872d39feaSAlan Cox 	outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
60972d39feaSAlan Cox 		host->addr + TUL_XCtrl);
61072d39feaSAlan Cox 	outb(((host->config & HCC_AUTO_TERM) >> 4) |
61172d39feaSAlan Cox 		(inb(host->addr + TUL_GCTRL1) & 0xFE),
61272d39feaSAlan Cox 		host->addr + TUL_GCTRL1);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	for (i = 0,
61572d39feaSAlan Cox 	     flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
61672d39feaSAlan Cox 	     heads = bios_addr + 0x180;
61772d39feaSAlan Cox 	     i < host->max_tar;
61872d39feaSAlan Cox 	     i++, flags++) {
61972d39feaSAlan Cox 		host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
62072d39feaSAlan Cox 		if (host->targets[i].flags & TCF_EN_255)
62172d39feaSAlan Cox 			host->targets[i].drv_flags = TCF_DRV_255_63;
6221da177e4SLinus Torvalds 		else
62372d39feaSAlan Cox 			host->targets[i].drv_flags = 0;
62472d39feaSAlan Cox 		host->targets[i].js_period = 0;
62572d39feaSAlan Cox 		host->targets[i].sconfig0 = host->sconf1;
62672d39feaSAlan Cox 		host->targets[i].heads = *heads++;
62772d39feaSAlan Cox 		if (host->targets[i].heads == 255)
62872d39feaSAlan Cox 			host->targets[i].drv_flags = TCF_DRV_255_63;
6291da177e4SLinus Torvalds 		else
63072d39feaSAlan Cox 			host->targets[i].drv_flags = 0;
63172d39feaSAlan Cox 		host->targets[i].sectors = *heads++;
63272d39feaSAlan Cox 		host->targets[i].flags &= ~TCF_BUSY;
63372d39feaSAlan Cox 		host->act_tags[i] = 0;
63472d39feaSAlan Cox 		host->max_tags[i] = 0xFF;
6351da177e4SLinus Torvalds 	}			/* for                          */
6361da177e4SLinus Torvalds 	printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
637e9e42fafSAlan Cox 	       host->addr, host->pci_dev->irq,
63872d39feaSAlan Cox 	       host->bios_addr, host->scsi_id);
63972d39feaSAlan Cox 	/* Reset SCSI Bus */
64072d39feaSAlan Cox 	if (host->config & HCC_SCSI_RESET) {
64172d39feaSAlan Cox 		printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
64272d39feaSAlan Cox 		initio_reset_scsi(host, 10);
6431da177e4SLinus Torvalds 	}
64472d39feaSAlan Cox 	outb(0x17, host->addr + TUL_SCFG1);
64572d39feaSAlan Cox 	outb(0xE9, host->addr + TUL_SIntEnable);
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
64872d39feaSAlan Cox /**
64972d39feaSAlan Cox  *	initio_alloc_scb		-	Allocate an SCB
65072d39feaSAlan Cox  *	@host: InitIO host we are allocating for
65172d39feaSAlan Cox  *
65272d39feaSAlan Cox  *	Walk the SCB list for the controller and allocate a free SCB if
65372d39feaSAlan Cox  *	one exists.
65472d39feaSAlan Cox  */
initio_alloc_scb(struct initio_host * host)65572d39feaSAlan Cox static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
6561da177e4SLinus Torvalds {
65772d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
65872d39feaSAlan Cox 	unsigned long flags;
65972d39feaSAlan Cox 
66072d39feaSAlan Cox 	spin_lock_irqsave(&host->avail_lock, flags);
66172d39feaSAlan Cox 	if ((scb = host->first_avail) != NULL) {
6621da177e4SLinus Torvalds #if DEBUG_QUEUE
66372d39feaSAlan Cox 		printk("find scb at %p\n", scb);
6641da177e4SLinus Torvalds #endif
66572d39feaSAlan Cox 		if ((host->first_avail = scb->next) == NULL)
66672d39feaSAlan Cox 			host->last_avail = NULL;
66772d39feaSAlan Cox 		scb->next = NULL;
66872d39feaSAlan Cox 		scb->status = SCB_RENT;
6691da177e4SLinus Torvalds 	}
67072d39feaSAlan Cox 	spin_unlock_irqrestore(&host->avail_lock, flags);
67172d39feaSAlan Cox 	return scb;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
67472d39feaSAlan Cox /**
67572d39feaSAlan Cox  *	initio_release_scb		-	Release an SCB
67672d39feaSAlan Cox  *	@host: InitIO host that owns the SCB
67772d39feaSAlan Cox  *	@cmnd: SCB command block being returned
67872d39feaSAlan Cox  *
67972d39feaSAlan Cox  *	Return an allocated SCB to the host free list
68072d39feaSAlan Cox  */
68172d39feaSAlan Cox 
initio_release_scb(struct initio_host * host,struct scsi_ctrl_blk * cmnd)68272d39feaSAlan Cox static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
6831da177e4SLinus Torvalds {
68472d39feaSAlan Cox 	unsigned long flags;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds #if DEBUG_QUEUE
68772d39feaSAlan Cox 	printk("Release SCB %p; ", cmnd);
6881da177e4SLinus Torvalds #endif
68972d39feaSAlan Cox 	spin_lock_irqsave(&(host->avail_lock), flags);
69072d39feaSAlan Cox 	cmnd->srb = NULL;
69172d39feaSAlan Cox 	cmnd->status = 0;
69272d39feaSAlan Cox 	cmnd->next = NULL;
69372d39feaSAlan Cox 	if (host->last_avail != NULL) {
69472d39feaSAlan Cox 		host->last_avail->next = cmnd;
69572d39feaSAlan Cox 		host->last_avail = cmnd;
6961da177e4SLinus Torvalds 	} else {
69772d39feaSAlan Cox 		host->first_avail = cmnd;
69872d39feaSAlan Cox 		host->last_avail = cmnd;
6991da177e4SLinus Torvalds 	}
70072d39feaSAlan Cox 	spin_unlock_irqrestore(&(host->avail_lock), flags);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /***************************************************************************/
initio_append_pend_scb(struct initio_host * host,struct scsi_ctrl_blk * scbp)70472d39feaSAlan Cox static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds #if DEBUG_QUEUE
70872d39feaSAlan Cox 	printk("Append pend SCB %p; ", scbp);
7091da177e4SLinus Torvalds #endif
71072d39feaSAlan Cox 	scbp->status = SCB_PEND;
71172d39feaSAlan Cox 	scbp->next = NULL;
71272d39feaSAlan Cox 	if (host->last_pending != NULL) {
71372d39feaSAlan Cox 		host->last_pending->next = scbp;
71472d39feaSAlan Cox 		host->last_pending = scbp;
7151da177e4SLinus Torvalds 	} else {
71672d39feaSAlan Cox 		host->first_pending = scbp;
71772d39feaSAlan Cox 		host->last_pending = scbp;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /***************************************************************************/
initio_push_pend_scb(struct initio_host * host,struct scsi_ctrl_blk * scbp)72272d39feaSAlan Cox static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds #if DEBUG_QUEUE
72672d39feaSAlan Cox 	printk("Push pend SCB %p; ", scbp);
7271da177e4SLinus Torvalds #endif
72872d39feaSAlan Cox 	scbp->status = SCB_PEND;
72972d39feaSAlan Cox 	if ((scbp->next = host->first_pending) != NULL) {
73072d39feaSAlan Cox 		host->first_pending = scbp;
7311da177e4SLinus Torvalds 	} else {
73272d39feaSAlan Cox 		host->first_pending = scbp;
73372d39feaSAlan Cox 		host->last_pending = scbp;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
initio_find_first_pend_scb(struct initio_host * host)73772d39feaSAlan Cox static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
7381da177e4SLinus Torvalds {
73972d39feaSAlan Cox 	struct scsi_ctrl_blk *first;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 
74272d39feaSAlan Cox 	first = host->first_pending;
74372d39feaSAlan Cox 	while (first != NULL) {
74472d39feaSAlan Cox 		if (first->opcode != ExecSCSI)
74572d39feaSAlan Cox 			return first;
74672d39feaSAlan Cox 		if (first->tagmsg == 0) {
74772d39feaSAlan Cox 			if ((host->act_tags[first->target] == 0) &&
74872d39feaSAlan Cox 			    !(host->targets[first->target].flags & TCF_BUSY))
74972d39feaSAlan Cox 				return first;
7501da177e4SLinus Torvalds 		} else {
75172d39feaSAlan Cox 			if ((host->act_tags[first->target] >=
75272d39feaSAlan Cox 			  host->max_tags[first->target]) |
75372d39feaSAlan Cox 			    (host->targets[first->target].flags & TCF_BUSY)) {
75472d39feaSAlan Cox 				first = first->next;
7551da177e4SLinus Torvalds 				continue;
7561da177e4SLinus Torvalds 			}
75772d39feaSAlan Cox 			return first;
7581da177e4SLinus Torvalds 		}
75972d39feaSAlan Cox 		first = first->next;
76072d39feaSAlan Cox 	}
76172d39feaSAlan Cox 	return first;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
initio_unlink_pend_scb(struct initio_host * host,struct scsi_ctrl_blk * scb)76472d39feaSAlan Cox static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
7651da177e4SLinus Torvalds {
76672d39feaSAlan Cox 	struct scsi_ctrl_blk *tmp, *prev;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds #if DEBUG_QUEUE
76972d39feaSAlan Cox 	printk("unlink pend SCB %p; ", scb);
7701da177e4SLinus Torvalds #endif
7711da177e4SLinus Torvalds 
77272d39feaSAlan Cox 	prev = tmp = host->first_pending;
77372d39feaSAlan Cox 	while (tmp != NULL) {
77472d39feaSAlan Cox 		if (scb == tmp) {	/* Unlink this SCB              */
77572d39feaSAlan Cox 			if (tmp == host->first_pending) {
77672d39feaSAlan Cox 				if ((host->first_pending = tmp->next) == NULL)
77772d39feaSAlan Cox 					host->last_pending = NULL;
7781da177e4SLinus Torvalds 			} else {
77972d39feaSAlan Cox 				prev->next = tmp->next;
78072d39feaSAlan Cox 				if (tmp == host->last_pending)
78172d39feaSAlan Cox 					host->last_pending = prev;
7821da177e4SLinus Torvalds 			}
78372d39feaSAlan Cox 			tmp->next = NULL;
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 		}
78672d39feaSAlan Cox 		prev = tmp;
78772d39feaSAlan Cox 		tmp = tmp->next;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds }
79072d39feaSAlan Cox 
initio_append_busy_scb(struct initio_host * host,struct scsi_ctrl_blk * scbp)79172d39feaSAlan Cox static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds #if DEBUG_QUEUE
795e2d435eaSStuart Swales 	printk("append busy SCB %p; ", scbp);
7961da177e4SLinus Torvalds #endif
79772d39feaSAlan Cox 	if (scbp->tagmsg)
79872d39feaSAlan Cox 		host->act_tags[scbp->target]++;
7991da177e4SLinus Torvalds 	else
80072d39feaSAlan Cox 		host->targets[scbp->target].flags |= TCF_BUSY;
80172d39feaSAlan Cox 	scbp->status = SCB_BUSY;
80272d39feaSAlan Cox 	scbp->next = NULL;
80372d39feaSAlan Cox 	if (host->last_busy != NULL) {
80472d39feaSAlan Cox 		host->last_busy->next = scbp;
80572d39feaSAlan Cox 		host->last_busy = scbp;
8061da177e4SLinus Torvalds 	} else {
80772d39feaSAlan Cox 		host->first_busy = scbp;
80872d39feaSAlan Cox 		host->last_busy = scbp;
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /***************************************************************************/
initio_pop_busy_scb(struct initio_host * host)81372d39feaSAlan Cox static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
8141da177e4SLinus Torvalds {
81572d39feaSAlan Cox 	struct scsi_ctrl_blk *tmp;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 
81872d39feaSAlan Cox 	if ((tmp = host->first_busy) != NULL) {
81972d39feaSAlan Cox 		if ((host->first_busy = tmp->next) == NULL)
82072d39feaSAlan Cox 			host->last_busy = NULL;
82172d39feaSAlan Cox 		tmp->next = NULL;
82272d39feaSAlan Cox 		if (tmp->tagmsg)
82372d39feaSAlan Cox 			host->act_tags[tmp->target]--;
8241da177e4SLinus Torvalds 		else
82572d39feaSAlan Cox 			host->targets[tmp->target].flags &= ~TCF_BUSY;
8261da177e4SLinus Torvalds 	}
8271da177e4SLinus Torvalds #if DEBUG_QUEUE
82872d39feaSAlan Cox 	printk("Pop busy SCB %p; ", tmp);
8291da177e4SLinus Torvalds #endif
83072d39feaSAlan Cox 	return tmp;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds /***************************************************************************/
initio_unlink_busy_scb(struct initio_host * host,struct scsi_ctrl_blk * scb)83472d39feaSAlan Cox static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
8351da177e4SLinus Torvalds {
83672d39feaSAlan Cox 	struct scsi_ctrl_blk *tmp, *prev;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds #if DEBUG_QUEUE
83972d39feaSAlan Cox 	printk("unlink busy SCB %p; ", scb);
8401da177e4SLinus Torvalds #endif
8411da177e4SLinus Torvalds 
84272d39feaSAlan Cox 	prev = tmp = host->first_busy;
84372d39feaSAlan Cox 	while (tmp != NULL) {
84472d39feaSAlan Cox 		if (scb == tmp) {	/* Unlink this SCB              */
84572d39feaSAlan Cox 			if (tmp == host->first_busy) {
84672d39feaSAlan Cox 				if ((host->first_busy = tmp->next) == NULL)
84772d39feaSAlan Cox 					host->last_busy = NULL;
8481da177e4SLinus Torvalds 			} else {
84972d39feaSAlan Cox 				prev->next = tmp->next;
85072d39feaSAlan Cox 				if (tmp == host->last_busy)
85172d39feaSAlan Cox 					host->last_busy = prev;
8521da177e4SLinus Torvalds 			}
85372d39feaSAlan Cox 			tmp->next = NULL;
85472d39feaSAlan Cox 			if (tmp->tagmsg)
85572d39feaSAlan Cox 				host->act_tags[tmp->target]--;
8561da177e4SLinus Torvalds 			else
85772d39feaSAlan Cox 				host->targets[tmp->target].flags &= ~TCF_BUSY;
8581da177e4SLinus Torvalds 			break;
8591da177e4SLinus Torvalds 		}
86072d39feaSAlan Cox 		prev = tmp;
86172d39feaSAlan Cox 		tmp = tmp->next;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 	return;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
initio_find_busy_scb(struct initio_host * host,u16 tarlun)86672d39feaSAlan Cox struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
8671da177e4SLinus Torvalds {
86878e40ac8SLee Jones 	struct scsi_ctrl_blk *tmp;
86972d39feaSAlan Cox 	u16 scbp_tarlun;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 
87278e40ac8SLee Jones 	tmp = host->first_busy;
87372d39feaSAlan Cox 	while (tmp != NULL) {
87472d39feaSAlan Cox 		scbp_tarlun = (tmp->lun << 8) | (tmp->target);
8751da177e4SLinus Torvalds 		if (scbp_tarlun == tarlun) {	/* Unlink this SCB              */
8761da177e4SLinus Torvalds 			break;
8771da177e4SLinus Torvalds 		}
87872d39feaSAlan Cox 		tmp = tmp->next;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds #if DEBUG_QUEUE
88172d39feaSAlan Cox 	printk("find busy SCB %p; ", tmp);
8821da177e4SLinus Torvalds #endif
88372d39feaSAlan Cox 	return tmp;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
initio_append_done_scb(struct initio_host * host,struct scsi_ctrl_blk * scbp)88672d39feaSAlan Cox static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds #if DEBUG_QUEUE
88972d39feaSAlan Cox 	printk("append done SCB %p; ", scbp);
8901da177e4SLinus Torvalds #endif
8911da177e4SLinus Torvalds 
89272d39feaSAlan Cox 	scbp->status = SCB_DONE;
89372d39feaSAlan Cox 	scbp->next = NULL;
89472d39feaSAlan Cox 	if (host->last_done != NULL) {
89572d39feaSAlan Cox 		host->last_done->next = scbp;
89672d39feaSAlan Cox 		host->last_done = scbp;
8971da177e4SLinus Torvalds 	} else {
89872d39feaSAlan Cox 		host->first_done = scbp;
89972d39feaSAlan Cox 		host->last_done = scbp;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
initio_find_done_scb(struct initio_host * host)90372d39feaSAlan Cox struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
9041da177e4SLinus Torvalds {
90572d39feaSAlan Cox 	struct scsi_ctrl_blk *tmp;
9061da177e4SLinus Torvalds 
90772d39feaSAlan Cox 	if ((tmp = host->first_done) != NULL) {
90872d39feaSAlan Cox 		if ((host->first_done = tmp->next) == NULL)
90972d39feaSAlan Cox 			host->last_done = NULL;
91072d39feaSAlan Cox 		tmp->next = NULL;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds #if DEBUG_QUEUE
91372d39feaSAlan Cox 	printk("find done SCB %p; ",tmp);
9141da177e4SLinus Torvalds #endif
91572d39feaSAlan Cox 	return tmp;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
initio_abort_srb(struct initio_host * host,struct scsi_cmnd * srbp)91872d39feaSAlan Cox static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
9191da177e4SLinus Torvalds {
92072d39feaSAlan Cox 	unsigned long flags;
92172d39feaSAlan Cox 	struct scsi_ctrl_blk *tmp, *prev;
9221da177e4SLinus Torvalds 
92372d39feaSAlan Cox 	spin_lock_irqsave(&host->semaph_lock, flags);
9241da177e4SLinus Torvalds 
92572d39feaSAlan Cox 	if ((host->semaph == 0) && (host->active == NULL)) {
9261da177e4SLinus Torvalds 		/* disable Jasmin SCSI Int        */
92772d39feaSAlan Cox 		outb(0x1F, host->addr + TUL_Mask);
92872d39feaSAlan Cox 		spin_unlock_irqrestore(&host->semaph_lock, flags);
92972d39feaSAlan Cox 		/* FIXME: synchronize_irq needed ? */
93072d39feaSAlan Cox 		tulip_main(host);
93172d39feaSAlan Cox 		spin_lock_irqsave(&host->semaph_lock, flags);
93272d39feaSAlan Cox 		host->semaph = 1;
93372d39feaSAlan Cox 		outb(0x0F, host->addr + TUL_Mask);
93472d39feaSAlan Cox 		spin_unlock_irqrestore(&host->semaph_lock, flags);
9351da177e4SLinus Torvalds 		return SCSI_ABORT_SNOOZE;
9361da177e4SLinus Torvalds 	}
93772d39feaSAlan Cox 	prev = tmp = host->first_pending;	/* Check Pend queue */
93872d39feaSAlan Cox 	while (tmp != NULL) {
9391da177e4SLinus Torvalds 		/* 07/27/98 */
94072d39feaSAlan Cox 		if (tmp->srb == srbp) {
94172d39feaSAlan Cox 			if (tmp == host->active) {
94272d39feaSAlan Cox 				spin_unlock_irqrestore(&host->semaph_lock, flags);
9431da177e4SLinus Torvalds 				return SCSI_ABORT_BUSY;
94472d39feaSAlan Cox 			} else if (tmp == host->first_pending) {
94572d39feaSAlan Cox 				if ((host->first_pending = tmp->next) == NULL)
94672d39feaSAlan Cox 					host->last_pending = NULL;
9471da177e4SLinus Torvalds 			} else {
94872d39feaSAlan Cox 				prev->next = tmp->next;
94972d39feaSAlan Cox 				if (tmp == host->last_pending)
95072d39feaSAlan Cox 					host->last_pending = prev;
9511da177e4SLinus Torvalds 			}
95272d39feaSAlan Cox 			tmp->hastat = HOST_ABORTED;
95372d39feaSAlan Cox 			tmp->flags |= SCF_DONE;
95472d39feaSAlan Cox 			if (tmp->flags & SCF_POST)
95572d39feaSAlan Cox 				(*tmp->post) ((u8 *) host, (u8 *) tmp);
95672d39feaSAlan Cox 			spin_unlock_irqrestore(&host->semaph_lock, flags);
9571da177e4SLinus Torvalds 			return SCSI_ABORT_SUCCESS;
9581da177e4SLinus Torvalds 		}
95972d39feaSAlan Cox 		prev = tmp;
96072d39feaSAlan Cox 		tmp = tmp->next;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
96372d39feaSAlan Cox 	prev = tmp = host->first_busy;	/* Check Busy queue */
96472d39feaSAlan Cox 	while (tmp != NULL) {
96572d39feaSAlan Cox 		if (tmp->srb == srbp) {
96672d39feaSAlan Cox 			if (tmp == host->active) {
96772d39feaSAlan Cox 				spin_unlock_irqrestore(&host->semaph_lock, flags);
9681da177e4SLinus Torvalds 				return SCSI_ABORT_BUSY;
96972d39feaSAlan Cox 			} else if (tmp->tagmsg == 0) {
97072d39feaSAlan Cox 				spin_unlock_irqrestore(&host->semaph_lock, flags);
9711da177e4SLinus Torvalds 				return SCSI_ABORT_BUSY;
9721da177e4SLinus Torvalds 			} else {
97372d39feaSAlan Cox 				host->act_tags[tmp->target]--;
97472d39feaSAlan Cox 				if (tmp == host->first_busy) {
97572d39feaSAlan Cox 					if ((host->first_busy = tmp->next) == NULL)
97672d39feaSAlan Cox 						host->last_busy = NULL;
9771da177e4SLinus Torvalds 				} else {
97872d39feaSAlan Cox 					prev->next = tmp->next;
97972d39feaSAlan Cox 					if (tmp == host->last_busy)
98072d39feaSAlan Cox 						host->last_busy = prev;
9811da177e4SLinus Torvalds 				}
98272d39feaSAlan Cox 				tmp->next = NULL;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 
98572d39feaSAlan Cox 				tmp->hastat = HOST_ABORTED;
98672d39feaSAlan Cox 				tmp->flags |= SCF_DONE;
98772d39feaSAlan Cox 				if (tmp->flags & SCF_POST)
98872d39feaSAlan Cox 					(*tmp->post) ((u8 *) host, (u8 *) tmp);
98972d39feaSAlan Cox 				spin_unlock_irqrestore(&host->semaph_lock, flags);
9901da177e4SLinus Torvalds 				return SCSI_ABORT_SUCCESS;
9911da177e4SLinus Torvalds 			}
9921da177e4SLinus Torvalds 		}
99372d39feaSAlan Cox 		prev = tmp;
99472d39feaSAlan Cox 		tmp = tmp->next;
9951da177e4SLinus Torvalds 	}
99672d39feaSAlan Cox 	spin_unlock_irqrestore(&host->semaph_lock, flags);
99772d39feaSAlan Cox 	return SCSI_ABORT_NOT_RUNNING;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /***************************************************************************/
initio_bad_seq(struct initio_host * host)100172d39feaSAlan Cox static int initio_bad_seq(struct initio_host * host)
10021da177e4SLinus Torvalds {
100372d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
10041da177e4SLinus Torvalds 
100572d39feaSAlan Cox 	printk("initio_bad_seg c=%d\n", host->index);
10061da177e4SLinus Torvalds 
100772d39feaSAlan Cox 	if ((scb = host->active) != NULL) {
100872d39feaSAlan Cox 		initio_unlink_busy_scb(host, scb);
100972d39feaSAlan Cox 		scb->hastat = HOST_BAD_PHAS;
101072d39feaSAlan Cox 		scb->tastat = 0;
101172d39feaSAlan Cox 		initio_append_done_scb(host, scb);
10121da177e4SLinus Torvalds 	}
101372d39feaSAlan Cox 	initio_stop_bm(host);
101472d39feaSAlan Cox 	initio_reset_scsi(host, 8);	/* 7/29/98 */
101572d39feaSAlan Cox 	return initio_post_scsi_rst(host);
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
1018a2ba192cSAdrian Bunk 
10191da177e4SLinus Torvalds /************************************************************************/
initio_exec_scb(struct initio_host * host,struct scsi_ctrl_blk * scb)102072d39feaSAlan Cox static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
10211da177e4SLinus Torvalds {
102272d39feaSAlan Cox 	unsigned long flags;
10231da177e4SLinus Torvalds 
102472d39feaSAlan Cox 	scb->mode = 0;
10251da177e4SLinus Torvalds 
102672d39feaSAlan Cox 	scb->sgidx = 0;
102772d39feaSAlan Cox 	scb->sgmax = scb->sglen;
10281da177e4SLinus Torvalds 
102972d39feaSAlan Cox 	spin_lock_irqsave(&host->semaph_lock, flags);
10301da177e4SLinus Torvalds 
103172d39feaSAlan Cox 	initio_append_pend_scb(host, scb);	/* Append this SCB to Pending queue */
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /* VVVVV 07/21/98 */
103472d39feaSAlan Cox 	if (host->semaph == 1) {
103572d39feaSAlan Cox 		/* Disable Jasmin SCSI Int */
103672d39feaSAlan Cox 		outb(0x1F, host->addr + TUL_Mask);
103772d39feaSAlan Cox 		host->semaph = 0;
103872d39feaSAlan Cox 		spin_unlock_irqrestore(&host->semaph_lock, flags);
10391da177e4SLinus Torvalds 
104072d39feaSAlan Cox 		tulip_main(host);
10411da177e4SLinus Torvalds 
104272d39feaSAlan Cox 		spin_lock_irqsave(&host->semaph_lock, flags);
104372d39feaSAlan Cox 		host->semaph = 1;
104472d39feaSAlan Cox 		outb(0x0F, host->addr + TUL_Mask);
10451da177e4SLinus Torvalds 	}
104672d39feaSAlan Cox 	spin_unlock_irqrestore(&host->semaph_lock, flags);
10471da177e4SLinus Torvalds 	return;
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds /***************************************************************************/
initio_isr(struct initio_host * host)105172d39feaSAlan Cox static int initio_isr(struct initio_host * host)
10521da177e4SLinus Torvalds {
105372d39feaSAlan Cox 	if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
105472d39feaSAlan Cox 		if (host->semaph == 1) {
105572d39feaSAlan Cox 			outb(0x1F, host->addr + TUL_Mask);
10561da177e4SLinus Torvalds 			/* Disable Tulip SCSI Int */
105772d39feaSAlan Cox 			host->semaph = 0;
10581da177e4SLinus Torvalds 
105972d39feaSAlan Cox 			tulip_main(host);
10601da177e4SLinus Torvalds 
106172d39feaSAlan Cox 			host->semaph = 1;
106272d39feaSAlan Cox 			outb(0x0F, host->addr + TUL_Mask);
106372d39feaSAlan Cox 			return 1;
10641da177e4SLinus Torvalds 		}
10651da177e4SLinus Torvalds 	}
106672d39feaSAlan Cox 	return 0;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
tulip_main(struct initio_host * host)106972d39feaSAlan Cox static int tulip_main(struct initio_host * host)
10701da177e4SLinus Torvalds {
107172d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	for (;;) {
107472d39feaSAlan Cox 		tulip_scsi(host);	/* Call tulip_scsi              */
10751da177e4SLinus Torvalds 
107672d39feaSAlan Cox 		/* Walk the list of completed SCBs */
107772d39feaSAlan Cox 		while ((scb = initio_find_done_scb(host)) != NULL) {	/* find done entry */
107872d39feaSAlan Cox 			if (scb->tastat == INI_QUEUE_FULL) {
107972d39feaSAlan Cox 				host->max_tags[scb->target] =
108072d39feaSAlan Cox 				    host->act_tags[scb->target] - 1;
108172d39feaSAlan Cox 				scb->tastat = 0;
108272d39feaSAlan Cox 				initio_append_pend_scb(host, scb);
10831da177e4SLinus Torvalds 				continue;
10841da177e4SLinus Torvalds 			}
108572d39feaSAlan Cox 			if (!(scb->mode & SCM_RSENS)) {		/* not in auto req. sense mode */
108672d39feaSAlan Cox 				if (scb->tastat == 2) {
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 					/* clr sync. nego flag */
10891da177e4SLinus Torvalds 
109072d39feaSAlan Cox 					if (scb->flags & SCF_SENSE) {
109172d39feaSAlan Cox 						u8 len;
109272d39feaSAlan Cox 						len = scb->senselen;
10931da177e4SLinus Torvalds 						if (len == 0)
10941da177e4SLinus Torvalds 							len = 1;
109572d39feaSAlan Cox 						scb->buflen = scb->senselen;
109672d39feaSAlan Cox 						scb->bufptr = scb->senseptr;
109772d39feaSAlan Cox 						scb->flags &= ~(SCF_SG | SCF_DIR);	/* for xfer_data_in */
109872d39feaSAlan Cox 						/* so, we won't report wrong direction in xfer_data_in,
10991da177e4SLinus Torvalds 						   and won't report HOST_DO_DU in state_6 */
110072d39feaSAlan Cox 						scb->mode = SCM_RSENS;
110172d39feaSAlan Cox 						scb->ident &= 0xBF;	/* Disable Disconnect */
110272d39feaSAlan Cox 						scb->tagmsg = 0;
110372d39feaSAlan Cox 						scb->tastat = 0;
110472d39feaSAlan Cox 						scb->cdblen = 6;
110572d39feaSAlan Cox 						scb->cdb[0] = SCSICMD_RequestSense;
110672d39feaSAlan Cox 						scb->cdb[1] = 0;
110772d39feaSAlan Cox 						scb->cdb[2] = 0;
110872d39feaSAlan Cox 						scb->cdb[3] = 0;
110972d39feaSAlan Cox 						scb->cdb[4] = len;
111072d39feaSAlan Cox 						scb->cdb[5] = 0;
111172d39feaSAlan Cox 						initio_push_pend_scb(host, scb);
11121da177e4SLinus Torvalds 						break;
11131da177e4SLinus Torvalds 					}
11141da177e4SLinus Torvalds 				}
11151da177e4SLinus Torvalds 			} else {	/* in request sense mode */
11161da177e4SLinus Torvalds 
111772d39feaSAlan Cox 				if (scb->tastat == 2) {		/* check contition status again after sending
11181da177e4SLinus Torvalds 									   requset sense cmd 0x3 */
111972d39feaSAlan Cox 					scb->hastat = HOST_BAD_PHAS;
11201da177e4SLinus Torvalds 				}
112172d39feaSAlan Cox 				scb->tastat = 2;
11221da177e4SLinus Torvalds 			}
112372d39feaSAlan Cox 			scb->flags |= SCF_DONE;
112472d39feaSAlan Cox 			if (scb->flags & SCF_POST) {
112572d39feaSAlan Cox 				/* FIXME: only one post method and lose casts */
112672d39feaSAlan Cox 				(*scb->post) ((u8 *) host, (u8 *) scb);
11271da177e4SLinus Torvalds 			}
11281da177e4SLinus Torvalds 		}		/* while */
11291da177e4SLinus Torvalds 		/* find_active: */
113072d39feaSAlan Cox 		if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
11311da177e4SLinus Torvalds 			continue;
113272d39feaSAlan Cox 		if (host->active)	/* return to OS and wait for xfer_done_ISR/Selected_ISR */
11331da177e4SLinus Torvalds 			return 1;	/* return to OS, enable interrupt */
11341da177e4SLinus Torvalds 		/* Check pending SCB            */
113572d39feaSAlan Cox 		if (initio_find_first_pend_scb(host) == NULL)
11361da177e4SLinus Torvalds 			return 1;	/* return to OS, enable interrupt */
11371da177e4SLinus Torvalds 	}			/* End of for loop */
11381da177e4SLinus Torvalds 	/* statement won't reach here */
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
tulip_scsi(struct initio_host * host)114172d39feaSAlan Cox static void tulip_scsi(struct initio_host * host)
11421da177e4SLinus Torvalds {
114372d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
114472d39feaSAlan Cox 	struct target_control *active_tc;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* make sure to service interrupt asap */
114772d39feaSAlan Cox 	if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
114872d39feaSAlan Cox 		host->phase = host->jsstatus0 & TSS_PH_MASK;
114972d39feaSAlan Cox 		host->jsstatus1 = inb(host->addr + TUL_SStatus1);
115072d39feaSAlan Cox 		host->jsint = inb(host->addr + TUL_SInt);
115172d39feaSAlan Cox 		if (host->jsint & TSS_SCSIRST_INT) {	/* SCSI bus reset detected      */
115272d39feaSAlan Cox 			int_initio_scsi_rst(host);
11531da177e4SLinus Torvalds 			return;
11541da177e4SLinus Torvalds 		}
115572d39feaSAlan Cox 		if (host->jsint & TSS_RESEL_INT) {	/* if selected/reselected interrupt */
115672d39feaSAlan Cox 			if (int_initio_resel(host) == 0)
115772d39feaSAlan Cox 				initio_next_state(host);
11581da177e4SLinus Torvalds 			return;
11591da177e4SLinus Torvalds 		}
116072d39feaSAlan Cox 		if (host->jsint & TSS_SEL_TIMEOUT) {
116172d39feaSAlan Cox 			int_initio_busfree(host);
11621da177e4SLinus Torvalds 			return;
11631da177e4SLinus Torvalds 		}
116472d39feaSAlan Cox 		if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
116572d39feaSAlan Cox 			int_initio_busfree(host);	/* unexpected bus free or sel timeout */
11661da177e4SLinus Torvalds 			return;
11671da177e4SLinus Torvalds 		}
116872d39feaSAlan Cox 		if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) {	/* func complete or Bus service */
11693fa5975bSColin Ian King 			if (host->active)
117072d39feaSAlan Cox 				initio_next_state(host);
11711da177e4SLinus Torvalds 			return;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 	}
117472d39feaSAlan Cox 	if (host->active != NULL)
11751da177e4SLinus Torvalds 		return;
11761da177e4SLinus Torvalds 
117772d39feaSAlan Cox 	if ((scb = initio_find_first_pend_scb(host)) == NULL)
11781da177e4SLinus Torvalds 		return;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	/* program HBA's SCSI ID & target SCSI ID */
118172d39feaSAlan Cox 	outb((host->scsi_id << 4) | (scb->target & 0x0F),
118272d39feaSAlan Cox 		host->addr + TUL_SScsiId);
118372d39feaSAlan Cox 	if (scb->opcode == ExecSCSI) {
118472d39feaSAlan Cox 		active_tc = &host->targets[scb->target];
11851da177e4SLinus Torvalds 
118672d39feaSAlan Cox 		if (scb->tagmsg)
118772d39feaSAlan Cox 			active_tc->drv_flags |= TCF_DRV_EN_TAG;
11881da177e4SLinus Torvalds 		else
118972d39feaSAlan Cox 			active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
11901da177e4SLinus Torvalds 
119172d39feaSAlan Cox 		outb(active_tc->js_period, host->addr + TUL_SPeriod);
119272d39feaSAlan Cox 		if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {	/* do wdtr negotiation          */
119372d39feaSAlan Cox 			initio_select_atn_stop(host, scb);
11941da177e4SLinus Torvalds 		} else {
119572d39feaSAlan Cox 			if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {	/* do sync negotiation          */
119672d39feaSAlan Cox 				initio_select_atn_stop(host, scb);
11971da177e4SLinus Torvalds 			} else {
119872d39feaSAlan Cox 				if (scb->tagmsg)
119972d39feaSAlan Cox 					initio_select_atn3(host, scb);
12001da177e4SLinus Torvalds 				else
120172d39feaSAlan Cox 					initio_select_atn(host, scb);
12021da177e4SLinus Torvalds 			}
12031da177e4SLinus Torvalds 		}
120472d39feaSAlan Cox 		if (scb->flags & SCF_POLL) {
120572d39feaSAlan Cox 			while (wait_tulip(host) != -1) {
120672d39feaSAlan Cox 				if (initio_next_state(host) == -1)
12071da177e4SLinus Torvalds 					break;
12081da177e4SLinus Torvalds 			}
12091da177e4SLinus Torvalds 		}
121072d39feaSAlan Cox 	} else if (scb->opcode == BusDevRst) {
121172d39feaSAlan Cox 		initio_select_atn_stop(host, scb);
121272d39feaSAlan Cox 		scb->next_state = 8;
121372d39feaSAlan Cox 		if (scb->flags & SCF_POLL) {
121472d39feaSAlan Cox 			while (wait_tulip(host) != -1) {
121572d39feaSAlan Cox 				if (initio_next_state(host) == -1)
12161da177e4SLinus Torvalds 					break;
12171da177e4SLinus Torvalds 			}
12181da177e4SLinus Torvalds 		}
121972d39feaSAlan Cox 	} else if (scb->opcode == AbortCmd) {
122072d39feaSAlan Cox 		if (initio_abort_srb(host, scb->srb) != 0) {
122172d39feaSAlan Cox 			initio_unlink_pend_scb(host, scb);
122272d39feaSAlan Cox 			initio_release_scb(host, scb);
12231da177e4SLinus Torvalds 		} else {
122472d39feaSAlan Cox 			scb->opcode = BusDevRst;
122572d39feaSAlan Cox 			initio_select_atn_stop(host, scb);
122672d39feaSAlan Cox 			scb->next_state = 8;
12271da177e4SLinus Torvalds 		}
12281da177e4SLinus Torvalds 	} else {
122972d39feaSAlan Cox 		initio_unlink_pend_scb(host, scb);
123072d39feaSAlan Cox 		scb->hastat = 0x16;	/* bad command */
123172d39feaSAlan Cox 		initio_append_done_scb(host, scb);
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	return;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
123672d39feaSAlan Cox /**
123772d39feaSAlan Cox  *	initio_next_state		-	Next SCSI state
123872d39feaSAlan Cox  *	@host: InitIO host we are processing
123972d39feaSAlan Cox  *
124072d39feaSAlan Cox  *	Progress the active command block along the state machine
124172d39feaSAlan Cox  *	until we hit a state which we must wait for activity to occur.
124272d39feaSAlan Cox  *
124372d39feaSAlan Cox  *	Returns zero or a negative code.
124472d39feaSAlan Cox  */
12451da177e4SLinus Torvalds 
initio_next_state(struct initio_host * host)124672d39feaSAlan Cox static int initio_next_state(struct initio_host * host)
12471da177e4SLinus Torvalds {
12481da177e4SLinus Torvalds 	int next;
12491da177e4SLinus Torvalds 
125072d39feaSAlan Cox 	next = host->active->next_state;
12511da177e4SLinus Torvalds 	for (;;) {
12521da177e4SLinus Torvalds 		switch (next) {
12531da177e4SLinus Torvalds 		case 1:
125472d39feaSAlan Cox 			next = initio_state_1(host);
12551da177e4SLinus Torvalds 			break;
12561da177e4SLinus Torvalds 		case 2:
125772d39feaSAlan Cox 			next = initio_state_2(host);
12581da177e4SLinus Torvalds 			break;
12591da177e4SLinus Torvalds 		case 3:
126072d39feaSAlan Cox 			next = initio_state_3(host);
12611da177e4SLinus Torvalds 			break;
12621da177e4SLinus Torvalds 		case 4:
126372d39feaSAlan Cox 			next = initio_state_4(host);
12641da177e4SLinus Torvalds 			break;
12651da177e4SLinus Torvalds 		case 5:
126672d39feaSAlan Cox 			next = initio_state_5(host);
12671da177e4SLinus Torvalds 			break;
12681da177e4SLinus Torvalds 		case 6:
126972d39feaSAlan Cox 			next = initio_state_6(host);
12701da177e4SLinus Torvalds 			break;
12711da177e4SLinus Torvalds 		case 7:
127272d39feaSAlan Cox 			next = initio_state_7(host);
12731da177e4SLinus Torvalds 			break;
12741da177e4SLinus Torvalds 		case 8:
127572d39feaSAlan Cox 			return initio_bus_device_reset(host);
12761da177e4SLinus Torvalds 		default:
127772d39feaSAlan Cox 			return initio_bad_seq(host);
12781da177e4SLinus Torvalds 		}
12791da177e4SLinus Torvalds 		if (next <= 0)
12801da177e4SLinus Torvalds 			return next;
12811da177e4SLinus Torvalds 	}
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 
128572d39feaSAlan Cox /**
128672d39feaSAlan Cox  *	initio_state_1		-	SCSI state machine
128772d39feaSAlan Cox  *	@host: InitIO host we are controlling
128872d39feaSAlan Cox  *
128972d39feaSAlan Cox  *	Perform SCSI state processing for Select/Attention/Stop
129072d39feaSAlan Cox  */
129172d39feaSAlan Cox 
initio_state_1(struct initio_host * host)129272d39feaSAlan Cox static int initio_state_1(struct initio_host * host)
12931da177e4SLinus Torvalds {
129472d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
129572d39feaSAlan Cox 	struct target_control *active_tc = host->active_tc;
12961da177e4SLinus Torvalds #if DEBUG_STATE
12971da177e4SLinus Torvalds 	printk("-s1-");
12981da177e4SLinus Torvalds #endif
12991da177e4SLinus Torvalds 
130072d39feaSAlan Cox 	/* Move the SCB from pending to busy */
130172d39feaSAlan Cox 	initio_unlink_pend_scb(host, scb);
130272d39feaSAlan Cox 	initio_append_busy_scb(host, scb);
13031da177e4SLinus Torvalds 
130472d39feaSAlan Cox 	outb(active_tc->sconfig0, host->addr + TUL_SConfig );
13051da177e4SLinus Torvalds 	/* ATN on */
130672d39feaSAlan Cox 	if (host->phase == MSG_OUT) {
130772d39feaSAlan Cox 		outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
130872d39feaSAlan Cox 		outb(scb->ident, host->addr + TUL_SFifo);
13091da177e4SLinus Torvalds 
131072d39feaSAlan Cox 		if (scb->tagmsg) {
131172d39feaSAlan Cox 			outb(scb->tagmsg, host->addr + TUL_SFifo);
131272d39feaSAlan Cox 			outb(scb->tagid, host->addr + TUL_SFifo);
13131da177e4SLinus Torvalds 		}
131472d39feaSAlan Cox 		if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
131572d39feaSAlan Cox 			active_tc->flags |= TCF_WDTR_DONE;
13169df17f46SHannes Reinecke 			outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
131772d39feaSAlan Cox 			outb(2, host->addr + TUL_SFifo);	/* Extended msg length */
13189df17f46SHannes Reinecke 			outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* Sync request */
131972d39feaSAlan Cox 			outb(1, host->addr + TUL_SFifo);	/* Start from 16 bits */
132072d39feaSAlan Cox 		} else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
132172d39feaSAlan Cox 			active_tc->flags |= TCF_SYNC_DONE;
13229df17f46SHannes Reinecke 			outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
132372d39feaSAlan Cox 			outb(3, host->addr + TUL_SFifo);	/* extended msg length */
13249df17f46SHannes Reinecke 			outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* sync request */
132572d39feaSAlan Cox 			outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
132672d39feaSAlan Cox 			outb(MAX_OFFSET, host->addr + TUL_SFifo);	/* REQ/ACK offset */
13271da177e4SLinus Torvalds 		}
132872d39feaSAlan Cox 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
132972d39feaSAlan Cox 		if (wait_tulip(host) == -1)
133072d39feaSAlan Cox 			return -1;
13311da177e4SLinus Torvalds 	}
133272d39feaSAlan Cox 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
133372d39feaSAlan Cox 	outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
133472d39feaSAlan Cox 	/* Into before CDB xfer */
133572d39feaSAlan Cox 	return 3;
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 
133972d39feaSAlan Cox /**
134072d39feaSAlan Cox  *	initio_state_2		-	SCSI state machine
134172d39feaSAlan Cox  *	@host: InitIO host we are controlling
134272d39feaSAlan Cox  *
134372d39feaSAlan Cox  * state after selection with attention
134472d39feaSAlan Cox  * state after selection with attention3
134572d39feaSAlan Cox  */
134672d39feaSAlan Cox 
initio_state_2(struct initio_host * host)134772d39feaSAlan Cox static int initio_state_2(struct initio_host * host)
13481da177e4SLinus Torvalds {
134972d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
135072d39feaSAlan Cox 	struct target_control *active_tc = host->active_tc;
13511da177e4SLinus Torvalds #if DEBUG_STATE
13521da177e4SLinus Torvalds 	printk("-s2-");
13531da177e4SLinus Torvalds #endif
13541da177e4SLinus Torvalds 
135572d39feaSAlan Cox 	initio_unlink_pend_scb(host, scb);
135672d39feaSAlan Cox 	initio_append_busy_scb(host, scb);
13571da177e4SLinus Torvalds 
135872d39feaSAlan Cox 	outb(active_tc->sconfig0, host->addr + TUL_SConfig);
13591da177e4SLinus Torvalds 
136072d39feaSAlan Cox 	if (host->jsstatus1 & TSS_CMD_PH_CMP)
136172d39feaSAlan Cox 		return 4;
136272d39feaSAlan Cox 
136372d39feaSAlan Cox 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
136472d39feaSAlan Cox 	outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
136572d39feaSAlan Cox 	/* Into before CDB xfer */
136672d39feaSAlan Cox 	return 3;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
136972d39feaSAlan Cox /**
137072d39feaSAlan Cox  *	initio_state_3		-	SCSI state machine
137172d39feaSAlan Cox  *	@host: InitIO host we are controlling
137272d39feaSAlan Cox  *
137372d39feaSAlan Cox  * state before CDB xfer is done
137472d39feaSAlan Cox  */
137572d39feaSAlan Cox 
initio_state_3(struct initio_host * host)137672d39feaSAlan Cox static int initio_state_3(struct initio_host * host)
13771da177e4SLinus Torvalds {
137872d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
137972d39feaSAlan Cox 	struct target_control *active_tc = host->active_tc;
13801da177e4SLinus Torvalds 	int i;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds #if DEBUG_STATE
13831da177e4SLinus Torvalds 	printk("-s3-");
13841da177e4SLinus Torvalds #endif
13851da177e4SLinus Torvalds 	for (;;) {
138672d39feaSAlan Cox 		switch (host->phase) {
13871da177e4SLinus Torvalds 		case CMD_OUT:	/* Command out phase            */
138872d39feaSAlan Cox 			for (i = 0; i < (int) scb->cdblen; i++)
138972d39feaSAlan Cox 				outb(scb->cdb[i], host->addr + TUL_SFifo);
139072d39feaSAlan Cox 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
139172d39feaSAlan Cox 			if (wait_tulip(host) == -1)
139272d39feaSAlan Cox 				return -1;
139372d39feaSAlan Cox 			if (host->phase == CMD_OUT)
139472d39feaSAlan Cox 				return initio_bad_seq(host);
139572d39feaSAlan Cox 			return 4;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 		case MSG_IN:	/* Message in phase             */
139872d39feaSAlan Cox 			scb->next_state = 3;
139972d39feaSAlan Cox 			if (initio_msgin(host) == -1)
140072d39feaSAlan Cox 				return -1;
14011da177e4SLinus Torvalds 			break;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 		case STATUS_IN:	/* Status phase                 */
140472d39feaSAlan Cox 			if (initio_status_msg(host) == -1)
140572d39feaSAlan Cox 				return -1;
14061da177e4SLinus Torvalds 			break;
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 		case MSG_OUT:	/* Message out phase            */
140972d39feaSAlan Cox 			if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
14109df17f46SHannes Reinecke 				outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
141172d39feaSAlan Cox 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
141272d39feaSAlan Cox 				if (wait_tulip(host) == -1)
141372d39feaSAlan Cox 					return -1;
14141da177e4SLinus Torvalds 			} else {
141572d39feaSAlan Cox 				active_tc->flags |= TCF_SYNC_DONE;
14161da177e4SLinus Torvalds 
14179df17f46SHannes Reinecke 				outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
141872d39feaSAlan Cox 				outb(3, host->addr + TUL_SFifo);	/* ext. msg len */
14199df17f46SHannes Reinecke 				outb(EXTENDED_SDTR, host->addr + TUL_SFifo);	/* sync request */
142072d39feaSAlan Cox 				outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
142172d39feaSAlan Cox 				outb(MAX_OFFSET, host->addr + TUL_SFifo);	/* REQ/ACK offset */
142272d39feaSAlan Cox 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
142372d39feaSAlan Cox 				if (wait_tulip(host) == -1)
142472d39feaSAlan Cox 					return -1;
142572d39feaSAlan Cox 				outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
142672d39feaSAlan Cox 				outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 			}
14291da177e4SLinus Torvalds 			break;
14301da177e4SLinus Torvalds 		default:
143172d39feaSAlan Cox 			return initio_bad_seq(host);
14321da177e4SLinus Torvalds 		}
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
143672d39feaSAlan Cox /**
143772d39feaSAlan Cox  *	initio_state_4		-	SCSI state machine
143872d39feaSAlan Cox  *	@host: InitIO host we are controlling
143972d39feaSAlan Cox  *
144072d39feaSAlan Cox  *	SCSI state machine. State 4
144172d39feaSAlan Cox  */
14421da177e4SLinus Torvalds 
initio_state_4(struct initio_host * host)144372d39feaSAlan Cox static int initio_state_4(struct initio_host * host)
14441da177e4SLinus Torvalds {
144572d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds #if DEBUG_STATE
14481da177e4SLinus Torvalds 	printk("-s4-");
14491da177e4SLinus Torvalds #endif
145072d39feaSAlan Cox 	if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
145172d39feaSAlan Cox 		return 6;	/* Go to state 6 (After data) */
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds 	for (;;) {
145472d39feaSAlan Cox 		if (scb->buflen == 0)
145572d39feaSAlan Cox 			return 6;
14561da177e4SLinus Torvalds 
145772d39feaSAlan Cox 		switch (host->phase) {
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 		case STATUS_IN:	/* Status phase                 */
146072d39feaSAlan Cox 			if ((scb->flags & SCF_DIR) != 0)	/* if direction bit set then report data underrun */
146172d39feaSAlan Cox 				scb->hastat = HOST_DO_DU;
146272d39feaSAlan Cox 			if ((initio_status_msg(host)) == -1)
146372d39feaSAlan Cox 				return -1;
14641da177e4SLinus Torvalds 			break;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 		case MSG_IN:	/* Message in phase             */
146772d39feaSAlan Cox 			scb->next_state = 0x4;
146872d39feaSAlan Cox 			if (initio_msgin(host) == -1)
146972d39feaSAlan Cox 				return -1;
14701da177e4SLinus Torvalds 			break;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 		case MSG_OUT:	/* Message out phase            */
147372d39feaSAlan Cox 			if (host->jsstatus0 & TSS_PAR_ERROR) {
147472d39feaSAlan Cox 				scb->buflen = 0;
147572d39feaSAlan Cox 				scb->hastat = HOST_DO_DU;
147672d39feaSAlan Cox 				if (initio_msgout_ide(host) == -1)
147772d39feaSAlan Cox 					return -1;
147872d39feaSAlan Cox 				return 6;
14791da177e4SLinus Torvalds 			} else {
14809df17f46SHannes Reinecke 				outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
148172d39feaSAlan Cox 				outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
148272d39feaSAlan Cox 				if (wait_tulip(host) == -1)
148372d39feaSAlan Cox 					return -1;
14841da177e4SLinus Torvalds 			}
14851da177e4SLinus Torvalds 			break;
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 		case DATA_IN:	/* Data in phase                */
148872d39feaSAlan Cox 			return initio_xfer_data_in(host);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 		case DATA_OUT:	/* Data out phase               */
149172d39feaSAlan Cox 			return initio_xfer_data_out(host);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 		default:
149472d39feaSAlan Cox 			return initio_bad_seq(host);
14951da177e4SLinus Torvalds 		}
14961da177e4SLinus Torvalds 	}
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 
150072d39feaSAlan Cox /**
150172d39feaSAlan Cox  *	initio_state_5		-	SCSI state machine
150272d39feaSAlan Cox  *	@host: InitIO host we are controlling
150372d39feaSAlan Cox  *
150472d39feaSAlan Cox  *	State after dma xfer done or phase change before xfer done
150572d39feaSAlan Cox  */
150672d39feaSAlan Cox 
initio_state_5(struct initio_host * host)150772d39feaSAlan Cox static int initio_state_5(struct initio_host * host)
15081da177e4SLinus Torvalds {
150972d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
15101da177e4SLinus Torvalds 	long cnt, xcnt;		/* cannot use unsigned !! code: if (xcnt < 0) */
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds #if DEBUG_STATE
15131da177e4SLinus Torvalds 	printk("-s5-");
15141da177e4SLinus Torvalds #endif
15151da177e4SLinus Torvalds 	/*------ get remaining count -------*/
151672d39feaSAlan Cox 	cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
15171da177e4SLinus Torvalds 
151872d39feaSAlan Cox 	if (inb(host->addr + TUL_XCmd) & 0x20) {
15191da177e4SLinus Torvalds 		/* ----------------------- DATA_IN ----------------------------- */
15201da177e4SLinus Torvalds 		/* check scsi parity error */
152172d39feaSAlan Cox 		if (host->jsstatus0 & TSS_PAR_ERROR)
152272d39feaSAlan Cox 			scb->hastat = HOST_DO_DU;
152372d39feaSAlan Cox 		if (inb(host->addr + TUL_XStatus) & XPEND) {	/* DMA xfer pending, Send STOP  */
15241da177e4SLinus Torvalds 			/* tell Hardware  scsi xfer has been terminated */
152572d39feaSAlan Cox 			outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
15261da177e4SLinus Torvalds 			/* wait until DMA xfer not pending */
152772d39feaSAlan Cox 			while (inb(host->addr + TUL_XStatus) & XPEND)
152872d39feaSAlan Cox 				cpu_relax();
15291da177e4SLinus Torvalds 		}
15301da177e4SLinus Torvalds 	} else {
15311da177e4SLinus Torvalds 		/*-------- DATA OUT -----------*/
153272d39feaSAlan Cox 		if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
153372d39feaSAlan Cox 			if (host->active_tc->js_period & TSC_WIDE_SCSI)
153472d39feaSAlan Cox 				cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
15351da177e4SLinus Torvalds 			else
153672d39feaSAlan Cox 				cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
15371da177e4SLinus Torvalds 		}
153872d39feaSAlan Cox 		if (inb(host->addr + TUL_XStatus) & XPEND) {	/* if DMA xfer is pending, abort DMA xfer */
153972d39feaSAlan Cox 			outb(TAX_X_ABT, host->addr + TUL_XCmd);
15401da177e4SLinus Torvalds 			/* wait Abort DMA xfer done */
154172d39feaSAlan Cox 			while ((inb(host->addr + TUL_Int) & XABT) == 0)
154272d39feaSAlan Cox 				cpu_relax();
15431da177e4SLinus Torvalds 		}
154472d39feaSAlan Cox 		if ((cnt == 1) && (host->phase == DATA_OUT)) {
154572d39feaSAlan Cox 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
154672d39feaSAlan Cox 			if (wait_tulip(host) == -1)
154772d39feaSAlan Cox 				return -1;
15481da177e4SLinus Torvalds 			cnt = 0;
15491da177e4SLinus Torvalds 		} else {
155072d39feaSAlan Cox 			if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
155172d39feaSAlan Cox 				outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
15521da177e4SLinus Torvalds 		}
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds 	if (cnt == 0) {
155572d39feaSAlan Cox 		scb->buflen = 0;
155672d39feaSAlan Cox 		return 6;	/* After Data */
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 	/* Update active data pointer */
155972d39feaSAlan Cox 	xcnt = (long) scb->buflen - cnt;	/* xcnt== bytes already xferred */
156072d39feaSAlan Cox 	scb->buflen = (u32) cnt;		/* cnt == bytes left to be xferred */
156172d39feaSAlan Cox 	if (scb->flags & SCF_SG) {
156272d39feaSAlan Cox 		struct sg_entry *sgp;
156372d39feaSAlan Cox 		unsigned long i;
15641da177e4SLinus Torvalds 
156572d39feaSAlan Cox 		sgp = &scb->sglist[scb->sgidx];
156672d39feaSAlan Cox 		for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
156772d39feaSAlan Cox 			xcnt -= (long) sgp->len;
15681da177e4SLinus Torvalds 			if (xcnt < 0) {		/* this sgp xfer half done */
156972d39feaSAlan Cox 				xcnt += (long) sgp->len;	/* xcnt == bytes xferred in this sgp */
157072d39feaSAlan Cox 				sgp->data += (u32) xcnt;	/* new ptr to be xfer */
157172d39feaSAlan Cox 				sgp->len -= (u32) xcnt;	/* new len to be xfer */
157272d39feaSAlan Cox 				scb->bufptr += ((u32) (i - scb->sgidx) << 3);
15731da177e4SLinus Torvalds 				/* new SG table ptr */
157472d39feaSAlan Cox 				scb->sglen = (u8) (scb->sgmax - i);
15751da177e4SLinus Torvalds 				/* new SG table len */
157672d39feaSAlan Cox 				scb->sgidx = (u16) i;
15771da177e4SLinus Torvalds 				/* for next disc and come in this loop */
157872d39feaSAlan Cox 				return 4;	/* Go to state 4                */
15791da177e4SLinus Torvalds 			}
15801da177e4SLinus Torvalds 			/* else (xcnt >= 0 , i.e. this sgp already xferred */
15811da177e4SLinus Torvalds 		}		/* for */
158272d39feaSAlan Cox 		return 6;	/* Go to state 6                */
15831da177e4SLinus Torvalds 	} else {
158472d39feaSAlan Cox 		scb->bufptr += (u32) xcnt;
15851da177e4SLinus Torvalds 	}
158672d39feaSAlan Cox 	return 4;		/* Go to state 4                */
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
158972d39feaSAlan Cox /**
159072d39feaSAlan Cox  *	initio_state_6		-	SCSI state machine
159172d39feaSAlan Cox  *	@host: InitIO host we are controlling
159272d39feaSAlan Cox  *
159372d39feaSAlan Cox  *	State after Data phase
159472d39feaSAlan Cox  */
159572d39feaSAlan Cox 
initio_state_6(struct initio_host * host)159672d39feaSAlan Cox static int initio_state_6(struct initio_host * host)
15971da177e4SLinus Torvalds {
159872d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds #if DEBUG_STATE
16011da177e4SLinus Torvalds 	printk("-s6-");
16021da177e4SLinus Torvalds #endif
16031da177e4SLinus Torvalds 	for (;;) {
160472d39feaSAlan Cox 		switch (host->phase) {
16051da177e4SLinus Torvalds 		case STATUS_IN:	/* Status phase                 */
160672d39feaSAlan Cox 			if ((initio_status_msg(host)) == -1)
160772d39feaSAlan Cox 				return -1;
16081da177e4SLinus Torvalds 			break;
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 		case MSG_IN:	/* Message in phase             */
161172d39feaSAlan Cox 			scb->next_state = 6;
161272d39feaSAlan Cox 			if ((initio_msgin(host)) == -1)
161372d39feaSAlan Cox 				return -1;
16141da177e4SLinus Torvalds 			break;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 		case MSG_OUT:	/* Message out phase            */
16179df17f46SHannes Reinecke 			outb(NOP, host->addr + TUL_SFifo);		/* msg nop */
161872d39feaSAlan Cox 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
161972d39feaSAlan Cox 			if (wait_tulip(host) == -1)
162072d39feaSAlan Cox 				return -1;
16211da177e4SLinus Torvalds 			break;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 		case DATA_IN:	/* Data in phase                */
162472d39feaSAlan Cox 			return initio_xpad_in(host);
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 		case DATA_OUT:	/* Data out phase               */
162772d39feaSAlan Cox 			return initio_xpad_out(host);
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 		default:
163072d39feaSAlan Cox 			return initio_bad_seq(host);
16311da177e4SLinus Torvalds 		}
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
163572d39feaSAlan Cox /**
163672d39feaSAlan Cox  *	initio_state_7		-	SCSI state machine
163772d39feaSAlan Cox  *	@host: InitIO host we are controlling
163872d39feaSAlan Cox  *
163972d39feaSAlan Cox  */
164072d39feaSAlan Cox 
initio_state_7(struct initio_host * host)16410c3dbdebSChen Zhou static int initio_state_7(struct initio_host * host)
16421da177e4SLinus Torvalds {
16431da177e4SLinus Torvalds 	int cnt, i;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds #if DEBUG_STATE
16461da177e4SLinus Torvalds 	printk("-s7-");
16471da177e4SLinus Torvalds #endif
16481da177e4SLinus Torvalds 	/* flush SCSI FIFO */
164972d39feaSAlan Cox 	cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
16501da177e4SLinus Torvalds 	if (cnt) {
16511da177e4SLinus Torvalds 		for (i = 0; i < cnt; i++)
165272d39feaSAlan Cox 			inb(host->addr + TUL_SFifo);
16531da177e4SLinus Torvalds 	}
165472d39feaSAlan Cox 	switch (host->phase) {
16551da177e4SLinus Torvalds 	case DATA_IN:		/* Data in phase                */
16561da177e4SLinus Torvalds 	case DATA_OUT:		/* Data out phase               */
165772d39feaSAlan Cox 		return initio_bad_seq(host);
16581da177e4SLinus Torvalds 	default:
165972d39feaSAlan Cox 		return 6;	/* Go to state 6                */
16601da177e4SLinus Torvalds 	}
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
166372d39feaSAlan Cox /**
166472d39feaSAlan Cox  *	initio_xfer_data_in	-	Commence data input
166572d39feaSAlan Cox  *	@host: InitIO host in use
166672d39feaSAlan Cox  *
166772d39feaSAlan Cox  *	Commence a block of data transfer. The transfer itself will
166872d39feaSAlan Cox  *	be managed by the controller and we will get a completion (or
166972d39feaSAlan Cox  *	failure) interrupt.
167072d39feaSAlan Cox  */
initio_xfer_data_in(struct initio_host * host)167172d39feaSAlan Cox static int initio_xfer_data_in(struct initio_host * host)
16721da177e4SLinus Torvalds {
167372d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
16741da177e4SLinus Torvalds 
167572d39feaSAlan Cox 	if ((scb->flags & SCF_DIR) == SCF_DOUT)
167672d39feaSAlan Cox 		return 6;	/* wrong direction */
16771da177e4SLinus Torvalds 
167872d39feaSAlan Cox 	outl(scb->buflen, host->addr + TUL_SCnt0);
167972d39feaSAlan Cox 	outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd);	/* 7/25/95 */
16801da177e4SLinus Torvalds 
168172d39feaSAlan Cox 	if (scb->flags & SCF_SG) {	/* S/G xfer */
168272d39feaSAlan Cox 		outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
168372d39feaSAlan Cox 		outl(scb->bufptr, host->addr + TUL_XAddH);
168472d39feaSAlan Cox 		outb(TAX_SG_IN, host->addr + TUL_XCmd);
16851da177e4SLinus Torvalds 	} else {
168672d39feaSAlan Cox 		outl(scb->buflen, host->addr + TUL_XCntH);
168772d39feaSAlan Cox 		outl(scb->bufptr, host->addr + TUL_XAddH);
168872d39feaSAlan Cox 		outb(TAX_X_IN, host->addr + TUL_XCmd);
16891da177e4SLinus Torvalds 	}
169072d39feaSAlan Cox 	scb->next_state = 0x5;
169172d39feaSAlan Cox 	return 0;		/* return to OS, wait xfer done , let jas_isr come in */
16921da177e4SLinus Torvalds }
16931da177e4SLinus Torvalds 
169472d39feaSAlan Cox /**
169572d39feaSAlan Cox  *	initio_xfer_data_out	-	Commence data output
169672d39feaSAlan Cox  *	@host: InitIO host in use
169772d39feaSAlan Cox  *
169872d39feaSAlan Cox  *	Commence a block of data transfer. The transfer itself will
169972d39feaSAlan Cox  *	be managed by the controller and we will get a completion (or
170072d39feaSAlan Cox  *	failure) interrupt.
170172d39feaSAlan Cox  */
17021da177e4SLinus Torvalds 
initio_xfer_data_out(struct initio_host * host)170372d39feaSAlan Cox static int initio_xfer_data_out(struct initio_host * host)
17041da177e4SLinus Torvalds {
170572d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
17061da177e4SLinus Torvalds 
170772d39feaSAlan Cox 	if ((scb->flags & SCF_DIR) == SCF_DIN)
170872d39feaSAlan Cox 		return 6;	/* wrong direction */
17091da177e4SLinus Torvalds 
171072d39feaSAlan Cox 	outl(scb->buflen, host->addr + TUL_SCnt0);
171172d39feaSAlan Cox 	outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
171272d39feaSAlan Cox 
171372d39feaSAlan Cox 	if (scb->flags & SCF_SG) {	/* S/G xfer */
171472d39feaSAlan Cox 		outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
171572d39feaSAlan Cox 		outl(scb->bufptr, host->addr + TUL_XAddH);
171672d39feaSAlan Cox 		outb(TAX_SG_OUT, host->addr + TUL_XCmd);
17171da177e4SLinus Torvalds 	} else {
171872d39feaSAlan Cox 		outl(scb->buflen, host->addr + TUL_XCntH);
171972d39feaSAlan Cox 		outl(scb->bufptr, host->addr + TUL_XAddH);
172072d39feaSAlan Cox 		outb(TAX_X_OUT, host->addr + TUL_XCmd);
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 
172372d39feaSAlan Cox 	scb->next_state = 0x5;
172472d39feaSAlan Cox 	return 0;		/* return to OS, wait xfer done , let jas_isr come in */
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
initio_xpad_in(struct initio_host * host)172772d39feaSAlan Cox int initio_xpad_in(struct initio_host * host)
17281da177e4SLinus Torvalds {
172972d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
173072d39feaSAlan Cox 	struct target_control *active_tc = host->active_tc;
17311da177e4SLinus Torvalds 
173272d39feaSAlan Cox 	if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
173372d39feaSAlan Cox 		scb->hastat = HOST_DO_DU;	/* over run             */
17341da177e4SLinus Torvalds 	for (;;) {
173572d39feaSAlan Cox 		if (active_tc->js_period & TSC_WIDE_SCSI)
173672d39feaSAlan Cox 			outl(2, host->addr + TUL_SCnt0);
17371da177e4SLinus Torvalds 		else
173872d39feaSAlan Cox 			outl(1, host->addr + TUL_SCnt0);
17391da177e4SLinus Torvalds 
174072d39feaSAlan Cox 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
174172d39feaSAlan Cox 		if (wait_tulip(host) == -1)
174272d39feaSAlan Cox 			return -1;
174372d39feaSAlan Cox 		if (host->phase != DATA_IN) {
174472d39feaSAlan Cox 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
174572d39feaSAlan Cox 			return 6;
17461da177e4SLinus Torvalds 		}
174772d39feaSAlan Cox 		inb(host->addr + TUL_SFifo);
17481da177e4SLinus Torvalds 	}
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
initio_xpad_out(struct initio_host * host)175172d39feaSAlan Cox int initio_xpad_out(struct initio_host * host)
17521da177e4SLinus Torvalds {
175372d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
175472d39feaSAlan Cox 	struct target_control *active_tc = host->active_tc;
17551da177e4SLinus Torvalds 
175672d39feaSAlan Cox 	if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
175772d39feaSAlan Cox 		scb->hastat = HOST_DO_DU;	/* over run             */
17581da177e4SLinus Torvalds 	for (;;) {
175972d39feaSAlan Cox 		if (active_tc->js_period & TSC_WIDE_SCSI)
176072d39feaSAlan Cox 			outl(2, host->addr + TUL_SCnt0);
17611da177e4SLinus Torvalds 		else
176272d39feaSAlan Cox 			outl(1, host->addr + TUL_SCnt0);
17631da177e4SLinus Torvalds 
176472d39feaSAlan Cox 		outb(0, host->addr + TUL_SFifo);
176572d39feaSAlan Cox 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
176672d39feaSAlan Cox 		if ((wait_tulip(host)) == -1)
176772d39feaSAlan Cox 			return -1;
176872d39feaSAlan Cox 		if (host->phase != DATA_OUT) {	/* Disable wide CPU to allow read 16 bits */
176972d39feaSAlan Cox 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
177072d39feaSAlan Cox 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
177172d39feaSAlan Cox 			return 6;
17721da177e4SLinus Torvalds 		}
17731da177e4SLinus Torvalds 	}
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
initio_status_msg(struct initio_host * host)177672d39feaSAlan Cox int initio_status_msg(struct initio_host * host)
17771da177e4SLinus Torvalds {				/* status & MSG_IN */
177872d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
177972d39feaSAlan Cox 	u8 msg;
17801da177e4SLinus Torvalds 
178172d39feaSAlan Cox 	outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
178272d39feaSAlan Cox 	if (wait_tulip(host) == -1)
178372d39feaSAlan Cox 		return -1;
178472d39feaSAlan Cox 
17851da177e4SLinus Torvalds 	/* get status */
178672d39feaSAlan Cox 	scb->tastat = inb(host->addr + TUL_SFifo);
17871da177e4SLinus Torvalds 
178872d39feaSAlan Cox 	if (host->phase == MSG_OUT) {
178972d39feaSAlan Cox 		if (host->jsstatus0 & TSS_PAR_ERROR)
17909df17f46SHannes Reinecke 			outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
179172d39feaSAlan Cox 		else
17929df17f46SHannes Reinecke 			outb(NOP, host->addr + TUL_SFifo);
179372d39feaSAlan Cox 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
179472d39feaSAlan Cox 		return wait_tulip(host);
17951da177e4SLinus Torvalds 	}
179672d39feaSAlan Cox 	if (host->phase == MSG_IN) {
179772d39feaSAlan Cox 		msg = inb(host->addr + TUL_SFifo);
179872d39feaSAlan Cox 		if (host->jsstatus0 & TSS_PAR_ERROR) {	/* Parity error                 */
179972d39feaSAlan Cox 			if ((initio_msgin_accept(host)) == -1)
180072d39feaSAlan Cox 				return -1;
180172d39feaSAlan Cox 			if (host->phase != MSG_OUT)
180272d39feaSAlan Cox 				return initio_bad_seq(host);
18039df17f46SHannes Reinecke 			outb(MSG_PARITY_ERROR, host->addr + TUL_SFifo);
180472d39feaSAlan Cox 			outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
180572d39feaSAlan Cox 			return wait_tulip(host);
18061da177e4SLinus Torvalds 		}
18071da177e4SLinus Torvalds 		if (msg == 0) {	/* Command complete             */
18081da177e4SLinus Torvalds 
180972d39feaSAlan Cox 			if ((scb->tastat & 0x18) == 0x10)	/* No link support              */
181072d39feaSAlan Cox 				return initio_bad_seq(host);
181172d39feaSAlan Cox 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
181272d39feaSAlan Cox 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
181372d39feaSAlan Cox 			return initio_wait_done_disc(host);
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 		}
18169df17f46SHannes Reinecke 		if (msg == LINKED_CMD_COMPLETE ||
18179df17f46SHannes Reinecke 		    msg == LINKED_FLG_CMD_COMPLETE) {
181872d39feaSAlan Cox 			if ((scb->tastat & 0x18) == 0x10)
181972d39feaSAlan Cox 				return initio_msgin_accept(host);
18201da177e4SLinus Torvalds 		}
18211da177e4SLinus Torvalds 	}
182272d39feaSAlan Cox 	return initio_bad_seq(host);
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds /* scsi bus free */
int_initio_busfree(struct initio_host * host)182772d39feaSAlan Cox int int_initio_busfree(struct initio_host * host)
18281da177e4SLinus Torvalds {
182972d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
18301da177e4SLinus Torvalds 
183172d39feaSAlan Cox 	if (scb != NULL) {
183272d39feaSAlan Cox 		if (scb->status & SCB_SELECT) {		/* selection timeout */
183372d39feaSAlan Cox 			initio_unlink_pend_scb(host, scb);
183472d39feaSAlan Cox 			scb->hastat = HOST_SEL_TOUT;
183572d39feaSAlan Cox 			initio_append_done_scb(host, scb);
18361da177e4SLinus Torvalds 		} else {	/* Unexpected bus free          */
183772d39feaSAlan Cox 			initio_unlink_busy_scb(host, scb);
183872d39feaSAlan Cox 			scb->hastat = HOST_BUS_FREE;
183972d39feaSAlan Cox 			initio_append_done_scb(host, scb);
18401da177e4SLinus Torvalds 		}
184172d39feaSAlan Cox 		host->active = NULL;
184272d39feaSAlan Cox 		host->active_tc = NULL;
18431da177e4SLinus Torvalds 	}
184472d39feaSAlan Cox 	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);		/* Flush SCSI FIFO  */
184572d39feaSAlan Cox 	outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
184672d39feaSAlan Cox 	outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect       */
184772d39feaSAlan Cox 	return -1;
18481da177e4SLinus Torvalds }
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 
185172d39feaSAlan Cox /**
185272d39feaSAlan Cox  *	int_initio_scsi_rst	-	SCSI reset occurred
185372d39feaSAlan Cox  *	@host: Host seeing the reset
185472d39feaSAlan Cox  *
185572d39feaSAlan Cox  *	A SCSI bus reset has occurred. Clean up any pending transfer
185672d39feaSAlan Cox  *	the hardware is doing by DMA and then abort all active and
185772d39feaSAlan Cox  *	disconnected commands. The mid layer should sort the rest out
185872d39feaSAlan Cox  *	for us
185972d39feaSAlan Cox  */
186072d39feaSAlan Cox 
int_initio_scsi_rst(struct initio_host * host)186172d39feaSAlan Cox static int int_initio_scsi_rst(struct initio_host * host)
18621da177e4SLinus Torvalds {
186372d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
18641da177e4SLinus Torvalds 	int i;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	/* if DMA xfer is pending, abort DMA xfer */
186772d39feaSAlan Cox 	if (inb(host->addr + TUL_XStatus) & 0x01) {
186872d39feaSAlan Cox 		outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
18691da177e4SLinus Torvalds 		/* wait Abort DMA xfer done */
187072d39feaSAlan Cox 		while ((inb(host->addr + TUL_Int) & 0x04) == 0)
187172d39feaSAlan Cox 			cpu_relax();
187272d39feaSAlan Cox 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds 	/* Abort all active & disconnected scb */
187572d39feaSAlan Cox 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
187672d39feaSAlan Cox 		scb->hastat = HOST_BAD_PHAS;
187772d39feaSAlan Cox 		initio_append_done_scb(host, scb);
18781da177e4SLinus Torvalds 	}
187972d39feaSAlan Cox 	host->active = NULL;
188072d39feaSAlan Cox 	host->active_tc = NULL;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	/* clr sync nego. done flag */
188372d39feaSAlan Cox 	for (i = 0; i < host->max_tar; i++)
188472d39feaSAlan Cox 		host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
188572d39feaSAlan Cox 	return -1;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
188872d39feaSAlan Cox /**
1889100ec495SLee Jones  *	int_initio_resel	-	Reselection occurred
189072d39feaSAlan Cox  *	@host: InitIO host adapter
189172d39feaSAlan Cox  *
189272d39feaSAlan Cox  *	A SCSI reselection event has been signalled and the interrupt
189372d39feaSAlan Cox  *	is now being processed. Work out which command block needs attention
189472d39feaSAlan Cox  *	and continue processing that command.
189572d39feaSAlan Cox  */
18961da177e4SLinus Torvalds 
int_initio_resel(struct initio_host * host)189772d39feaSAlan Cox int int_initio_resel(struct initio_host * host)
18981da177e4SLinus Torvalds {
189972d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
190072d39feaSAlan Cox 	struct target_control *active_tc;
190172d39feaSAlan Cox 	u8 tag, msg = 0;
190272d39feaSAlan Cox 	u8 tar, lun;
19031da177e4SLinus Torvalds 
190472d39feaSAlan Cox 	if ((scb = host->active) != NULL) {
190572d39feaSAlan Cox 		/* FIXME: Why check and not just clear ? */
190672d39feaSAlan Cox 		if (scb->status & SCB_SELECT)		/* if waiting for selection complete */
190772d39feaSAlan Cox 			scb->status &= ~SCB_SELECT;
190872d39feaSAlan Cox 		host->active = NULL;
19091da177e4SLinus Torvalds 	}
19101da177e4SLinus Torvalds 	/* --------- get target id---------------------- */
191172d39feaSAlan Cox 	tar = inb(host->addr + TUL_SBusId);
19121da177e4SLinus Torvalds 	/* ------ get LUN from Identify message----------- */
191372d39feaSAlan Cox 	lun = inb(host->addr + TUL_SIdent) & 0x0F;
19141da177e4SLinus Torvalds 	/* 07/22/98 from 0x1F -> 0x0F */
191572d39feaSAlan Cox 	active_tc = &host->targets[tar];
191672d39feaSAlan Cox 	host->active_tc = active_tc;
191772d39feaSAlan Cox 	outb(active_tc->sconfig0, host->addr + TUL_SConfig);
191872d39feaSAlan Cox 	outb(active_tc->js_period, host->addr + TUL_SPeriod);
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	/* ------------- tag queueing ? ------------------- */
192172d39feaSAlan Cox 	if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
192272d39feaSAlan Cox 		if ((initio_msgin_accept(host)) == -1)
192372d39feaSAlan Cox 			return -1;
192472d39feaSAlan Cox 		if (host->phase != MSG_IN)
19251da177e4SLinus Torvalds 			goto no_tag;
192672d39feaSAlan Cox 		outl(1, host->addr + TUL_SCnt0);
192772d39feaSAlan Cox 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
192872d39feaSAlan Cox 		if (wait_tulip(host) == -1)
192972d39feaSAlan Cox 			return -1;
193072d39feaSAlan Cox 		msg = inb(host->addr + TUL_SFifo);	/* Read Tag Message    */
19311da177e4SLinus Torvalds 
19329df17f46SHannes Reinecke 		if (msg < SIMPLE_QUEUE_TAG || msg > ORDERED_QUEUE_TAG)
19339df17f46SHannes Reinecke 			/* Is simple Tag      */
19341da177e4SLinus Torvalds 			goto no_tag;
19351da177e4SLinus Torvalds 
193672d39feaSAlan Cox 		if (initio_msgin_accept(host) == -1)
193772d39feaSAlan Cox 			return -1;
19381da177e4SLinus Torvalds 
193972d39feaSAlan Cox 		if (host->phase != MSG_IN)
19401da177e4SLinus Torvalds 			goto no_tag;
19411da177e4SLinus Torvalds 
194272d39feaSAlan Cox 		outl(1, host->addr + TUL_SCnt0);
194372d39feaSAlan Cox 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
194472d39feaSAlan Cox 		if (wait_tulip(host) == -1)
194572d39feaSAlan Cox 			return -1;
194672d39feaSAlan Cox 		tag = inb(host->addr + TUL_SFifo);	/* Read Tag ID       */
194772d39feaSAlan Cox 		scb = host->scb + tag;
194872d39feaSAlan Cox 		if (scb->target != tar || scb->lun != lun) {
194972d39feaSAlan Cox 			return initio_msgout_abort_tag(host);
19501da177e4SLinus Torvalds 		}
195172d39feaSAlan Cox 		if (scb->status != SCB_BUSY) {	/* 03/24/95             */
195272d39feaSAlan Cox 			return initio_msgout_abort_tag(host);
19531da177e4SLinus Torvalds 		}
195472d39feaSAlan Cox 		host->active = scb;
195572d39feaSAlan Cox 		if ((initio_msgin_accept(host)) == -1)
195672d39feaSAlan Cox 			return -1;
19571da177e4SLinus Torvalds 	} else {		/* No tag               */
19581da177e4SLinus Torvalds 	      no_tag:
195972d39feaSAlan Cox 		if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
196072d39feaSAlan Cox 			return initio_msgout_abort_targ(host);
19611da177e4SLinus Torvalds 		}
196272d39feaSAlan Cox 		host->active = scb;
196372d39feaSAlan Cox 		if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
196472d39feaSAlan Cox 			if ((initio_msgin_accept(host)) == -1)
196572d39feaSAlan Cox 				return -1;
19661da177e4SLinus Torvalds 		}
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds 	return 0;
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds 
197172d39feaSAlan Cox /**
197272d39feaSAlan Cox  *	int_initio_bad_seq		-	out of phase
197372d39feaSAlan Cox  *	@host: InitIO host flagging event
197472d39feaSAlan Cox  *
197572d39feaSAlan Cox  *	We have ended up out of phase somehow. Reset the host controller
197672d39feaSAlan Cox  *	and throw all our toys out of the pram. Let the midlayer clean up
197772d39feaSAlan Cox  */
19781da177e4SLinus Torvalds 
int_initio_bad_seq(struct initio_host * host)197972d39feaSAlan Cox static int int_initio_bad_seq(struct initio_host * host)
19801da177e4SLinus Torvalds {				/* target wrong phase           */
198172d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
19821da177e4SLinus Torvalds 	int i;
19831da177e4SLinus Torvalds 
198472d39feaSAlan Cox 	initio_reset_scsi(host, 10);
19851da177e4SLinus Torvalds 
198672d39feaSAlan Cox 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
198772d39feaSAlan Cox 		scb->hastat = HOST_BAD_PHAS;
198872d39feaSAlan Cox 		initio_append_done_scb(host, scb);
19891da177e4SLinus Torvalds 	}
199072d39feaSAlan Cox 	for (i = 0; i < host->max_tar; i++)
199172d39feaSAlan Cox 		host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
199272d39feaSAlan Cox 	return -1;
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds 
199672d39feaSAlan Cox /**
199772d39feaSAlan Cox  *	initio_msgout_abort_targ		-	abort a tag
199872d39feaSAlan Cox  *	@host: InitIO host
199972d39feaSAlan Cox  *
200072d39feaSAlan Cox  *	Abort when the target/lun does not match or when our SCB is not
200172d39feaSAlan Cox  *	busy. Used by untagged commands.
200272d39feaSAlan Cox  */
200372d39feaSAlan Cox 
initio_msgout_abort_targ(struct initio_host * host)200472d39feaSAlan Cox static int initio_msgout_abort_targ(struct initio_host * host)
20051da177e4SLinus Torvalds {
20061da177e4SLinus Torvalds 
200772d39feaSAlan Cox 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
200872d39feaSAlan Cox 	if (initio_msgin_accept(host) == -1)
200972d39feaSAlan Cox 		return -1;
201072d39feaSAlan Cox 	if (host->phase != MSG_OUT)
201172d39feaSAlan Cox 		return initio_bad_seq(host);
20121da177e4SLinus Torvalds 
20139df17f46SHannes Reinecke 	outb(ABORT_TASK_SET, host->addr + TUL_SFifo);
201472d39feaSAlan Cox 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
20151da177e4SLinus Torvalds 
201672d39feaSAlan Cox 	return initio_wait_disc(host);
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
201972d39feaSAlan Cox /**
202072d39feaSAlan Cox  *	initio_msgout_abort_tag		-	abort a tag
202172d39feaSAlan Cox  *	@host: InitIO host
202272d39feaSAlan Cox  *
202372d39feaSAlan Cox  *	Abort when the target/lun does not match or when our SCB is not
202472d39feaSAlan Cox  *	busy. Used for tagged commands.
202572d39feaSAlan Cox  */
202672d39feaSAlan Cox 
initio_msgout_abort_tag(struct initio_host * host)202772d39feaSAlan Cox static int initio_msgout_abort_tag(struct initio_host * host)
20281da177e4SLinus Torvalds {
20291da177e4SLinus Torvalds 
203072d39feaSAlan Cox 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
203172d39feaSAlan Cox 	if (initio_msgin_accept(host) == -1)
203272d39feaSAlan Cox 		return -1;
203372d39feaSAlan Cox 	if (host->phase != MSG_OUT)
203472d39feaSAlan Cox 		return initio_bad_seq(host);
20351da177e4SLinus Torvalds 
20369df17f46SHannes Reinecke 	outb(ABORT_TASK, host->addr + TUL_SFifo);
203772d39feaSAlan Cox 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
20381da177e4SLinus Torvalds 
203972d39feaSAlan Cox 	return initio_wait_disc(host);
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
204372d39feaSAlan Cox /**
204472d39feaSAlan Cox  *	initio_msgin		-	Message in
204572d39feaSAlan Cox  *	@host: InitIO Host
204672d39feaSAlan Cox  *
204772d39feaSAlan Cox  *	Process incoming message
204872d39feaSAlan Cox  */
initio_msgin(struct initio_host * host)204972d39feaSAlan Cox static int initio_msgin(struct initio_host * host)
20501da177e4SLinus Torvalds {
205172d39feaSAlan Cox 	struct target_control *active_tc;
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	for (;;) {
205472d39feaSAlan Cox 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
20551da177e4SLinus Torvalds 
205672d39feaSAlan Cox 		outl(1, host->addr + TUL_SCnt0);
205772d39feaSAlan Cox 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
205872d39feaSAlan Cox 		if (wait_tulip(host) == -1)
205972d39feaSAlan Cox 			return -1;
20601da177e4SLinus Torvalds 
206172d39feaSAlan Cox 		switch (inb(host->addr + TUL_SFifo)) {
20629df17f46SHannes Reinecke 		case DISCONNECT:	/* Disconnect msg */
206372d39feaSAlan Cox 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
206472d39feaSAlan Cox 			return initio_wait_disc(host);
20659df17f46SHannes Reinecke 		case SAVE_POINTERS:
20669df17f46SHannes Reinecke 		case RESTORE_POINTERS:
20679df17f46SHannes Reinecke 		case NOP:
206872d39feaSAlan Cox 			initio_msgin_accept(host);
20691da177e4SLinus Torvalds 			break;
20709df17f46SHannes Reinecke 		case MESSAGE_REJECT:	/* Clear ATN first              */
207172d39feaSAlan Cox 			outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
207272d39feaSAlan Cox 				host->addr + TUL_SSignal);
207372d39feaSAlan Cox 			active_tc = host->active_tc;
207472d39feaSAlan Cox 			if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)	/* do sync nego */
207572d39feaSAlan Cox 				outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
207672d39feaSAlan Cox 					host->addr + TUL_SSignal);
207772d39feaSAlan Cox 			initio_msgin_accept(host);
20781da177e4SLinus Torvalds 			break;
20799df17f46SHannes Reinecke 		case EXTENDED_MESSAGE:	/* extended msg */
208072d39feaSAlan Cox 			initio_msgin_extend(host);
20811da177e4SLinus Torvalds 			break;
20829df17f46SHannes Reinecke 		case IGNORE_WIDE_RESIDUE:
208372d39feaSAlan Cox 			initio_msgin_accept(host);
20841da177e4SLinus Torvalds 			break;
20859df17f46SHannes Reinecke 		case COMMAND_COMPLETE:
208672d39feaSAlan Cox 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
208772d39feaSAlan Cox 			outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
208872d39feaSAlan Cox 			return initio_wait_done_disc(host);
20891da177e4SLinus Torvalds 		default:
209072d39feaSAlan Cox 			initio_msgout_reject(host);
20911da177e4SLinus Torvalds 			break;
20921da177e4SLinus Torvalds 		}
209372d39feaSAlan Cox 		if (host->phase != MSG_IN)
209472d39feaSAlan Cox 			return host->phase;
20951da177e4SLinus Torvalds 	}
20961da177e4SLinus Torvalds 	/* statement won't reach here */
20971da177e4SLinus Torvalds }
20981da177e4SLinus Torvalds 
initio_msgout_reject(struct initio_host * host)209972d39feaSAlan Cox static int initio_msgout_reject(struct initio_host * host)
21001da177e4SLinus Torvalds {
210172d39feaSAlan Cox 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
21021da177e4SLinus Torvalds 
210372d39feaSAlan Cox 	if (initio_msgin_accept(host) == -1)
210472d39feaSAlan Cox 		return -1;
21051da177e4SLinus Torvalds 
210672d39feaSAlan Cox 	if (host->phase == MSG_OUT) {
21079df17f46SHannes Reinecke 		outb(MESSAGE_REJECT, host->addr + TUL_SFifo);		/* Msg reject           */
210872d39feaSAlan Cox 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
210972d39feaSAlan Cox 		return wait_tulip(host);
21101da177e4SLinus Torvalds 	}
211172d39feaSAlan Cox 	return host->phase;
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
initio_msgout_ide(struct initio_host * host)211472d39feaSAlan Cox static int initio_msgout_ide(struct initio_host * host)
21151da177e4SLinus Torvalds {
21169df17f46SHannes Reinecke 	outb(INITIATOR_ERROR, host->addr + TUL_SFifo);		/* Initiator Detected Error */
211772d39feaSAlan Cox 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
211872d39feaSAlan Cox 	return wait_tulip(host);
21191da177e4SLinus Torvalds }
21201da177e4SLinus Torvalds 
initio_msgin_extend(struct initio_host * host)212172d39feaSAlan Cox static int initio_msgin_extend(struct initio_host * host)
21221da177e4SLinus Torvalds {
212372d39feaSAlan Cox 	u8 len, idx;
21241da177e4SLinus Torvalds 
212572d39feaSAlan Cox 	if (initio_msgin_accept(host) != MSG_IN)
212672d39feaSAlan Cox 		return host->phase;
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	/* Get extended msg length      */
212972d39feaSAlan Cox 	outl(1, host->addr + TUL_SCnt0);
213072d39feaSAlan Cox 	outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
213172d39feaSAlan Cox 	if (wait_tulip(host) == -1)
213272d39feaSAlan Cox 		return -1;
21331da177e4SLinus Torvalds 
213472d39feaSAlan Cox 	len = inb(host->addr + TUL_SFifo);
213572d39feaSAlan Cox 	host->msg[0] = len;
21361da177e4SLinus Torvalds 	for (idx = 1; len != 0; len--) {
21371da177e4SLinus Torvalds 
213872d39feaSAlan Cox 		if ((initio_msgin_accept(host)) != MSG_IN)
213972d39feaSAlan Cox 			return host->phase;
214072d39feaSAlan Cox 		outl(1, host->addr + TUL_SCnt0);
214172d39feaSAlan Cox 		outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
214272d39feaSAlan Cox 		if (wait_tulip(host) == -1)
214372d39feaSAlan Cox 			return -1;
214472d39feaSAlan Cox 		host->msg[idx++] = inb(host->addr + TUL_SFifo);
21451da177e4SLinus Torvalds 	}
214672d39feaSAlan Cox 	if (host->msg[1] == 1) {		/* if it's synchronous data transfer request */
214772d39feaSAlan Cox 		u8 r;
214872d39feaSAlan Cox 		if (host->msg[0] != 3)	/* if length is not right */
214972d39feaSAlan Cox 			return initio_msgout_reject(host);
215072d39feaSAlan Cox 		if (host->active_tc->flags & TCF_NO_SYNC_NEGO) {	/* Set OFFSET=0 to do async, nego back */
215172d39feaSAlan Cox 			host->msg[3] = 0;
21521da177e4SLinus Torvalds 		} else {
215372d39feaSAlan Cox 			if (initio_msgin_sync(host) == 0 &&
215472d39feaSAlan Cox 			    (host->active_tc->flags & TCF_SYNC_DONE)) {
215572d39feaSAlan Cox 				initio_sync_done(host);
215672d39feaSAlan Cox 				return initio_msgin_accept(host);
21571da177e4SLinus Torvalds 			}
21581da177e4SLinus Torvalds 		}
21591da177e4SLinus Torvalds 
216072d39feaSAlan Cox 		r = inb(host->addr + TUL_SSignal);
216172d39feaSAlan Cox 		outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
216272d39feaSAlan Cox 			host->addr + TUL_SSignal);
216372d39feaSAlan Cox 		if (initio_msgin_accept(host) != MSG_OUT)
216472d39feaSAlan Cox 			return host->phase;
21651da177e4SLinus Torvalds 		/* sync msg out */
216672d39feaSAlan Cox 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
21671da177e4SLinus Torvalds 
216872d39feaSAlan Cox 		initio_sync_done(host);
21691da177e4SLinus Torvalds 
21709df17f46SHannes Reinecke 		outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
217172d39feaSAlan Cox 		outb(3, host->addr + TUL_SFifo);
21729df17f46SHannes Reinecke 		outb(EXTENDED_SDTR, host->addr + TUL_SFifo);
217372d39feaSAlan Cox 		outb(host->msg[2], host->addr + TUL_SFifo);
217472d39feaSAlan Cox 		outb(host->msg[3], host->addr + TUL_SFifo);
217572d39feaSAlan Cox 		outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
217672d39feaSAlan Cox 		return wait_tulip(host);
21771da177e4SLinus Torvalds 	}
217872d39feaSAlan Cox 	if (host->msg[0] != 2 || host->msg[1] != 3)
217972d39feaSAlan Cox 		return initio_msgout_reject(host);
21801da177e4SLinus Torvalds 	/* if it's WIDE DATA XFER REQ   */
218172d39feaSAlan Cox 	if (host->active_tc->flags & TCF_NO_WDTR) {
218272d39feaSAlan Cox 		host->msg[2] = 0;
21831da177e4SLinus Torvalds 	} else {
218472d39feaSAlan Cox 		if (host->msg[2] > 2)	/* > 32 bits            */
218572d39feaSAlan Cox 			return initio_msgout_reject(host);
218672d39feaSAlan Cox 		if (host->msg[2] == 2) {		/* == 32                */
218772d39feaSAlan Cox 			host->msg[2] = 1;
21881da177e4SLinus Torvalds 		} else {
218972d39feaSAlan Cox 			if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
219072d39feaSAlan Cox 				wdtr_done(host);
219172d39feaSAlan Cox 				if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
219272d39feaSAlan Cox 					outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
219372d39feaSAlan Cox 				return initio_msgin_accept(host);
21941da177e4SLinus Torvalds 			}
21951da177e4SLinus Torvalds 		}
21961da177e4SLinus Torvalds 	}
219772d39feaSAlan Cox 	outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
21981da177e4SLinus Torvalds 
219972d39feaSAlan Cox 	if (initio_msgin_accept(host) != MSG_OUT)
220072d39feaSAlan Cox 		return host->phase;
22011da177e4SLinus Torvalds 	/* WDTR msg out                 */
22029df17f46SHannes Reinecke 	outb(EXTENDED_MESSAGE, host->addr + TUL_SFifo);
220372d39feaSAlan Cox 	outb(2, host->addr + TUL_SFifo);
22049df17f46SHannes Reinecke 	outb(EXTENDED_WDTR, host->addr + TUL_SFifo);
220572d39feaSAlan Cox 	outb(host->msg[2], host->addr + TUL_SFifo);
220672d39feaSAlan Cox 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
220772d39feaSAlan Cox 	return wait_tulip(host);
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
initio_msgin_sync(struct initio_host * host)221072d39feaSAlan Cox static int initio_msgin_sync(struct initio_host * host)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds 	char default_period;
22131da177e4SLinus Torvalds 
221472d39feaSAlan Cox 	default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
221572d39feaSAlan Cox 	if (host->msg[3] > MAX_OFFSET) {
221672d39feaSAlan Cox 		host->msg[3] = MAX_OFFSET;
221772d39feaSAlan Cox 		if (host->msg[2] < default_period) {
221872d39feaSAlan Cox 			host->msg[2] = default_period;
22191da177e4SLinus Torvalds 			return 1;
22201da177e4SLinus Torvalds 		}
222172d39feaSAlan Cox 		if (host->msg[2] >= 59)	/* Change to async              */
222272d39feaSAlan Cox 			host->msg[3] = 0;
22231da177e4SLinus Torvalds 		return 1;
22241da177e4SLinus Torvalds 	}
22251da177e4SLinus Torvalds 	/* offset requests asynchronous transfers ? */
222672d39feaSAlan Cox 	if (host->msg[3] == 0) {
22271da177e4SLinus Torvalds 		return 0;
22281da177e4SLinus Torvalds 	}
222972d39feaSAlan Cox 	if (host->msg[2] < default_period) {
223072d39feaSAlan Cox 		host->msg[2] = default_period;
22311da177e4SLinus Torvalds 		return 1;
22321da177e4SLinus Torvalds 	}
223372d39feaSAlan Cox 	if (host->msg[2] >= 59) {
223472d39feaSAlan Cox 		host->msg[3] = 0;
22351da177e4SLinus Torvalds 		return 1;
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 	return 0;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
wdtr_done(struct initio_host * host)224072d39feaSAlan Cox static int wdtr_done(struct initio_host * host)
22411da177e4SLinus Torvalds {
224272d39feaSAlan Cox 	host->active_tc->flags &= ~TCF_SYNC_DONE;
224372d39feaSAlan Cox 	host->active_tc->flags |= TCF_WDTR_DONE;
22441da177e4SLinus Torvalds 
224572d39feaSAlan Cox 	host->active_tc->js_period = 0;
224672d39feaSAlan Cox 	if (host->msg[2])	/* if 16 bit */
224772d39feaSAlan Cox 		host->active_tc->js_period |= TSC_WIDE_SCSI;
224872d39feaSAlan Cox 	host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
224972d39feaSAlan Cox 	outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
225072d39feaSAlan Cox 	outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	return 1;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
initio_sync_done(struct initio_host * host)225572d39feaSAlan Cox static int initio_sync_done(struct initio_host * host)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	int i;
22581da177e4SLinus Torvalds 
225972d39feaSAlan Cox 	host->active_tc->flags |= TCF_SYNC_DONE;
22601da177e4SLinus Torvalds 
226172d39feaSAlan Cox 	if (host->msg[3]) {
226272d39feaSAlan Cox 		host->active_tc->js_period |= host->msg[3];
22631da177e4SLinus Torvalds 		for (i = 0; i < 8; i++) {
226472d39feaSAlan Cox 			if (initio_rate_tbl[i] >= host->msg[2])	/* pick the big one */
22651da177e4SLinus Torvalds 				break;
22661da177e4SLinus Torvalds 		}
226772d39feaSAlan Cox 		host->active_tc->js_period |= (i << 4);
226872d39feaSAlan Cox 		host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
22691da177e4SLinus Torvalds 	}
227072d39feaSAlan Cox 	outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
227172d39feaSAlan Cox 	outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
22721da177e4SLinus Torvalds 
227372d39feaSAlan Cox 	return -1;
22741da177e4SLinus Torvalds }
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 
initio_post_scsi_rst(struct initio_host * host)227772d39feaSAlan Cox static int initio_post_scsi_rst(struct initio_host * host)
22781da177e4SLinus Torvalds {
227972d39feaSAlan Cox 	struct scsi_ctrl_blk *scb;
228072d39feaSAlan Cox 	struct target_control *active_tc;
22811da177e4SLinus Torvalds 	int i;
22821da177e4SLinus Torvalds 
228372d39feaSAlan Cox 	host->active = NULL;
228472d39feaSAlan Cox 	host->active_tc = NULL;
228572d39feaSAlan Cox 	host->flags = 0;
22861da177e4SLinus Torvalds 
228772d39feaSAlan Cox 	while ((scb = initio_pop_busy_scb(host)) != NULL) {
228872d39feaSAlan Cox 		scb->hastat = HOST_BAD_PHAS;
228972d39feaSAlan Cox 		initio_append_done_scb(host, scb);
22901da177e4SLinus Torvalds 	}
22911da177e4SLinus Torvalds 	/* clear sync done flag         */
229272d39feaSAlan Cox 	active_tc = &host->targets[0];
229372d39feaSAlan Cox 	for (i = 0; i < host->max_tar; active_tc++, i++) {
229472d39feaSAlan Cox 		active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
22951da177e4SLinus Torvalds 		/* Initialize the sync. xfer register values to an asyn xfer */
229672d39feaSAlan Cox 		active_tc->js_period = 0;
229772d39feaSAlan Cox 		active_tc->sconfig0 = host->sconf1;
229872d39feaSAlan Cox 		host->act_tags[0] = 0;	/* 07/22/98 */
229972d39feaSAlan Cox 		host->targets[i].flags &= ~TCF_BUSY;	/* 07/22/98 */
23001da177e4SLinus Torvalds 	}			/* for */
23011da177e4SLinus Torvalds 
230272d39feaSAlan Cox 	return -1;
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
initio_select_atn_stop(struct initio_host * host,struct scsi_ctrl_blk * scb)230572d39feaSAlan Cox static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
23061da177e4SLinus Torvalds {
230772d39feaSAlan Cox 	scb->status |= SCB_SELECT;
230872d39feaSAlan Cox 	scb->next_state = 0x1;
230972d39feaSAlan Cox 	host->active = scb;
231072d39feaSAlan Cox 	host->active_tc = &host->targets[scb->target];
231172d39feaSAlan Cox 	outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 
initio_select_atn(struct initio_host * host,struct scsi_ctrl_blk * scb)231572d39feaSAlan Cox static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	int i;
23181da177e4SLinus Torvalds 
231972d39feaSAlan Cox 	scb->status |= SCB_SELECT;
232072d39feaSAlan Cox 	scb->next_state = 0x2;
23211da177e4SLinus Torvalds 
232272d39feaSAlan Cox 	outb(scb->ident, host->addr + TUL_SFifo);
232372d39feaSAlan Cox 	for (i = 0; i < (int) scb->cdblen; i++)
232472d39feaSAlan Cox 		outb(scb->cdb[i], host->addr + TUL_SFifo);
232572d39feaSAlan Cox 	host->active_tc = &host->targets[scb->target];
232672d39feaSAlan Cox 	host->active = scb;
232772d39feaSAlan Cox 	outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
initio_select_atn3(struct initio_host * host,struct scsi_ctrl_blk * scb)233072d39feaSAlan Cox static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
23311da177e4SLinus Torvalds {
23321da177e4SLinus Torvalds 	int i;
23331da177e4SLinus Torvalds 
233472d39feaSAlan Cox 	scb->status |= SCB_SELECT;
233572d39feaSAlan Cox 	scb->next_state = 0x2;
23361da177e4SLinus Torvalds 
233772d39feaSAlan Cox 	outb(scb->ident, host->addr + TUL_SFifo);
233872d39feaSAlan Cox 	outb(scb->tagmsg, host->addr + TUL_SFifo);
233972d39feaSAlan Cox 	outb(scb->tagid, host->addr + TUL_SFifo);
234072d39feaSAlan Cox 	for (i = 0; i < scb->cdblen; i++)
234172d39feaSAlan Cox 		outb(scb->cdb[i], host->addr + TUL_SFifo);
234272d39feaSAlan Cox 	host->active_tc = &host->targets[scb->target];
234372d39feaSAlan Cox 	host->active = scb;
234472d39feaSAlan Cox 	outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
23451da177e4SLinus Torvalds }
23461da177e4SLinus Torvalds 
234772d39feaSAlan Cox /**
234872d39feaSAlan Cox  *	initio_bus_device_reset	-	 SCSI Bus Device Reset
234972d39feaSAlan Cox  *	@host: InitIO host to reset
235072d39feaSAlan Cox  *
235172d39feaSAlan Cox  *	Perform a device reset and abort all pending SCBs for the
235272d39feaSAlan Cox  *	victim device
235372d39feaSAlan Cox  */
initio_bus_device_reset(struct initio_host * host)235472d39feaSAlan Cox int initio_bus_device_reset(struct initio_host * host)
23551da177e4SLinus Torvalds {
235672d39feaSAlan Cox 	struct scsi_ctrl_blk *scb = host->active;
235772d39feaSAlan Cox 	struct target_control *active_tc = host->active_tc;
235872d39feaSAlan Cox 	struct scsi_ctrl_blk *tmp, *prev;
235972d39feaSAlan Cox 	u8 tar;
23601da177e4SLinus Torvalds 
236172d39feaSAlan Cox 	if (host->phase != MSG_OUT)
236272d39feaSAlan Cox 		return int_initio_bad_seq(host);	/* Unexpected phase */
236372d39feaSAlan Cox 
236472d39feaSAlan Cox 	initio_unlink_pend_scb(host, scb);
236572d39feaSAlan Cox 	initio_release_scb(host, scb);
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 
236872d39feaSAlan Cox 	tar = scb->target;	/* target                       */
236972d39feaSAlan Cox 	active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
23701da177e4SLinus Torvalds 	/* clr sync. nego & WDTR flags  07/22/98 */
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	/* abort all SCB with same target */
237372d39feaSAlan Cox 	prev = tmp = host->first_busy;	/* Check Busy queue */
237472d39feaSAlan Cox 	while (tmp != NULL) {
237572d39feaSAlan Cox 		if (tmp->target == tar) {
23761da177e4SLinus Torvalds 			/* unlink it */
237772d39feaSAlan Cox 			if (tmp == host->first_busy) {
237872d39feaSAlan Cox 				if ((host->first_busy = tmp->next) == NULL)
237972d39feaSAlan Cox 					host->last_busy = NULL;
23801da177e4SLinus Torvalds 			} else {
238172d39feaSAlan Cox 				prev->next = tmp->next;
238272d39feaSAlan Cox 				if (tmp == host->last_busy)
238372d39feaSAlan Cox 					host->last_busy = prev;
23841da177e4SLinus Torvalds 			}
238572d39feaSAlan Cox 			tmp->hastat = HOST_ABORTED;
238672d39feaSAlan Cox 			initio_append_done_scb(host, tmp);
23871da177e4SLinus Torvalds 		}
23881da177e4SLinus Torvalds 		/* Previous haven't change      */
23891da177e4SLinus Torvalds 		else {
239072d39feaSAlan Cox 			prev = tmp;
23911da177e4SLinus Torvalds 		}
239272d39feaSAlan Cox 		tmp = tmp->next;
23931da177e4SLinus Torvalds 	}
23949df17f46SHannes Reinecke 	outb(TARGET_RESET, host->addr + TUL_SFifo);
239572d39feaSAlan Cox 	outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
239672d39feaSAlan Cox 	return initio_wait_disc(host);
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds }
23991da177e4SLinus Torvalds 
initio_msgin_accept(struct initio_host * host)240072d39feaSAlan Cox static int initio_msgin_accept(struct initio_host * host)
24011da177e4SLinus Torvalds {
240272d39feaSAlan Cox 	outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
240372d39feaSAlan Cox 	return wait_tulip(host);
24041da177e4SLinus Torvalds }
24051da177e4SLinus Torvalds 
wait_tulip(struct initio_host * host)240672d39feaSAlan Cox static int wait_tulip(struct initio_host * host)
24071da177e4SLinus Torvalds {
24081da177e4SLinus Torvalds 
240972d39feaSAlan Cox 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
241072d39feaSAlan Cox 		 & TSS_INT_PENDING))
241172d39feaSAlan Cox 			cpu_relax();
24121da177e4SLinus Torvalds 
241372d39feaSAlan Cox 	host->jsint = inb(host->addr + TUL_SInt);
241472d39feaSAlan Cox 	host->phase = host->jsstatus0 & TSS_PH_MASK;
241572d39feaSAlan Cox 	host->jsstatus1 = inb(host->addr + TUL_SStatus1);
24161da177e4SLinus Torvalds 
241772d39feaSAlan Cox 	if (host->jsint & TSS_RESEL_INT)	/* if SCSI bus reset detected */
241872d39feaSAlan Cox 		return int_initio_resel(host);
241972d39feaSAlan Cox 	if (host->jsint & TSS_SEL_TIMEOUT)	/* if selected/reselected timeout interrupt */
242072d39feaSAlan Cox 		return int_initio_busfree(host);
242172d39feaSAlan Cox 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected   */
242272d39feaSAlan Cox 		return int_initio_scsi_rst(host);
242372d39feaSAlan Cox 
242472d39feaSAlan Cox 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
242572d39feaSAlan Cox 		if (host->flags & HCF_EXPECT_DONE_DISC) {
242672d39feaSAlan Cox 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
242772d39feaSAlan Cox 			initio_unlink_busy_scb(host, host->active);
242872d39feaSAlan Cox 			host->active->hastat = 0;
242972d39feaSAlan Cox 			initio_append_done_scb(host, host->active);
243072d39feaSAlan Cox 			host->active = NULL;
243172d39feaSAlan Cox 			host->active_tc = NULL;
243272d39feaSAlan Cox 			host->flags &= ~HCF_EXPECT_DONE_DISC;
243372d39feaSAlan Cox 			outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
243472d39feaSAlan Cox 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
243572d39feaSAlan Cox 			return -1;
24361da177e4SLinus Torvalds 		}
243772d39feaSAlan Cox 		if (host->flags & HCF_EXPECT_DISC) {
243872d39feaSAlan Cox 			outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
243972d39feaSAlan Cox 			host->active = NULL;
244072d39feaSAlan Cox 			host->active_tc = NULL;
244172d39feaSAlan Cox 			host->flags &= ~HCF_EXPECT_DISC;
244272d39feaSAlan Cox 			outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
244372d39feaSAlan Cox 			outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
244472d39feaSAlan Cox 			return -1;
24451da177e4SLinus Torvalds 		}
244672d39feaSAlan Cox 		return int_initio_busfree(host);
24471da177e4SLinus Torvalds 	}
244872d39feaSAlan Cox 	/* The old code really does the below. Can probably be removed */
244972d39feaSAlan Cox 	if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
245072d39feaSAlan Cox 		return host->phase;
245172d39feaSAlan Cox 	return host->phase;
24521da177e4SLinus Torvalds }
245372d39feaSAlan Cox 
initio_wait_disc(struct initio_host * host)245472d39feaSAlan Cox static int initio_wait_disc(struct initio_host * host)
24551da177e4SLinus Torvalds {
245672d39feaSAlan Cox 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
245772d39feaSAlan Cox 		cpu_relax();
24581da177e4SLinus Torvalds 
245972d39feaSAlan Cox 	host->jsint = inb(host->addr + TUL_SInt);
24601da177e4SLinus Torvalds 
246172d39feaSAlan Cox 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected */
246272d39feaSAlan Cox 		return int_initio_scsi_rst(host);
246372d39feaSAlan Cox 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection */
246472d39feaSAlan Cox 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
246572d39feaSAlan Cox 		outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
246672d39feaSAlan Cox 		outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);	/* Enable HW reselect */
246772d39feaSAlan Cox 		host->active = NULL;
246872d39feaSAlan Cox 		return -1;
24691da177e4SLinus Torvalds 	}
247072d39feaSAlan Cox 	return initio_bad_seq(host);
24711da177e4SLinus Torvalds }
24721da177e4SLinus Torvalds 
initio_wait_done_disc(struct initio_host * host)247372d39feaSAlan Cox static int initio_wait_done_disc(struct initio_host * host)
24741da177e4SLinus Torvalds {
247572d39feaSAlan Cox 	while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
247672d39feaSAlan Cox 		 & TSS_INT_PENDING))
247772d39feaSAlan Cox 		 cpu_relax();
24781da177e4SLinus Torvalds 
247972d39feaSAlan Cox 	host->jsint = inb(host->addr + TUL_SInt);
24801da177e4SLinus Torvalds 
248172d39feaSAlan Cox 	if (host->jsint & TSS_SCSIRST_INT)	/* if SCSI bus reset detected */
248272d39feaSAlan Cox 		return int_initio_scsi_rst(host);
248372d39feaSAlan Cox 	if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection */
248472d39feaSAlan Cox 		outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);		/* Flush SCSI FIFO */
248572d39feaSAlan Cox 		outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
248672d39feaSAlan Cox 		outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);		/* Enable HW reselect */
248772d39feaSAlan Cox 		initio_unlink_busy_scb(host, host->active);
24881da177e4SLinus Torvalds 
248972d39feaSAlan Cox 		initio_append_done_scb(host, host->active);
249072d39feaSAlan Cox 		host->active = NULL;
249172d39feaSAlan Cox 		return -1;
24921da177e4SLinus Torvalds 	}
249372d39feaSAlan Cox 	return initio_bad_seq(host);
249472d39feaSAlan Cox }
24951da177e4SLinus Torvalds 
249672d39feaSAlan Cox /**
249772d39feaSAlan Cox  *	i91u_intr		-	IRQ handler
249872d39feaSAlan Cox  *	@irqno: IRQ number
249972d39feaSAlan Cox  *	@dev_id: IRQ identifier
250072d39feaSAlan Cox  *
250172d39feaSAlan Cox  *	Take the relevant locks and then invoke the actual isr processing
250272d39feaSAlan Cox  *	code under the lock.
250372d39feaSAlan Cox  */
25041da177e4SLinus Torvalds 
i91u_intr(int irqno,void * dev_id)25057d12e780SDavid Howells static irqreturn_t i91u_intr(int irqno, void *dev_id)
25061da177e4SLinus Torvalds {
25071da177e4SLinus Torvalds 	struct Scsi_Host *dev = dev_id;
25081da177e4SLinus Torvalds 	unsigned long flags;
250972d39feaSAlan Cox 	int r;
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	spin_lock_irqsave(dev->host_lock, flags);
251272d39feaSAlan Cox 	r = initio_isr((struct initio_host *)dev->hostdata);
25131da177e4SLinus Torvalds 	spin_unlock_irqrestore(dev->host_lock, flags);
251472d39feaSAlan Cox 	if (r)
25151da177e4SLinus Torvalds 		return IRQ_HANDLED;
251672d39feaSAlan Cox 	else
251772d39feaSAlan Cox 		return IRQ_NONE;
25181da177e4SLinus Torvalds }
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 
252172d39feaSAlan Cox /**
252272d39feaSAlan Cox  *	initio_build_scb		-	Build the mappings and SCB
252372d39feaSAlan Cox  *	@host: InitIO host taking the command
252472d39feaSAlan Cox  *	@cblk: Firmware command block
252572d39feaSAlan Cox  *	@cmnd: SCSI midlayer command block
252672d39feaSAlan Cox  *
252772d39feaSAlan Cox  *	Translate the abstract SCSI command into a firmware command block
252872d39feaSAlan Cox  *	suitable for feeding to the InitIO host controller. This also requires
252972d39feaSAlan Cox  *	we build the scatter gather lists and ensure they are mapped properly.
253072d39feaSAlan Cox  */
25311da177e4SLinus Torvalds 
initio_build_scb(struct initio_host * host,struct scsi_ctrl_blk * cblk,struct scsi_cmnd * cmnd)253272d39feaSAlan Cox static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
25331da177e4SLinus Torvalds {				/* Create corresponding SCB     */
253472d39feaSAlan Cox 	struct scatterlist *sglist;
253572d39feaSAlan Cox 	struct sg_entry *sg;		/* Pointer to SG list           */
2536a258c85dSFUJITA Tomonori 	int i, nseg;
253772d39feaSAlan Cox 	long total_len;
25381da177e4SLinus Torvalds 	dma_addr_t dma_addr;
25391da177e4SLinus Torvalds 
254072d39feaSAlan Cox 	/* Fill in the command headers */
254172d39feaSAlan Cox 	cblk->post = i91uSCBPost;	/* i91u's callback routine      */
254272d39feaSAlan Cox 	cblk->srb = cmnd;
254372d39feaSAlan Cox 	cblk->opcode = ExecSCSI;
254472d39feaSAlan Cox 	cblk->flags = SCF_POST;	/* After SCSI done, call post routine */
254572d39feaSAlan Cox 	cblk->target = cmnd->device->id;
254672d39feaSAlan Cox 	cblk->lun = cmnd->device->lun;
254772d39feaSAlan Cox 	cblk->ident = cmnd->device->lun | DISC_ALLOW;
25481da177e4SLinus Torvalds 
254972d39feaSAlan Cox 	cblk->flags |= SCF_SENSE;	/* Turn on auto request sense   */
255072d39feaSAlan Cox 
255172d39feaSAlan Cox 	/* Map the sense buffer into bus memory */
255272d39feaSAlan Cox 	dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
25531da177e4SLinus Torvalds 				  SENSE_SIZE, DMA_FROM_DEVICE);
2554423eef6fSGrant Grundler 	cblk->senseptr = (u32)dma_addr;
2555423eef6fSGrant Grundler 	cblk->senselen = SENSE_SIZE;
255609cc102bSBart Van Assche 	initio_priv(cmnd)->sense_dma_addr = dma_addr;
255772d39feaSAlan Cox 	cblk->cdblen = cmnd->cmd_len;
25581da177e4SLinus Torvalds 
255972d39feaSAlan Cox 	/* Clear the returned status */
256072d39feaSAlan Cox 	cblk->hastat = 0;
256172d39feaSAlan Cox 	cblk->tastat = 0;
256272d39feaSAlan Cox 	/* Command the command */
256364a87b24SBoaz Harrosh 	memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
25641da177e4SLinus Torvalds 
256572d39feaSAlan Cox 	/* Set up tags */
256672d39feaSAlan Cox 	if (cmnd->device->tagged_supported) {	/* Tag Support                  */
256772d39feaSAlan Cox 		cblk->tagmsg = SIMPLE_QUEUE_TAG;	/* Do simple tag only   */
25681da177e4SLinus Torvalds 	} else {
256972d39feaSAlan Cox 		cblk->tagmsg = 0;	/* No tag support               */
25701da177e4SLinus Torvalds 	}
257172d39feaSAlan Cox 
25721da177e4SLinus Torvalds 	/* todo handle map_sg error */
2573a258c85dSFUJITA Tomonori 	nseg = scsi_dma_map(cmnd);
2574a258c85dSFUJITA Tomonori 	BUG_ON(nseg < 0);
2575a258c85dSFUJITA Tomonori 	if (nseg) {
257672d39feaSAlan Cox 		dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
257772d39feaSAlan Cox 					  sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
25781da177e4SLinus Torvalds 					  DMA_BIDIRECTIONAL);
2579423eef6fSGrant Grundler 		cblk->bufptr = (u32)dma_addr;
258009cc102bSBart Van Assche 		initio_priv(cmnd)->sglist_dma_addr = dma_addr;
25811da177e4SLinus Torvalds 
2582e2d435eaSStuart Swales 		cblk->sglen = nseg;
25831da177e4SLinus Torvalds 
258472d39feaSAlan Cox 		cblk->flags |= SCF_SG;	/* Turn on SG list flag       */
2585a258c85dSFUJITA Tomonori 		total_len = 0;
2586a258c85dSFUJITA Tomonori 		sg = &cblk->sglist[0];
2587a258c85dSFUJITA Tomonori 		scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
258872d39feaSAlan Cox 			sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
2589423eef6fSGrant Grundler 			sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
2590423eef6fSGrant Grundler 			total_len += sg_dma_len(sglist);
2591a169e637SBoaz Harrosh 			++sg;
25921da177e4SLinus Torvalds 		}
25931da177e4SLinus Torvalds 
2594a258c85dSFUJITA Tomonori 		cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
2595a258c85dSFUJITA Tomonori 			total_len : scsi_bufflen(cmnd);
259672d39feaSAlan Cox 	} else {	/* No data transfer required */
259772d39feaSAlan Cox 		cblk->buflen = 0;
259872d39feaSAlan Cox 		cblk->sglen = 0;
25991da177e4SLinus Torvalds 	}
26001da177e4SLinus Torvalds }
26011da177e4SLinus Torvalds 
260272d39feaSAlan Cox /**
2603100ec495SLee Jones  *	i91u_queuecommand_lck	-	Queue a new command if possible
260472d39feaSAlan Cox  *	@cmd: SCSI command block from the mid layer
260572d39feaSAlan Cox  *
260672d39feaSAlan Cox  *	Attempts to queue a new command with the host adapter. Will return
260772d39feaSAlan Cox  *	zero if successful or indicate a host busy condition if not (which
260872d39feaSAlan Cox  *	will cause the mid layer to call us again later with the command)
260972d39feaSAlan Cox  */
i91u_queuecommand_lck(struct scsi_cmnd * cmd)2610af049dfdSBart Van Assche static int i91u_queuecommand_lck(struct scsi_cmnd *cmd)
26111da177e4SLinus Torvalds {
261272d39feaSAlan Cox 	struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
261372d39feaSAlan Cox 	struct scsi_ctrl_blk *cmnd;
26141da177e4SLinus Torvalds 
261572d39feaSAlan Cox 	cmnd = initio_alloc_scb(host);
261672d39feaSAlan Cox 	if (!cmnd)
26171da177e4SLinus Torvalds 		return SCSI_MLQUEUE_HOST_BUSY;
26181da177e4SLinus Torvalds 
261972d39feaSAlan Cox 	initio_build_scb(host, cmnd, cmd);
262072d39feaSAlan Cox 	initio_exec_scb(host, cmnd);
26211da177e4SLinus Torvalds 	return 0;
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
DEF_SCSI_QCMD(i91u_queuecommand)2624f281233dSJeff Garzik static DEF_SCSI_QCMD(i91u_queuecommand)
2625f281233dSJeff Garzik 
262672d39feaSAlan Cox /**
262772d39feaSAlan Cox  *	i91u_bus_reset		-	reset the SCSI bus
262872d39feaSAlan Cox  *	@cmnd: Command block we want to trigger the reset for
262972d39feaSAlan Cox  *
263072d39feaSAlan Cox  *	Initiate a SCSI bus reset sequence
26311da177e4SLinus Torvalds  */
263272d39feaSAlan Cox 
263372d39feaSAlan Cox static int i91u_bus_reset(struct scsi_cmnd * cmnd)
26341da177e4SLinus Torvalds {
263572d39feaSAlan Cox 	struct initio_host *host;
26361da177e4SLinus Torvalds 
263772d39feaSAlan Cox 	host = (struct initio_host *) cmnd->device->host->hostdata;
26381da177e4SLinus Torvalds 
263972d39feaSAlan Cox 	spin_lock_irq(cmnd->device->host->host_lock);
264072d39feaSAlan Cox 	initio_reset_scsi(host, 0);
264172d39feaSAlan Cox 	spin_unlock_irq(cmnd->device->host->host_lock);
264268b3aa7cSJeff Garzik  
26431da177e4SLinus Torvalds 	return SUCCESS;
26441da177e4SLinus Torvalds }
26451da177e4SLinus Torvalds 
264672d39feaSAlan Cox /**
2647100ec495SLee Jones  *	i91u_biosparam			-	return the "logical geometry
264872d39feaSAlan Cox  *	@sdev: SCSI device
2649100ec495SLee Jones  *	@dev: Matching block device
265072d39feaSAlan Cox  *	@capacity: Sector size of drive
265172d39feaSAlan Cox  *	@info_array: Return space for BIOS geometry
265272d39feaSAlan Cox  *
265372d39feaSAlan Cox  *	Map the device geometry in a manner compatible with the host
265472d39feaSAlan Cox  *	controller BIOS behaviour.
265572d39feaSAlan Cox  *
265672d39feaSAlan Cox  *	FIXME: limited to 2^32 sector devices.
26571da177e4SLinus Torvalds  */
265872d39feaSAlan Cox 
i91u_biosparam(struct scsi_device * sdev,struct block_device * dev,sector_t capacity,int * info_array)26591da177e4SLinus Torvalds static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
26601da177e4SLinus Torvalds 		sector_t capacity, int *info_array)
26611da177e4SLinus Torvalds {
266272d39feaSAlan Cox 	struct initio_host *host;		/* Point to Host adapter control block */
266372d39feaSAlan Cox 	struct target_control *tc;
26641da177e4SLinus Torvalds 
266572d39feaSAlan Cox 	host = (struct initio_host *) sdev->host->hostdata;
266672d39feaSAlan Cox 	tc = &host->targets[sdev->id];
26671da177e4SLinus Torvalds 
266872d39feaSAlan Cox 	if (tc->heads) {
266972d39feaSAlan Cox 		info_array[0] = tc->heads;
267072d39feaSAlan Cox 		info_array[1] = tc->sectors;
267172d39feaSAlan Cox 		info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
26721da177e4SLinus Torvalds 	} else {
267372d39feaSAlan Cox 		if (tc->drv_flags & TCF_DRV_255_63) {
26741da177e4SLinus Torvalds 			info_array[0] = 255;
26751da177e4SLinus Torvalds 			info_array[1] = 63;
26761da177e4SLinus Torvalds 			info_array[2] = (unsigned long)capacity / 255 / 63;
26771da177e4SLinus Torvalds 		} else {
26781da177e4SLinus Torvalds 			info_array[0] = 64;
26791da177e4SLinus Torvalds 			info_array[1] = 32;
26801da177e4SLinus Torvalds 			info_array[2] = (unsigned long)capacity >> 11;
26811da177e4SLinus Torvalds 		}
26821da177e4SLinus Torvalds 	}
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds #if defined(DEBUG_BIOSPARAM)
26851da177e4SLinus Torvalds 	if (i91u_debug & debug_biosparam) {
26861da177e4SLinus Torvalds 		printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
26871da177e4SLinus Torvalds 		       info_array[0], info_array[1], info_array[2]);
26881da177e4SLinus Torvalds 		printk("WARNING: check, if the bios geometry is correct.\n");
26891da177e4SLinus Torvalds 	}
26901da177e4SLinus Torvalds #endif
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	return 0;
26931da177e4SLinus Torvalds }
26941da177e4SLinus Torvalds 
269572d39feaSAlan Cox /**
269672d39feaSAlan Cox  *	i91u_unmap_scb		-	Unmap a command
269772d39feaSAlan Cox  *	@pci_dev: PCI device the command is for
269872d39feaSAlan Cox  *	@cmnd: The command itself
269972d39feaSAlan Cox  *
270072d39feaSAlan Cox  *	Unmap any PCI mapping/IOMMU resources allocated when the command
270172d39feaSAlan Cox  *	was mapped originally as part of initio_build_scb
270272d39feaSAlan Cox  */
270372d39feaSAlan Cox 
i91u_unmap_scb(struct pci_dev * pci_dev,struct scsi_cmnd * cmnd)270472d39feaSAlan Cox static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
27051da177e4SLinus Torvalds {
27061da177e4SLinus Torvalds 	/* auto sense buffer */
270709cc102bSBart Van Assche 	if (initio_priv(cmnd)->sense_dma_addr) {
27081da177e4SLinus Torvalds 		dma_unmap_single(&pci_dev->dev,
270909cc102bSBart Van Assche 				 initio_priv(cmnd)->sense_dma_addr,
27101da177e4SLinus Torvalds 				 SENSE_SIZE, DMA_FROM_DEVICE);
271109cc102bSBart Van Assche 		initio_priv(cmnd)->sense_dma_addr = 0;
27121da177e4SLinus Torvalds 	}
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	/* request buffer */
2715a258c85dSFUJITA Tomonori 	if (scsi_sg_count(cmnd)) {
271609cc102bSBart Van Assche 		dma_unmap_single(&pci_dev->dev,
271709cc102bSBart Van Assche 				 initio_priv(cmnd)->sglist_dma_addr,
271872d39feaSAlan Cox 				 sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
27191da177e4SLinus Torvalds 				 DMA_BIDIRECTIONAL);
27201da177e4SLinus Torvalds 
2721a258c85dSFUJITA Tomonori 		scsi_dma_unmap(cmnd);
27221da177e4SLinus Torvalds 	}
27231da177e4SLinus Torvalds }
27241da177e4SLinus Torvalds 
2725100ec495SLee Jones /*
272672d39feaSAlan Cox  *	i91uSCBPost		-	SCSI callback
272772d39feaSAlan Cox  *
272872d39feaSAlan Cox  *	This is callback routine be called when tulip finish one
272972d39feaSAlan Cox  *	SCSI command.
273072d39feaSAlan Cox  */
273172d39feaSAlan Cox 
i91uSCBPost(u8 * host_mem,u8 * cblk_mem)273272d39feaSAlan Cox static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
27331da177e4SLinus Torvalds {
273472d39feaSAlan Cox 	struct scsi_cmnd *cmnd;	/* Pointer to SCSI request block */
273572d39feaSAlan Cox 	struct initio_host *host;
273672d39feaSAlan Cox 	struct scsi_ctrl_blk *cblk;
27371da177e4SLinus Torvalds 
273872d39feaSAlan Cox 	host = (struct initio_host *) host_mem;
273972d39feaSAlan Cox 	cblk = (struct scsi_ctrl_blk *) cblk_mem;
274072d39feaSAlan Cox 	if ((cmnd = cblk->srb) == NULL) {
274172d39feaSAlan Cox 		printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
274272d39feaSAlan Cox 		WARN_ON(1);
274372d39feaSAlan Cox 		initio_release_scb(host, cblk);	/* Release SCB for current channel */
27441da177e4SLinus Torvalds 		return;
27451da177e4SLinus Torvalds 	}
274672d39feaSAlan Cox 
274772d39feaSAlan Cox 	/*
274872d39feaSAlan Cox 	 *	Remap the firmware error status into a mid layer one
274972d39feaSAlan Cox 	 */
275072d39feaSAlan Cox 	switch (cblk->hastat) {
27511da177e4SLinus Torvalds 	case 0x0:
27521da177e4SLinus Torvalds 	case 0xa:		/* Linked command complete without error and linked normally */
27531da177e4SLinus Torvalds 	case 0xb:		/* Linked command complete without error interrupt generated */
275472d39feaSAlan Cox 		cblk->hastat = 0;
27551da177e4SLinus Torvalds 		break;
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	case 0x11:		/* Selection time out-The initiator selection or target
27581da177e4SLinus Torvalds 				   reselection was not complete within the SCSI Time out period */
275972d39feaSAlan Cox 		cblk->hastat = DID_TIME_OUT;
27601da177e4SLinus Torvalds 		break;
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds 	case 0x14:		/* Target bus phase sequence failure-An invalid bus phase or bus
27631da177e4SLinus Torvalds 				   phase sequence was requested by the target. The host adapter
27641da177e4SLinus Torvalds 				   will generate a SCSI Reset Condition, notifying the host with
27651da177e4SLinus Torvalds 				   a SCRD interrupt */
276672d39feaSAlan Cox 		cblk->hastat = DID_RESET;
27671da177e4SLinus Torvalds 		break;
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds 	case 0x1a:		/* SCB Aborted. 07/21/98 */
277072d39feaSAlan Cox 		cblk->hastat = DID_ABORT;
27711da177e4SLinus Torvalds 		break;
27721da177e4SLinus Torvalds 
27731da177e4SLinus Torvalds 	case 0x12:		/* Data overrun/underrun-The target attempted to transfer more data
27741da177e4SLinus Torvalds 				   than was allocated by the Data Length field or the sum of the
27751da177e4SLinus Torvalds 				   Scatter / Gather Data Length fields. */
27761da177e4SLinus Torvalds 	case 0x13:		/* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
27771da177e4SLinus Torvalds 	case 0x16:		/* Invalid SCB Operation Code. */
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 	default:
278072d39feaSAlan Cox 		printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
278172d39feaSAlan Cox 		cblk->hastat = DID_ERROR;	/* Couldn't find any better */
27821da177e4SLinus Torvalds 		break;
27831da177e4SLinus Torvalds 	}
27841da177e4SLinus Torvalds 
278572d39feaSAlan Cox 	cmnd->result = cblk->tastat | (cblk->hastat << 16);
278672d39feaSAlan Cox 	i91u_unmap_scb(host->pci_dev, cmnd);
278725e1d896SBart Van Assche 	scsi_done(cmnd);	/* Notify system DONE           */
278872d39feaSAlan Cox 	initio_release_scb(host, cblk);	/* Release SCB for current channel */
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds 
2791*4ba116afSBart Van Assche static const struct scsi_host_template initio_template = {
27921da177e4SLinus Torvalds 	.proc_name		= "INI9100U",
279372d39feaSAlan Cox 	.name			= "Initio INI-9X00U/UW SCSI device driver",
27941da177e4SLinus Torvalds 	.queuecommand		= i91u_queuecommand,
27951da177e4SLinus Torvalds 	.eh_bus_reset_handler	= i91u_bus_reset,
27961da177e4SLinus Torvalds 	.bios_param		= i91u_biosparam,
279772d39feaSAlan Cox 	.can_queue		= MAX_TARGETS * i91u_MAXQUEUE,
27981da177e4SLinus Torvalds 	.this_id		= 1,
27991da177e4SLinus Torvalds 	.sg_tablesize		= SG_ALL,
280009cc102bSBart Van Assche 	.cmd_size		= sizeof(struct initio_cmd_priv),
28011da177e4SLinus Torvalds };
28021da177e4SLinus Torvalds 
initio_probe_one(struct pci_dev * pdev,const struct pci_device_id * id)280372d39feaSAlan Cox static int initio_probe_one(struct pci_dev *pdev,
280472d39feaSAlan Cox 	const struct pci_device_id *id)
280572d39feaSAlan Cox {
280672d39feaSAlan Cox 	struct Scsi_Host *shost;
280772d39feaSAlan Cox 	struct initio_host *host;
280872d39feaSAlan Cox 	u32 reg;
280972d39feaSAlan Cox 	u16 bios_seg;
281072d39feaSAlan Cox 	struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
281172d39feaSAlan Cox 	int num_scb, i, error;
281272d39feaSAlan Cox 
281372d39feaSAlan Cox 	error = pci_enable_device(pdev);
281472d39feaSAlan Cox 	if (error)
281572d39feaSAlan Cox 		return error;
281672d39feaSAlan Cox 
281772d39feaSAlan Cox 	pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
281872d39feaSAlan Cox 	bios_seg = (u16) (reg & 0xFF);
281972d39feaSAlan Cox 	if (((reg & 0xFF00) >> 8) == 0xFF)
282072d39feaSAlan Cox 		reg = 0;
282172d39feaSAlan Cox 	bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
282272d39feaSAlan Cox 
2823663b4117SChristoph Hellwig 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
282472d39feaSAlan Cox 		printk(KERN_WARNING  "i91u: Could not set 32 bit DMA mask\n");
282572d39feaSAlan Cox 		error = -ENODEV;
282672d39feaSAlan Cox 		goto out_disable_device;
282772d39feaSAlan Cox 	}
282872d39feaSAlan Cox 	shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
282972d39feaSAlan Cox 	if (!shost) {
283072d39feaSAlan Cox 		printk(KERN_WARNING "initio: Could not allocate host structure.\n");
283172d39feaSAlan Cox 		error = -ENOMEM;
283272d39feaSAlan Cox 		goto out_disable_device;
283372d39feaSAlan Cox 	}
283472d39feaSAlan Cox 	host = (struct initio_host *)shost->hostdata;
283572d39feaSAlan Cox 	memset(host, 0, sizeof(struct initio_host));
283699f1f534SAlan Cox 	host->addr = pci_resource_start(pdev, 0);
2837e2d435eaSStuart Swales 	host->bios_addr = bios_seg;
283872d39feaSAlan Cox 
283972d39feaSAlan Cox 	if (!request_region(host->addr, 256, "i91u")) {
284072d39feaSAlan Cox 		printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
284172d39feaSAlan Cox 		error = -ENODEV;
284272d39feaSAlan Cox 		goto out_host_put;
284372d39feaSAlan Cox 	}
284472d39feaSAlan Cox 
284572d39feaSAlan Cox 	if (initio_tag_enable)	/* 1.01i */
284672d39feaSAlan Cox 		num_scb = MAX_TARGETS * i91u_MAXQUEUE;
284772d39feaSAlan Cox 	else
284872d39feaSAlan Cox 		num_scb = MAX_TARGETS + 3;	/* 1-tape, 1-CD_ROM, 1- extra */
284972d39feaSAlan Cox 
285072d39feaSAlan Cox 	for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
285172d39feaSAlan Cox 		i = num_scb * sizeof(struct scsi_ctrl_blk);
2852c981e9e0SChristoph Hellwig 		scb = kzalloc(i, GFP_KERNEL);
2853c981e9e0SChristoph Hellwig 		if (scb)
285472d39feaSAlan Cox 			break;
285572d39feaSAlan Cox 	}
285672d39feaSAlan Cox 
285772d39feaSAlan Cox 	if (!scb) {
285872d39feaSAlan Cox 		printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
285972d39feaSAlan Cox 		error = -ENOMEM;
286072d39feaSAlan Cox 		goto out_release_region;
286172d39feaSAlan Cox 	}
286272d39feaSAlan Cox 
2863e9e42fafSAlan Cox 	host->pci_dev = pdev;
2864e9e42fafSAlan Cox 
2865e2d435eaSStuart Swales 	host->semaph = 1;
2866e2d435eaSStuart Swales 	spin_lock_init(&host->semaph_lock);
286772d39feaSAlan Cox 	host->num_scbs = num_scb;
286872d39feaSAlan Cox 	host->scb = scb;
286972d39feaSAlan Cox 	host->next_pending = scb;
287072d39feaSAlan Cox 	host->next_avail = scb;
287172d39feaSAlan Cox 	for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
287272d39feaSAlan Cox 		tmp->tagid = i;
287372d39feaSAlan Cox 		if (i != 0)
287472d39feaSAlan Cox 			prev->next = tmp;
287572d39feaSAlan Cox 		prev = tmp;
287672d39feaSAlan Cox 	}
287772d39feaSAlan Cox 	prev->next = NULL;
287872d39feaSAlan Cox 	host->scb_end = tmp;
287972d39feaSAlan Cox 	host->first_avail = scb;
288072d39feaSAlan Cox 	host->last_avail = prev;
2881e9e42fafSAlan Cox 	spin_lock_init(&host->avail_lock);
288272d39feaSAlan Cox 
2883e2d435eaSStuart Swales 	initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
288472d39feaSAlan Cox 
288572d39feaSAlan Cox 	host->jsstatus0 = 0;
288672d39feaSAlan Cox 
288772d39feaSAlan Cox 	shost->io_port = host->addr;
288872d39feaSAlan Cox 	shost->n_io_port = 0xff;
288972d39feaSAlan Cox 	shost->can_queue = num_scb;		/* 03/05/98                      */
289072d39feaSAlan Cox 	shost->unique_id = host->addr;
289172d39feaSAlan Cox 	shost->max_id = host->max_tar;
289272d39feaSAlan Cox 	shost->max_lun = 32;	/* 10/21/97                     */
289372d39feaSAlan Cox 	shost->irq = pdev->irq;
289472d39feaSAlan Cox 	shost->this_id = host->scsi_id;	/* Assign HCS index           */
289572d39feaSAlan Cox 	shost->base = host->addr;
289672d39feaSAlan Cox 	shost->sg_tablesize = TOTAL_SG_ENTRY;
289772d39feaSAlan Cox 
28984909cc2bSMichael Opdenacker 	error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
289972d39feaSAlan Cox 	if (error < 0) {
290072d39feaSAlan Cox 		printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
290172d39feaSAlan Cox 		goto out_free_scbs;
290272d39feaSAlan Cox 	}
290372d39feaSAlan Cox 
290472d39feaSAlan Cox 	pci_set_drvdata(pdev, shost);
290572d39feaSAlan Cox 
290672d39feaSAlan Cox 	error = scsi_add_host(shost, &pdev->dev);
290772d39feaSAlan Cox 	if (error)
290872d39feaSAlan Cox 		goto out_free_irq;
290972d39feaSAlan Cox 	scsi_scan_host(shost);
291072d39feaSAlan Cox 	return 0;
291172d39feaSAlan Cox out_free_irq:
291272d39feaSAlan Cox 	free_irq(pdev->irq, shost);
291372d39feaSAlan Cox out_free_scbs:
291472d39feaSAlan Cox 	kfree(host->scb);
291572d39feaSAlan Cox out_release_region:
291672d39feaSAlan Cox 	release_region(host->addr, 256);
291772d39feaSAlan Cox out_host_put:
291872d39feaSAlan Cox 	scsi_host_put(shost);
291972d39feaSAlan Cox out_disable_device:
292072d39feaSAlan Cox 	pci_disable_device(pdev);
292172d39feaSAlan Cox 	return error;
292272d39feaSAlan Cox }
292372d39feaSAlan Cox 
292472d39feaSAlan Cox /**
292572d39feaSAlan Cox  *	initio_remove_one	-	control shutdown
292672d39feaSAlan Cox  *	@pdev:	PCI device being released
292772d39feaSAlan Cox  *
292872d39feaSAlan Cox  *	Release the resources assigned to this adapter after it has
292972d39feaSAlan Cox  *	finished being used.
293072d39feaSAlan Cox  */
293172d39feaSAlan Cox 
initio_remove_one(struct pci_dev * pdev)293272d39feaSAlan Cox static void initio_remove_one(struct pci_dev *pdev)
293372d39feaSAlan Cox {
293472d39feaSAlan Cox 	struct Scsi_Host *host = pci_get_drvdata(pdev);
293572d39feaSAlan Cox 	struct initio_host *s = (struct initio_host *)host->hostdata;
293672d39feaSAlan Cox 	scsi_remove_host(host);
293772d39feaSAlan Cox 	free_irq(pdev->irq, host);
293872d39feaSAlan Cox 	release_region(s->addr, 256);
293972d39feaSAlan Cox 	scsi_host_put(host);
294072d39feaSAlan Cox 	pci_disable_device(pdev);
294172d39feaSAlan Cox }
294272d39feaSAlan Cox 
294372d39feaSAlan Cox MODULE_LICENSE("GPL");
294472d39feaSAlan Cox 
294572d39feaSAlan Cox static struct pci_device_id initio_pci_tbl[] = {
294672d39feaSAlan Cox 	{PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
294772d39feaSAlan Cox 	{PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
294872d39feaSAlan Cox 	{PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
294972d39feaSAlan Cox 	{PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
295072d39feaSAlan Cox 	{PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
295172d39feaSAlan Cox 	{0,}
295272d39feaSAlan Cox };
295372d39feaSAlan Cox MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
295472d39feaSAlan Cox 
295572d39feaSAlan Cox static struct pci_driver initio_pci_driver = {
295672d39feaSAlan Cox 	.name		= "initio",
295772d39feaSAlan Cox 	.id_table	= initio_pci_tbl,
295872d39feaSAlan Cox 	.probe		= initio_probe_one,
29596f039790SGreg Kroah-Hartman 	.remove		= initio_remove_one,
296072d39feaSAlan Cox };
2961ca57b069SLiu Shixin module_pci_driver(initio_pci_driver);
296272d39feaSAlan Cox 
296372d39feaSAlan Cox MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
296472d39feaSAlan Cox MODULE_AUTHOR("Initio Corporation");
296572d39feaSAlan Cox MODULE_LICENSE("GPL");
2966