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