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