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