• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/scsi/arm/
1/*
2 *  linux/drivers/acorn/scsi/fas216.c
3 *
4 *  Copyright (C) 1997-2003 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
11 * other sources, including:
12 *   the AMD Am53CF94 data sheet
13 *   the AMD Am53C94 data sheet
14 *
15 * This is a generic driver.  To use it, have a look at cumana_2.c.  You
16 * should define your own structure that overlays FAS216_Info, eg:
17 * struct my_host_data {
18 *    FAS216_Info info;
19 *    ... my host specific data ...
20 * };
21 *
22 * Changelog:
23 *  30-08-1997	RMK	Created
24 *  14-09-1997	RMK	Started disconnect support
25 *  08-02-1998	RMK	Corrected real DMA support
26 *  15-02-1998	RMK	Started sync xfer support
27 *  06-04-1998	RMK	Tightened conditions for printing incomplete
28 *			transfers
29 *  02-05-1998	RMK	Added extra checks in fas216_reset
30 *  24-05-1998	RMK	Fixed synchronous transfers with period >= 200ns
31 *  27-06-1998	RMK	Changed asm/delay.h to linux/delay.h
32 *  26-08-1998	RMK	Improved message support wrt MESSAGE_REJECT
33 *  02-04-2000	RMK	Converted to use the new error handling, and
34 *			automatically request sense data upon check
35 *			condition status from targets.
36 */
37#include <linux/module.h>
38#include <linux/blkdev.h>
39#include <linux/kernel.h>
40#include <linux/string.h>
41#include <linux/ioport.h>
42#include <linux/proc_fs.h>
43#include <linux/delay.h>
44#include <linux/bitops.h>
45#include <linux/init.h>
46#include <linux/interrupt.h>
47
48#include <asm/dma.h>
49#include <asm/io.h>
50#include <asm/irq.h>
51#include <asm/ecard.h>
52
53#include "../scsi.h"
54#include <scsi/scsi_dbg.h>
55#include <scsi/scsi_host.h>
56#include "fas216.h"
57#include "scsi.h"
58
59/* NOTE: SCSI2 Synchronous transfers *require* DMA according to
60 *  the data sheet.  This restriction is crazy, especially when
61 *  you only want to send 16 bytes!  What were the guys who
62 *  designed this chip on at that time?  Did they read the SCSI2
63 *  spec at all?  The following sections are taken from the SCSI2
64 *  standard (s2r10) concerning this:
65 *
66 * > IMPLEMENTORS NOTES:
67 * >   (1)  Re-negotiation at every selection is not recommended, since a
68 * >   significant performance impact is likely.
69 *
70 * >  The implied synchronous agreement shall remain in effect until a BUS DEVICE
71 * >  RESET message is received, until a hard reset condition occurs, or until one
72 * >  of the two SCSI devices elects to modify the agreement.  The default data
73 * >  transfer mode is asynchronous data transfer mode.  The default data transfer
74 * >  mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
75 * >  reset condition.
76 *
77 *  In total, this means that once you have elected to use synchronous
78 *  transfers, you must always use DMA.
79 *
80 *  I was thinking that this was a good chip until I found this restriction ;(
81 */
82#define SCSI2_SYNC
83#undef  SCSI2_TAG
84
85#undef DEBUG_CONNECT
86#undef DEBUG_MESSAGES
87
88#undef CHECK_STRUCTURE
89
90#define LOG_CONNECT		(1 << 0)
91#define LOG_BUSSERVICE		(1 << 1)
92#define LOG_FUNCTIONDONE	(1 << 2)
93#define LOG_MESSAGES		(1 << 3)
94#define LOG_BUFFER		(1 << 4)
95#define LOG_ERROR		(1 << 8)
96
97static int level_mask = LOG_ERROR;
98
99module_param(level_mask, int, 0644);
100
101static int __init fas216_log_setup(char *str)
102{
103	char *s;
104
105	level_mask = 0;
106
107	while ((s = strsep(&str, ",")) != NULL) {
108		switch (s[0]) {
109		case 'a':
110			if (strcmp(s, "all") == 0)
111				level_mask |= -1;
112			break;
113		case 'b':
114			if (strncmp(s, "bus", 3) == 0)
115				level_mask |= LOG_BUSSERVICE;
116			if (strncmp(s, "buf", 3) == 0)
117				level_mask |= LOG_BUFFER;
118			break;
119		case 'c':
120			level_mask |= LOG_CONNECT;
121			break;
122		case 'e':
123			level_mask |= LOG_ERROR;
124			break;
125		case 'm':
126			level_mask |= LOG_MESSAGES;
127			break;
128		case 'n':
129			if (strcmp(s, "none") == 0)
130				level_mask = 0;
131			break;
132		case 's':
133			level_mask |= LOG_FUNCTIONDONE;
134			break;
135		}
136	}
137	return 1;
138}
139
140__setup("fas216_logging=", fas216_log_setup);
141
142static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
143{
144	unsigned int off = reg << info->scsi.io_shift;
145	return readb(info->scsi.io_base + off);
146}
147
148static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
149{
150	unsigned int off = reg << info->scsi.io_shift;
151	writeb(val, info->scsi.io_base + off);
152}
153
154static void fas216_dumpstate(FAS216_Info *info)
155{
156	unsigned char is, stat, inst;
157
158	is   = fas216_readb(info, REG_IS);
159	stat = fas216_readb(info, REG_STAT);
160	inst = fas216_readb(info, REG_INST);
161
162	printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
163	       " INST=%02X IS=%02X CFIS=%02X",
164		fas216_readb(info, REG_CTCL),
165		fas216_readb(info, REG_CTCM),
166		fas216_readb(info, REG_CMD),  stat, inst, is,
167		fas216_readb(info, REG_CFIS));
168	printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
169		fas216_readb(info, REG_CNTL1),
170		fas216_readb(info, REG_CNTL2),
171		fas216_readb(info, REG_CNTL3),
172		fas216_readb(info, REG_CTCH));
173}
174
175static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
176{
177	printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
178		prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
179		SCp->buffers_residual, suffix);
180}
181
182static void fas216_dumpinfo(FAS216_Info *info)
183{
184	static int used = 0;
185	int i;
186
187	if (used++)
188		return;
189
190	printk("FAS216_Info=\n");
191	printk("  { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
192		info->magic_start, info->host, info->SCpnt,
193		info->origSCpnt);
194	printk("    scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
195		info->scsi.io_shift, info->scsi.irq,
196		info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
197		info->scsi.cfg[3]);
198	printk("           type=%p phase=%X\n",
199		info->scsi.type, info->scsi.phase);
200	print_SCp(&info->scsi.SCp, "           SCp={ ", " }\n");
201	printk("      msgs async_stp=%X disconnectable=%d aborting=%d }\n",
202		info->scsi.async_stp,
203		info->scsi.disconnectable, info->scsi.aborting);
204	printk("    stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
205	       "            disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
206		info->stats.queues, info->stats.removes, info->stats.fins,
207		info->stats.reads, info->stats.writes, info->stats.miscs,
208		info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
209		info->stats.host_resets);
210	printk("    ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
211		info->ifcfg.clockrate, info->ifcfg.select_timeout,
212		info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
213	for (i = 0; i < 8; i++) {
214		printk("    busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
215			i, info->busyluns[i], i,
216			info->device[i].disconnect_ok, info->device[i].stp,
217			info->device[i].sof, info->device[i].sync_state);
218	}
219	printk("    dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
220		info->dma.transfer_type, info->dma.setup,
221		info->dma.pseudo, info->dma.stop);
222	printk("    internal_done=%X magic_end=%lX }\n",
223		info->internal_done, info->magic_end);
224}
225
226#ifdef CHECK_STRUCTURE
227static void __fas216_checkmagic(FAS216_Info *info, const char *func)
228{
229	int corruption = 0;
230	if (info->magic_start != MAGIC) {
231		printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
232		corruption++;
233	}
234	if (info->magic_end != MAGIC) {
235		printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
236		corruption++;
237	}
238	if (corruption) {
239		fas216_dumpinfo(info);
240		panic("scsi memory space corrupted in %s", func);
241	}
242}
243#define fas216_checkmagic(info) __fas216_checkmagic((info), __func__)
244#else
245#define fas216_checkmagic(info)
246#endif
247
248static const char *fas216_bus_phase(int stat)
249{
250	static const char *phases[] = {
251		"DATA OUT", "DATA IN",
252		"COMMAND", "STATUS",
253		"MISC OUT", "MISC IN",
254		"MESG OUT", "MESG IN"
255	};
256
257	return phases[stat & STAT_BUSMASK];
258}
259
260static const char *fas216_drv_phase(FAS216_Info *info)
261{
262	static const char *phases[] = {
263		[PHASE_IDLE]		= "idle",
264		[PHASE_SELECTION]	= "selection",
265		[PHASE_COMMAND]		= "command",
266		[PHASE_DATAOUT]		= "data out",
267		[PHASE_DATAIN]		= "data in",
268		[PHASE_MSGIN]		= "message in",
269		[PHASE_MSGIN_DISCONNECT]= "disconnect",
270		[PHASE_MSGOUT_EXPECT]	= "expect message out",
271		[PHASE_MSGOUT]		= "message out",
272		[PHASE_STATUS]		= "status",
273		[PHASE_DONE]		= "done",
274	};
275
276	if (info->scsi.phase < ARRAY_SIZE(phases) &&
277	    phases[info->scsi.phase])
278		return phases[info->scsi.phase];
279	return "???";
280}
281
282static char fas216_target(FAS216_Info *info)
283{
284	if (info->SCpnt)
285		return '0' + info->SCpnt->device->id;
286	else
287		return 'H';
288}
289
290static void
291fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
292{
293	static char buf[1024];
294
295	vsnprintf(buf, sizeof(buf), fmt, ap);
296	printk("scsi%d.%c: %s", info->host->host_no, target, buf);
297}
298
299static void fas216_log_command(FAS216_Info *info, int level,
300			       struct scsi_cmnd *SCpnt, char *fmt, ...)
301{
302	va_list args;
303
304	if (level != 0 && !(level & level_mask))
305		return;
306
307	va_start(args, fmt);
308	fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
309	va_end(args);
310
311	printk(" CDB: ");
312	__scsi_print_command(SCpnt->cmnd);
313}
314
315static void
316fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
317{
318	va_list args;
319
320	if (level != 0 && !(level & level_mask))
321		return;
322
323	if (target < 0)
324		target = 'H';
325	else
326		target += '0';
327
328	va_start(args, fmt);
329	fas216_do_log(info, target, fmt, args);
330	va_end(args);
331
332	printk("\n");
333}
334
335static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
336{
337	va_list args;
338
339	if (level != 0 && !(level & level_mask))
340		return;
341
342	va_start(args, fmt);
343	fas216_do_log(info, fas216_target(info), fmt, args);
344	va_end(args);
345
346	printk("\n");
347}
348
349#define PH_SIZE	32
350
351static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
352static int ph_ptr;
353
354static void add_debug_list(int stat, int ssr, int isr, int ph)
355{
356	ph_list[ph_ptr].stat = stat;
357	ph_list[ph_ptr].ssr = ssr;
358	ph_list[ph_ptr].isr = isr;
359	ph_list[ph_ptr].ph = ph;
360
361	ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
362}
363
364static struct { int command; void *from; } cmd_list[8];
365static int cmd_ptr;
366
367static void fas216_cmd(FAS216_Info *info, unsigned int command)
368{
369	cmd_list[cmd_ptr].command = command;
370	cmd_list[cmd_ptr].from = __builtin_return_address(0);
371
372	cmd_ptr = (cmd_ptr + 1) & 7;
373
374	fas216_writeb(info, REG_CMD, command);
375}
376
377static void print_debug_list(void)
378{
379	int i;
380
381	i = ph_ptr;
382
383	printk(KERN_ERR "SCSI IRQ trail\n");
384	do {
385		printk(" %02x:%02x:%02x:%1x",
386			ph_list[i].stat, ph_list[i].ssr,
387			ph_list[i].isr, ph_list[i].ph);
388		i = (i + 1) & (PH_SIZE - 1);
389		if (((i ^ ph_ptr) & 7) == 0)
390			printk("\n");
391	} while (i != ph_ptr);
392	if ((i ^ ph_ptr) & 7)
393		printk("\n");
394
395	i = cmd_ptr;
396	printk(KERN_ERR "FAS216 commands: ");
397	do {
398		printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
399		i = (i + 1) & 7;
400	} while (i != cmd_ptr);
401	printk("\n");
402}
403
404static void fas216_done(FAS216_Info *info, unsigned int result);
405
406/**
407 * fas216_get_last_msg - retrive last message from the list
408 * @info: interface to search
409 * @pos: current fifo position
410 *
411 * Retrieve a last message from the list, using position in fifo.
412 */
413static inline unsigned short
414fas216_get_last_msg(FAS216_Info *info, int pos)
415{
416	unsigned short packed_msg = NOP;
417	struct message *msg;
418	int msgnr = 0;
419
420	while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
421		if (pos >= msg->fifo)
422			break;
423	}
424
425	if (msg) {
426		if (msg->msg[0] == EXTENDED_MESSAGE)
427			packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
428		else
429			packed_msg = msg->msg[0];
430	}
431
432	fas216_log(info, LOG_MESSAGES,
433		"Message: %04x found at position %02x\n", packed_msg, pos);
434
435	return packed_msg;
436}
437
438/**
439 * fas216_syncperiod - calculate STP register value
440 * @info: state structure for interface connected to device
441 * @ns: period in ns (between subsequent bytes)
442 *
443 * Calculate value to be loaded into the STP register for a given period
444 * in ns. Returns a value suitable for REG_STP.
445 */
446static int fas216_syncperiod(FAS216_Info *info, int ns)
447{
448	int value = (info->ifcfg.clockrate * ns) / 1000;
449
450	fas216_checkmagic(info);
451
452	if (value < 4)
453		value = 4;
454	else if (value > 35)
455		value = 35;
456
457	return value & 31;
458}
459
460/**
461 * fas216_set_sync - setup FAS216 chip for specified transfer period.
462 * @info: state structure for interface connected to device
463 * @target: target
464 *
465 * Correctly setup FAS216 chip for specified transfer period.
466 * Notes   : we need to switch the chip out of FASTSCSI mode if we have
467 *           a transfer period >= 200ns - otherwise the chip will violate
468 *           the SCSI timings.
469 */
470static void fas216_set_sync(FAS216_Info *info, int target)
471{
472	unsigned int cntl3;
473
474	fas216_writeb(info, REG_SOF, info->device[target].sof);
475	fas216_writeb(info, REG_STP, info->device[target].stp);
476
477	cntl3 = info->scsi.cfg[2];
478	if (info->device[target].period >= (200 / 4))
479		cntl3 = cntl3 & ~CNTL3_FASTSCSI;
480
481	fas216_writeb(info, REG_CNTL3, cntl3);
482}
483
484/* Synchronous transfer support
485 *
486 * Note: The SCSI II r10 spec says (5.6.12):
487 *
488 *  (2)  Due to historical problems with early host adapters that could
489 *  not accept an SDTR message, some targets may not initiate synchronous
490 *  negotiation after a power cycle as required by this standard.  Host
491 *  adapters that support synchronous mode may avoid the ensuing failure
492 *  modes when the target is independently power cycled by initiating a
493 *  synchronous negotiation on each REQUEST SENSE and INQUIRY command.
494 *  This approach increases the SCSI bus overhead and is not recommended
495 *  for new implementations.  The correct method is to respond to an
496 *  SDTR message with a MESSAGE REJECT message if the either the
497 *  initiator or target devices does not support synchronous transfers
498 *  or does not want to negotiate for synchronous transfers at the time.
499 *  Using the correct method assures compatibility with wide data
500 *  transfers and future enhancements.
501 *
502 * We will always initiate a synchronous transfer negotiation request on
503 * every INQUIRY or REQUEST SENSE message, unless the target itself has
504 * at some point performed a synchronous transfer negotiation request, or
505 * we have synchronous transfers disabled for this device.
506 */
507
508/**
509 * fas216_handlesync - Handle a synchronous transfer message
510 * @info: state structure for interface
511 * @msg: message from target
512 *
513 * Handle a synchronous transfer message from the target
514 */
515static void fas216_handlesync(FAS216_Info *info, char *msg)
516{
517	struct fas216_device *dev = &info->device[info->SCpnt->device->id];
518	enum { sync, async, none, reject } res = none;
519
520#ifdef SCSI2_SYNC
521	switch (msg[0]) {
522	case MESSAGE_REJECT:
523		/* Synchronous transfer request failed.
524		 * Note: SCSI II r10:
525		 *
526		 *  SCSI devices that are capable of synchronous
527		 *  data transfers shall not respond to an SDTR
528		 *  message with a MESSAGE REJECT message.
529		 *
530		 * Hence, if we get this condition, we disable
531		 * negotiation for this device.
532		 */
533		if (dev->sync_state == neg_inprogress) {
534			dev->sync_state = neg_invalid;
535			res = async;
536		}
537		break;
538
539	case EXTENDED_MESSAGE:
540		switch (dev->sync_state) {
541		/* We don't accept synchronous transfer requests.
542		 * Respond with a MESSAGE_REJECT to prevent a
543		 * synchronous transfer agreement from being reached.
544		 */
545		case neg_invalid:
546			res = reject;
547			break;
548
549		/* We were not negotiating a synchronous transfer,
550		 * but the device sent us a negotiation request.
551		 * Honour the request by sending back a SDTR
552		 * message containing our capability, limited by
553		 * the targets capability.
554		 */
555		default:
556			fas216_cmd(info, CMD_SETATN);
557			if (msg[4] > info->ifcfg.sync_max_depth)
558				msg[4] = info->ifcfg.sync_max_depth;
559			if (msg[3] < 1000 / info->ifcfg.clockrate)
560				msg[3] = 1000 / info->ifcfg.clockrate;
561
562			msgqueue_flush(&info->scsi.msgs);
563			msgqueue_addmsg(&info->scsi.msgs, 5,
564					EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
565					msg[3], msg[4]);
566			info->scsi.phase = PHASE_MSGOUT_EXPECT;
567
568			/* This is wrong.  The agreement is not in effect
569			 * until this message is accepted by the device
570			 */
571			dev->sync_state = neg_targcomplete;
572			res = sync;
573			break;
574
575		/* We initiated the synchronous transfer negotiation,
576		 * and have successfully received a response from the
577		 * target.  The synchronous transfer agreement has been
578		 * reached.  Note: if the values returned are out of our
579		 * bounds, we must reject the message.
580		 */
581		case neg_inprogress:
582			res = reject;
583			if (msg[4] <= info->ifcfg.sync_max_depth &&
584			    msg[3] >= 1000 / info->ifcfg.clockrate) {
585				dev->sync_state = neg_complete;
586				res = sync;
587			}
588			break;
589		}
590	}
591#else
592	res = reject;
593#endif
594
595	switch (res) {
596	case sync:
597		dev->period = msg[3];
598		dev->sof    = msg[4];
599		dev->stp    = fas216_syncperiod(info, msg[3] * 4);
600		fas216_set_sync(info, info->SCpnt->device->id);
601		break;
602
603	case reject:
604		fas216_cmd(info, CMD_SETATN);
605		msgqueue_flush(&info->scsi.msgs);
606		msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
607		info->scsi.phase = PHASE_MSGOUT_EXPECT;
608
609	case async:
610		dev->period = info->ifcfg.asyncperiod / 4;
611		dev->sof    = 0;
612		dev->stp    = info->scsi.async_stp;
613		fas216_set_sync(info, info->SCpnt->device->id);
614		break;
615
616	case none:
617		break;
618	}
619}
620
621/**
622 * fas216_updateptrs - update data pointers after transfer suspended/paused
623 * @info: interface's local pointer to update
624 * @bytes_transferred: number of bytes transferred
625 *
626 * Update data pointers after transfer suspended/paused
627 */
628static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
629{
630	struct scsi_pointer *SCp = &info->scsi.SCp;
631
632	fas216_checkmagic(info);
633
634	BUG_ON(bytes_transferred < 0);
635
636	SCp->phase -= bytes_transferred;
637
638	while (bytes_transferred != 0) {
639		if (SCp->this_residual > bytes_transferred)
640			break;
641		/*
642		 * We have used up this buffer.  Move on to the
643		 * next buffer.
644		 */
645		bytes_transferred -= SCp->this_residual;
646		if (!next_SCp(SCp) && bytes_transferred) {
647			printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
648				info->host->host_no, '0' + info->SCpnt->device->id);
649			return;
650		}
651	}
652
653	SCp->this_residual -= bytes_transferred;
654	if (SCp->this_residual)
655		SCp->ptr += bytes_transferred;
656	else
657		SCp->ptr = NULL;
658}
659
660/**
661 * fas216_pio - transfer data off of/on to card using programmed IO
662 * @info: interface to transfer data to/from
663 * @direction: direction to transfer data (DMA_OUT/DMA_IN)
664 *
665 * Transfer data off of/on to card using programmed IO.
666 * Notes: this is incredibly slow.
667 */
668static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
669{
670	struct scsi_pointer *SCp = &info->scsi.SCp;
671
672	fas216_checkmagic(info);
673
674	if (direction == DMA_OUT)
675		fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
676	else
677		put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
678
679	if (SCp->this_residual == 0)
680		next_SCp(SCp);
681}
682
683static void fas216_set_stc(FAS216_Info *info, unsigned int length)
684{
685	fas216_writeb(info, REG_STCL, length);
686	fas216_writeb(info, REG_STCM, length >> 8);
687	fas216_writeb(info, REG_STCH, length >> 16);
688}
689
690static unsigned int fas216_get_ctc(FAS216_Info *info)
691{
692	return fas216_readb(info, REG_CTCL) +
693	       (fas216_readb(info, REG_CTCM) << 8) +
694	       (fas216_readb(info, REG_CTCH) << 16);
695}
696
697/**
698 * fas216_cleanuptransfer - clean up after a transfer has completed.
699 * @info: interface to clean up
700 *
701 * Update the data pointers according to the number of bytes transferred
702 * on the SCSI bus.
703 */
704static void fas216_cleanuptransfer(FAS216_Info *info)
705{
706	unsigned long total, residual, fifo;
707	fasdmatype_t dmatype = info->dma.transfer_type;
708
709	info->dma.transfer_type = fasdma_none;
710
711	/*
712	 * PIO transfers do not need to be cleaned up.
713	 */
714	if (dmatype == fasdma_pio || dmatype == fasdma_none)
715		return;
716
717	if (dmatype == fasdma_real_all)
718		total = info->scsi.SCp.phase;
719	else
720		total = info->scsi.SCp.this_residual;
721
722	residual = fas216_get_ctc(info);
723
724	fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
725
726	fas216_log(info, LOG_BUFFER, "cleaning up from previous "
727		   "transfer: length 0x%06x, residual 0x%x, fifo %d",
728		   total, residual, fifo);
729
730	/*
731	 * If we were performing Data-Out, the transfer counter
732	 * counts down each time a byte is transferred by the
733	 * host to the FIFO.  This means we must include the
734	 * bytes left in the FIFO from the transfer counter.
735	 */
736	if (info->scsi.phase == PHASE_DATAOUT)
737		residual += fifo;
738
739	fas216_updateptrs(info, total - residual);
740}
741
742/**
743 * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
744 * @info: interface from which device disconnected from
745 *
746 * Start a DMA/PIO transfer off of/on to card
747 */
748static void fas216_transfer(FAS216_Info *info)
749{
750	fasdmadir_t direction;
751	fasdmatype_t dmatype;
752
753	fas216_log(info, LOG_BUFFER,
754		   "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
755		   info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
756		   info->scsi.SCp.phase);
757
758	if (!info->scsi.SCp.ptr) {
759		fas216_log(info, LOG_ERROR, "null buffer passed to "
760			   "fas216_starttransfer");
761		print_SCp(&info->scsi.SCp, "SCp: ", "\n");
762		print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
763		return;
764	}
765
766	/*
767	 * If we have a synchronous transfer agreement in effect, we must
768	 * use DMA mode.  If we are using asynchronous transfers, we may
769	 * use DMA mode or PIO mode.
770	 */
771	if (info->device[info->SCpnt->device->id].sof)
772		dmatype = fasdma_real_all;
773	else
774		dmatype = fasdma_pio;
775
776	if (info->scsi.phase == PHASE_DATAOUT)
777		direction = DMA_OUT;
778	else
779		direction = DMA_IN;
780
781	if (info->dma.setup)
782		dmatype = info->dma.setup(info->host, &info->scsi.SCp,
783					  direction, dmatype);
784	info->dma.transfer_type = dmatype;
785
786	if (dmatype == fasdma_real_all)
787		fas216_set_stc(info, info->scsi.SCp.phase);
788	else
789		fas216_set_stc(info, info->scsi.SCp.this_residual);
790
791	switch (dmatype) {
792	case fasdma_pio:
793		fas216_log(info, LOG_BUFFER, "PIO transfer");
794		fas216_writeb(info, REG_SOF, 0);
795		fas216_writeb(info, REG_STP, info->scsi.async_stp);
796		fas216_cmd(info, CMD_TRANSFERINFO);
797		fas216_pio(info, direction);
798		break;
799
800	case fasdma_pseudo:
801		fas216_log(info, LOG_BUFFER, "pseudo transfer");
802		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
803		info->dma.pseudo(info->host, &info->scsi.SCp,
804				 direction, info->SCpnt->transfersize);
805		break;
806
807	case fasdma_real_block:
808		fas216_log(info, LOG_BUFFER, "block dma transfer");
809		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
810		break;
811
812	case fasdma_real_all:
813		fas216_log(info, LOG_BUFFER, "total dma transfer");
814		fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
815		break;
816
817	default:
818		fas216_log(info, LOG_BUFFER | LOG_ERROR,
819			   "invalid FAS216 DMA type");
820		break;
821	}
822}
823
824/**
825 * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
826 * @info: interface from which device disconnected from
827 *
828 * Called when we switch away from DATA IN or DATA OUT phases.
829 */
830static void fas216_stoptransfer(FAS216_Info *info)
831{
832	fas216_checkmagic(info);
833
834	if (info->dma.transfer_type == fasdma_real_all ||
835	    info->dma.transfer_type == fasdma_real_block)
836		info->dma.stop(info->host, &info->scsi.SCp);
837
838	fas216_cleanuptransfer(info);
839
840	if (info->scsi.phase == PHASE_DATAIN) {
841		unsigned int fifo;
842
843		/*
844		 * If we were performing Data-In, then the FIFO counter
845		 * contains the number of bytes not transferred via DMA
846		 * from the on-board FIFO.  Read them manually.
847		 */
848		fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
849		while (fifo && info->scsi.SCp.ptr) {
850			*info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
851			fas216_updateptrs(info, 1);
852			fifo--;
853		}
854	} else {
855		/*
856		 * After a Data-Out phase, there may be unsent
857		 * bytes left in the FIFO.  Flush them out.
858		 */
859		fas216_cmd(info, CMD_FLUSHFIFO);
860	}
861}
862
863static void fas216_aborttransfer(FAS216_Info *info)
864{
865	fas216_checkmagic(info);
866
867	if (info->dma.transfer_type == fasdma_real_all ||
868	    info->dma.transfer_type == fasdma_real_block)
869		info->dma.stop(info->host, &info->scsi.SCp);
870
871	info->dma.transfer_type = fasdma_none;
872	fas216_cmd(info, CMD_FLUSHFIFO);
873}
874
875static void fas216_kick(FAS216_Info *info);
876
877/**
878 * fas216_disconnected_intr - handle device disconnection
879 * @info: interface from which device disconnected from
880 *
881 * Handle device disconnection
882 */
883static void fas216_disconnect_intr(FAS216_Info *info)
884{
885	unsigned long flags;
886
887	fas216_checkmagic(info);
888
889	fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
890		   info->scsi.phase);
891
892	msgqueue_flush(&info->scsi.msgs);
893
894	switch (info->scsi.phase) {
895	case PHASE_SELECTION:			/* while selecting - no target		*/
896	case PHASE_SELSTEPS:
897		fas216_done(info, DID_NO_CONNECT);
898		break;
899
900	case PHASE_MSGIN_DISCONNECT:		/* message in - disconnecting		*/
901		info->scsi.disconnectable = 1;
902		info->scsi.phase = PHASE_IDLE;
903		info->stats.disconnects += 1;
904		spin_lock_irqsave(&info->host_lock, flags);
905		if (info->scsi.phase == PHASE_IDLE)
906			fas216_kick(info);
907		spin_unlock_irqrestore(&info->host_lock, flags);
908		break;
909
910	case PHASE_DONE:			/* at end of command - complete		*/
911		fas216_done(info, DID_OK);
912		break;
913
914	case PHASE_MSGOUT:			/* message out - possible ABORT message	*/
915		if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
916			info->scsi.aborting = 0;
917			fas216_done(info, DID_ABORT);
918			break;
919		}
920
921	default:				/* huh?					*/
922		printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
923			info->host->host_no, fas216_target(info), fas216_drv_phase(info));
924		print_debug_list();
925		fas216_stoptransfer(info);
926		fas216_done(info, DID_ERROR);
927		break;
928	}
929}
930
931/**
932 * fas216_reselected_intr - start reconnection of a device
933 * @info: interface which was reselected
934 *
935 * Start reconnection of a device
936 */
937static void
938fas216_reselected_intr(FAS216_Info *info)
939{
940	unsigned int cfis, i;
941	unsigned char msg[4];
942	unsigned char target, lun, tag;
943
944	fas216_checkmagic(info);
945
946	WARN_ON(info->scsi.phase == PHASE_SELECTION ||
947		info->scsi.phase == PHASE_SELSTEPS);
948
949	cfis = fas216_readb(info, REG_CFIS);
950
951	fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
952		   info->scsi.phase, cfis);
953
954	cfis &= CFIS_CF;
955
956	if (cfis < 2 || cfis > 4) {
957		printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
958			info->host->host_no);
959		goto bad_message;
960	}
961
962	for (i = 0; i < cfis; i++)
963		msg[i] = fas216_readb(info, REG_FF);
964
965	if (!(msg[0] & (1 << info->host->this_id)) ||
966	    !(msg[1] & 0x80))
967		goto initiator_error;
968
969	target = msg[0] & ~(1 << info->host->this_id);
970	target = ffs(target) - 1;
971	lun = msg[1] & 7;
972	tag = 0;
973
974	if (cfis >= 3) {
975		if (msg[2] != SIMPLE_QUEUE_TAG)
976			goto initiator_error;
977
978		tag = msg[3];
979	}
980
981	/* set up for synchronous transfers */
982	fas216_writeb(info, REG_SDID, target);
983	fas216_set_sync(info, target);
984	msgqueue_flush(&info->scsi.msgs);
985
986	fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
987		   target, lun, tag);
988
989	if (info->scsi.disconnectable && info->SCpnt) {
990		info->scsi.disconnectable = 0;
991		if (info->SCpnt->device->id  == target &&
992		    info->SCpnt->device->lun == lun &&
993		    info->SCpnt->tag         == tag) {
994			fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
995		} else {
996			queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
997			fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
998			info->SCpnt = NULL;
999		}
1000	}
1001	if (!info->SCpnt) {
1002		info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
1003					target, lun, tag);
1004		fas216_log(info, LOG_CONNECT, "had to get command");
1005	}
1006
1007	if (info->SCpnt) {
1008		/*
1009		 * Restore data pointer from SAVED data pointer
1010		 */
1011		info->scsi.SCp = info->SCpnt->SCp;
1012
1013		fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
1014			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1015		info->scsi.phase = PHASE_MSGIN;
1016	} else {
1017		/*
1018		 * Our command structure not found - abort the
1019		 * command on the target.  Since we have no
1020		 * record of this command, we can't send
1021		 * an INITIATOR DETECTED ERROR message.
1022		 */
1023		fas216_cmd(info, CMD_SETATN);
1024
1025			msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
1026		info->scsi.phase = PHASE_MSGOUT_EXPECT;
1027		info->scsi.aborting = 1;
1028	}
1029
1030	fas216_cmd(info, CMD_MSGACCEPTED);
1031	return;
1032
1033 initiator_error:
1034	printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
1035		info->host->host_no);
1036	for (i = 0; i < cfis; i++)
1037		printk(" %02x", msg[i]);
1038	printk("\n");
1039 bad_message:
1040	fas216_cmd(info, CMD_SETATN);
1041	msgqueue_flush(&info->scsi.msgs);
1042	msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1043	info->scsi.phase = PHASE_MSGOUT_EXPECT;
1044	fas216_cmd(info, CMD_MSGACCEPTED);
1045}
1046
1047static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
1048{
1049	int i;
1050
1051	switch (message[0]) {
1052	case COMMAND_COMPLETE:
1053		if (msglen != 1)
1054			goto unrecognised;
1055
1056		printk(KERN_ERR "scsi%d.%c: command complete with no "
1057			"status in MESSAGE_IN?\n",
1058			info->host->host_no, fas216_target(info));
1059		break;
1060
1061	case SAVE_POINTERS:
1062		if (msglen != 1)
1063			goto unrecognised;
1064
1065		/*
1066		 * Save current data pointer to SAVED data pointer
1067		 * SCSI II standard says that we must not acknowledge
1068		 * this until we have really saved pointers.
1069		 * NOTE: we DO NOT save the command nor status pointers
1070		 * as required by the SCSI II standard.  These always
1071		 * point to the start of their respective areas.
1072		 */
1073		info->SCpnt->SCp = info->scsi.SCp;
1074		info->SCpnt->SCp.sent_command = 0;
1075		fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1076			"save data pointers: [%p, %X]",
1077			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1078		break;
1079
1080	case RESTORE_POINTERS:
1081		if (msglen != 1)
1082			goto unrecognised;
1083
1084		/*
1085		 * Restore current data pointer from SAVED data pointer
1086		 */
1087		info->scsi.SCp = info->SCpnt->SCp;
1088		fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1089			"restore data pointers: [%p, 0x%x]",
1090			info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1091		break;
1092
1093	case DISCONNECT:
1094		if (msglen != 1)
1095			goto unrecognised;
1096
1097		info->scsi.phase = PHASE_MSGIN_DISCONNECT;
1098		break;
1099
1100	case MESSAGE_REJECT:
1101		if (msglen != 1)
1102			goto unrecognised;
1103
1104		switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
1105		case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
1106			fas216_handlesync(info, message);
1107			break;
1108
1109		default:
1110			fas216_log(info, 0, "reject, last message 0x%04x",
1111				fas216_get_last_msg(info, info->scsi.msgin_fifo));
1112		}
1113		break;
1114
1115	case NOP:
1116		break;
1117
1118	case EXTENDED_MESSAGE:
1119		if (msglen < 3)
1120			goto unrecognised;
1121
1122		switch (message[2]) {
1123		case EXTENDED_SDTR:	/* Sync transfer negotiation request/reply */
1124			fas216_handlesync(info, message);
1125			break;
1126
1127		default:
1128			goto unrecognised;
1129		}
1130		break;
1131
1132	default:
1133		goto unrecognised;
1134	}
1135	return;
1136
1137unrecognised:
1138	fas216_log(info, 0, "unrecognised message, rejecting");
1139	printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
1140	for (i = 0; i < msglen; i++)
1141		printk("%s%02X", i & 31 ? " " : "\n  ", message[i]);
1142	printk("\n");
1143
1144	/*
1145	 * Something strange seems to be happening here -
1146	 * I can't use SETATN since the chip gives me an
1147	 * invalid command interrupt when I do.  Weird.
1148	 */
1149fas216_cmd(info, CMD_NOP);
1150fas216_dumpstate(info);
1151	fas216_cmd(info, CMD_SETATN);
1152	msgqueue_flush(&info->scsi.msgs);
1153	msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
1154	info->scsi.phase = PHASE_MSGOUT_EXPECT;
1155fas216_dumpstate(info);
1156}
1157
1158static int fas216_wait_cmd(FAS216_Info *info, int cmd)
1159{
1160	int tout;
1161	int stat;
1162
1163	fas216_cmd(info, cmd);
1164
1165	for (tout = 1000; tout; tout -= 1) {
1166		stat = fas216_readb(info, REG_STAT);
1167		if (stat & (STAT_INT|STAT_PARITYERROR))
1168			break;
1169		udelay(1);
1170	}
1171
1172	return stat;
1173}
1174
1175static int fas216_get_msg_byte(FAS216_Info *info)
1176{
1177	unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
1178
1179	if ((stat & STAT_INT) == 0)
1180		goto timedout;
1181
1182	if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1183		goto unexpected_phase_change;
1184
1185	fas216_readb(info, REG_INST);
1186
1187	stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
1188
1189	if ((stat & STAT_INT) == 0)
1190		goto timedout;
1191
1192	if (stat & STAT_PARITYERROR)
1193		goto parity_error;
1194
1195	if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1196		goto unexpected_phase_change;
1197
1198	fas216_readb(info, REG_INST);
1199
1200	return fas216_readb(info, REG_FF);
1201
1202timedout:
1203	fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
1204	return -1;
1205
1206unexpected_phase_change:
1207	fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
1208	return -2;
1209
1210parity_error:
1211	fas216_log(info, LOG_ERROR, "parity error during message in phase");
1212	return -3;
1213}
1214
1215/**
1216 * fas216_message - handle a function done interrupt from FAS216 chip
1217 * @info: interface which caused function done interrupt
1218 *
1219 * Handle a function done interrupt from FAS216 chip
1220 */
1221static void fas216_message(FAS216_Info *info)
1222{
1223	unsigned char *message = info->scsi.message;
1224	unsigned int msglen = 1;
1225	int msgbyte = 0;
1226
1227	fas216_checkmagic(info);
1228
1229	message[0] = fas216_readb(info, REG_FF);
1230
1231	if (message[0] == EXTENDED_MESSAGE) {
1232		msgbyte = fas216_get_msg_byte(info);
1233
1234		if (msgbyte >= 0) {
1235			message[1] = msgbyte;
1236
1237			for (msglen = 2; msglen < message[1] + 2; msglen++) {
1238				msgbyte = fas216_get_msg_byte(info);
1239
1240				if (msgbyte >= 0)
1241					message[msglen] = msgbyte;
1242				else
1243					break;
1244			}
1245		}
1246	}
1247
1248	if (msgbyte == -3)
1249		goto parity_error;
1250
1251#ifdef DEBUG_MESSAGES
1252	{
1253		int i;
1254
1255		printk("scsi%d.%c: message in: ",
1256			info->host->host_no, fas216_target(info));
1257		for (i = 0; i < msglen; i++)
1258			printk("%02X ", message[i]);
1259		printk("\n");
1260	}
1261#endif
1262
1263	fas216_parse_message(info, message, msglen);
1264	fas216_cmd(info, CMD_MSGACCEPTED);
1265	return;
1266
1267parity_error:
1268	fas216_cmd(info, CMD_SETATN);
1269	msgqueue_flush(&info->scsi.msgs);
1270	msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
1271	info->scsi.phase = PHASE_MSGOUT_EXPECT;
1272	fas216_cmd(info, CMD_MSGACCEPTED);
1273	return;
1274}
1275
1276/**
1277 * fas216_send_command - send command after all message bytes have been sent
1278 * @info: interface which caused bus service
1279 *
1280 * Send a command to a target after all message bytes have been sent
1281 */
1282static void fas216_send_command(FAS216_Info *info)
1283{
1284	int i;
1285
1286	fas216_checkmagic(info);
1287
1288	fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
1289	fas216_cmd(info, CMD_FLUSHFIFO);
1290
1291	/* load command */
1292	for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
1293		fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
1294
1295	fas216_cmd(info, CMD_TRANSFERINFO);
1296
1297	info->scsi.phase = PHASE_COMMAND;
1298}
1299
1300/**
1301 * fas216_send_messageout - handle bus service to send a message
1302 * @info: interface which caused bus service
1303 *
1304 * Handle bus service to send a message.
1305 * Note: We do not allow the device to change the data direction!
1306 */
1307static void fas216_send_messageout(FAS216_Info *info, int start)
1308{
1309	unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1310
1311	fas216_checkmagic(info);
1312
1313	fas216_cmd(info, CMD_FLUSHFIFO);
1314
1315	if (tot_msglen) {
1316		struct message *msg;
1317		int msgnr = 0;
1318
1319		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1320			int i;
1321
1322			for (i = start; i < msg->length; i++)
1323				fas216_writeb(info, REG_FF, msg->msg[i]);
1324
1325			msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1326			start = 0;
1327		}
1328	} else
1329		fas216_writeb(info, REG_FF, NOP);
1330
1331	fas216_cmd(info, CMD_TRANSFERINFO);
1332
1333	info->scsi.phase = PHASE_MSGOUT;
1334}
1335
1336/**
1337 * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
1338 * @info: interface which caused bus service interrupt
1339 * @stat: Status register contents
1340 * @is: SCSI Status register contents
1341 *
1342 * Handle a bus service interrupt from FAS216 chip
1343 */
1344static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1345{
1346	fas216_checkmagic(info);
1347
1348	fas216_log(info, LOG_BUSSERVICE,
1349		   "bus service: stat=%02x is=%02x phase=%02x",
1350		   stat, is, info->scsi.phase);
1351
1352	switch (info->scsi.phase) {
1353	case PHASE_SELECTION:
1354		if ((is & IS_BITS) != IS_MSGBYTESENT)
1355			goto bad_is;
1356		break;
1357
1358	case PHASE_SELSTEPS:
1359		switch (is & IS_BITS) {
1360		case IS_SELARB:
1361		case IS_MSGBYTESENT:
1362			goto bad_is;
1363
1364		case IS_NOTCOMMAND:
1365		case IS_EARLYPHASE:
1366			if ((stat & STAT_BUSMASK) == STAT_MESGIN)
1367				break;
1368			goto bad_is;
1369
1370		case IS_COMPLETE:
1371			break;
1372		}
1373
1374	default:
1375		break;
1376	}
1377
1378	fas216_cmd(info, CMD_NOP);
1379
1380#define STATE(st,ph) ((ph) << 3 | (st))
1381	/* This table describes the legal SCSI state transitions,
1382	 * as described by the SCSI II spec.
1383	 */
1384	switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
1385	case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In      */
1386	case STATE(STAT_DATAIN, PHASE_MSGOUT):  /* Message Out  -> Data In      */
1387	case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command      -> Data In      */
1388	case STATE(STAT_DATAIN, PHASE_MSGIN):   /* Message In   -> Data In      */
1389		info->scsi.phase = PHASE_DATAIN;
1390		fas216_transfer(info);
1391		return;
1392
1393	case STATE(STAT_DATAIN, PHASE_DATAIN):  /* Data In      -> Data In      */
1394	case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out     -> Data Out     */
1395		fas216_cleanuptransfer(info);
1396		fas216_transfer(info);
1397		return;
1398
1399	case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out     */
1400	case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out  -> Data Out     */
1401	case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command      -> Data Out     */
1402	case STATE(STAT_DATAOUT, PHASE_MSGIN):  /* Message In   -> Data Out     */
1403		fas216_cmd(info, CMD_FLUSHFIFO);
1404		info->scsi.phase = PHASE_DATAOUT;
1405		fas216_transfer(info);
1406		return;
1407
1408	case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out     -> Status       */
1409	case STATE(STAT_STATUS, PHASE_DATAIN):  /* Data In      -> Status       */
1410		fas216_stoptransfer(info);
1411	case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status       */
1412	case STATE(STAT_STATUS, PHASE_MSGOUT):  /* Message Out  -> Status       */
1413	case STATE(STAT_STATUS, PHASE_COMMAND): /* Command      -> Status       */
1414	case STATE(STAT_STATUS, PHASE_MSGIN):   /* Message In   -> Status       */
1415		fas216_cmd(info, CMD_INITCMDCOMPLETE);
1416		info->scsi.phase = PHASE_STATUS;
1417		return;
1418
1419	case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out     -> Message In   */
1420	case STATE(STAT_MESGIN, PHASE_DATAIN):  /* Data In      -> Message In   */
1421		fas216_stoptransfer(info);
1422	case STATE(STAT_MESGIN, PHASE_COMMAND):	/* Command	-> Message In	*/
1423	case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In   */
1424	case STATE(STAT_MESGIN, PHASE_MSGOUT):  /* Message Out  -> Message In   */
1425		info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1426		fas216_cmd(info, CMD_FLUSHFIFO);
1427		fas216_cmd(info, CMD_TRANSFERINFO);
1428		info->scsi.phase = PHASE_MSGIN;
1429		return;
1430
1431	case STATE(STAT_MESGIN, PHASE_MSGIN):
1432		info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1433		fas216_cmd(info, CMD_TRANSFERINFO);
1434		return;
1435
1436	case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out  -> Command      */
1437	case STATE(STAT_COMMAND, PHASE_MSGIN):  /* Message In   -> Command      */
1438		fas216_send_command(info);
1439		info->scsi.phase = PHASE_COMMAND;
1440		return;
1441
1442
1443	/*
1444	 * Selection    -> Message Out
1445	 */
1446	case STATE(STAT_MESGOUT, PHASE_SELECTION):
1447		fas216_send_messageout(info, 1);
1448		return;
1449
1450	/*
1451	 * Message Out  -> Message Out
1452	 */
1453	case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
1454	case STATE(STAT_MESGOUT, PHASE_MSGOUT):
1455		/*
1456		 * If we get another message out phase, this usually
1457		 * means some parity error occurred.  Resend complete
1458		 * set of messages.  If we have more than one byte to
1459		 * send, we need to assert ATN again.
1460		 */
1461		if (info->device[info->SCpnt->device->id].parity_check) {
1462			/*
1463			 * We were testing... good, the device
1464			 * supports parity checking.
1465			 */
1466			info->device[info->SCpnt->device->id].parity_check = 0;
1467			info->device[info->SCpnt->device->id].parity_enabled = 1;
1468			fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1469		}
1470
1471		if (msgqueue_msglength(&info->scsi.msgs) > 1)
1472			fas216_cmd(info, CMD_SETATN);
1473		/*FALLTHROUGH*/
1474
1475	/*
1476	 * Any          -> Message Out
1477	 */
1478	case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
1479		fas216_send_messageout(info, 0);
1480		return;
1481
1482	/* Error recovery rules.
1483	 *   These either attempt to abort or retry the operation.
1484	 * TODO: we need more of these
1485	 */
1486	case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command      -> Command      */
1487		/* error - we've sent out all the command bytes
1488		 * we have.
1489		 * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
1490		 * to include the command bytes sent for this to work
1491		 * correctly.
1492		 */
1493		printk(KERN_ERR "scsi%d.%c: "
1494			"target trying to receive more command bytes\n",
1495			info->host->host_no, fas216_target(info));
1496		fas216_cmd(info, CMD_SETATN);
1497		fas216_set_stc(info, 15);
1498		fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
1499		msgqueue_flush(&info->scsi.msgs);
1500		msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1501		info->scsi.phase = PHASE_MSGOUT_EXPECT;
1502		return;
1503	}
1504
1505	if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
1506		printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
1507			info->host->host_no, fas216_target(info),
1508			fas216_bus_phase(stat));
1509		msgqueue_flush(&info->scsi.msgs);
1510		fas216_cmd(info, CMD_SETATN);
1511		msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1512		info->scsi.phase = PHASE_MSGOUT_EXPECT;
1513		info->scsi.aborting = 1;
1514		fas216_cmd(info, CMD_TRANSFERINFO);
1515		return;
1516	}
1517	printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
1518		info->host->host_no, fas216_target(info),
1519		fas216_bus_phase(stat),
1520		fas216_drv_phase(info));
1521	print_debug_list();
1522	return;
1523
1524bad_is:
1525	fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
1526	fas216_dumpstate(info);
1527	print_debug_list();
1528
1529	fas216_done(info, DID_ERROR);
1530}
1531
1532/**
1533 * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
1534 * @info: interface which caused function done interrupt
1535 * @stat: Status register contents
1536 * @is: SCSI Status register contents
1537 *
1538 * Handle a function done interrupt from FAS216 chip
1539 */
1540static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1541{
1542	unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
1543
1544	fas216_checkmagic(info);
1545
1546	fas216_log(info, LOG_FUNCTIONDONE,
1547		   "function done: stat=%02x is=%02x phase=%02x",
1548		   stat, is, info->scsi.phase);
1549
1550	switch (info->scsi.phase) {
1551	case PHASE_STATUS:			/* status phase - read status and msg	*/
1552		if (fifo_len != 2) {
1553			fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
1554		}
1555		/*
1556		 * Read status then message byte.
1557		 */
1558		info->scsi.SCp.Status = fas216_readb(info, REG_FF);
1559		info->scsi.SCp.Message = fas216_readb(info, REG_FF);
1560		info->scsi.phase = PHASE_DONE;
1561		fas216_cmd(info, CMD_MSGACCEPTED);
1562		break;
1563
1564	case PHASE_IDLE:
1565	case PHASE_SELECTION:
1566	case PHASE_SELSTEPS:
1567		break;
1568
1569	case PHASE_MSGIN:			/* message in phase			*/
1570		if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
1571			info->scsi.msgin_fifo = fifo_len;
1572			fas216_message(info);
1573			break;
1574		}
1575
1576	default:
1577		fas216_log(info, 0, "internal phase %s for function done?"
1578			"  What do I do with this?",
1579			fas216_target(info), fas216_drv_phase(info));
1580	}
1581}
1582
1583static void fas216_bus_reset(FAS216_Info *info)
1584{
1585	neg_t sync_state;
1586	int i;
1587
1588	msgqueue_flush(&info->scsi.msgs);
1589
1590	sync_state = neg_invalid;
1591
1592#ifdef SCSI2_SYNC
1593	if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
1594		sync_state = neg_wait;
1595#endif
1596
1597	info->scsi.phase = PHASE_IDLE;
1598	info->SCpnt = NULL; /* bug! */
1599	memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
1600
1601	for (i = 0; i < 8; i++) {
1602		info->device[i].disconnect_ok	= info->ifcfg.disconnect_ok;
1603		info->device[i].sync_state	= sync_state;
1604		info->device[i].period		= info->ifcfg.asyncperiod / 4;
1605		info->device[i].stp		= info->scsi.async_stp;
1606		info->device[i].sof		= 0;
1607		info->device[i].wide_xfer	= 0;
1608	}
1609
1610	info->rst_bus_status = 1;
1611	wake_up(&info->eh_wait);
1612}
1613
1614/**
1615 * fas216_intr - handle interrupts to progress a command
1616 * @info: interface to service
1617 *
1618 * Handle interrupts from the interface to progress a command
1619 */
1620irqreturn_t fas216_intr(FAS216_Info *info)
1621{
1622	unsigned char inst, is, stat;
1623	int handled = IRQ_NONE;
1624
1625	fas216_checkmagic(info);
1626
1627	stat = fas216_readb(info, REG_STAT);
1628	is = fas216_readb(info, REG_IS);
1629	inst = fas216_readb(info, REG_INST);
1630
1631	add_debug_list(stat, is, inst, info->scsi.phase);
1632
1633	if (stat & STAT_INT) {
1634		if (inst & INST_BUSRESET) {
1635			fas216_log(info, 0, "bus reset detected");
1636			fas216_bus_reset(info);
1637			scsi_report_bus_reset(info->host, 0);
1638		} else if (inst & INST_ILLEGALCMD) {
1639			fas216_log(info, LOG_ERROR, "illegal command given\n");
1640			fas216_dumpstate(info);
1641			print_debug_list();
1642		} else if (inst & INST_DISCONNECT)
1643			fas216_disconnect_intr(info);
1644		else if (inst & INST_RESELECTED)	/* reselected			*/
1645			fas216_reselected_intr(info);
1646		else if (inst & INST_BUSSERVICE)	/* bus service request		*/
1647			fas216_busservice_intr(info, stat, is);
1648		else if (inst & INST_FUNCDONE)		/* function done		*/
1649			fas216_funcdone_intr(info, stat, is);
1650		else
1651		    	fas216_log(info, 0, "unknown interrupt received:"
1652				" phase %s inst %02X is %02X stat %02X",
1653				fas216_drv_phase(info), inst, is, stat);
1654		handled = IRQ_HANDLED;
1655	}
1656	return handled;
1657}
1658
1659static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
1660{
1661	int tot_msglen;
1662
1663	/* following what the ESP driver says */
1664	fas216_set_stc(info, 0);
1665	fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1666
1667	/* flush FIFO */
1668	fas216_cmd(info, CMD_FLUSHFIFO);
1669
1670	/* load bus-id and timeout */
1671	fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1672	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1673
1674	/* synchronous transfers */
1675	fas216_set_sync(info, SCpnt->device->id);
1676
1677	tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1678
1679#ifdef DEBUG_MESSAGES
1680	{
1681		struct message *msg;
1682		int msgnr = 0, i;
1683
1684		printk("scsi%d.%c: message out: ",
1685			info->host->host_no, '0' + SCpnt->device->id);
1686		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1687			printk("{ ");
1688			for (i = 0; i < msg->length; i++)
1689				printk("%02x ", msg->msg[i]);
1690			printk("} ");
1691		}
1692		printk("\n");
1693	}
1694#endif
1695
1696	if (tot_msglen == 1 || tot_msglen == 3) {
1697		/*
1698		 * We have an easy message length to send...
1699		 */
1700		struct message *msg;
1701		int msgnr = 0, i;
1702
1703		info->scsi.phase = PHASE_SELSTEPS;
1704
1705		/* load message bytes */
1706		while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1707			for (i = 0; i < msg->length; i++)
1708				fas216_writeb(info, REG_FF, msg->msg[i]);
1709			msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1710		}
1711
1712		/* load command */
1713		for (i = 0; i < SCpnt->cmd_len; i++)
1714			fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
1715
1716		if (tot_msglen == 1)
1717			fas216_cmd(info, CMD_SELECTATN);
1718		else
1719			fas216_cmd(info, CMD_SELECTATN3);
1720	} else {
1721		/*
1722		 * We have an unusual number of message bytes to send.
1723		 *  Load first byte into fifo, and issue SELECT with ATN and
1724		 *  stop steps.
1725		 */
1726		struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1727
1728		fas216_writeb(info, REG_FF, msg->msg[0]);
1729		msg->fifo = 1;
1730
1731		fas216_cmd(info, CMD_SELECTATNSTOP);
1732	}
1733}
1734
1735/*
1736 * Decide whether we need to perform a parity test on this device.
1737 * Can also be used to force parity error conditions during initial
1738 * information transfer phase (message out) for test purposes.
1739 */
1740static int parity_test(FAS216_Info *info, int target)
1741{
1742	return info->device[target].parity_check;
1743}
1744
1745static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
1746{
1747	int disconnect_ok;
1748
1749	/*
1750	 * claim host busy
1751	 */
1752	info->scsi.phase = PHASE_SELECTION;
1753	info->scsi.SCp = SCpnt->SCp;
1754	info->SCpnt = SCpnt;
1755	info->dma.transfer_type = fasdma_none;
1756
1757	if (parity_test(info, SCpnt->device->id))
1758		fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
1759	else
1760		fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1761
1762	/*
1763	 * Don't allow request sense commands to disconnect.
1764	 */
1765	disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
1766			info->device[SCpnt->device->id].disconnect_ok;
1767
1768	/*
1769	 * build outgoing message bytes
1770	 */
1771	msgqueue_flush(&info->scsi.msgs);
1772	msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
1773
1774	/*
1775	 * add tag message if required
1776	 */
1777	if (SCpnt->tag)
1778		msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
1779
1780	do {
1781#ifdef SCSI2_SYNC
1782		if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
1783		     info->device[SCpnt->device->id].sync_state == neg_complete) &&
1784		    (SCpnt->cmnd[0] == REQUEST_SENSE ||
1785		     SCpnt->cmnd[0] == INQUIRY)) {
1786			info->device[SCpnt->device->id].sync_state = neg_inprogress;
1787			msgqueue_addmsg(&info->scsi.msgs, 5,
1788					EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1789					1000 / info->ifcfg.clockrate,
1790					info->ifcfg.sync_max_depth);
1791			break;
1792		}
1793#endif
1794	} while (0);
1795
1796	__fas216_start_command(info, SCpnt);
1797}
1798
1799static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
1800{
1801#ifdef SCSI2_TAG
1802	/*
1803	 * tagged queuing - allocate a new tag to this command
1804	 */
1805	if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
1806	    SCpnt->cmnd[0] != INQUIRY) {
1807	    SCpnt->device->current_tag += 1;
1808		if (SCpnt->device->current_tag == 0)
1809		    SCpnt->device->current_tag = 1;
1810			SCpnt->tag = SCpnt->device->current_tag;
1811	} else
1812#endif
1813		set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
1814
1815	info->stats.removes += 1;
1816	switch (SCpnt->cmnd[0]) {
1817	case WRITE_6:
1818	case WRITE_10:
1819	case WRITE_12:
1820		info->stats.writes += 1;
1821		break;
1822	case READ_6:
1823	case READ_10:
1824	case READ_12:
1825		info->stats.reads += 1;
1826		break;
1827	default:
1828		info->stats.miscs += 1;
1829		break;
1830	}
1831}
1832
1833static void fas216_do_bus_device_reset(FAS216_Info *info,
1834				       struct scsi_cmnd *SCpnt)
1835{
1836	struct message *msg;
1837
1838	/*
1839	 * claim host busy
1840	 */
1841	info->scsi.phase = PHASE_SELECTION;
1842	info->scsi.SCp = SCpnt->SCp;
1843	info->SCpnt = SCpnt;
1844	info->dma.transfer_type = fasdma_none;
1845
1846	fas216_log(info, LOG_ERROR, "sending bus device reset");
1847
1848	msgqueue_flush(&info->scsi.msgs);
1849	msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
1850
1851	/* following what the ESP driver says */
1852	fas216_set_stc(info, 0);
1853	fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1854
1855	/* flush FIFO */
1856	fas216_cmd(info, CMD_FLUSHFIFO);
1857
1858	/* load bus-id and timeout */
1859	fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1860	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1861
1862	/* synchronous transfers */
1863	fas216_set_sync(info, SCpnt->device->id);
1864
1865	msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1866
1867	fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
1868	msg->fifo = 1;
1869
1870	fas216_cmd(info, CMD_SELECTATNSTOP);
1871}
1872
1873/**
1874 * fas216_kick - kick a command to the interface
1875 * @info: our host interface to kick
1876 *
1877 * Kick a command to the interface, interface should be idle.
1878 * Notes: Interrupts are always disabled!
1879 */
1880static void fas216_kick(FAS216_Info *info)
1881{
1882	struct scsi_cmnd *SCpnt = NULL;
1883#define TYPE_OTHER	0
1884#define TYPE_RESET	1
1885#define TYPE_QUEUE	2
1886	int where_from = TYPE_OTHER;
1887
1888	fas216_checkmagic(info);
1889
1890	/*
1891	 * Obtain the next command to process.
1892	 */
1893	do {
1894		if (info->rstSCpnt) {
1895			SCpnt = info->rstSCpnt;
1896			/* don't remove it */
1897			where_from = TYPE_RESET;
1898			break;
1899		}
1900
1901		if (info->reqSCpnt) {
1902			SCpnt = info->reqSCpnt;
1903			info->reqSCpnt = NULL;
1904			break;
1905		}
1906
1907		if (info->origSCpnt) {
1908			SCpnt = info->origSCpnt;
1909			info->origSCpnt = NULL;
1910			break;
1911		}
1912
1913		/* retrieve next command */
1914		if (!SCpnt) {
1915			SCpnt = queue_remove_exclude(&info->queues.issue,
1916						     info->busyluns);
1917			where_from = TYPE_QUEUE;
1918			break;
1919		}
1920	} while (0);
1921
1922	if (!SCpnt) {
1923		/*
1924		 * no command pending, so enable reselection.
1925		 */
1926		fas216_cmd(info, CMD_ENABLESEL);
1927		return;
1928	}
1929
1930	/*
1931	 * We're going to start a command, so disable reselection
1932	 */
1933	fas216_cmd(info, CMD_DISABLESEL);
1934
1935	if (info->scsi.disconnectable && info->SCpnt) {
1936		fas216_log(info, LOG_CONNECT,
1937			"moved command for %d to disconnected queue",
1938			info->SCpnt->device->id);
1939		queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
1940		info->scsi.disconnectable = 0;
1941		info->SCpnt = NULL;
1942	}
1943
1944	fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
1945			   "starting");
1946
1947	switch (where_from) {
1948	case TYPE_QUEUE:
1949		fas216_allocate_tag(info, SCpnt);
1950	case TYPE_OTHER:
1951		fas216_start_command(info, SCpnt);
1952		break;
1953	case TYPE_RESET:
1954		fas216_do_bus_device_reset(info, SCpnt);
1955		break;
1956	}
1957
1958	fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
1959		info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1960
1961	/*
1962	 * should now get either DISCONNECT or
1963	 * (FUNCTION DONE with BUS SERVICE) interrupt
1964	 */
1965}
1966
1967/*
1968 * Clean up from issuing a BUS DEVICE RESET message to a device.
1969 */
1970static void fas216_devicereset_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
1971				    unsigned int result)
1972{
1973	fas216_log(info, LOG_ERROR, "fas216 device reset complete");
1974
1975	info->rstSCpnt = NULL;
1976	info->rst_dev_status = 1;
1977	wake_up(&info->eh_wait);
1978}
1979
1980/**
1981 * fas216_rq_sns_done - Finish processing automatic request sense command
1982 * @info: interface that completed
1983 * @SCpnt: command that completed
1984 * @result: driver byte of result
1985 *
1986 * Finish processing automatic request sense command
1987 */
1988static void fas216_rq_sns_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
1989			       unsigned int result)
1990{
1991	fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
1992		   "request sense complete, result=0x%04x%02x%02x",
1993		   result, SCpnt->SCp.Message, SCpnt->SCp.Status);
1994
1995	if (result != DID_OK || SCpnt->SCp.Status != GOOD)
1996		/*
1997		 * Something went wrong.  Make sure that we don't
1998		 * have valid data in the sense buffer that could
1999		 * confuse the higher levels.
2000		 */
2001		memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2002//printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
2003//{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
2004	/*
2005	 * Note that we don't set SCpnt->result, since that should
2006	 * reflect the status of the command that we were asked by
2007	 * the upper layers to process.  This would have been set
2008	 * correctly by fas216_std_done.
2009	 */
2010	scsi_eh_restore_cmnd(SCpnt, &info->ses);
2011	SCpnt->scsi_done(SCpnt);
2012}
2013
2014/**
2015 * fas216_std_done - finish processing of standard command
2016 * @info: interface that completed
2017 * @SCpnt: command that completed
2018 * @result: driver byte of result
2019 *
2020 * Finish processing of standard command
2021 */
2022static void
2023fas216_std_done(FAS216_Info *info, struct scsi_cmnd *SCpnt, unsigned int result)
2024{
2025	info->stats.fins += 1;
2026
2027	SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
2028			info->scsi.SCp.Status;
2029
2030	fas216_log_command(info, LOG_CONNECT, SCpnt,
2031		"command complete, result=0x%08x", SCpnt->result);
2032
2033	/*
2034	 * If the driver detected an error, we're all done.
2035	 */
2036	if (host_byte(SCpnt->result) != DID_OK ||
2037	    msg_byte(SCpnt->result) != COMMAND_COMPLETE)
2038		goto done;
2039
2040	/*
2041	 * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
2042	 * status, request the sense information.
2043	 */
2044	if (status_byte(SCpnt->result) == CHECK_CONDITION ||
2045	    status_byte(SCpnt->result) == COMMAND_TERMINATED)
2046		goto request_sense;
2047
2048	/*
2049	 * If the command did not complete with GOOD status,
2050	 * we are all done here.
2051	 */
2052	if (status_byte(SCpnt->result) != GOOD)
2053		goto done;
2054
2055	/*
2056	 * We have successfully completed a command.  Make sure that
2057	 * we do not have any buffers left to transfer.  The world
2058	 * is not perfect, and we seem to occasionally hit this.
2059	 * It can be indicative of a buggy driver, target or the upper
2060	 * levels of the SCSI code.
2061	 */
2062	if (info->scsi.SCp.ptr) {
2063		switch (SCpnt->cmnd[0]) {
2064		case INQUIRY:
2065		case START_STOP:
2066		case MODE_SENSE:
2067			break;
2068
2069		default:
2070			printk(KERN_ERR "scsi%d.%c: incomplete data transfer "
2071				"detected: res=%08X ptr=%p len=%X CDB: ",
2072				info->host->host_no, '0' + SCpnt->device->id,
2073				SCpnt->result, info->scsi.SCp.ptr,
2074				info->scsi.SCp.this_residual);
2075			__scsi_print_command(SCpnt->cmnd);
2076			SCpnt->result &= ~(255 << 16);
2077			SCpnt->result |= DID_BAD_TARGET << 16;
2078			goto request_sense;
2079		}
2080	}
2081
2082done:
2083	if (SCpnt->scsi_done) {
2084		SCpnt->scsi_done(SCpnt);
2085		return;
2086	}
2087
2088	panic("scsi%d.H: null scsi_done function in fas216_done",
2089		info->host->host_no);
2090
2091
2092request_sense:
2093	if (SCpnt->cmnd[0] == REQUEST_SENSE)
2094		goto done;
2095
2096	scsi_eh_prep_cmnd(SCpnt, &info->ses, NULL, 0, ~0);
2097	fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2098			  "requesting sense");
2099	init_SCp(SCpnt);
2100	SCpnt->SCp.Message = 0;
2101	SCpnt->SCp.Status = 0;
2102	SCpnt->tag = 0;
2103	SCpnt->host_scribble = (void *)fas216_rq_sns_done;
2104
2105	/*
2106	 * Place this command into the high priority "request
2107	 * sense" slot.  This will be the very next command
2108	 * executed, unless a target connects to us.
2109	 */
2110	if (info->reqSCpnt)
2111		printk(KERN_WARNING "scsi%d.%c: loosing request command\n",
2112			info->host->host_no, '0' + SCpnt->device->id);
2113	info->reqSCpnt = SCpnt;
2114}
2115
2116/**
2117 * fas216_done - complete processing for current command
2118 * @info: interface that completed
2119 * @result: driver byte of result
2120 *
2121 * Complete processing for current command
2122 */
2123static void fas216_done(FAS216_Info *info, unsigned int result)
2124{
2125	void (*fn)(FAS216_Info *, struct scsi_cmnd *, unsigned int);
2126	struct scsi_cmnd *SCpnt;
2127	unsigned long flags;
2128
2129	fas216_checkmagic(info);
2130
2131	if (!info->SCpnt)
2132		goto no_command;
2133
2134	SCpnt = info->SCpnt;
2135	info->SCpnt = NULL;
2136    	info->scsi.phase = PHASE_IDLE;
2137
2138	if (info->scsi.aborting) {
2139		fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
2140		result = DID_ABORT;
2141		info->scsi.aborting = 0;
2142	}
2143
2144	/*
2145	 * Sanity check the completion - if we have zero bytes left
2146	 * to transfer, we should not have a valid pointer.
2147	 */
2148	if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2149		printk("scsi%d.%c: zero bytes left to transfer, but "
2150		       "buffer pointer still valid: ptr=%p len=%08x CDB: ",
2151		       info->host->host_no, '0' + SCpnt->device->id,
2152		       info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
2153		info->scsi.SCp.ptr = NULL;
2154		__scsi_print_command(SCpnt->cmnd);
2155	}
2156
2157	/*
2158	 * Clear down this command as completed.  If we need to request
2159	 * the sense information, fas216_kick will re-assert the busy
2160	 * status.
2161	 */
2162	info->device[SCpnt->device->id].parity_check = 0;
2163	clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
2164
2165	fn = (void (*)(FAS216_Info *, struct scsi_cmnd *, unsigned int))SCpnt->host_scribble;
2166	fn(info, SCpnt, result);
2167
2168	if (info->scsi.irq != NO_IRQ) {
2169		spin_lock_irqsave(&info->host_lock, flags);
2170		if (info->scsi.phase == PHASE_IDLE)
2171			fas216_kick(info);
2172		spin_unlock_irqrestore(&info->host_lock, flags);
2173	}
2174	return;
2175
2176no_command:
2177	panic("scsi%d.H: null command in fas216_done",
2178		info->host->host_no);
2179}
2180
2181/**
2182 * fas216_queue_command - queue a command for adapter to process.
2183 * @SCpnt: Command to queue
2184 * @done: done function to call once command is complete
2185 *
2186 * Queue a command for adapter to process.
2187 * Returns: 0 on success, else error.
2188 * Notes: io_request_lock is held, interrupts are disabled.
2189 */
2190int fas216_queue_command(struct scsi_cmnd *SCpnt,
2191			 void (*done)(struct scsi_cmnd *))
2192{
2193	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2194	int result;
2195
2196	fas216_checkmagic(info);
2197
2198	fas216_log_command(info, LOG_CONNECT, SCpnt,
2199			   "received command (%p)", SCpnt);
2200
2201	SCpnt->scsi_done = done;
2202	SCpnt->host_scribble = (void *)fas216_std_done;
2203	SCpnt->result = 0;
2204
2205	init_SCp(SCpnt);
2206
2207	info->stats.queues += 1;
2208	SCpnt->tag = 0;
2209
2210	spin_lock(&info->host_lock);
2211
2212	/*
2213	 * Add command into execute queue and let it complete under
2214	 * whatever scheme we're using.
2215	 */
2216	result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
2217
2218	/*
2219	 * If we successfully added the command,
2220	 * kick the interface to get it moving.
2221	 */
2222	if (result == 0 && info->scsi.phase == PHASE_IDLE)
2223		fas216_kick(info);
2224	spin_unlock(&info->host_lock);
2225
2226	fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
2227		result ? "failure" : "success");
2228
2229	return result;
2230}
2231
2232/**
2233 * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
2234 * @SCpnt: Command to wake
2235 *
2236 * Trigger restart of a waiting thread in fas216_command
2237 */
2238static void fas216_internal_done(struct scsi_cmnd *SCpnt)
2239{
2240	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2241
2242	fas216_checkmagic(info);
2243
2244	info->internal_done = 1;
2245}
2246
2247/**
2248 * fas216_noqueue_command - process a command for the adapter.
2249 * @SCpnt: Command to queue
2250 *
2251 * Queue a command for adapter to process.
2252 * Returns: scsi result code.
2253 * Notes: io_request_lock is held, interrupts are disabled.
2254 */
2255int fas216_noqueue_command(struct scsi_cmnd *SCpnt,
2256			   void (*done)(struct scsi_cmnd *))
2257{
2258	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2259
2260	fas216_checkmagic(info);
2261
2262	/*
2263	 * We should only be using this if we don't have an interrupt.
2264	 * Provide some "incentive" to use the queueing code.
2265	 */
2266	BUG_ON(info->scsi.irq != NO_IRQ);
2267
2268	info->internal_done = 0;
2269	fas216_queue_command(SCpnt, fas216_internal_done);
2270
2271	/*
2272	 * This wastes time, since we can't return until the command is
2273	 * complete. We can't sleep either since we may get re-entered!
2274	 * However, we must re-enable interrupts, or else we'll be
2275	 * waiting forever.
2276	 */
2277	spin_unlock_irq(info->host->host_lock);
2278
2279	while (!info->internal_done) {
2280		/*
2281		 * If we don't have an IRQ, then we must poll the card for
2282		 * it's interrupt, and use that to call this driver's
2283		 * interrupt routine.  That way, we keep the command
2284		 * progressing.  Maybe we can add some inteligence here
2285		 * and go to sleep if we know that the device is going
2286		 * to be some time (eg, disconnected).
2287		 */
2288		if (fas216_readb(info, REG_STAT) & STAT_INT) {
2289			spin_lock_irq(info->host->host_lock);
2290			fas216_intr(info);
2291			spin_unlock_irq(info->host->host_lock);
2292		}
2293	}
2294
2295	spin_lock_irq(info->host->host_lock);
2296
2297	done(SCpnt);
2298
2299	return 0;
2300}
2301
2302/*
2303 * Error handler timeout function.  Indicate that we timed out,
2304 * and wake up any error handler process so it can continue.
2305 */
2306static void fas216_eh_timer(unsigned long data)
2307{
2308	FAS216_Info *info = (FAS216_Info *)data;
2309
2310	fas216_log(info, LOG_ERROR, "error handling timed out\n");
2311
2312	del_timer(&info->eh_timer);
2313
2314	if (info->rst_bus_status == 0)
2315		info->rst_bus_status = -1;
2316	if (info->rst_dev_status == 0)
2317		info->rst_dev_status = -1;
2318
2319	wake_up(&info->eh_wait);
2320}
2321
2322enum res_find {
2323	res_failed,		/* not found			*/
2324	res_success,		/* command on issue queue	*/
2325	res_hw_abort		/* command on disconnected dev	*/
2326};
2327
2328/**
2329 * fas216_do_abort - decide how to abort a command
2330 * @SCpnt: command to abort
2331 *
2332 * Decide how to abort a command.
2333 * Returns: abort status
2334 */
2335static enum res_find fas216_find_command(FAS216_Info *info,
2336					 struct scsi_cmnd *SCpnt)
2337{
2338	enum res_find res = res_failed;
2339
2340	if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
2341		/*
2342		 * The command was on the issue queue, and has not been
2343		 * issued yet.  We can remove the command from the queue,
2344		 * and acknowledge the abort.  Neither the device nor the
2345		 * interface know about the command.
2346		 */
2347		printk("on issue queue ");
2348
2349		res = res_success;
2350	} else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
2351		/*
2352		 * The command was on the disconnected queue.  We must
2353		 * reconnect with the device if possible, and send it
2354		 * an abort message.
2355		 */
2356		printk("on disconnected queue ");
2357
2358		res = res_hw_abort;
2359	} else if (info->SCpnt == SCpnt) {
2360		printk("executing ");
2361
2362		switch (info->scsi.phase) {
2363		/*
2364		 * If the interface is idle, and the command is 'disconnectable',
2365		 * then it is the same as on the disconnected queue.
2366		 */
2367		case PHASE_IDLE:
2368			if (info->scsi.disconnectable) {
2369				info->scsi.disconnectable = 0;
2370				info->SCpnt = NULL;
2371				res = res_hw_abort;
2372			}
2373			break;
2374
2375		default:
2376			break;
2377		}
2378	} else if (info->origSCpnt == SCpnt) {
2379		/*
2380		 * The command will be executed next, but a command
2381		 * is currently using the interface.  This is similar to
2382		 * being on the issue queue, except the busylun bit has
2383		 * been set.
2384		 */
2385		info->origSCpnt = NULL;
2386		clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
2387		printk("waiting for execution ");
2388		res = res_success;
2389	} else
2390		printk("unknown ");
2391
2392	return res;
2393}
2394
2395/**
2396 * fas216_eh_abort - abort this command
2397 * @SCpnt: command to abort
2398 *
2399 * Abort this command.
2400 * Returns: FAILED if unable to abort
2401 * Notes: io_request_lock is taken, and irqs are disabled
2402 */
2403int fas216_eh_abort(struct scsi_cmnd *SCpnt)
2404{
2405	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2406	int result = FAILED;
2407
2408	fas216_checkmagic(info);
2409
2410	info->stats.aborts += 1;
2411
2412	printk(KERN_WARNING "scsi%d: abort command ", info->host->host_no);
2413	__scsi_print_command(SCpnt->cmnd);
2414
2415	print_debug_list();
2416	fas216_dumpstate(info);
2417
2418	printk(KERN_WARNING "scsi%d: abort %p ", info->host->host_no, SCpnt);
2419
2420	switch (fas216_find_command(info, SCpnt)) {
2421	/*
2422	 * We found the command, and cleared it out.  Either
2423	 * the command is still known to be executing on the
2424	 * target, or the busylun bit is not set.
2425	 */
2426	case res_success:
2427		printk("success\n");
2428		result = SUCCESS;
2429		break;
2430
2431	/*
2432	 * We need to reconnect to the target and send it an
2433	 * ABORT or ABORT_TAG message.  We can only do this
2434	 * if the bus is free.
2435	 */
2436	case res_hw_abort:
2437
2438
2439	/*
2440	 * We are unable to abort the command for some reason.
2441	 */
2442	default:
2443	case res_failed:
2444		printk("failed\n");
2445		break;
2446	}
2447
2448	return result;
2449}
2450
2451/**
2452 * fas216_eh_device_reset - Reset the device associated with this command
2453 * @SCpnt: command specifing device to reset
2454 *
2455 * Reset the device associated with this command.
2456 * Returns: FAILED if unable to reset.
2457 * Notes: We won't be re-entered, so we'll only have one device
2458 * reset on the go at one time.
2459 */
2460int fas216_eh_device_reset(struct scsi_cmnd *SCpnt)
2461{
2462	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2463	unsigned long flags;
2464	int i, res = FAILED, target = SCpnt->device->id;
2465
2466	fas216_log(info, LOG_ERROR, "device reset for target %d", target);
2467
2468	spin_lock_irqsave(&info->host_lock, flags);
2469
2470	do {
2471		/*
2472		 * If we are currently connected to a device, and
2473		 * it is the device we want to reset, there is
2474		 * nothing we can do here.  Chances are it is stuck,
2475		 * and we need a bus reset.
2476		 */
2477		if (info->SCpnt && !info->scsi.disconnectable &&
2478		    info->SCpnt->device->id == SCpnt->device->id)
2479			break;
2480
2481		/*
2482		 * We're going to be resetting this device.  Remove
2483		 * all pending commands from the driver.  By doing
2484		 * so, we guarantee that we won't touch the command
2485		 * structures except to process the reset request.
2486		 */
2487		queue_remove_all_target(&info->queues.issue, target);
2488		queue_remove_all_target(&info->queues.disconnected, target);
2489		if (info->origSCpnt && info->origSCpnt->device->id == target)
2490			info->origSCpnt = NULL;
2491		if (info->reqSCpnt && info->reqSCpnt->device->id == target)
2492			info->reqSCpnt = NULL;
2493		for (i = 0; i < 8; i++)
2494			clear_bit(target * 8 + i, info->busyluns);
2495
2496		/*
2497		 * Hijack this SCSI command structure to send
2498		 * a bus device reset message to this device.
2499		 */
2500		SCpnt->host_scribble = (void *)fas216_devicereset_done;
2501
2502		info->rst_dev_status = 0;
2503		info->rstSCpnt = SCpnt;
2504
2505		if (info->scsi.phase == PHASE_IDLE)
2506			fas216_kick(info);
2507
2508		mod_timer(&info->eh_timer, jiffies + 30 * HZ);
2509		spin_unlock_irqrestore(&info->host_lock, flags);
2510
2511		/*
2512		 * Wait up to 30 seconds for the reset to complete.
2513		 */
2514		wait_event(info->eh_wait, info->rst_dev_status);
2515
2516		del_timer_sync(&info->eh_timer);
2517		spin_lock_irqsave(&info->host_lock, flags);
2518		info->rstSCpnt = NULL;
2519
2520		if (info->rst_dev_status == 1)
2521			res = SUCCESS;
2522	} while (0);
2523
2524	SCpnt->host_scribble = NULL;
2525	spin_unlock_irqrestore(&info->host_lock, flags);
2526
2527	fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
2528		   res == SUCCESS ? "success" : "failed");
2529
2530	return res;
2531}
2532
2533/**
2534 * fas216_eh_bus_reset - Reset the bus associated with the command
2535 * @SCpnt: command specifing bus to reset
2536 *
2537 * Reset the bus associated with the command.
2538 * Returns: FAILED if unable to reset.
2539 * Notes: Further commands are blocked.
2540 */
2541int fas216_eh_bus_reset(struct scsi_cmnd *SCpnt)
2542{
2543	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2544	unsigned long flags;
2545	struct scsi_device *SDpnt;
2546
2547	fas216_checkmagic(info);
2548	fas216_log(info, LOG_ERROR, "resetting bus");
2549
2550	info->stats.bus_resets += 1;
2551
2552	spin_lock_irqsave(&info->host_lock, flags);
2553
2554	/*
2555	 * Stop all activity on this interface.
2556	 */
2557	fas216_aborttransfer(info);
2558	fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2559
2560	/*
2561	 * Clear any pending interrupts.
2562	 */
2563	while (fas216_readb(info, REG_STAT) & STAT_INT)
2564		fas216_readb(info, REG_INST);
2565
2566	info->rst_bus_status = 0;
2567
2568	/*
2569	 * For each attached hard-reset device, clear out
2570	 * all command structures.  Leave the running
2571	 * command in place.
2572	 */
2573	shost_for_each_device(SDpnt, info->host) {
2574		int i;
2575
2576		if (SDpnt->soft_reset)
2577			continue;
2578
2579		queue_remove_all_target(&info->queues.issue, SDpnt->id);
2580		queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
2581		if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
2582			info->origSCpnt = NULL;
2583		if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
2584			info->reqSCpnt = NULL;
2585		info->SCpnt = NULL;
2586
2587		for (i = 0; i < 8; i++)
2588			clear_bit(SDpnt->id * 8 + i, info->busyluns);
2589	}
2590
2591	info->scsi.phase = PHASE_IDLE;
2592
2593	/*
2594	 * Reset the SCSI bus.  Device cleanup happens in
2595	 * the interrupt handler.
2596	 */
2597	fas216_cmd(info, CMD_RESETSCSI);
2598
2599	mod_timer(&info->eh_timer, jiffies + HZ);
2600	spin_unlock_irqrestore(&info->host_lock, flags);
2601
2602	/*
2603	 * Wait one second for the interrupt.
2604	 */
2605	wait_event(info->eh_wait, info->rst_bus_status);
2606	del_timer_sync(&info->eh_timer);
2607
2608	fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
2609		   info->rst_bus_status == 1 ? "success" : "failed");
2610
2611	return info->rst_bus_status == 1 ? SUCCESS : FAILED;
2612}
2613
2614/**
2615 * fas216_init_chip - Initialise FAS216 state after reset
2616 * @info: state structure for interface
2617 *
2618 * Initialise FAS216 state after reset
2619 */
2620static void fas216_init_chip(FAS216_Info *info)
2621{
2622	unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
2623	fas216_writeb(info, REG_CLKF, clock);
2624	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2625	fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
2626	fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2627	fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
2628	fas216_writeb(info, REG_SOF, 0);
2629	fas216_writeb(info, REG_STP, info->scsi.async_stp);
2630	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2631}
2632
2633/**
2634 * fas216_eh_host_reset - Reset the host associated with this command
2635 * @SCpnt: command specifing host to reset
2636 *
2637 * Reset the host associated with this command.
2638 * Returns: FAILED if unable to reset.
2639 * Notes: io_request_lock is taken, and irqs are disabled
2640 */
2641int fas216_eh_host_reset(struct scsi_cmnd *SCpnt)
2642{
2643	FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2644
2645	spin_lock_irq(info->host->host_lock);
2646
2647	fas216_checkmagic(info);
2648
2649	printk("scsi%d.%c: %s: resetting host\n",
2650		info->host->host_no, '0' + SCpnt->device->id, __func__);
2651
2652	/*
2653	 * Reset the SCSI chip.
2654	 */
2655	fas216_cmd(info, CMD_RESETCHIP);
2656
2657	/*
2658	 * Ugly ugly ugly!
2659	 * We need to release the host_lock and enable
2660	 * IRQs if we sleep, but we must relock and disable
2661	 * IRQs after the sleep.
2662	 */
2663	spin_unlock_irq(info->host->host_lock);
2664	msleep(50 * 1000/100);
2665	spin_lock_irq(info->host->host_lock);
2666
2667	/*
2668	 * Release the SCSI reset.
2669	 */
2670	fas216_cmd(info, CMD_NOP);
2671
2672	fas216_init_chip(info);
2673
2674	spin_unlock_irq(info->host->host_lock);
2675	return SUCCESS;
2676}
2677
2678#define TYPE_UNKNOWN	0
2679#define TYPE_NCR53C90	1
2680#define TYPE_NCR53C90A	2
2681#define TYPE_NCR53C9x	3
2682#define TYPE_Am53CF94	4
2683#define TYPE_EmFAS216	5
2684#define TYPE_QLFAS216	6
2685
2686static char *chip_types[] = {
2687	"unknown",
2688	"NS NCR53C90",
2689	"NS NCR53C90A",
2690	"NS NCR53C9x",
2691	"AMD Am53CF94",
2692	"Emulex FAS216",
2693	"QLogic FAS216"
2694};
2695
2696static int fas216_detect_type(FAS216_Info *info)
2697{
2698	int family, rev;
2699
2700	/*
2701	 * Reset the chip.
2702	 */
2703	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2704	udelay(50);
2705	fas216_writeb(info, REG_CMD, CMD_NOP);
2706
2707	/*
2708	 * Check to see if control reg 2 is present.
2709	 */
2710	fas216_writeb(info, REG_CNTL3, 0);
2711	fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
2712
2713	/*
2714	 * If we are unable to read back control reg 2
2715	 * correctly, it is not present, and we have a
2716	 * NCR53C90.
2717	 */
2718	if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
2719		return TYPE_NCR53C90;
2720
2721	/*
2722	 * Now, check control register 3
2723	 */
2724	fas216_writeb(info, REG_CNTL2, 0);
2725	fas216_writeb(info, REG_CNTL3, 0);
2726	fas216_writeb(info, REG_CNTL3, 5);
2727
2728	/*
2729	 * If we are unable to read the register back
2730	 * correctly, we have a NCR53C90A
2731	 */
2732	if (fas216_readb(info, REG_CNTL3) != 5)
2733		return TYPE_NCR53C90A;
2734
2735	/*
2736	 * Now read the ID from the chip.
2737	 */
2738	fas216_writeb(info, REG_CNTL3, 0);
2739
2740	fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
2741	fas216_writeb(info, REG_CNTL3, 0);
2742
2743	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2744	udelay(50);
2745	fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
2746
2747	fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
2748	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2749	udelay(50);
2750	fas216_writeb(info, REG_CMD, CMD_NOP);
2751
2752	rev     = fas216_readb(info, REG_ID);
2753	family  = rev >> 3;
2754	rev    &= 7;
2755
2756	switch (family) {
2757	case 0x01:
2758		if (rev == 4)
2759			return TYPE_Am53CF94;
2760		break;
2761
2762	case 0x02:
2763		switch (rev) {
2764		case 2:
2765			return TYPE_EmFAS216;
2766		case 3:
2767			return TYPE_QLFAS216;
2768		}
2769		break;
2770
2771	default:
2772		break;
2773	}
2774	printk("family %x rev %x\n", family, rev);
2775	return TYPE_NCR53C9x;
2776}
2777
2778/**
2779 * fas216_reset_state - Initialise driver internal state
2780 * @info: state to initialise
2781 *
2782 * Initialise driver internal state
2783 */
2784static void fas216_reset_state(FAS216_Info *info)
2785{
2786	int i;
2787
2788	fas216_checkmagic(info);
2789
2790	fas216_bus_reset(info);
2791
2792	/*
2793	 * Clear out all stale info in our state structure
2794	 */
2795	memset(info->busyluns, 0, sizeof(info->busyluns));
2796	info->scsi.disconnectable = 0;
2797	info->scsi.aborting = 0;
2798
2799	for (i = 0; i < 8; i++) {
2800		info->device[i].parity_enabled	= 0;
2801		info->device[i].parity_check	= 1;
2802	}
2803
2804	/*
2805	 * Drain all commands on disconnected queue
2806	 */
2807	while (queue_remove(&info->queues.disconnected) != NULL);
2808
2809	/*
2810	 * Remove executing commands.
2811	 */
2812	info->SCpnt     = NULL;
2813	info->reqSCpnt  = NULL;
2814	info->rstSCpnt  = NULL;
2815	info->origSCpnt = NULL;
2816}
2817
2818/**
2819 * fas216_init - initialise FAS/NCR/AMD SCSI structures.
2820 * @host: a driver-specific filled-out structure
2821 *
2822 * Initialise FAS/NCR/AMD SCSI structures.
2823 * Returns: 0 on success
2824 */
2825int fas216_init(struct Scsi_Host *host)
2826{
2827	FAS216_Info *info = (FAS216_Info *)host->hostdata;
2828
2829	info->magic_start    = MAGIC;
2830	info->magic_end      = MAGIC;
2831	info->host           = host;
2832	info->scsi.cfg[0]    = host->this_id | CNTL1_PERE;
2833	info->scsi.cfg[1]    = CNTL2_ENF | CNTL2_S2FE;
2834	info->scsi.cfg[2]    = info->ifcfg.cntl3 |
2835			       CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
2836	info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
2837
2838	info->rst_dev_status = -1;
2839	info->rst_bus_status = -1;
2840	init_waitqueue_head(&info->eh_wait);
2841	init_timer(&info->eh_timer);
2842	info->eh_timer.data  = (unsigned long)info;
2843	info->eh_timer.function = fas216_eh_timer;
2844
2845	spin_lock_init(&info->host_lock);
2846
2847	memset(&info->stats, 0, sizeof(info->stats));
2848
2849	msgqueue_initialise(&info->scsi.msgs);
2850
2851	if (!queue_initialise(&info->queues.issue))
2852		return -ENOMEM;
2853
2854	if (!queue_initialise(&info->queues.disconnected)) {
2855		queue_free(&info->queues.issue);
2856		return -ENOMEM;
2857	}
2858
2859	return 0;
2860}
2861
2862/**
2863 * fas216_add - initialise FAS/NCR/AMD SCSI ic.
2864 * @host: a driver-specific filled-out structure
2865 * @dev: parent device
2866 *
2867 * Initialise FAS/NCR/AMD SCSI ic.
2868 * Returns: 0 on success
2869 */
2870int fas216_add(struct Scsi_Host *host, struct device *dev)
2871{
2872	FAS216_Info *info = (FAS216_Info *)host->hostdata;
2873	int type, ret;
2874
2875	if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
2876		printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
2877			info->ifcfg.clockrate);
2878		return -EINVAL;
2879	}
2880
2881	fas216_reset_state(info);
2882	type = fas216_detect_type(info);
2883	info->scsi.type = chip_types[type];
2884
2885	udelay(300);
2886
2887	/*
2888	 * Initialise the chip correctly.
2889	 */
2890	fas216_init_chip(info);
2891
2892	/*
2893	 * Reset the SCSI bus.  We don't want to see
2894	 * the resulting reset interrupt, so mask it
2895	 * out.
2896	 */
2897	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
2898	fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
2899
2900	/*
2901	 * scsi standard says wait 250ms
2902	 */
2903	spin_unlock_irq(info->host->host_lock);
2904	msleep(100*1000/100);
2905	spin_lock_irq(info->host->host_lock);
2906
2907	fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2908	fas216_readb(info, REG_INST);
2909
2910	fas216_checkmagic(info);
2911
2912	ret = scsi_add_host(host, dev);
2913	if (ret)
2914		fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2915	else
2916		scsi_scan_host(host);
2917
2918	return ret;
2919}
2920
2921void fas216_remove(struct Scsi_Host *host)
2922{
2923	FAS216_Info *info = (FAS216_Info *)host->hostdata;
2924
2925	fas216_checkmagic(info);
2926	scsi_remove_host(host);
2927
2928	fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2929	scsi_host_put(host);
2930}
2931
2932/**
2933 * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
2934 * @host: a driver-specific filled-out structure
2935 *
2936 * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
2937 */
2938void fas216_release(struct Scsi_Host *host)
2939{
2940	FAS216_Info *info = (FAS216_Info *)host->hostdata;
2941
2942	queue_free(&info->queues.disconnected);
2943	queue_free(&info->queues.issue);
2944}
2945
2946int fas216_print_host(FAS216_Info *info, char *buffer)
2947{
2948	return sprintf(buffer,
2949			"\n"
2950			"Chip    : %s\n"
2951			" Address: 0x%p\n"
2952			" IRQ    : %d\n"
2953			" DMA    : %d\n",
2954			info->scsi.type, info->scsi.io_base,
2955			info->scsi.irq, info->scsi.dma);
2956}
2957
2958int fas216_print_stats(FAS216_Info *info, char *buffer)
2959{
2960	char *p = buffer;
2961
2962	p += sprintf(p, "\n"
2963			"Command Statistics:\n"
2964			" Queued     : %u\n"
2965			" Issued     : %u\n"
2966			" Completed  : %u\n"
2967			" Reads      : %u\n"
2968			" Writes     : %u\n"
2969			" Others     : %u\n"
2970			" Disconnects: %u\n"
2971			" Aborts     : %u\n"
2972			" Bus resets : %u\n"
2973			" Host resets: %u\n",
2974			info->stats.queues,	 info->stats.removes,
2975			info->stats.fins,	 info->stats.reads,
2976			info->stats.writes,	 info->stats.miscs,
2977			info->stats.disconnects, info->stats.aborts,
2978			info->stats.bus_resets,	 info->stats.host_resets);
2979
2980	return p - buffer;
2981}
2982
2983int fas216_print_devices(FAS216_Info *info, char *buffer)
2984{
2985	struct fas216_device *dev;
2986	struct scsi_device *scd;
2987	char *p = buffer;
2988
2989	p += sprintf(p, "Device/Lun TaggedQ       Parity   Sync\n");
2990
2991	shost_for_each_device(scd, info->host) {
2992		dev = &info->device[scd->id];
2993		p += sprintf(p, "     %d/%d   ", scd->id, scd->lun);
2994		if (scd->tagged_supported)
2995			p += sprintf(p, "%3sabled(%3d) ",
2996				     scd->simple_tags ? "en" : "dis",
2997				     scd->current_tag);
2998		else
2999			p += sprintf(p, "unsupported   ");
3000
3001		p += sprintf(p, "%3sabled ", dev->parity_enabled ? "en" : "dis");
3002
3003		if (dev->sof)
3004			p += sprintf(p, "offset %d, %d ns\n",
3005				     dev->sof, dev->period * 4);
3006		else
3007			p += sprintf(p, "async\n");
3008	}
3009
3010	return p - buffer;
3011}
3012
3013EXPORT_SYMBOL(fas216_init);
3014EXPORT_SYMBOL(fas216_add);
3015EXPORT_SYMBOL(fas216_queue_command);
3016EXPORT_SYMBOL(fas216_noqueue_command);
3017EXPORT_SYMBOL(fas216_intr);
3018EXPORT_SYMBOL(fas216_remove);
3019EXPORT_SYMBOL(fas216_release);
3020EXPORT_SYMBOL(fas216_eh_abort);
3021EXPORT_SYMBOL(fas216_eh_device_reset);
3022EXPORT_SYMBOL(fas216_eh_bus_reset);
3023EXPORT_SYMBOL(fas216_eh_host_reset);
3024EXPORT_SYMBOL(fas216_print_host);
3025EXPORT_SYMBOL(fas216_print_stats);
3026EXPORT_SYMBOL(fas216_print_devices);
3027
3028MODULE_AUTHOR("Russell King");
3029MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
3030MODULE_LICENSE("GPL");
3031