Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 *    in2000.c -  Linux device driver for the
   3 *                Always IN2000 ISA SCSI card.
   4 *
   5 * Copyright (c) 1996 John Shifflett, GeoLog Consulting
   6 *    john@geolog.com
   7 *    jshiffle@netcom.com
   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 * For the avoidance of doubt the "preferred form" of this code is one which
  20 * is in an open non patent encumbered format. Where cryptographic key signing
  21 * forms part of the process of creating an executable the information
  22 * including keys needed to generate an equivalently functional executable
  23 * are deemed to be part of the source code.
  24 *
  25 * Drew Eckhardt's excellent 'Generic NCR5380' sources provided
  26 * much of the inspiration and some of the code for this driver.
  27 * The Linux IN2000 driver distributed in the Linux kernels through
  28 * version 1.2.13 was an extremely valuable reference on the arcane
  29 * (and still mysterious) workings of the IN2000's fifo. It also
  30 * is where I lifted in2000_biosparam(), the gist of the card
  31 * detection scheme, and other bits of code. Many thanks to the
  32 * talented and courageous people who wrote, contributed to, and
  33 * maintained that driver (including Brad McLean, Shaun Savage,
  34 * Bill Earnest, Larry Doolittle, Roger Sunshine, John Luckey,
  35 * Matt Postiff, Peter Lu, zerucha@shell.portal.com, and Eric
  36 * Youngdale). I should also mention the driver written by
  37 * Hamish Macdonald for the (GASP!) Amiga A2091 card, included
  38 * in the Linux-m68k distribution; it gave me a good initial
  39 * understanding of the proper way to run a WD33c93 chip, and I
  40 * ended up stealing lots of code from it.
  41 *
  42 * _This_ driver is (I feel) an improvement over the old one in
  43 * several respects:
  44 *    -  All problems relating to the data size of a SCSI request are
  45 *          gone (as far as I know). The old driver couldn't handle
  46 *          swapping to partitions because that involved 4k blocks, nor
  47 *          could it deal with the st.c tape driver unmodified, because
  48 *          that usually involved 4k - 32k blocks. The old driver never
  49 *          quite got away from a morbid dependence on 2k block sizes -
  50 *          which of course is the size of the card's fifo.
  51 *
  52 *    -  Target Disconnection/Reconnection is now supported. Any
  53 *          system with more than one device active on the SCSI bus
  54 *          will benefit from this. The driver defaults to what I'm
  55 *          calling 'adaptive disconnect' - meaning that each command
  56 *          is evaluated individually as to whether or not it should
  57 *          be run with the option to disconnect/reselect (if the
  58 *          device chooses), or as a "SCSI-bus-hog".
  59 *
  60 *    -  Synchronous data transfers are now supported. Because there
  61 *          are a few devices (and many improperly terminated systems)
  62 *          that choke when doing sync, the default is sync DISABLED
  63 *          for all devices. This faster protocol can (and should!)
  64 *          be enabled on selected devices via the command-line.
  65 *
  66 *    -  Runtime operating parameters can now be specified through
  67 *       either the LILO or the 'insmod' command line. For LILO do:
  68 *          "in2000=blah,blah,blah"
  69 *       and with insmod go like:
  70 *          "insmod /usr/src/linux/modules/in2000.o setup_strings=blah,blah"
  71 *       The defaults should be good for most people. See the comment
  72 *       for 'setup_strings' below for more details.
  73 *
  74 *    -  The old driver relied exclusively on what the Western Digital
  75 *          docs call "Combination Level 2 Commands", which are a great
  76 *          idea in that the CPU is relieved of a lot of interrupt
  77 *          overhead. However, by accepting a certain (user-settable)
  78 *          amount of additional interrupts, this driver achieves
  79 *          better control over the SCSI bus, and data transfers are
  80 *          almost as fast while being much easier to define, track,
  81 *          and debug.
  82 *
  83 *    -  You can force detection of a card whose BIOS has been disabled.
  84 *
  85 *    -  Multiple IN2000 cards might almost be supported. I've tried to
  86 *       keep it in mind, but have no way to test...
  87 *
  88 *
  89 * TODO:
  90 *       tagged queuing. multiple cards.
  91 *
  92 *
  93 * NOTE:
  94 *       When using this or any other SCSI driver as a module, you'll
  95 *       find that with the stock kernel, at most _two_ SCSI hard
  96 *       drives will be linked into the device list (ie, usable).
  97 *       If your IN2000 card has more than 2 disks on its bus, you
  98 *       might want to change the define of 'SD_EXTRA_DEVS' in the
  99 *       'hosts.h' file from 2 to whatever is appropriate. It took
 100 *       me a while to track down this surprisingly obscure and
 101 *       undocumented little "feature".
 102 *
 103 *
 104 * People with bug reports, wish-lists, complaints, comments,
 105 * or improvements are asked to pah-leeez email me (John Shifflett)
 106 * at john@geolog.com or jshiffle@netcom.com! I'm anxious to get
 107 * this thing into as good a shape as possible, and I'm positive
 108 * there are lots of lurking bugs and "Stupid Places".
 109 *
 110 * Updated for Linux 2.5 by Alan Cox <alan@lxorguk.ukuu.org.uk>
 111 *	- Using new_eh handler
 112 *	- Hopefully got all the locking right again
 113 *	See "FIXME" notes for items that could do with more work
 114 */
 115
 116#include <linux/module.h>
 117#include <linux/blkdev.h>
 118#include <linux/interrupt.h>
 119#include <linux/string.h>
 120#include <linux/delay.h>
 121#include <linux/proc_fs.h>
 122#include <linux/ioport.h>
 123#include <linux/stat.h>
 124
 125#include <asm/io.h>
 126#include <asm/system.h>
 127
 128#include "scsi.h"
 129#include <scsi/scsi_host.h>
 130
 131#define IN2000_VERSION    "1.33-2.5"
 132#define IN2000_DATE       "2002/11/03"
 133
 134#include "in2000.h"
 135
 136
 137/*
 138 * 'setup_strings' is a single string used to pass operating parameters and
 139 * settings from the kernel/module command-line to the driver. 'setup_args[]'
 140 * is an array of strings that define the compile-time default values for
 141 * these settings. If Linux boots with a LILO or insmod command-line, those
 142 * settings are combined with 'setup_args[]'. Note that LILO command-lines
 143 * are prefixed with "in2000=" while insmod uses a "setup_strings=" prefix.
 144 * The driver recognizes the following keywords (lower case required) and
 145 * arguments:
 146 *
 147 * -  ioport:addr    -Where addr is IO address of a (usually ROM-less) card.
 148 * -  noreset        -No optional args. Prevents SCSI bus reset at boot time.
 149 * -  nosync:x       -x is a bitmask where the 1st 7 bits correspond with
 150 *                    the 7 possible SCSI devices (bit 0 for device #0, etc).
 151 *                    Set a bit to PREVENT sync negotiation on that device.
 152 *                    The driver default is sync DISABLED on all devices.
 153 * -  period:ns      -ns is the minimum # of nanoseconds in a SCSI data transfer
 154 *                    period. Default is 500; acceptable values are 250 - 1000.
 155 * -  disconnect:x   -x = 0 to never allow disconnects, 2 to always allow them.
 156 *                    x = 1 does 'adaptive' disconnects, which is the default
 157 *                    and generally the best choice.
 158 * -  debug:x        -If 'DEBUGGING_ON' is defined, x is a bitmask that causes
 159 *                    various types of debug output to printed - see the DB_xxx
 160 *                    defines in in2000.h
 161 * -  proc:x         -If 'PROC_INTERFACE' is defined, x is a bitmask that
 162 *                    determines how the /proc interface works and what it
 163 *                    does - see the PR_xxx defines in in2000.h
 164 *
 165 * Syntax Notes:
 166 * -  Numeric arguments can be decimal or the '0x' form of hex notation. There
 167 *    _must_ be a colon between a keyword and its numeric argument, with no
 168 *    spaces.
 169 * -  Keywords are separated by commas, no spaces, in the standard kernel
 170 *    command-line manner.
 171 * -  A keyword in the 'nth' comma-separated command-line member will overwrite
 172 *    the 'nth' element of setup_args[]. A blank command-line member (in
 173 *    other words, a comma with no preceding keyword) will _not_ overwrite
 174 *    the corresponding setup_args[] element.
 175 *
 176 * A few LILO examples (for insmod, use 'setup_strings' instead of 'in2000'):
 177 * -  in2000=ioport:0x220,noreset
 178 * -  in2000=period:250,disconnect:2,nosync:0x03
 179 * -  in2000=debug:0x1e
 180 * -  in2000=proc:3
 181 */
 182
 183/* Normally, no defaults are specified... */
 184static char *setup_args[] = { "", "", "", "", "", "", "", "", "" };
 185
 186/* filled in by 'insmod' */
 187static char *setup_strings;
 188
 189module_param(setup_strings, charp, 0);
 190
 191static inline uchar read_3393(struct IN2000_hostdata *hostdata, uchar reg_num)
 192{
 193	write1_io(reg_num, IO_WD_ADDR);
 194	return read1_io(IO_WD_DATA);
 195}
 196
 197
 198#define READ_AUX_STAT() read1_io(IO_WD_ASR)
 199
 200
 201static inline void write_3393(struct IN2000_hostdata *hostdata, uchar reg_num, uchar value)
 202{
 203	write1_io(reg_num, IO_WD_ADDR);
 204	write1_io(value, IO_WD_DATA);
 205}
 206
 207
 208static inline void write_3393_cmd(struct IN2000_hostdata *hostdata, uchar cmd)
 209{
 210/*   while (READ_AUX_STAT() & ASR_CIP)
 211      printk("|");*/
 212	write1_io(WD_COMMAND, IO_WD_ADDR);
 213	write1_io(cmd, IO_WD_DATA);
 214}
 215
 216
 217static uchar read_1_byte(struct IN2000_hostdata *hostdata)
 218{
 219	uchar asr, x = 0;
 220
 221	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
 222	write_3393_cmd(hostdata, WD_CMD_TRANS_INFO | 0x80);
 223	do {
 224		asr = READ_AUX_STAT();
 225		if (asr & ASR_DBR)
 226			x = read_3393(hostdata, WD_DATA);
 227	} while (!(asr & ASR_INT));
 228	return x;
 229}
 230
 231
 232static void write_3393_count(struct IN2000_hostdata *hostdata, unsigned long value)
 233{
 234	write1_io(WD_TRANSFER_COUNT_MSB, IO_WD_ADDR);
 235	write1_io((value >> 16), IO_WD_DATA);
 236	write1_io((value >> 8), IO_WD_DATA);
 237	write1_io(value, IO_WD_DATA);
 238}
 239
 240
 241static unsigned long read_3393_count(struct IN2000_hostdata *hostdata)
 242{
 243	unsigned long value;
 244
 245	write1_io(WD_TRANSFER_COUNT_MSB, IO_WD_ADDR);
 246	value = read1_io(IO_WD_DATA) << 16;
 247	value |= read1_io(IO_WD_DATA) << 8;
 248	value |= read1_io(IO_WD_DATA);
 249	return value;
 250}
 251
 252
 253/* The 33c93 needs to be told which direction a command transfers its
 254 * data; we use this function to figure it out. Returns true if there
 255 * will be a DATA_OUT phase with this command, false otherwise.
 256 * (Thanks to Joerg Dorchain for the research and suggestion.)
 257 */
 258static int is_dir_out(Scsi_Cmnd * cmd)
 259{
 260	switch (cmd->cmnd[0]) {
 261	case WRITE_6:
 262	case WRITE_10:
 263	case WRITE_12:
 264	case WRITE_LONG:
 265	case WRITE_SAME:
 266	case WRITE_BUFFER:
 267	case WRITE_VERIFY:
 268	case WRITE_VERIFY_12:
 269	case COMPARE:
 270	case COPY:
 271	case COPY_VERIFY:
 272	case SEARCH_EQUAL:
 273	case SEARCH_HIGH:
 274	case SEARCH_LOW:
 275	case SEARCH_EQUAL_12:
 276	case SEARCH_HIGH_12:
 277	case SEARCH_LOW_12:
 278	case FORMAT_UNIT:
 279	case REASSIGN_BLOCKS:
 280	case RESERVE:
 281	case MODE_SELECT:
 282	case MODE_SELECT_10:
 283	case LOG_SELECT:
 284	case SEND_DIAGNOSTIC:
 285	case CHANGE_DEFINITION:
 286	case UPDATE_BLOCK:
 287	case SET_WINDOW:
 288	case MEDIUM_SCAN:
 289	case SEND_VOLUME_TAG:
 290	case 0xea:
 291		return 1;
 292	default:
 293		return 0;
 294	}
 295}
 296
 297
 298
 299static struct sx_period sx_table[] = {
 300	{1, 0x20},
 301	{252, 0x20},
 302	{376, 0x30},
 303	{500, 0x40},
 304	{624, 0x50},
 305	{752, 0x60},
 306	{876, 0x70},
 307	{1000, 0x00},
 308	{0, 0}
 309};
 310
 311static int round_period(unsigned int period)
 312{
 313	int x;
 314
 315	for (x = 1; sx_table[x].period_ns; x++) {
 316		if ((period <= sx_table[x - 0].period_ns) && (period > sx_table[x - 1].period_ns)) {
 317			return x;
 318		}
 319	}
 320	return 7;
 321}
 322
 323static uchar calc_sync_xfer(unsigned int period, unsigned int offset)
 324{
 325	uchar result;
 326
 327	period *= 4;		/* convert SDTR code to ns */
 328	result = sx_table[round_period(period)].reg_value;
 329	result |= (offset < OPTIMUM_SX_OFF) ? offset : OPTIMUM_SX_OFF;
 330	return result;
 331}
 332
 333
 334
 335static void in2000_execute(struct Scsi_Host *instance);
 336
 337static int in2000_queuecommand_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
 338{
 339	struct Scsi_Host *instance;
 340	struct IN2000_hostdata *hostdata;
 341	Scsi_Cmnd *tmp;
 342
 343	instance = cmd->device->host;
 344	hostdata = (struct IN2000_hostdata *) instance->hostdata;
 345
 346	DB(DB_QUEUE_COMMAND, scmd_printk(KERN_DEBUG, cmd, "Q-%02x(", cmd->cmnd[0]))
 347
 348/* Set up a few fields in the Scsi_Cmnd structure for our own use:
 349 *  - host_scribble is the pointer to the next cmd in the input queue
 350 *  - scsi_done points to the routine we call when a cmd is finished
 351 *  - result is what you'd expect
 352 */
 353	    cmd->host_scribble = NULL;
 354	cmd->scsi_done = done;
 355	cmd->result = 0;
 356
 357/* We use the Scsi_Pointer structure that's included with each command
 358 * as a scratchpad (as it's intended to be used!). The handy thing about
 359 * the SCp.xxx fields is that they're always associated with a given
 360 * cmd, and are preserved across disconnect-reselect. This means we
 361 * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
 362 * if we keep all the critical pointers and counters in SCp:
 363 *  - SCp.ptr is the pointer into the RAM buffer
 364 *  - SCp.this_residual is the size of that buffer
 365 *  - SCp.buffer points to the current scatter-gather buffer
 366 *  - SCp.buffers_residual tells us how many S.G. buffers there are
 367 *  - SCp.have_data_in helps keep track of >2048 byte transfers
 368 *  - SCp.sent_command is not used
 369 *  - SCp.phase records this command's SRCID_ER bit setting
 370 */
 371
 372	if (scsi_bufflen(cmd)) {
 373		cmd->SCp.buffer = scsi_sglist(cmd);
 374		cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
 375		cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
 376		cmd->SCp.this_residual = cmd->SCp.buffer->length;
 377	} else {
 378		cmd->SCp.buffer = NULL;
 379		cmd->SCp.buffers_residual = 0;
 380		cmd->SCp.ptr = NULL;
 381		cmd->SCp.this_residual = 0;
 382	}
 383	cmd->SCp.have_data_in = 0;
 384
 385/* We don't set SCp.phase here - that's done in in2000_execute() */
 386
 387/* WD docs state that at the conclusion of a "LEVEL2" command, the
 388 * status byte can be retrieved from the LUN register. Apparently,
 389 * this is the case only for *uninterrupted* LEVEL2 commands! If
 390 * there are any unexpected phases entered, even if they are 100%
 391 * legal (different devices may choose to do things differently),
 392 * the LEVEL2 command sequence is exited. This often occurs prior
 393 * to receiving the status byte, in which case the driver does a
 394 * status phase interrupt and gets the status byte on its own.
 395 * While such a command can then be "resumed" (ie restarted to
 396 * finish up as a LEVEL2 command), the LUN register will NOT be
 397 * a valid status byte at the command's conclusion, and we must
 398 * use the byte obtained during the earlier interrupt. Here, we
 399 * preset SCp.Status to an illegal value (0xff) so that when
 400 * this command finally completes, we can tell where the actual
 401 * status byte is stored.
 402 */
 403
 404	cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
 405
 406/* We need to disable interrupts before messing with the input
 407 * queue and calling in2000_execute().
 408 */
 409
 410	/*
 411	 * Add the cmd to the end of 'input_Q'. Note that REQUEST_SENSE
 412	 * commands are added to the head of the queue so that the desired
 413	 * sense data is not lost before REQUEST_SENSE executes.
 414	 */
 415
 416	if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
 417		cmd->host_scribble = (uchar *) hostdata->input_Q;
 418		hostdata->input_Q = cmd;
 419	} else {		/* find the end of the queue */
 420		for (tmp = (Scsi_Cmnd *) hostdata->input_Q; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
 421		tmp->host_scribble = (uchar *) cmd;
 422	}
 423
 424/* We know that there's at least one command in 'input_Q' now.
 425 * Go see if any of them are runnable!
 426 */
 427
 428	in2000_execute(cmd->device->host);
 429
 430	DB(DB_QUEUE_COMMAND, printk(")Q "))
 431	    return 0;
 432}
 433
 434static DEF_SCSI_QCMD(in2000_queuecommand)
 435
 436
 437
 438/*
 439 * This routine attempts to start a scsi command. If the host_card is
 440 * already connected, we give up immediately. Otherwise, look through
 441 * the input_Q, using the first command we find that's intended
 442 * for a currently non-busy target/lun.
 443 * Note that this function is always called with interrupts already
 444 * disabled (either from in2000_queuecommand() or in2000_intr()).
 445 */
 446static void in2000_execute(struct Scsi_Host *instance)
 447{
 448	struct IN2000_hostdata *hostdata;
 449	Scsi_Cmnd *cmd, *prev;
 450	int i;
 451	unsigned short *sp;
 452	unsigned short f;
 453	unsigned short flushbuf[16];
 454
 455
 456	hostdata = (struct IN2000_hostdata *) instance->hostdata;
 457
 458	DB(DB_EXECUTE, printk("EX("))
 459
 460	    if (hostdata->selecting || hostdata->connected) {
 461
 462		DB(DB_EXECUTE, printk(")EX-0 "))
 463
 464		    return;
 465	}
 466
 467	/*
 468	 * Search through the input_Q for a command destined
 469	 * for an idle target/lun.
 470	 */
 471
 472	cmd = (Scsi_Cmnd *) hostdata->input_Q;
 473	prev = NULL;
 474	while (cmd) {
 475		if (!(hostdata->busy[cmd->device->id] & (1 << cmd->device->lun)))
 476			break;
 477		prev = cmd;
 478		cmd = (Scsi_Cmnd *) cmd->host_scribble;
 479	}
 480
 481	/* quit if queue empty or all possible targets are busy */
 482
 483	if (!cmd) {
 484
 485		DB(DB_EXECUTE, printk(")EX-1 "))
 486
 487		    return;
 488	}
 489
 490	/*  remove command from queue */
 491
 492	if (prev)
 493		prev->host_scribble = cmd->host_scribble;
 494	else
 495		hostdata->input_Q = (Scsi_Cmnd *) cmd->host_scribble;
 496
 497#ifdef PROC_STATISTICS
 498	hostdata->cmd_cnt[cmd->device->id]++;
 499#endif
 500
 501/*
 502 * Start the selection process
 503 */
 504
 505	if (is_dir_out(cmd))
 506		write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id);
 507	else
 508		write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
 509
 510/* Now we need to figure out whether or not this command is a good
 511 * candidate for disconnect/reselect. We guess to the best of our
 512 * ability, based on a set of hierarchical rules. When several
 513 * devices are operating simultaneously, disconnects are usually
 514 * an advantage. In a single device system, or if only 1 device
 515 * is being accessed, transfers usually go faster if disconnects
 516 * are not allowed:
 517 *
 518 * + Commands should NEVER disconnect if hostdata->disconnect =
 519 *   DIS_NEVER (this holds for tape drives also), and ALWAYS
 520 *   disconnect if hostdata->disconnect = DIS_ALWAYS.
 521 * + Tape drive commands should always be allowed to disconnect.
 522 * + Disconnect should be allowed if disconnected_Q isn't empty.
 523 * + Commands should NOT disconnect if input_Q is empty.
 524 * + Disconnect should be allowed if there are commands in input_Q
 525 *   for a different target/lun. In this case, the other commands
 526 *   should be made disconnect-able, if not already.
 527 *
 528 * I know, I know - this code would flunk me out of any
 529 * "C Programming 101" class ever offered. But it's easy
 530 * to change around and experiment with for now.
 531 */
 532
 533	cmd->SCp.phase = 0;	/* assume no disconnect */
 534	if (hostdata->disconnect == DIS_NEVER)
 535		goto no;
 536	if (hostdata->disconnect == DIS_ALWAYS)
 537		goto yes;
 538	if (cmd->device->type == 1)	/* tape drive? */
 539		goto yes;
 540	if (hostdata->disconnected_Q)	/* other commands disconnected? */
 541		goto yes;
 542	if (!(hostdata->input_Q))	/* input_Q empty? */
 543		goto no;
 544	for (prev = (Scsi_Cmnd *) hostdata->input_Q; prev; prev = (Scsi_Cmnd *) prev->host_scribble) {
 545		if ((prev->device->id != cmd->device->id) || (prev->device->lun != cmd->device->lun)) {
 546			for (prev = (Scsi_Cmnd *) hostdata->input_Q; prev; prev = (Scsi_Cmnd *) prev->host_scribble)
 547				prev->SCp.phase = 1;
 548			goto yes;
 549		}
 550	}
 551	goto no;
 552
 553      yes:
 554	cmd->SCp.phase = 1;
 555
 556#ifdef PROC_STATISTICS
 557	hostdata->disc_allowed_cnt[cmd->device->id]++;
 558#endif
 559
 560      no:
 561	write_3393(hostdata, WD_SOURCE_ID, ((cmd->SCp.phase) ? SRCID_ER : 0));
 562
 563	write_3393(hostdata, WD_TARGET_LUN, cmd->device->lun);
 564	write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, hostdata->sync_xfer[cmd->device->id]);
 565	hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
 566
 567	if ((hostdata->level2 <= L2_NONE) || (hostdata->sync_stat[cmd->device->id] == SS_UNSET)) {
 568
 569		/*
 570		 * Do a 'Select-With-ATN' command. This will end with
 571		 * one of the following interrupts:
 572		 *    CSR_RESEL_AM:  failure - can try again later.
 573		 *    CSR_TIMEOUT:   failure - give up.
 574		 *    CSR_SELECT:    success - proceed.
 575		 */
 576
 577		hostdata->selecting = cmd;
 578
 579/* Every target has its own synchronous transfer setting, kept in
 580 * the sync_xfer array, and a corresponding status byte in sync_stat[].
 581 * Each target's sync_stat[] entry is initialized to SS_UNSET, and its
 582 * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
 583 * means that the parameters are undetermined as yet, and that we
 584 * need to send an SDTR message to this device after selection is
 585 * complete. We set SS_FIRST to tell the interrupt routine to do so,
 586 * unless we don't want to even _try_ synchronous transfers: In this
 587 * case we set SS_SET to make the defaults final.
 588 */
 589		if (hostdata->sync_stat[cmd->device->id] == SS_UNSET) {
 590			if (hostdata->sync_off & (1 << cmd->device->id))
 591				hostdata->sync_stat[cmd->device->id] = SS_SET;
 592			else
 593				hostdata->sync_stat[cmd->device->id] = SS_FIRST;
 594		}
 595		hostdata->state = S_SELECTING;
 596		write_3393_count(hostdata, 0);	/* this guarantees a DATA_PHASE interrupt */
 597		write_3393_cmd(hostdata, WD_CMD_SEL_ATN);
 598	}
 599
 600	else {
 601
 602		/*
 603		 * Do a 'Select-With-ATN-Xfer' command. This will end with
 604		 * one of the following interrupts:
 605		 *    CSR_RESEL_AM:  failure - can try again later.
 606		 *    CSR_TIMEOUT:   failure - give up.
 607		 *    anything else: success - proceed.
 608		 */
 609
 610		hostdata->connected = cmd;
 611		write_3393(hostdata, WD_COMMAND_PHASE, 0);
 612
 613		/* copy command_descriptor_block into WD chip
 614		 * (take advantage of auto-incrementing)
 615		 */
 616
 617		write1_io(WD_CDB_1, IO_WD_ADDR);
 618		for (i = 0; i < cmd->cmd_len; i++)
 619			write1_io(cmd->cmnd[i], IO_WD_DATA);
 620
 621		/* The wd33c93 only knows about Group 0, 1, and 5 commands when
 622		 * it's doing a 'select-and-transfer'. To be safe, we write the
 623		 * size of the CDB into the OWN_ID register for every case. This
 624		 * way there won't be problems with vendor-unique, audio, etc.
 625		 */
 626
 627		write_3393(hostdata, WD_OWN_ID, cmd->cmd_len);
 628
 629		/* When doing a non-disconnect command, we can save ourselves a DATA
 630		 * phase interrupt later by setting everything up now. With writes we
 631		 * need to pre-fill the fifo; if there's room for the 32 flush bytes,
 632		 * put them in there too - that'll avoid a fifo interrupt. Reads are
 633		 * somewhat simpler.
 634		 * KLUDGE NOTE: It seems that you can't completely fill the fifo here:
 635		 * This results in the IO_FIFO_COUNT register rolling over to zero,
 636		 * and apparently the gate array logic sees this as empty, not full,
 637		 * so the 3393 chip is never signalled to start reading from the
 638		 * fifo. Or maybe it's seen as a permanent fifo interrupt condition.
 639		 * Regardless, we fix this by temporarily pretending that the fifo
 640		 * is 16 bytes smaller. (I see now that the old driver has a comment
 641		 * about "don't fill completely" in an analogous place - must be the
 642		 * same deal.) This results in CDROM, swap partitions, and tape drives
 643		 * needing an extra interrupt per write command - I think we can live
 644		 * with that!
 645		 */
 646
 647		if (!(cmd->SCp.phase)) {
 648			write_3393_count(hostdata, cmd->SCp.this_residual);
 649			write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_BUS);
 650			write1_io(0, IO_FIFO_WRITE);	/* clear fifo counter, write mode */
 651
 652			if (is_dir_out(cmd)) {
 653				hostdata->fifo = FI_FIFO_WRITING;
 654				if ((i = cmd->SCp.this_residual) > (IN2000_FIFO_SIZE - 16))
 655					i = IN2000_FIFO_SIZE - 16;
 656				cmd->SCp.have_data_in = i;	/* this much data in fifo */
 657				i >>= 1;	/* Gulp. Assuming modulo 2. */
 658				sp = (unsigned short *) cmd->SCp.ptr;
 659				f = hostdata->io_base + IO_FIFO;
 660
 661#ifdef FAST_WRITE_IO
 662
 663				FAST_WRITE2_IO();
 664#else
 665				while (i--)
 666					write2_io(*sp++, IO_FIFO);
 667
 668#endif
 669
 670				/* Is there room for the flush bytes? */
 671
 672				if (cmd->SCp.have_data_in <= ((IN2000_FIFO_SIZE - 16) - 32)) {
 673					sp = flushbuf;
 674					i = 16;
 675
 676#ifdef FAST_WRITE_IO
 677
 678					FAST_WRITE2_IO();
 679#else
 680					while (i--)
 681						write2_io(0, IO_FIFO);
 682
 683#endif
 684
 685				}
 686			}
 687
 688			else {
 689				write1_io(0, IO_FIFO_READ);	/* put fifo in read mode */
 690				hostdata->fifo = FI_FIFO_READING;
 691				cmd->SCp.have_data_in = 0;	/* nothing transferred yet */
 692			}
 693
 694		} else {
 695			write_3393_count(hostdata, 0);	/* this guarantees a DATA_PHASE interrupt */
 696		}
 697		hostdata->state = S_RUNNING_LEVEL2;
 698		write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
 699	}
 700
 701	/*
 702	 * Since the SCSI bus can handle only 1 connection at a time,
 703	 * we get out of here now. If the selection fails, or when
 704	 * the command disconnects, we'll come back to this routine
 705	 * to search the input_Q again...
 706	 */
 707
 708	DB(DB_EXECUTE, printk("%s)EX-2 ", (cmd->SCp.phase) ? "d:" : ""))
 709
 710}
 711
 712
 713
 714static void transfer_pio(uchar * buf, int cnt, int data_in_dir, struct IN2000_hostdata *hostdata)
 715{
 716	uchar asr;
 717
 718	DB(DB_TRANSFER, printk("(%p,%d,%s)", buf, cnt, data_in_dir ? "in" : "out"))
 719
 720	    write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
 721	write_3393_count(hostdata, cnt);
 722	write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
 723	if (data_in_dir) {
 724		do {
 725			asr = READ_AUX_STAT();
 726			if (asr & ASR_DBR)
 727				*buf++ = read_3393(hostdata, WD_DATA);
 728		} while (!(asr & ASR_INT));
 729	} else {
 730		do {
 731			asr = READ_AUX_STAT();
 732			if (asr & ASR_DBR)
 733				write_3393(hostdata, WD_DATA, *buf++);
 734		} while (!(asr & ASR_INT));
 735	}
 736
 737	/* Note: we are returning with the interrupt UN-cleared.
 738	 * Since (presumably) an entire I/O operation has
 739	 * completed, the bus phase is probably different, and
 740	 * the interrupt routine will discover this when it
 741	 * responds to the uncleared int.
 742	 */
 743
 744}
 745
 746
 747
 748static void transfer_bytes(Scsi_Cmnd * cmd, int data_in_dir)
 749{
 750	struct IN2000_hostdata *hostdata;
 751	unsigned short *sp;
 752	unsigned short f;
 753	int i;
 754
 755	hostdata = (struct IN2000_hostdata *) cmd->device->host->hostdata;
 756
 757/* Normally, you'd expect 'this_residual' to be non-zero here.
 758 * In a series of scatter-gather transfers, however, this
 759 * routine will usually be called with 'this_residual' equal
 760 * to 0 and 'buffers_residual' non-zero. This means that a
 761 * previous transfer completed, clearing 'this_residual', and
 762 * now we need to setup the next scatter-gather buffer as the
 763 * source or destination for THIS transfer.
 764 */
 765	if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
 766		++cmd->SCp.buffer;
 767		--cmd->SCp.buffers_residual;
 768		cmd->SCp.this_residual = cmd->SCp.buffer->length;
 769		cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
 770	}
 771
 772/* Set up hardware registers */
 773
 774	write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, hostdata->sync_xfer[cmd->device->id]);
 775	write_3393_count(hostdata, cmd->SCp.this_residual);
 776	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_BUS);
 777	write1_io(0, IO_FIFO_WRITE);	/* zero counter, assume write */
 778
 779/* Reading is easy. Just issue the command and return - we'll
 780 * get an interrupt later when we have actual data to worry about.
 781 */
 782
 783	if (data_in_dir) {
 784		write1_io(0, IO_FIFO_READ);
 785		if ((hostdata->level2 >= L2_DATA) || (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
 786			write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
 787			write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
 788			hostdata->state = S_RUNNING_LEVEL2;
 789		} else
 790			write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
 791		hostdata->fifo = FI_FIFO_READING;
 792		cmd->SCp.have_data_in = 0;
 793		return;
 794	}
 795
 796/* Writing is more involved - we'll start the WD chip and write as
 797 * much data to the fifo as we can right now. Later interrupts will
 798 * write any bytes that don't make it at this stage.
 799 */
 800
 801	if ((hostdata->level2 >= L2_DATA) || (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
 802		write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
 803		write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
 804		hostdata->state = S_RUNNING_LEVEL2;
 805	} else
 806		write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
 807	hostdata->fifo = FI_FIFO_WRITING;
 808	sp = (unsigned short *) cmd->SCp.ptr;
 809
 810	if ((i = cmd->SCp.this_residual) > IN2000_FIFO_SIZE)
 811		i = IN2000_FIFO_SIZE;
 812	cmd->SCp.have_data_in = i;
 813	i >>= 1;		/* Gulp. We assume this_residual is modulo 2 */
 814	f = hostdata->io_base + IO_FIFO;
 815
 816#ifdef FAST_WRITE_IO
 817
 818	FAST_WRITE2_IO();
 819#else
 820	while (i--)
 821		write2_io(*sp++, IO_FIFO);
 822
 823#endif
 824
 825}
 826
 827
 828/* We need to use spin_lock_irqsave() & spin_unlock_irqrestore() in this
 829 * function in order to work in an SMP environment. (I'd be surprised
 830 * if the driver is ever used by anyone on a real multi-CPU motherboard,
 831 * but it _does_ need to be able to compile and run in an SMP kernel.)
 832 */
 833
 834static irqreturn_t in2000_intr(int irqnum, void *dev_id)
 835{
 836	struct Scsi_Host *instance = dev_id;
 837	struct IN2000_hostdata *hostdata;
 838	Scsi_Cmnd *patch, *cmd;
 839	uchar asr, sr, phs, id, lun, *ucp, msg;
 840	int i, j;
 841	unsigned long length;
 842	unsigned short *sp;
 843	unsigned short f;
 844	unsigned long flags;
 845
 846	hostdata = (struct IN2000_hostdata *) instance->hostdata;
 847
 848/* Get the spin_lock and disable further ints, for SMP */
 849
 850	spin_lock_irqsave(instance->host_lock, flags);
 851
 852#ifdef PROC_STATISTICS
 853	hostdata->int_cnt++;
 854#endif
 855
 856/* The IN2000 card has 2 interrupt sources OR'ed onto its IRQ line - the
 857 * WD3393 chip and the 2k fifo (which is actually a dual-port RAM combined
 858 * with a big logic array, so it's a little different than what you might
 859 * expect). As far as I know, there's no reason that BOTH can't be active
 860 * at the same time, but there's a problem: while we can read the 3393
 861 * to tell if _it_ wants an interrupt, I don't know of a way to ask the
 862 * fifo the same question. The best we can do is check the 3393 and if
 863 * it _isn't_ the source of the interrupt, then we can be pretty sure
 864 * that the fifo is the culprit.
 865 *  UPDATE: I have it on good authority (Bill Earnest) that bit 0 of the
 866 *          IO_FIFO_COUNT register mirrors the fifo interrupt state. I
 867 *          assume that bit clear means interrupt active. As it turns
 868 *          out, the driver really doesn't need to check for this after
 869 *          all, so my remarks above about a 'problem' can safely be
 870 *          ignored. The way the logic is set up, there's no advantage
 871 *          (that I can see) to worrying about it.
 872 *
 873 * It seems that the fifo interrupt signal is negated when we extract
 874 * bytes during read or write bytes during write.
 875 *  - fifo will interrupt when data is moving from it to the 3393, and
 876 *    there are 31 (or less?) bytes left to go. This is sort of short-
 877 *    sighted: what if you don't WANT to do more? In any case, our
 878 *    response is to push more into the fifo - either actual data or
 879 *    dummy bytes if need be. Note that we apparently have to write at
 880 *    least 32 additional bytes to the fifo after an interrupt in order
 881 *    to get it to release the ones it was holding on to - writing fewer
 882 *    than 32 will result in another fifo int.
 883 *  UPDATE: Again, info from Bill Earnest makes this more understandable:
 884 *          32 bytes = two counts of the fifo counter register. He tells
 885 *          me that the fifo interrupt is a non-latching signal derived
 886 *          from a straightforward boolean interpretation of the 7
 887 *          highest bits of the fifo counter and the fifo-read/fifo-write
 888 *          state. Who'd a thought?
 889 */
 890
 891	write1_io(0, IO_LED_ON);
 892	asr = READ_AUX_STAT();
 893	if (!(asr & ASR_INT)) {	/* no WD33c93 interrupt? */
 894
 895/* Ok. This is definitely a FIFO-only interrupt.
 896 *
 897 * If FI_FIFO_READING is set, there are up to 2048 bytes waiting to be read,
 898 * maybe more to come from the SCSI bus. Read as many as we can out of the
 899 * fifo and into memory at the location of SCp.ptr[SCp.have_data_in], and
 900 * update have_data_in afterwards.
 901 *
 902 * If we have FI_FIFO_WRITING, the FIFO has almost run out of bytes to move
 903 * into the WD3393 chip (I think the interrupt happens when there are 31
 904 * bytes left, but it may be fewer...). The 3393 is still waiting, so we
 905 * shove some more into the fifo, which gets things moving again. If the
 906 * original SCSI command specified more than 2048 bytes, there may still
 907 * be some of that data left: fine - use it (from SCp.ptr[SCp.have_data_in]).
 908 * Don't forget to update have_data_in. If we've already written out the
 909 * entire buffer, feed 32 dummy bytes to the fifo - they're needed to
 910 * push out the remaining real data.
 911 *    (Big thanks to Bill Earnest for getting me out of the mud in here.)
 912 */
 913
 914		cmd = (Scsi_Cmnd *) hostdata->connected;	/* assume we're connected */
 915		CHECK_NULL(cmd, "fifo_int")
 916
 917		    if (hostdata->fifo == FI_FIFO_READING) {
 918
 919			DB(DB_FIFO, printk("{R:%02x} ", read1_io(IO_FIFO_COUNT)))
 920
 921			    sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 922			i = read1_io(IO_FIFO_COUNT) & 0xfe;
 923			i <<= 2;	/* # of words waiting in the fifo */
 924			f = hostdata->io_base + IO_FIFO;
 925
 926#ifdef FAST_READ_IO
 927
 928			FAST_READ2_IO();
 929#else
 930			while (i--)
 931				*sp++ = read2_io(IO_FIFO);
 932
 933#endif
 934
 935			i = sp - (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 936			i <<= 1;
 937			cmd->SCp.have_data_in += i;
 938		}
 939
 940		else if (hostdata->fifo == FI_FIFO_WRITING) {
 941
 942			DB(DB_FIFO, printk("{W:%02x} ", read1_io(IO_FIFO_COUNT)))
 943
 944/* If all bytes have been written to the fifo, flush out the stragglers.
 945 * Note that while writing 16 dummy words seems arbitrary, we don't
 946 * have another choice that I can see. What we really want is to read
 947 * the 3393 transfer count register (that would tell us how many bytes
 948 * needed flushing), but the TRANSFER_INFO command hasn't completed
 949 * yet (not enough bytes!) and that register won't be accessible. So,
 950 * we use 16 words - a number obtained through trial and error.
 951 *  UPDATE: Bill says this is exactly what Always does, so there.
 952 *          More thanks due him for help in this section.
 953 */
 954			    if (cmd->SCp.this_residual == cmd->SCp.have_data_in) {
 955				i = 16;
 956				while (i--)	/* write 32 dummy bytes */
 957					write2_io(0, IO_FIFO);
 958			}
 959
 960/* If there are still bytes left in the SCSI buffer, write as many as we
 961 * can out to the fifo.
 962 */
 963
 964			else {
 965				sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 966				i = cmd->SCp.this_residual - cmd->SCp.have_data_in;	/* bytes yet to go */
 967				j = read1_io(IO_FIFO_COUNT) & 0xfe;
 968				j <<= 2;	/* how many words the fifo has room for */
 969				if ((j << 1) > i)
 970					j = (i >> 1);
 971				while (j--)
 972					write2_io(*sp++, IO_FIFO);
 973
 974				i = sp - (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 975				i <<= 1;
 976				cmd->SCp.have_data_in += i;
 977			}
 978		}
 979
 980		else {
 981			printk("*** Spurious FIFO interrupt ***");
 982		}
 983
 984		write1_io(0, IO_LED_OFF);
 985
 986/* release the SMP spin_lock and restore irq state */
 987		spin_unlock_irqrestore(instance->host_lock, flags);
 988		return IRQ_HANDLED;
 989	}
 990
 991/* This interrupt was triggered by the WD33c93 chip. The fifo interrupt
 992 * may also be asserted, but we don't bother to check it: we get more
 993 * detailed info from FIFO_READING and FIFO_WRITING (see below).
 994 */
 995
 996	cmd = (Scsi_Cmnd *) hostdata->connected;	/* assume we're connected */
 997	sr = read_3393(hostdata, WD_SCSI_STATUS);	/* clear the interrupt */
 998	phs = read_3393(hostdata, WD_COMMAND_PHASE);
 999
1000	if (!cmd && (sr != CSR_RESEL_AM && sr != CSR_TIMEOUT && sr != CSR_SELECT)) {
1001		printk("\nNR:wd-intr-1\n");
1002		write1_io(0, IO_LED_OFF);
1003
1004/* release the SMP spin_lock and restore irq state */
1005		spin_unlock_irqrestore(instance->host_lock, flags);
1006		return IRQ_HANDLED;
1007	}
1008
1009	DB(DB_INTR, printk("{%02x:%02x-", asr, sr))
1010
1011/* After starting a FIFO-based transfer, the next _WD3393_ interrupt is
1012 * guaranteed to be in response to the completion of the transfer.
1013 * If we were reading, there's probably data in the fifo that needs
1014 * to be copied into RAM - do that here. Also, we have to update
1015 * 'this_residual' and 'ptr' based on the contents of the
1016 * TRANSFER_COUNT register, in case the device decided to do an
1017 * intermediate disconnect (a device may do this if it has to
1018 * do a seek,  or just to be nice and let other devices have
1019 * some bus time during long transfers).
1020 * After doing whatever is necessary with the fifo, we go on and
1021 * service the WD3393 interrupt normally.
1022 */
1023	    if (hostdata->fifo == FI_FIFO_READING) {
1024
1025/* buffer index = start-of-buffer + #-of-bytes-already-read */
1026
1027		sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
1028
1029/* bytes remaining in fifo = (total-wanted - #-not-got) - #-already-read */
1030
1031		i = (cmd->SCp.this_residual - read_3393_count(hostdata)) - cmd->SCp.have_data_in;
1032		i >>= 1;	/* Gulp. We assume this will always be modulo 2 */
1033		f = hostdata->io_base + IO_FIFO;
1034
1035#ifdef FAST_READ_IO
1036
1037		FAST_READ2_IO();
1038#else
1039		while (i--)
1040			*sp++ = read2_io(IO_FIFO);
1041
1042#endif
1043
1044		hostdata->fifo = FI_FIFO_UNUSED;
1045		length = cmd->SCp.this_residual;
1046		cmd->SCp.this_residual = read_3393_count(hostdata);
1047		cmd->SCp.ptr += (length - cmd->SCp.this_residual);
1048
1049		DB(DB_TRANSFER, printk("(%p,%d)", cmd->SCp.ptr, cmd->SCp.this_residual))
1050
1051	}
1052
1053	else if (hostdata->fifo == FI_FIFO_WRITING) {
1054		hostdata->fifo = FI_FIFO_UNUSED;
1055		length = cmd->SCp.this_residual;
1056		cmd->SCp.this_residual = read_3393_count(hostdata);
1057		cmd->SCp.ptr += (length - cmd->SCp.this_residual);
1058
1059		DB(DB_TRANSFER, printk("(%p,%d)", cmd->SCp.ptr, cmd->SCp.this_residual))
1060
1061	}
1062
1063/* Respond to the specific WD3393 interrupt - there are quite a few! */
1064
1065	switch (sr) {
1066
1067	case CSR_TIMEOUT:
1068		DB(DB_INTR, printk("TIMEOUT"))
1069
1070		    if (hostdata->state == S_RUNNING_LEVEL2)
1071			hostdata->connected = NULL;
1072		else {
1073			cmd = (Scsi_Cmnd *) hostdata->selecting;	/* get a valid cmd */
1074			CHECK_NULL(cmd, "csr_timeout")
1075			    hostdata->selecting = NULL;
1076		}
1077
1078		cmd->result = DID_NO_CONNECT << 16;
1079		hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1080		hostdata->state = S_UNCONNECTED;
1081		cmd->scsi_done(cmd);
1082
1083/* We are not connected to a target - check to see if there
1084 * are commands waiting to be executed.
1085 */
1086
1087		in2000_execute(instance);
1088		break;
1089
1090
1091/* Note: this interrupt should not occur in a LEVEL2 command */
1092
1093	case CSR_SELECT:
1094		DB(DB_INTR, printk("SELECT"))
1095		    hostdata->connected = cmd = (Scsi_Cmnd *) hostdata->selecting;
1096		CHECK_NULL(cmd, "csr_select")
1097		    hostdata->selecting = NULL;
1098
1099		/* construct an IDENTIFY message with correct disconnect bit */
1100
1101		hostdata->outgoing_msg[0] = (0x80 | 0x00 | cmd->device->lun);
1102		if (cmd->SCp.phase)
1103			hostdata->outgoing_msg[0] |= 0x40;
1104
1105		if (hostdata->sync_stat[cmd->device->id] == SS_FIRST) {
1106#ifdef SYNC_DEBUG
1107			printk(" sending SDTR ");
1108#endif
1109
1110			hostdata->sync_stat[cmd->device->id] = SS_WAITING;
1111
1112			/* tack on a 2nd message to ask about synchronous transfers */
1113
1114			hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
1115			hostdata->outgoing_msg[2] = 3;
1116			hostdata->outgoing_msg[3] = EXTENDED_SDTR;
1117			hostdata->outgoing_msg[4] = OPTIMUM_SX_PER / 4;
1118			hostdata->outgoing_msg[5] = OPTIMUM_SX_OFF;
1119			hostdata->outgoing_len = 6;
1120		} else
1121			hostdata->outgoing_len = 1;
1122
1123		hostdata->state = S_CONNECTED;
1124		break;
1125
1126
1127	case CSR_XFER_DONE | PHS_DATA_IN:
1128	case CSR_UNEXP | PHS_DATA_IN:
1129	case CSR_SRV_REQ | PHS_DATA_IN:
1130		DB(DB_INTR, printk("IN-%d.%d", cmd->SCp.this_residual, cmd->SCp.buffers_residual))
1131		    transfer_bytes(cmd, DATA_IN_DIR);
1132		if (hostdata->state != S_RUNNING_LEVEL2)
1133			hostdata->state = S_CONNECTED;
1134		break;
1135
1136
1137	case CSR_XFER_DONE | PHS_DATA_OUT:
1138	case CSR_UNEXP | PHS_DATA_OUT:
1139	case CSR_SRV_REQ | PHS_DATA_OUT:
1140		DB(DB_INTR, printk("OUT-%d.%d", cmd->SCp.this_residual, cmd->SCp.buffers_residual))
1141		    transfer_bytes(cmd, DATA_OUT_DIR);
1142		if (hostdata->state != S_RUNNING_LEVEL2)
1143			hostdata->state = S_CONNECTED;
1144		break;
1145
1146
1147/* Note: this interrupt should not occur in a LEVEL2 command */
1148
1149	case CSR_XFER_DONE | PHS_COMMAND:
1150	case CSR_UNEXP | PHS_COMMAND:
1151	case CSR_SRV_REQ | PHS_COMMAND:
1152		DB(DB_INTR, printk("CMND-%02x", cmd->cmnd[0]))
1153		    transfer_pio(cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR, hostdata);
1154		hostdata->state = S_CONNECTED;
1155		break;
1156
1157
1158	case CSR_XFER_DONE | PHS_STATUS:
1159	case CSR_UNEXP | PHS_STATUS:
1160	case CSR_SRV_REQ | PHS_STATUS:
1161		DB(DB_INTR, printk("STATUS="))
1162
1163		    cmd->SCp.Status = read_1_byte(hostdata);
1164		DB(DB_INTR, printk("%02x", cmd->SCp.Status))
1165		    if (hostdata->level2 >= L2_BASIC) {
1166			sr = read_3393(hostdata, WD_SCSI_STATUS);	/* clear interrupt */
1167			hostdata->state = S_RUNNING_LEVEL2;
1168			write_3393(hostdata, WD_COMMAND_PHASE, 0x50);
1169			write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1170		} else {
1171			hostdata->state = S_CONNECTED;
1172		}
1173		break;
1174
1175
1176	case CSR_XFER_DONE | PHS_MESS_IN:
1177	case CSR_UNEXP | PHS_MESS_IN:
1178	case CSR_SRV_REQ | PHS_MESS_IN:
1179		DB(DB_INTR, printk("MSG_IN="))
1180
1181		    msg = read_1_byte(hostdata);
1182		sr = read_3393(hostdata, WD_SCSI_STATUS);	/* clear interrupt */
1183
1184		hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
1185		if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
1186			msg = EXTENDED_MESSAGE;
1187		else
1188			hostdata->incoming_ptr = 0;
1189
1190		cmd->SCp.Message = msg;
1191		switch (msg) {
1192
1193		case COMMAND_COMPLETE:
1194			DB(DB_INTR, printk("CCMP"))
1195			    write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1196			hostdata->state = S_PRE_CMP_DISC;
1197			break;
1198
1199		case SAVE_POINTERS:
1200			DB(DB_INTR, printk("SDP"))
1201			    write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1202			hostdata->state = S_CONNECTED;
1203			break;
1204
1205		case RESTORE_POINTERS:
1206			DB(DB_INTR, printk("RDP"))
1207			    if (hostdata->level2 >= L2_BASIC) {
1208				write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
1209				write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1210				hostdata->state = S_RUNNING_LEVEL2;
1211			} else {
1212				write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1213				hostdata->state = S_CONNECTED;
1214			}
1215			break;
1216
1217		case DISCONNECT:
1218			DB(DB_INTR, printk("DIS"))
1219			    cmd->device->disconnect = 1;
1220			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1221			hostdata->state = S_PRE_TMP_DISC;
1222			break;
1223
1224		case MESSAGE_REJECT:
1225			DB(DB_INTR, printk("REJ"))
1226#ifdef SYNC_DEBUG
1227			    printk("-REJ-");
1228#endif
1229			if (hostdata->sync_stat[cmd->device->id] == SS_WAITING)
1230				hostdata->sync_stat[cmd->device->id] = SS_SET;
1231			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1232			hostdata->state = S_CONNECTED;
1233			break;
1234
1235		case EXTENDED_MESSAGE:
1236			DB(DB_INTR, printk("EXT"))
1237
1238			    ucp = hostdata->incoming_msg;
1239
1240#ifdef SYNC_DEBUG
1241			printk("%02x", ucp[hostdata->incoming_ptr]);
1242#endif
1243			/* Is this the last byte of the extended message? */
1244
1245			if ((hostdata->incoming_ptr >= 2) && (hostdata->incoming_ptr == (ucp[1] + 1))) {
1246
1247				switch (ucp[2]) {	/* what's the EXTENDED code? */
1248				case EXTENDED_SDTR:
1249					id = calc_sync_xfer(ucp[3], ucp[4]);
1250					if (hostdata->sync_stat[cmd->device->id] != SS_WAITING) {
1251
1252/* A device has sent an unsolicited SDTR message; rather than go
1253 * through the effort of decoding it and then figuring out what
1254 * our reply should be, we're just gonna say that we have a
1255 * synchronous fifo depth of 0. This will result in asynchronous
1256 * transfers - not ideal but so much easier.
1257 * Actually, this is OK because it assures us that if we don't
1258 * specifically ask for sync transfers, we won't do any.
1259 */
1260
1261						write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1262						hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
1263						hostdata->outgoing_msg[1] = 3;
1264						hostdata->outgoing_msg[2] = EXTENDED_SDTR;
1265						hostdata->outgoing_msg[3] = hostdata->default_sx_per / 4;
1266						hostdata->outgoing_msg[4] = 0;
1267						hostdata->outgoing_len = 5;
1268						hostdata->sync_xfer[cmd->device->id] = calc_sync_xfer(hostdata->default_sx_per / 4, 0);
1269					} else {
1270						hostdata->sync_xfer[cmd->device->id] = id;
1271					}
1272#ifdef SYNC_DEBUG
1273					printk("sync_xfer=%02x", hostdata->sync_xfer[cmd->device->id]);
1274#endif
1275					hostdata->sync_stat[cmd->device->id] = SS_SET;
1276					write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1277					hostdata->state = S_CONNECTED;
1278					break;
1279				case EXTENDED_WDTR:
1280					write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1281					printk("sending WDTR ");
1282					hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
1283					hostdata->outgoing_msg[1] = 2;
1284					hostdata->outgoing_msg[2] = EXTENDED_WDTR;
1285					hostdata->outgoing_msg[3] = 0;	/* 8 bit transfer width */
1286					hostdata->outgoing_len = 4;
1287					write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1288					hostdata->state = S_CONNECTED;
1289					break;
1290				default:
1291					write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1292					printk("Rejecting Unknown Extended Message(%02x). ", ucp[2]);
1293					hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1294					hostdata->outgoing_len = 1;
1295					write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1296					hostdata->state = S_CONNECTED;
1297					break;
1298				}
1299				hostdata->incoming_ptr = 0;
1300			}
1301
1302			/* We need to read more MESS_IN bytes for the extended message */
1303
1304			else {
1305				hostdata->incoming_ptr++;
1306				write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1307				hostdata->state = S_CONNECTED;
1308			}
1309			break;
1310
1311		default:
1312			printk("Rejecting Unknown Message(%02x) ", msg);
1313			write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1314			hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1315			hostdata->outgoing_len = 1;
1316			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1317			hostdata->state = S_CONNECTED;
1318		}
1319		break;
1320
1321
1322/* Note: this interrupt will occur only after a LEVEL2 command */
1323
1324	case CSR_SEL_XFER_DONE:
1325
1326/* Make sure that reselection is enabled at this point - it may
1327 * have been turned off for the command that just completed.
1328 */
1329
1330		write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
1331		if (phs == 0x60) {
1332			DB(DB_INTR, printk("SX-DONE"))
1333			    cmd->SCp.Message = COMMAND_COMPLETE;
1334			lun = read_3393(hostdata, WD_TARGET_LUN);
1335			DB(DB_INTR, printk(":%d.%d", cmd->SCp.Status, lun))
1336			    hostdata->connected = NULL;
1337			hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1338			hostdata->state = S_UNCONNECTED;
1339			if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
1340				cmd->SCp.Status = lun;
1341			if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1342				cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1343			else
1344				cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1345			cmd->scsi_done(cmd);
1346
1347/* We are no longer connected to a target - check to see if
1348 * there are commands waiting to be executed.
1349 */
1350
1351			in2000_execute(instance);
1352		} else {
1353			printk("%02x:%02x:%02x: Unknown SEL_XFER_DONE phase!!---", asr, sr, phs);
1354		}
1355		break;
1356
1357
1358/* Note: this interrupt will occur only after a LEVEL2 command */
1359
1360	case CSR_SDP:
1361		DB(DB_INTR, printk("SDP"))
1362		    hostdata->state = S_RUNNING_LEVEL2;
1363		write_3393(hostdata, WD_COMMAND_PHASE, 0x41);
1364		write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1365		break;
1366
1367
1368	case CSR_XFER_DONE | PHS_MESS_OUT:
1369	case CSR_UNEXP | PHS_MESS_OUT:
1370	case CSR_SRV_REQ | PHS_MESS_OUT:
1371		DB(DB_INTR, printk("MSG_OUT="))
1372
1373/* To get here, we've probably requested MESSAGE_OUT and have
1374 * already put the correct bytes in outgoing_msg[] and filled
1375 * in outgoing_len. We simply send them out to the SCSI bus.
1376 * Sometimes we get MESSAGE_OUT phase when we're not expecting
1377 * it - like when our SDTR message is rejected by a target. Some
1378 * targets send the REJECT before receiving all of the extended
1379 * message, and then seem to go back to MESSAGE_OUT for a byte
1380 * or two. Not sure why, or if I'm doing something wrong to
1381 * cause this to happen. Regardless, it seems that sending
1382 * NOP messages in these situations results in no harm and
1383 * makes everyone happy.
1384 */
1385		    if (hostdata->outgoing_len == 0) {
1386			hostdata->outgoing_len = 1;
1387			hostdata->outgoing_msg[0] = NOP;
1388		}
1389		transfer_pio(hostdata->outgoing_msg, hostdata->outgoing_len, DATA_OUT_DIR, hostdata);
1390		DB(DB_INTR, printk("%02x", hostdata->outgoing_msg[0]))
1391		    hostdata->outgoing_len = 0;
1392		hostdata->state = S_CONNECTED;
1393		break;
1394
1395
1396	case CSR_UNEXP_DISC:
1397
1398/* I think I've seen this after a request-sense that was in response
1399 * to an error condition, but not sure. We certainly need to do
1400 * something when we get this interrupt - the question is 'what?'.
1401 * Let's think positively, and assume some command has finished
1402 * in a legal manner (like a command that provokes a request-sense),
1403 * so we treat it as a normal command-complete-disconnect.
1404 */
1405
1406
1407/* Make sure that reselection is enabled at this point - it may
1408 * have been turned off for the command that just completed.
1409 */
1410
1411		write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
1412		if (cmd == NULL) {
1413			printk(" - Already disconnected! ");
1414			hostdata->state = S_UNCONNECTED;
1415
1416/* release the SMP spin_lock and restore irq state */
1417			spin_unlock_irqrestore(instance->host_lock, flags);
1418			return IRQ_HANDLED;
1419		}
1420		DB(DB_INTR, printk("UNEXP_DISC"))
1421		    hostdata->connected = NULL;
1422		hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1423		hostdata->state = S_UNCONNECTED;
1424		if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1425			cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1426		else
1427			cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1428		cmd->scsi_done(cmd);
1429
1430/* We are no longer connected to a target - check to see if
1431 * there are commands waiting to be executed.
1432 */
1433
1434		in2000_execute(instance);
1435		break;
1436
1437
1438	case CSR_DISC:
1439
1440/* Make sure that reselection is enabled at this point - it may
1441 * have been turned off for the command that just completed.
1442 */
1443
1444		write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
1445		DB(DB_INTR, printk("DISC"))
1446		    if (cmd == NULL) {
1447			printk(" - Already disconnected! ");
1448			hostdata->state = S_UNCONNECTED;
1449		}
1450		switch (hostdata->state) {
1451		case S_PRE_CMP_DISC:
1452			hostdata->connected = NULL;
1453			hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1454			hostdata->state = S_UNCONNECTED;
1455			DB(DB_INTR, printk(":%d", cmd->SCp.Status))
1456			    if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1457				cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1458			else
1459				cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1460			cmd->scsi_done(cmd);
1461			break;
1462		case S_PRE_TMP_DISC:
1463		case S_RUNNING_LEVEL2:
1464			cmd->host_scribble = (uchar *) hostdata->disconnected_Q;
1465			hostdata->disconnected_Q = cmd;
1466			hostdata->connected = NULL;
1467			hostdata->state = S_UNCONNECTED;
1468
1469#ifdef PROC_STATISTICS
1470			hostdata->disc_done_cnt[cmd->device->id]++;
1471#endif
1472
1473			break;
1474		default:
1475			printk("*** Unexpected DISCONNECT interrupt! ***");
1476			hostdata->state = S_UNCONNECTED;
1477		}
1478
1479/* We are no longer connected to a target - check to see if
1480 * there are commands waiting to be executed.
1481 */
1482
1483		in2000_execute(instance);
1484		break;
1485
1486
1487	case CSR_RESEL_AM:
1488		DB(DB_INTR, printk("RESEL"))
1489
1490		    /* First we have to make sure this reselection didn't */
1491		    /* happen during Arbitration/Selection of some other device. */
1492		    /* If yes, put losing command back on top of input_Q. */
1493		    if (hostdata->level2 <= L2_NONE) {
1494
1495			if (hostdata->selecting) {
1496				cmd = (Scsi_Cmnd *) hostdata->selecting;
1497				hostdata->selecting = NULL;
1498				hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1499				cmd->host_scribble = (uchar *) hostdata->input_Q;
1500				hostdata->input_Q = cmd;
1501			}
1502		}
1503
1504		else {
1505
1506			if (cmd) {
1507				if (phs == 0x00) {
1508					hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1509					cmd->host_scribble = (uchar *) hostdata->input_Q;
1510					hostdata->input_Q = cmd;
1511				} else {
1512					printk("---%02x:%02x:%02x-TROUBLE: Intrusive ReSelect!---", asr, sr, phs);
1513					while (1)
1514						printk("\r");
1515				}
1516			}
1517
1518		}
1519
1520		/* OK - find out which device reselected us. */
1521
1522		id = read_3393(hostdata, WD_SOURCE_ID);
1523		id &= SRCID_MASK;
1524
1525		/* and extract the lun from the ID message. (Note that we don't
1526		 * bother to check for a valid message here - I guess this is
1527		 * not the right way to go, but....)
1528		 */
1529
1530		lun = read_3393(hostdata, WD_DATA);
1531		if (hostdata->level2 < L2_RESELECT)
1532			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1533		lun &= 7;
1534
1535		/* Now we look for the command that's reconnecting. */
1536
1537		cmd = (Scsi_Cmnd *) hostdata->disconnected_Q;
1538		patch = NULL;
1539		while (cmd) {
1540			if (id == cmd->device->id && lun == cmd->device->lun)
1541				break;
1542			patch = cmd;
1543			cmd = (Scsi_Cmnd *) cmd->host_scribble;
1544		}
1545
1546		/* Hmm. Couldn't find a valid command.... What to do? */
1547
1548		if (!cmd) {
1549			printk("---TROUBLE: target %d.%d not in disconnect queue---", id, lun);
1550			break;
1551		}
1552
1553		/* Ok, found the command - now start it up again. */
1554
1555		if (patch)
1556			patch->host_scribble = cmd->host_scribble;
1557		else
1558			hostdata->disconnected_Q = (Scsi_Cmnd *) cmd->host_scribble;
1559		hostdata->connected = cmd;
1560
1561		/* We don't need to worry about 'initialize_SCp()' or 'hostdata->busy[]'
1562		 * because these things are preserved over a disconnect.
1563		 * But we DO need to fix the DPD bit so it's correct for this command.
1564		 */
1565
1566		if (is_dir_out(cmd))
1567			write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id);
1568		else
1569			write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
1570		if (hostdata->level2 >= L2_RESELECT) {
1571			write_3393_count(hostdata, 0);	/* we want a DATA_PHASE interrupt */
1572			write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
1573			write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1574			hostdata->state = S_RUNNING_LEVEL2;
1575		} else
1576			hostdata->state = S_CONNECTED;
1577
1578		    break;
1579
1580	default:
1581		printk("--UNKNOWN INTERRUPT:%02x:%02x:%02x--", asr, sr, phs);
1582	}
1583
1584	write1_io(0, IO_LED_OFF);
1585
1586	DB(DB_INTR, printk("} "))
1587
1588/* release the SMP spin_lock and restore irq state */
1589	    spin_unlock_irqrestore(instance->host_lock, flags);
1590	return IRQ_HANDLED;
1591}
1592
1593
1594
1595#define RESET_CARD         0
1596#define RESET_CARD_AND_BUS 1
1597#define B_FLAG 0x80
1598
1599/*
1600 *	Caller must hold instance lock!
1601 */
1602
1603static int reset_hardware(struct Scsi_Host *instance, int type)
1604{
1605	struct IN2000_hostdata *hostdata;
1606	int qt, x;
1607
1608	hostdata = (struct IN2000_hostdata *) instance->hostdata;
1609
1610	write1_io(0, IO_LED_ON);
1611	if (type == RESET_CARD_AND_BUS) {
1612		write1_io(0, IO_CARD_RESET);
1613		x = read1_io(IO_HARDWARE);
1614	}
1615	x = read_3393(hostdata, WD_SCSI_STATUS);	/* clear any WD intrpt */
1616	write_3393(hostdata, WD_OWN_ID, instance->this_id | OWNID_EAF | OWNID_RAF | OWNID_FS_8);
1617	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1618	write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, calc_sync_xfer(hostdata->default_sx_per / 4, DEFAULT_SX_OFF));
1619
1620	write1_io(0, IO_FIFO_WRITE);	/* clear fifo counter */
1621	write1_io(0, IO_FIFO_READ);	/* start fifo out in read mode */
1622	write_3393(hostdata, WD_COMMAND, WD_CMD_RESET);
1623	/* FIXME: timeout ?? */
1624	while (!(READ_AUX_STAT() & ASR_INT))
1625		cpu_relax();	/* wait for RESET to complete */
1626
1627	x = read_3393(hostdata, WD_SCSI_STATUS);	/* clear interrupt */
1628
1629	write_3393(hostdata, WD_QUEUE_TAG, 0xa5);	/* any random number */
1630	qt = read_3393(hostdata, WD_QUEUE_TAG);
1631	if (qt == 0xa5) {
1632		x |= B_FLAG;
1633		write_3393(hostdata, WD_QUEUE_TAG, 0);
1634	}
1635	write_3393(hostdata, WD_TIMEOUT_PERIOD, TIMEOUT_PERIOD_VALUE);
1636	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1637	write1_io(0, IO_LED_OFF);
1638	return x;
1639}
1640
1641
1642
1643static int in2000_bus_reset(Scsi_Cmnd * cmd)
1644{
1645	struct Scsi_Host *instance;
1646	struct IN2000_hostdata *hostdata;
1647	int x;
1648	unsigned long flags;
1649
1650	instance = cmd->device->host;
1651	hostdata = (struct IN2000_hostdata *) instance->hostdata;
1652
1653	printk(KERN_WARNING "scsi%d: Reset. ", instance->host_no);
1654
1655	spin_lock_irqsave(instance->host_lock, flags);
1656
1657	/* do scsi-reset here */
1658	reset_hardware(instance, RESET_CARD_AND_BUS);
1659	for (x = 0; x < 8; x++) {
1660		hostdata->busy[x] = 0;
1661		hostdata->sync_xfer[x] = calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
1662		hostdata->sync_stat[x] = SS_UNSET;	/* using default sync values */
1663	}
1664	hostdata->input_Q = NULL;
1665	hostdata->selecting = NULL;
1666	hostdata->connected = NULL;
1667	hostdata->disconnected_Q = NULL;
1668	hostdata->state = S_UNCONNECTED;
1669	hostdata->fifo = FI_FIFO_UNUSED;
1670	hostdata->incoming_ptr = 0;
1671	hostdata->outgoing_len = 0;
1672
1673	cmd->result = DID_RESET << 16;
1674
1675	spin_unlock_irqrestore(instance->host_lock, flags);
1676	return SUCCESS;
1677}
1678
1679static int __in2000_abort(Scsi_Cmnd * cmd)
1680{
1681	struct Scsi_Host *instance;
1682	struct IN2000_hostdata *hostdata;
1683	Scsi_Cmnd *tmp, *prev;
1684	uchar sr, asr;
1685	unsigned long timeout;
1686
1687	instance = cmd->device->host;
1688	hostdata = (struct IN2000_hostdata *) instance->hostdata;
1689
1690	printk(KERN_DEBUG "scsi%d: Abort-", instance->host_no);
1691	printk("(asr=%02x,count=%ld,resid=%d,buf_resid=%d,have_data=%d,FC=%02x)- ", READ_AUX_STAT(), read_3393_count(hostdata), cmd->SCp.this_residual, cmd->SCp.buffers_residual, cmd->SCp.have_data_in, read1_io(IO_FIFO_COUNT));
1692
1693/*
1694 * Case 1 : If the command hasn't been issued yet, we simply remove it
1695 *     from the inout_Q.
1696 */
1697
1698	tmp = (Scsi_Cmnd *) hostdata->input_Q;
1699	prev = NULL;
1700	while (tmp) {
1701		if (tmp == cmd) {
1702			if (prev)
1703				prev->host_scribble = cmd->host_scribble;
1704			cmd->host_scribble = NULL;
1705			cmd->result = DID_ABORT << 16;
1706			printk(KERN_WARNING "scsi%d: Abort - removing command from input_Q. ", instance->host_no);
1707			cmd->scsi_done(cmd);
1708			return SUCCESS;
1709		}
1710		prev = tmp;
1711		tmp = (Scsi_Cmnd *) tmp->host_scribble;
1712	}
1713
1714/*
1715 * Case 2 : If the command is connected, we're going to fail the abort
1716 *     and let the high level SCSI driver retry at a later time or
1717 *     issue a reset.
1718 *
1719 *     Timeouts, and therefore aborted commands, will be highly unlikely
1720 *     and handling them cleanly in this situation would make the common
1721 *     case of noresets less efficient, and would pollute our code.  So,
1722 *     we fail.
1723 */
1724
1725	if (hostdata->connected == cmd) {
1726
1727		printk(KERN_WARNING "scsi%d: Aborting connected command - ", instance->host_no);
1728
1729		printk("sending wd33c93 ABORT command - ");
1730		write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1731		write_3393_cmd(hostdata, WD_CMD_ABORT);
1732
1733/* Now we have to attempt to flush out the FIFO... */
1734
1735		printk("flushing fifo - ");
1736		timeout = 1000000;
1737		do {
1738			asr = READ_AUX_STAT();
1739			if (asr & ASR_DBR)
1740				read_3393(hostdata, WD_DATA);
1741		} while (!(asr & ASR_INT) && timeout-- > 0);
1742		sr = read_3393(hostdata, WD_SCSI_STATUS);
1743		printk("asr=%02x, sr=%02x, %ld bytes un-transferred (timeout=%ld) - ", asr, sr, read_3393_count(hostdata), timeout);
1744
1745		/*
1746		 * Abort command processed.
1747		 * Still connected.
1748		 * We must disconnect.
1749		 */
1750
1751		printk("sending wd33c93 DISCONNECT command - ");
1752		write_3393_cmd(hostdata, WD_CMD_DISCONNECT);
1753
1754		timeout = 1000000;
1755		asr = READ_AUX_STAT();
1756		while ((asr & ASR_CIP) && timeout-- > 0)
1757			asr = READ_AUX_STAT();
1758		sr = read_3393(hostdata, WD_SCSI_STATUS);
1759		printk("asr=%02x, sr=%02x.", asr, sr);
1760
1761		hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1762		hostdata->connected = NULL;
1763		hostdata->state = S_UNCONNECTED;
1764		cmd->result = DID_ABORT << 16;
1765		cmd->scsi_done(cmd);
1766
1767		in2000_execute(instance);
1768
1769		return SUCCESS;
1770	}
1771
1772/*
1773 * Case 3: If the command is currently disconnected from the bus,
1774 * we're not going to expend much effort here: Let's just return
1775 * an ABORT_SNOOZE and hope for the best...
1776 */
1777
1778	for (tmp = (Scsi_Cmnd *) hostdata->disconnected_Q; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
1779		if (cmd == tmp) {
1780			printk(KERN_DEBUG "scsi%d: unable to abort disconnected command.\n", instance->host_no);
1781			return FAILED;
1782		}
1783
1784/*
1785 * Case 4 : If we reached this point, the command was not found in any of
1786 *     the queues.
1787 *
1788 * We probably reached this point because of an unlikely race condition
1789 * between the command completing successfully and the abortion code,
1790 * so we won't panic, but we will notify the user in case something really
1791 * broke.
1792 */
1793
1794	in2000_execute(instance);
1795
1796	printk("scsi%d: warning : SCSI command probably completed successfully" "         before abortion. ", instance->host_no);
1797	return SUCCESS;
1798}
1799
1800static int in2000_abort(Scsi_Cmnd * cmd)
1801{
1802	int rc;
1803
1804	spin_lock_irq(cmd->device->host->host_lock);
1805	rc = __in2000_abort(cmd);
1806	spin_unlock_irq(cmd->device->host->host_lock);
1807
1808	return rc;
1809}
1810
1811
1812#define MAX_IN2000_HOSTS 3
1813#define MAX_SETUP_ARGS ARRAY_SIZE(setup_args)
1814#define SETUP_BUFFER_SIZE 200
1815static char setup_buffer[SETUP_BUFFER_SIZE];
1816static char setup_used[MAX_SETUP_ARGS];
1817static int done_setup = 0;
1818
1819static void __init in2000_setup(char *str, int *ints)
1820{
1821	int i;
1822	char *p1, *p2;
1823
1824	strlcpy(setup_buffer, str, SETUP_BUFFER_SIZE);
1825	p1 = setup_buffer;
1826	i = 0;
1827	while (*p1 && (i < MAX_SETUP_ARGS)) {
1828		p2 = strchr(p1, ',');
1829		if (p2) {
1830			*p2 = '\0';
1831			if (p1 != p2)
1832				setup_args[i] = p1;
1833			p1 = p2 + 1;
1834			i++;
1835		} else {
1836			setup_args[i] = p1;
1837			break;
1838		}
1839	}
1840	for (i = 0; i < MAX_SETUP_ARGS; i++)
1841		setup_used[i] = 0;
1842	done_setup = 1;
1843}
1844
1845
1846/* check_setup_args() returns index if key found, 0 if not
1847 */
1848
1849static int __init check_setup_args(char *key, int *val, char *buf)
1850{
1851	int x;
1852	char *cp;
1853
1854	for (x = 0; x < MAX_SETUP_ARGS; x++) {
1855		if (setup_used[x])
1856			continue;
1857		if (!strncmp(setup_args[x], key, strlen(key)))
1858			break;
1859	}
1860	if (x == MAX_SETUP_ARGS)
1861		return 0;
1862	setup_used[x] = 1;
1863	cp = setup_args[x] + strlen(key);
1864	*val = -1;
1865	if (*cp != ':')
1866		return ++x;
1867	cp++;
1868	if ((*cp >= '0') && (*cp <= '9')) {
1869		*val = simple_strtoul(cp, NULL, 0);
1870	}
1871	return ++x;
1872}
1873
1874
1875
1876/* The "correct" (ie portable) way to access memory-mapped hardware
1877 * such as the IN2000 EPROM and dip switch is through the use of
1878 * special macros declared in 'asm/io.h'. We use readb() and readl()
1879 * when reading from the card's BIOS area in in2000_detect().
1880 */
1881static u32 bios_tab[] in2000__INITDATA = {
1882	0xc8000,
1883	0xd0000,
1884	0xd8000,
1885	0
1886};
1887
1888static unsigned short base_tab[] in2000__INITDATA = {
1889	0x220,
1890	0x200,
1891	0x110,
1892	0x100,
1893};
1894
1895static int int_tab[] in2000__INITDATA = {
1896	15,
1897	14,
1898	11,
1899	10
1900};
1901
1902static int probe_bios(u32 addr, u32 *s1, uchar *switches)
1903{
1904	void __iomem *p = ioremap(addr, 0x34);
1905	if (!p)
1906		return 0;
1907	*s1 = readl(p + 0x10);
1908	if (*s1 == 0x41564f4e || readl(p + 0x30) == 0x61776c41) {
1909		/* Read the switch image that's mapped into EPROM space */
1910		*switches = ~readb(p + 0x20);
1911		iounmap(p);
1912		return 1;
1913	}
1914	iounmap(p);
1915	return 0;
1916}
1917
1918static int __init in2000_detect(struct scsi_host_template * tpnt)
1919{
1920	struct Scsi_Host *instance;
1921	struct IN2000_hostdata *hostdata;
1922	int detect_count;
1923	int bios;
1924	int x;
1925	unsigned short base;
1926	uchar switches;
1927	uchar hrev;
1928	unsigned long flags;
1929	int val;
1930	char buf[32];
1931
1932/* Thanks to help from Bill Earnest, probing for IN2000 cards is a
1933 * pretty straightforward and fool-proof operation. There are 3
1934 * possible locations for the IN2000 EPROM in memory space - if we
1935 * find a BIOS signature, we can read the dip switch settings from
1936 * the byte at BIOS+32 (shadowed in by logic on the card). From 2
1937 * of the switch bits we get the card's address in IO space. There's
1938 * an image of the dip switch there, also, so we have a way to back-
1939 * check that this really is an IN2000 card. Very nifty. Use the
1940 * 'ioport:xx' command-line parameter if your BIOS EPROM is absent
1941 * or disabled.
1942 */
1943
1944	if (!done_setup && setup_strings)
1945		in2000_setup(setup_strings, NULL);
1946
1947	detect_count = 0;
1948	for (bios = 0; bios_tab[bios]; bios++) {
1949		u32 s1 = 0;
1950		if (check_setup_args("ioport", &val, buf)) {
1951			base = val;
1952			switches = ~inb(base + IO_SWITCHES) & 0xff;
1953			printk("Forcing IN2000 detection at IOport 0x%x ", base);
1954			bios = 2;
1955		}
1956/*
1957 * There have been a couple of BIOS versions with different layouts
1958 * for the obvious ID strings. We look for the 2 most common ones and
1959 * hope that they cover all the cases...
1960 */
1961		else if (probe_bios(bios_tab[bios], &s1, &switches)) {
1962			printk("Found IN2000 BIOS at 0x%x ", (unsigned int) bios_tab[bios]);
1963
1964/* Find out where the IO space is */
1965
1966			x = switches & (SW_ADDR0 | SW_ADDR1);
1967			base = base_tab[x];
1968
1969/* Check for the IN2000 signature in IO space. */
1970
1971			x = ~inb(base + IO_SWITCHES) & 0xff;
1972			if (x != switches) {
1973				printk("Bad IO signature: %02x vs %02x.\n", x, switches);
1974				continue;
1975			}
1976		} else
1977			continue;
1978
1979/* OK. We have a base address for the IO ports - run a few safety checks */
1980
1981		if (!(switches & SW_BIT7)) {	/* I _think_ all cards do this */
1982			printk("There is no IN-2000 SCSI card at IOport 0x%03x!\n", base);
1983			continue;
1984		}
1985
1986/* Let's assume any hardware version will work, although the driver
1987 * has only been tested on 0x21, 0x22, 0x25, 0x26, and 0x27. We'll
1988 * print out the rev number for reference later, but accept them all.
1989 */
1990
1991		hrev = inb(base + IO_HARDWARE);
1992
1993		/* Bit 2 tells us if interrupts are disabled */
1994		if (switches & SW_DISINT) {
1995			printk("The IN-2000 SCSI card at IOport 0x%03x ", base);
1996			printk("is not configured for interrupt operation!\n");
1997			printk("This driver requires an interrupt: cancelling detection.\n");
1998			continue;
1999		}
2000
2001/* Ok. We accept that there's an IN2000 at ioaddr 'base'. Now
2002 * initialize it.
2003 */
2004
2005		tpnt->proc_name = "in2000";
2006		instance = scsi_register(tpnt, sizeof(struct IN2000_hostdata));
2007		if (instance == NULL)
2008			continue;
2009		detect_count++;
2010		hostdata = (struct IN2000_hostdata *) instance->hostdata;
2011		instance->io_port = hostdata->io_base = base;
2012		hostdata->dip_switch = switches;
2013		hostdata->hrev = hrev;
2014
2015		write1_io(0, IO_FIFO_WRITE);	/* clear fifo counter */
2016		write1_io(0, IO_FIFO_READ);	/* start fifo out in read mode */
2017		write1_io(0, IO_INTR_MASK);	/* allow all ints */
2018		x = int_tab[(switches & (SW_INT0 | SW_INT1)) >> SW_INT_SHIFT];
2019		if (request_irq(x, in2000_intr, IRQF_DISABLED, "in2000", instance)) {
2020			printk("in2000_detect: Unable to allocate IRQ.\n");
2021			detect_count--;
2022			continue;
2023		}
2024		instance->irq = x;
2025		instance->n_io_port = 13;
2026		request_region(base, 13, "in2000");	/* lock in this IO space for our use */
2027
2028		for (x = 0; x < 8; x++) {
2029			hostdata->busy[x] = 0;
2030			hostdata->sync_xfer[x] = calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
2031			hostdata->sync_stat[x] = SS_UNSET;	/* using default sync values */
2032#ifdef PROC_STATISTICS
2033			hostdata->cmd_cnt[x] = 0;
2034			hostdata->disc_allowed_cnt[x] = 0;
2035			hostdata->disc_done_cnt[x] = 0;
2036#endif
2037		}
2038		hostdata->input_Q = NULL;
2039		hostdata->selecting = NULL;
2040		hostdata->connected = NULL;
2041		hostdata->disconnected_Q = NULL;
2042		hostdata->state = S_UNCONNECTED;
2043		hostdata->fifo = FI_FIFO_UNUSED;
2044		hostdata->level2 = L2_BASIC;
2045		hostdata->disconnect = DIS_ADAPTIVE;
2046		hostdata->args = DEBUG_DEFAULTS;
2047		hostdata->incoming_ptr = 0;
2048		hostdata->outgoing_len = 0;
2049		hostdata->default_sx_per = DEFAULT_SX_PER;
2050
2051/* Older BIOS's had a 'sync on/off' switch - use its setting */
2052
2053		if (s1 == 0x41564f4e && (switches & SW_SYNC_DOS5))
2054			hostdata->sync_off = 0x00;	/* sync defaults to on */
2055		else
2056			hostdata->sync_off = 0xff;	/* sync defaults to off */
2057
2058#ifdef PROC_INTERFACE
2059		hostdata->proc = PR_VERSION | PR_INFO | PR_STATISTICS | PR_CONNECTED | PR_INPUTQ | PR_DISCQ | PR_STOP;
2060#ifdef PROC_STATISTICS
2061		hostdata->int_cnt = 0;
2062#endif
2063#endif
2064
2065		if (check_setup_args("nosync", &val, buf))
2066			hostdata->sync_off = val;
2067
2068		if (check_setup_args("period", &val, buf))
2069			hostdata->default_sx_per = sx_table[round_period((unsigned int) val)].period_ns;
2070
2071		if (check_setup_args("disconnect", &val, buf)) {
2072			if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
2073				hostdata->disconnect = val;
2074			else
2075				hostdata->disconnect = DIS_ADAPTIVE;
2076		}
2077
2078		if (check_setup_args("noreset", &val, buf))
2079			hostdata->args ^= A_NO_SCSI_RESET;
2080
2081		if (check_setup_args("level2", &val, buf))
2082			hostdata->level2 = val;
2083
2084		if (check_setup_args("debug", &val, buf))
2085			hostdata->args = (val & DB_MASK);
2086
2087#ifdef PROC_INTERFACE
2088		if (check_setup_args("proc", &val, buf))
2089			hostdata->proc = val;
2090#endif
2091
2092
2093		/* FIXME: not strictly needed I think but the called code expects
2094		   to be locked */
2095		spin_lock_irqsave(instance->host_lock, flags);
2096		x = reset_hardware(instance, (hostdata->args & A_NO_SCSI_RESET) ? RESET_CARD : RESET_CARD_AND_BUS);
2097		spin_unlock_irqrestore(instance->host_lock, flags);
2098
2099		hostdata->microcode = read_3393(hostdata, WD_CDB_1);
2100		if (x & 0x01) {
2101			if (x & B_FLAG)
2102				hostdata->chip = C_WD33C93B;
2103			else
2104				hostdata->chip = C_WD33C93A;
2105		} else
2106			hostdata->chip = C_WD33C93;
2107
2108		printk("dip_switch=%02x irq=%d ioport=%02x floppy=%s sync/DOS5=%s ", (switches & 0x7f), instance->irq, hostdata->io_base, (switches & SW_FLOPPY) ? "Yes" : "No", (switches & SW_SYNC_DOS5) ? "Yes" : "No");
2109		printk("hardware_ver=%02x chip=%s microcode=%02x\n", hrev, (hostdata->chip == C_WD33C93) ? "WD33c93" : (hostdata->chip == C_WD33C93A) ? "WD33c93A" : (hostdata->chip == C_WD33C93B) ? "WD33c93B" : "unknown", hostdata->microcode);
2110#ifdef DEBUGGING_ON
2111		printk("setup_args = ");
2112		for (x = 0; x < MAX_SETUP_ARGS; x++)
2113			printk("%s,", setup_args[x]);
2114		printk("\n");
2115#endif
2116		if (hostdata->sync_off == 0xff)
2117			printk("Sync-transfer DISABLED on all devices: ENABLE from command-line\n");
2118		printk("IN2000 driver version %s - %s\n", IN2000_VERSION, IN2000_DATE);
2119	}
2120
2121	return detect_count;
2122}
2123
2124static int in2000_release(struct Scsi_Host *shost)
2125{
2126	if (shost->irq)
2127		free_irq(shost->irq, shost);
2128	if (shost->io_port && shost->n_io_port)
2129		release_region(shost->io_port, shost->n_io_port);
2130	return 0;
2131}
2132
2133/* NOTE: I lifted this function straight out of the old driver,
2134 *       and have not tested it. Presumably it does what it's
2135 *       supposed to do...
2136 */
2137
2138static int in2000_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int *iinfo)
2139{
2140	int size;
2141
2142	size = capacity;
2143	iinfo[0] = 64;
2144	iinfo[1] = 32;
2145	iinfo[2] = size >> 11;
2146
2147/* This should approximate the large drive handling that the DOS ASPI manager
2148   uses.  Drives very near the boundaries may not be handled correctly (i.e.
2149   near 2.0 Gb and 4.0 Gb) */
2150
2151	if (iinfo[2] > 1024) {
2152		iinfo[0] = 64;
2153		iinfo[1] = 63;
2154		iinfo[2] = (unsigned long) capacity / (iinfo[0] * iinfo[1]);
2155	}
2156	if (iinfo[2] > 1024) {
2157		iinfo[0] = 128;
2158		iinfo[1] = 63;
2159		iinfo[2] = (unsigned long) capacity / (iinfo[0] * iinfo[1]);
2160	}
2161	if (iinfo[2] > 1024) {
2162		iinfo[0] = 255;
2163		iinfo[1] = 63;
2164		iinfo[2] = (unsigned long) capacity / (iinfo[0] * iinfo[1]);
2165	}
2166	return 0;
2167}
2168
2169
2170static int in2000_proc_info(struct Scsi_Host *instance, char *buf, char **start, off_t off, int len, int in)
2171{
2172
2173#ifdef PROC_INTERFACE
2174
2175	char *bp;
2176	char tbuf[128];
2177	unsigned long flags;
2178	struct IN2000_hostdata *hd;
2179	Scsi_Cmnd *cmd;
2180	int x, i;
2181	static int stop = 0;
2182
2183	hd = (struct IN2000_hostdata *) instance->hostdata;
2184
2185/* If 'in' is TRUE we need to _read_ the proc file. We accept the following
2186 * keywords (same format as command-line, but only ONE per read):
2187 *    debug
2188 *    disconnect
2189 *    period
2190 *    resync
2191 *    proc
2192 */
2193
2194	if (in) {
2195		buf[len] = '\0';
2196		bp = buf;
2197		if (!strncmp(bp, "debug:", 6)) {
2198			bp += 6;
2199			hd->args = simple_strtoul(bp, NULL, 0) & DB_MASK;
2200		} else if (!strncmp(bp, "disconnect:", 11)) {
2201			bp += 11;
2202			x = simple_strtoul(bp, NULL, 0);
2203			if (x < DIS_NEVER || x > DIS_ALWAYS)
2204				x = DIS_ADAPTIVE;
2205			hd->disconnect = x;
2206		} else if (!strncmp(bp, "period:", 7)) {
2207			bp += 7;
2208			x = simple_strtoul(bp, NULL, 0);
2209			hd->default_sx_per = sx_table[round_period((unsigned int) x)].period_ns;
2210		} else if (!strncmp(bp, "resync:", 7)) {
2211			bp += 7;
2212			x = simple_strtoul(bp, NULL, 0);
2213			for (i = 0; i < 7; i++)
2214				if (x & (1 << i))
2215					hd->sync_stat[i] = SS_UNSET;
2216		} else if (!strncmp(bp, "proc:", 5)) {
2217			bp += 5;
2218			hd->proc = simple_strtoul(bp, NULL, 0);
2219		} else if (!strncmp(bp, "level2:", 7)) {
2220			bp += 7;
2221			hd->level2 = simple_strtoul(bp, NULL, 0);
2222		}
2223		return len;
2224	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2225
2226	spin_lock_irqsave(instance->host_lock, flags);
2227	bp = buf;
2228	*bp = '\0';
2229	if (hd->proc & PR_VERSION) {
2230		sprintf(tbuf, "\nVersion %s - %s.", IN2000_VERSION, IN2000_DATE);
2231		strcat(bp, tbuf);
2232	}
2233	if (hd->proc & PR_INFO) {
2234		sprintf(tbuf, "\ndip_switch=%02x: irq=%d io=%02x floppy=%s sync/DOS5=%s", (hd->dip_switch & 0x7f), instance->irq, hd->io_base, (hd->dip_switch & 0x40) ? "Yes" : "No", (hd->dip_switch & 0x20) ? "Yes" : "No");
2235		strcat(bp, tbuf);
2236		strcat(bp, "\nsync_xfer[] =       ");
2237		for (x = 0; x < 7; x++) {
2238			sprintf(tbuf, "\t%02x", hd->sync_xfer[x]);
2239			strcat(bp, tbuf);
2240		}
2241		strcat(bp, "\nsync_stat[] =       ");
2242		for (x = 0; x < 7; x++) {
2243			sprintf(tbuf, "\t%02x", hd->sync_stat[x]);
2244			strcat(bp, tbuf);
2245		}
2246	}
2247#ifdef PROC_STATISTICS
2248	if (hd->proc & PR_STATISTICS) {
2249		strcat(bp, "\ncommands issued:    ");
2250		for (x = 0; x < 7; x++) {
2251			sprintf(tbuf, "\t%ld", hd->cmd_cnt[x]);
2252			strcat(bp, tbuf);
2253		}
2254		strcat(bp, "\ndisconnects allowed:");
2255		for (x = 0; x < 7; x++) {
2256			sprintf(tbuf, "\t%ld", hd->disc_allowed_cnt[x]);
2257			strcat(bp, tbuf);
2258		}
2259		strcat(bp, "\ndisconnects done:   ");
2260		for (x = 0; x < 7; x++) {
2261			sprintf(tbuf, "\t%ld", hd->disc_done_cnt[x]);
2262			strcat(bp, tbuf);
2263		}
2264		sprintf(tbuf, "\ninterrupts:      \t%ld", hd->int_cnt);
2265		strcat(bp, tbuf);
2266	}
2267#endif
2268	if (hd->proc & PR_CONNECTED) {
2269		strcat(bp, "\nconnected:     ");
2270		if (hd->connected) {
2271			cmd = (Scsi_Cmnd *) hd->connected;
2272			sprintf(tbuf, " %d:%d(%02x)", cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
2273			strcat(bp, tbuf);
2274		}
2275	}
2276	if (hd->proc & PR_INPUTQ) {
2277		strcat(bp, "\ninput_Q:       ");
2278		cmd = (Scsi_Cmnd *) hd->input_Q;
2279		while (cmd) {
2280			sprintf(tbuf, " %d:%d(%02x)", cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
2281			strcat(bp, tbuf);
2282			cmd = (Scsi_Cmnd *) cmd->host_scribble;
2283		}
2284	}
2285	if (hd->proc & PR_DISCQ) {
2286		strcat(bp, "\ndisconnected_Q:");
2287		cmd = (Scsi_Cmnd *) hd->disconnected_Q;
2288		while (cmd) {
2289			sprintf(tbuf, " %d:%d(%02x)", cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
2290			strcat(bp, tbuf);
2291			cmd = (Scsi_Cmnd *) cmd->host_scribble;
2292		}
2293	}
2294	if (hd->proc & PR_TEST) {
2295		;		/* insert your own custom function here */
2296	}
2297	strcat(bp, "\n");
2298	spin_unlock_irqrestore(instance->host_lock, flags);
2299	*start = buf;
2300	if (stop) {
2301		stop = 0;
2302		return 0;	/* return 0 to signal end-of-file */
2303	}
2304	if (off > 0x40000)	/* ALWAYS stop after 256k bytes have been read */
2305		stop = 1;
2306	if (hd->proc & PR_STOP)	/* stop every other time */
2307		stop = 1;
2308	return strlen(bp);
2309
2310#else				/* PROC_INTERFACE */
2311
2312	return 0;
2313
2314#endif				/* PROC_INTERFACE */
2315
2316}
2317
2318MODULE_LICENSE("GPL");
2319
2320
2321static struct scsi_host_template driver_template = {
2322	.proc_name       		= "in2000",
2323	.proc_info       		= in2000_proc_info,
 
2324	.name            		= "Always IN2000",
2325	.detect          		= in2000_detect, 
2326	.release			= in2000_release,
2327	.queuecommand    		= in2000_queuecommand,
2328	.eh_abort_handler		= in2000_abort,
2329	.eh_bus_reset_handler		= in2000_bus_reset,
2330	.bios_param      		= in2000_biosparam, 
2331	.can_queue       		= IN2000_CAN_Q,
2332	.this_id         		= IN2000_HOST_ID,
2333	.sg_tablesize    		= IN2000_SG,
2334	.cmd_per_lun     		= IN2000_CPL,
2335	.use_clustering  		= DISABLE_CLUSTERING,
2336};
2337#include "scsi_module.c"
v4.6
   1/*
   2 *    in2000.c -  Linux device driver for the
   3 *                Always IN2000 ISA SCSI card.
   4 *
   5 * Copyright (c) 1996 John Shifflett, GeoLog Consulting
   6 *    john@geolog.com
   7 *    jshiffle@netcom.com
   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 * For the avoidance of doubt the "preferred form" of this code is one which
  20 * is in an open non patent encumbered format. Where cryptographic key signing
  21 * forms part of the process of creating an executable the information
  22 * including keys needed to generate an equivalently functional executable
  23 * are deemed to be part of the source code.
  24 *
  25 * Drew Eckhardt's excellent 'Generic NCR5380' sources provided
  26 * much of the inspiration and some of the code for this driver.
  27 * The Linux IN2000 driver distributed in the Linux kernels through
  28 * version 1.2.13 was an extremely valuable reference on the arcane
  29 * (and still mysterious) workings of the IN2000's fifo. It also
  30 * is where I lifted in2000_biosparam(), the gist of the card
  31 * detection scheme, and other bits of code. Many thanks to the
  32 * talented and courageous people who wrote, contributed to, and
  33 * maintained that driver (including Brad McLean, Shaun Savage,
  34 * Bill Earnest, Larry Doolittle, Roger Sunshine, John Luckey,
  35 * Matt Postiff, Peter Lu, zerucha@shell.portal.com, and Eric
  36 * Youngdale). I should also mention the driver written by
  37 * Hamish Macdonald for the (GASP!) Amiga A2091 card, included
  38 * in the Linux-m68k distribution; it gave me a good initial
  39 * understanding of the proper way to run a WD33c93 chip, and I
  40 * ended up stealing lots of code from it.
  41 *
  42 * _This_ driver is (I feel) an improvement over the old one in
  43 * several respects:
  44 *    -  All problems relating to the data size of a SCSI request are
  45 *          gone (as far as I know). The old driver couldn't handle
  46 *          swapping to partitions because that involved 4k blocks, nor
  47 *          could it deal with the st.c tape driver unmodified, because
  48 *          that usually involved 4k - 32k blocks. The old driver never
  49 *          quite got away from a morbid dependence on 2k block sizes -
  50 *          which of course is the size of the card's fifo.
  51 *
  52 *    -  Target Disconnection/Reconnection is now supported. Any
  53 *          system with more than one device active on the SCSI bus
  54 *          will benefit from this. The driver defaults to what I'm
  55 *          calling 'adaptive disconnect' - meaning that each command
  56 *          is evaluated individually as to whether or not it should
  57 *          be run with the option to disconnect/reselect (if the
  58 *          device chooses), or as a "SCSI-bus-hog".
  59 *
  60 *    -  Synchronous data transfers are now supported. Because there
  61 *          are a few devices (and many improperly terminated systems)
  62 *          that choke when doing sync, the default is sync DISABLED
  63 *          for all devices. This faster protocol can (and should!)
  64 *          be enabled on selected devices via the command-line.
  65 *
  66 *    -  Runtime operating parameters can now be specified through
  67 *       either the LILO or the 'insmod' command line. For LILO do:
  68 *          "in2000=blah,blah,blah"
  69 *       and with insmod go like:
  70 *          "insmod /usr/src/linux/modules/in2000.o setup_strings=blah,blah"
  71 *       The defaults should be good for most people. See the comment
  72 *       for 'setup_strings' below for more details.
  73 *
  74 *    -  The old driver relied exclusively on what the Western Digital
  75 *          docs call "Combination Level 2 Commands", which are a great
  76 *          idea in that the CPU is relieved of a lot of interrupt
  77 *          overhead. However, by accepting a certain (user-settable)
  78 *          amount of additional interrupts, this driver achieves
  79 *          better control over the SCSI bus, and data transfers are
  80 *          almost as fast while being much easier to define, track,
  81 *          and debug.
  82 *
  83 *    -  You can force detection of a card whose BIOS has been disabled.
  84 *
  85 *    -  Multiple IN2000 cards might almost be supported. I've tried to
  86 *       keep it in mind, but have no way to test...
  87 *
  88 *
  89 * TODO:
  90 *       tagged queuing. multiple cards.
  91 *
  92 *
  93 * NOTE:
  94 *       When using this or any other SCSI driver as a module, you'll
  95 *       find that with the stock kernel, at most _two_ SCSI hard
  96 *       drives will be linked into the device list (ie, usable).
  97 *       If your IN2000 card has more than 2 disks on its bus, you
  98 *       might want to change the define of 'SD_EXTRA_DEVS' in the
  99 *       'hosts.h' file from 2 to whatever is appropriate. It took
 100 *       me a while to track down this surprisingly obscure and
 101 *       undocumented little "feature".
 102 *
 103 *
 104 * People with bug reports, wish-lists, complaints, comments,
 105 * or improvements are asked to pah-leeez email me (John Shifflett)
 106 * at john@geolog.com or jshiffle@netcom.com! I'm anxious to get
 107 * this thing into as good a shape as possible, and I'm positive
 108 * there are lots of lurking bugs and "Stupid Places".
 109 *
 110 * Updated for Linux 2.5 by Alan Cox <alan@lxorguk.ukuu.org.uk>
 111 *	- Using new_eh handler
 112 *	- Hopefully got all the locking right again
 113 *	See "FIXME" notes for items that could do with more work
 114 */
 115
 116#include <linux/module.h>
 117#include <linux/blkdev.h>
 118#include <linux/interrupt.h>
 119#include <linux/string.h>
 120#include <linux/delay.h>
 121#include <linux/proc_fs.h>
 122#include <linux/ioport.h>
 123#include <linux/stat.h>
 124
 125#include <asm/io.h>
 
 126
 127#include "scsi.h"
 128#include <scsi/scsi_host.h>
 129
 130#define IN2000_VERSION    "1.33-2.5"
 131#define IN2000_DATE       "2002/11/03"
 132
 133#include "in2000.h"
 134
 135
 136/*
 137 * 'setup_strings' is a single string used to pass operating parameters and
 138 * settings from the kernel/module command-line to the driver. 'setup_args[]'
 139 * is an array of strings that define the compile-time default values for
 140 * these settings. If Linux boots with a LILO or insmod command-line, those
 141 * settings are combined with 'setup_args[]'. Note that LILO command-lines
 142 * are prefixed with "in2000=" while insmod uses a "setup_strings=" prefix.
 143 * The driver recognizes the following keywords (lower case required) and
 144 * arguments:
 145 *
 146 * -  ioport:addr    -Where addr is IO address of a (usually ROM-less) card.
 147 * -  noreset        -No optional args. Prevents SCSI bus reset at boot time.
 148 * -  nosync:x       -x is a bitmask where the 1st 7 bits correspond with
 149 *                    the 7 possible SCSI devices (bit 0 for device #0, etc).
 150 *                    Set a bit to PREVENT sync negotiation on that device.
 151 *                    The driver default is sync DISABLED on all devices.
 152 * -  period:ns      -ns is the minimum # of nanoseconds in a SCSI data transfer
 153 *                    period. Default is 500; acceptable values are 250 - 1000.
 154 * -  disconnect:x   -x = 0 to never allow disconnects, 2 to always allow them.
 155 *                    x = 1 does 'adaptive' disconnects, which is the default
 156 *                    and generally the best choice.
 157 * -  debug:x        -If 'DEBUGGING_ON' is defined, x is a bitmask that causes
 158 *                    various types of debug output to printed - see the DB_xxx
 159 *                    defines in in2000.h
 160 * -  proc:x         -If 'PROC_INTERFACE' is defined, x is a bitmask that
 161 *                    determines how the /proc interface works and what it
 162 *                    does - see the PR_xxx defines in in2000.h
 163 *
 164 * Syntax Notes:
 165 * -  Numeric arguments can be decimal or the '0x' form of hex notation. There
 166 *    _must_ be a colon between a keyword and its numeric argument, with no
 167 *    spaces.
 168 * -  Keywords are separated by commas, no spaces, in the standard kernel
 169 *    command-line manner.
 170 * -  A keyword in the 'nth' comma-separated command-line member will overwrite
 171 *    the 'nth' element of setup_args[]. A blank command-line member (in
 172 *    other words, a comma with no preceding keyword) will _not_ overwrite
 173 *    the corresponding setup_args[] element.
 174 *
 175 * A few LILO examples (for insmod, use 'setup_strings' instead of 'in2000'):
 176 * -  in2000=ioport:0x220,noreset
 177 * -  in2000=period:250,disconnect:2,nosync:0x03
 178 * -  in2000=debug:0x1e
 179 * -  in2000=proc:3
 180 */
 181
 182/* Normally, no defaults are specified... */
 183static char *setup_args[] = { "", "", "", "", "", "", "", "", "" };
 184
 185/* filled in by 'insmod' */
 186static char *setup_strings;
 187
 188module_param(setup_strings, charp, 0);
 189
 190static inline uchar read_3393(struct IN2000_hostdata *hostdata, uchar reg_num)
 191{
 192	write1_io(reg_num, IO_WD_ADDR);
 193	return read1_io(IO_WD_DATA);
 194}
 195
 196
 197#define READ_AUX_STAT() read1_io(IO_WD_ASR)
 198
 199
 200static inline void write_3393(struct IN2000_hostdata *hostdata, uchar reg_num, uchar value)
 201{
 202	write1_io(reg_num, IO_WD_ADDR);
 203	write1_io(value, IO_WD_DATA);
 204}
 205
 206
 207static inline void write_3393_cmd(struct IN2000_hostdata *hostdata, uchar cmd)
 208{
 209/*   while (READ_AUX_STAT() & ASR_CIP)
 210      printk("|");*/
 211	write1_io(WD_COMMAND, IO_WD_ADDR);
 212	write1_io(cmd, IO_WD_DATA);
 213}
 214
 215
 216static uchar read_1_byte(struct IN2000_hostdata *hostdata)
 217{
 218	uchar asr, x = 0;
 219
 220	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
 221	write_3393_cmd(hostdata, WD_CMD_TRANS_INFO | 0x80);
 222	do {
 223		asr = READ_AUX_STAT();
 224		if (asr & ASR_DBR)
 225			x = read_3393(hostdata, WD_DATA);
 226	} while (!(asr & ASR_INT));
 227	return x;
 228}
 229
 230
 231static void write_3393_count(struct IN2000_hostdata *hostdata, unsigned long value)
 232{
 233	write1_io(WD_TRANSFER_COUNT_MSB, IO_WD_ADDR);
 234	write1_io((value >> 16), IO_WD_DATA);
 235	write1_io((value >> 8), IO_WD_DATA);
 236	write1_io(value, IO_WD_DATA);
 237}
 238
 239
 240static unsigned long read_3393_count(struct IN2000_hostdata *hostdata)
 241{
 242	unsigned long value;
 243
 244	write1_io(WD_TRANSFER_COUNT_MSB, IO_WD_ADDR);
 245	value = read1_io(IO_WD_DATA) << 16;
 246	value |= read1_io(IO_WD_DATA) << 8;
 247	value |= read1_io(IO_WD_DATA);
 248	return value;
 249}
 250
 251
 252/* The 33c93 needs to be told which direction a command transfers its
 253 * data; we use this function to figure it out. Returns true if there
 254 * will be a DATA_OUT phase with this command, false otherwise.
 255 * (Thanks to Joerg Dorchain for the research and suggestion.)
 256 */
 257static int is_dir_out(Scsi_Cmnd * cmd)
 258{
 259	switch (cmd->cmnd[0]) {
 260	case WRITE_6:
 261	case WRITE_10:
 262	case WRITE_12:
 263	case WRITE_LONG:
 264	case WRITE_SAME:
 265	case WRITE_BUFFER:
 266	case WRITE_VERIFY:
 267	case WRITE_VERIFY_12:
 268	case COMPARE:
 269	case COPY:
 270	case COPY_VERIFY:
 271	case SEARCH_EQUAL:
 272	case SEARCH_HIGH:
 273	case SEARCH_LOW:
 274	case SEARCH_EQUAL_12:
 275	case SEARCH_HIGH_12:
 276	case SEARCH_LOW_12:
 277	case FORMAT_UNIT:
 278	case REASSIGN_BLOCKS:
 279	case RESERVE:
 280	case MODE_SELECT:
 281	case MODE_SELECT_10:
 282	case LOG_SELECT:
 283	case SEND_DIAGNOSTIC:
 284	case CHANGE_DEFINITION:
 285	case UPDATE_BLOCK:
 286	case SET_WINDOW:
 287	case MEDIUM_SCAN:
 288	case SEND_VOLUME_TAG:
 289	case 0xea:
 290		return 1;
 291	default:
 292		return 0;
 293	}
 294}
 295
 296
 297
 298static struct sx_period sx_table[] = {
 299	{1, 0x20},
 300	{252, 0x20},
 301	{376, 0x30},
 302	{500, 0x40},
 303	{624, 0x50},
 304	{752, 0x60},
 305	{876, 0x70},
 306	{1000, 0x00},
 307	{0, 0}
 308};
 309
 310static int round_period(unsigned int period)
 311{
 312	int x;
 313
 314	for (x = 1; sx_table[x].period_ns; x++) {
 315		if ((period <= sx_table[x - 0].period_ns) && (period > sx_table[x - 1].period_ns)) {
 316			return x;
 317		}
 318	}
 319	return 7;
 320}
 321
 322static uchar calc_sync_xfer(unsigned int period, unsigned int offset)
 323{
 324	uchar result;
 325
 326	period *= 4;		/* convert SDTR code to ns */
 327	result = sx_table[round_period(period)].reg_value;
 328	result |= (offset < OPTIMUM_SX_OFF) ? offset : OPTIMUM_SX_OFF;
 329	return result;
 330}
 331
 332
 333
 334static void in2000_execute(struct Scsi_Host *instance);
 335
 336static int in2000_queuecommand_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
 337{
 338	struct Scsi_Host *instance;
 339	struct IN2000_hostdata *hostdata;
 340	Scsi_Cmnd *tmp;
 341
 342	instance = cmd->device->host;
 343	hostdata = (struct IN2000_hostdata *) instance->hostdata;
 344
 345	DB(DB_QUEUE_COMMAND, scmd_printk(KERN_DEBUG, cmd, "Q-%02x(", cmd->cmnd[0]))
 346
 347/* Set up a few fields in the Scsi_Cmnd structure for our own use:
 348 *  - host_scribble is the pointer to the next cmd in the input queue
 349 *  - scsi_done points to the routine we call when a cmd is finished
 350 *  - result is what you'd expect
 351 */
 352	    cmd->host_scribble = NULL;
 353	cmd->scsi_done = done;
 354	cmd->result = 0;
 355
 356/* We use the Scsi_Pointer structure that's included with each command
 357 * as a scratchpad (as it's intended to be used!). The handy thing about
 358 * the SCp.xxx fields is that they're always associated with a given
 359 * cmd, and are preserved across disconnect-reselect. This means we
 360 * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
 361 * if we keep all the critical pointers and counters in SCp:
 362 *  - SCp.ptr is the pointer into the RAM buffer
 363 *  - SCp.this_residual is the size of that buffer
 364 *  - SCp.buffer points to the current scatter-gather buffer
 365 *  - SCp.buffers_residual tells us how many S.G. buffers there are
 366 *  - SCp.have_data_in helps keep track of >2048 byte transfers
 367 *  - SCp.sent_command is not used
 368 *  - SCp.phase records this command's SRCID_ER bit setting
 369 */
 370
 371	if (scsi_bufflen(cmd)) {
 372		cmd->SCp.buffer = scsi_sglist(cmd);
 373		cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
 374		cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
 375		cmd->SCp.this_residual = cmd->SCp.buffer->length;
 376	} else {
 377		cmd->SCp.buffer = NULL;
 378		cmd->SCp.buffers_residual = 0;
 379		cmd->SCp.ptr = NULL;
 380		cmd->SCp.this_residual = 0;
 381	}
 382	cmd->SCp.have_data_in = 0;
 383
 384/* We don't set SCp.phase here - that's done in in2000_execute() */
 385
 386/* WD docs state that at the conclusion of a "LEVEL2" command, the
 387 * status byte can be retrieved from the LUN register. Apparently,
 388 * this is the case only for *uninterrupted* LEVEL2 commands! If
 389 * there are any unexpected phases entered, even if they are 100%
 390 * legal (different devices may choose to do things differently),
 391 * the LEVEL2 command sequence is exited. This often occurs prior
 392 * to receiving the status byte, in which case the driver does a
 393 * status phase interrupt and gets the status byte on its own.
 394 * While such a command can then be "resumed" (ie restarted to
 395 * finish up as a LEVEL2 command), the LUN register will NOT be
 396 * a valid status byte at the command's conclusion, and we must
 397 * use the byte obtained during the earlier interrupt. Here, we
 398 * preset SCp.Status to an illegal value (0xff) so that when
 399 * this command finally completes, we can tell where the actual
 400 * status byte is stored.
 401 */
 402
 403	cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
 404
 405/* We need to disable interrupts before messing with the input
 406 * queue and calling in2000_execute().
 407 */
 408
 409	/*
 410	 * Add the cmd to the end of 'input_Q'. Note that REQUEST_SENSE
 411	 * commands are added to the head of the queue so that the desired
 412	 * sense data is not lost before REQUEST_SENSE executes.
 413	 */
 414
 415	if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
 416		cmd->host_scribble = (uchar *) hostdata->input_Q;
 417		hostdata->input_Q = cmd;
 418	} else {		/* find the end of the queue */
 419		for (tmp = (Scsi_Cmnd *) hostdata->input_Q; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
 420		tmp->host_scribble = (uchar *) cmd;
 421	}
 422
 423/* We know that there's at least one command in 'input_Q' now.
 424 * Go see if any of them are runnable!
 425 */
 426
 427	in2000_execute(cmd->device->host);
 428
 429	DB(DB_QUEUE_COMMAND, printk(")Q "))
 430	    return 0;
 431}
 432
 433static DEF_SCSI_QCMD(in2000_queuecommand)
 434
 435
 436
 437/*
 438 * This routine attempts to start a scsi command. If the host_card is
 439 * already connected, we give up immediately. Otherwise, look through
 440 * the input_Q, using the first command we find that's intended
 441 * for a currently non-busy target/lun.
 442 * Note that this function is always called with interrupts already
 443 * disabled (either from in2000_queuecommand() or in2000_intr()).
 444 */
 445static void in2000_execute(struct Scsi_Host *instance)
 446{
 447	struct IN2000_hostdata *hostdata;
 448	Scsi_Cmnd *cmd, *prev;
 449	int i;
 450	unsigned short *sp;
 451	unsigned short f;
 452	unsigned short flushbuf[16];
 453
 454
 455	hostdata = (struct IN2000_hostdata *) instance->hostdata;
 456
 457	DB(DB_EXECUTE, printk("EX("))
 458
 459	    if (hostdata->selecting || hostdata->connected) {
 460
 461		DB(DB_EXECUTE, printk(")EX-0 "))
 462
 463		    return;
 464	}
 465
 466	/*
 467	 * Search through the input_Q for a command destined
 468	 * for an idle target/lun.
 469	 */
 470
 471	cmd = (Scsi_Cmnd *) hostdata->input_Q;
 472	prev = NULL;
 473	while (cmd) {
 474		if (!(hostdata->busy[cmd->device->id] & (1 << cmd->device->lun)))
 475			break;
 476		prev = cmd;
 477		cmd = (Scsi_Cmnd *) cmd->host_scribble;
 478	}
 479
 480	/* quit if queue empty or all possible targets are busy */
 481
 482	if (!cmd) {
 483
 484		DB(DB_EXECUTE, printk(")EX-1 "))
 485
 486		    return;
 487	}
 488
 489	/*  remove command from queue */
 490
 491	if (prev)
 492		prev->host_scribble = cmd->host_scribble;
 493	else
 494		hostdata->input_Q = (Scsi_Cmnd *) cmd->host_scribble;
 495
 496#ifdef PROC_STATISTICS
 497	hostdata->cmd_cnt[cmd->device->id]++;
 498#endif
 499
 500/*
 501 * Start the selection process
 502 */
 503
 504	if (is_dir_out(cmd))
 505		write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id);
 506	else
 507		write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
 508
 509/* Now we need to figure out whether or not this command is a good
 510 * candidate for disconnect/reselect. We guess to the best of our
 511 * ability, based on a set of hierarchical rules. When several
 512 * devices are operating simultaneously, disconnects are usually
 513 * an advantage. In a single device system, or if only 1 device
 514 * is being accessed, transfers usually go faster if disconnects
 515 * are not allowed:
 516 *
 517 * + Commands should NEVER disconnect if hostdata->disconnect =
 518 *   DIS_NEVER (this holds for tape drives also), and ALWAYS
 519 *   disconnect if hostdata->disconnect = DIS_ALWAYS.
 520 * + Tape drive commands should always be allowed to disconnect.
 521 * + Disconnect should be allowed if disconnected_Q isn't empty.
 522 * + Commands should NOT disconnect if input_Q is empty.
 523 * + Disconnect should be allowed if there are commands in input_Q
 524 *   for a different target/lun. In this case, the other commands
 525 *   should be made disconnect-able, if not already.
 526 *
 527 * I know, I know - this code would flunk me out of any
 528 * "C Programming 101" class ever offered. But it's easy
 529 * to change around and experiment with for now.
 530 */
 531
 532	cmd->SCp.phase = 0;	/* assume no disconnect */
 533	if (hostdata->disconnect == DIS_NEVER)
 534		goto no;
 535	if (hostdata->disconnect == DIS_ALWAYS)
 536		goto yes;
 537	if (cmd->device->type == 1)	/* tape drive? */
 538		goto yes;
 539	if (hostdata->disconnected_Q)	/* other commands disconnected? */
 540		goto yes;
 541	if (!(hostdata->input_Q))	/* input_Q empty? */
 542		goto no;
 543	for (prev = (Scsi_Cmnd *) hostdata->input_Q; prev; prev = (Scsi_Cmnd *) prev->host_scribble) {
 544		if ((prev->device->id != cmd->device->id) || (prev->device->lun != cmd->device->lun)) {
 545			for (prev = (Scsi_Cmnd *) hostdata->input_Q; prev; prev = (Scsi_Cmnd *) prev->host_scribble)
 546				prev->SCp.phase = 1;
 547			goto yes;
 548		}
 549	}
 550	goto no;
 551
 552      yes:
 553	cmd->SCp.phase = 1;
 554
 555#ifdef PROC_STATISTICS
 556	hostdata->disc_allowed_cnt[cmd->device->id]++;
 557#endif
 558
 559      no:
 560	write_3393(hostdata, WD_SOURCE_ID, ((cmd->SCp.phase) ? SRCID_ER : 0));
 561
 562	write_3393(hostdata, WD_TARGET_LUN, cmd->device->lun);
 563	write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, hostdata->sync_xfer[cmd->device->id]);
 564	hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
 565
 566	if ((hostdata->level2 <= L2_NONE) || (hostdata->sync_stat[cmd->device->id] == SS_UNSET)) {
 567
 568		/*
 569		 * Do a 'Select-With-ATN' command. This will end with
 570		 * one of the following interrupts:
 571		 *    CSR_RESEL_AM:  failure - can try again later.
 572		 *    CSR_TIMEOUT:   failure - give up.
 573		 *    CSR_SELECT:    success - proceed.
 574		 */
 575
 576		hostdata->selecting = cmd;
 577
 578/* Every target has its own synchronous transfer setting, kept in
 579 * the sync_xfer array, and a corresponding status byte in sync_stat[].
 580 * Each target's sync_stat[] entry is initialized to SS_UNSET, and its
 581 * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
 582 * means that the parameters are undetermined as yet, and that we
 583 * need to send an SDTR message to this device after selection is
 584 * complete. We set SS_FIRST to tell the interrupt routine to do so,
 585 * unless we don't want to even _try_ synchronous transfers: In this
 586 * case we set SS_SET to make the defaults final.
 587 */
 588		if (hostdata->sync_stat[cmd->device->id] == SS_UNSET) {
 589			if (hostdata->sync_off & (1 << cmd->device->id))
 590				hostdata->sync_stat[cmd->device->id] = SS_SET;
 591			else
 592				hostdata->sync_stat[cmd->device->id] = SS_FIRST;
 593		}
 594		hostdata->state = S_SELECTING;
 595		write_3393_count(hostdata, 0);	/* this guarantees a DATA_PHASE interrupt */
 596		write_3393_cmd(hostdata, WD_CMD_SEL_ATN);
 597	}
 598
 599	else {
 600
 601		/*
 602		 * Do a 'Select-With-ATN-Xfer' command. This will end with
 603		 * one of the following interrupts:
 604		 *    CSR_RESEL_AM:  failure - can try again later.
 605		 *    CSR_TIMEOUT:   failure - give up.
 606		 *    anything else: success - proceed.
 607		 */
 608
 609		hostdata->connected = cmd;
 610		write_3393(hostdata, WD_COMMAND_PHASE, 0);
 611
 612		/* copy command_descriptor_block into WD chip
 613		 * (take advantage of auto-incrementing)
 614		 */
 615
 616		write1_io(WD_CDB_1, IO_WD_ADDR);
 617		for (i = 0; i < cmd->cmd_len; i++)
 618			write1_io(cmd->cmnd[i], IO_WD_DATA);
 619
 620		/* The wd33c93 only knows about Group 0, 1, and 5 commands when
 621		 * it's doing a 'select-and-transfer'. To be safe, we write the
 622		 * size of the CDB into the OWN_ID register for every case. This
 623		 * way there won't be problems with vendor-unique, audio, etc.
 624		 */
 625
 626		write_3393(hostdata, WD_OWN_ID, cmd->cmd_len);
 627
 628		/* When doing a non-disconnect command, we can save ourselves a DATA
 629		 * phase interrupt later by setting everything up now. With writes we
 630		 * need to pre-fill the fifo; if there's room for the 32 flush bytes,
 631		 * put them in there too - that'll avoid a fifo interrupt. Reads are
 632		 * somewhat simpler.
 633		 * KLUDGE NOTE: It seems that you can't completely fill the fifo here:
 634		 * This results in the IO_FIFO_COUNT register rolling over to zero,
 635		 * and apparently the gate array logic sees this as empty, not full,
 636		 * so the 3393 chip is never signalled to start reading from the
 637		 * fifo. Or maybe it's seen as a permanent fifo interrupt condition.
 638		 * Regardless, we fix this by temporarily pretending that the fifo
 639		 * is 16 bytes smaller. (I see now that the old driver has a comment
 640		 * about "don't fill completely" in an analogous place - must be the
 641		 * same deal.) This results in CDROM, swap partitions, and tape drives
 642		 * needing an extra interrupt per write command - I think we can live
 643		 * with that!
 644		 */
 645
 646		if (!(cmd->SCp.phase)) {
 647			write_3393_count(hostdata, cmd->SCp.this_residual);
 648			write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_BUS);
 649			write1_io(0, IO_FIFO_WRITE);	/* clear fifo counter, write mode */
 650
 651			if (is_dir_out(cmd)) {
 652				hostdata->fifo = FI_FIFO_WRITING;
 653				if ((i = cmd->SCp.this_residual) > (IN2000_FIFO_SIZE - 16))
 654					i = IN2000_FIFO_SIZE - 16;
 655				cmd->SCp.have_data_in = i;	/* this much data in fifo */
 656				i >>= 1;	/* Gulp. Assuming modulo 2. */
 657				sp = (unsigned short *) cmd->SCp.ptr;
 658				f = hostdata->io_base + IO_FIFO;
 659
 660#ifdef FAST_WRITE_IO
 661
 662				FAST_WRITE2_IO();
 663#else
 664				while (i--)
 665					write2_io(*sp++, IO_FIFO);
 666
 667#endif
 668
 669				/* Is there room for the flush bytes? */
 670
 671				if (cmd->SCp.have_data_in <= ((IN2000_FIFO_SIZE - 16) - 32)) {
 672					sp = flushbuf;
 673					i = 16;
 674
 675#ifdef FAST_WRITE_IO
 676
 677					FAST_WRITE2_IO();
 678#else
 679					while (i--)
 680						write2_io(0, IO_FIFO);
 681
 682#endif
 683
 684				}
 685			}
 686
 687			else {
 688				write1_io(0, IO_FIFO_READ);	/* put fifo in read mode */
 689				hostdata->fifo = FI_FIFO_READING;
 690				cmd->SCp.have_data_in = 0;	/* nothing transferred yet */
 691			}
 692
 693		} else {
 694			write_3393_count(hostdata, 0);	/* this guarantees a DATA_PHASE interrupt */
 695		}
 696		hostdata->state = S_RUNNING_LEVEL2;
 697		write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
 698	}
 699
 700	/*
 701	 * Since the SCSI bus can handle only 1 connection at a time,
 702	 * we get out of here now. If the selection fails, or when
 703	 * the command disconnects, we'll come back to this routine
 704	 * to search the input_Q again...
 705	 */
 706
 707	DB(DB_EXECUTE, printk("%s)EX-2 ", (cmd->SCp.phase) ? "d:" : ""))
 708
 709}
 710
 711
 712
 713static void transfer_pio(uchar * buf, int cnt, int data_in_dir, struct IN2000_hostdata *hostdata)
 714{
 715	uchar asr;
 716
 717	DB(DB_TRANSFER, printk("(%p,%d,%s)", buf, cnt, data_in_dir ? "in" : "out"))
 718
 719	    write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
 720	write_3393_count(hostdata, cnt);
 721	write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
 722	if (data_in_dir) {
 723		do {
 724			asr = READ_AUX_STAT();
 725			if (asr & ASR_DBR)
 726				*buf++ = read_3393(hostdata, WD_DATA);
 727		} while (!(asr & ASR_INT));
 728	} else {
 729		do {
 730			asr = READ_AUX_STAT();
 731			if (asr & ASR_DBR)
 732				write_3393(hostdata, WD_DATA, *buf++);
 733		} while (!(asr & ASR_INT));
 734	}
 735
 736	/* Note: we are returning with the interrupt UN-cleared.
 737	 * Since (presumably) an entire I/O operation has
 738	 * completed, the bus phase is probably different, and
 739	 * the interrupt routine will discover this when it
 740	 * responds to the uncleared int.
 741	 */
 742
 743}
 744
 745
 746
 747static void transfer_bytes(Scsi_Cmnd * cmd, int data_in_dir)
 748{
 749	struct IN2000_hostdata *hostdata;
 750	unsigned short *sp;
 751	unsigned short f;
 752	int i;
 753
 754	hostdata = (struct IN2000_hostdata *) cmd->device->host->hostdata;
 755
 756/* Normally, you'd expect 'this_residual' to be non-zero here.
 757 * In a series of scatter-gather transfers, however, this
 758 * routine will usually be called with 'this_residual' equal
 759 * to 0 and 'buffers_residual' non-zero. This means that a
 760 * previous transfer completed, clearing 'this_residual', and
 761 * now we need to setup the next scatter-gather buffer as the
 762 * source or destination for THIS transfer.
 763 */
 764	if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
 765		++cmd->SCp.buffer;
 766		--cmd->SCp.buffers_residual;
 767		cmd->SCp.this_residual = cmd->SCp.buffer->length;
 768		cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
 769	}
 770
 771/* Set up hardware registers */
 772
 773	write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, hostdata->sync_xfer[cmd->device->id]);
 774	write_3393_count(hostdata, cmd->SCp.this_residual);
 775	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_BUS);
 776	write1_io(0, IO_FIFO_WRITE);	/* zero counter, assume write */
 777
 778/* Reading is easy. Just issue the command and return - we'll
 779 * get an interrupt later when we have actual data to worry about.
 780 */
 781
 782	if (data_in_dir) {
 783		write1_io(0, IO_FIFO_READ);
 784		if ((hostdata->level2 >= L2_DATA) || (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
 785			write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
 786			write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
 787			hostdata->state = S_RUNNING_LEVEL2;
 788		} else
 789			write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
 790		hostdata->fifo = FI_FIFO_READING;
 791		cmd->SCp.have_data_in = 0;
 792		return;
 793	}
 794
 795/* Writing is more involved - we'll start the WD chip and write as
 796 * much data to the fifo as we can right now. Later interrupts will
 797 * write any bytes that don't make it at this stage.
 798 */
 799
 800	if ((hostdata->level2 >= L2_DATA) || (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
 801		write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
 802		write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
 803		hostdata->state = S_RUNNING_LEVEL2;
 804	} else
 805		write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
 806	hostdata->fifo = FI_FIFO_WRITING;
 807	sp = (unsigned short *) cmd->SCp.ptr;
 808
 809	if ((i = cmd->SCp.this_residual) > IN2000_FIFO_SIZE)
 810		i = IN2000_FIFO_SIZE;
 811	cmd->SCp.have_data_in = i;
 812	i >>= 1;		/* Gulp. We assume this_residual is modulo 2 */
 813	f = hostdata->io_base + IO_FIFO;
 814
 815#ifdef FAST_WRITE_IO
 816
 817	FAST_WRITE2_IO();
 818#else
 819	while (i--)
 820		write2_io(*sp++, IO_FIFO);
 821
 822#endif
 823
 824}
 825
 826
 827/* We need to use spin_lock_irqsave() & spin_unlock_irqrestore() in this
 828 * function in order to work in an SMP environment. (I'd be surprised
 829 * if the driver is ever used by anyone on a real multi-CPU motherboard,
 830 * but it _does_ need to be able to compile and run in an SMP kernel.)
 831 */
 832
 833static irqreturn_t in2000_intr(int irqnum, void *dev_id)
 834{
 835	struct Scsi_Host *instance = dev_id;
 836	struct IN2000_hostdata *hostdata;
 837	Scsi_Cmnd *patch, *cmd;
 838	uchar asr, sr, phs, id, lun, *ucp, msg;
 839	int i, j;
 840	unsigned long length;
 841	unsigned short *sp;
 842	unsigned short f;
 843	unsigned long flags;
 844
 845	hostdata = (struct IN2000_hostdata *) instance->hostdata;
 846
 847/* Get the spin_lock and disable further ints, for SMP */
 848
 849	spin_lock_irqsave(instance->host_lock, flags);
 850
 851#ifdef PROC_STATISTICS
 852	hostdata->int_cnt++;
 853#endif
 854
 855/* The IN2000 card has 2 interrupt sources OR'ed onto its IRQ line - the
 856 * WD3393 chip and the 2k fifo (which is actually a dual-port RAM combined
 857 * with a big logic array, so it's a little different than what you might
 858 * expect). As far as I know, there's no reason that BOTH can't be active
 859 * at the same time, but there's a problem: while we can read the 3393
 860 * to tell if _it_ wants an interrupt, I don't know of a way to ask the
 861 * fifo the same question. The best we can do is check the 3393 and if
 862 * it _isn't_ the source of the interrupt, then we can be pretty sure
 863 * that the fifo is the culprit.
 864 *  UPDATE: I have it on good authority (Bill Earnest) that bit 0 of the
 865 *          IO_FIFO_COUNT register mirrors the fifo interrupt state. I
 866 *          assume that bit clear means interrupt active. As it turns
 867 *          out, the driver really doesn't need to check for this after
 868 *          all, so my remarks above about a 'problem' can safely be
 869 *          ignored. The way the logic is set up, there's no advantage
 870 *          (that I can see) to worrying about it.
 871 *
 872 * It seems that the fifo interrupt signal is negated when we extract
 873 * bytes during read or write bytes during write.
 874 *  - fifo will interrupt when data is moving from it to the 3393, and
 875 *    there are 31 (or less?) bytes left to go. This is sort of short-
 876 *    sighted: what if you don't WANT to do more? In any case, our
 877 *    response is to push more into the fifo - either actual data or
 878 *    dummy bytes if need be. Note that we apparently have to write at
 879 *    least 32 additional bytes to the fifo after an interrupt in order
 880 *    to get it to release the ones it was holding on to - writing fewer
 881 *    than 32 will result in another fifo int.
 882 *  UPDATE: Again, info from Bill Earnest makes this more understandable:
 883 *          32 bytes = two counts of the fifo counter register. He tells
 884 *          me that the fifo interrupt is a non-latching signal derived
 885 *          from a straightforward boolean interpretation of the 7
 886 *          highest bits of the fifo counter and the fifo-read/fifo-write
 887 *          state. Who'd a thought?
 888 */
 889
 890	write1_io(0, IO_LED_ON);
 891	asr = READ_AUX_STAT();
 892	if (!(asr & ASR_INT)) {	/* no WD33c93 interrupt? */
 893
 894/* Ok. This is definitely a FIFO-only interrupt.
 895 *
 896 * If FI_FIFO_READING is set, there are up to 2048 bytes waiting to be read,
 897 * maybe more to come from the SCSI bus. Read as many as we can out of the
 898 * fifo and into memory at the location of SCp.ptr[SCp.have_data_in], and
 899 * update have_data_in afterwards.
 900 *
 901 * If we have FI_FIFO_WRITING, the FIFO has almost run out of bytes to move
 902 * into the WD3393 chip (I think the interrupt happens when there are 31
 903 * bytes left, but it may be fewer...). The 3393 is still waiting, so we
 904 * shove some more into the fifo, which gets things moving again. If the
 905 * original SCSI command specified more than 2048 bytes, there may still
 906 * be some of that data left: fine - use it (from SCp.ptr[SCp.have_data_in]).
 907 * Don't forget to update have_data_in. If we've already written out the
 908 * entire buffer, feed 32 dummy bytes to the fifo - they're needed to
 909 * push out the remaining real data.
 910 *    (Big thanks to Bill Earnest for getting me out of the mud in here.)
 911 */
 912
 913		cmd = (Scsi_Cmnd *) hostdata->connected;	/* assume we're connected */
 914		CHECK_NULL(cmd, "fifo_int")
 915
 916		    if (hostdata->fifo == FI_FIFO_READING) {
 917
 918			DB(DB_FIFO, printk("{R:%02x} ", read1_io(IO_FIFO_COUNT)))
 919
 920			    sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 921			i = read1_io(IO_FIFO_COUNT) & 0xfe;
 922			i <<= 2;	/* # of words waiting in the fifo */
 923			f = hostdata->io_base + IO_FIFO;
 924
 925#ifdef FAST_READ_IO
 926
 927			FAST_READ2_IO();
 928#else
 929			while (i--)
 930				*sp++ = read2_io(IO_FIFO);
 931
 932#endif
 933
 934			i = sp - (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 935			i <<= 1;
 936			cmd->SCp.have_data_in += i;
 937		}
 938
 939		else if (hostdata->fifo == FI_FIFO_WRITING) {
 940
 941			DB(DB_FIFO, printk("{W:%02x} ", read1_io(IO_FIFO_COUNT)))
 942
 943/* If all bytes have been written to the fifo, flush out the stragglers.
 944 * Note that while writing 16 dummy words seems arbitrary, we don't
 945 * have another choice that I can see. What we really want is to read
 946 * the 3393 transfer count register (that would tell us how many bytes
 947 * needed flushing), but the TRANSFER_INFO command hasn't completed
 948 * yet (not enough bytes!) and that register won't be accessible. So,
 949 * we use 16 words - a number obtained through trial and error.
 950 *  UPDATE: Bill says this is exactly what Always does, so there.
 951 *          More thanks due him for help in this section.
 952 */
 953			    if (cmd->SCp.this_residual == cmd->SCp.have_data_in) {
 954				i = 16;
 955				while (i--)	/* write 32 dummy bytes */
 956					write2_io(0, IO_FIFO);
 957			}
 958
 959/* If there are still bytes left in the SCSI buffer, write as many as we
 960 * can out to the fifo.
 961 */
 962
 963			else {
 964				sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 965				i = cmd->SCp.this_residual - cmd->SCp.have_data_in;	/* bytes yet to go */
 966				j = read1_io(IO_FIFO_COUNT) & 0xfe;
 967				j <<= 2;	/* how many words the fifo has room for */
 968				if ((j << 1) > i)
 969					j = (i >> 1);
 970				while (j--)
 971					write2_io(*sp++, IO_FIFO);
 972
 973				i = sp - (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
 974				i <<= 1;
 975				cmd->SCp.have_data_in += i;
 976			}
 977		}
 978
 979		else {
 980			printk("*** Spurious FIFO interrupt ***");
 981		}
 982
 983		write1_io(0, IO_LED_OFF);
 984
 985/* release the SMP spin_lock and restore irq state */
 986		spin_unlock_irqrestore(instance->host_lock, flags);
 987		return IRQ_HANDLED;
 988	}
 989
 990/* This interrupt was triggered by the WD33c93 chip. The fifo interrupt
 991 * may also be asserted, but we don't bother to check it: we get more
 992 * detailed info from FIFO_READING and FIFO_WRITING (see below).
 993 */
 994
 995	cmd = (Scsi_Cmnd *) hostdata->connected;	/* assume we're connected */
 996	sr = read_3393(hostdata, WD_SCSI_STATUS);	/* clear the interrupt */
 997	phs = read_3393(hostdata, WD_COMMAND_PHASE);
 998
 999	if (!cmd && (sr != CSR_RESEL_AM && sr != CSR_TIMEOUT && sr != CSR_SELECT)) {
1000		printk("\nNR:wd-intr-1\n");
1001		write1_io(0, IO_LED_OFF);
1002
1003/* release the SMP spin_lock and restore irq state */
1004		spin_unlock_irqrestore(instance->host_lock, flags);
1005		return IRQ_HANDLED;
1006	}
1007
1008	DB(DB_INTR, printk("{%02x:%02x-", asr, sr))
1009
1010/* After starting a FIFO-based transfer, the next _WD3393_ interrupt is
1011 * guaranteed to be in response to the completion of the transfer.
1012 * If we were reading, there's probably data in the fifo that needs
1013 * to be copied into RAM - do that here. Also, we have to update
1014 * 'this_residual' and 'ptr' based on the contents of the
1015 * TRANSFER_COUNT register, in case the device decided to do an
1016 * intermediate disconnect (a device may do this if it has to
1017 * do a seek,  or just to be nice and let other devices have
1018 * some bus time during long transfers).
1019 * After doing whatever is necessary with the fifo, we go on and
1020 * service the WD3393 interrupt normally.
1021 */
1022	    if (hostdata->fifo == FI_FIFO_READING) {
1023
1024/* buffer index = start-of-buffer + #-of-bytes-already-read */
1025
1026		sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
1027
1028/* bytes remaining in fifo = (total-wanted - #-not-got) - #-already-read */
1029
1030		i = (cmd->SCp.this_residual - read_3393_count(hostdata)) - cmd->SCp.have_data_in;
1031		i >>= 1;	/* Gulp. We assume this will always be modulo 2 */
1032		f = hostdata->io_base + IO_FIFO;
1033
1034#ifdef FAST_READ_IO
1035
1036		FAST_READ2_IO();
1037#else
1038		while (i--)
1039			*sp++ = read2_io(IO_FIFO);
1040
1041#endif
1042
1043		hostdata->fifo = FI_FIFO_UNUSED;
1044		length = cmd->SCp.this_residual;
1045		cmd->SCp.this_residual = read_3393_count(hostdata);
1046		cmd->SCp.ptr += (length - cmd->SCp.this_residual);
1047
1048		DB(DB_TRANSFER, printk("(%p,%d)", cmd->SCp.ptr, cmd->SCp.this_residual))
1049
1050	}
1051
1052	else if (hostdata->fifo == FI_FIFO_WRITING) {
1053		hostdata->fifo = FI_FIFO_UNUSED;
1054		length = cmd->SCp.this_residual;
1055		cmd->SCp.this_residual = read_3393_count(hostdata);
1056		cmd->SCp.ptr += (length - cmd->SCp.this_residual);
1057
1058		DB(DB_TRANSFER, printk("(%p,%d)", cmd->SCp.ptr, cmd->SCp.this_residual))
1059
1060	}
1061
1062/* Respond to the specific WD3393 interrupt - there are quite a few! */
1063
1064	switch (sr) {
1065
1066	case CSR_TIMEOUT:
1067		DB(DB_INTR, printk("TIMEOUT"))
1068
1069		    if (hostdata->state == S_RUNNING_LEVEL2)
1070			hostdata->connected = NULL;
1071		else {
1072			cmd = (Scsi_Cmnd *) hostdata->selecting;	/* get a valid cmd */
1073			CHECK_NULL(cmd, "csr_timeout")
1074			    hostdata->selecting = NULL;
1075		}
1076
1077		cmd->result = DID_NO_CONNECT << 16;
1078		hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1079		hostdata->state = S_UNCONNECTED;
1080		cmd->scsi_done(cmd);
1081
1082/* We are not connected to a target - check to see if there
1083 * are commands waiting to be executed.
1084 */
1085
1086		in2000_execute(instance);
1087		break;
1088
1089
1090/* Note: this interrupt should not occur in a LEVEL2 command */
1091
1092	case CSR_SELECT:
1093		DB(DB_INTR, printk("SELECT"))
1094		    hostdata->connected = cmd = (Scsi_Cmnd *) hostdata->selecting;
1095		CHECK_NULL(cmd, "csr_select")
1096		    hostdata->selecting = NULL;
1097
1098		/* construct an IDENTIFY message with correct disconnect bit */
1099
1100		hostdata->outgoing_msg[0] = (0x80 | 0x00 | cmd->device->lun);
1101		if (cmd->SCp.phase)
1102			hostdata->outgoing_msg[0] |= 0x40;
1103
1104		if (hostdata->sync_stat[cmd->device->id] == SS_FIRST) {
1105#ifdef SYNC_DEBUG
1106			printk(" sending SDTR ");
1107#endif
1108
1109			hostdata->sync_stat[cmd->device->id] = SS_WAITING;
1110
1111			/* tack on a 2nd message to ask about synchronous transfers */
1112
1113			hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
1114			hostdata->outgoing_msg[2] = 3;
1115			hostdata->outgoing_msg[3] = EXTENDED_SDTR;
1116			hostdata->outgoing_msg[4] = OPTIMUM_SX_PER / 4;
1117			hostdata->outgoing_msg[5] = OPTIMUM_SX_OFF;
1118			hostdata->outgoing_len = 6;
1119		} else
1120			hostdata->outgoing_len = 1;
1121
1122		hostdata->state = S_CONNECTED;
1123		break;
1124
1125
1126	case CSR_XFER_DONE | PHS_DATA_IN:
1127	case CSR_UNEXP | PHS_DATA_IN:
1128	case CSR_SRV_REQ | PHS_DATA_IN:
1129		DB(DB_INTR, printk("IN-%d.%d", cmd->SCp.this_residual, cmd->SCp.buffers_residual))
1130		    transfer_bytes(cmd, DATA_IN_DIR);
1131		if (hostdata->state != S_RUNNING_LEVEL2)
1132			hostdata->state = S_CONNECTED;
1133		break;
1134
1135
1136	case CSR_XFER_DONE | PHS_DATA_OUT:
1137	case CSR_UNEXP | PHS_DATA_OUT:
1138	case CSR_SRV_REQ | PHS_DATA_OUT:
1139		DB(DB_INTR, printk("OUT-%d.%d", cmd->SCp.this_residual, cmd->SCp.buffers_residual))
1140		    transfer_bytes(cmd, DATA_OUT_DIR);
1141		if (hostdata->state != S_RUNNING_LEVEL2)
1142			hostdata->state = S_CONNECTED;
1143		break;
1144
1145
1146/* Note: this interrupt should not occur in a LEVEL2 command */
1147
1148	case CSR_XFER_DONE | PHS_COMMAND:
1149	case CSR_UNEXP | PHS_COMMAND:
1150	case CSR_SRV_REQ | PHS_COMMAND:
1151		DB(DB_INTR, printk("CMND-%02x", cmd->cmnd[0]))
1152		    transfer_pio(cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR, hostdata);
1153		hostdata->state = S_CONNECTED;
1154		break;
1155
1156
1157	case CSR_XFER_DONE | PHS_STATUS:
1158	case CSR_UNEXP | PHS_STATUS:
1159	case CSR_SRV_REQ | PHS_STATUS:
1160		DB(DB_INTR, printk("STATUS="))
1161
1162		    cmd->SCp.Status = read_1_byte(hostdata);
1163		DB(DB_INTR, printk("%02x", cmd->SCp.Status))
1164		    if (hostdata->level2 >= L2_BASIC) {
1165			sr = read_3393(hostdata, WD_SCSI_STATUS);	/* clear interrupt */
1166			hostdata->state = S_RUNNING_LEVEL2;
1167			write_3393(hostdata, WD_COMMAND_PHASE, 0x50);
1168			write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1169		} else {
1170			hostdata->state = S_CONNECTED;
1171		}
1172		break;
1173
1174
1175	case CSR_XFER_DONE | PHS_MESS_IN:
1176	case CSR_UNEXP | PHS_MESS_IN:
1177	case CSR_SRV_REQ | PHS_MESS_IN:
1178		DB(DB_INTR, printk("MSG_IN="))
1179
1180		    msg = read_1_byte(hostdata);
1181		sr = read_3393(hostdata, WD_SCSI_STATUS);	/* clear interrupt */
1182
1183		hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
1184		if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
1185			msg = EXTENDED_MESSAGE;
1186		else
1187			hostdata->incoming_ptr = 0;
1188
1189		cmd->SCp.Message = msg;
1190		switch (msg) {
1191
1192		case COMMAND_COMPLETE:
1193			DB(DB_INTR, printk("CCMP"))
1194			    write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1195			hostdata->state = S_PRE_CMP_DISC;
1196			break;
1197
1198		case SAVE_POINTERS:
1199			DB(DB_INTR, printk("SDP"))
1200			    write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1201			hostdata->state = S_CONNECTED;
1202			break;
1203
1204		case RESTORE_POINTERS:
1205			DB(DB_INTR, printk("RDP"))
1206			    if (hostdata->level2 >= L2_BASIC) {
1207				write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
1208				write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1209				hostdata->state = S_RUNNING_LEVEL2;
1210			} else {
1211				write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1212				hostdata->state = S_CONNECTED;
1213			}
1214			break;
1215
1216		case DISCONNECT:
1217			DB(DB_INTR, printk("DIS"))
1218			    cmd->device->disconnect = 1;
1219			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1220			hostdata->state = S_PRE_TMP_DISC;
1221			break;
1222
1223		case MESSAGE_REJECT:
1224			DB(DB_INTR, printk("REJ"))
1225#ifdef SYNC_DEBUG
1226			    printk("-REJ-");
1227#endif
1228			if (hostdata->sync_stat[cmd->device->id] == SS_WAITING)
1229				hostdata->sync_stat[cmd->device->id] = SS_SET;
1230			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1231			hostdata->state = S_CONNECTED;
1232			break;
1233
1234		case EXTENDED_MESSAGE:
1235			DB(DB_INTR, printk("EXT"))
1236
1237			    ucp = hostdata->incoming_msg;
1238
1239#ifdef SYNC_DEBUG
1240			printk("%02x", ucp[hostdata->incoming_ptr]);
1241#endif
1242			/* Is this the last byte of the extended message? */
1243
1244			if ((hostdata->incoming_ptr >= 2) && (hostdata->incoming_ptr == (ucp[1] + 1))) {
1245
1246				switch (ucp[2]) {	/* what's the EXTENDED code? */
1247				case EXTENDED_SDTR:
1248					id = calc_sync_xfer(ucp[3], ucp[4]);
1249					if (hostdata->sync_stat[cmd->device->id] != SS_WAITING) {
1250
1251/* A device has sent an unsolicited SDTR message; rather than go
1252 * through the effort of decoding it and then figuring out what
1253 * our reply should be, we're just gonna say that we have a
1254 * synchronous fifo depth of 0. This will result in asynchronous
1255 * transfers - not ideal but so much easier.
1256 * Actually, this is OK because it assures us that if we don't
1257 * specifically ask for sync transfers, we won't do any.
1258 */
1259
1260						write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1261						hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
1262						hostdata->outgoing_msg[1] = 3;
1263						hostdata->outgoing_msg[2] = EXTENDED_SDTR;
1264						hostdata->outgoing_msg[3] = hostdata->default_sx_per / 4;
1265						hostdata->outgoing_msg[4] = 0;
1266						hostdata->outgoing_len = 5;
1267						hostdata->sync_xfer[cmd->device->id] = calc_sync_xfer(hostdata->default_sx_per / 4, 0);
1268					} else {
1269						hostdata->sync_xfer[cmd->device->id] = id;
1270					}
1271#ifdef SYNC_DEBUG
1272					printk("sync_xfer=%02x", hostdata->sync_xfer[cmd->device->id]);
1273#endif
1274					hostdata->sync_stat[cmd->device->id] = SS_SET;
1275					write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1276					hostdata->state = S_CONNECTED;
1277					break;
1278				case EXTENDED_WDTR:
1279					write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1280					printk("sending WDTR ");
1281					hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
1282					hostdata->outgoing_msg[1] = 2;
1283					hostdata->outgoing_msg[2] = EXTENDED_WDTR;
1284					hostdata->outgoing_msg[3] = 0;	/* 8 bit transfer width */
1285					hostdata->outgoing_len = 4;
1286					write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1287					hostdata->state = S_CONNECTED;
1288					break;
1289				default:
1290					write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1291					printk("Rejecting Unknown Extended Message(%02x). ", ucp[2]);
1292					hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1293					hostdata->outgoing_len = 1;
1294					write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1295					hostdata->state = S_CONNECTED;
1296					break;
1297				}
1298				hostdata->incoming_ptr = 0;
1299			}
1300
1301			/* We need to read more MESS_IN bytes for the extended message */
1302
1303			else {
1304				hostdata->incoming_ptr++;
1305				write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1306				hostdata->state = S_CONNECTED;
1307			}
1308			break;
1309
1310		default:
1311			printk("Rejecting Unknown Message(%02x) ", msg);
1312			write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN);	/* want MESS_OUT */
1313			hostdata->outgoing_msg[0] = MESSAGE_REJECT;
1314			hostdata->outgoing_len = 1;
1315			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1316			hostdata->state = S_CONNECTED;
1317		}
1318		break;
1319
1320
1321/* Note: this interrupt will occur only after a LEVEL2 command */
1322
1323	case CSR_SEL_XFER_DONE:
1324
1325/* Make sure that reselection is enabled at this point - it may
1326 * have been turned off for the command that just completed.
1327 */
1328
1329		write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
1330		if (phs == 0x60) {
1331			DB(DB_INTR, printk("SX-DONE"))
1332			    cmd->SCp.Message = COMMAND_COMPLETE;
1333			lun = read_3393(hostdata, WD_TARGET_LUN);
1334			DB(DB_INTR, printk(":%d.%d", cmd->SCp.Status, lun))
1335			    hostdata->connected = NULL;
1336			hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1337			hostdata->state = S_UNCONNECTED;
1338			if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
1339				cmd->SCp.Status = lun;
1340			if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1341				cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1342			else
1343				cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1344			cmd->scsi_done(cmd);
1345
1346/* We are no longer connected to a target - check to see if
1347 * there are commands waiting to be executed.
1348 */
1349
1350			in2000_execute(instance);
1351		} else {
1352			printk("%02x:%02x:%02x: Unknown SEL_XFER_DONE phase!!---", asr, sr, phs);
1353		}
1354		break;
1355
1356
1357/* Note: this interrupt will occur only after a LEVEL2 command */
1358
1359	case CSR_SDP:
1360		DB(DB_INTR, printk("SDP"))
1361		    hostdata->state = S_RUNNING_LEVEL2;
1362		write_3393(hostdata, WD_COMMAND_PHASE, 0x41);
1363		write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1364		break;
1365
1366
1367	case CSR_XFER_DONE | PHS_MESS_OUT:
1368	case CSR_UNEXP | PHS_MESS_OUT:
1369	case CSR_SRV_REQ | PHS_MESS_OUT:
1370		DB(DB_INTR, printk("MSG_OUT="))
1371
1372/* To get here, we've probably requested MESSAGE_OUT and have
1373 * already put the correct bytes in outgoing_msg[] and filled
1374 * in outgoing_len. We simply send them out to the SCSI bus.
1375 * Sometimes we get MESSAGE_OUT phase when we're not expecting
1376 * it - like when our SDTR message is rejected by a target. Some
1377 * targets send the REJECT before receiving all of the extended
1378 * message, and then seem to go back to MESSAGE_OUT for a byte
1379 * or two. Not sure why, or if I'm doing something wrong to
1380 * cause this to happen. Regardless, it seems that sending
1381 * NOP messages in these situations results in no harm and
1382 * makes everyone happy.
1383 */
1384		    if (hostdata->outgoing_len == 0) {
1385			hostdata->outgoing_len = 1;
1386			hostdata->outgoing_msg[0] = NOP;
1387		}
1388		transfer_pio(hostdata->outgoing_msg, hostdata->outgoing_len, DATA_OUT_DIR, hostdata);
1389		DB(DB_INTR, printk("%02x", hostdata->outgoing_msg[0]))
1390		    hostdata->outgoing_len = 0;
1391		hostdata->state = S_CONNECTED;
1392		break;
1393
1394
1395	case CSR_UNEXP_DISC:
1396
1397/* I think I've seen this after a request-sense that was in response
1398 * to an error condition, but not sure. We certainly need to do
1399 * something when we get this interrupt - the question is 'what?'.
1400 * Let's think positively, and assume some command has finished
1401 * in a legal manner (like a command that provokes a request-sense),
1402 * so we treat it as a normal command-complete-disconnect.
1403 */
1404
1405
1406/* Make sure that reselection is enabled at this point - it may
1407 * have been turned off for the command that just completed.
1408 */
1409
1410		write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
1411		if (cmd == NULL) {
1412			printk(" - Already disconnected! ");
1413			hostdata->state = S_UNCONNECTED;
1414
1415/* release the SMP spin_lock and restore irq state */
1416			spin_unlock_irqrestore(instance->host_lock, flags);
1417			return IRQ_HANDLED;
1418		}
1419		DB(DB_INTR, printk("UNEXP_DISC"))
1420		    hostdata->connected = NULL;
1421		hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1422		hostdata->state = S_UNCONNECTED;
1423		if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1424			cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1425		else
1426			cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1427		cmd->scsi_done(cmd);
1428
1429/* We are no longer connected to a target - check to see if
1430 * there are commands waiting to be executed.
1431 */
1432
1433		in2000_execute(instance);
1434		break;
1435
1436
1437	case CSR_DISC:
1438
1439/* Make sure that reselection is enabled at this point - it may
1440 * have been turned off for the command that just completed.
1441 */
1442
1443		write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
1444		DB(DB_INTR, printk("DISC"))
1445		    if (cmd == NULL) {
1446			printk(" - Already disconnected! ");
1447			hostdata->state = S_UNCONNECTED;
1448		}
1449		switch (hostdata->state) {
1450		case S_PRE_CMP_DISC:
1451			hostdata->connected = NULL;
1452			hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1453			hostdata->state = S_UNCONNECTED;
1454			DB(DB_INTR, printk(":%d", cmd->SCp.Status))
1455			    if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
1456				cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
1457			else
1458				cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
1459			cmd->scsi_done(cmd);
1460			break;
1461		case S_PRE_TMP_DISC:
1462		case S_RUNNING_LEVEL2:
1463			cmd->host_scribble = (uchar *) hostdata->disconnected_Q;
1464			hostdata->disconnected_Q = cmd;
1465			hostdata->connected = NULL;
1466			hostdata->state = S_UNCONNECTED;
1467
1468#ifdef PROC_STATISTICS
1469			hostdata->disc_done_cnt[cmd->device->id]++;
1470#endif
1471
1472			break;
1473		default:
1474			printk("*** Unexpected DISCONNECT interrupt! ***");
1475			hostdata->state = S_UNCONNECTED;
1476		}
1477
1478/* We are no longer connected to a target - check to see if
1479 * there are commands waiting to be executed.
1480 */
1481
1482		in2000_execute(instance);
1483		break;
1484
1485
1486	case CSR_RESEL_AM:
1487		DB(DB_INTR, printk("RESEL"))
1488
1489		    /* First we have to make sure this reselection didn't */
1490		    /* happen during Arbitration/Selection of some other device. */
1491		    /* If yes, put losing command back on top of input_Q. */
1492		    if (hostdata->level2 <= L2_NONE) {
1493
1494			if (hostdata->selecting) {
1495				cmd = (Scsi_Cmnd *) hostdata->selecting;
1496				hostdata->selecting = NULL;
1497				hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1498				cmd->host_scribble = (uchar *) hostdata->input_Q;
1499				hostdata->input_Q = cmd;
1500			}
1501		}
1502
1503		else {
1504
1505			if (cmd) {
1506				if (phs == 0x00) {
1507					hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1508					cmd->host_scribble = (uchar *) hostdata->input_Q;
1509					hostdata->input_Q = cmd;
1510				} else {
1511					printk("---%02x:%02x:%02x-TROUBLE: Intrusive ReSelect!---", asr, sr, phs);
1512					while (1)
1513						printk("\r");
1514				}
1515			}
1516
1517		}
1518
1519		/* OK - find out which device reselected us. */
1520
1521		id = read_3393(hostdata, WD_SOURCE_ID);
1522		id &= SRCID_MASK;
1523
1524		/* and extract the lun from the ID message. (Note that we don't
1525		 * bother to check for a valid message here - I guess this is
1526		 * not the right way to go, but....)
1527		 */
1528
1529		lun = read_3393(hostdata, WD_DATA);
1530		if (hostdata->level2 < L2_RESELECT)
1531			write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
1532		lun &= 7;
1533
1534		/* Now we look for the command that's reconnecting. */
1535
1536		cmd = (Scsi_Cmnd *) hostdata->disconnected_Q;
1537		patch = NULL;
1538		while (cmd) {
1539			if (id == cmd->device->id && lun == cmd->device->lun)
1540				break;
1541			patch = cmd;
1542			cmd = (Scsi_Cmnd *) cmd->host_scribble;
1543		}
1544
1545		/* Hmm. Couldn't find a valid command.... What to do? */
1546
1547		if (!cmd) {
1548			printk("---TROUBLE: target %d.%d not in disconnect queue---", id, lun);
1549			break;
1550		}
1551
1552		/* Ok, found the command - now start it up again. */
1553
1554		if (patch)
1555			patch->host_scribble = cmd->host_scribble;
1556		else
1557			hostdata->disconnected_Q = (Scsi_Cmnd *) cmd->host_scribble;
1558		hostdata->connected = cmd;
1559
1560		/* We don't need to worry about 'initialize_SCp()' or 'hostdata->busy[]'
1561		 * because these things are preserved over a disconnect.
1562		 * But we DO need to fix the DPD bit so it's correct for this command.
1563		 */
1564
1565		if (is_dir_out(cmd))
1566			write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id);
1567		else
1568			write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
1569		if (hostdata->level2 >= L2_RESELECT) {
1570			write_3393_count(hostdata, 0);	/* we want a DATA_PHASE interrupt */
1571			write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
1572			write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
1573			hostdata->state = S_RUNNING_LEVEL2;
1574		} else
1575			hostdata->state = S_CONNECTED;
1576
1577		    break;
1578
1579	default:
1580		printk("--UNKNOWN INTERRUPT:%02x:%02x:%02x--", asr, sr, phs);
1581	}
1582
1583	write1_io(0, IO_LED_OFF);
1584
1585	DB(DB_INTR, printk("} "))
1586
1587/* release the SMP spin_lock and restore irq state */
1588	    spin_unlock_irqrestore(instance->host_lock, flags);
1589	return IRQ_HANDLED;
1590}
1591
1592
1593
1594#define RESET_CARD         0
1595#define RESET_CARD_AND_BUS 1
1596#define B_FLAG 0x80
1597
1598/*
1599 *	Caller must hold instance lock!
1600 */
1601
1602static int reset_hardware(struct Scsi_Host *instance, int type)
1603{
1604	struct IN2000_hostdata *hostdata;
1605	int qt, x;
1606
1607	hostdata = (struct IN2000_hostdata *) instance->hostdata;
1608
1609	write1_io(0, IO_LED_ON);
1610	if (type == RESET_CARD_AND_BUS) {
1611		write1_io(0, IO_CARD_RESET);
1612		x = read1_io(IO_HARDWARE);
1613	}
1614	x = read_3393(hostdata, WD_SCSI_STATUS);	/* clear any WD intrpt */
1615	write_3393(hostdata, WD_OWN_ID, instance->this_id | OWNID_EAF | OWNID_RAF | OWNID_FS_8);
1616	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1617	write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, calc_sync_xfer(hostdata->default_sx_per / 4, DEFAULT_SX_OFF));
1618
1619	write1_io(0, IO_FIFO_WRITE);	/* clear fifo counter */
1620	write1_io(0, IO_FIFO_READ);	/* start fifo out in read mode */
1621	write_3393(hostdata, WD_COMMAND, WD_CMD_RESET);
1622	/* FIXME: timeout ?? */
1623	while (!(READ_AUX_STAT() & ASR_INT))
1624		cpu_relax();	/* wait for RESET to complete */
1625
1626	x = read_3393(hostdata, WD_SCSI_STATUS);	/* clear interrupt */
1627
1628	write_3393(hostdata, WD_QUEUE_TAG, 0xa5);	/* any random number */
1629	qt = read_3393(hostdata, WD_QUEUE_TAG);
1630	if (qt == 0xa5) {
1631		x |= B_FLAG;
1632		write_3393(hostdata, WD_QUEUE_TAG, 0);
1633	}
1634	write_3393(hostdata, WD_TIMEOUT_PERIOD, TIMEOUT_PERIOD_VALUE);
1635	write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1636	write1_io(0, IO_LED_OFF);
1637	return x;
1638}
1639
1640
1641
1642static int in2000_bus_reset(Scsi_Cmnd * cmd)
1643{
1644	struct Scsi_Host *instance;
1645	struct IN2000_hostdata *hostdata;
1646	int x;
1647	unsigned long flags;
1648
1649	instance = cmd->device->host;
1650	hostdata = (struct IN2000_hostdata *) instance->hostdata;
1651
1652	printk(KERN_WARNING "scsi%d: Reset. ", instance->host_no);
1653
1654	spin_lock_irqsave(instance->host_lock, flags);
1655
1656	/* do scsi-reset here */
1657	reset_hardware(instance, RESET_CARD_AND_BUS);
1658	for (x = 0; x < 8; x++) {
1659		hostdata->busy[x] = 0;
1660		hostdata->sync_xfer[x] = calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
1661		hostdata->sync_stat[x] = SS_UNSET;	/* using default sync values */
1662	}
1663	hostdata->input_Q = NULL;
1664	hostdata->selecting = NULL;
1665	hostdata->connected = NULL;
1666	hostdata->disconnected_Q = NULL;
1667	hostdata->state = S_UNCONNECTED;
1668	hostdata->fifo = FI_FIFO_UNUSED;
1669	hostdata->incoming_ptr = 0;
1670	hostdata->outgoing_len = 0;
1671
1672	cmd->result = DID_RESET << 16;
1673
1674	spin_unlock_irqrestore(instance->host_lock, flags);
1675	return SUCCESS;
1676}
1677
1678static int __in2000_abort(Scsi_Cmnd * cmd)
1679{
1680	struct Scsi_Host *instance;
1681	struct IN2000_hostdata *hostdata;
1682	Scsi_Cmnd *tmp, *prev;
1683	uchar sr, asr;
1684	unsigned long timeout;
1685
1686	instance = cmd->device->host;
1687	hostdata = (struct IN2000_hostdata *) instance->hostdata;
1688
1689	printk(KERN_DEBUG "scsi%d: Abort-", instance->host_no);
1690	printk("(asr=%02x,count=%ld,resid=%d,buf_resid=%d,have_data=%d,FC=%02x)- ", READ_AUX_STAT(), read_3393_count(hostdata), cmd->SCp.this_residual, cmd->SCp.buffers_residual, cmd->SCp.have_data_in, read1_io(IO_FIFO_COUNT));
1691
1692/*
1693 * Case 1 : If the command hasn't been issued yet, we simply remove it
1694 *     from the inout_Q.
1695 */
1696
1697	tmp = (Scsi_Cmnd *) hostdata->input_Q;
1698	prev = NULL;
1699	while (tmp) {
1700		if (tmp == cmd) {
1701			if (prev)
1702				prev->host_scribble = cmd->host_scribble;
1703			cmd->host_scribble = NULL;
1704			cmd->result = DID_ABORT << 16;
1705			printk(KERN_WARNING "scsi%d: Abort - removing command from input_Q. ", instance->host_no);
1706			cmd->scsi_done(cmd);
1707			return SUCCESS;
1708		}
1709		prev = tmp;
1710		tmp = (Scsi_Cmnd *) tmp->host_scribble;
1711	}
1712
1713/*
1714 * Case 2 : If the command is connected, we're going to fail the abort
1715 *     and let the high level SCSI driver retry at a later time or
1716 *     issue a reset.
1717 *
1718 *     Timeouts, and therefore aborted commands, will be highly unlikely
1719 *     and handling them cleanly in this situation would make the common
1720 *     case of noresets less efficient, and would pollute our code.  So,
1721 *     we fail.
1722 */
1723
1724	if (hostdata->connected == cmd) {
1725
1726		printk(KERN_WARNING "scsi%d: Aborting connected command - ", instance->host_no);
1727
1728		printk("sending wd33c93 ABORT command - ");
1729		write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
1730		write_3393_cmd(hostdata, WD_CMD_ABORT);
1731
1732/* Now we have to attempt to flush out the FIFO... */
1733
1734		printk("flushing fifo - ");
1735		timeout = 1000000;
1736		do {
1737			asr = READ_AUX_STAT();
1738			if (asr & ASR_DBR)
1739				read_3393(hostdata, WD_DATA);
1740		} while (!(asr & ASR_INT) && timeout-- > 0);
1741		sr = read_3393(hostdata, WD_SCSI_STATUS);
1742		printk("asr=%02x, sr=%02x, %ld bytes un-transferred (timeout=%ld) - ", asr, sr, read_3393_count(hostdata), timeout);
1743
1744		/*
1745		 * Abort command processed.
1746		 * Still connected.
1747		 * We must disconnect.
1748		 */
1749
1750		printk("sending wd33c93 DISCONNECT command - ");
1751		write_3393_cmd(hostdata, WD_CMD_DISCONNECT);
1752
1753		timeout = 1000000;
1754		asr = READ_AUX_STAT();
1755		while ((asr & ASR_CIP) && timeout-- > 0)
1756			asr = READ_AUX_STAT();
1757		sr = read_3393(hostdata, WD_SCSI_STATUS);
1758		printk("asr=%02x, sr=%02x.", asr, sr);
1759
1760		hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
1761		hostdata->connected = NULL;
1762		hostdata->state = S_UNCONNECTED;
1763		cmd->result = DID_ABORT << 16;
1764		cmd->scsi_done(cmd);
1765
1766		in2000_execute(instance);
1767
1768		return SUCCESS;
1769	}
1770
1771/*
1772 * Case 3: If the command is currently disconnected from the bus,
1773 * we're not going to expend much effort here: Let's just return
1774 * an ABORT_SNOOZE and hope for the best...
1775 */
1776
1777	for (tmp = (Scsi_Cmnd *) hostdata->disconnected_Q; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
1778		if (cmd == tmp) {
1779			printk(KERN_DEBUG "scsi%d: unable to abort disconnected command.\n", instance->host_no);
1780			return FAILED;
1781		}
1782
1783/*
1784 * Case 4 : If we reached this point, the command was not found in any of
1785 *     the queues.
1786 *
1787 * We probably reached this point because of an unlikely race condition
1788 * between the command completing successfully and the abortion code,
1789 * so we won't panic, but we will notify the user in case something really
1790 * broke.
1791 */
1792
1793	in2000_execute(instance);
1794
1795	printk("scsi%d: warning : SCSI command probably completed successfully" "         before abortion. ", instance->host_no);
1796	return SUCCESS;
1797}
1798
1799static int in2000_abort(Scsi_Cmnd * cmd)
1800{
1801	int rc;
1802
1803	spin_lock_irq(cmd->device->host->host_lock);
1804	rc = __in2000_abort(cmd);
1805	spin_unlock_irq(cmd->device->host->host_lock);
1806
1807	return rc;
1808}
1809
1810
1811#define MAX_IN2000_HOSTS 3
1812#define MAX_SETUP_ARGS ARRAY_SIZE(setup_args)
1813#define SETUP_BUFFER_SIZE 200
1814static char setup_buffer[SETUP_BUFFER_SIZE];
1815static char setup_used[MAX_SETUP_ARGS];
1816static int done_setup = 0;
1817
1818static void __init in2000_setup(char *str, int *ints)
1819{
1820	int i;
1821	char *p1, *p2;
1822
1823	strlcpy(setup_buffer, str, SETUP_BUFFER_SIZE);
1824	p1 = setup_buffer;
1825	i = 0;
1826	while (*p1 && (i < MAX_SETUP_ARGS)) {
1827		p2 = strchr(p1, ',');
1828		if (p2) {
1829			*p2 = '\0';
1830			if (p1 != p2)
1831				setup_args[i] = p1;
1832			p1 = p2 + 1;
1833			i++;
1834		} else {
1835			setup_args[i] = p1;
1836			break;
1837		}
1838	}
1839	for (i = 0; i < MAX_SETUP_ARGS; i++)
1840		setup_used[i] = 0;
1841	done_setup = 1;
1842}
1843
1844
1845/* check_setup_args() returns index if key found, 0 if not
1846 */
1847
1848static int __init check_setup_args(char *key, int *val, char *buf)
1849{
1850	int x;
1851	char *cp;
1852
1853	for (x = 0; x < MAX_SETUP_ARGS; x++) {
1854		if (setup_used[x])
1855			continue;
1856		if (!strncmp(setup_args[x], key, strlen(key)))
1857			break;
1858	}
1859	if (x == MAX_SETUP_ARGS)
1860		return 0;
1861	setup_used[x] = 1;
1862	cp = setup_args[x] + strlen(key);
1863	*val = -1;
1864	if (*cp != ':')
1865		return ++x;
1866	cp++;
1867	if ((*cp >= '0') && (*cp <= '9')) {
1868		*val = simple_strtoul(cp, NULL, 0);
1869	}
1870	return ++x;
1871}
1872
1873
1874
1875/* The "correct" (ie portable) way to access memory-mapped hardware
1876 * such as the IN2000 EPROM and dip switch is through the use of
1877 * special macros declared in 'asm/io.h'. We use readb() and readl()
1878 * when reading from the card's BIOS area in in2000_detect().
1879 */
1880static u32 bios_tab[] in2000__INITDATA = {
1881	0xc8000,
1882	0xd0000,
1883	0xd8000,
1884	0
1885};
1886
1887static unsigned short base_tab[] in2000__INITDATA = {
1888	0x220,
1889	0x200,
1890	0x110,
1891	0x100,
1892};
1893
1894static int int_tab[] in2000__INITDATA = {
1895	15,
1896	14,
1897	11,
1898	10
1899};
1900
1901static int probe_bios(u32 addr, u32 *s1, uchar *switches)
1902{
1903	void __iomem *p = ioremap(addr, 0x34);
1904	if (!p)
1905		return 0;
1906	*s1 = readl(p + 0x10);
1907	if (*s1 == 0x41564f4e || readl(p + 0x30) == 0x61776c41) {
1908		/* Read the switch image that's mapped into EPROM space */
1909		*switches = ~readb(p + 0x20);
1910		iounmap(p);
1911		return 1;
1912	}
1913	iounmap(p);
1914	return 0;
1915}
1916
1917static int __init in2000_detect(struct scsi_host_template * tpnt)
1918{
1919	struct Scsi_Host *instance;
1920	struct IN2000_hostdata *hostdata;
1921	int detect_count;
1922	int bios;
1923	int x;
1924	unsigned short base;
1925	uchar switches;
1926	uchar hrev;
1927	unsigned long flags;
1928	int val;
1929	char buf[32];
1930
1931/* Thanks to help from Bill Earnest, probing for IN2000 cards is a
1932 * pretty straightforward and fool-proof operation. There are 3
1933 * possible locations for the IN2000 EPROM in memory space - if we
1934 * find a BIOS signature, we can read the dip switch settings from
1935 * the byte at BIOS+32 (shadowed in by logic on the card). From 2
1936 * of the switch bits we get the card's address in IO space. There's
1937 * an image of the dip switch there, also, so we have a way to back-
1938 * check that this really is an IN2000 card. Very nifty. Use the
1939 * 'ioport:xx' command-line parameter if your BIOS EPROM is absent
1940 * or disabled.
1941 */
1942
1943	if (!done_setup && setup_strings)
1944		in2000_setup(setup_strings, NULL);
1945
1946	detect_count = 0;
1947	for (bios = 0; bios_tab[bios]; bios++) {
1948		u32 s1 = 0;
1949		if (check_setup_args("ioport", &val, buf)) {
1950			base = val;
1951			switches = ~inb(base + IO_SWITCHES) & 0xff;
1952			printk("Forcing IN2000 detection at IOport 0x%x ", base);
1953			bios = 2;
1954		}
1955/*
1956 * There have been a couple of BIOS versions with different layouts
1957 * for the obvious ID strings. We look for the 2 most common ones and
1958 * hope that they cover all the cases...
1959 */
1960		else if (probe_bios(bios_tab[bios], &s1, &switches)) {
1961			printk("Found IN2000 BIOS at 0x%x ", (unsigned int) bios_tab[bios]);
1962
1963/* Find out where the IO space is */
1964
1965			x = switches & (SW_ADDR0 | SW_ADDR1);
1966			base = base_tab[x];
1967
1968/* Check for the IN2000 signature in IO space. */
1969
1970			x = ~inb(base + IO_SWITCHES) & 0xff;
1971			if (x != switches) {
1972				printk("Bad IO signature: %02x vs %02x.\n", x, switches);
1973				continue;
1974			}
1975		} else
1976			continue;
1977
1978/* OK. We have a base address for the IO ports - run a few safety checks */
1979
1980		if (!(switches & SW_BIT7)) {	/* I _think_ all cards do this */
1981			printk("There is no IN-2000 SCSI card at IOport 0x%03x!\n", base);
1982			continue;
1983		}
1984
1985/* Let's assume any hardware version will work, although the driver
1986 * has only been tested on 0x21, 0x22, 0x25, 0x26, and 0x27. We'll
1987 * print out the rev number for reference later, but accept them all.
1988 */
1989
1990		hrev = inb(base + IO_HARDWARE);
1991
1992		/* Bit 2 tells us if interrupts are disabled */
1993		if (switches & SW_DISINT) {
1994			printk("The IN-2000 SCSI card at IOport 0x%03x ", base);
1995			printk("is not configured for interrupt operation!\n");
1996			printk("This driver requires an interrupt: cancelling detection.\n");
1997			continue;
1998		}
1999
2000/* Ok. We accept that there's an IN2000 at ioaddr 'base'. Now
2001 * initialize it.
2002 */
2003
2004		tpnt->proc_name = "in2000";
2005		instance = scsi_register(tpnt, sizeof(struct IN2000_hostdata));
2006		if (instance == NULL)
2007			continue;
2008		detect_count++;
2009		hostdata = (struct IN2000_hostdata *) instance->hostdata;
2010		instance->io_port = hostdata->io_base = base;
2011		hostdata->dip_switch = switches;
2012		hostdata->hrev = hrev;
2013
2014		write1_io(0, IO_FIFO_WRITE);	/* clear fifo counter */
2015		write1_io(0, IO_FIFO_READ);	/* start fifo out in read mode */
2016		write1_io(0, IO_INTR_MASK);	/* allow all ints */
2017		x = int_tab[(switches & (SW_INT0 | SW_INT1)) >> SW_INT_SHIFT];
2018		if (request_irq(x, in2000_intr, 0, "in2000", instance)) {
2019			printk("in2000_detect: Unable to allocate IRQ.\n");
2020			detect_count--;
2021			continue;
2022		}
2023		instance->irq = x;
2024		instance->n_io_port = 13;
2025		request_region(base, 13, "in2000");	/* lock in this IO space for our use */
2026
2027		for (x = 0; x < 8; x++) {
2028			hostdata->busy[x] = 0;
2029			hostdata->sync_xfer[x] = calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
2030			hostdata->sync_stat[x] = SS_UNSET;	/* using default sync values */
2031#ifdef PROC_STATISTICS
2032			hostdata->cmd_cnt[x] = 0;
2033			hostdata->disc_allowed_cnt[x] = 0;
2034			hostdata->disc_done_cnt[x] = 0;
2035#endif
2036		}
2037		hostdata->input_Q = NULL;
2038		hostdata->selecting = NULL;
2039		hostdata->connected = NULL;
2040		hostdata->disconnected_Q = NULL;
2041		hostdata->state = S_UNCONNECTED;
2042		hostdata->fifo = FI_FIFO_UNUSED;
2043		hostdata->level2 = L2_BASIC;
2044		hostdata->disconnect = DIS_ADAPTIVE;
2045		hostdata->args = DEBUG_DEFAULTS;
2046		hostdata->incoming_ptr = 0;
2047		hostdata->outgoing_len = 0;
2048		hostdata->default_sx_per = DEFAULT_SX_PER;
2049
2050/* Older BIOS's had a 'sync on/off' switch - use its setting */
2051
2052		if (s1 == 0x41564f4e && (switches & SW_SYNC_DOS5))
2053			hostdata->sync_off = 0x00;	/* sync defaults to on */
2054		else
2055			hostdata->sync_off = 0xff;	/* sync defaults to off */
2056
2057#ifdef PROC_INTERFACE
2058		hostdata->proc = PR_VERSION | PR_INFO | PR_STATISTICS | PR_CONNECTED | PR_INPUTQ | PR_DISCQ | PR_STOP;
2059#ifdef PROC_STATISTICS
2060		hostdata->int_cnt = 0;
2061#endif
2062#endif
2063
2064		if (check_setup_args("nosync", &val, buf))
2065			hostdata->sync_off = val;
2066
2067		if (check_setup_args("period", &val, buf))
2068			hostdata->default_sx_per = sx_table[round_period((unsigned int) val)].period_ns;
2069
2070		if (check_setup_args("disconnect", &val, buf)) {
2071			if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
2072				hostdata->disconnect = val;
2073			else
2074				hostdata->disconnect = DIS_ADAPTIVE;
2075		}
2076
2077		if (check_setup_args("noreset", &val, buf))
2078			hostdata->args ^= A_NO_SCSI_RESET;
2079
2080		if (check_setup_args("level2", &val, buf))
2081			hostdata->level2 = val;
2082
2083		if (check_setup_args("debug", &val, buf))
2084			hostdata->args = (val & DB_MASK);
2085
2086#ifdef PROC_INTERFACE
2087		if (check_setup_args("proc", &val, buf))
2088			hostdata->proc = val;
2089#endif
2090
2091
2092		/* FIXME: not strictly needed I think but the called code expects
2093		   to be locked */
2094		spin_lock_irqsave(instance->host_lock, flags);
2095		x = reset_hardware(instance, (hostdata->args & A_NO_SCSI_RESET) ? RESET_CARD : RESET_CARD_AND_BUS);
2096		spin_unlock_irqrestore(instance->host_lock, flags);
2097
2098		hostdata->microcode = read_3393(hostdata, WD_CDB_1);
2099		if (x & 0x01) {
2100			if (x & B_FLAG)
2101				hostdata->chip = C_WD33C93B;
2102			else
2103				hostdata->chip = C_WD33C93A;
2104		} else
2105			hostdata->chip = C_WD33C93;
2106
2107		printk("dip_switch=%02x irq=%d ioport=%02x floppy=%s sync/DOS5=%s ", (switches & 0x7f), instance->irq, hostdata->io_base, (switches & SW_FLOPPY) ? "Yes" : "No", (switches & SW_SYNC_DOS5) ? "Yes" : "No");
2108		printk("hardware_ver=%02x chip=%s microcode=%02x\n", hrev, (hostdata->chip == C_WD33C93) ? "WD33c93" : (hostdata->chip == C_WD33C93A) ? "WD33c93A" : (hostdata->chip == C_WD33C93B) ? "WD33c93B" : "unknown", hostdata->microcode);
2109#ifdef DEBUGGING_ON
2110		printk("setup_args = ");
2111		for (x = 0; x < MAX_SETUP_ARGS; x++)
2112			printk("%s,", setup_args[x]);
2113		printk("\n");
2114#endif
2115		if (hostdata->sync_off == 0xff)
2116			printk("Sync-transfer DISABLED on all devices: ENABLE from command-line\n");
2117		printk("IN2000 driver version %s - %s\n", IN2000_VERSION, IN2000_DATE);
2118	}
2119
2120	return detect_count;
2121}
2122
2123static int in2000_release(struct Scsi_Host *shost)
2124{
2125	if (shost->irq)
2126		free_irq(shost->irq, shost);
2127	if (shost->io_port && shost->n_io_port)
2128		release_region(shost->io_port, shost->n_io_port);
2129	return 0;
2130}
2131
2132/* NOTE: I lifted this function straight out of the old driver,
2133 *       and have not tested it. Presumably it does what it's
2134 *       supposed to do...
2135 */
2136
2137static int in2000_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int *iinfo)
2138{
2139	int size;
2140
2141	size = capacity;
2142	iinfo[0] = 64;
2143	iinfo[1] = 32;
2144	iinfo[2] = size >> 11;
2145
2146/* This should approximate the large drive handling that the DOS ASPI manager
2147   uses.  Drives very near the boundaries may not be handled correctly (i.e.
2148   near 2.0 Gb and 4.0 Gb) */
2149
2150	if (iinfo[2] > 1024) {
2151		iinfo[0] = 64;
2152		iinfo[1] = 63;
2153		iinfo[2] = (unsigned long) capacity / (iinfo[0] * iinfo[1]);
2154	}
2155	if (iinfo[2] > 1024) {
2156		iinfo[0] = 128;
2157		iinfo[1] = 63;
2158		iinfo[2] = (unsigned long) capacity / (iinfo[0] * iinfo[1]);
2159	}
2160	if (iinfo[2] > 1024) {
2161		iinfo[0] = 255;
2162		iinfo[1] = 63;
2163		iinfo[2] = (unsigned long) capacity / (iinfo[0] * iinfo[1]);
2164	}
2165	return 0;
2166}
2167
2168
2169static int in2000_write_info(struct Scsi_Host *instance, char *buf, int len)
2170{
2171
2172#ifdef PROC_INTERFACE
2173
2174	char *bp;
 
 
2175	struct IN2000_hostdata *hd;
 
2176	int x, i;
 
2177
2178	hd = (struct IN2000_hostdata *) instance->hostdata;
2179
2180	buf[len] = '\0';
2181	bp = buf;
2182	if (!strncmp(bp, "debug:", 6)) {
2183		bp += 6;
2184		hd->args = simple_strtoul(bp, NULL, 0) & DB_MASK;
2185	} else if (!strncmp(bp, "disconnect:", 11)) {
2186		bp += 11;
2187		x = simple_strtoul(bp, NULL, 0);
2188		if (x < DIS_NEVER || x > DIS_ALWAYS)
2189			x = DIS_ADAPTIVE;
2190		hd->disconnect = x;
2191	} else if (!strncmp(bp, "period:", 7)) {
2192		bp += 7;
2193		x = simple_strtoul(bp, NULL, 0);
2194		hd->default_sx_per = sx_table[round_period((unsigned int) x)].period_ns;
2195	} else if (!strncmp(bp, "resync:", 7)) {
2196		bp += 7;
2197		x = simple_strtoul(bp, NULL, 0);
2198		for (i = 0; i < 7; i++)
2199			if (x & (1 << i))
2200				hd->sync_stat[i] = SS_UNSET;
2201	} else if (!strncmp(bp, "proc:", 5)) {
2202		bp += 5;
2203		hd->proc = simple_strtoul(bp, NULL, 0);
2204	} else if (!strncmp(bp, "level2:", 7)) {
2205		bp += 7;
2206		hd->level2 = simple_strtoul(bp, NULL, 0);
 
 
 
 
 
 
 
 
 
 
 
 
2207	}
2208#endif
2209	return len;
2210}
2211
2212static int in2000_show_info(struct seq_file *m, struct Scsi_Host *instance)
2213{
2214
2215#ifdef PROC_INTERFACE
2216	unsigned long flags;
2217	struct IN2000_hostdata *hd;
2218	Scsi_Cmnd *cmd;
2219	int x;
2220
2221	hd = (struct IN2000_hostdata *) instance->hostdata;
2222
2223	spin_lock_irqsave(instance->host_lock, flags);
2224	if (hd->proc & PR_VERSION)
2225		seq_printf(m, "\nVersion %s - %s.", IN2000_VERSION, IN2000_DATE);
2226
 
 
 
2227	if (hd->proc & PR_INFO) {
2228		seq_printf(m, "\ndip_switch=%02x: irq=%d io=%02x floppy=%s sync/DOS5=%s", (hd->dip_switch & 0x7f), instance->irq, hd->io_base, (hd->dip_switch & 0x40) ? "Yes" : "No", (hd->dip_switch & 0x20) ? "Yes" : "No");
2229		seq_puts(m, "\nsync_xfer[] =       ");
2230		for (x = 0; x < 7; x++)
2231			seq_printf(m, "\t%02x", hd->sync_xfer[x]);
2232		seq_puts(m, "\nsync_stat[] =       ");
2233		for (x = 0; x < 7; x++)
2234			seq_printf(m, "\t%02x", hd->sync_stat[x]);
 
 
 
 
 
2235	}
2236#ifdef PROC_STATISTICS
2237	if (hd->proc & PR_STATISTICS) {
2238		seq_puts(m, "\ncommands issued:    ");
2239		for (x = 0; x < 7; x++)
2240			seq_printf(m, "\t%ld", hd->cmd_cnt[x]);
2241		seq_puts(m, "\ndisconnects allowed:");
2242		for (x = 0; x < 7; x++)
2243			seq_printf(m, "\t%ld", hd->disc_allowed_cnt[x]);
2244		seq_puts(m, "\ndisconnects done:   ");
2245		for (x = 0; x < 7; x++)
2246			seq_printf(m, "\t%ld", hd->disc_done_cnt[x]);
2247		seq_printf(m, "\ninterrupts:      \t%ld", hd->int_cnt);
 
 
 
 
 
 
 
2248	}
2249#endif
2250	if (hd->proc & PR_CONNECTED) {
2251		seq_puts(m, "\nconnected:     ");
2252		if (hd->connected) {
2253			cmd = (Scsi_Cmnd *) hd->connected;
2254			seq_printf(m, " %d:%llu(%02x)", cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
 
2255		}
2256	}
2257	if (hd->proc & PR_INPUTQ) {
2258		seq_puts(m, "\ninput_Q:       ");
2259		cmd = (Scsi_Cmnd *) hd->input_Q;
2260		while (cmd) {
2261			seq_printf(m, " %d:%llu(%02x)", cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
 
2262			cmd = (Scsi_Cmnd *) cmd->host_scribble;
2263		}
2264	}
2265	if (hd->proc & PR_DISCQ) {
2266		seq_puts(m, "\ndisconnected_Q:");
2267		cmd = (Scsi_Cmnd *) hd->disconnected_Q;
2268		while (cmd) {
2269			seq_printf(m, " %d:%llu(%02x)", cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
 
2270			cmd = (Scsi_Cmnd *) cmd->host_scribble;
2271		}
2272	}
2273	if (hd->proc & PR_TEST) {
2274		;		/* insert your own custom function here */
2275	}
2276	seq_putc(m, '\n');
2277	spin_unlock_irqrestore(instance->host_lock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2278#endif				/* PROC_INTERFACE */
2279	return 0;
2280}
2281
2282MODULE_LICENSE("GPL");
2283
2284
2285static struct scsi_host_template driver_template = {
2286	.proc_name       		= "in2000",
2287	.write_info       		= in2000_write_info,
2288	.show_info       		= in2000_show_info,
2289	.name            		= "Always IN2000",
2290	.detect          		= in2000_detect, 
2291	.release			= in2000_release,
2292	.queuecommand    		= in2000_queuecommand,
2293	.eh_abort_handler		= in2000_abort,
2294	.eh_bus_reset_handler		= in2000_bus_reset,
2295	.bios_param      		= in2000_biosparam, 
2296	.can_queue       		= IN2000_CAN_Q,
2297	.this_id         		= IN2000_HOST_ID,
2298	.sg_tablesize    		= IN2000_SG,
2299	.cmd_per_lun     		= IN2000_CPL,
2300	.use_clustering  		= DISABLE_CLUSTERING,
2301};
2302#include "scsi_module.c"