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