initio.c 81.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/**************************************************************************
 * Initio 9100 device driver for Linux.
 *
 * Copyright (c) 1994-1998 Initio Corporation
 * Copyright (c) 1998 Bas Vermeulen <[email protected]>
6
 * Copyright (c) 2004 Christoph Hellwig <[email protected]>
7
 * Copyright (c) 2007 Red Hat
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *************************************************************************
 *
 * DESCRIPTION:
 *
 * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
 * adapters
 *
 * 08/06/97 hc	- v1.01h
 *		- Support inic-940 and inic-935
 * 09/26/97 hc	- v1.01i
 *		- Make correction from J.W. Schultz suggestion
 * 10/13/97 hc	- Support reset function
 * 10/21/97 hc	- v1.01j
 *		- Support 32 LUN (SCSI 3)
 * 01/14/98 hc	- v1.01k
 *		- Fix memory allocation problem
 * 03/04/98 hc	- v1.01l
 *		- Fix tape rewind which will hang the system problem
42
 *		- Set can_queue to initio_num_scb
Linus Torvalds's avatar
Linus Torvalds committed
43 44
 * 06/25/98 hc	- v1.01m
 *		- Get it work for kernel version >= 2.1.75
45
 *		- Dynamic assign SCSI bus reset holding time in initio_init()
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48
 * 07/02/98 hc	- v1.01n
 *		- Support 0002134A
 * 08/07/98 hc  - v1.01o
49
 *		- Change the initio_abort_srb routine to use scsi_done. <01>
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
 * 09/07/98 hl  - v1.02
 *              - Change the INI9100U define and proc_dir_entry to
 *                reflect the newer Kernel 2.1.118, but the v1.o1o
 *                should work with Kernel 2.1.118.
 * 09/20/98 wh  - v1.02a
 *              - Support Abort command.
 *              - Handle reset routine.
 * 09/21/98 hl  - v1.03
 *              - remove comments.
 * 12/09/98 bv	- v1.03a
 *		- Removed unused code
 * 12/13/98 bv	- v1.03b
 *		- Remove cli() locking for kernels >= 2.1.95. This uses
 *		  spinlocks to serialize access to the pSRB_head and
 *		  pSRB_tail members of the HCS structure.
 * 09/01/99 bv	- v1.03d
 *		- Fixed a deadlock problem in SMP.
 * 21/01/99 bv	- v1.03e
 *		- Add support for the Domex 3192U PCI SCSI
 *		  This is a slightly modified patch by
 *		  Brian Macy <[email protected]>
 * 22/02/99 bv	- v1.03f
 *		- Didn't detect the INIC-950 in 2.0.x correctly.
 *		  Now fixed.
 * 05/07/99 bv	- v1.03g
 *		- Changed the assumption that HZ = 100
 * 10/17/03 mc	- v1.04
 *		- added new DMA API support
 * 06/01/04 jmd	- v1.04a
 *		- Re-add reset_bus support
 **************************************************************************/

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/spinlock.h>
#include <linux/stat.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
97
#include <linux/dma-mapping.h>
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#include <asm/io.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_tcq.h>

#include "initio.h"

#define SENSE_SIZE		14

#define i91u_MAXQUEUE		2
#define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"

#define I950_DEVICE_ID	0x9500	/* Initio's inic-950 product ID   */
#define I940_DEVICE_ID	0x9400	/* Initio's inic-940 product ID   */
#define I935_DEVICE_ID	0x9401	/* Initio's inic-935 product ID   */
#define I920_DEVICE_ID	0x0002	/* Initio's other product ID      */

#ifdef DEBUG_i91u
static unsigned int i91u_debug = DEBUG_DEFAULT;
#endif

122
static int initio_tag_enable = 1;
Linus Torvalds's avatar
Linus Torvalds committed
123 124 125 126 127

#ifdef DEBUG_i91u
static int setup_debug = 0;
#endif

128
static void i91uSCBPost(u8 * pHcb, u8 * pScb);
Linus Torvalds's avatar
Linus Torvalds committed
129

130
/* PCI Devices supported by this driver */
131
static struct pci_device_id i91u_pci_devices[] = {
132 133 134 135 136 137
	{ PCI_VENDOR_ID_INIT,  I950_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ PCI_VENDOR_ID_INIT,  I940_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ PCI_VENDOR_ID_INIT,  I935_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ PCI_VENDOR_ID_INIT,  I920_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ PCI_VENDOR_ID_DOMEX, I920_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ }
Linus Torvalds's avatar
Linus Torvalds committed
138
};
139
MODULE_DEVICE_TABLE(pci, i91u_pci_devices);
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145

#define DEBUG_INTERRUPT 0
#define DEBUG_QUEUE     0
#define DEBUG_STATE     0
#define INT_DISC	0

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/*--- forward references ---*/
static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);

static int tulip_main(struct initio_host * host);

static int initio_next_state(struct initio_host * host);
static int initio_state_1(struct initio_host * host);
static int initio_state_2(struct initio_host * host);
static int initio_state_3(struct initio_host * host);
static int initio_state_4(struct initio_host * host);
static int initio_state_5(struct initio_host * host);
static int initio_state_6(struct initio_host * host);
static int initio_state_7(struct initio_host * host);
static int initio_xfer_data_in(struct initio_host * host);
static int initio_xfer_data_out(struct initio_host * host);
static int initio_xpad_in(struct initio_host * host);
static int initio_xpad_out(struct initio_host * host);
static int initio_status_msg(struct initio_host * host);

static int initio_msgin(struct initio_host * host);
static int initio_msgin_sync(struct initio_host * host);
static int initio_msgin_accept(struct initio_host * host);
static int initio_msgout_reject(struct initio_host * host);
static int initio_msgin_extend(struct initio_host * host);

static int initio_msgout_ide(struct initio_host * host);
static int initio_msgout_abort_targ(struct initio_host * host);
static int initio_msgout_abort_tag(struct initio_host * host);

