File: /usr/src/linux/drivers/scsi/aha152x.c

1     /* aha152x.c -- Adaptec AHA-152x driver
2      * Author: Jürgen E. Fischer, fischer@norbit.de
3      * Copyright 1993-2000 Jürgen E. Fischer
4      *
5      * This program is free software; you can redistribute it and/or modify it
6      * under the terms of the GNU General Public License as published by the
7      * Free Software Foundation; either version 2, or (at your option) any
8      * later version.
9      *
10      * This program is distributed in the hope that it will be useful, but
11      * WITHOUT ANY WARRANTY; without even the implied warranty of
12      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      * General Public License for more details.
14      *
15      *
16      * $Id: aha152x.c,v 2.4 2000/12/16 12:53:56 fischer Exp $
17      *
18      * $Log: aha152x.c,v $
19      * Revision 2.4  2000/12/16 12:53:56  fischer
20      * - allow REQUEST SENSE to be queued
21      * - handle shared PCI interrupts
22      *
23      * Revision 2.3  2000/11/04 16:40:26  fischer
24      * - handle data overruns
25      * - extend timeout for data phases
26      *
27      * Revision 2.2  2000/08/08 19:54:53  fischer
28      * - minor changes
29      *
30      * Revision 2.1  2000/05/17 16:23:17  fischer
31      * - signature update
32      * - fix for data out w/o scatter gather
33      *
34      * Revision 2.0  1999/12/25 15:07:32  fischer
35      * - interrupt routine completly reworked
36      * - basic support for new eh code
37      *
38      * Revision 1.21  1999/11/10 23:46:36  fischer
39      * - default to synchronous operation
40      * - synchronous negotiation fixed
41      * - added timeout to loops
42      * - debugging output can be controlled through procfs
43      *
44      * Revision 1.20  1999/11/07 18:37:31  fischer
45      * - synchronous operation works
46      * - resid support for sg driver
47      *
48      * Revision 1.19  1999/11/02 22:39:59  fischer
49      * - moved leading comments to README.aha152x
50      * - new additional module parameters
51      * - updates for 2.3
52      * - support for the Tripace TC1550 controller
53      * - interrupt handling changed
54      *
55      * Revision 1.18  1996/09/07 20:10:40  fischer
56      * - fixed can_queue handling (multiple outstanding commands working again)
57      *
58      * Revision 1.17  1996/08/17 16:05:14  fischer
59      * - biosparam improved
60      * - interrupt verification
61      * - updated documentation
62      * - cleanups
63      *
64      * Revision 1.16  1996/06/09 00:04:56  root
65      * - added configuration symbols for insmod (aha152x/aha152x1)
66      *
67      * Revision 1.15  1996/04/30 14:52:06  fischer
68      * - proc info fixed
69      * - support for extended translation for >1GB disks
70      *
71      * Revision 1.14  1996/01/17  15:11:20  fischer
72      * - fixed lockup in MESSAGE IN phase after reconnection
73      *
74      * Revision 1.13  1996/01/09  02:15:53  fischer
75      * - some cleanups
76      * - moved request_irq behind controller initialization
77      *   (to avoid spurious interrupts)
78      *
79      * Revision 1.12  1995/12/16  12:26:07  fischer
80      * - barrier()s added
81      * - configurable RESET delay added
82      *
83      * Revision 1.11  1995/12/06  21:18:35  fischer
84      * - some minor updates
85      *
86      * Revision 1.10  1995/07/22  19:18:45  fischer
87      * - support for 2 controllers
88      * - started synchronous data transfers (not working yet)
89      *
90      * Revision 1.9  1995/03/18  09:20:24  root
91      * - patches for PCMCIA and modules
92      *
93      * Revision 1.8  1995/01/21  22:07:19  root
94      * - snarf_region => request_region
95      * - aha152x_intr interface change
96      *
97      * Revision 1.7  1995/01/02  23:19:36  root
98      * - updated COMMAND_SIZE to cmd_len
99      * - changed sti() to restore_flags()
100      * - fixed some #ifdef which generated warnings
101      *
102      * Revision 1.6  1994/11/24  20:35:27  root
103      * - problem with odd number of bytes in fifo fixed
104      *
105      * Revision 1.5  1994/10/30  14:39:56  root
106      * - abort code fixed
107      * - debugging improved
108      *
109      * Revision 1.4  1994/09/12  11:33:01  root
110      * - irqaction to request_irq
111      * - abortion updated
112      *
113      * Revision 1.3  1994/08/04  13:53:05  root
114      * - updates for mid-level-driver changes
115      * - accept unexpected BUSFREE phase as error condition
116      * - parity check now configurable
117      *
118      * Revision 1.2  1994/07/03  12:56:36  root
119      * - cleaned up debugging code
120      * - more tweaking on reset delays
121      * - updated abort/reset code (pretty untested...)
122      *
123      * Revision 1.1  1994/05/28  21:18:49  root
124      * - update for mid-level interface change (abort-reset)
125      * - delays after resets adjusted for some slow devices
126      *
127      * Revision 1.0  1994/03/25  12:52:00  root
128      * - Fixed "more data than expected" problem
129      * - added new BIOS signatures
130      *
131      * Revision 0.102  1994/01/31  20:44:12  root
132      * - minor changes in insw/outsw handling
133      *
134      * Revision 0.101  1993/12/13  01:16:27  root
135      * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
136      *   fixes problems with CD-ROM sector size detection & media change)
137      *
138      * Revision 0.100  1993/12/10  16:58:47  root
139      * - fix for unsuccessful selections in case of non-continuous id assignments
140      *   on the scsi bus.
141      *
142      * Revision 0.99  1993/10/24  16:19:59  root
143      * - fixed DATA IN (rare read errors gone)
144      *
145      * Revision 0.98  1993/10/17  12:54:44  root
146      * - fixed some recent fixes (shame on me)
147      * - moved initialization of scratch area to aha152x_queue
148      *
149      * Revision 0.97  1993/10/09  18:53:53  root
150      * - DATA IN fixed. Rarely left data in the fifo.
151      *
152      * Revision 0.96  1993/10/03  00:53:59  root
153      * - minor changes on DATA IN
154      *
155      * Revision 0.95  1993/09/24  10:36:01  root
156      * - change handling of MSGI after reselection
157      * - fixed sti/cli
158      * - minor changes
159      *
160      * Revision 0.94  1993/09/18  14:08:22  root
161      * - fixed bug in multiple outstanding command code
162      * - changed detection
163      * - support for kernel command line configuration
164      * - reset corrected
165      * - changed message handling
166      *
167      * Revision 0.93  1993/09/15  20:41:19  root
168      * - fixed bugs with multiple outstanding commands
169      *
170      * Revision 0.92  1993/09/13  02:46:33  root
171      * - multiple outstanding commands work (no problems with IBM drive)
172      *
173      * Revision 0.91  1993/09/12  20:51:46  root
174      * added multiple outstanding commands
175      * (some problem with this $%&? IBM device remain)
176      *
177      * Revision 0.9  1993/09/12  11:11:22  root
178      * - corrected auto-configuration
179      * - changed the auto-configuration (added some '#define's)
180      * - added support for dis-/reconnection
181      *
182      * Revision 0.8  1993/09/06  23:09:39  root
183      * - added support for the drive activity light
184      * - minor changes
185      *
186      * Revision 0.7  1993/09/05  14:30:15  root
187      * - improved phase detection
188      * - now using the new snarf_region code of 0.99pl13
189      *
190      * Revision 0.6  1993/09/02  11:01:38  root
191      * first public release; added some signatures and biosparam()
192      *
193      * Revision 0.5  1993/08/30  10:23:30  root
194      * fixed timing problems with my IBM drive
195      *
196      * Revision 0.4  1993/08/29  14:06:52  root
197      * fixed some problems with timeouts due incomplete commands
198      *
199      * Revision 0.3  1993/08/28  15:55:03  root
200      * writing data works too.  mounted and worked on a dos partition
201      *
202      * Revision 0.2  1993/08/27  22:42:07  root
203      * reading data works.  Mounted a msdos partition.
204      *
205      * Revision 0.1  1993/08/25  13:38:30  root
206      * first "damn thing doesn't work" version
207      *
208      * Revision 0.0  1993/08/14  19:54:25  root
209      * empty function bodies; detect() works.
210      *
211      *
212      **************************************************************************
213      
214      see README.aha152x for configuration details
215     
216      **************************************************************************/
217     
218     #include <linux/module.h>
219     
220     #if defined(PCMCIA)
221     #undef MODULE
222     #endif
223     
224     #include <linux/sched.h>
225     #include <asm/io.h>
226     #include <linux/blk.h>
227     #include "scsi.h"
228     #include "sd.h"
229     #include "hosts.h"
230     #include "constants.h"
231     #include <asm/system.h>
232     #include <linux/errno.h>
233     #include <linux/string.h>
234     #include <linux/wait.h>
235     #include <linux/ioport.h>
236     #include <linux/delay.h>
237     #include <linux/proc_fs.h>
238     #include <linux/interrupt.h>
239     #include <linux/init.h>
240     #include <linux/kernel.h>
241     #include <linux/isapnp.h>
242     #include <asm/semaphore.h>
243     #include <linux/spinlock.h>
244     
245     #include "aha152x.h"
246     #include <linux/stat.h>
247     
248     #include <scsi/scsicam.h>
249     
250     /* DEFINES */
251     
252     /* For PCMCIA cards, always use AUTOCONF */
253     #if defined(PCMCIA) || defined(MODULE)
254     #if !defined(AUTOCONF)
255     #define AUTOCONF
256     #endif
257     #endif
258     
259     #if !defined(AUTOCONF) && !defined(SETUP0)
260     #error define AUTOCONF or SETUP0
261     #endif
262     
263     #if defined(AHA152X_DEBUG)
264     #define DEBUG_DEFAULT debug_eh
265     
266     #define DPRINTK(when,msgs...) \
267     	do { if(HOSTDATA(shpnt)->debug & (when)) printk(msgs); } while(0)
268     
269     #define DO_LOCK(flags)	\
270     	do { \
271     		if(spin_is_locked(&QLOCK)) { \
272     			DPRINTK(debug_intr, DEBUG_LEAD "(%s:%d) already locked at %s:%d\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__, QLOCKER, QLOCKERL); \
273     		} \
274     		DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) locking\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
275     		spin_lock_irqsave(&QLOCK,flags); \
276     		DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) locked\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
277     		QLOCKER=__FUNCTION__; \
278     		QLOCKERL=__LINE__; \
279     	} while(0)
280     
281     #define DO_UNLOCK(flags)	\
282     	do { \
283     		DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) unlocking (locked at %s:%d)\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__, QLOCKER, QLOCKERL); \
284     		spin_unlock_irqrestore(&QLOCK,flags); \
285     		DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) unlocked\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
286     		QLOCKER="(not locked)"; \
287     		QLOCKERL=0; \
288     	} while(0)
289     
290     #else
291     #define DPRINTK(when,msgs...)
292     #define	DO_LOCK(flags)		spin_lock_irqsave(&QLOCK,flags)
293     #define	DO_UNLOCK(flags)	spin_unlock_irqrestore(&QLOCK,flags)
294     #endif
295     
296     #define LEAD		"(scsi%d:%d:%d) "
297     #define WARN_LEAD	KERN_WARNING	LEAD
298     #define INFO_LEAD	KERN_INFO	LEAD
299     #define NOTE_LEAD	KERN_NOTICE	LEAD
300     #define ERR_LEAD	KERN_ERR	LEAD
301     #define DEBUG_LEAD	KERN_DEBUG	LEAD
302     #define CMDINFO(cmd) \
303     			(cmd) ? ((cmd)->host->host_no) : -1, \
304                             (cmd) ? ((cmd)->target & 0x0f) : -1, \
305     			(cmd) ? ((cmd)->lun & 0x07) : -1
306     
307     #define DELAY_DEFAULT 1000
308     
309     /* possible irq range */
310     #if defined(PCMCIA)
311     #define IRQ_MIN 0
312     #define IRQ_MAX 16
313     #else
314     #define IRQ_MIN 9
315     #define IRQ_MAX 12
316     #endif
317     #define IRQS    IRQ_MAX-IRQ_MIN+1
318     
319     enum {
320     	not_issued	= 0x0001,	/* command not yet issued */
321     	selecting	= 0x0002, 	/* target is beeing selected */
322     	identified	= 0x0004,	/* IDENTIFY was sent */
323     	disconnected	= 0x0008,	/* target disconnected */
324     	completed	= 0x0010,	/* target sent COMMAND COMPLETE */ 
325     	aborted		= 0x0020,	/* ABORT was sent */
326     	resetted	= 0x0040,	/* BUS DEVICE RESET was sent */
327     	spiordy		= 0x0080,	/* waiting for SPIORDY to raise */
328     	syncneg		= 0x0100,	/* synchronous negotiation in progress */
329     	aborting	= 0x0200,	/* ABORT is pending */
330     	resetting	= 0x0400,	/* BUS DEVICE RESET is pending */
331     };
332     
333     #if defined(MODULE)
334     MODULE_AUTHOR("Jürgen Fischer");
335     MODULE_DESCRIPTION(AHA152X_REVID);
336     MODULE_PARM(io, "1-2i");
337     MODULE_PARM_DESC(io,"base io address of controller");
338     static int io[] = {0, 0};
339     
340     MODULE_PARM(irq, "1-2i");
341     MODULE_PARM_DESC(irq,"interrupt for controller");
342     static int irq[] = {0, 0};
343     
344     MODULE_PARM(scsiid, "1-2i");
345     MODULE_PARM_DESC(scsiid,"scsi id of controller");
346     static int scsiid[] = {7, 7};
347     
348     MODULE_PARM(reconnect, "1-2i");
349     MODULE_PARM_DESC(reconnect,"allow targets to disconnect");
350     static int reconnect[] = {1, 1};
351     
352     MODULE_PARM(parity, "1-2i");
353     MODULE_PARM_DESC(parity,"use scsi parity");
354     static int parity[] = {1, 1};
355     
356     MODULE_PARM(sync, "1-2i");
357     MODULE_PARM_DESC(sync,"use synchronous transfers");
358     static int sync[] = {1, 1};
359     
360     MODULE_PARM(delay, "1-2i");
361     MODULE_PARM_DESC(delay,"scsi reset delay");
362     static int delay[] = {DELAY_DEFAULT, DELAY_DEFAULT};
363     
364     MODULE_PARM(exttrans, "1-2i");
365     MODULE_PARM_DESC(exttrans,"use extended translation");
366     static int exttrans[] = {0, 0};
367     
368     #if !defined(AHA152X_DEBUG)
369     MODULE_PARM(aha152x, "1-8i");
370     MODULE_PARM_DESC(aha152x, "parameters for first controller");
371     static int aha152x[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
372     
373     MODULE_PARM(aha152x1, "1-8i");
374     MODULE_PARM_DESC(aha152x1, "parameters for second controller");
375     static int aha152x1[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
376     #else
377     MODULE_PARM(debug, "1-2i");
378     MODULE_PARM_DESC(debug, "flags for driver debugging");
379     static int debug[] = {DEBUG_DEFAULT, DEBUG_DEFAULT};
380     
381     MODULE_PARM(aha152x, "1-9i");
382     MODULE_PARM_DESC(aha152x, "parameters for first controller");
383     static int aha152x[]   = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
384     
385     MODULE_PARM(aha152x1, "1-9i");
386     MODULE_PARM_DESC(aha152x1, "parameters for second controller");
387     static int aha152x1[]  = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
388     #endif /* !defined(AHA152X_DEBUG) */
389     
390     #ifdef __ISAPNP__
391     
392     static struct isapnp_device_id id_table[] __devinitdata = {
393     	{ ISAPNP_DEVICE_SINGLE('A','D','P',0x1505, 'A','D','P',0x1505), },
394     	{ ISAPNP_DEVICE_SINGLE_END, }
395     };
396     MODULE_DEVICE_TABLE(isapnp, id_table);
397     
398     #endif /* ISAPNP */
399     #endif /* MODULE */
400     
401     /* set by aha152x_setup according to the command line */
402     static int setup_count = 0;
403     static int registered_count = 0;
404     static struct aha152x_setup {
405     	int io_port;
406     	int irq;
407     	int scsiid;
408     	int reconnect;
409     	int parity;
410     	int synchronous;
411     	int delay;
412     	int ext_trans;
413     	int tc1550;
414     #if defined(AHA152X_DEBUG)
415     	int debug;
416     #endif
417     	char *conf;
418     } setup[2];
419     
420     static struct Scsi_Host *aha152x_host[IRQS];
421     
422     /*
423      * internal states of the host
424      *
425      */ 
426     enum aha152x_state {
427     	idle=0,
428     	unknown,
429     	seldo,
430     	seldi,
431     	selto,
432     	busfree,
433     	msgo,
434     	cmd,
435     	msgi,
436     	status,
437     	datai,
438     	datao,
439     	parerr,
440     	rsti,
441     	maxstate
442     };
443     
444     /*
445      * current state information of the host
446      *
447      */
448     struct aha152x_hostdata {
449     	Scsi_Cmnd *issue_SC;
450     		/* pending commands to issue */
451     
452     	Scsi_Cmnd *current_SC;
453     		/* current command on the bus */
454     
455     	Scsi_Cmnd *disconnected_SC;
456     		/* commands that disconnected */
457     
458     	Scsi_Cmnd *done_SC;
459     		/* command that was completed */
460     
461     	spinlock_t lock;
462     		/* host lock */
463     
464     #if defined(AHA152X_DEBUG)
465     	char *locker;	/* which function has the lock */
466     	int lockerl;	/* where did it get it */
467     
468     	int debug;	/* current debugging setting */
469     #endif
470     
471     #if defined(AHA152X_STAT)
472     	int           total_commands;
473     	int	      disconnections;
474     	int	      busfree_without_any_action;
475     	int	      busfree_without_old_command;
476     	int	      busfree_without_new_command;
477     	int	      busfree_without_done_command;
478     	int	      busfree_with_check_condition;
479     	int           count[maxstate];
480     	int           count_trans[maxstate];
481     	unsigned long time[maxstate];
482     #endif
483     
484     	int commands;		/* current number of commands */
485     
486     	int reconnect;		/* disconnection allowed */
487     	int parity;		/* parity checking enabled */
488     	int synchronous;	/* synchronous transferes enabled */
489     	int delay;		/* reset out delay */
490     	int ext_trans;		/* extended translation enabled */
491     
492     	int swint; 		/* software-interrupt was fired during detect() */
493     	int service;		/* bh needs to be run */
494     	int in_intr;		/* bh is running */
495     
496     	/* current state,
497     	   previous state,
498     	   last state different from current state */
499     	enum aha152x_state state, prevstate, laststate;
500     
501     	int target;
502     		/* reconnecting target */
503     
504     	unsigned char syncrate[8];
505     		/* current synchronous transfer agreements */
506     
507     	unsigned char syncneg[8];
508     		/* 0: no negotiation;
509     		 * 1: negotiation in progress;
510     		 * 2: negotiation completed
511     		 */
512     
513     	int cmd_i;
514     		/* number of sent bytes of current command */
515     
516     	int msgi_len;
517     		/* number of received message bytes */
518     	unsigned char msgi[256];
519     		/* received message bytes */
520     
521     	int msgo_i, msgo_len;	
522     		/* number of sent bytes and length of current messages */
523     	unsigned char msgo[256];
524     		/* pending messages */
525     
526     	int data_len;
527     		/* number of sent/received bytes in dataphase */
528     
529     	unsigned long io_port0;
530     	unsigned long io_port1;
531     };
532     
533     
534     /*
535      * host specific command extension
536      *
537      */
538     struct aha152x_scdata {
539     	Scsi_Cmnd *next;	/* next sc in queue */
540     	Scsi_Cmnd *done;	/* done command */
541     	struct semaphore *sem;	/* semaphore to block on */
542     };
543     
544     
545     /* access macros for hostdata */
546     
547     #define HOSTDATA(shpnt)		((struct aha152x_hostdata *) &shpnt->hostdata)
548     
549     #define HOSTNO			((shpnt)->host_no)
550     
551     #define CURRENT_SC		(HOSTDATA(shpnt)->current_SC)
552     #define DONE_SC			(HOSTDATA(shpnt)->done_SC)
553     #define ISSUE_SC		(HOSTDATA(shpnt)->issue_SC)
554     #define DISCONNECTED_SC		(HOSTDATA(shpnt)->disconnected_SC)
555     #define QLOCK			(HOSTDATA(shpnt)->lock)
556     #define QLOCKER			(HOSTDATA(shpnt)->locker)
557     #define QLOCKERL		(HOSTDATA(shpnt)->lockerl)
558     
559     #define STATE			(HOSTDATA(shpnt)->state)
560     #define PREVSTATE		(HOSTDATA(shpnt)->prevstate)
561     #define LASTSTATE		(HOSTDATA(shpnt)->laststate)
562     
563     #define RECONN_TARGET		(HOSTDATA(shpnt)->target)
564     
565     #define CMD_I			(HOSTDATA(shpnt)->cmd_i)
566     
567     #define MSGO(i)			(HOSTDATA(shpnt)->msgo[i])
568     #define MSGO_I			(HOSTDATA(shpnt)->msgo_i)
569     #define MSGOLEN			(HOSTDATA(shpnt)->msgo_len)
570     #define ADDMSGO(x)		(MSGOLEN<256 ? MSGO(MSGOLEN++)=x : aha152x_error(shpnt,"MSGO overflow"))
571     
572     #define MSGI(i)			(HOSTDATA(shpnt)->msgi[i])
573     #define MSGILEN			(HOSTDATA(shpnt)->msgi_len)
574     #define ADDMSGI(x)		(MSGILEN<256 ? MSGI(MSGILEN++)=x : aha152x_error(shpnt,"MSGI overflow"))
575     
576     #define DATA_LEN		(HOSTDATA(shpnt)->data_len)
577     
578     #define SYNCRATE		(HOSTDATA(shpnt)->syncrate[CURRENT_SC->target])
579     #define SYNCNEG			(HOSTDATA(shpnt)->syncneg[CURRENT_SC->target])
580     
581     #define DELAY			(HOSTDATA(shpnt)->delay)
582     #define EXT_TRANS		(HOSTDATA(shpnt)->ext_trans)
583     #define TC1550			(HOSTDATA(shpnt)->tc1550)
584     #define RECONNECT		(HOSTDATA(shpnt)->reconnect)
585     #define PARITY			(HOSTDATA(shpnt)->parity)
586     #define SYNCHRONOUS		(HOSTDATA(shpnt)->synchronous)
587     
588     #define HOSTIOPORT0		(HOSTDATA(shpnt)->io_port0)
589     #define HOSTIOPORT1		(HOSTDATA(shpnt)->io_port1)
590     
591     #define SCDATA(SCpnt)		((struct aha152x_scdata *) (SCpnt)->host_scribble)
592     #define SCNEXT(SCpnt)		SCDATA(SCpnt)->next
593     #define SCDONE(SCpnt)		SCDATA(SCpnt)->done
594     #define SCSEM(SCpnt)		SCDATA(SCpnt)->sem
595     
596     
597     /* state handling */
598     static void seldi_run(struct Scsi_Host *shpnt);
599     static void seldo_run(struct Scsi_Host *shpnt);
600     static void selto_run(struct Scsi_Host *shpnt);
601     static void busfree_run(struct Scsi_Host *shpnt);
602     
603     static void msgo_init(struct Scsi_Host *shpnt);
604     static void msgo_run(struct Scsi_Host *shpnt);
605     static void msgo_end(struct Scsi_Host *shpnt);
606     
607     static void cmd_init(struct Scsi_Host *shpnt);
608     static void cmd_run(struct Scsi_Host *shpnt);
609     static void cmd_end(struct Scsi_Host *shpnt);
610     
611     static void datai_init(struct Scsi_Host *shpnt);
612     static void datai_run(struct Scsi_Host *shpnt);
613     static void datai_end(struct Scsi_Host *shpnt);
614     
615     static void datao_init(struct Scsi_Host *shpnt);
616     static void datao_run(struct Scsi_Host *shpnt);
617     static void datao_end(struct Scsi_Host *shpnt);
618     
619     static void status_run(struct Scsi_Host *shpnt);
620     
621     static void msgi_run(struct Scsi_Host *shpnt);
622     static void msgi_end(struct Scsi_Host *shpnt);
623     
624     static void parerr_run(struct Scsi_Host *shpnt);
625     static void rsti_run(struct Scsi_Host *shpnt);
626     
627     static void is_complete(struct Scsi_Host *shpnt);
628     
629     /*
630      * driver states
631      *
632      */
633     static struct {
634     	char		*name;
635     	void		(*init)(struct Scsi_Host *);
636     	void		(*run)(struct Scsi_Host *);
637     	void		(*end)(struct Scsi_Host *);
638     	int		spio;
639     } states[] = {
640     	{ "idle",	0,		0,		0,		0},
641     	{ "unknown",	0,		0,		0,		0},
642     	{ "seldo",	0,		seldo_run,	0,		0},
643     	{ "seldi",	0,		seldi_run,	0,		0},
644     	{ "selto",	0,		selto_run,	0,		0},
645     	{ "busfree",	0,		busfree_run,	0,		0},
646     	{ "msgo",	msgo_init,	msgo_run,	msgo_end,	1},
647     	{ "cmd",	cmd_init,	cmd_run,	cmd_end,	1},
648     	{ "msgi",	0,		msgi_run,	msgi_end,	1},
649     	{ "status",	0,		status_run,	0,		1},
650     	{ "datai",	datai_init,	datai_run,	datai_end,	0},
651     	{ "datao",	datao_init,	datao_run,	datao_end,	0},
652     	{ "parerr",	0,		parerr_run,	0,		0},
653     	{ "rsti",	0,		rsti_run,	0,		0},
654     };
655     
656     /* setup & interrupt */
657     static void intr(int irq, void *dev_id, struct pt_regs *);
658     static void reset_ports(struct Scsi_Host *shpnt);
659     static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
660     static void done(struct Scsi_Host *shpnt, int error);
661     static int checksetup(struct aha152x_setup *setup);
662     
663     /* diagnostics */
664     static void disp_ports(struct Scsi_Host *shpnt);
665     static void show_command(Scsi_Cmnd * ptr);
666     static void show_queues(struct Scsi_Host *shpnt);
667     static void disp_enintr(struct Scsi_Host *shpnt);
668     
669     /* possible i/o addresses for the AIC-6260; default first */
670     static unsigned short ports[] = { 0x340, 0x140 };
671     #define PORT_COUNT (sizeof(ports) / sizeof(unsigned short))
672     
673     #if !defined(SKIP_BIOSTEST)
674     /* possible locations for the Adaptec BIOS; defaults first */
675     static unsigned int addresses[] =
676     {
677     	0xdc000,		/* default first */
678     	0xc8000,
679     	0xcc000,
680     	0xd0000,
681     	0xd4000,
682     	0xd8000,
683     	0xe0000,
684     	0xeb800,		/* VTech Platinum SMP */
685     	0xf0000,
686     };
687     #define ADDRESS_COUNT (sizeof(addresses) / sizeof(unsigned int))
688     
689     /* signatures for various AIC-6[23]60 based controllers.
690        The point in detecting signatures is to avoid useless and maybe
691        harmful probes on ports. I'm not sure that all listed boards pass
692        auto-configuration. For those which fail the BIOS signature is
693        obsolete, because user intervention to supply the configuration is
694        needed anyway.  May be an information whether or not the BIOS supports
695        extended translation could be also useful here. */
696     static struct signature {
697     	unsigned char *signature;
698     	int sig_offset;
699     	int sig_length;
700     } signatures[] =
701     {
702     	{ "Adaptec AHA-1520 BIOS",	0x102e, 21 },
703     		/* Adaptec 152x */
704     	{ "Adaptec AHA-1520B",		0x000b, 17 },
705     		/* Adaptec 152x rev B */
706     	{ "Adaptec AHA-1520B",		0x0026, 17 },
707     		/* Iomega Jaz Jet ISA (AIC6370Q) */
708     	{ "Adaptec ASW-B626 BIOS",	0x1029, 21 },
709     		/* on-board controller */
710     	{ "Adaptec BIOS: ASW-B626",	0x000f, 22 },
711     		/* on-board controller */
712     	{ "Adaptec ASW-B626 S2",	0x2e6c, 19 },
713     		/* on-board controller */
714     	{ "Adaptec BIOS:AIC-6360",	0x000c, 21 },
715     		/* on-board controller */
716     	{ "ScsiPro SP-360 BIOS",	0x2873, 19 },
717     		/* ScsiPro-Controller  */
718     	{ "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },
719     		/* Gigabyte Local-Bus-SCSI */
720     	{ "Adaptec BIOS:AVA-282X",	0x000c, 21 },
721     		/* Adaptec 282x */
722     	{ "Adaptec IBM Dock II SCSI",	0x2edd, 24 },
723     		/* IBM Thinkpad Dock II */
724     	{ "Adaptec BIOS:AHA-1532P",	0x001c, 22 },
725     		/* IBM Thinkpad Dock II SCSI */
726     	{ "DTC3520A Host Adapter BIOS", 0x318a, 26 },
727     		/* DTC 3520A ISA SCSI */
728     };
729     
730     #define SIGNATURE_COUNT (sizeof(signatures) / sizeof(struct signature))
731     #endif
732     
733     
734     /*
735      *  queue services:
736      *
737      */
738     static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
739     {
740     	Scsi_Cmnd *end;
741     
742     	SCNEXT(new_SC) = NULL;
743     	if (!*SC)
744     		*SC = new_SC;
745     	else {
746     		for (end = *SC; SCNEXT(end); end = SCNEXT(end))
747     			;
748     		SCNEXT(end) = new_SC;
749     	}
750     }
751     
752     static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd ** SC)
753     {
754     	Scsi_Cmnd *ptr;
755     
756     	ptr = *SC;
757     	if (ptr) {
758     		*SC = SCNEXT(*SC);
759     		SCNEXT(ptr)=NULL;
760     	}
761     	return ptr;
762     }
763     
764     static inline Scsi_Cmnd *remove_lun_SC(Scsi_Cmnd ** SC, int target, int lun)
765     {
766     	Scsi_Cmnd *ptr, *prev;
767     
768     	for (ptr = *SC, prev = NULL;
769     	     ptr && ((ptr->target != target) || (ptr->lun != lun));
770     	     prev = ptr, ptr = SCNEXT(ptr))
771     	     ;
772     
773     	if (ptr) {
774     		if (prev)
775     			SCNEXT(prev) = SCNEXT(ptr);
776     		else
777     			*SC = SCNEXT(ptr);
778     
779     		SCNEXT(ptr)=NULL;
780     	}
781     
782     	return ptr;
783     }
784     
785     static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, Scsi_Cmnd *SCp)
786     {
787     	Scsi_Cmnd *ptr, *prev;
788     
789     	for (ptr = *SC, prev = NULL;
790     	     ptr && SCp!=ptr;
791     	     prev = ptr, ptr = SCNEXT(ptr))
792     	     ;
793     
794     	if (ptr) {
795     		if (prev)
796     			SCNEXT(prev) = SCNEXT(ptr);
797     		else
798     			*SC = SCNEXT(ptr);
799     
800     		SCNEXT(ptr)=NULL;
801     	}
802     
803     	return ptr;
804     }
805     
806     #if defined(PCMCIA) || !defined(MODULE)
807     void aha152x_setup(char *str, int *ints)
808     {
809     	if(setup_count>2) {
810     		printk(KERN_ERR "aha152x: you can only configure up to two controllers\n");
811     		return;
812     	}
813     
814     	setup[setup_count].conf        = str;
815     	setup[setup_count].io_port     = ints[0] >= 1 ? ints[1] : 0x340;
816     	setup[setup_count].irq         = ints[0] >= 2 ? ints[2] : 11;
817     	setup[setup_count].scsiid      = ints[0] >= 3 ? ints[3] : 7;
818     	setup[setup_count].reconnect   = ints[0] >= 4 ? ints[4] : 1;
819     	setup[setup_count].parity      = ints[0] >= 5 ? ints[5] : 1;
820     	setup[setup_count].synchronous = ints[0] >= 6 ? ints[6] : 1;
821     	setup[setup_count].delay       = ints[0] >= 7 ? ints[7] : DELAY_DEFAULT;
822     	setup[setup_count].ext_trans   = ints[0] >= 8 ? ints[8] : 0;
823     #if defined(AHA152X_DEBUG)
824     	setup[setup_count].debug       = ints[0] >= 9 ? ints[9] : DEBUG_DEFAULT;
825     	if (ints[0] > 9) {
826     		printk(KERN_NOTICE "aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
827     		       "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>[,<DEBUG>]]]]]]]]\n");
828     #else
829     	if (ints[0] > 8) {                                                /*}*/
830     		printk(KERN_NOTICE "aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
831     		       "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>]]]]]]]\n");
832     #endif
833     		return;
834     	} else {
835     		setup_count++;
836     		return;
837     	}
838     }
839     #endif
840     
841     #if !defined(MODULE)
842     static int __init do_setup(char *str)
843     {
844     
845     #if defined(AHA152X_DEBUG)
846     	int ints[11];
847     #else
848     	int ints[10];
849     #endif
850     	int count=setup_count;
851     
852     	get_options(str, sizeof(ints)/sizeof(int), ints);
853     	aha152x_setup(str,ints);
854     
855     	return count<setup_count;
856     }
857     
858     __setup("aha152x=", do_setup);
859     #endif
860     
861     /*
862      * Test, if port_base is valid.
863      *
864      */
865     static int aha152x_porttest(int io_port)
866     {
867     	int i;
868     
869     	if (check_region(io_port, IO_RANGE))
870     		return 0;
871     
872     	SETPORT(io_port + O_DMACNTRL1, 0);	/* reset stack pointer */
873     	for (i = 0; i < 16; i++)
874     		SETPORT(io_port + O_STACK, i);
875     
876     	SETPORT(io_port + O_DMACNTRL1, 0);	/* reset stack pointer */
877     	for (i = 0; i < 16 && GETPORT(io_port + O_STACK) == i; i++)
878     		;
879     
880     	return (i == 16);
881     }
882     
883     static int tc1550_porttest(int io_port)
884     {
885     	int i;
886     
887     	if (check_region(io_port, IO_RANGE))
888     		return 0;
889     
890     	SETPORT(io_port + O_TC_DMACNTRL1, 0);	/* reset stack pointer */
891     	for (i = 0; i < 16; i++)
892     		SETPORT(io_port + O_STACK, i);
893     
894     	SETPORT(io_port + O_TC_DMACNTRL1, 0);	/* reset stack pointer */
895     	for (i = 0; i < 16 && GETPORT(io_port + O_TC_STACK) == i; i++)
896     		;
897     
898     	return (i == 16);
899     }
900     
901     static int checksetup(struct aha152x_setup *setup)
902     {
903     
904     #if !defined(PCMCIA)
905     	int i;
906     	for (i = 0; i < PORT_COUNT && (setup->io_port != ports[i]); i++)
907     		;
908     
909     	if (i == PORT_COUNT)
910     		return 0;
911     #endif
912     
913     	if(aha152x_porttest(setup->io_port)) {
914               setup->tc1550=0;
915             } else if(tc1550_porttest(setup->io_port)) {
916               setup->tc1550=1;
917             } else
918               return 0;
919     
920     	if ((setup->irq < IRQ_MIN) || (setup->irq > IRQ_MAX))
921     		return 0;
922     
923     	if ((setup->scsiid < 0) || (setup->scsiid > 7))
924     		return 0;
925     
926     	if ((setup->reconnect < 0) || (setup->reconnect > 1))
927     		return 0;
928     
929     	if ((setup->parity < 0) || (setup->parity > 1))
930     		return 0;
931     
932     	if ((setup->synchronous < 0) || (setup->synchronous > 1))
933     		return 0;
934     
935     	if ((setup->ext_trans < 0) || (setup->ext_trans > 1))
936     		return 0;
937     
938     
939     	return 1;
940     }
941     
942     static void swintr(int irqno, void *dev_id, struct pt_regs *regs)
943     {
944     	struct Scsi_Host *shpnt = aha152x_host[irqno - IRQ_MIN];
945     
946     	if (!shpnt)
947             	printk(KERN_ERR "aha152x%d: catched software interrupt for unknown controller.\n", HOSTNO);
948     
949     	HOSTDATA(shpnt)->swint++;
950     
951     	SETPORT(DMACNTRL0, INTEN);
952     }
953     
954     #ifdef __ISAPNP__
955     static struct pci_dev *pnpdev[2];
956     static int num_pnpdevs;
957     #endif
958     int aha152x_detect(Scsi_Host_Template * tpnt)
959     {
960     	int i, j, ok;
961     #if defined(AUTOCONF)
962     	aha152x_config conf;
963     #ifdef __ISAPNP__
964     	struct pci_dev *dev = NULL;
965     #endif
966     #endif
967     	tpnt->proc_name = "aha152x"; 
968     
969     	for (i = 0; i < IRQS; i++)
970     		aha152x_host[i] = (struct Scsi_Host *) NULL;
971     
972     	if (setup_count) {
973     		printk(KERN_INFO "aha152x: processing commandline: ");
974     
975     		for (i = 0; i < setup_count; i++)
976     			if (!checksetup(&setup[i])) {
977     				printk(KERN_ERR "\naha152x: %s\n", setup[i].conf);
978     				printk(KERN_ERR "aha152x: invalid line\n");
979     			}
980     		printk("ok\n");
981     	}
982     #ifdef __ISAPNP__
983     	while (setup_count <= 2 &&
984     	       (dev = isapnp_find_dev (NULL, ISAPNP_VENDOR('A','D','P'),
985     				       ISAPNP_FUNCTION(0x1505), dev))) {
986     		if (dev->prepare(dev) < 0)
987     			continue;
988     		if (dev->active)
989     			continue;
990     		if (!(dev->resource[0].flags & IORESOURCE_IO))
991     			continue;
992     		dev->resource[0].flags |= IORESOURCE_AUTO;
993     		if (dev->activate(dev) < 0)
994     			continue;
995     		setup[setup_count].io_port = dev->resource[0].start;
996     		setup[setup_count].irq = dev->irq_resource[0].start;
997     		pnpdev[num_pnpdevs++] = dev;
998     		printk (KERN_INFO
999     			"aha152x: found ISAPnP AVA-1505A at address 0x%03X, IRQ %d\n",
1000     			setup[setup_count].io_port, setup[setup_count].irq);
1001     		setup_count++;
1002     	}
1003     #endif
1004     
1005     #if defined(SETUP0)
1006     	if (setup_count < 2) {
1007     		struct aha152x_setup override = SETUP0;
1008     
1009     		if (setup_count == 0 || (override.io_port != setup[0].io_port)) {
1010     			if (!checksetup(&override)) {
1011     				printk(KERN_ERR "\naha152x: invalid override SETUP0={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
1012     				       override.io_port,
1013     				       override.irq,
1014     				       override.scsiid,
1015     				       override.reconnect,
1016     				       override.parity,
1017     				       override.synchronous,
1018     				       override.delay,
1019     				       override.ext_trans);
1020     			} else
1021     				setup[setup_count++] = override;
1022     		}
1023     	}
1024     #endif
1025     
1026     #if defined(SETUP1)
1027     	if (setup_count < 2) {
1028     		struct aha152x_setup override = SETUP1;
1029     
1030     		if (setup_count == 0 || (override.io_port != setup[0].io_port)) {
1031     			if (!checksetup(&override)) {
1032     				printk(KERN_ERR "\naha152x: invalid override SETUP1={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
1033     				       override.io_port,
1034     				       override.irq,
1035     				       override.scsiid,
1036     				       override.reconnect,
1037     				       override.parity,
1038     				       override.synchronous,
1039     				       override.delay,
1040     				       override.ext_trans);
1041     			} else
1042     				setup[setup_count++] = override;
1043     		}
1044     	}
1045     #endif
1046     
1047     #if defined(MODULE)
1048     	if (setup_count<2 && (aha152x[0]!=0 || io[0]!=0 || irq[0]!=0)) {
1049     		if(aha152x[0]!=0) {
1050     			setup[setup_count].conf        = "";
1051     			setup[setup_count].io_port     = aha152x[0];
1052     			setup[setup_count].irq         = aha152x[1];
1053     			setup[setup_count].scsiid      = aha152x[2];
1054     			setup[setup_count].reconnect   = aha152x[3];
1055     			setup[setup_count].parity      = aha152x[4];
1056     			setup[setup_count].synchronous = aha152x[5];
1057     			setup[setup_count].delay       = aha152x[6];
1058     			setup[setup_count].ext_trans   = aha152x[7];
1059     #if defined(AHA152X_DEBUG)
1060     			setup[setup_count].debug       = aha152x[8];
1061     #endif
1062     	  	} else if(io[0]!=0 || irq[0]!=0) {
1063     			if(io[0]!=0)  setup[setup_count].io_port = io[0];
1064     			if(irq[0]!=0) setup[setup_count].irq     = irq[0];
1065     
1066     	    		setup[setup_count].scsiid      = scsiid[0];
1067     	    		setup[setup_count].reconnect   = reconnect[0];
1068     	    		setup[setup_count].parity      = parity[0];
1069     	    		setup[setup_count].synchronous = sync[0];
1070     	    		setup[setup_count].delay       = delay[0];
1071     	    		setup[setup_count].ext_trans   = exttrans[0];
1072     #if defined(AHA152X_DEBUG)
1073     			setup[setup_count].debug       = debug[0];
1074     #endif
1075     		}
1076     
1077               	if (checksetup(&setup[setup_count]))
1078     			setup_count++;
1079     		else
1080     			printk(KERN_ERR "aha152x: invalid module params io=0x%x, irq=%d,scsiid=%d,reconnect=%d,parity=%d,sync=%d,delay=%d,exttrans=%d\n",
1081     			       setup[setup_count].io_port,
1082     			       setup[setup_count].irq,
1083     			       setup[setup_count].scsiid,
1084     			       setup[setup_count].reconnect,
1085     			       setup[setup_count].parity,
1086     			       setup[setup_count].synchronous,
1087     			       setup[setup_count].delay,
1088     			       setup[setup_count].ext_trans);
1089     	}
1090     
1091     	if (setup_count < 2 && (aha152x1[0]!=0 || io[1]!=0 || irq[1]!=0)) {
1092     		if(aha152x1[0]!=0) {
1093     			setup[setup_count].conf        = "";
1094     			setup[setup_count].io_port     = aha152x1[0];
1095     			setup[setup_count].irq         = aha152x1[1];
1096     			setup[setup_count].scsiid      = aha152x1[2];
1097     			setup[setup_count].reconnect   = aha152x1[3];
1098     			setup[setup_count].parity      = aha152x1[4];
1099     			setup[setup_count].synchronous = aha152x1[5];
1100     			setup[setup_count].delay       = aha152x1[6];
1101     			setup[setup_count].ext_trans   = aha152x1[7];
1102     #if defined(AHA152X_DEBUG)
1103     			setup[setup_count].debug       = aha152x1[8];
1104     #endif
1105     	  	} else if(io[1]!=0 || irq[1]!=0) {
1106     			if(io[1]!=0)  setup[setup_count].io_port = io[1];
1107     			if(irq[1]!=0) setup[setup_count].irq     = irq[1];
1108     
1109     	    		setup[setup_count].scsiid      = scsiid[1];
1110     	    		setup[setup_count].reconnect   = reconnect[1];
1111     	    		setup[setup_count].parity      = parity[1];
1112     	    		setup[setup_count].synchronous = sync[1];
1113     	    		setup[setup_count].delay       = delay[1];
1114     	    		setup[setup_count].ext_trans   = exttrans[1];
1115     #if defined(AHA152X_DEBUG)
1116     			setup[setup_count].debug       = debug[1];
1117     #endif
1118     		}
1119     		if (checksetup(&setup[setup_count]))
1120     			setup_count++;
1121     		else
1122     			printk(KERN_ERR "aha152x: invalid module params io=0x%x, irq=%d,scsiid=%d,reconnect=%d,parity=%d,sync=%d,delay=%d,exttrans=%d\n",
1123     			       setup[setup_count].io_port,
1124     			       setup[setup_count].irq,
1125     			       setup[setup_count].scsiid,
1126     			       setup[setup_count].reconnect,
1127     			       setup[setup_count].parity,
1128     			       setup[setup_count].synchronous,
1129     			       setup[setup_count].delay,
1130     			       setup[setup_count].ext_trans);
1131     	}
1132     #endif
1133     
1134     #if defined(AUTOCONF)
1135     	if (setup_count < 2) {
1136     #if !defined(SKIP_BIOSTEST)
1137     		ok = 0;
1138     		for (i = 0; i < ADDRESS_COUNT && !ok; i++)
1139     			for (j = 0; (j < SIGNATURE_COUNT) && !ok; j++)
1140     				ok = isa_check_signature(addresses[i] + signatures[j].sig_offset,
1141     								signatures[j].signature, signatures[j].sig_length);
1142     
1143     		if (!ok && setup_count == 0)
1144     			return 0;
1145     
1146     		printk(KERN_INFO "aha152x: BIOS test: passed, ");
1147     #else
1148     		printk(KERN_INFO "aha152x: ");
1149     #endif				/* !SKIP_BIOSTEST */
1150     
1151     		ok = 0;
1152     		for (i = 0; i < PORT_COUNT && setup_count < 2; i++) {
1153     			if ((setup_count == 1) && (setup[0].io_port == ports[i]))
1154     				continue;
1155     
1156     			if (aha152x_porttest(ports[i])) {
1157     				ok++;
1158     				setup[setup_count].io_port = ports[i];
1159     				setup[setup_count].tc1550  = 0;
1160     
1161     				conf.cf_port =
1162     				    (GETPORT(ports[i] + O_PORTA) << 8) + GETPORT(ports[i] + O_PORTB);
1163     
1164     				setup[setup_count].irq = IRQ_MIN + conf.cf_irq;
1165     				setup[setup_count].scsiid = conf.cf_id;
1166     				setup[setup_count].reconnect = conf.cf_tardisc;
1167     				setup[setup_count].parity = !conf.cf_parity;
1168     				setup[setup_count].synchronous = conf.cf_syncneg;
1169     				setup[setup_count].delay = DELAY_DEFAULT;
1170     				setup[setup_count].ext_trans = 0;
1171     #if defined(AHA152X_DEBUG)
1172     				setup[setup_count].debug = DEBUG_DEFAULT;
1173     #endif
1174     				setup_count++;
1175     			} else if (tc1550_porttest(ports[i])) {
1176     				ok++;
1177     				setup[setup_count].io_port = ports[i];
1178     				setup[setup_count].tc1550  = 1;
1179     
1180     				conf.cf_port =
1181     				    (GETPORT(ports[i] + O_PORTA) << 8) + GETPORT(ports[i] + O_PORTB);
1182     
1183     				setup[setup_count].irq = IRQ_MIN + conf.cf_irq;
1184     				setup[setup_count].scsiid = conf.cf_id;
1185     				setup[setup_count].reconnect = conf.cf_tardisc;
1186     				setup[setup_count].parity = !conf.cf_parity;
1187     				setup[setup_count].synchronous = conf.cf_syncneg;
1188     				setup[setup_count].delay = DELAY_DEFAULT;
1189     				setup[setup_count].ext_trans = 0;
1190     #if defined(AHA152X_DEBUG)
1191     				setup[setup_count].debug = DEBUG_DEFAULT;
1192     #endif
1193     				setup_count++;
1194     			}
1195     		}
1196     
1197     		if (ok)
1198     			printk("auto configuration: ok, ");
1199     	}
1200     #endif
1201     
1202     	printk("detected %d controller(s)\n", setup_count);
1203     
1204     	for (i=0; i<setup_count; i++) {
1205     		struct Scsi_Host *shpnt;
1206     
1207     		aha152x_host[setup[i].irq - IRQ_MIN] = shpnt =
1208     		    scsi_register(tpnt, sizeof(struct aha152x_hostdata));
1209     
1210     		if(!shpnt) {
1211     			printk(KERN_ERR "aha152x: scsi_register failed\n");
1212     			continue;
1213     		}
1214     
1215     		registered_count++;
1216     
1217     		shpnt->io_port   = setup[i].io_port;
1218     		shpnt->n_io_port = IO_RANGE;
1219     		shpnt->irq       = setup[i].irq;
1220     
1221     		if(!setup[i].tc1550) {
1222     			HOSTIOPORT0 = setup[i].io_port;
1223     			HOSTIOPORT1 = setup[i].io_port;
1224     		} else {
1225     			HOSTIOPORT0 = setup[i].io_port+0x10;
1226     			HOSTIOPORT1 = setup[i].io_port-0x10;
1227     		}
1228     
1229     		ISSUE_SC	= 0;
1230     		CURRENT_SC	= 0;
1231     		DONE_SC		= 0;
1232     		DISCONNECTED_SC	= 0;
1233     
1234     		QLOCK		= SPIN_LOCK_UNLOCKED;
1235     
1236     		STATE		= 0;
1237     		PREVSTATE	= 0;
1238     		LASTSTATE	= 0;
1239     
1240     		MSGILEN		= 0;
1241     		MSGOLEN		= 0;
1242     
1243     		RECONNECT	= setup[i].reconnect;
1244     		SYNCHRONOUS	= setup[i].synchronous;
1245     		PARITY		= setup[i].parity;
1246     		DELAY		= setup[i].delay;
1247     		EXT_TRANS	= setup[i].ext_trans;
1248     #if defined(AHA152X_DEBUG)
1249     		HOSTDATA(shpnt)->debug = setup[i].debug;
1250     #endif
1251     		HOSTDATA(shpnt)->in_intr = 0;
1252     		HOSTDATA(shpnt)->commands = 0;
1253     
1254     #if defined(AHA152X_STAT)
1255     		HOSTDATA(shpnt)->total_commands=0;
1256     		HOSTDATA(shpnt)->disconnections=0;
1257     		HOSTDATA(shpnt)->busfree_without_any_action=0;
1258     		HOSTDATA(shpnt)->busfree_without_old_command=0;
1259     		HOSTDATA(shpnt)->busfree_without_new_command=0;
1260     		HOSTDATA(shpnt)->busfree_without_done_command=0;
1261     		HOSTDATA(shpnt)->busfree_with_check_condition=0;
1262     		for (j = idle; j<maxstate; j++) {
1263     			HOSTDATA(shpnt)->count[j]=0;
1264     			HOSTDATA(shpnt)->count_trans[j]=0;
1265     			HOSTDATA(shpnt)->time[j]=0;
1266     		}
1267     #endif
1268     
1269     		for (j = 0; j < 8; j++) {
1270     			HOSTDATA(shpnt)->syncrate[j] = 0;
1271     			HOSTDATA(shpnt)->syncneg[j] = 0;
1272     		}
1273     
1274     		SETPORT(SCSIID, setup[i].scsiid << 4);
1275     		shpnt->this_id = setup[i].scsiid;
1276     
1277     		if (setup[i].reconnect)
1278     			shpnt->can_queue = AHA152X_MAXQUEUE;
1279     
1280     		/* RESET OUT */
1281     		printk("aha152x: resetting bus...\n");
1282     		SETPORT(SCSISEQ, SCSIRSTO);
1283     		mdelay(256);
1284     		SETPORT(SCSISEQ, 0);
1285     		mdelay(DELAY);
1286     
1287     		reset_ports(shpnt);
1288     
1289     		printk(KERN_INFO
1290     		       "aha152x%d%s: "
1291     		       "vital data: rev=%x, "
1292     		       "io=0x%03lx (0x%03lx/0x%03lx), "
1293     		       "irq=%d, "
1294     		       "scsiid=%d, "
1295     		       "reconnect=%s, "
1296     		       "parity=%s, "
1297     		       "synchronous=%s, "
1298     		       "delay=%d, "
1299     		       "extended translation=%s\n",
1300     		       HOSTNO, setup[i].tc1550 ? " (tc1550 mode)" : "",
1301     		       GETPORT(REV) & 0x7,
1302     		       shpnt->io_port, HOSTIOPORT0, HOSTIOPORT1,
1303     		       shpnt->irq,
1304     		       shpnt->this_id,
1305     		       RECONNECT ? "enabled" : "disabled",
1306     		       PARITY ? "enabled" : "disabled",
1307     		       SYNCHRONOUS ? "enabled" : "disabled",
1308     		       DELAY,
1309     		       EXT_TRANS ? "enabled" : "disabled");
1310     
1311     		request_region(shpnt->io_port, IO_RANGE, "aha152x");
1312     
1313     		/* not expecting any interrupts */
1314     		SETPORT(SIMODE0, 0);
1315     		SETPORT(SIMODE1, 0);
1316     
1317     		ok = request_irq(shpnt->irq, swintr, SA_INTERRUPT|SA_SHIRQ, "aha152x", shpnt);
1318     		if (ok < 0) {
1319     			if (ok==-EINVAL)
1320     				printk(KERN_ERR "aha152x%d: bad IRQ %d.\n", HOSTNO, shpnt->irq);
1321     			else if(ok==-EBUSY)
1322     				printk(KERN_ERR "aha152x%d: IRQ %d already in use.\n", HOSTNO, shpnt->irq);
1323     			else
1324     				printk(KERN_ERR "aha152x%d: Unexpected error code %d on requesting IRQ %d.\n", HOSTNO, ok, shpnt->irq);
1325     
1326     			printk(KERN_ERR "aha152x%d: driver needs an IRQ.\n", HOSTNO);
1327     
1328     			scsi_unregister(shpnt);
1329     			registered_count--;
1330     			release_region(shpnt->io_port, IO_RANGE);
1331     			aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1332     			shpnt = 0;
1333     			continue;
1334     		}
1335     		HOSTDATA(shpnt)->swint = 0;
1336     
1337     		printk(KERN_INFO "aha152x%d: trying software interrupt, ", HOSTNO);
1338     		SETPORT(DMACNTRL0, SWINT|INTEN);
1339     		spin_unlock_irq(&io_request_lock);
1340     		mdelay(1000);
1341     		spin_lock_irq(&io_request_lock);
1342     		free_irq(shpnt->irq, shpnt);
1343     
1344     		if (!HOSTDATA(shpnt)->swint) {
1345     			if (TESTHI(DMASTAT, INTSTAT)) {
1346     				printk("lost.\n");
1347     			} else {
1348     				printk("failed.\n");
1349     			}
1350     
1351     			SETPORT(DMACNTRL0, INTEN);
1352     
1353     			printk(KERN_ERR "aha152x%d: IRQ %d possibly wrong.  Please verify.\n", HOSTNO, shpnt->irq);
1354     
1355     			registered_count--;
1356     			release_region(shpnt->io_port, IO_RANGE);
1357     			aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1358     			scsi_unregister(shpnt);
1359     			shpnt=NULL;
1360     			continue;
1361     		}
1362     		printk("ok.\n");
1363     
1364     
1365     		/* clear interrupts */
1366     		SETPORT(SSTAT0, 0x7f);
1367     		SETPORT(SSTAT1, 0xef);
1368     
1369     		if (request_irq(shpnt->irq, intr, SA_INTERRUPT|SA_SHIRQ, "aha152x", shpnt) < 0) {
1370     			printk(KERN_ERR "aha152x%d: failed to reassign interrupt.\n", HOSTNO);
1371     
1372     			scsi_unregister(shpnt);
1373     			registered_count--;
1374     			release_region(shpnt->io_port, IO_RANGE);
1375     			shpnt = aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1376     			continue;
1377     		}
1378     	}
1379     
1380     	return registered_count>0;
1381     }
1382     
1383     
1384     int aha152x_release(struct Scsi_Host *shpnt)
1385     {
1386     	if (shpnt->irq)
1387     		free_irq(shpnt->irq, shpnt);
1388     
1389     	if (shpnt->io_port)
1390     		release_region(shpnt->io_port, IO_RANGE);
1391     
1392     #ifdef __ISAPNP__
1393     	while (num_pnpdevs--)
1394     		pnpdev[num_pnpdevs]->deactivate(pnpdev[num_pnpdevs]);
1395     #endif
1396     	scsi_unregister(shpnt);
1397     
1398     	return 0;
1399     }
1400     
1401     /*
1402      * setup controller to generate interrupts depending
1403      * on current state (lock has to be acquired)
1404      *
1405      */ 
1406     static int setup_expected_interrupts(struct Scsi_Host *shpnt)
1407     {
1408     	ASSERT_LOCK(&QLOCK,1);
1409     
1410     	if(CURRENT_SC) {
1411     		CURRENT_SC->SCp.phase |= 1 << 16;
1412     	
1413     		if(CURRENT_SC->SCp.phase & selecting) {
1414     			DPRINTK(debug_intr, DEBUG_LEAD "expecting: (seldo) (seltimo) (seldi)\n", CMDINFO(CURRENT_SC));
1415     			SETPORT(SSTAT1, SELTO);
1416     			SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1417     			SETPORT(SIMODE1, ENSELTIMO);
1418     		} else {
1419     			DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (busfree) %s\n", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.phase & spiordy ? "(spiordy)" : "");
1420     			SETPORT(SIMODE0, (CURRENT_SC->SCp.phase & spiordy) ? ENSPIORDY : 0);
1421     			SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE); 
1422     		}
1423     	} else if(STATE==seldi) {
1424     		DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (identify)\n", CMDINFO(CURRENT_SC));
1425     		SETPORT(SIMODE0, 0);
1426     		SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE); 
1427     	} else {
1428     		DPRINTK(debug_intr, DEBUG_LEAD "expecting: %s %s\n",
1429     			CMDINFO(CURRENT_SC),
1430     			DISCONNECTED_SC ? "(reselection)" : "",
1431     			ISSUE_SC ? "(busfree)" : "");
1432     		SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1433     		SETPORT(SIMODE1, ENSCSIRST | ( (ISSUE_SC||DONE_SC) ? ENBUSFREE : 0));
1434     	}
1435     
1436     	if(!HOSTDATA(shpnt)->in_intr)
1437     		SETBITS(DMACNTRL0, INTEN);
1438     
1439     	return TESTHI(DMASTAT, INTSTAT);
1440     }
1441     
1442     
1443     /* 
1444      *  Queue a command and setup interrupts for a free bus.
1445      */
1446     int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct semaphore *sem, int phase, Scsi_Cmnd *done_SC, void (*done)(Scsi_Cmnd *))
1447     {
1448     	struct Scsi_Host *shpnt = SCpnt->host;
1449     	unsigned long flags;
1450     
1451     #if defined(AHA152X_DEBUG)
1452     	if (HOSTDATA(shpnt)->debug & debug_queue) {
1453     		printk(INFO_LEAD "queue: cmd_len=%d pieces=%d size=%u cmnd=",
1454     		       CMDINFO(SCpnt), SCpnt->cmd_len, SCpnt->use_sg, SCpnt->request_bufflen);
1455     		print_command(SCpnt->cmnd);
1456     	}
1457     #endif
1458     
1459     	SCpnt->scsi_done	= done;
1460     	SCpnt->resid 		= SCpnt->request_bufflen;
1461     	SCpnt->SCp.phase	= not_issued | phase;
1462     	SCpnt->SCp.Status	= CHECK_CONDITION;
1463     	SCpnt->SCp.Message	= 0;
1464     	SCpnt->SCp.have_data_in	= 0;
1465     	SCpnt->SCp.sent_command	= 0;
1466     	SCpnt->host_scribble    = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
1467     	if(!SCpnt->host_scribble) {
1468     		printk(ERR_LEAD "allocation failed\n", CMDINFO(SCpnt));
1469     		return FAILED;
1470     	}
1471     
1472     	SCNEXT(SCpnt)		= 0;
1473     	SCDONE(SCpnt)		= done_SC;
1474     	SCSEM(SCpnt)		= sem;
1475     
1476     	/* setup scratch area
1477     	   SCp.ptr              : buffer pointer
1478     	   SCp.this_residual    : buffer length
1479     	   SCp.buffer           : next buffer
1480     	   SCp.buffers_residual : left buffers in list
1481     	   SCp.phase            : current state of the command */
1482     	if (SCpnt->use_sg) {
1483     		SCpnt->SCp.buffer           = (struct scatterlist *) SCpnt->request_buffer;
1484     		SCpnt->SCp.ptr              = SCpnt->SCp.buffer->address;
1485     		SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
1486     		SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
1487     	} else {
1488     		SCpnt->SCp.ptr              = (char *) SCpnt->request_buffer;
1489     		SCpnt->SCp.this_residual    = SCpnt->request_bufflen;
1490     		SCpnt->SCp.buffer           = NULL;
1491     		SCpnt->SCp.buffers_residual = 0;
1492     	}
1493     
1494     	DO_LOCK(flags);
1495     
1496     #if defined(AHA152X_STAT)
1497     	HOSTDATA(shpnt)->total_commands++;
1498     #endif
1499     
1500     	/* Turn led on, when this is the first command. */
1501     	HOSTDATA(shpnt)->commands++;
1502     	if (HOSTDATA(shpnt)->commands==1)
1503     		SETPORT(PORTA, 1);
1504     
1505     	append_SC(&ISSUE_SC, SCpnt);
1506     
1507     	if(!HOSTDATA(shpnt)->in_intr)
1508     		setup_expected_interrupts(shpnt);
1509     
1510     	DO_UNLOCK(flags);
1511     
1512     	return 0;
1513     }
1514     
1515     int aha152x_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1516     {
1517     #if 0
1518     	if(*SCpnt->cmnd == REQUEST_SENSE) {
1519     		SCpnt->result = 0;
1520     		done(SCpnt);
1521     
1522     		return 0;
1523     	}
1524     #endif
1525     
1526     	return aha152x_internal_queue(SCpnt, 0, 0, 0, done);
1527     }
1528     
1529     
1530     /*
1531      *  run a command
1532      *
1533      */
1534     void internal_done(Scsi_Cmnd *SCpnt)
1535     {
1536     #if 0
1537     	struct Scsi_Host *shpnt = SCpnt->host;
1538     
1539     	DPRINTK(debug_eh, INFO_LEAD "internal_done called\n", CMDINFO(SCpnt));
1540     #endif
1541     	if(SCSEM(SCpnt))
1542     		up(SCSEM(SCpnt));
1543     }
1544     
1545     int aha152x_command(Scsi_Cmnd * SCpnt)
1546     {
1547     	DECLARE_MUTEX_LOCKED(sem);
1548     
1549     	aha152x_internal_queue(SCpnt, &sem, 0, 0, internal_done);
1550     	down(&sem);
1551     
1552     	return SUCCESS;
1553     }
1554     
1555     /*
1556      *  Abort a command
1557      *
1558      */
1559     int aha152x_abort(Scsi_Cmnd *SCpnt)
1560     {
1561     	struct Scsi_Host *shpnt = SCpnt->host;
1562     	Scsi_Cmnd *ptr;
1563     	unsigned long flags;
1564     
1565     	if(!shpnt) {
1566     		printk(ERR_LEAD "abort(%p): no host structure\n", CMDINFO(SCpnt), SCpnt);
1567     		return FAILED;
1568     	}
1569     
1570     #if defined(AHA152X_DEBUG)
1571     	if(HOSTDATA(shpnt)->debug & debug_eh) {
1572     		printk(DEBUG_LEAD "abort(%p)", CMDINFO(SCpnt), SCpnt);
1573     		show_queues(shpnt);
1574     		mdelay(1000);
1575     	}
1576     #endif
1577     
1578     	DO_LOCK(flags);
1579     
1580     	ptr=remove_SC(&ISSUE_SC, SCpnt);
1581     
1582     	if(ptr) {
1583     		DPRINTK(debug_eh, DEBUG_LEAD "not yet issued - SUCCESS\n", CMDINFO(SCpnt));
1584     
1585     		HOSTDATA(shpnt)->commands--;
1586     		if (!HOSTDATA(shpnt)->commands)
1587     			SETPORT(PORTA, 0);
1588     		DO_UNLOCK(flags);
1589     
1590     		kfree(SCpnt->host_scribble);
1591     		SCpnt->host_scribble=0;
1592     
1593     		return SUCCESS;
1594     	} 
1595     
1596     	DO_UNLOCK(flags);
1597     
1598     	/*
1599     	 * FIXME:
1600     	 * for current command: queue ABORT for message out and raise ATN
1601     	 * for disconnected command: pseudo SC with ABORT message or ABORT on reselection?
1602     	 *
1603     	 */
1604     
1605     	printk(ERR_LEAD "cannot abort running or disconnected command\n", CMDINFO(SCpnt));
1606     
1607     	return FAILED;
1608     }
1609     
1610     static void timer_expired(unsigned long p)
1611     {
1612     	struct semaphore *sem = (void *)p;
1613     
1614     	printk(KERN_INFO "aha152x: timer expired\n");
1615     	up(sem);
1616     }
1617     
1618     /*
1619      * Reset a device
1620      *
1621      * FIXME: never seen this live. might lockup...
1622      *
1623      */
1624     int aha152x_device_reset(Scsi_Cmnd * SCpnt)
1625     {
1626     	struct Scsi_Host *shpnt = SCpnt->host;
1627     	DECLARE_MUTEX_LOCKED(sem);
1628     	struct timer_list timer;
1629     	Scsi_Cmnd cmnd;
1630     
1631     #if defined(AHA152X_DEBUG)
1632     	if(HOSTDATA(shpnt)->debug & debug_eh) {
1633     		printk(INFO_LEAD "aha152x_device_reset(%p)", CMDINFO(SCpnt), SCpnt);
1634     		show_queues(shpnt);
1635     		mdelay(1000);
1636     	}
1637     #endif
1638     
1639     	if(CURRENT_SC==SCpnt) {
1640     		printk(ERR_LEAD "cannot reset current device\n", CMDINFO(SCpnt));
1641     		return FAILED;
1642     	}
1643     
1644     	cmnd.cmd_len         = 0;
1645     	cmnd.host            = SCpnt->host;
1646     	cmnd.target          = SCpnt->target;
1647     	cmnd.lun             = SCpnt->lun;
1648     	cmnd.use_sg          = 0;
1649     	cmnd.request_buffer  = 0;
1650     	cmnd.request_bufflen = 0;
1651     
1652     	init_timer(&timer);
1653     	timer.data     = (unsigned long) &sem;
1654     	timer.expires  = jiffies + 100*HZ;   /* 10s */
1655     	timer.function = (void (*)(unsigned long)) timer_expired;
1656     	add_timer(&timer);
1657     
1658     	aha152x_internal_queue(&cmnd, &sem, resetting, 0, internal_done);
1659     
1660     	down(&sem);
1661     
1662     	del_timer(&timer);
1663     
1664     	if(cmnd.SCp.phase & resetted) {
1665     		return SUCCESS;
1666     	} else {
1667     		return FAILED;
1668     	}
1669     }
1670     
1671     void free_hard_reset_SCs(struct Scsi_Host *shpnt, Scsi_Cmnd **SCs)
1672     {
1673     	Scsi_Cmnd *ptr;
1674     	unsigned long flags;
1675     
1676     	DO_LOCK(flags);
1677     
1678     	ptr=*SCs;
1679     	while(ptr) {
1680     		Scsi_Cmnd *next = SCNEXT(ptr);
1681     
1682     		if (!ptr->device->soft_reset) {
1683     			DPRINTK(debug_eh, DEBUG_LEAD "disconnected command %p removed\n", CMDINFO(ptr), ptr);
1684     			remove_SC(SCs, ptr);
1685     			HOSTDATA(shpnt)->commands--;
1686     			kfree(ptr->host_scribble);
1687     			ptr->host_scribble=0;
1688     		}
1689     
1690     		ptr = next;
1691     	}
1692     
1693     	DO_UNLOCK(flags);
1694     }
1695     
1696     /*
1697      * Reset the bus
1698      *
1699      */
1700     int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
1701     {
1702     	struct Scsi_Host *shpnt = SCpnt->host;
1703     	unsigned long flags;
1704     
1705     #if defined(AHA152X_DEBUG)
1706     	if(HOSTDATA(shpnt)->debug & debug_eh) {
1707     		printk(DEBUG_LEAD "aha152x_bus_reset(%p)", CMDINFO(SCpnt), SCpnt);
1708     		show_queues(shpnt);
1709     		mdelay(1000);
1710     	}
1711     #endif
1712     
1713     	free_hard_reset_SCs(shpnt, &ISSUE_SC);
1714     	free_hard_reset_SCs(shpnt, &DISCONNECTED_SC);
1715     
1716     	DPRINTK(debug_eh, DEBUG_LEAD "resetting bus\n", CMDINFO(SCpnt));
1717     
1718     	SETPORT(SCSISEQ, SCSIRSTO);
1719     	mdelay(256);
1720     	SETPORT(SCSISEQ, 0);
1721     	mdelay(DELAY);
1722     
1723     	DPRINTK(debug_eh, DEBUG_LEAD "bus reset returns\n", CMDINFO(SCpnt));
1724     
1725     	DO_LOCK(flags);
1726     	setup_expected_interrupts(shpnt);
1727     	if(HOSTDATA(shpnt)->commands==0)
1728     		SETPORT(PORTA, 0);
1729     	DO_UNLOCK(flags);
1730     
1731     	return SUCCESS;
1732     }
1733     
1734     
1735     /*
1736      *  Restore default values to the AIC-6260 registers and reset the fifos
1737      *
1738      */
1739     static void reset_ports(struct Scsi_Host *shpnt)
1740     {
1741     	unsigned long flags;
1742     
1743     	/* disable interrupts */
1744     	SETPORT(DMACNTRL0, RSTFIFO);
1745     
1746     	SETPORT(SCSISEQ, 0);
1747     
1748     	SETPORT(SXFRCTL1, 0);
1749     	SETPORT(SCSISIG, 0);
1750     	SETRATE(0);
1751     
1752     	/* clear all interrupt conditions */
1753     	SETPORT(SSTAT0, 0x7f);
1754     	SETPORT(SSTAT1, 0xef);
1755     
1756     	SETPORT(SSTAT4, SYNCERR | FWERR | FRERR);
1757     
1758     	SETPORT(DMACNTRL0, 0);
1759     	SETPORT(DMACNTRL1, 0);
1760     
1761     	SETPORT(BRSTCNTRL, 0xf1);
1762     
1763     	/* clear SCSI fifos and transfer count */
1764     	SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1765     	SETPORT(SXFRCTL0, CH1);
1766     
1767     	DO_LOCK(flags);
1768     	setup_expected_interrupts(shpnt);
1769     	DO_UNLOCK(flags);
1770     }
1771     
1772     /*
1773      * Reset the host (bus and controller)
1774      *
1775      */
1776     int aha152x_host_reset(Scsi_Cmnd * SCpnt)
1777     {
1778     #if defined(AHA152X_DEBUG)
1779     	struct Scsi_Host *shpnt = SCpnt->host;
1780     #endif
1781     
1782     	DPRINTK(debug_eh, DEBUG_LEAD "aha152x_host_reset(%p)\n", CMDINFO(SCpnt), SCpnt);
1783     
1784     	aha152x_bus_reset(SCpnt);
1785     
1786     	DPRINTK(debug_eh, DEBUG_LEAD "resetting ports\n", CMDINFO(SCpnt));
1787     	reset_ports(SCpnt->host);
1788     
1789     	return SUCCESS;
1790     }
1791     
1792     /*
1793      * Return the "logical geometry"
1794      *
1795      */
1796     int aha152x_biosparam(Scsi_Disk * disk, kdev_t dev, int *info_array)
1797     {
1798     	struct Scsi_Host *shpnt = disk->device->host;
1799     
1800     	/* try default translation */
1801     	info_array[0] = 64;
1802     	info_array[1] = 32;
1803     	info_array[2] = disk->capacity / (64 * 32);
1804     
1805     	/* for disks >1GB do some guessing */
1806     	if (info_array[2] >= 1024) {
1807     		int info[3];
1808     
1809     		/* try to figure out the geometry from the partition table */
1810     		if (scsicam_bios_param(disk, dev, info) < 0 ||
1811     		    !((info[0] == 64 && info[1] == 32) || (info[0] == 255 && info[1] == 63))) {
1812     			if (EXT_TRANS) {
1813     				printk(KERN_NOTICE
1814     				       "aha152x: unable to verify geometry for disk with >1GB.\n"
1815     				       "         using extended translation.\n");
1816     				info_array[0] = 255;
1817     				info_array[1] = 63;
1818     				info_array[2] = disk->capacity / (255 * 63);
1819     			} else {
1820     				printk(KERN_NOTICE
1821     				       "aha152x: unable to verify geometry for disk with >1GB.\n"
1822     				       "         Using default translation. Please verify yourself.\n"
1823     				       "         Perhaps you need to enable extended translation in the driver.\n"
1824     				       "         See /usr/src/linux/drivers/scsi/README.aha152x for details.\n");
1825     			}
1826     		} else {
1827     			info_array[0] = info[0];
1828     			info_array[1] = info[1];
1829     			info_array[2] = info[2];
1830     
1831     			if (info[0] == 255 && !EXT_TRANS) {
1832     				printk(KERN_NOTICE
1833     				       "aha152x: current partition table is using extended translation.\n"
1834     				       "         using it also, although it's not explicitly enabled.\n");
1835     			}
1836     		}
1837     	}
1838     
1839     	return 0;
1840     }
1841     
1842     /*
1843      *  Internal done function
1844      *
1845      */
1846     static void done(struct Scsi_Host *shpnt, int error)
1847     {
1848     	if (CURRENT_SC) {
1849     		if(DONE_SC)
1850     			printk(ERR_LEAD "there's already a completed command %p - will cause abort\n", CMDINFO(CURRENT_SC), DONE_SC);
1851     
1852     		DONE_SC = CURRENT_SC;
1853     		CURRENT_SC = 0;
1854     		DONE_SC->result = error;
1855     	} else
1856     		printk(KERN_ERR "aha152x: done() called outside of command\n");
1857     }
1858     
1859     static struct tq_struct aha152x_tq;
1860     
1861     /*
1862      * Run service completions on the card with interrupts enabled.
1863      *
1864      */
1865     static void run(void)
1866     {
1867     	int i;
1868     	for (i = 0; i < IRQS; i++) {
1869     		struct Scsi_Host *shpnt = aha152x_host[i];
1870     		if (shpnt && HOSTDATA(shpnt)->service) {
1871     			HOSTDATA(shpnt)->service=0;
1872     			is_complete(shpnt);
1873     		}
1874     	}
1875     }
1876     
1877     /*
1878      *    Interrupts handler
1879      *
1880      */
1881     
1882     static void intr(int irqno, void *dev_id, struct pt_regs *regs)
1883     {
1884     	struct Scsi_Host *shpnt = aha152x_host[irqno - IRQ_MIN];
1885     
1886     	if (!shpnt) {
1887     		printk(KERN_ERR "aha152x: catched interrupt for unknown controller.\n");
1888     		return;
1889     	}
1890     
1891     	/* no more interrupts from the controller, while we're busy.
1892     	   INTEN is restored by the BH handler */
1893     	CLRBITS(DMACNTRL0, INTEN);
1894     
1895     #if 0
1896     	/* check if there is already something to be
1897                serviced; should not happen */
1898     	if(HOSTDATA(shpnt)->service) {
1899     		printk(KERN_ERR "aha152x%d: lost interrupt (%d)\n", HOSTNO, HOSTDATA(shpnt)->service);
1900     	        show_queues(shpnt);
1901     	}
1902     #endif
1903     	
1904     	/* Poke the BH handler */
1905     	HOSTDATA(shpnt)->service++;
1906     	aha152x_tq.routine = (void *) run;
1907     	queue_task(&aha152x_tq, &tq_immediate);
1908     	mark_bh(IMMEDIATE_BH);
1909     }
1910     
1911     /*
1912      * busfree phase
1913      * - handle completition/disconnection/error of current command
1914      * - start selection for next command (if any)
1915      */
1916     static void busfree_run(struct Scsi_Host *shpnt)
1917     {
1918     	unsigned long flags;
1919     #if defined(AHA152X_STAT)
1920     	int action=0;
1921     #endif
1922     
1923     	SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1924     	SETPORT(SXFRCTL0, CH1);
1925     
1926     	SETPORT(SSTAT1, CLRBUSFREE);
1927     	
1928     	if(CURRENT_SC) {
1929     #if defined(AHA152X_STAT)
1930     		action++;
1931     #endif
1932     		CURRENT_SC->SCp.phase &= ~syncneg;
1933     
1934     		if(CURRENT_SC->SCp.phase & completed) {
1935     			/* target sent COMMAND COMPLETE */
1936     			done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16));
1937     
1938     		} else if(CURRENT_SC->SCp.phase & aborted) {
1939     			DPRINTK(debug_eh, DEBUG_LEAD "ABORT sent\n", CMDINFO(CURRENT_SC));
1940     			done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_ABORT << 16));
1941     
1942     		} else if(CURRENT_SC->SCp.phase & resetted) {
1943     			DPRINTK(debug_eh, DEBUG_LEAD "BUS DEVICE RESET sent\n", CMDINFO(CURRENT_SC));
1944     			done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_RESET << 16));
1945     
1946     		} else if(CURRENT_SC->SCp.phase & disconnected) {
1947     			/* target sent DISCONNECT */
1948     			DPRINTK(debug_selection, DEBUG_LEAD "target disconnected at %d/%d\n",
1949     				CMDINFO(CURRENT_SC),
1950     				CURRENT_SC->resid,
1951     				CURRENT_SC->request_bufflen);
1952     #if defined(AHA152X_STAT)
1953     			HOSTDATA(shpnt)->disconnections++;
1954     #endif
1955     			append_SC(&DISCONNECTED_SC, CURRENT_SC);
1956     			CURRENT_SC->SCp.phase |= 1 << 16;
1957     			CURRENT_SC = 0;
1958     
1959     		} else {
1960     			done(shpnt, DID_ERROR << 16);
1961     		}
1962     #if defined(AHA152X_STAT)
1963     	} else {
1964     		HOSTDATA(shpnt)->busfree_without_old_command++;
1965     #endif
1966     	}
1967     
1968     	DO_LOCK(flags);
1969     
1970     	if(DONE_SC) {
1971     #if defined(AHA152X_STAT)
1972     		action++;
1973     #endif
1974     		if(SCDONE(DONE_SC)) {
1975     			Scsi_Cmnd *ptr=DONE_SC;
1976     			DONE_SC=SCDONE(DONE_SC);
1977     
1978     #if 0
1979     			if(HOSTDATA(shpnt)->debug & debug_eh) {
1980     				printk(ERR_LEAD "received sense: ", CMDINFO(ptr));
1981     				print_sense("bh", DONE_SC);
1982     			}
1983     #endif
1984     
1985     			HOSTDATA(shpnt)->commands--;
1986     			if (!HOSTDATA(shpnt)->commands)
1987     				SETPORT(PORTA, 0);	/* turn led off */
1988     
1989     			kfree(ptr->host_scribble);
1990     			kfree(ptr);
1991     		} else if(DONE_SC->SCp.Status==0x02) {
1992     #if defined(AHA152X_STAT)
1993     			HOSTDATA(shpnt)->busfree_with_check_condition++;
1994     #endif
1995     #if 0
1996     			DPRINTK(debug_eh, ERR_LEAD "CHECK CONDITION found\n", CMDINFO(DONE_SC));
1997     #endif
1998     
1999     			if(!(DONE_SC->SCp.Status & not_issued)) {
2000     				Scsi_Cmnd *cmnd = kmalloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2001     
2002     				if(cmnd) {
2003     					Scsi_Cmnd *ptr=DONE_SC;
2004     					DONE_SC=0;
2005     
2006     #if 0
2007     					DPRINTK(debug_eh, ERR_LEAD "requesting sense\n", CMDINFO(ptr));
2008     #endif
2009     
2010     					cmnd->cmnd[0]         = REQUEST_SENSE;
2011     					cmnd->cmnd[1]         = 0;
2012     					cmnd->cmnd[2]         = 0;
2013     					cmnd->cmnd[3]         = 0;
2014     					cmnd->cmnd[4]         = sizeof(ptr->sense_buffer);
2015     					cmnd->cmnd[5]         = 0;
2016     					cmnd->cmd_len	      = 6;
2017     					cmnd->host            = ptr->host;
2018     					cmnd->target          = ptr->target;
2019     					cmnd->lun             = ptr->lun;
2020     					cmnd->use_sg          = 0; 
2021     					cmnd->request_buffer  = ptr->sense_buffer;
2022     					cmnd->request_bufflen = sizeof(ptr->sense_buffer);
2023     			
2024     					DO_UNLOCK(flags);
2025     					aha152x_internal_queue(cmnd, 0, 0, ptr, internal_done);
2026     					DO_LOCK(flags);
2027     				} else {
2028     					printk(ERR_LEAD "allocation failed\n", CMDINFO(CURRENT_SC));
2029     					if(cmnd)
2030     						kfree(cmnd);
2031     				}
2032     			} else {
2033     #if 0
2034     				DPRINTK(debug_eh, ERR_LEAD "command not issued - CHECK CONDITION ignored\n", CMDINFO(DONE_SC));
2035     #endif				
2036     			}
2037     		}
2038     
2039     		if(DONE_SC && DONE_SC->scsi_done) {
2040     			/* turn led off, when no commands are in the driver */
2041     			HOSTDATA(shpnt)->commands--;
2042     			if (!HOSTDATA(shpnt)->commands)
2043     				SETPORT(PORTA, 0);	/* turn led off */
2044     
2045     			DO_UNLOCK(flags);
2046     			DPRINTK(debug_done, DEBUG_LEAD "calling scsi_done(%p)\n", CMDINFO(DONE_SC), DONE_SC);
2047                     	DONE_SC->scsi_done(DONE_SC);
2048     			DPRINTK(debug_done, DEBUG_LEAD "scsi_done(%p) returned\n", CMDINFO(DONE_SC), DONE_SC);
2049     			DO_LOCK(flags);
2050     
2051     			kfree(DONE_SC->host_scribble);
2052     			DONE_SC->host_scribble=0;
2053     		}
2054     
2055     		DONE_SC=0;
2056     #if defined(AHA152X_STAT)
2057     	} else {
2058     		HOSTDATA(shpnt)->busfree_without_done_command++;
2059     #endif
2060     	}
2061     
2062     	if(ISSUE_SC)
2063     		CURRENT_SC = remove_first_SC(&ISSUE_SC);
2064     
2065     	DO_UNLOCK(flags);
2066     
2067     	if(CURRENT_SC) {
2068     #if defined(AHA152X_STAT)
2069     		action++;
2070     #endif
2071     	    	CURRENT_SC->SCp.phase |= selecting;
2072     
2073     		DPRINTK(debug_selection, DEBUG_LEAD "selecting target\n", CMDINFO(CURRENT_SC));
2074     
2075     		/* clear selection timeout */
2076     		SETPORT(SSTAT1, SELTO);
2077     
2078     		SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
2079     		SETPORT(SXFRCTL1, (PARITY ? ENSPCHK : 0 ) | ENSTIMER);
2080     		SETPORT(SCSISEQ, ENSELO | ENAUTOATNO | (DISCONNECTED_SC ? ENRESELI : 0));
2081     	} else {
2082     #if defined(AHA152X_STAT)
2083     		HOSTDATA(shpnt)->busfree_without_new_command++;
2084     #endif
2085     		SETPORT(SCSISEQ, DISCONNECTED_SC ? ENRESELI : 0);
2086     	}
2087     
2088     #if defined(AHA152X_STAT)
2089     	if(!action)
2090     		HOSTDATA(shpnt)->busfree_without_any_action++;
2091     #endif
2092     }
2093     
2094     /*
2095      * Selection done (OUT)
2096      * - queue IDENTIFY message and SDTR to selected target for message out
2097      *   (ATN asserted automagically via ENAUTOATNO in busfree())
2098      */
2099     static void seldo_run(struct Scsi_Host *shpnt)
2100     {
2101     	SETPORT(SCSISIG, 0);
2102     	SETPORT(SSTAT1, CLRBUSFREE);
2103     	SETPORT(SSTAT1, CLRPHASECHG);
2104     
2105         	CURRENT_SC->SCp.phase &= ~(selecting|not_issued);
2106     
2107     	SETPORT(SCSISEQ, 0);
2108     
2109     	if (TESTLO(SSTAT0, SELDO)) {
2110     		printk(ERR_LEAD "aha152x: passing bus free condition\n", CMDINFO(CURRENT_SC));
2111     		done(shpnt, DID_NO_CONNECT << 16);
2112     		return;
2113     	}
2114     
2115     	SETPORT(SSTAT0, CLRSELDO);
2116     	
2117     	ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->lun));
2118     
2119     	if (CURRENT_SC->SCp.phase & aborting) {
2120     		ADDMSGO(ABORT);
2121     	} else if (CURRENT_SC->SCp.phase & resetting) {
2122     		ADDMSGO(BUS_DEVICE_RESET);
2123     	} else if (SYNCNEG==0 && SYNCHRONOUS) {
2124         		CURRENT_SC->SCp.phase |= syncneg;
2125     		ADDMSGO(EXTENDED_MESSAGE);
2126     		ADDMSGO(3);
2127     		ADDMSGO(EXTENDED_SDTR);
2128     		ADDMSGO(50);		/* 200ns */
2129     		ADDMSGO(8);		/* 8 byte req/ack offset */
2130     
2131     		SYNCNEG=1;		/* negotiation in progress */
2132     	}
2133     
2134     	SETRATE(SYNCRATE);
2135     }
2136     
2137     /*
2138      * Selection timeout
2139      * - return command to mid-level with failure cause
2140      *
2141      */
2142     static void selto_run(struct Scsi_Host *shpnt)
2143     {
2144     	SETPORT(SCSISEQ, 0);		
2145     	SETPORT(SSTAT1, CLRSELTIMO);
2146     
2147     	DPRINTK(debug_selection, DEBUG_LEAD "selection timeout\n", CMDINFO(CURRENT_SC));
2148     
2149     	if(!CURRENT_SC) {
2150     		DPRINTK(debug_selection, DEBUG_LEAD "!CURRENT_SC\n", CMDINFO(CURRENT_SC));
2151     		return;
2152     	}
2153     
2154         	CURRENT_SC->SCp.phase &= ~selecting;
2155     
2156     	if (CURRENT_SC->SCp.phase & aborted) {
2157     		DPRINTK(debug_selection, DEBUG_LEAD "aborted\n", CMDINFO(CURRENT_SC));
2158     		done(shpnt, DID_ABORT << 16);
2159     	} else if (TESTLO(SSTAT0, SELINGO)) {
2160     		DPRINTK(debug_selection, DEBUG_LEAD "arbitration not won\n", CMDINFO(CURRENT_SC));
2161     		done(shpnt, DID_BUS_BUSY << 16);
2162     	} else {
2163     		/* ARBITRATION won, but SELECTION failed */
2164     		DPRINTK(debug_selection, DEBUG_LEAD "selection failed\n", CMDINFO(CURRENT_SC));
2165     		done(shpnt, DID_NO_CONNECT << 16);
2166     	}
2167     }
2168     
2169     /*
2170      * Selection in done
2171      * - put current command back to issue queue
2172      *   (reconnection of a disconnected nexus instead
2173      *    of successful selection out)
2174      *
2175      */
2176     static void seldi_run(struct Scsi_Host *shpnt)
2177     {
2178     	int selid;
2179     	int target;
2180     	unsigned long flags;
2181     
2182     	SETPORT(SCSISIG, 0);
2183     	SETPORT(SSTAT0, CLRSELDI);
2184     	SETPORT(SSTAT1, CLRBUSFREE);
2185     	SETPORT(SSTAT1, CLRPHASECHG);
2186     
2187     	if(CURRENT_SC) {
2188     		if(!(CURRENT_SC->SCp.phase & not_issued))
2189     			printk(ERR_LEAD "command should not have been issued yet\n", CMDINFO(CURRENT_SC));
2190     
2191     		DPRINTK(debug_selection, ERR_LEAD "command requeued - reselection\n", CMDINFO(CURRENT_SC));
2192     
2193     		DO_LOCK(flags);
2194     		append_SC(&ISSUE_SC, CURRENT_SC);
2195     		DO_UNLOCK(flags);
2196     
2197     		CURRENT_SC = 0;
2198     	}
2199     
2200     	if(!DISCONNECTED_SC) {
2201     		DPRINTK(debug_selection, DEBUG_LEAD "unexpected SELDI ", CMDINFO(CURRENT_SC));
2202     		return;
2203     	}
2204     
2205     	RECONN_TARGET=-1;
2206     
2207     	selid = GETPORT(SELID) & ~(1 << shpnt->this_id);
2208     
2209     	if (selid==0) {
2210     		printk("aha152x%d: target id unknown (%02x)\n", HOSTNO, selid);
2211     		return;
2212     	}
2213     
2214     	for(target=7; !(selid & (1 << target)); target--)
2215     		;
2216     
2217     	if(selid & ~(1 << target)) {
2218     		printk("aha152x%d: multiple targets reconnected (%02x)\n",
2219     		       HOSTNO, selid);
2220     	}
2221     
2222     
2223     	SETPORT(SCSIID, (shpnt->this_id << OID_) | target);
2224     	SETPORT(SCSISEQ, 0);
2225     
2226     	SETRATE(HOSTDATA(shpnt)->syncrate[target]);
2227     
2228     	RECONN_TARGET=target;
2229     	DPRINTK(debug_selection, DEBUG_LEAD "target %d reselected (%02x).\n", CMDINFO(CURRENT_SC), target, selid);
2230     }
2231     
2232     /*
2233      * message in phase
2234      * - handle initial message after reconnection to identify
2235      *   reconnecting nexus
2236      * - queue command on DISCONNECTED_SC on DISCONNECT message
2237      * - set completed flag on COMMAND COMPLETE
2238      *   (other completition code moved to busfree_run)
2239      * - handle response to SDTR
2240      * - clear synchronous transfer agreements on BUS RESET
2241      *
2242      * FIXME: what about SAVE POINTERS, RESTORE POINTERS?
2243      *
2244      */
2245     static void msgi_run(struct Scsi_Host *shpnt)
2246     {
2247     	for(;;) {
2248     		int sstat1 = GETPORT(SSTAT1);
2249     
2250     		if(sstat1 & (PHASECHG|PHASEMIS|BUSFREE) || !(sstat1 & REQINIT))
2251     			return;
2252     
2253     		if(TESTLO(SSTAT0,SPIORDY)) {
2254     			DPRINTK(debug_msgi, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2255     			return;
2256     		}	
2257     
2258     		ADDMSGI(GETPORT(SCSIDAT));
2259     
2260     #if defined(AHA152X_DEBUG)
2261     		if (HOSTDATA(shpnt)->debug & debug_msgi) {
2262     			printk(INFO_LEAD "inbound message %02x ", CMDINFO(CURRENT_SC), MSGI(0));
2263     			print_msg(&MSGI(0));
2264     			printk("\n");
2265     		}
2266     #endif
2267     
2268     		if(!CURRENT_SC) {
2269     			if(LASTSTATE!=seldi) {
2270     				printk(KERN_ERR "aha152x%d: message in w/o current command not after reselection\n", HOSTNO);
2271     			}
2272     
2273     			/*
2274     	 	 	 * Handle reselection
2275     	 		 */
2276     			if(!(MSGI(0) & IDENTIFY_BASE)) {
2277     				printk(KERN_ERR "aha152x%d: target didn't identify after reselection\n", HOSTNO);
2278     				continue;
2279     			}
2280     
2281     			CURRENT_SC = remove_lun_SC(&DISCONNECTED_SC, RECONN_TARGET, MSGI(0) & 0x3f);
2282     
2283     			if (!CURRENT_SC) {
2284     				show_queues(shpnt);
2285     				printk(KERN_ERR "aha152x%d: no disconnected command for target %d/%d\n", HOSTNO, RECONN_TARGET, MSGI(0) & 0x3f);
2286     				continue;
2287     			}
2288     
2289     			DPRINTK(debug_msgi, DEBUG_LEAD "target reconnected\n", CMDINFO(CURRENT_SC));
2290     
2291     			CURRENT_SC->SCp.Message = MSGI(0);
2292     			CURRENT_SC->SCp.phase &= ~disconnected;
2293     
2294     			MSGILEN=0;
2295     
2296     			/* next message if any */
2297     			continue;
2298     		} 
2299     
2300     		CURRENT_SC->SCp.Message = MSGI(0);
2301     
2302     		switch (MSGI(0)) {
2303     		case DISCONNECT:
2304     			if (!RECONNECT)
2305     				printk(WARN_LEAD "target was not allowed to disconnect\n", CMDINFO(CURRENT_SC));
2306     
2307     			CURRENT_SC->SCp.phase |= disconnected;
2308     			break;
2309     
2310     		case COMMAND_COMPLETE:
2311     			if(CURRENT_SC->SCp.phase & completed)
2312     				DPRINTK(debug_msgi, DEBUG_LEAD "again COMMAND COMPLETE\n", CMDINFO(CURRENT_SC));
2313     
2314     			CURRENT_SC->SCp.phase |= completed;
2315     			break;
2316     
2317     		case MESSAGE_REJECT:
2318     			if (SYNCNEG==1) {
2319     				printk(INFO_LEAD "Synchronous Data Transfer Request was rejected\n", CMDINFO(CURRENT_SC));
2320     				SYNCNEG=2;	/* negotiation completed */
2321     			} else
2322     				printk(INFO_LEAD "inbound message (MESSAGE REJECT)\n", CMDINFO(CURRENT_SC));
2323     			break;
2324     
2325     		case SAVE_POINTERS:
2326     			break;
2327     
2328     		case RESTORE_POINTERS:
2329     			break;
2330     
2331     		case EXTENDED_MESSAGE:
2332     			if(MSGILEN<2 || MSGILEN<MSGI(1)+2) {
2333     				/* not yet completed */
2334     				continue;
2335     			}
2336     
2337     			switch (MSGI(2)) {
2338     			case EXTENDED_SDTR:
2339     				{
2340     					long ticks;
2341     
2342     					if (MSGI(1) != 3) {
2343     						printk(ERR_LEAD "SDTR message length!=3\n", CMDINFO(CURRENT_SC));
2344     						break;
2345     					}
2346     
2347     					if (!HOSTDATA(shpnt)->synchronous)
2348     						break;
2349     
2350     					printk(INFO_LEAD, CMDINFO(CURRENT_SC));
2351     					print_msg(&MSGI(0));
2352     					printk("\n");
2353     
2354     					ticks = (MSGI(3) * 4 + 49) / 50;
2355     
2356     					if (syncneg) {
2357     						/* negotiation in progress */
2358     						if (ticks > 9 || MSGI(4) < 1 || MSGI(4) > 8) {
2359     							ADDMSGO(MESSAGE_REJECT);
2360     							printk(INFO_LEAD "received Synchronous Data Transfer Request invalid - rejected\n", CMDINFO(CURRENT_SC));
2361     							break;
2362     						}
2363     						
2364     						SYNCRATE |= ((ticks - 2) << 4) + MSGI(4);
2365     					} else if (ticks <= 9 && MSGI(4) >= 1) {
2366     						ADDMSGO(EXTENDED_MESSAGE);
2367     						ADDMSGO(3);
2368     						ADDMSGO(EXTENDED_SDTR);
2369     						if (ticks < 4) {
2370     							ticks = 4;
2371     							ADDMSGO(50);
2372     						} else
2373     							ADDMSGO(MSGI(3));
2374     
2375     						if (MSGI(4) > 8)
2376     							MSGI(4) = 8;
2377     
2378     						ADDMSGO(MSGI(4));
2379     
2380     						SYNCRATE |= ((ticks - 2) << 4) + MSGI(4);
2381     					} else {
2382     						/* requested SDTR is too slow, do it asynchronously */
2383     						printk(INFO_LEAD "Synchronous Data Transfer Request too slow - Rejecting\n", CMDINFO(CURRENT_SC));
2384     						ADDMSGO(MESSAGE_REJECT);
2385     					}
2386     
2387     					SYNCNEG=2;		/* negotiation completed */
2388     					SETRATE(SYNCRATE);
2389     				}
2390     				break;
2391     
2392     			case BUS_DEVICE_RESET:
2393     				{
2394     					int i;
2395     
2396     					for(i=0; i<8; i++) {
2397     						HOSTDATA(shpnt)->syncrate[i]=0;
2398     						HOSTDATA(shpnt)->syncneg[i]=0;
2399     					}
2400     
2401     				}
2402     				break;
2403     
2404     			case EXTENDED_MODIFY_DATA_POINTER:
2405     			case EXTENDED_EXTENDED_IDENTIFY:
2406     			case EXTENDED_WDTR:
2407     			default:
2408     				ADDMSGO(MESSAGE_REJECT);
2409     				break;
2410     			}
2411     			break;
2412     		}
2413     
2414     		MSGILEN=0;
2415     	}
2416     }
2417     
2418     static void msgi_end(struct Scsi_Host *shpnt)
2419     {
2420     	if(MSGILEN>0)
2421     		printk(WARN_LEAD "target left before message completed (%d)\n", CMDINFO(CURRENT_SC), MSGILEN);
2422     
2423     	if (MSGOLEN > 0 && !(GETPORT(SSTAT1) & BUSFREE)) {
2424     		DPRINTK(debug_msgi, DEBUG_LEAD "msgo pending\n", CMDINFO(CURRENT_SC));
2425     		SETPORT(SCSISIG, P_MSGI | SIG_ATNO);
2426     	} 
2427     }
2428     
2429     /*
2430      * message out phase
2431      *
2432      */
2433     static void msgo_init(struct Scsi_Host *shpnt)
2434     {
2435     	if(MSGOLEN==0) {
2436     		if((CURRENT_SC->SCp.phase & syncneg) && SYNCNEG==2 && SYNCRATE==0) {
2437     			ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->lun));
2438     		} else {
2439     			printk(INFO_LEAD "unexpected MESSAGE OUT phase; rejecting\n", CMDINFO(CURRENT_SC));
2440     			ADDMSGO(MESSAGE_REJECT);
2441     		}
2442     	}
2443     
2444     #if defined(AHA152X_DEBUG)
2445     	if(HOSTDATA(shpnt)->debug & debug_msgo) {
2446     		int i;
2447     
2448     		printk(DEBUG_LEAD "messages( ", CMDINFO(CURRENT_SC));
2449     		for (i=0; i<MSGOLEN; i+=print_msg(&MSGO(i)), printk(" "))
2450     			;
2451     		printk(")\n");
2452     	}
2453     #endif
2454     }
2455     
2456     /*
2457      * message out phase
2458      *
2459      */
2460     static void msgo_run(struct Scsi_Host *shpnt)
2461     {
2462     	if(MSGO_I==MSGOLEN)
2463     		DPRINTK(debug_msgo, DEBUG_LEAD "messages all sent (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO_I, MSGOLEN);
2464     
2465     	while(MSGO_I<MSGOLEN) {
2466     		DPRINTK(debug_msgo, DEBUG_LEAD "message byte %02x (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO(MSGO_I), MSGO_I, MSGOLEN);
2467     
2468     		if(TESTLO(SSTAT0, SPIORDY)) {
2469     			DPRINTK(debug_msgo, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2470     			return;
2471     		}
2472     
2473     		if (MSGO_I==MSGOLEN-1) {
2474     			/* Leave MESSAGE OUT after transfer */
2475     			SETPORT(SSTAT1, CLRATNO);
2476     		}
2477     
2478     
2479     		if (MSGO(MSGO_I) & IDENTIFY_BASE)
2480     			CURRENT_SC->SCp.phase |= identified;
2481     
2482     		if (MSGO(MSGO_I)==ABORT)
2483     			CURRENT_SC->SCp.phase |= aborted;
2484     
2485     		if (MSGO(MSGO_I)==BUS_DEVICE_RESET)
2486     			CURRENT_SC->SCp.phase |= resetted;
2487     
2488     		SETPORT(SCSIDAT, MSGO(MSGO_I++));
2489     	}
2490     }
2491     
2492     static void msgo_end(struct Scsi_Host *shpnt)
2493     {
2494     	if(MSGO_I<MSGOLEN) {
2495     		printk(ERR_LEAD "message sent incompletely (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO_I, MSGOLEN);
2496     		if(SYNCNEG==1) {
2497     			printk(INFO_LEAD "Synchronous Data Transfer Request was rejected\n", CMDINFO(CURRENT_SC));
2498     			SYNCNEG=2;
2499     		}
2500     	}
2501     		
2502     	MSGO_I  = 0;
2503     	MSGOLEN = 0;
2504     }
2505     
2506     /* 
2507      * command phase
2508      *
2509      */
2510     static void cmd_init(struct Scsi_Host *shpnt)
2511     {
2512     	if (CURRENT_SC->SCp.sent_command) {
2513     		printk(ERR_LEAD "command already sent\n", CMDINFO(CURRENT_SC));
2514     		done(shpnt, DID_ERROR << 16);
2515     		return;
2516     	}
2517     
2518     #if defined(AHA152X_DEBUG)
2519     	if (HOSTDATA(shpnt)->debug & debug_cmd) {
2520     		printk(DEBUG_LEAD "cmd_init: ", CMDINFO(CURRENT_SC));
2521     		print_command(CURRENT_SC->cmnd);
2522     	}
2523     #endif
2524     
2525     	CMD_I=0;
2526     }
2527     
2528     /*
2529      * command phase
2530      *
2531      */
2532     static void cmd_run(struct Scsi_Host *shpnt)
2533     {
2534     	if(CMD_I==CURRENT_SC->cmd_len) {
2535     		DPRINTK(debug_cmd, DEBUG_LEAD "command already completely sent (%d/%d)", CMDINFO(CURRENT_SC), CMD_I, CURRENT_SC->cmd_len);
2536     		disp_ports(shpnt);
2537     	}
2538     
2539     	while(CMD_I<CURRENT_SC->cmd_len) {
2540     		DPRINTK(debug_cmd, DEBUG_LEAD "command byte %02x (%d/%d)\n", CMDINFO(CURRENT_SC), CURRENT_SC->cmnd[CMD_I], CMD_I, CURRENT_SC->cmd_len);
2541     
2542     		if(TESTLO(SSTAT0, SPIORDY)) {
2543     			DPRINTK(debug_cmd, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2544     			return;
2545     		}
2546     
2547     		SETPORT(SCSIDAT, CURRENT_SC->cmnd[CMD_I++]);
2548     	}
2549     }
2550     
2551     static void cmd_end(struct Scsi_Host *shpnt)
2552     {
2553     	if(CMD_I<CURRENT_SC->cmd_len)
2554     		printk(ERR_LEAD "command sent incompletely (%d/%d)\n", CMDINFO(CURRENT_SC), CMD_I, CURRENT_SC->cmd_len);
2555     	else
2556     		CURRENT_SC->SCp.sent_command++;
2557     }
2558     
2559     /*
2560      * status phase
2561      *
2562      */
2563     static void status_run(struct Scsi_Host *shpnt)
2564     {
2565     	if(TESTLO(SSTAT0,SPIORDY)) {
2566     		DPRINTK(debug_status, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2567     		return;
2568     	}
2569     
2570     	CURRENT_SC->SCp.Status = GETPORT(SCSIDAT);
2571     
2572     #if defined(AHA152X_DEBUG)
2573     	if (HOSTDATA(shpnt)->debug & debug_status) {
2574     		printk(DEBUG_LEAD "inbound status %02x ", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.Status);
2575     		print_status(CURRENT_SC->SCp.Status);
2576     		printk("\n");
2577     	}
2578     #endif
2579     }
2580     
2581     /*
2582      * data in phase
2583      *
2584      */
2585     static void datai_init(struct Scsi_Host *shpnt)
2586     {
2587     	SETPORT(DMACNTRL0, RSTFIFO);
2588     	SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
2589     
2590     	SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2591     	SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2592     
2593     	SETPORT(SIMODE0, 0);
2594     	SETPORT(SIMODE1, ENSCSIPERR | ENSCSIRST | ENPHASEMIS | ENBUSFREE);
2595     
2596     	DATA_LEN=0;
2597     	DPRINTK(debug_datai,
2598     		DEBUG_LEAD "datai_init: request_bufflen=%d resid=%d\n",
2599     		CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid);
2600     }
2601     
2602     static void datai_run(struct Scsi_Host *shpnt)
2603     {
2604     	unsigned int the_time;
2605     	int fifodata, data_count;
2606     
2607     	/*
2608     	 * loop while the phase persists or the fifos are not empty
2609     	 *
2610     	 */
2611     	while(TESTLO(DMASTAT, INTSTAT) || TESTLO(DMASTAT, DFIFOEMP) || TESTLO(SSTAT2, SEMPTY)) {
2612     		/* FIXME: maybe this should be done by setting up
2613     		 * STCNT to trigger ENSWRAP interrupt, instead of
2614     		 * polling for DFIFOFULL
2615     		 */
2616     		the_time=jiffies + 10*HZ;
2617     		while(TESTLO(DMASTAT, DFIFOFULL|INTSTAT) && time_before(jiffies,the_time))
2618     			barrier();
2619     
2620     		if(TESTLO(DMASTAT, DFIFOFULL|INTSTAT)) {
2621     			printk(ERR_LEAD "datai timeout", CMDINFO(CURRENT_SC));
2622     			disp_ports(shpnt);
2623     			break;
2624     		}
2625     
2626     		if(TESTHI(DMASTAT, DFIFOFULL)) {
2627     			fifodata = 128;
2628     		} else {
2629     			the_time=jiffies + 10*HZ;
2630     			while(TESTLO(SSTAT2, SEMPTY) && time_before(jiffies,the_time))
2631     				barrier();
2632     
2633     			if(TESTLO(SSTAT2, SEMPTY)) {
2634     				printk(ERR_LEAD "datai sempty timeout", CMDINFO(CURRENT_SC));
2635     				disp_ports(shpnt);
2636     				break;
2637     			}
2638     
2639     			fifodata = GETPORT(FIFOSTAT);
2640     		}
2641     
2642     		if(CURRENT_SC->SCp.this_residual>0) {
2643     			while(fifodata>0 && CURRENT_SC->SCp.this_residual>0) {
2644                             	data_count = fifodata>CURRENT_SC->SCp.this_residual ?
2645     						CURRENT_SC->SCp.this_residual :
2646     						fifodata;
2647     				fifodata -= data_count;
2648     
2649                             	if(data_count & 1) {
2650     					DPRINTK(debug_datai, DEBUG_LEAD "8bit\n", CMDINFO(CURRENT_SC));
2651                                     	SETPORT(DMACNTRL0, ENDMA|_8BIT);
2652                                     	*CURRENT_SC->SCp.ptr++ = GETPORT(DATAPORT);
2653                                     	CURRENT_SC->SCp.this_residual--;
2654                                     	DATA_LEN++;
2655                                     	SETPORT(DMACNTRL0, ENDMA);
2656                             	}
2657     	
2658                             	if(data_count > 1) {
2659     					DPRINTK(debug_datai, DEBUG_LEAD "16bit(%d)\n", CMDINFO(CURRENT_SC), data_count);
2660                                     	data_count >>= 1;
2661                                     	insw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2662                                     	CURRENT_SC->SCp.ptr           += 2 * data_count;
2663                                     	CURRENT_SC->SCp.this_residual -= 2 * data_count;
2664                                     	DATA_LEN                      += 2 * data_count;
2665                             	}
2666     	
2667                             	if(CURRENT_SC->SCp.this_residual==0 && CURRENT_SC->SCp.buffers_residual>0) {
2668                                    		/* advance to next buffer */
2669                                    		CURRENT_SC->SCp.buffers_residual--;
2670                                    		CURRENT_SC->SCp.buffer++;
2671                                    		CURRENT_SC->SCp.ptr           = CURRENT_SC->SCp.buffer->address;
2672                                    		CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2673     				} 
2674                     	}
2675     		} else if(fifodata>0) { 
2676     			printk(ERR_LEAD "no buffers left for %d(%d) bytes (data overrun!?)\n", CMDINFO(CURRENT_SC), fifodata, GETPORT(FIFOSTAT));
2677                             SETPORT(DMACNTRL0, ENDMA|_8BIT);
2678     			while(fifodata>0) {
2679     				int data;
2680     				data=GETPORT(DATAPORT);
2681     				DPRINTK(debug_datai, DEBUG_LEAD "data=%02x\n", CMDINFO(CURRENT_SC), data);
2682     				fifodata--;
2683     				DATA_LEN++;
2684     			}
2685                             SETPORT(DMACNTRL0, ENDMA|_8BIT);
2686     		}
2687     	}
2688     
2689     	if(TESTLO(DMASTAT, INTSTAT) ||
2690     	   TESTLO(DMASTAT, DFIFOEMP) ||
2691     	   TESTLO(SSTAT2, SEMPTY) ||
2692     	   GETPORT(FIFOSTAT)>0) {
2693     	   	/*
2694     		 * something went wrong, if there's something left in the fifos
2695     		 * or the phase didn't change
2696     		 */
2697     		printk(ERR_LEAD "fifos should be empty and phase should have changed\n", CMDINFO(CURRENT_SC));
2698     		disp_ports(shpnt);
2699     	}
2700     
2701     	if(DATA_LEN!=GETSTCNT()) {
2702     		printk(ERR_LEAD
2703     		       "manual transfer count differs from automatic (count=%d;stcnt=%d;diff=%d;fifostat=%d)",
2704     		       CMDINFO(CURRENT_SC), DATA_LEN, GETSTCNT(), GETSTCNT()-DATA_LEN, GETPORT(FIFOSTAT));
2705     		disp_ports(shpnt);
2706     		mdelay(10000);
2707     	}
2708     }
2709     
2710     static void datai_end(struct Scsi_Host *shpnt)
2711     {
2712     	CURRENT_SC->resid -= GETSTCNT();
2713     
2714     	DPRINTK(debug_datai,
2715     		DEBUG_LEAD "datai_end: request_bufflen=%d resid=%d stcnt=%d\n",
2716     		CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid, GETSTCNT());
2717     
2718     	SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2719     	SETPORT(DMACNTRL0, 0);
2720     }
2721     
2722     /*
2723      * data out phase
2724      *
2725      */
2726     static void datao_init(struct Scsi_Host *shpnt)
2727     {
2728     	SETPORT(DMACNTRL0, WRITE_READ | RSTFIFO);
2729     	SETPORT(DMACNTRL0, WRITE_READ | ENDMA);
2730     
2731     	SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2732     	SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2733     
2734     	SETPORT(SIMODE0, 0);
2735     	SETPORT(SIMODE1, ENSCSIPERR | ENSCSIRST | ENPHASEMIS | ENBUSFREE );
2736     
2737     	DATA_LEN = CURRENT_SC->resid;
2738     
2739     	DPRINTK(debug_datao,
2740     		DEBUG_LEAD "datao_init: request_bufflen=%d; resid=%d\n",
2741     		CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid);
2742     }
2743     
2744     static void datao_run(struct Scsi_Host *shpnt)
2745     {
2746     	unsigned int the_time;
2747     	int data_count;
2748     
2749     	/* until phase changes or all data sent */
2750     	while(TESTLO(DMASTAT, INTSTAT) && CURRENT_SC->SCp.this_residual>0) {
2751     		data_count = 128;
2752     		if(data_count > CURRENT_SC->SCp.this_residual)
2753     			data_count=CURRENT_SC->SCp.this_residual;
2754     
2755     		if(TESTLO(DMASTAT, DFIFOEMP)) {
2756     			printk(ERR_LEAD "datao fifo not empty (%d)", CMDINFO(CURRENT_SC), GETPORT(FIFOSTAT));
2757     			disp_ports(shpnt);
2758     			break;
2759     		}
2760     
2761     		if(data_count & 1) {
2762     			SETPORT(DMACNTRL0,WRITE_READ|ENDMA|_8BIT);
2763     			SETPORT(DATAPORT, *CURRENT_SC->SCp.ptr++);
2764     			CURRENT_SC->SCp.this_residual--;
2765     			CURRENT_SC->resid--;
2766     			SETPORT(DMACNTRL0,WRITE_READ|ENDMA);
2767     		}
2768     
2769     		if(data_count > 1) {
2770     			data_count >>= 1;
2771     			outsw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2772     			CURRENT_SC->SCp.ptr           += 2 * data_count;
2773     			CURRENT_SC->SCp.this_residual -= 2 * data_count;
2774     			CURRENT_SC->resid             -= 2 * data_count;
2775     	  	}
2776     
2777     		if(CURRENT_SC->SCp.this_residual==0 && CURRENT_SC->SCp.buffers_residual>0) {
2778     			/* advance to next buffer */
2779     			CURRENT_SC->SCp.buffers_residual--;
2780     			CURRENT_SC->SCp.buffer++;
2781     			CURRENT_SC->SCp.ptr           = CURRENT_SC->SCp.buffer->address;
2782     			CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2783     		}
2784     
2785     		the_time=jiffies + 10*HZ;
2786     		while(TESTLO(DMASTAT, DFIFOEMP|INTSTAT) && time_before(jiffies,the_time))
2787     			barrier();
2788     
2789     		if(TESTLO(DMASTAT, DFIFOEMP|INTSTAT)) {
2790     			printk(ERR_LEAD "dataout timeout", CMDINFO(CURRENT_SC));
2791     			disp_ports(shpnt);
2792     			break;
2793     		}
2794     	}
2795     }
2796     
2797     static void datao_end(struct Scsi_Host *shpnt)
2798     {
2799     	if(TESTLO(DMASTAT, DFIFOEMP)) {
2800     		int data_count = (DATA_LEN - CURRENT_SC->resid) - GETSTCNT();
2801     
2802     		DPRINTK(debug_datao, DEBUG_LEAD "datao: %d bytes to resend (%d written, %d transferred)\n",
2803     			CMDINFO(CURRENT_SC),
2804     			data_count,
2805     			DATA_LEN-CURRENT_SC->resid,
2806     			GETSTCNT());
2807     
2808     		CURRENT_SC->resid += data_count;
2809     
2810     		if(CURRENT_SC->use_sg) {
2811     			data_count -= CURRENT_SC->SCp.ptr - CURRENT_SC->SCp.buffer->address;
2812     			while(data_count>0) {
2813     				CURRENT_SC->SCp.buffer--;
2814     				CURRENT_SC->SCp.buffers_residual++;
2815     				data_count -= CURRENT_SC->SCp.buffer->length;
2816     			}
2817     			CURRENT_SC->SCp.ptr           = CURRENT_SC->SCp.buffer->address - data_count;
2818     			CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length + data_count;
2819     		} else {
2820     			CURRENT_SC->SCp.ptr           -= data_count;
2821     			CURRENT_SC->SCp.this_residual += data_count;
2822     		}
2823     	}
2824     
2825     	DPRINTK(debug_datao, DEBUG_LEAD "datao_end: request_bufflen=%d; resid=%d; stcnt=%d\n",
2826     		CMDINFO(CURRENT_SC),
2827     		CURRENT_SC->request_bufflen,
2828     		CURRENT_SC->resid,
2829     		GETSTCNT());
2830     
2831     	SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
2832     	SETPORT(SXFRCTL0, CH1);
2833     
2834     	SETPORT(DMACNTRL0, 0);
2835     }
2836     
2837     /*
2838      * figure out what state we're in
2839      *
2840      */
2841     static int update_state(struct Scsi_Host *shpnt)
2842     {
2843     	int dataphase=0;
2844     	unsigned int stat0 = GETPORT(SSTAT0);
2845     	unsigned int stat1 = GETPORT(SSTAT1);
2846     
2847     	PREVSTATE = STATE;
2848     	STATE=unknown;
2849     
2850     	if(stat1 & SCSIRSTI) {
2851     		STATE=rsti;
2852     		SETPORT(SCSISEQ,0);
2853     		SETPORT(SSTAT1,SCSIRSTI);
2854       	} else if(stat0 & SELDI && PREVSTATE==busfree) {
2855     		STATE=seldi;
2856     	} else if(stat0 & SELDO && CURRENT_SC && (CURRENT_SC->SCp.phase & selecting)) {
2857     		STATE=seldo;
2858     	} else if(stat1 & SELTO) {
2859     		STATE=selto;
2860     	} else if(stat1 & BUSFREE) {
2861     		STATE=busfree;
2862     		SETPORT(SSTAT1,BUSFREE);
2863     	} else if(stat1 & SCSIPERR) {
2864     		STATE=parerr;
2865     		SETPORT(SSTAT1,SCSIPERR);
2866     	} else if(stat1 & REQINIT) {
2867     		switch(GETPORT(SCSISIG) & P_MASK) {
2868     		case P_MSGI:	STATE=msgi;	break;
2869     		case P_MSGO:	STATE=msgo;	break;
2870     		case P_DATAO:	STATE=datao;	break;
2871     		case P_DATAI:	STATE=datai;	break;
2872     		case P_STATUS:	STATE=status;	break;
2873     		case P_CMD:	STATE=cmd;	break;
2874     		}
2875     		dataphase=1;
2876     	}
2877     
2878     	if((stat0 & SELDI) && STATE!=seldi && !dataphase) {
2879     		printk(INFO_LEAD "reselection missed?", CMDINFO(CURRENT_SC));
2880     		disp_ports(shpnt);
2881     	}
2882     
2883     	if(STATE!=PREVSTATE) {
2884     		LASTSTATE=PREVSTATE;
2885     	}
2886     
2887     	return dataphase;
2888     }
2889     
2890     /*
2891      * handle parity error
2892      *
2893      * FIXME: in which phase?
2894      *
2895      */
2896     static void parerr_run(struct Scsi_Host *shpnt)
2897     {
2898     	printk(ERR_LEAD "parity error\n", CMDINFO(CURRENT_SC));
2899     	done(shpnt, DID_PARITY << 16);
2900     }
2901     
2902     /*
2903      * handle reset in
2904      *
2905      */
2906     static void rsti_run(struct Scsi_Host *shpnt)
2907     {
2908     	Scsi_Cmnd *ptr;
2909     
2910     	printk(KERN_NOTICE "aha152x%d: scsi reset in\n", HOSTNO);
2911     	
2912     	ptr=DISCONNECTED_SC;
2913     	while(ptr) {
2914     		Scsi_Cmnd *next = SCNEXT(ptr);
2915     
2916     		if (!ptr->device->soft_reset) {
2917     			remove_SC(&DISCONNECTED_SC, ptr);
2918     
2919     			ptr->result =  DID_RESET << 16;
2920     			ptr->scsi_done(ptr);
2921     
2922     			kfree(ptr->host_scribble);
2923     			ptr->host_scribble=0;
2924     		}
2925     
2926     		ptr = next;
2927     	}
2928     
2929     	if(CURRENT_SC && !CURRENT_SC->device->soft_reset)
2930     		done(shpnt, DID_RESET << 16 );
2931     }
2932     
2933     
2934     /*
2935      * bottom-half handler
2936      *
2937      */
2938     static void is_complete(struct Scsi_Host *shpnt)
2939     {
2940     	int dataphase;
2941     	unsigned long flags;
2942     	int pending;
2943     
2944     	DO_LOCK(flags);
2945     	if(HOSTDATA(shpnt)->in_intr!=0)
2946     		aha152x_error(shpnt, "bottom-half already running!?");
2947     	HOSTDATA(shpnt)->in_intr++;
2948     	DO_UNLOCK(flags);
2949     
2950     	/*
2951     	 * loop while there are interrupt conditions pending
2952     	 *
2953     	 */
2954     	do {
2955     		unsigned long start = jiffies;
2956     		dataphase=update_state(shpnt);
2957     
2958     		DPRINTK(debug_phases, LEAD "start %s %s(%s)\n", CMDINFO(CURRENT_SC), states[STATE].name, states[PREVSTATE].name, states[LASTSTATE].name);
2959     
2960     		/*
2961     		 * end previous state
2962     		 *
2963     		 */
2964     		if(PREVSTATE!=STATE && states[PREVSTATE].end)
2965     			states[PREVSTATE].end(shpnt);
2966     
2967     		/*
2968     		 * disable SPIO mode if previous phase used it
2969     		 * and this one doesn't
2970     		 *
2971     		 */
2972     		if(states[PREVSTATE].spio && !states[STATE].spio) {
2973     			SETPORT(SXFRCTL0, CH1);
2974     			SETPORT(DMACNTRL0, 0);
2975     			if(CURRENT_SC)
2976     				CURRENT_SC->SCp.phase &= ~spiordy;
2977     		}
2978     
2979     		/*
2980     		 * accept current dataphase phase
2981     		 *
2982     		 */
2983     		if(dataphase) {
2984     			SETPORT(SSTAT0, REQINIT);
2985     			SETPORT(SCSISIG, GETPORT(SCSISIG) & P_MASK);
2986     			SETPORT(SSTAT1, PHASECHG);  
2987     		}
2988     		
2989     		/*
2990     		 * enable SPIO mode if previous didn't use it
2991     		 * and this one does
2992     		 *
2993     		 */
2994     		if(!states[PREVSTATE].spio && states[STATE].spio) {
2995     			SETPORT(DMACNTRL0, 0);
2996     			SETPORT(SXFRCTL0, CH1|SPIOEN);
2997     			if(CURRENT_SC)
2998     				CURRENT_SC->SCp.phase |= spiordy;
2999     		}
3000     		
3001     		/*
3002     		 * initialize for new state
3003     		 *
3004     		 */
3005     		if(PREVSTATE!=STATE && states[STATE].init)
3006     			states[STATE].init(shpnt);
3007     		
3008     		/*
3009     		 * handle current state
3010     		 *
3011     		 */
3012     		if(states[STATE].run)
3013     			states[STATE].run(shpnt);
3014     		else
3015     			printk(ERR_LEAD "unexpected state (%x)\n", CMDINFO(CURRENT_SC), STATE);
3016     		
3017     		/*
3018     		 * setup controller to interrupt on
3019     		 * the next expected condition and
3020     		 * loop if it's already there
3021     		 *
3022     		 */
3023     		DO_LOCK(flags);
3024     		pending=setup_expected_interrupts(shpnt);
3025     #if defined(AHA152X_STAT)
3026     		HOSTDATA(shpnt)->count[STATE]++;
3027     		if(PREVSTATE!=STATE)
3028     			HOSTDATA(shpnt)->count_trans[STATE]++;
3029     		HOSTDATA(shpnt)->time[STATE] += jiffies-start;
3030     #endif
3031     		DO_UNLOCK(flags);
3032     
3033     		DPRINTK(debug_phases, LEAD "end %s %s(%s)\n", CMDINFO(CURRENT_SC), states[STATE].name, states[PREVSTATE].name, states[LASTSTATE].name);
3034     	} while(pending);
3035     
3036     	/*
3037     	 * enable interrupts and leave bottom-half
3038     	 *
3039     	 */
3040     	DO_LOCK(flags);
3041     	HOSTDATA(shpnt)->in_intr--;
3042     	SETBITS(DMACNTRL0, INTEN);
3043     	DO_UNLOCK(flags);
3044     }
3045     
3046     
3047     /* 
3048      * Dump the current driver status and panic
3049      */
3050     static void aha152x_error(struct Scsi_Host *shpnt, char *msg)
3051     {
3052     	printk(KERN_EMERG "\naha152x%d: %s\n", HOSTNO, msg);
3053     	show_queues(shpnt);
3054     	panic("aha152x panic\n");
3055     }
3056     
3057     /*
3058      * Display registers of AIC-6260
3059      */
3060     static void disp_ports(struct Scsi_Host *shpnt)
3061     {
3062     #if defined(AHA152X_DEBUG)
3063     	int s;
3064     
3065     	printk("\n%s: %s(%s) ",
3066     		CURRENT_SC ? "busy" : "waiting",
3067     		states[STATE].name,
3068     		states[PREVSTATE].name);
3069     
3070     	s = GETPORT(SCSISEQ);
3071     	printk("SCSISEQ( ");
3072     	if (s & TEMODEO)
3073     		printk("TARGET MODE ");
3074     	if (s & ENSELO)
3075     		printk("SELO ");
3076     	if (s & ENSELI)
3077     		printk("SELI ");
3078     	if (s & ENRESELI)
3079     		printk("RESELI ");
3080     	if (s & ENAUTOATNO)
3081     		printk("AUTOATNO ");
3082     	if (s & ENAUTOATNI)
3083     		printk("AUTOATNI ");
3084     	if (s & ENAUTOATNP)
3085     		printk("AUTOATNP ");
3086     	if (s & SCSIRSTO)
3087     		printk("SCSIRSTO ");
3088     	printk(");");
3089     
3090     	printk(" SCSISIG(");
3091     	s = GETPORT(SCSISIG);
3092     	switch (s & P_MASK) {
3093     	case P_DATAO:
3094     		printk("DATA OUT");
3095     		break;
3096     	case P_DATAI:
3097     		printk("DATA IN");
3098     		break;
3099     	case P_CMD:
3100     		printk("COMMAND");
3101     		break;
3102     	case P_STATUS:
3103     		printk("STATUS");
3104     		break;
3105     	case P_MSGO:
3106     		printk("MESSAGE OUT");
3107     		break;
3108     	case P_MSGI:
3109     		printk("MESSAGE IN");
3110     		break;
3111     	default:
3112     		printk("*illegal*");
3113     		break;
3114     	}
3115     
3116     	printk("); ");
3117     
3118     	printk("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3119     
3120     	printk("SSTAT( ");
3121     	s = GETPORT(SSTAT0);
3122     	if (s & TARGET)
3123     		printk("TARGET ");
3124     	if (s & SELDO)
3125     		printk("SELDO ");
3126     	if (s & SELDI)
3127     		printk("SELDI ");
3128     	if (s & SELINGO)
3129     		printk("SELINGO ");
3130     	if (s & SWRAP)
3131     		printk("SWRAP ");
3132     	if (s & SDONE)
3133     		printk("SDONE ");
3134     	if (s & SPIORDY)
3135     		printk("SPIORDY ");
3136     	if (s & DMADONE)
3137     		printk("DMADONE ");
3138     
3139     	s = GETPORT(SSTAT1);
3140     	if (s & SELTO)
3141     		printk("SELTO ");
3142     	if (s & ATNTARG)
3143     		printk("ATNTARG ");
3144     	if (s & SCSIRSTI)
3145     		printk("SCSIRSTI ");
3146     	if (s & PHASEMIS)
3147     		printk("PHASEMIS ");
3148     	if (s & BUSFREE)
3149     		printk("BUSFREE ");
3150     	if (s & SCSIPERR)
3151     		printk("SCSIPERR ");
3152     	if (s & PHASECHG)
3153     		printk("PHASECHG ");
3154     	if (s & REQINIT)
3155     		printk("REQINIT ");
3156     	printk("); ");
3157     
3158     
3159     	printk("SSTAT( ");
3160     
3161     	s = GETPORT(SSTAT0) & GETPORT(SIMODE0);
3162     
3163     	if (s & TARGET)
3164     		printk("TARGET ");
3165     	if (s & SELDO)
3166     		printk("SELDO ");
3167     	if (s & SELDI)
3168     		printk("SELDI ");
3169     	if (s & SELINGO)
3170     		printk("SELINGO ");
3171     	if (s & SWRAP)
3172     		printk("SWRAP ");
3173     	if (s & SDONE)
3174     		printk("SDONE ");
3175     	if (s & SPIORDY)
3176     		printk("SPIORDY ");
3177     	if (s & DMADONE)
3178     		printk("DMADONE ");
3179     
3180     	s = GETPORT(SSTAT1) & GETPORT(SIMODE1);
3181     
3182     	if (s & SELTO)
3183     		printk("SELTO ");
3184     	if (s & ATNTARG)
3185     		printk("ATNTARG ");
3186     	if (s & SCSIRSTI)
3187     		printk("SCSIRSTI ");
3188     	if (s & PHASEMIS)
3189     		printk("PHASEMIS ");
3190     	if (s & BUSFREE)
3191     		printk("BUSFREE ");
3192     	if (s & SCSIPERR)
3193     		printk("SCSIPERR ");
3194     	if (s & PHASECHG)
3195     		printk("PHASECHG ");
3196     	if (s & REQINIT)
3197     		printk("REQINIT ");
3198     	printk("); ");
3199     
3200     	printk("SXFRCTL0( ");
3201     
3202     	s = GETPORT(SXFRCTL0);
3203     	if (s & SCSIEN)
3204     		printk("SCSIEN ");
3205     	if (s & DMAEN)
3206     		printk("DMAEN ");
3207     	if (s & CH1)
3208     		printk("CH1 ");
3209     	if (s & CLRSTCNT)
3210     		printk("CLRSTCNT ");
3211     	if (s & SPIOEN)
3212     		printk("SPIOEN ");
3213     	if (s & CLRCH1)
3214     		printk("CLRCH1 ");
3215     	printk("); ");
3216     
3217     	printk("SIGNAL( ");
3218     
3219     	s = GETPORT(SCSISIG);
3220     	if (s & SIG_ATNI)
3221     		printk("ATNI ");
3222     	if (s & SIG_SELI)
3223     		printk("SELI ");
3224     	if (s & SIG_BSYI)
3225     		printk("BSYI ");
3226     	if (s & SIG_REQI)
3227     		printk("REQI ");
3228     	if (s & SIG_ACKI)
3229     		printk("ACKI ");
3230     	printk("); ");
3231     
3232     	printk("SELID (%02x), ", GETPORT(SELID));
3233     
3234     	printk("STCNT (%d), ", GETSTCNT());
3235     	
3236     	printk("SSTAT2( ");
3237     
3238     	s = GETPORT(SSTAT2);
3239     	if (s & SOFFSET)
3240     		printk("SOFFSET ");
3241     	if (s & SEMPTY)
3242     		printk("SEMPTY ");
3243     	if (s & SFULL)
3244     		printk("SFULL ");
3245     	printk("); SFCNT (%d); ", s & (SFULL | SFCNT));
3246     
3247     	s = GETPORT(SSTAT3);
3248     	printk("SCSICNT (%d), OFFCNT(%d), ", (s & 0xf0) >> 4, s & 0x0f);
3249     
3250     	printk("SSTAT4( ");
3251     	s = GETPORT(SSTAT4);
3252     	if (s & SYNCERR)
3253     		printk("SYNCERR ");
3254     	if (s & FWERR)
3255     		printk("FWERR ");
3256     	if (s & FRERR)
3257     		printk("FRERR ");
3258     	printk("); ");
3259     
3260     	printk("DMACNTRL0( ");
3261     	s = GETPORT(DMACNTRL0);
3262     	printk("%s ", s & _8BIT ? "8BIT" : "16BIT");
3263     	printk("%s ", s & DMA ? "DMA" : "PIO");
3264     	printk("%s ", s & WRITE_READ ? "WRITE" : "READ");
3265     	if (s & ENDMA)
3266     		printk("ENDMA ");
3267     	if (s & INTEN)
3268     		printk("INTEN ");
3269     	if (s & RSTFIFO)
3270     		printk("RSTFIFO ");
3271     	if (s & SWINT)
3272     		printk("SWINT ");
3273     	printk("); ");
3274     
3275     	printk("DMASTAT( ");
3276     	s = GETPORT(DMASTAT);
3277     	if (s & ATDONE)
3278     		printk("ATDONE ");
3279     	if (s & WORDRDY)
3280     		printk("WORDRDY ");
3281     	if (s & DFIFOFULL)
3282     		printk("DFIFOFULL ");
3283     	if (s & DFIFOEMP)
3284     		printk("DFIFOEMP ");
3285     	printk(")\n");
3286     #endif
3287     }
3288     
3289     /*
3290      * display enabled interrupts
3291      */
3292     static void disp_enintr(struct Scsi_Host *shpnt)
3293     {
3294     	int s;
3295     
3296     	printk(KERN_DEBUG "enabled interrupts ( ");
3297     
3298     	s = GETPORT(SIMODE0);
3299     	if (s & ENSELDO)
3300     		printk("ENSELDO ");
3301     	if (s & ENSELDI)
3302     		printk("ENSELDI ");
3303     	if (s & ENSELINGO)
3304     		printk("ENSELINGO ");
3305     	if (s & ENSWRAP)
3306     		printk("ENSWRAP ");
3307     	if (s & ENSDONE)
3308     		printk("ENSDONE ");
3309     	if (s & ENSPIORDY)
3310     		printk("ENSPIORDY ");
3311     	if (s & ENDMADONE)
3312     		printk("ENDMADONE ");
3313     
3314     	s = GETPORT(SIMODE1);
3315     	if (s & ENSELTIMO)
3316     		printk("ENSELTIMO ");
3317     	if (s & ENATNTARG)
3318     		printk("ENATNTARG ");
3319     	if (s & ENPHASEMIS)
3320     		printk("ENPHASEMIS ");
3321     	if (s & ENBUSFREE)
3322     		printk("ENBUSFREE ");
3323     	if (s & ENSCSIPERR)
3324     		printk("ENSCSIPERR ");
3325     	if (s & ENPHASECHG)
3326     		printk("ENPHASECHG ");
3327     	if (s & ENREQINIT)
3328     		printk("ENREQINIT ");
3329     	printk(")\n");
3330     }
3331     
3332     /*
3333      * Show the command data of a command
3334      */
3335     static void show_command(Scsi_Cmnd *ptr)
3336     {
3337     	printk(KERN_DEBUG "0x%08x: target=%d; lun=%d; cmnd=(",
3338     	       (unsigned int) ptr, ptr->target, ptr->lun);
3339     
3340     	print_command(ptr->cmnd);
3341     
3342     	printk(KERN_DEBUG "); request_bufflen=%d; resid=%d; phase |",
3343     	       ptr->request_bufflen, ptr->resid);
3344     
3345     	if (ptr->SCp.phase & not_issued)
3346     		printk("not issued|");
3347     	if (ptr->SCp.phase & selecting)
3348     		printk("selecting|");
3349     	if (ptr->SCp.phase & identified)
3350     		printk("identified|");
3351     	if (ptr->SCp.phase & disconnected)
3352     		printk("disconnected|");
3353     	if (ptr->SCp.phase & completed)
3354     		printk("completed|");
3355     	if (ptr->SCp.phase & spiordy)
3356     		printk("spiordy|");
3357     	if (ptr->SCp.phase & syncneg)
3358     		printk("syncneg|");
3359     	if (ptr->SCp.phase & aborted)
3360     		printk("aborted|");
3361     	if (ptr->SCp.phase & resetted)
3362     		printk("resetted|");
3363     	printk("; next=0x%p\n", SCNEXT(ptr));
3364     }
3365     
3366     /*
3367      * Dump the queued data
3368      */
3369     static void show_queues(struct Scsi_Host *shpnt)
3370     {
3371     	Scsi_Cmnd *ptr;
3372     	unsigned long flags;
3373     
3374     	DO_LOCK(flags);
3375     	printk(KERN_DEBUG "\nqueue status:\nissue_SC:\n");
3376     	for (ptr = ISSUE_SC; ptr; ptr = SCNEXT(ptr))
3377     		show_command(ptr);
3378     	DO_UNLOCK(flags);
3379     
3380     	printk(KERN_DEBUG "current_SC:\n");
3381     	if (CURRENT_SC)
3382     		show_command(CURRENT_SC);
3383     	else
3384     		printk(KERN_DEBUG "none\n");
3385     
3386     	printk(KERN_DEBUG "disconnected_SC:\n");
3387     	for (ptr = DISCONNECTED_SC; ptr; ptr = SCNEXT(ptr))
3388     		show_command(ptr);
3389     
3390     	disp_ports(shpnt);
3391     	disp_enintr(shpnt);
3392     }
3393     
3394     #undef SPRINTF
3395     #define SPRINTF(args...) pos += sprintf(pos, ## args)
3396     
3397     static int get_command(char *pos, Scsi_Cmnd * ptr)
3398     {
3399     	char *start = pos;
3400     	int i;
3401     
3402     	SPRINTF("0x%08x: target=%d; lun=%d; cmnd=( ",
3403     		(unsigned int) ptr, ptr->target, ptr->lun);
3404     
3405     	for (i = 0; i < COMMAND_SIZE(ptr->cmnd[0]); i++)
3406     		SPRINTF("0x%02x ", ptr->cmnd[i]);
3407     
3408     	SPRINTF("); resid=%d; residual=%d; buffers=%d; phase |",
3409     		ptr->resid, ptr->SCp.this_residual, ptr->SCp.buffers_residual);
3410     
3411     	if (ptr->SCp.phase & not_issued)
3412     		SPRINTF("not issued|");
3413     	if (ptr->SCp.phase & selecting)
3414     		SPRINTF("selecting|");
3415     	if (ptr->SCp.phase & disconnected)
3416     		SPRINTF("disconnected|");
3417     	if (ptr->SCp.phase & aborted)
3418     		SPRINTF("aborted|");
3419     	if (ptr->SCp.phase & identified)
3420     		SPRINTF("identified|");
3421     	if (ptr->SCp.phase & completed)
3422     		SPRINTF("completed|");
3423     	if (ptr->SCp.phase & spiordy)
3424     		SPRINTF("spiordy|");
3425     	if (ptr->SCp.phase & syncneg)
3426     		SPRINTF("syncneg|");
3427     	SPRINTF("; next=0x%p\n", SCNEXT(ptr));
3428     
3429     	return (pos - start);
3430     }
3431     
3432     static int get_ports(struct Scsi_Host *shpnt, char *pos)
3433     {
3434     	char *start = pos;
3435     	int s;
3436     
3437     	SPRINTF("\n%s: %s(%s) ", CURRENT_SC ? "on bus" : "waiting", states[STATE].name, states[PREVSTATE].name);
3438     
3439     	s = GETPORT(SCSISEQ);
3440     	SPRINTF("SCSISEQ( ");
3441     	if (s & TEMODEO)
3442     		SPRINTF("TARGET MODE ");
3443     	if (s & ENSELO)
3444     		SPRINTF("SELO ");
3445     	if (s & ENSELI)
3446     		SPRINTF("SELI ");
3447     	if (s & ENRESELI)
3448     		SPRINTF("RESELI ");
3449     	if (s & ENAUTOATNO)
3450     		SPRINTF("AUTOATNO ");
3451     	if (s & ENAUTOATNI)
3452     		SPRINTF("AUTOATNI ");
3453     	if (s & ENAUTOATNP)
3454     		SPRINTF("AUTOATNP ");
3455     	if (s & SCSIRSTO)
3456     		SPRINTF("SCSIRSTO ");
3457     	SPRINTF(");");
3458     
3459     	SPRINTF(" SCSISIG(");
3460     	s = GETPORT(SCSISIG);
3461     	switch (s & P_MASK) {
3462     	case P_DATAO:
3463     		SPRINTF("DATA OUT");
3464     		break;
3465     	case P_DATAI:
3466     		SPRINTF("DATA IN");
3467     		break;
3468     	case P_CMD:
3469     		SPRINTF("COMMAND");
3470     		break;
3471     	case P_STATUS:
3472     		SPRINTF("STATUS");
3473     		break;
3474     	case P_MSGO:
3475     		SPRINTF("MESSAGE OUT");
3476     		break;
3477     	case P_MSGI:
3478     		SPRINTF("MESSAGE IN");
3479     		break;
3480     	default:
3481     		SPRINTF("*illegal*");
3482     		break;
3483     	}
3484     
3485     	SPRINTF("); ");
3486     
3487     	SPRINTF("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3488     
3489     	SPRINTF("SSTAT( ");
3490     	s = GETPORT(SSTAT0);
3491     	if (s & TARGET)
3492     		SPRINTF("TARGET ");
3493     	if (s & SELDO)
3494     		SPRINTF("SELDO ");
3495     	if (s & SELDI)
3496     		SPRINTF("SELDI ");
3497     	if (s & SELINGO)
3498     		SPRINTF("SELINGO ");
3499     	if (s & SWRAP)
3500     		SPRINTF("SWRAP ");
3501     	if (s & SDONE)
3502     		SPRINTF("SDONE ");
3503     	if (s & SPIORDY)
3504     		SPRINTF("SPIORDY ");
3505     	if (s & DMADONE)
3506     		SPRINTF("DMADONE ");
3507     
3508     	s = GETPORT(SSTAT1);
3509     	if (s & SELTO)
3510     		SPRINTF("SELTO ");
3511     	if (s & ATNTARG)
3512     		SPRINTF("ATNTARG ");
3513     	if (s & SCSIRSTI)
3514     		SPRINTF("SCSIRSTI ");
3515     	if (s & PHASEMIS)
3516     		SPRINTF("PHASEMIS ");
3517     	if (s & BUSFREE)
3518     		SPRINTF("BUSFREE ");
3519     	if (s & SCSIPERR)
3520     		SPRINTF("SCSIPERR ");
3521     	if (s & PHASECHG)
3522     		SPRINTF("PHASECHG ");
3523     	if (s & REQINIT)
3524     		SPRINTF("REQINIT ");
3525     	SPRINTF("); ");
3526     
3527     
3528     	SPRINTF("SSTAT( ");
3529     
3530     	s = GETPORT(SSTAT0) & GETPORT(SIMODE0);
3531     
3532     	if (s & TARGET)
3533     		SPRINTF("TARGET ");
3534     	if (s & SELDO)
3535     		SPRINTF("SELDO ");
3536     	if (s & SELDI)
3537     		SPRINTF("SELDI ");
3538     	if (s & SELINGO)
3539     		SPRINTF("SELINGO ");
3540     	if (s & SWRAP)
3541     		SPRINTF("SWRAP ");
3542     	if (s & SDONE)
3543     		SPRINTF("SDONE ");
3544     	if (s & SPIORDY)
3545     		SPRINTF("SPIORDY ");
3546     	if (s & DMADONE)
3547     		SPRINTF("DMADONE ");
3548     
3549     	s = GETPORT(SSTAT1) & GETPORT(SIMODE1);
3550     
3551     	if (s & SELTO)
3552     		SPRINTF("SELTO ");
3553     	if (s & ATNTARG)
3554     		SPRINTF("ATNTARG ");
3555     	if (s & SCSIRSTI)
3556     		SPRINTF("SCSIRSTI ");
3557     	if (s & PHASEMIS)
3558     		SPRINTF("PHASEMIS ");
3559     	if (s & BUSFREE)
3560     		SPRINTF("BUSFREE ");
3561     	if (s & SCSIPERR)
3562     		SPRINTF("SCSIPERR ");
3563     	if (s & PHASECHG)
3564     		SPRINTF("PHASECHG ");
3565     	if (s & REQINIT)
3566     		SPRINTF("REQINIT ");
3567     	SPRINTF("); ");
3568     
3569     	SPRINTF("SXFRCTL0( ");
3570     
3571     	s = GETPORT(SXFRCTL0);
3572     	if (s & SCSIEN)
3573     		SPRINTF("SCSIEN ");
3574     	if (s & DMAEN)
3575     		SPRINTF("DMAEN ");
3576     	if (s & CH1)
3577     		SPRINTF("CH1 ");
3578     	if (s & CLRSTCNT)
3579     		SPRINTF("CLRSTCNT ");
3580     	if (s & SPIOEN)
3581     		SPRINTF("SPIOEN ");
3582     	if (s & CLRCH1)
3583     		SPRINTF("CLRCH1 ");
3584     	SPRINTF("); ");
3585     
3586     	SPRINTF("SIGNAL( ");
3587     
3588     	s = GETPORT(SCSISIG);
3589     	if (s & SIG_ATNI)
3590     		SPRINTF("ATNI ");
3591     	if (s & SIG_SELI)
3592     		SPRINTF("SELI ");
3593     	if (s & SIG_BSYI)
3594     		SPRINTF("BSYI ");
3595     	if (s & SIG_REQI)
3596     		SPRINTF("REQI ");
3597     	if (s & SIG_ACKI)
3598     		SPRINTF("ACKI ");
3599     	SPRINTF("); ");
3600     
3601     	SPRINTF("SELID(%02x), ", GETPORT(SELID));
3602     
3603     	SPRINTF("STCNT(%d), ", GETSTCNT());
3604     
3605     	SPRINTF("SSTAT2( ");
3606     
3607     	s = GETPORT(SSTAT2);
3608     	if (s & SOFFSET)
3609     		SPRINTF("SOFFSET ");
3610     	if (s & SEMPTY)
3611     		SPRINTF("SEMPTY ");
3612     	if (s & SFULL)
3613     		SPRINTF("SFULL ");
3614     	SPRINTF("); SFCNT (%d); ", s & (SFULL | SFCNT));
3615     
3616     	s = GETPORT(SSTAT3);
3617     	SPRINTF("SCSICNT (%d), OFFCNT(%d), ", (s & 0xf0) >> 4, s & 0x0f);
3618     
3619     	SPRINTF("SSTAT4( ");
3620     	s = GETPORT(SSTAT4);
3621     	if (s & SYNCERR)
3622     		SPRINTF("SYNCERR ");
3623     	if (s & FWERR)
3624     		SPRINTF("FWERR ");
3625     	if (s & FRERR)
3626     		SPRINTF("FRERR ");
3627     	SPRINTF("); ");
3628     
3629     	SPRINTF("DMACNTRL0( ");
3630     	s = GETPORT(DMACNTRL0);
3631     	SPRINTF("%s ", s & _8BIT ? "8BIT" : "16BIT");
3632     	SPRINTF("%s ", s & DMA ? "DMA" : "PIO");
3633     	SPRINTF("%s ", s & WRITE_READ ? "WRITE" : "READ");
3634     	if (s & ENDMA)
3635     		SPRINTF("ENDMA ");
3636     	if (s & INTEN)
3637     		SPRINTF("INTEN ");
3638     	if (s & RSTFIFO)
3639     		SPRINTF("RSTFIFO ");
3640     	if (s & SWINT)
3641     		SPRINTF("SWINT ");
3642     	SPRINTF("); ");
3643     
3644     	SPRINTF("DMASTAT( ");
3645     	s = GETPORT(DMASTAT);
3646     	if (s & ATDONE)
3647     		SPRINTF("ATDONE ");
3648     	if (s & WORDRDY)
3649     		SPRINTF("WORDRDY ");
3650     	if (s & DFIFOFULL)
3651     		SPRINTF("DFIFOFULL ");
3652     	if (s & DFIFOEMP)
3653     		SPRINTF("DFIFOEMP ");
3654     	SPRINTF(")\n");
3655     
3656     	SPRINTF("enabled interrupts( ");
3657     
3658     	s = GETPORT(SIMODE0);
3659     	if (s & ENSELDO)
3660     		SPRINTF("ENSELDO ");
3661     	if (s & ENSELDI)
3662     		SPRINTF("ENSELDI ");
3663     	if (s & ENSELINGO)
3664     		SPRINTF("ENSELINGO ");
3665     	if (s & ENSWRAP)
3666     		SPRINTF("ENSWRAP ");
3667     	if (s & ENSDONE)
3668     		SPRINTF("ENSDONE ");
3669     	if (s & ENSPIORDY)
3670     		SPRINTF("ENSPIORDY ");
3671     	if (s & ENDMADONE)
3672     		SPRINTF("ENDMADONE ");
3673     
3674     	s = GETPORT(SIMODE1);
3675     	if (s & ENSELTIMO)
3676     		SPRINTF("ENSELTIMO ");
3677     	if (s & ENATNTARG)
3678     		SPRINTF("ENATNTARG ");
3679     	if (s & ENPHASEMIS)
3680     		SPRINTF("ENPHASEMIS ");
3681     	if (s & ENBUSFREE)
3682     		SPRINTF("ENBUSFREE ");
3683     	if (s & ENSCSIPERR)
3684     		SPRINTF("ENSCSIPERR ");
3685     	if (s & ENPHASECHG)
3686     		SPRINTF("ENPHASECHG ");
3687     	if (s & ENREQINIT)
3688     		SPRINTF("ENREQINIT ");
3689     	SPRINTF(")\n");
3690     
3691     	return (pos - start);
3692     }
3693     
3694     int aha152x_set_info(char *buffer, int length, struct Scsi_Host *shpnt)
3695     {
3696     	if(!shpnt || !buffer || length<8 || strncmp("aha152x ", buffer, 8)!=0)
3697     		return -EINVAL;
3698     
3699     #if defined(AHA152X_DEBUG)
3700     	if(length>14 && strncmp("debug ", buffer+8, 6)==0) {
3701     		int debug = HOSTDATA(shpnt)->debug;
3702     
3703     		HOSTDATA(shpnt)->debug = simple_strtoul(buffer+14, NULL, 0);
3704     
3705     		printk(KERN_INFO "aha152x%d: debugging options set to 0x%04x (were 0x%04x)\n", HOSTNO, HOSTDATA(shpnt)->debug, debug);
3706     	} else
3707     #endif
3708     #if defined(AHA152X_STAT)
3709     	if(length>13 && strncmp("reset", buffer+8, 5)==0) {
3710     		int i;
3711     
3712     		HOSTDATA(shpnt)->total_commands=0;
3713     		HOSTDATA(shpnt)->disconnections=0;
3714     		HOSTDATA(shpnt)->busfree_without_any_action=0;
3715     		HOSTDATA(shpnt)->busfree_without_old_command=0;
3716     		HOSTDATA(shpnt)->busfree_without_new_command=0;
3717     		HOSTDATA(shpnt)->busfree_without_done_command=0;
3718     		HOSTDATA(shpnt)->busfree_with_check_condition=0;
3719     		for (i = idle; i<maxstate; i++) {
3720     			HOSTDATA(shpnt)->count[i]=0;
3721     			HOSTDATA(shpnt)->count_trans[i]=0;
3722     			HOSTDATA(shpnt)->time[i]=0;
3723     		}
3724     
3725     		printk(KERN_INFO "aha152x%d: stats reseted.\n", HOSTNO);
3726     
3727     	} else
3728     #endif
3729     	{
3730     		return -EINVAL;
3731     	}
3732     
3733     
3734     	return length;
3735     }
3736     
3737     #undef SPRINTF
3738     #define SPRINTF(args...) \
3739     	do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
3740     
3741     int aha152x_proc_info(char *buffer, char **start,
3742     		      off_t offset, int length, int hostno, int inout)
3743     {
3744     	int i;
3745     	char *pos = buffer;
3746     	struct Scsi_Host *shpnt;
3747     	Scsi_Cmnd *ptr;
3748     	unsigned long flags;
3749     	int thislength;
3750     
3751     	for (i = 0, shpnt = (struct Scsi_Host *) NULL; i < IRQS; i++)
3752     		if (aha152x_host[i] && aha152x_host[i]->host_no == hostno)
3753     			shpnt = aha152x_host[i];
3754     
3755     	if (!shpnt)
3756     		return -ESRCH;
3757     
3758     	DPRINTK(debug_procinfo, 
3759     	       KERN_DEBUG "aha152x_proc_info: buffer=%p offset=%ld length=%d hostno=%d inout=%d\n",
3760     	       buffer, offset, length, hostno, inout);
3761     
3762     
3763     	if (inout)
3764     		return aha152x_set_info(buffer, length, shpnt);
3765     
3766     	SPRINTF(AHA152X_REVID "\n");
3767     
3768     	SPRINTF("ioports 0x%04lx to 0x%04lx\n",
3769     		shpnt->io_port, shpnt->io_port + shpnt->n_io_port - 1);
3770     	SPRINTF("interrupt 0x%02x\n", shpnt->irq);
3771     	SPRINTF("disconnection/reconnection %s\n",
3772     		RECONNECT ? "enabled" : "disabled");
3773     	SPRINTF("parity checking %s\n",
3774     		PARITY ? "enabled" : "disabled");
3775     	SPRINTF("synchronous transfers %s\n",
3776     		SYNCHRONOUS ? "enabled" : "disabled");
3777     	SPRINTF("%d commands currently queued\n", HOSTDATA(shpnt)->commands);
3778     
3779     	if(SYNCHRONOUS) {
3780     		SPRINTF("synchronously operating targets (tick=50 ns):\n");
3781     		for (i = 0; i < 8; i++)
3782     			if (HOSTDATA(shpnt)->syncrate[i] & 0x7f)
3783     				SPRINTF("target %d: period %dT/%dns; req/ack offset %d\n",
3784     					i,
3785     					(((HOSTDATA(shpnt)->syncrate[i] & 0x70) >> 4) + 2),
3786     					(((HOSTDATA(shpnt)->syncrate[i] & 0x70) >> 4) + 2) * 50,
3787     				    HOSTDATA(shpnt)->syncrate[i] & 0x0f);
3788     	}
3789     #if defined(AHA152X_DEBUG)
3790     #define PDEBUG(flags,txt) \
3791     	if(HOSTDATA(shpnt)->debug & flags) SPRINTF("(%s) ", txt);
3792     
3793     	SPRINTF("enabled debugging options: ");
3794     
3795     	PDEBUG(debug_procinfo, "procinfo");
3796     	PDEBUG(debug_queue, "queue");
3797     	PDEBUG(debug_intr, "interrupt");
3798     	PDEBUG(debug_selection, "selection");
3799     	PDEBUG(debug_msgo, "message out");
3800     	PDEBUG(debug_msgi, "message in");
3801     	PDEBUG(debug_status, "status");
3802     	PDEBUG(debug_cmd, "command");
3803     	PDEBUG(debug_datai, "data in");
3804     	PDEBUG(debug_datao, "data out");
3805     	PDEBUG(debug_eh, "eh");
3806     	PDEBUG(debug_locks, "locks");
3807     	PDEBUG(debug_phases, "phases");
3808     
3809     	SPRINTF("\n");
3810     #endif
3811     
3812     	SPRINTF("\nqueue status:\n");
3813     	DO_LOCK(flags);
3814     	if (ISSUE_SC) {
3815     		SPRINTF("not yet issued commands:\n");
3816     		for (ptr = ISSUE_SC; ptr; ptr = SCNEXT(ptr))
3817     			pos += get_command(pos, ptr);
3818     	} else
3819     		SPRINTF("no not yet issued commands\n");
3820     	DO_UNLOCK(flags);
3821     
3822     	if (CURRENT_SC) {
3823     		SPRINTF("current command:\n");
3824     		pos += get_command(pos, CURRENT_SC);
3825     	} else
3826     		SPRINTF("no current command\n");
3827     
3828     	if (DISCONNECTED_SC) {
3829     		SPRINTF("disconnected commands:\n");
3830     		for (ptr = DISCONNECTED_SC; ptr; ptr = SCNEXT(ptr))
3831     			pos += get_command(pos, ptr);
3832     	} else
3833     		SPRINTF("no disconnected commands\n");
3834     
3835     	pos += get_ports(shpnt, pos);
3836     
3837     #if defined(AHA152X_STAT)
3838     	SPRINTF("statistics:\n"
3839     	        "total commands:               %d\n"
3840     	        "disconnections:               %d\n"
3841     		"busfree with check condition: %d\n"
3842     		"busfree without old command:  %d\n"
3843     		"busfree without new command:  %d\n"
3844     		"busfree without done command: %d\n"
3845     		"busfree without any action:   %d\n"
3846     		"state      "
3847     		"transitions  "
3848     		"count        "
3849     		"time\n",
3850     		HOSTDATA(shpnt)->total_commands,
3851     		HOSTDATA(shpnt)->disconnections,
3852     		HOSTDATA(shpnt)->busfree_with_check_condition,
3853     		HOSTDATA(shpnt)->busfree_without_old_command,
3854     		HOSTDATA(shpnt)->busfree_without_new_command,
3855     		HOSTDATA(shpnt)->busfree_without_done_command,
3856     		HOSTDATA(shpnt)->busfree_without_any_action);
3857     	for(i=0; i<maxstate; i++) {
3858     		SPRINTF("%-10s %-12d %-12d %-12ld\n",
3859     		        states[i].name,
3860     			HOSTDATA(shpnt)->count_trans[i],
3861     			HOSTDATA(shpnt)->count[i],
3862     			HOSTDATA(shpnt)->time[i]);
3863     	}
3864     #endif
3865     
3866     	DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: pos=%p\n", pos);
3867     
3868     	thislength = pos - (buffer + offset);
3869     	DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: length=%d thislength=%d\n", length, thislength);
3870     
3871     	if(thislength<0) {
3872     		DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: output too short\n");
3873     		*start = 0;
3874     		return 0;
3875     	}
3876     
3877     	thislength = thislength<length ? thislength : length;
3878     
3879     	DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: return %d\n", thislength);
3880     
3881     	*start = buffer + offset;
3882     	return thislength < length ? thislength : length;
3883     }
3884     
3885     /* Eventually this will go into an include file, but this will be later */
3886     static Scsi_Host_Template driver_template = AHA152X;
3887     
3888     #include "scsi_module.c"
3889