Linux Audio

Check our new training course

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