static int initio_bus_device_reset(struct initio_host * host);
static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
static int int_initio_busfree(struct initio_host * host);
static int int_initio_scsi_rst(struct initio_host * host);
static int int_initio_bad_seq(struct initio_host * host);
static int int_initio_resel(struct initio_host * host);
static int initio_sync_done(struct initio_host * host);
static int wdtr_done(struct initio_host * host);
static int wait_tulip(struct initio_host * host);
static int initio_wait_done_disc(struct initio_host * host);
static int initio_wait_disc(struct initio_host * host);
static void tulip_scsi(struct initio_host * host);
static int initio_post_scsi_rst(struct initio_host * host);

static void initio_se2_ew_en(unsigned long base);
static void initio_se2_ew_ds(unsigned long base);
static int initio_se2_rd_all(unsigned long base);
static void initio_se2_update_all(unsigned long base);	/* setup default pattern */
static void initio_read_eeprom(unsigned long base);

/* ---- INTERNAL VARIABLES ---- */

Linus Torvalds's avatar
Linus Torvalds committed
200 201 202
static NVRAM i91unvram;
static NVRAM *i91unvramp;

203
static u8 i91udftNvRam[64] =
Linus Torvalds's avatar
Linus Torvalds committed
204
{
205
	/*----------- header -----------*/
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	0x25, 0xc9,		/* Signature    */
	0x40,			/* Size         */
	0x01,			/* Revision     */
	/* -- Host Adapter Structure -- */
	0x95,			/* ModelByte0   */
	0x00,			/* ModelByte1   */
	0x00,			/* ModelInfo    */
	0x01,			/* NumOfCh      */
	NBC1_DEFAULT,		/* BIOSConfig1  */
	0,			/* BIOSConfig2  */
	0,			/* HAConfig1    */
	0,			/* HAConfig2    */
	/* SCSI channel 0 and target Structure  */
	7,			/* SCSIid       */
	NCC1_DEFAULT,		/* SCSIconfig1  */
	0,			/* SCSIconfig2  */
	0x10,			/* NumSCSItarget */

	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,

	/* SCSI channel 1 and target Structure  */
	7,			/* SCSIid       */
	NCC1_DEFAULT,		/* SCSIconfig1  */
	0,			/* SCSIconfig2  */
	0x10,			/* NumSCSItarget */

	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0};			/*      - CheckSum -            */


243
static u8 initio_rate_tbl[8] =	/* fast 20      */
Linus Torvalds's avatar
Linus Torvalds committed
244
{
Lucas De Marchi's avatar
Lucas De Marchi committed
245
				/* nanosecond divide by 4 */
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249 250 251 252 253 254 255
	12,			/* 50ns,  20M   */
	18,			/* 75ns,  13.3M */
	25,			/* 100ns, 10M   */
	31,			/* 125ns, 8M    */
	37,			/* 150ns, 6.6M  */
	43,			/* 175ns, 5.7M  */
	50,			/* 200ns, 5M    */
	62			/* 250ns, 4M    */
};

256 257 258
static void initio_do_pause(unsigned amount)
{
	/* Pause for amount jiffies */
Linus Torvalds's avatar
Linus Torvalds committed
259 260
	unsigned long the_time = jiffies + amount;

261 262
	while (time_before_eq(jiffies, the_time))
		cpu_relax();
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}

/*-- forward reference --*/

/******************************************************************
 Input: instruction for  Serial E2PROM

 EX: se2_rd(0 call se2_instr() to send address and read command

	 StartBit  OP_Code   Address                Data
	 --------- --------  ------------------     -------
	 1         1 , 0     A5,A4,A3,A2,A1,A0      D15-D0

		 +-----------------------------------------------------
		 |
 CS -----+
			+--+  +--+  +--+  +--+  +--+
			^  |  ^  |  ^  |  ^  |  ^  |
			|  |  |  |  |  |  |  |  |  |
 CLK -------+  +--+  +--+  +--+  +--+  +--
 (leading edge trigger)

		 +--1-----1--+
		 | SB    OP  |  OP    A5    A4
 DI  ----+           +--0------------------
 (address and cmd sent to nvram)

	 -------------------------------------------+
												|
 DO                                             +---
 (data sent from nvram)


******************************************************************/
297 298 299 300 301 302 303 304 305 306

/**
 *	initio_se2_instr	-	bitbang an instruction
 *	@base: Base of InitIO controller
 *	@instr: Instruction for serial E2PROM
 *
 *	Bitbang an instruction out to the serial E2Prom
 */

static void initio_se2_instr(unsigned long base, u8 instr)
Linus Torvalds's avatar
Linus Torvalds committed
307 308
{
	int i;
309
	u8 b;
Linus Torvalds's avatar
Linus Torvalds committed
310

311 312 313 314
	outb(SE2CS | SE2DO, base + TUL_NVRAM);		/* cs+start bit */
	udelay(30);
	outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM);	/* +CLK */
	udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
315 316 317

	for (i = 0; i < 8; i++) {
		if (instr & 0x80)
318
			b = SE2CS | SE2DO;		/* -CLK+dataBit */
Linus Torvalds's avatar
Linus Torvalds committed
319
		else
320 321 322 323 324
			b = SE2CS;			/* -CLK */
		outb(b, base + TUL_NVRAM);
		udelay(30);
		outb(b | SE2CLK, base + TUL_NVRAM);	/* +CLK */
		udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
325 326
		instr <<= 1;
	}
327 328
	outb(SE2CS, base + TUL_NVRAM);			/* -CLK */
	udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331
}


332 333 334 335 336 337 338
/**
 *	initio_se2_ew_en	-	Enable erase/write
 *	@base: Base address of InitIO controller
 *
 *	Enable erase/write state of serial EEPROM
 */
void initio_se2_ew_en(unsigned long base)
Linus Torvalds's avatar
Linus Torvalds committed
339
{
340 341 342
	initio_se2_instr(base, 0x30);	/* EWEN */
	outb(0, base + TUL_NVRAM);	/* -CS  */
	udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
343 344 345
}


346 347 348 349 350 351 352
/**
 *	initio_se2_ew_ds	-	Disable erase/write
 *	@base: Base address of InitIO controller
 *
 *	Disable erase/write state of serial EEPROM
 */
void initio_se2_ew_ds(unsigned long base)
Linus Torvalds's avatar
Linus Torvalds committed
353
{
354 355 356
	initio_se2_instr(base, 0);	/* EWDS */
	outb(0, base + TUL_NVRAM);	/* -CS  */
	udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
357 358 359
}


