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