Linux Audio

Check our new training course

Embedded Linux training

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