360 361 362 363 364 365 366 367
/**
 *	initio_se2_rd		-	read E2PROM word
 *	@base: Base of InitIO controller
 *	@addr: Address of word in E2PROM
 *
 *	Read a word from the NV E2PROM device
 */
static u16 initio_se2_rd(unsigned long base, u8 addr)
Linus Torvalds's avatar
Linus Torvalds committed
368
{
369 370
	u8 instr, rb;
	u16 val = 0;
Linus Torvalds's avatar
Linus Torvalds committed
371 372
	int i;

373 374
	instr = (u8) (addr | 0x80);
	initio_se2_instr(base, instr);	/* READ INSTR */
Linus Torvalds's avatar
Linus Torvalds committed
375 376

	for (i = 15; i >= 0; i--) {
377 378 379
		outb(SE2CS | SE2CLK, base + TUL_NVRAM);	/* +CLK */
		udelay(30);
		outb(SE2CS, base + TUL_NVRAM);		/* -CLK */
Linus Torvalds's avatar
Linus Torvalds committed
380 381

		/* sample data after the following edge of clock  */
382 383 384 385
		rb = inb(base + TUL_NVRAM);
		rb &= SE2DI;
		val += (rb << i);
		udelay(30);	/* 6/20/95 */
Linus Torvalds's avatar
Linus Torvalds committed
386 387
	}

388 389 390
	outb(0, base + TUL_NVRAM);		/* no chip select */
	udelay(30);
	return val;
Linus Torvalds's avatar
Linus Torvalds committed
391 392
}

393 394 395 396 397 398 399 400 401 402
/**
 *	initio_se2_wr		-	read E2PROM word
 *	@base: Base of InitIO controller
 *	@addr: Address of word in E2PROM
 *	@val: Value to write
 *
 *	Write a word to the NV E2PROM device. Used when recovering from
 *	a problem with the NV.
 */
static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
Linus Torvalds's avatar
Linus Torvalds committed
403
{
404 405
	u8 rb;
	u8 instr;
Linus Torvalds's avatar
Linus Torvalds committed
406 407
	int i;

408 409
	instr = (u8) (addr | 0x40);
	initio_se2_instr(base, instr);	/* WRITE INSTR */
Linus Torvalds's avatar
Linus Torvalds committed
410
	for (i = 15; i >= 0; i--) {
411 412
		if (val & 0x8000)
			outb(SE2CS | SE2DO, base + TUL_NVRAM);	/* -CLK+dataBit 1 */
Linus Torvalds's avatar
Linus Torvalds committed
413
		else
414 415 416 417 418
			outb(SE2CS, base + TUL_NVRAM);		/* -CLK+dataBit 0 */
		udelay(30);
		outb(SE2CS | SE2CLK, base + TUL_NVRAM);		/* +CLK */
		udelay(30);
		val <<= 1;
Linus Torvalds's avatar
Linus Torvalds committed
419
	}
420 421 422 423
	outb(SE2CS, base + TUL_NVRAM);				/* -CLK */
	udelay(30);
	outb(0, base + TUL_NVRAM);				/* -CS  */
	udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
424

425 426
	outb(SE2CS, base + TUL_NVRAM);				/* +CS  */
	udelay(30);
Linus Torvalds's avatar
Linus Torvalds committed
427 428

	for (;;) {
429 430 431 432 433
		outb(SE2CS | SE2CLK, base + TUL_NVRAM);		/* +CLK */
		udelay(30);
		outb(SE2CS, base + TUL_NVRAM);			/* -CLK */
		udelay(30);
		if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
Linus Torvalds's avatar
Linus Torvalds committed
434 435
			break;	/* write complete */
	}
436
	outb(0, base + TUL_NVRAM);				/* -CS */
Linus Torvalds's avatar
Linus Torvalds committed
437 438
}

439 440 441 442 443 444 445
/**
 *	initio_se2_rd_all	-	read hostadapter NV configuration
 *	@base: Base address of InitIO controller
 *
 *	Reads the E2PROM data into main memory. Ensures that the checksum
 *	and header marker are valid. Returns 1 on success -1 on error.
 */
Linus Torvalds's avatar
Linus Torvalds committed
446

447
static int initio_se2_rd_all(unsigned long base)
Linus Torvalds's avatar
Linus Torvalds committed
448 449
{
	int i;
450 451
	u16 chksum = 0;
	u16 *np;
Linus Torvalds's avatar
Linus Torvalds committed
452 453

	i91unvramp = &i91unvram;
454 455 456
	np = (u16 *) i91unvramp;
	for (i = 0; i < 32; i++)
		*np++ = initio_se2_rd(base, i);
Linus Torvalds's avatar
Linus Torvalds committed
457

458
	/* Is signature "ini" ok ? */
Linus Torvalds's avatar
Linus Torvalds committed
459 460
	if (i91unvramp->NVM_Signature != INI_SIGNATURE)
		return -1;
461 462
	/* Is ckecksum ok ? */
	np = (u16 *) i91unvramp;
Linus Torvalds's avatar
Linus Torvalds committed
463 464
	for (i = 0; i < 31; i++)
		chksum += *np++;
465
	if (i91unvramp->NVM_CheckSum != chksum)
Linus Torvalds's avatar
Linus Torvalds committed
466 467 468 469
		return -1;
	return 1;
}

470 471 472 473 474 475 476 477
/**
 *	initio_se2_update_all		-	Update E2PROM
 *	@base: Base of InitIO controller
 *
 *	Update the E2PROM by wrting any changes into the E2PROM
 *	chip, rewriting the checksum.
 */
