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