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