static void initio_se2_update_all(unsigned long base)
Linus Torvalds's avatar
Linus Torvalds committed
478 479
{				/* setup default pattern */
	int i;
480 481
	u16 chksum = 0;
	u16 *np, *np1;
Linus Torvalds's avatar
Linus Torvalds committed
482 483 484

	i91unvramp = &i91unvram;
	/* Calculate checksum first */
485
	np = (u16 *) i91udftNvRam;
Linus Torvalds's avatar
Linus Torvalds committed
486 487
	for (i = 0; i < 31; i++)
		chksum += *np++;
488 489
	*np = chksum;
	initio_se2_ew_en(base);	/* Enable write  */
Linus Torvalds's avatar
Linus Torvalds committed
490

491 492
	np = (u16 *) i91udftNvRam;
	np1 = (u16 *) i91unvramp;
Linus Torvalds's avatar
Linus Torvalds committed
493
	for (i = 0; i < 32; i++, np++, np1++) {
494 495
		if (*np != *np1)
			initio_se2_wr(base, i, *np);
Linus Torvalds's avatar
Linus Torvalds committed
496
	}
497
	initio_se2_ew_ds(base);	/* Disable write   */
Linus Torvalds's avatar
Linus Torvalds committed
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511
/**
 *	initio_read_eeprom		-	Retrieve configuration
 *	@base: Base of InitIO Host Adapter
 *
 *	Retrieve the host adapter configuration data from E2Prom. If the
 *	data is invalid then the defaults are used and are also restored
 *	into the E2PROM. This forms the access point for the SCSI driver
 *	into the E2PROM layer, the other functions for the E2PROM are all
 *	internal use.
 *
 *	Must be called single threaded, uses a shared global area.
 */
Linus Torvalds's avatar
Linus Torvalds committed
512

513
static void initio_read_eeprom(unsigned long base)
Linus Torvalds's avatar
Linus Torvalds committed
514
{
515
	u8 gctrl;
Linus Torvalds's avatar
Linus Torvalds committed
516

517 518 519 520 521 522 523
	i91unvramp = &i91unvram;
	/* Enable EEProm programming */
	gctrl = inb(base + TUL_GCTRL);
	outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
	if (initio_se2_rd_all(base) != 1) {
		initio_se2_update_all(base);	/* setup default pattern */
		initio_se2_rd_all(base);	/* load again  */
Linus Torvalds's avatar
Linus Torvalds committed
524
	}
525 526 527
	/* Disable EEProm programming */
	gctrl = inb(base + TUL_GCTRL);
	outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
Linus Torvalds's avatar
Linus Torvalds committed
528 529
}

530 531 532 533
/**
 *	initio_stop_bm		-	stop bus master
 *	@host: InitIO we are stopping
 *
534
 *	Stop any pending DMA operation, aborting the DMA if necessary
535
 */
Linus Torvalds's avatar
Linus Torvalds committed
536

537
static void initio_stop_bm(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
538 539
{

540 541
	if (inb(host->addr + TUL_XStatus) & XPEND) {	/* if DMA xfer is pending, abort DMA xfer */
		outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
Linus Torvalds's avatar
Linus Torvalds committed
542
		/* wait Abort DMA xfer done */
543 544
		while ((inb(host->addr + TUL_Int) & XABT) == 0)
			cpu_relax();
Linus Torvalds's avatar
Linus Torvalds committed
545
	}
546
	outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
Linus Torvalds's avatar
Linus Torvalds committed
547 548
}

549 550 551 552 553 554 555
/**
 *	initio_reset_scsi		-	Reset SCSI host controller
 *	@host: InitIO host to reset
 *	@seconds: Recovery time
 *
 *	Perform a full reset of the SCSI subsystem.
 */
Linus Torvalds's avatar
Linus Torvalds committed
556

557
static int initio_reset_scsi(struct initio_host * host, int seconds)
Linus Torvalds's avatar
Linus Torvalds committed
558
{
559
	outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
Linus Torvalds's avatar
Linus Torvalds committed
560

561 562
	while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
		cpu_relax();
Linus Torvalds's avatar
Linus Torvalds committed
563

564 565
	/* reset tulip chip */
	outb(0, host->addr + TUL_SSignal);
Linus Torvalds's avatar
Linus Torvalds committed
566 567 568

	/* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
	/* SONY 5200 tape drive won't work if only stall for 1 sec */
569 570
	/* FIXME: this is a very long busy wait right now */
	initio_do_pause(seconds * HZ);
Linus Torvalds's avatar
Linus Torvalds committed
571

572 573
	inb(host->addr + TUL_SInt);
	return SCSI_RESET_SUCCESS;
Linus Torvalds's avatar
Linus Torvalds committed
574 575
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589
/**
 *	initio_init		-	set up an InitIO host adapter
 *	@host: InitIO host adapter
 *	@num_scbs: Number of SCBS
 *	@bios_addr: BIOS address
 *
 *	Set up the host adapter and devices according to the configuration
 *	retrieved from the E2PROM.
 *
 *	Locking: Calls E2PROM layer code which is not re-enterable so must
 *	run single threaded for now.
 */

static void initio_init(struct initio_host * host, u8 *bios_addr)
Linus Torvalds's avatar
Linus Torvalds committed
590 591
{
	int i;
592 593 594 595 596
	u8 *flags;
	u8 *heads;

	/* Get E2Prom configuration */
	initio_read_eeprom(host->addr);
Linus Torvalds's avatar
Linus Torvalds committed
597
	if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
598
		host->max_tar = 8;
Linus Torvalds's avatar
Linus Torvalds committed
599
	else
600
		host->max_tar = 16;
Linus Torvalds's avatar
Linus Torvalds committed
601

602
	host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
Linus Torvalds's avatar
Linus Torvalds committed
603

604 605
	host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
	host->idmask = ~(1 << host->scsi_id);
Linus Torvalds's avatar
Linus Torvalds committed
606

607
#ifdef CHK_PARITY
Linus Torvalds's avatar
Linus Torvalds committed
608
	/* Enable parity error response */
609
	outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
Linus Torvalds's avatar
Linus Torvalds committed
610 611 612
#endif

	/* Mask all the interrupt       */
613
	outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds's avatar
Linus Torvalds committed
614

615
	initio_stop_bm(host);
Linus Torvalds's avatar
Linus Torvalds committed
616
	/* --- Initialize the tulip --- */
617
	outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
Linus Torvalds's avatar
Linus Torvalds committed
618 619

	/* program HBA's SCSI ID        */
620
	outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
Linus Torvalds's avatar
Linus Torvalds committed
621 622 623

	/* Enable Initiator Mode ,phase latch,alternate sync period mode,
	   disable SCSI reset */
624 625
	if (host->config & HCC_EN_PAR)
		host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
Linus Torvalds's avatar
Linus Torvalds committed
626
	else
627 628
		host->sconf1 = (TSC_INITDEFAULT);
	outb(host->sconf1, host->addr + TUL_SConfig);
Linus Torvalds's avatar
Linus Torvalds committed
629

630 631
	/* Enable HW reselect */
	outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
Linus Torvalds's avatar
Linus Torvalds committed
632

633
	outb(0, host->addr + TUL_SPeriod);
Linus Torvalds's avatar
Linus Torvalds committed
634 635

	/* selection time out = 250 ms */
636
	outb(153, host->addr + TUL_STimeOut);
Linus Torvalds's avatar
Linus Torvalds committed
637

638 639 640 641 642 643
	/* Enable SCSI terminator */
	outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
		host->addr + TUL_XCtrl);
	outb(((host->config & HCC_AUTO_TERM) >> 4) |
		(inb(host->addr + TUL_GCTRL1) & 0xFE),
		host->addr + TUL_GCTRL1);
Linus Torvalds's avatar
Linus Torvalds committed
644 645

	for (i = 0,
646 647 648 649 650 651 652
	     flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
	     heads = bios_addr + 0x180;
	     i < host->max_tar;
	     i++, flags++) {
		host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
		if (host->targets[i].flags & TCF_EN_255)
			host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds's avatar
Linus Torvalds committed
653
		else
654 655 656 657 658 659
			host->targets[i].drv_flags = 0;
		host->targets[i].js_period = 0;
		host->targets[i].sconfig0 = host->sconf1;
		host->targets[i].heads = *heads++;
		if (host->targets[i].heads == 255)
			host->targets[i].drv_flags = TCF_DRV_255_63;
Linus Torvalds's avatar
Linus Torvalds committed
660
		else
661 662 663 664 665
			host->targets[i].drv_flags = 0;
		host->targets[i].sectors = *heads++;
		host->targets[i].flags &= ~TCF_BUSY;
		host->act_tags[i] = 0;
		host->max_tags[i] = 0xFF;
Linus Torvalds's avatar
Linus Torvalds committed
666 667
	}			/* for                          */
	printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
Alan Cox's avatar
Alan Cox committed
668
	       host->addr, host->pci_dev->irq,
669 670 671 672 673
	       host->bios_addr, host->scsi_id);
	/* Reset SCSI Bus */
	if (host->config & HCC_SCSI_RESET) {
		printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
		initio_reset_scsi(host, 10);
Linus Torvalds's avatar
Linus Torvalds committed
674
	}
675 676
	outb(0x17, host->addr + TUL_SCFG1);
	outb(0xE9, host->addr + TUL_SIntEnable);
Linus Torvalds's avatar
Linus Torvalds committed
677 678
}

679 680 681 682 683 684 685 686
/**
 *	initio_alloc_scb		-	Allocate an SCB
 *	@host: InitIO host we are allocating for
 *
 *	Walk the SCB list for the controller and allocate a free SCB if
 *	one exists.
 */
static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
Linus Torvalds's avatar
Linus Torvalds committed
687
{
688 689 690 691 692
	struct scsi_ctrl_blk *scb;
	unsigned long flags;

	spin_lock_irqsave(&host->avail_lock, flags);
	if ((scb = host->first_avail) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
693
#if DEBUG_QUEUE
694
		printk("find scb at %p\n", scb);
Linus Torvalds's avatar
Linus Torvalds committed
695
#endif
696 697 698 699
		if ((host->first_avail = scb->next) == NULL)
			host->last_avail = NULL;
		scb->next = NULL;
		scb->status = SCB_RENT;
Linus Torvalds's avatar
Linus Torvalds committed
700
	}
701 702
	spin_unlock_irqrestore(&host->avail_lock, flags);
	return scb;
Linus Torvalds's avatar
Linus Torvalds committed
703 704
}

705 706 707 708 709 710 711 712 713
/**
 *	initio_release_scb		-	Release an SCB
 *	@host: InitIO host that owns the SCB
 *	@cmnd: SCB command block being returned
 *
 *	Return an allocated SCB to the host free list
 */

static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
Linus Torvalds's avatar
Linus Torvalds committed
714
{
715
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
716 717

#if DEBUG_QUEUE
718
	printk("Release SCB %p; ", cmnd);
Linus Torvalds's avatar
Linus Torvalds committed
719
#endif
720 721 722 723 724 725 726
	spin_lock_irqsave(&(host->avail_lock), flags);
	cmnd->srb = NULL;
	cmnd->status = 0;
	cmnd->next = NULL;
	if (host->last_avail != NULL) {
		host->last_avail->next = cmnd;
		host->last_avail = cmnd;
Linus Torvalds's avatar
Linus Torvalds committed
727
	} else {
728 729
		host->first_avail = cmnd;
		host->last_avail = cmnd;
Linus Torvalds's avatar
Linus Torvalds committed
730
	}
731
	spin_unlock_irqrestore(&(host->avail_lock), flags);
Linus Torvalds's avatar
Linus Torvalds committed
732 733 734
}

/***************************************************************************/
735
static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds's avatar
Linus Torvalds committed
736 737 738
{

#if DEBUG_QUEUE
739
	printk("Append pend SCB %p; ", scbp);
Linus Torvalds's avatar
Linus Torvalds committed
740
#endif
741 742 743 744 745
	scbp->status = SCB_PEND;
	scbp->next = NULL;
	if (host->last_pending != NULL) {
		host->last_pending->next = scbp;
		host->last_pending = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
746
	} else {
747 748
		host->first_pending = scbp;
		host->last_pending = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
749 750 751 752
	}
}

/***************************************************************************/
753
static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
{

#if DEBUG_QUEUE
757
	printk("Push pend SCB %p; ", scbp);
Linus Torvalds's avatar
Linus Torvalds committed
758
#endif
759 760 761
	scbp->status = SCB_PEND;
	if ((scbp->next = host->first_pending) != NULL) {
		host->first_pending = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
762
	} else {
763 764
		host->first_pending = scbp;
		host->last_pending = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
765 766 767
	}
}

768
static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
769
{
770
	struct scsi_ctrl_blk *first;
Linus Torvalds's avatar
Linus Torvalds committed
771 772


773 774 775 776 777 778 779 780
	first = host->first_pending;
	while (first != NULL) {
		if (first->opcode != ExecSCSI)
			return first;
		if (first->tagmsg == 0) {
			if ((host->act_tags[first->target] == 0) &&
			    !(host->targets[first->target].flags & TCF_BUSY))
				return first;
Linus Torvalds's avatar
Linus Torvalds committed
781
		} else {
782 783 784 785
			if ((host->act_tags[first->target] >=
			  host->max_tags[first->target]) |
			    (host->targets[first->target].flags & TCF_BUSY)) {
				first = first->next;
Linus Torvalds's avatar
Linus Torvalds committed
786 787
				continue;
			}
788
			return first;
Linus Torvalds's avatar
Linus Torvalds committed
789
		}
790
		first = first->next;
Linus Torvalds's avatar
Linus Torvalds committed
791
	}
792
	return first;
Linus Torvalds's avatar
Linus Torvalds committed
793
}
794 795

static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds's avatar
Linus Torvalds committed
796
{
797
	struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds's avatar
Linus Torvalds committed
798 799

#if DEBUG_QUEUE
800
	printk("unlink pend SCB %p; ", scb);
Linus Torvalds's avatar
Linus Torvalds committed
801 802
#endif

803 804 805 806 807 808
	prev = tmp = host->first_pending;
	while (tmp != NULL) {
		if (scb == tmp) {	/* Unlink this SCB              */
			if (tmp == host->first_pending) {
				if ((host->first_pending = tmp->next) == NULL)
					host->last_pending = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
809
			} else {
810 811 812
				prev->next = tmp->next;
				if (tmp == host->last_pending)
					host->last_pending = prev;
Linus Torvalds's avatar
Linus Torvalds committed
813
			}
814
			tmp->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
815 816
			break;
		}
817 818
		prev = tmp;
		tmp = tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
819 820
	}
}
821 822

static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds's avatar
Linus Torvalds committed
823 824 825
{

#if DEBUG_QUEUE
826
	printk("append busy SCB %p; ", scbp);
Linus Torvalds's avatar
Linus Torvalds committed
827
#endif
828 829
	if (scbp->tagmsg)
		host->act_tags[scbp->target]++;
Linus Torvalds's avatar
Linus Torvalds committed
830
	else
831 832 833 834 835 836
		host->targets[scbp->target].flags |= TCF_BUSY;
	scbp->status = SCB_BUSY;
	scbp->next = NULL;
	if (host->last_busy != NULL) {
		host->last_busy->next = scbp;
		host->last_busy = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
837
	} else {
838 839
		host->first_busy = scbp;
		host->last_busy = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
840 841 842 843
	}
}

/***************************************************************************/
844
static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
845
{
846
	struct scsi_ctrl_blk *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
847 848


849 850 851 852 853 854
	if ((tmp = host->first_busy) != NULL) {
		if ((host->first_busy = tmp->next) == NULL)
			host->last_busy = NULL;
		tmp->next = NULL;
		if (tmp->tagmsg)
			host->act_tags[tmp->target]--;
Linus Torvalds's avatar
Linus Torvalds committed
855
		else
856
			host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
857 858
	}
#if DEBUG_QUEUE
859
	printk("Pop busy SCB %p; ", tmp);
Linus Torvalds's avatar
Linus Torvalds committed
860
#endif
861
	return tmp;
Linus Torvalds's avatar
Linus Torvalds committed
862 863 864
}

/***************************************************************************/
865
static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds's avatar
Linus Torvalds committed
866
{
867
	struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds's avatar
Linus Torvalds committed
868 869

#if DEBUG_QUEUE
870
	printk("unlink busy SCB %p; ", scb);
Linus Torvalds's avatar
Linus Torvalds committed
871 872
#endif

873 874 875 876 877 878
	prev = tmp = host->first_busy;
	while (tmp != NULL) {
		if (scb == tmp) {	/* Unlink this SCB              */
			if (tmp == host->first_busy) {
				if ((host->first_busy = tmp->next) == NULL)
					host->last_busy = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
879
			} else {
880 881 882
				prev->next = tmp->next;
				if (tmp == host->last_busy)
					host->last_busy = prev;
Linus Torvalds's avatar
Linus Torvalds committed
883
			}
884 885 886
			tmp->next = NULL;
			if (tmp->tagmsg)
				host->act_tags[tmp->target]--;
Linus Torvalds's avatar
Linus Torvalds committed
887
			else
888
				host->targets[tmp->target].flags &= ~TCF_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
889 890
			break;
		}
891 892
		prev = tmp;
		tmp = tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
893 894 895 896
	}
	return;
}

897
struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
Linus Torvalds's avatar
Linus Torvalds committed
898
{
899 900
	struct scsi_ctrl_blk *tmp, *prev;
	u16 scbp_tarlun;
Linus Torvalds's avatar
Linus Torvalds committed
901 902


903 904 905
	prev = tmp = host->first_busy;
	while (tmp != NULL) {
		scbp_tarlun = (tmp->lun << 8) | (tmp->target);
Linus Torvalds's avatar
Linus Torvalds committed
906 907 908
		if (scbp_tarlun == tarlun) {	/* Unlink this SCB              */
			break;
		}
909 910
		prev = tmp;
		tmp = tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
911 912
	}
#if DEBUG_QUEUE
913
	printk("find busy SCB %p; ", tmp);
Linus Torvalds's avatar
Linus Torvalds committed
914
#endif
915
	return tmp;
Linus Torvalds's avatar
Linus Torvalds committed
916 917
}

918
static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
Linus Torvalds's avatar
Linus Torvalds committed
919 920
{
#if DEBUG_QUEUE
921
	printk("append done SCB %p; ", scbp);
Linus Torvalds's avatar
Linus Torvalds committed
922 923
#endif

924 925 926 927 928
	scbp->status = SCB_DONE;
	scbp->next = NULL;
	if (host->last_done != NULL) {
		host->last_done->next = scbp;
		host->last_done = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
929
	} else {
930 931
		host->first_done = scbp;
		host->last_done = scbp;
Linus Torvalds's avatar
Linus Torvalds committed
932 933 934
	}
}

935
struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
936
{
937
	struct scsi_ctrl_blk *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
938

939 940 941 942
	if ((tmp = host->first_done) != NULL) {
		if ((host->first_done = tmp->next) == NULL)
			host->last_done = NULL;
		tmp->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
943 944
	}
#if DEBUG_QUEUE
945
	printk("find done SCB %p; ",tmp);
Linus Torvalds's avatar
Linus Torvalds committed
946
#endif
947
	return tmp;
Linus Torvalds's avatar
Linus Torvalds committed
948 949
}

950
static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
Linus Torvalds's avatar
Linus Torvalds committed
951
{
952 953
	unsigned long flags;
	struct scsi_ctrl_blk *tmp, *prev;
Linus Torvalds's avatar
Linus Torvalds committed
954

955
	spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
956

957
	if ((host->semaph == 0) && (host->active == NULL)) {
Linus Torvalds's avatar
Linus Torvalds committed
958
		/* disable Jasmin SCSI Int        */
959 960 961 962 963 964 965 966
		outb(0x1F, host->addr + TUL_Mask);
		spin_unlock_irqrestore(&host->semaph_lock, flags);
		/* FIXME: synchronize_irq needed ? */
		tulip_main(host);
		spin_lock_irqsave(&host->semaph_lock, flags);
		host->semaph = 1;
		outb(0x0F, host->addr + TUL_Mask);
		spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
967 968
		return SCSI_ABORT_SNOOZE;
	}
969 970
	prev = tmp = host->first_pending;	/* Check Pend queue */
	while (tmp != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
971
		/* 07/27/98 */
972 973 974
		if (tmp->srb == srbp) {
			if (tmp == host->active) {
				spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
975
				return SCSI_ABORT_BUSY;
976 977 978
			} else if (tmp == host->first_pending) {
				if ((host->first_pending = tmp->next) == NULL)
					host->last_pending = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
979
			} else {
980 981 982
				prev->next = tmp->next;
				if (tmp == host->last_pending)
					host->last_pending = prev;
Linus Torvalds's avatar
Linus Torvalds committed
983
			}
984 985 986 987 988
			tmp->hastat = HOST_ABORTED;
			tmp->flags |= SCF_DONE;
			if (tmp->flags & SCF_POST)
				(*tmp->post) ((u8 *) host, (u8 *) tmp);
			spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
989 990
			return SCSI_ABORT_SUCCESS;
		}
991 992
		prev = tmp;
		tmp = tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
993 994
	}

995 996 997 998 999
	prev = tmp = host->first_busy;	/* Check Busy queue */
	while (tmp != NULL) {
		if (tmp->srb == srbp) {
			if (tmp == host->active) {
				spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1000
				return SCSI_ABORT_BUSY;
1001 1002
			} else if (tmp->tagmsg == 0) {
				spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1003 1004
				return SCSI_ABORT_BUSY;
			} else {
1005 1006 1007 1008
				host->act_tags[tmp->target]--;
				if (tmp == host->first_busy) {
					if ((host->first_busy = tmp->next) == NULL)
						host->last_busy = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1009
				} else {
1010 1011 1012
					prev->next = tmp->next;
					if (tmp == host->last_busy)
						host->last_busy = prev;
Linus Torvalds's avatar
Linus Torvalds committed
1013
				}
1014
				tmp->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1015 1016


1017 1018 1019 1020 1021
				tmp->hastat = HOST_ABORTED;
				tmp->flags |= SCF_DONE;
				if (tmp->flags & SCF_POST)
					(*tmp->post) ((u8 *) host, (u8 *) tmp);
				spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1022 1023 1024
				return SCSI_ABORT_SUCCESS;
			}
		}
1025 1026
		prev = tmp;
		tmp = tmp->next;
Linus Torvalds's avatar
Linus Torvalds committed
1027
	}
1028 1029
	spin_unlock_irqrestore(&host->semaph_lock, flags);
	return SCSI_ABORT_NOT_RUNNING;
Linus Torvalds's avatar
Linus Torvalds committed
1030 1031 1032
}

/***************************************************************************/
1033
static int initio_bad_seq(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
1034
{
1035
	struct scsi_ctrl_blk *scb;
Linus Torvalds's avatar
Linus Torvalds committed
1036

1037
	printk("initio_bad_seg c=%d\n", host->index);
Linus Torvalds's avatar
Linus Torvalds committed
1038

1039 1040 1041 1042 1043
	if ((scb = host->active) != NULL) {
		initio_unlink_busy_scb(host, scb);
		scb->hastat = HOST_BAD_PHAS;
		scb->tastat = 0;
		initio_append_done_scb(host, scb);
Linus Torvalds's avatar
Linus Torvalds committed
1044
	}
1045 1046 1047
	initio_stop_bm(host);
	initio_reset_scsi(host, 8);	/* 7/29/98 */
	return initio_post_scsi_rst(host);
Linus Torvalds's avatar
Linus Torvalds committed
1048 1049
}

1050

Linus Torvalds's avatar
Linus Torvalds committed
1051
/************************************************************************/
1052
static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
Linus Torvalds's avatar
Linus Torvalds committed
1053
{
1054
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1055

1056
	scb->mode = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1057

1058 1059
	scb->sgidx = 0;
	scb->sgmax = scb->sglen;
Linus Torvalds's avatar
Linus Torvalds committed
1060

1061
	spin_lock_irqsave(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1062

1063
	initio_append_pend_scb(host, scb);	/* Append this SCB to Pending queue */
Linus Torvalds's avatar
Linus Torvalds committed
1064 1065

/* VVVVV 07/21/98 */
1066 1067 1068 1069 1070
	if (host->semaph == 1) {
		/* Disable Jasmin SCSI Int */
		outb(0x1F, host->addr + TUL_Mask);
		host->semaph = 0;
		spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1071

1072
		tulip_main(host);
Linus Torvalds's avatar
Linus Torvalds committed
1073

1074 1075 1076
		spin_lock_irqsave(&host->semaph_lock, flags);
		host->semaph = 1;
		outb(0x0F, host->addr + TUL_Mask);
Linus Torvalds's avatar
Linus Torvalds committed
1077
	}
1078
	spin_unlock_irqrestore(&host->semaph_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1079 1080 1081 1082
	return;
}

/***************************************************************************/
1083
static int initio_isr(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
1084
{
1085 1086 1087
	if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
		if (host->semaph == 1) {
			outb(0x1F, host->addr + TUL_Mask);
Linus Torvalds's avatar
Linus Torvalds committed
1088
			/* Disable Tulip SCSI Int */
1089
			host->semaph = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1090

1091
			tulip_main(host);
Linus Torvalds's avatar
Linus Torvalds committed
1092

1093 1094 1095
			host->semaph = 1;
			outb(0x0F, host->addr + TUL_Mask);
			return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1096 1097
		}
	}
1098
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1099 1100
}

1101
static int tulip_main(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
1102
{
1103
	struct scsi_ctrl_blk *scb;
Linus Torvalds's avatar
Linus Torvalds committed
1104 1105

	for (;;) {
1106 1107 1108 1109 1110 1111 1112 1113 1114
		tulip_scsi(host);	/* Call tulip_scsi              */

		/* Walk the list of completed SCBs */
		while ((scb = initio_find_done_scb(host)) != NULL) {	/* find done entry */
			if (scb->tastat == INI_QUEUE_FULL) {
				host->max_tags[scb->target] =
				    host->act_tags[scb->target] - 1;
				scb->tastat = 0;
				initio_append_pend_scb(host, scb);
Linus Torvalds's avatar
Linus Torvalds committed
1115 1116
				continue;
			}
1117 1118
			if (!(scb->mode & SCM_RSENS)) {		/* not in auto req. sense mode */
				if (scb->tastat == 2) {
Linus Torvalds's avatar
Linus Torvalds committed
1119 1120 1121

					/* clr sync. nego flag */

1122 1123 1124
					if (scb->flags & SCF_SENSE) {
						u8 len;
						len = scb->senselen;
Linus Torvalds's avatar
Linus Torvalds committed
1125 1126
						if (len == 0)
							len = 1;
1127 1128 1129 1130
						scb->buflen = scb->senselen;
						scb->bufptr = scb->senseptr;
						scb->flags &= ~(SCF_SG | SCF_DIR);	/* for xfer_data_in */
						/* so, we won't report wrong direction in xfer_data_in,
Linus Torvalds's avatar
Linus Torvalds committed
1131
						   and won't report HOST_DO_DU in state_6 */
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
						scb->mode = SCM_RSENS;
						scb->ident &= 0xBF;	/* Disable Disconnect */
						scb->tagmsg = 0;
						scb->tastat = 0;
						scb->cdblen = 6;
						scb->cdb[0] = SCSICMD_RequestSense;
						scb->cdb[1] = 0;
						scb->cdb[2] = 0;
						scb->cdb[3] = 0;
						scb->cdb[4] = len;
						scb->cdb[5] = 0;
						initio_push_pend_scb(host, scb);
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145 1146 1147 1148
						break;
					}
				}
			} else {	/* in request sense mode */

1149
				if (scb->tastat == 2) {		/* check contition status again after sending
Linus Torvalds's avatar
Linus Torvalds committed
1150
									   requset sense cmd 0x3 */
1151
					scb->hastat = HOST_BAD_PHAS;
Linus Torvalds's avatar
Linus Torvalds committed
1152
				}
1153
				scb->tastat = 2;
Linus Torvalds's avatar
Linus Torvalds committed
1154
			}
1155 1156 1157 1158
			scb->flags |= SCF_DONE;
			if (scb->flags & SCF_POST) {
				/* FIXME: only one post method and lose casts */
				(*scb->post) ((u8 *) host, (u8 *) scb);
Linus Torvalds's avatar
Linus Torvalds committed
1159 1160 1161
			}
		}		/* while */
		/* find_active: */
1162
		if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
Linus Torvalds's avatar
Linus Torvalds committed
1163
			continue;
1164
		if (host->active)	/* return to OS and wait for xfer_done_ISR/Selected_ISR */
Linus Torvalds's avatar
Linus Torvalds committed
1165 1166
			return 1;	/* return to OS, enable interrupt */
		/* Check pending SCB            */
1167
		if (initio_find_first_pend_scb(host) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1168 1169 1170 1171 1172
			return 1;	/* return to OS, enable interrupt */
	}			/* End of for loop */
	/* statement won't reach here */
}

1173
static void tulip_scsi(struct initio_host * host)
Linus Torvalds's avatar
Linus Torvalds committed
1174
{
1175 1176
	struct scsi_ctrl_blk *scb;
	struct target_control *active_tc;
Linus Torvalds's avatar
Linus Torvalds committed
1177 1178

	/* make sure to service interrupt asap */
1179 1180 1181 1182 1183 1184
	if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
		host->phase = host->jsstatus0 & TSS_PH_MASK;
		host->jsstatus1 = inb(host->addr + TUL_SStatus1);
		host->jsint = inb(host->addr + TUL_SInt);
		if (host->jsint & TSS_SCSIRST_INT) {	/* SCSI bus reset detected      */
			int_initio_scsi_rst(host);
Linus Torvalds's avatar
Linus Torvalds committed
1185 1186
			return;
		}
1187 1188 1189
		if (host->jsint & TSS_RESEL_INT) {	/* if selected/reselected interrupt */
			if (int_initio_resel(host) == 0)
				initio_next_state(host);
Linus Torvalds's avatar
Linus Torvalds committed
1190 1191
			return;
		}
1192 1193
		if (host->jsint & TSS_SEL_TIMEOUT) {
			int_initio_busfree(host);
Linus Torvalds's avatar
Linus Torvalds committed
1194 1195
			return;
		}
1196 1197
		if (host->jsint & TSS_DISC_INT) {	/* BUS disconnection            */
			int_initio_busfree(host);	/* unexpected bus free or sel timeout */
Linus Torvalds's avatar
Linus Torvalds committed
1198 1199
			return;
		}
1200 1201 1202
		if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) {	/* func complete or Bus service */
			if ((scb = host->active) != NULL)
				initio_next_state(host);
Linus Torvalds's avatar
Linus Torvalds committed
1203 1204 1205
			return;
		}
	}
1206
	if (host->active != NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1207 1208
		return;

1209
	if ((scb = initio_find_first_pend_scb(host)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
121