- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / aha152x.c
blob25636b008aad0ab75189bdfdc982db5cdeac166e
1 /* aha152x.c -- Adaptec AHA-152x driver
2 * Author: Jürgen E. Fischer, fischer@norbit.de
3 * Copyright 1993-1999 Jürgen E. Fischer
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.
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.
16 * $Id: aha152x.c,v 2.3 2000/11/04 16:40:26 fischer Exp $
18 * $Log: aha152x.c,v $
19 * Revision 2.3 2000/11/04 16:40:26 fischer
20 * - handle data overruns
21 * - extend timeout for data phases
23 * Revision 2.2 2000/08/08 19:54:53 fischer
24 * - minor changes
26 * Revision 2.1 2000/05/17 16:23:17 fischer
27 * - signature update
28 * - fix for data out w/o scatter gather
30 * Revision 2.0 1999/12/25 15:07:32 fischer
31 * - interrupt routine completly reworked
32 * - basic support for new eh code
34 * Revision 1.21 1999/11/10 23:46:36 fischer
35 * - default to synchronous operation
36 * - synchronous negotiation fixed
37 * - added timeout to loops
38 * - debugging output can be controlled through procfs
40 * Revision 1.20 1999/11/07 18:37:31 fischer
41 * - synchronous operation works
42 * - resid support for sg driver
44 * Revision 1.19 1999/11/02 22:39:59 fischer
45 * - moved leading comments to README.aha152x
46 * - new additional module parameters
47 * - updates for 2.3
48 * - support for the Tripace TC1550 controller
49 * - interrupt handling changed
51 * Revision 1.18 1996/09/07 20:10:40 fischer
52 * - fixed can_queue handling (multiple outstanding commands working again)
54 * Revision 1.17 1996/08/17 16:05:14 fischer
55 * - biosparam improved
56 * - interrupt verification
57 * - updated documentation
58 * - cleanups
60 * Revision 1.16 1996/06/09 00:04:56 root
61 * - added configuration symbols for insmod (aha152x/aha152x1)
63 * Revision 1.15 1996/04/30 14:52:06 fischer
64 * - proc info fixed
65 * - support for extended translation for >1GB disks
67 * Revision 1.14 1996/01/17 15:11:20 fischer
68 * - fixed lockup in MESSAGE IN phase after reconnection
70 * Revision 1.13 1996/01/09 02:15:53 fischer
71 * - some cleanups
72 * - moved request_irq behind controller initialization
73 * (to avoid spurious interrupts)
75 * Revision 1.12 1995/12/16 12:26:07 fischer
76 * - barrier()s added
77 * - configurable RESET delay added
79 * Revision 1.11 1995/12/06 21:18:35 fischer
80 * - some minor updates
82 * Revision 1.10 1995/07/22 19:18:45 fischer
83 * - support for 2 controllers
84 * - started synchronous data transfers (not working yet)
86 * Revision 1.9 1995/03/18 09:20:24 root
87 * - patches for PCMCIA and modules
89 * Revision 1.8 1995/01/21 22:07:19 root
90 * - snarf_region => request_region
91 * - aha152x_intr interface change
93 * Revision 1.7 1995/01/02 23:19:36 root
94 * - updated COMMAND_SIZE to cmd_len
95 * - changed sti() to restore_flags()
96 * - fixed some #ifdef which generated warnings
98 * Revision 1.6 1994/11/24 20:35:27 root
99 * - problem with odd number of bytes in fifo fixed
101 * Revision 1.5 1994/10/30 14:39:56 root
102 * - abort code fixed
103 * - debugging improved
105 * Revision 1.4 1994/09/12 11:33:01 root
106 * - irqaction to request_irq
107 * - abortion updated
109 * Revision 1.3 1994/08/04 13:53:05 root
110 * - updates for mid-level-driver changes
111 * - accept unexpected BUSFREE phase as error condition
112 * - parity check now configurable
114 * Revision 1.2 1994/07/03 12:56:36 root
115 * - cleaned up debugging code
116 * - more tweaking on reset delays
117 * - updated abort/reset code (pretty untested...)
119 * Revision 1.1 1994/05/28 21:18:49 root
120 * - update for mid-level interface change (abort-reset)
121 * - delays after resets adjusted for some slow devices
123 * Revision 1.0 1994/03/25 12:52:00 root
124 * - Fixed "more data than expected" problem
125 * - added new BIOS signatures
127 * Revision 0.102 1994/01/31 20:44:12 root
128 * - minor changes in insw/outsw handling
130 * Revision 0.101 1993/12/13 01:16:27 root
131 * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
132 * fixes problems with CD-ROM sector size detection & media change)
134 * Revision 0.100 1993/12/10 16:58:47 root
135 * - fix for unsuccessful selections in case of non-continuous id assignments
136 * on the scsi bus.
138 * Revision 0.99 1993/10/24 16:19:59 root
139 * - fixed DATA IN (rare read errors gone)
141 * Revision 0.98 1993/10/17 12:54:44 root
142 * - fixed some recent fixes (shame on me)
143 * - moved initialization of scratch area to aha152x_queue
145 * Revision 0.97 1993/10/09 18:53:53 root
146 * - DATA IN fixed. Rarely left data in the fifo.
148 * Revision 0.96 1993/10/03 00:53:59 root
149 * - minor changes on DATA IN
151 * Revision 0.95 1993/09/24 10:36:01 root
152 * - change handling of MSGI after reselection
153 * - fixed sti/cli
154 * - minor changes
156 * Revision 0.94 1993/09/18 14:08:22 root
157 * - fixed bug in multiple outstanding command code
158 * - changed detection
159 * - support for kernel command line configuration
160 * - reset corrected
161 * - changed message handling
163 * Revision 0.93 1993/09/15 20:41:19 root
164 * - fixed bugs with multiple outstanding commands
166 * Revision 0.92 1993/09/13 02:46:33 root
167 * - multiple outstanding commands work (no problems with IBM drive)
169 * Revision 0.91 1993/09/12 20:51:46 root
170 * added multiple outstanding commands
171 * (some problem with this $%&? IBM device remain)
173 * Revision 0.9 1993/09/12 11:11:22 root
174 * - corrected auto-configuration
175 * - changed the auto-configuration (added some '#define's)
176 * - added support for dis-/reconnection
178 * Revision 0.8 1993/09/06 23:09:39 root
179 * - added support for the drive activity light
180 * - minor changes
182 * Revision 0.7 1993/09/05 14:30:15 root
183 * - improved phase detection
184 * - now using the new snarf_region code of 0.99pl13
186 * Revision 0.6 1993/09/02 11:01:38 root
187 * first public release; added some signatures and biosparam()
189 * Revision 0.5 1993/08/30 10:23:30 root
190 * fixed timing problems with my IBM drive
192 * Revision 0.4 1993/08/29 14:06:52 root
193 * fixed some problems with timeouts due incomplete commands
195 * Revision 0.3 1993/08/28 15:55:03 root
196 * writing data works too. mounted and worked on a dos partition
198 * Revision 0.2 1993/08/27 22:42:07 root
199 * reading data works. Mounted a msdos partition.
201 * Revision 0.1 1993/08/25 13:38:30 root
202 * first "damn thing doesn't work" version
204 * Revision 0.0 1993/08/14 19:54:25 root
205 * empty function bodies; detect() works.
208 **************************************************************************
210 see README.aha152x for configuration details
212 **************************************************************************/
214 #include <linux/module.h>
216 #if defined(PCMCIA)
217 #undef MODULE
218 #endif
220 #include <linux/sched.h>
221 #include <asm/io.h>
222 #include <linux/blk.h>
223 #include "scsi.h"
224 #include "sd.h"
225 #include "hosts.h"
226 #include "constants.h"
227 #include <asm/system.h>
228 #include <linux/errno.h>
229 #include <linux/string.h>
230 #include <linux/wait.h>
231 #include <linux/ioport.h>
232 #include <linux/delay.h>
233 #include <linux/proc_fs.h>
234 #include <linux/interrupt.h>
235 #include <linux/init.h>
236 #include <linux/kernel.h>
237 #include <asm/semaphore.h>
238 #include <linux/spinlock.h>
240 #include "aha152x.h"
241 #include <linux/stat.h>
243 #include <scsi/scsicam.h>
245 /* DEFINES */
247 /* For PCMCIA cards, always use AUTOCONF */
248 #if defined(PCMCIA) || defined(MODULE)
249 #if !defined(AUTOCONF)
250 #define AUTOCONF
251 #endif
252 #endif
254 #if !defined(AUTOCONF) && !defined(SETUP0)
255 #error define AUTOCONF or SETUP0
256 #endif
258 #if defined(AHA152X_DEBUG)
259 #define DEBUG_DEFAULT debug_eh
261 #define DPRINTK(when,msgs...) \
262 do { if(HOSTDATA(shpnt)->debug & (when)) printk(msgs); } while(0)
264 #define DO_LOCK(flags) \
265 do { \
266 if(spin_is_locked(&QLOCK)) { \
267 DPRINTK(debug_intr, DEBUG_LEAD "(%s:%d) already locked at %s:%d\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__, QLOCKER, QLOCKERL); \
269 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) locking\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
270 spin_lock_irqsave(&QLOCK,flags); \
271 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) locked\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
272 QLOCKER=__FUNCTION__; \
273 QLOCKERL=__LINE__; \
274 } while(0)
276 #define DO_UNLOCK(flags) \
277 do { \
278 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) unlocking (locked at %s:%d)\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__, QLOCKER, QLOCKERL); \
279 spin_unlock_irqrestore(&QLOCK,flags); \
280 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) unlocked\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
281 QLOCKER="(not locked)"; \
282 QLOCKERL=0; \
283 } while(0)
285 #else
286 #define DPRINTK(when,msgs...)
287 #define DO_LOCK(flags) spin_lock_irqsave(&QLOCK,flags)
288 #define DO_UNLOCK(flags) spin_unlock_irqrestore(&QLOCK,flags)
289 #endif
291 #define LEAD "(scsi%d:%d:%d) "
292 #define WARN_LEAD KERN_WARNING LEAD
293 #define INFO_LEAD KERN_INFO LEAD
294 #define NOTE_LEAD KERN_NOTICE LEAD
295 #define ERR_LEAD KERN_ERR LEAD
296 #define DEBUG_LEAD KERN_DEBUG LEAD
297 #define CMDINFO(cmd) \
298 (cmd) ? ((cmd)->host->host_no) : -1, \
299 (cmd) ? ((cmd)->target & 0x0f) : -1, \
300 (cmd) ? ((cmd)->lun & 0x07) : -1
302 #define DELAY_DEFAULT 100
304 /* possible irq range */
305 #if defined(PCMCIA)
306 #define IRQ_MIN 0
307 #define IRQ_MAX 16
308 #else
309 #define IRQ_MIN 9
310 #define IRQ_MAX 12
311 #endif
312 #define IRQS IRQ_MAX-IRQ_MIN+1
314 enum {
315 not_issued = 0x0001, /* command not yet issued */
316 selecting = 0x0002, /* target is beeing selected */
317 identified = 0x0004, /* IDENTIFY was sent */
318 disconnected = 0x0008, /* target disconnected */
319 completed = 0x0010, /* target sent COMMAND COMPLETE */
320 aborted = 0x0020, /* ABORT was sent */
321 resetted = 0x0040, /* BUS DEVICE RESET was sent */
322 spiordy = 0x0080, /* waiting for SPIORDY to raise */
323 syncneg = 0x0100, /* synchronous negotiation in progress */
324 aborting = 0x0200, /* ABORT is pending */
325 resetting = 0x0400, /* BUS DEVICE RESET is pending */
328 #if defined(MODULE)
329 MODULE_AUTHOR("Jürgen Fischer");
330 MODULE_DESCRIPTION(AHA152X_REVID);
331 MODULE_PARM(io, "1-2i");
332 MODULE_PARM_DESC(io,"base io address of controller");
333 static int io[] = {0, 0};
335 MODULE_PARM(irq, "1-2i");
336 MODULE_PARM_DESC(irq,"interrupt for controller");
337 static int irq[] = {0, 0};
339 MODULE_PARM(scsiid, "1-2i");
340 MODULE_PARM_DESC(scsiid,"scsi id of controller");
341 static int scsiid[] = {7, 7};
343 MODULE_PARM(reconnect, "1-2i");
344 MODULE_PARM_DESC(reconnect,"allow targets to disconnect");
345 static int reconnect[] = {1, 1};
347 MODULE_PARM(parity, "1-2i");
348 MODULE_PARM_DESC(parity,"use scsi parity");
349 static int parity[] = {1, 1};
351 MODULE_PARM(sync, "1-2i");
352 MODULE_PARM_DESC(sync,"use synchronous transfers");
353 static int sync[] = {1, 1};
355 MODULE_PARM(delay, "1-2i");
356 MODULE_PARM_DESC(delay,"scsi reset delay");
357 static int delay[] = {DELAY_DEFAULT, DELAY_DEFAULT};
359 MODULE_PARM(exttrans, "1-2i");
360 MODULE_PARM_DESC(exttrans,"use extended translation");
361 static int exttrans[] = {0, 0};
363 #if !defined(AHA152X_DEBUG)
364 MODULE_PARM(aha152x, "1-8i");
365 MODULE_PARM_DESC(aha152x, "parameters for first controller");
366 static int aha152x[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
368 MODULE_PARM(aha152x1, "1-8i");
369 MODULE_PARM_DESC(aha152x1, "parameters for second controller");
370 static int aha152x1[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
371 #else
372 MODULE_PARM(debug, "1-2i");
373 MODULE_PARM_DESC(debug, "flags for driver debugging");
374 static int debug[] = {DEBUG_DEFAULT, DEBUG_DEFAULT};
376 MODULE_PARM(aha152x, "1-9i");
377 MODULE_PARM_DESC(aha152x, "parameters for first controller");
378 static int aha152x[] = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
380 MODULE_PARM(aha152x1, "1-9i");
381 MODULE_PARM_DESC(aha152x1, "parameters for second controller");
382 static int aha152x1[] = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
383 #endif /* !defined(AHA152X_DEBUG) */
384 #endif /* MODULE */
386 /* set by aha152x_setup according to the command line */
387 static int setup_count = 0;
388 static int registered_count = 0;
389 static struct aha152x_setup {
390 int io_port;
391 int irq;
392 int scsiid;
393 int reconnect;
394 int parity;
395 int synchronous;
396 int delay;
397 int ext_trans;
398 int tc1550;
399 #if defined(AHA152X_DEBUG)
400 int debug;
401 #endif
402 char *conf;
403 } setup[2];
405 static struct Scsi_Host *aha152x_host[IRQS];
408 * internal states of the host
411 enum aha152x_state {
412 idle=0,
413 unknown,
414 seldo,
415 seldi,
416 selto,
417 busfree,
418 msgo,
419 cmd,
420 msgi,
421 status,
422 datai,
423 datao,
424 parerr,
425 rsti,
426 maxstate
430 * current state information of the host
433 struct aha152x_hostdata {
434 Scsi_Cmnd *issue_SC;
435 /* pending commands to issue */
437 Scsi_Cmnd *current_SC;
438 /* current command on the bus */
440 Scsi_Cmnd *disconnected_SC;
441 /* commands that disconnected */
443 Scsi_Cmnd *done_SC;
444 /* command that was completed */
446 spinlock_t lock;
447 /* host lock */
449 #if defined(AHA152X_DEBUG)
450 char *locker; /* which function has the lock */
451 int lockerl; /* where did it get it */
453 int debug; /* current debugging setting */
454 #endif
456 #if defined(AHA152X_STAT)
457 int total_commands;
458 int disconnections;
459 int busfree_without_any_action;
460 int busfree_without_old_command;
461 int busfree_without_new_command;
462 int busfree_without_done_command;
463 int busfree_with_check_condition;
464 int count[maxstate];
465 int count_trans[maxstate];
466 unsigned long time[maxstate];
467 #endif
469 int commands; /* current number of commands */
471 int reconnect; /* disconnection allowed */
472 int parity; /* parity checking enabled */
473 int synchronous; /* synchronous transferes enabled */
474 int delay; /* reset out delay */
475 int ext_trans; /* extended translation enabled */
477 int swint; /* software-interrupt was fired during detect() */
478 int service; /* bh needs to be run */
479 int in_intr; /* bh is running */
481 /* current state,
482 previous state,
483 last state different from current state */
484 enum aha152x_state state, prevstate, laststate;
486 int target;
487 /* reconnecting target */
489 unsigned char syncrate[8];
490 /* current synchronous transfer agreements */
492 unsigned char syncneg[8];
493 /* 0: no negotiation;
494 * 1: negotiation in progress;
495 * 2: negotiation completed
498 int cmd_i;
499 /* number of sent bytes of current command */
501 int msgi_len;
502 /* number of received message bytes */
503 unsigned char msgi[256];
504 /* received message bytes */
506 int msgo_i, msgo_len;
507 /* number of sent bytes and length of current messages */
508 unsigned char msgo[256];
509 /* pending messages */
511 int data_len;
512 /* number of sent/received bytes in dataphase */
514 unsigned long io_port0;
515 unsigned long io_port1;
520 * host specific command extension
523 struct aha152x_scdata {
524 Scsi_Cmnd *next; /* next sc in queue */
525 Scsi_Cmnd *done; /* done command */
526 struct semaphore *sem; /* semaphore to block on */
530 /* access macros for hostdata */
532 #define HOSTDATA(shpnt) ((struct aha152x_hostdata *) &shpnt->hostdata)
534 #define HOSTNO ((shpnt)->host_no)
536 #define CURRENT_SC (HOSTDATA(shpnt)->current_SC)
537 #define DONE_SC (HOSTDATA(shpnt)->done_SC)
538 #define ISSUE_SC (HOSTDATA(shpnt)->issue_SC)
539 #define DISCONNECTED_SC (HOSTDATA(shpnt)->disconnected_SC)
540 #define QLOCK (HOSTDATA(shpnt)->lock)
541 #define QLOCKER (HOSTDATA(shpnt)->locker)
542 #define QLOCKERL (HOSTDATA(shpnt)->lockerl)
544 #define STATE (HOSTDATA(shpnt)->state)
545 #define PREVSTATE (HOSTDATA(shpnt)->prevstate)
546 #define LASTSTATE (HOSTDATA(shpnt)->laststate)
548 #define RECONN_TARGET (HOSTDATA(shpnt)->target)
550 #define CMD_I (HOSTDATA(shpnt)->cmd_i)
552 #define MSGO(i) (HOSTDATA(shpnt)->msgo[i])
553 #define MSGO_I (HOSTDATA(shpnt)->msgo_i)
554 #define MSGOLEN (HOSTDATA(shpnt)->msgo_len)
555 #define ADDMSGO(x) (MSGOLEN<256 ? MSGO(MSGOLEN++)=x : aha152x_error(shpnt,"MSGO overflow"))
557 #define MSGI(i) (HOSTDATA(shpnt)->msgi[i])
558 #define MSGILEN (HOSTDATA(shpnt)->msgi_len)
559 #define ADDMSGI(x) (MSGILEN<256 ? MSGI(MSGILEN++)=x : aha152x_error(shpnt,"MSGI overflow"))
561 #define DATA_LEN (HOSTDATA(shpnt)->data_len)
563 #define SYNCRATE (HOSTDATA(shpnt)->syncrate[CURRENT_SC->target])
564 #define SYNCNEG (HOSTDATA(shpnt)->syncneg[CURRENT_SC->target])
566 #define DELAY (HOSTDATA(shpnt)->delay)
567 #define EXT_TRANS (HOSTDATA(shpnt)->ext_trans)
568 #define TC1550 (HOSTDATA(shpnt)->tc1550)
569 #define RECONNECT (HOSTDATA(shpnt)->reconnect)
570 #define PARITY (HOSTDATA(shpnt)->parity)
571 #define SYNCHRONOUS (HOSTDATA(shpnt)->synchronous)
573 #define HOSTIOPORT0 (HOSTDATA(shpnt)->io_port0)
574 #define HOSTIOPORT1 (HOSTDATA(shpnt)->io_port1)
576 #define SCDATA(SCpnt) ((struct aha152x_scdata *) (SCpnt)->host_scribble)
577 #define SCNEXT(SCpnt) SCDATA(SCpnt)->next
578 #define SCDONE(SCpnt) SCDATA(SCpnt)->done
579 #define SCSEM(SCpnt) SCDATA(SCpnt)->sem
582 /* state handling */
583 static void seldi_run(struct Scsi_Host *shpnt);
584 static void seldo_run(struct Scsi_Host *shpnt);
585 static void selto_run(struct Scsi_Host *shpnt);
586 static void busfree_run(struct Scsi_Host *shpnt);
588 static void msgo_init(struct Scsi_Host *shpnt);
589 static void msgo_run(struct Scsi_Host *shpnt);
590 static void msgo_end(struct Scsi_Host *shpnt);
592 static void cmd_init(struct Scsi_Host *shpnt);
593 static void cmd_run(struct Scsi_Host *shpnt);
594 static void cmd_end(struct Scsi_Host *shpnt);
596 static void datai_init(struct Scsi_Host *shpnt);
597 static void datai_run(struct Scsi_Host *shpnt);
598 static void datai_end(struct Scsi_Host *shpnt);
600 static void datao_init(struct Scsi_Host *shpnt);
601 static void datao_run(struct Scsi_Host *shpnt);
602 static void datao_end(struct Scsi_Host *shpnt);
604 static void status_run(struct Scsi_Host *shpnt);
606 static void msgi_run(struct Scsi_Host *shpnt);
607 static void msgi_end(struct Scsi_Host *shpnt);
609 static void parerr_run(struct Scsi_Host *shpnt);
610 static void rsti_run(struct Scsi_Host *shpnt);
612 static void complete(struct Scsi_Host *shpnt);
615 * driver states
618 static struct {
619 char *name;
620 void (*init)(struct Scsi_Host *);
621 void (*run)(struct Scsi_Host *);
622 void (*end)(struct Scsi_Host *);
623 int spio;
624 } states[] = {
625 { "idle", 0, 0, 0, 0},
626 { "unknown", 0, 0, 0, 0},
627 { "seldo", 0, seldo_run, 0, 0},
628 { "seldi", 0, seldi_run, 0, 0},
629 { "selto", 0, selto_run, 0, 0},
630 { "busfree", 0, busfree_run, 0, 0},
631 { "msgo", msgo_init, msgo_run, msgo_end, 1},
632 { "cmd", cmd_init, cmd_run, cmd_end, 1},
633 { "msgi", 0, msgi_run, msgi_end, 1},
634 { "status", 0, status_run, 0, 1},
635 { "datai", datai_init, datai_run, datai_end, 0},
636 { "datao", datao_init, datao_run, datao_end, 0},
637 { "parerr", 0, parerr_run, 0, 0},
638 { "rsti", 0, rsti_run, 0, 0},
641 /* setup & interrupt */
642 static void intr(int irq, void *dev_id, struct pt_regs *);
643 static void reset_ports(struct Scsi_Host *shpnt);
644 static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
645 static void done(struct Scsi_Host *shpnt, int error);
646 static int checksetup(struct aha152x_setup *setup);
648 /* diagnostics */
649 static void disp_ports(struct Scsi_Host *shpnt);
650 static void show_command(Scsi_Cmnd * ptr);
651 static void show_queues(struct Scsi_Host *shpnt);
652 static void disp_enintr(struct Scsi_Host *shpnt);
654 /* possible i/o addresses for the AIC-6260; default first */
655 static unsigned short ports[] = { 0x340, 0x140 };
656 #define PORT_COUNT (sizeof(ports) / sizeof(unsigned short))
658 #if !defined(SKIP_BIOSTEST)
659 /* possible locations for the Adaptec BIOS; defaults first */
660 static unsigned int addresses[] =
662 0xdc000, /* default first */
663 0xc8000,
664 0xcc000,
665 0xd0000,
666 0xd4000,
667 0xd8000,
668 0xe0000,
669 0xeb800, /* VTech Platinum SMP */
670 0xf0000,
672 #define ADDRESS_COUNT (sizeof(addresses) / sizeof(unsigned int))
674 /* signatures for various AIC-6[23]60 based controllers.
675 The point in detecting signatures is to avoid useless and maybe
676 harmful probes on ports. I'm not sure that all listed boards pass
677 auto-configuration. For those which fail the BIOS signature is
678 obsolete, because user intervention to supply the configuration is
679 needed anyway. May be an information whether or not the BIOS supports
680 extended translation could be also useful here. */
681 static struct signature {
682 unsigned char *signature;
683 int sig_offset;
684 int sig_length;
685 } signatures[] =
687 { "Adaptec AHA-1520 BIOS", 0x102e, 21 },
688 /* Adaptec 152x */
689 { "Adaptec AHA-1520B", 0x000b, 17 },
690 /* Adaptec 152x rev B */
691 { "Adaptec AHA-1520B", 0x0026, 17 },
692 /* Iomega Jaz Jet ISA (AIC6370Q) */
693 { "Adaptec ASW-B626 BIOS", 0x1029, 21 },
694 /* on-board controller */
695 { "Adaptec BIOS: ASW-B626", 0x000f, 22 },
696 /* on-board controller */
697 { "Adaptec ASW-B626 S2", 0x2e6c, 19 },
698 /* on-board controller */
699 { "Adaptec BIOS:AIC-6360", 0x000c, 21 },
700 /* on-board controller */
701 { "ScsiPro SP-360 BIOS", 0x2873, 19 },
702 /* ScsiPro-Controller */
703 { "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },
704 /* Gigabyte Local-Bus-SCSI */
705 { "Adaptec BIOS:AVA-282X", 0x000c, 21 },
706 /* Adaptec 282x */
707 { "Adaptec IBM Dock II SCSI", 0x2edd, 24 },
708 /* IBM Thinkpad Dock II */
709 { "Adaptec BIOS:AHA-1532P", 0x001c, 22 },
710 /* IBM Thinkpad Dock II SCSI */
711 { "DTC3520A Host Adapter BIOS", 0x318a, 26 },
712 /* DTC 3520A ISA SCSI */
715 #define SIGNATURE_COUNT (sizeof(signatures) / sizeof(struct signature))
716 #endif
720 * queue services:
723 static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
725 Scsi_Cmnd *end;
727 SCNEXT(new_SC) = NULL;
728 if (!*SC)
729 *SC = new_SC;
730 else {
731 for (end = *SC; SCNEXT(end); end = SCNEXT(end))
733 SCNEXT(end) = new_SC;
737 static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd ** SC)
739 Scsi_Cmnd *ptr;
741 ptr = *SC;
742 if (ptr) {
743 *SC = SCNEXT(*SC);
744 SCNEXT(ptr)=NULL;
746 return ptr;
749 static inline Scsi_Cmnd *remove_lun_SC(Scsi_Cmnd ** SC, int target, int lun)
751 Scsi_Cmnd *ptr, *prev;
753 for (ptr = *SC, prev = NULL;
754 ptr && ((ptr->target != target) || (ptr->lun != lun));
755 prev = ptr, ptr = SCNEXT(ptr))
758 if (ptr) {
759 if (prev)
760 SCNEXT(prev) = SCNEXT(ptr);
761 else
762 *SC = SCNEXT(ptr);
764 SCNEXT(ptr)=NULL;
767 return ptr;
770 static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, Scsi_Cmnd *SCp)
772 Scsi_Cmnd *ptr, *prev;
774 for (ptr = *SC, prev = NULL;
775 ptr && SCp!=ptr;
776 prev = ptr, ptr = SCNEXT(ptr))
779 if (ptr) {
780 if (prev)
781 SCNEXT(prev) = SCNEXT(ptr);
782 else
783 *SC = SCNEXT(ptr);
785 SCNEXT(ptr)=NULL;
788 return ptr;
791 #if defined(PCMCIA) || !defined(MODULE)
792 void aha152x_setup(char *str, int *ints)
794 if(setup_count>2) {
795 printk(KERN_ERR "aha152x: you can only configure up to two controllers\n");
796 return;
799 setup[setup_count].conf = str;
800 setup[setup_count].io_port = ints[0] >= 1 ? ints[1] : 0x340;
801 setup[setup_count].irq = ints[0] >= 2 ? ints[2] : 11;
802 setup[setup_count].scsiid = ints[0] >= 3 ? ints[3] : 7;
803 setup[setup_count].reconnect = ints[0] >= 4 ? ints[4] : 1;
804 setup[setup_count].parity = ints[0] >= 5 ? ints[5] : 1;
805 setup[setup_count].synchronous = ints[0] >= 6 ? ints[6] : 1;
806 setup[setup_count].delay = ints[0] >= 7 ? ints[7] : DELAY_DEFAULT;
807 setup[setup_count].ext_trans = ints[0] >= 8 ? ints[8] : 0;
808 #if defined(AHA152X_DEBUG)
809 setup[setup_count].debug = ints[0] >= 9 ? ints[9] : DEBUG_DEFAULT;
810 if (ints[0] > 9) {
811 printk(KERN_NOTICE "aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
812 "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>[,<DEBUG>]]]]]]]]\n");
813 #else
814 if (ints[0] > 8) { /*}*/
815 printk(KERN_NOTICE "aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
816 "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>]]]]]]]\n");
817 #endif
818 return;
819 } else {
820 setup_count++;
821 return;
824 #endif
826 #if !defined(MODULE)
827 static int __init do_setup(char *str)
830 #if defined(AHA152X_DEBUG)
831 int ints[11];
832 #else
833 int ints[10];
834 #endif
835 int count=setup_count;
837 get_options(str, sizeof(ints)/sizeof(int), ints);
838 aha152x_setup(str,ints);
840 return count<setup_count;
843 __setup("aha152x=", do_setup);
844 #endif
847 * Test, if port_base is valid.
850 static int aha152x_porttest(int io_port)
852 int i;
854 if (check_region(io_port, IO_RANGE))
855 return 0;
857 SETPORT(io_port + O_DMACNTRL1, 0); /* reset stack pointer */
858 for (i = 0; i < 16; i++)
859 SETPORT(io_port + O_STACK, i);
861 SETPORT(io_port + O_DMACNTRL1, 0); /* reset stack pointer */
862 for (i = 0; i < 16 && GETPORT(io_port + O_STACK) == i; i++)
865 return (i == 16);
868 static int tc1550_porttest(int io_port)
870 int i;
872 if (check_region(io_port, IO_RANGE))
873 return 0;
875 SETPORT(io_port + O_TC_DMACNTRL1, 0); /* reset stack pointer */
876 for (i = 0; i < 16; i++)
877 SETPORT(io_port + O_STACK, i);
879 SETPORT(io_port + O_TC_DMACNTRL1, 0); /* reset stack pointer */
880 for (i = 0; i < 16 && GETPORT(io_port + O_TC_STACK) == i; i++)
883 return (i == 16);
886 static int checksetup(struct aha152x_setup *setup)
889 #if !defined(PCMCIA)
890 int i;
891 for (i = 0; i < PORT_COUNT && (setup->io_port != ports[i]); i++)
894 if (i == PORT_COUNT)
895 return 0;
896 #endif
898 if(aha152x_porttest(setup->io_port)) {
899 setup->tc1550=0;
900 } else if(tc1550_porttest(setup->io_port)) {
901 setup->tc1550=1;
902 } else
903 return 0;
905 if ((setup->irq < IRQ_MIN) || (setup->irq > IRQ_MAX))
906 return 0;
908 if ((setup->scsiid < 0) || (setup->scsiid > 7))
909 return 0;
911 if ((setup->reconnect < 0) || (setup->reconnect > 1))
912 return 0;
914 if ((setup->parity < 0) || (setup->parity > 1))
915 return 0;
917 if ((setup->synchronous < 0) || (setup->synchronous > 1))
918 return 0;
920 if ((setup->ext_trans < 0) || (setup->ext_trans > 1))
921 return 0;
924 return 1;
927 static void swintr(int irqno, void *dev_id, struct pt_regs *regs)
929 struct Scsi_Host *shpnt = aha152x_host[irqno - IRQ_MIN];
931 if (!shpnt)
932 printk(KERN_ERR "aha152x%d: catched software interrupt for unknown controller.\n", HOSTNO);
934 HOSTDATA(shpnt)->swint++;
938 int aha152x_detect(Scsi_Host_Template * tpnt)
940 int i, j, ok;
941 #if defined(AUTOCONF)
942 aha152x_config conf;
943 #endif
944 tpnt->proc_name = "aha152x";
946 for (i = 0; i < IRQS; i++)
947 aha152x_host[i] = (struct Scsi_Host *) NULL;
949 if (setup_count) {
950 printk(KERN_INFO "aha152x: processing commandline: ");
952 for (i = 0; i < setup_count; i++)
953 if (!checksetup(&setup[i])) {
954 printk(KERN_ERR "\naha152x: %s\n", setup[i].conf);
955 printk(KERN_ERR "aha152x: invalid line\n");
957 printk("ok\n");
959 #if defined(SETUP0)
960 if (setup_count < 2) {
961 struct aha152x_setup override = SETUP0;
963 if (setup_count == 0 || (override.io_port != setup[0].io_port)) {
964 if (!checksetup(&override)) {
965 printk(KERN_ERR "\naha152x: invalid override SETUP0={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
966 override.io_port,
967 override.irq,
968 override.scsiid,
969 override.reconnect,
970 override.parity,
971 override.synchronous,
972 override.delay,
973 override.ext_trans);
974 } else
975 setup[setup_count++] = override;
978 #endif
980 #if defined(SETUP1)
981 if (setup_count < 2) {
982 struct aha152x_setup override = SETUP1;
984 if (setup_count == 0 || (override.io_port != setup[0].io_port)) {
985 if (!checksetup(&override)) {
986 printk(KERN_ERR "\naha152x: invalid override SETUP1={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
987 override.io_port,
988 override.irq,
989 override.scsiid,
990 override.reconnect,
991 override.parity,
992 override.synchronous,
993 override.delay,
994 override.ext_trans);
995 } else
996 setup[setup_count++] = override;
999 #endif
1001 #if defined(MODULE)
1002 if (setup_count<2 && (aha152x[0]!=0 || io[0]!=0 || irq[0]!=0)) {
1003 if(aha152x[0]!=0) {
1004 setup[setup_count].conf = "";
1005 setup[setup_count].io_port = aha152x[0];
1006 setup[setup_count].irq = aha152x[1];
1007 setup[setup_count].scsiid = aha152x[2];
1008 setup[setup_count].reconnect = aha152x[3];
1009 setup[setup_count].parity = aha152x[4];
1010 setup[setup_count].synchronous = aha152x[5];
1011 setup[setup_count].delay = aha152x[6];
1012 setup[setup_count].ext_trans = aha152x[7];
1013 #if defined(AHA152X_DEBUG)
1014 setup[setup_count].debug = aha152x[8];
1015 #endif
1016 } else if(io[0]!=0 || irq[0]!=0) {
1017 if(io[0]!=0) setup[setup_count].io_port = io[0];
1018 if(irq[0]!=0) setup[setup_count].irq = irq[0];
1020 setup[setup_count].scsiid = scsiid[0];
1021 setup[setup_count].reconnect = reconnect[0];
1022 setup[setup_count].parity = parity[0];
1023 setup[setup_count].synchronous = sync[0];
1024 setup[setup_count].delay = delay[0];
1025 setup[setup_count].ext_trans = exttrans[0];
1026 #if defined(AHA152X_DEBUG)
1027 setup[setup_count].debug = debug[0];
1028 #endif
1031 if (checksetup(&setup[setup_count]))
1032 setup_count++;
1033 else
1034 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",
1035 setup[setup_count].io_port,
1036 setup[setup_count].irq,
1037 setup[setup_count].scsiid,
1038 setup[setup_count].reconnect,
1039 setup[setup_count].parity,
1040 setup[setup_count].synchronous,
1041 setup[setup_count].delay,
1042 setup[setup_count].ext_trans);
1045 if (setup_count < 2 && (aha152x1[0]!=0 || io[1]!=0 || irq[1]!=0)) {
1046 if(aha152x1[0]!=0) {
1047 setup[setup_count].conf = "";
1048 setup[setup_count].io_port = aha152x1[0];
1049 setup[setup_count].irq = aha152x1[1];
1050 setup[setup_count].scsiid = aha152x1[2];
1051 setup[setup_count].reconnect = aha152x1[3];
1052 setup[setup_count].parity = aha152x1[4];
1053 setup[setup_count].synchronous = aha152x1[5];
1054 setup[setup_count].delay = aha152x1[6];
1055 setup[setup_count].ext_trans = aha152x1[7];
1056 #if defined(AHA152X_DEBUG)
1057 setup[setup_count].debug = aha152x1[8];
1058 #endif
1059 } else if(io[1]!=0 || irq[1]!=0) {
1060 if(io[1]!=0) setup[setup_count].io_port = io[1];
1061 if(irq[1]!=0) setup[setup_count].irq = irq[1];
1063 setup[setup_count].scsiid = scsiid[1];
1064 setup[setup_count].reconnect = reconnect[1];
1065 setup[setup_count].parity = parity[1];
1066 setup[setup_count].synchronous = sync[1];
1067 setup[setup_count].delay = delay[1];
1068 setup[setup_count].ext_trans = exttrans[1];
1069 #if defined(AHA152X_DEBUG)
1070 setup[setup_count].debug = debug[1];
1071 #endif
1073 if (checksetup(&setup[setup_count]))
1074 setup_count++;
1075 else
1076 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",
1077 setup[setup_count].io_port,
1078 setup[setup_count].irq,
1079 setup[setup_count].scsiid,
1080 setup[setup_count].reconnect,
1081 setup[setup_count].parity,
1082 setup[setup_count].synchronous,
1083 setup[setup_count].delay,
1084 setup[setup_count].ext_trans);
1086 #endif
1088 #if defined(AUTOCONF)
1089 if (setup_count < 2) {
1090 #if !defined(SKIP_BIOSTEST)
1091 ok = 0;
1092 for (i = 0; i < ADDRESS_COUNT && !ok; i++)
1093 for (j = 0; (j < SIGNATURE_COUNT) && !ok; j++)
1094 ok = isa_check_signature(addresses[i] + signatures[j].sig_offset,
1095 signatures[j].signature, signatures[j].sig_length);
1097 if (!ok && setup_count == 0)
1098 return 0;
1100 printk(KERN_INFO "aha152x: BIOS test: passed, ");
1101 #else
1102 printk(KERN_INFO "aha152x: ");
1103 #endif /* !SKIP_BIOSTEST */
1105 ok = 0;
1106 for (i = 0; i < PORT_COUNT && setup_count < 2; i++) {
1107 if ((setup_count == 1) && (setup[0].io_port == ports[i]))
1108 continue;
1110 if (aha152x_porttest(ports[i])) {
1111 ok++;
1112 setup[setup_count].io_port = ports[i];
1113 setup[setup_count].tc1550 = 0;
1115 conf.cf_port =
1116 (GETPORT(ports[i] + O_PORTA) << 8) + GETPORT(ports[i] + O_PORTB);
1118 setup[setup_count].irq = IRQ_MIN + conf.cf_irq;
1119 setup[setup_count].scsiid = conf.cf_id;
1120 setup[setup_count].reconnect = conf.cf_tardisc;
1121 setup[setup_count].parity = !conf.cf_parity;
1122 setup[setup_count].synchronous = conf.cf_syncneg;
1123 setup[setup_count].delay = DELAY_DEFAULT;
1124 setup[setup_count].ext_trans = 0;
1125 #if defined(AHA152X_DEBUG)
1126 setup[setup_count].debug = DEBUG_DEFAULT;
1127 #endif
1128 setup_count++;
1129 } else if (tc1550_porttest(ports[i])) {
1130 ok++;
1131 setup[setup_count].io_port = ports[i];
1132 setup[setup_count].tc1550 = 1;
1134 conf.cf_port =
1135 (GETPORT(ports[i] + O_PORTA) << 8) + GETPORT(ports[i] + O_PORTB);
1137 setup[setup_count].irq = IRQ_MIN + conf.cf_irq;
1138 setup[setup_count].scsiid = conf.cf_id;
1139 setup[setup_count].reconnect = conf.cf_tardisc;
1140 setup[setup_count].parity = !conf.cf_parity;
1141 setup[setup_count].synchronous = conf.cf_syncneg;
1142 setup[setup_count].delay = DELAY_DEFAULT;
1143 setup[setup_count].ext_trans = 0;
1144 #if defined(AHA152X_DEBUG)
1145 setup[setup_count].debug = DEBUG_DEFAULT;
1146 #endif
1147 setup_count++;
1151 if (ok)
1152 printk("auto configuration: ok, ");
1154 #endif
1156 printk("detected %d controller(s)\n", setup_count);
1158 for (i=0; i<setup_count; i++) {
1159 struct Scsi_Host *shpnt;
1161 aha152x_host[setup[i].irq - IRQ_MIN] = shpnt =
1162 scsi_register(tpnt, sizeof(struct aha152x_hostdata));
1164 if(!shpnt) {
1165 printk(KERN_ERR "aha152x: scsi_register failed\n");
1166 continue;
1169 registered_count++;
1171 shpnt->io_port = setup[i].io_port;
1172 shpnt->n_io_port = IO_RANGE;
1173 shpnt->irq = setup[i].irq;
1175 if(!setup[i].tc1550) {
1176 HOSTIOPORT0 = setup[i].io_port;
1177 HOSTIOPORT1 = setup[i].io_port;
1178 } else {
1179 HOSTIOPORT0 = setup[i].io_port+0x10;
1180 HOSTIOPORT1 = setup[i].io_port-0x10;
1183 ISSUE_SC = 0;
1184 CURRENT_SC = 0;
1185 DONE_SC = 0;
1186 DISCONNECTED_SC = 0;
1188 QLOCK = SPIN_LOCK_UNLOCKED;
1190 STATE = 0;
1191 PREVSTATE = 0;
1192 LASTSTATE = 0;
1194 MSGILEN = 0;
1195 MSGOLEN = 0;
1197 RECONNECT = setup[i].reconnect;
1198 SYNCHRONOUS = setup[i].synchronous;
1199 PARITY = setup[i].parity;
1200 DELAY = setup[i].delay;
1201 EXT_TRANS = setup[i].ext_trans;
1202 #if defined(AHA152X_DEBUG)
1203 HOSTDATA(shpnt)->debug = setup[i].debug;
1204 #endif
1205 HOSTDATA(shpnt)->in_intr = 0;
1206 HOSTDATA(shpnt)->commands = 0;
1208 #if defined(AHA152X_STAT)
1209 HOSTDATA(shpnt)->total_commands=0;
1210 HOSTDATA(shpnt)->disconnections=0;
1211 HOSTDATA(shpnt)->busfree_without_any_action=0;
1212 HOSTDATA(shpnt)->busfree_without_old_command=0;
1213 HOSTDATA(shpnt)->busfree_without_new_command=0;
1214 HOSTDATA(shpnt)->busfree_without_done_command=0;
1215 HOSTDATA(shpnt)->busfree_with_check_condition=0;
1216 for (j = idle; j<maxstate; j++) {
1217 HOSTDATA(shpnt)->count[j]=0;
1218 HOSTDATA(shpnt)->count_trans[j]=0;
1219 HOSTDATA(shpnt)->time[j]=0;
1221 #endif
1223 for (j = 0; j < 8; j++) {
1224 HOSTDATA(shpnt)->syncrate[j] = 0;
1225 HOSTDATA(shpnt)->syncneg[j] = 0;
1228 SETPORT(SCSIID, setup[i].scsiid << 4);
1229 shpnt->this_id = setup[i].scsiid;
1231 if (setup[i].reconnect)
1232 shpnt->can_queue = AHA152X_MAXQUEUE;
1234 #if 0
1235 if(!shpnt->hostt->use_new_eh_code) {
1236 #endif
1237 /* RESET OUT */
1238 printk("aha152x: resetting bus...\n");
1239 SETPORT(SCSISEQ, SCSIRSTO);
1240 mdelay(256);
1241 SETPORT(SCSISEQ, 0);
1242 mdelay(DELAY);
1243 #if 0
1245 #endif
1247 reset_ports(shpnt);
1249 printk(KERN_INFO
1250 "aha152x%d%s: "
1251 "vital data: rev=%x, "
1252 "io=0x%03lx (0x%03lx/0x%03lx), "
1253 "irq=%d, "
1254 "scsiid=%d, "
1255 "reconnect=%s, "
1256 "parity=%s, "
1257 "synchronous=%s, "
1258 "delay=%d, "
1259 "extended translation=%s\n",
1260 HOSTNO, setup[i].tc1550 ? " (tc1550 mode)" : "",
1261 GETPORT(REV) & 0x7,
1262 shpnt->io_port, HOSTIOPORT0, HOSTIOPORT1,
1263 shpnt->irq,
1264 shpnt->this_id,
1265 RECONNECT ? "enabled" : "disabled",
1266 PARITY ? "enabled" : "disabled",
1267 SYNCHRONOUS ? "enabled" : "disabled",
1268 DELAY,
1269 EXT_TRANS ? "enabled" : "disabled");
1271 request_region(shpnt->io_port, IO_RANGE, "aha152x");
1273 /* not expecting any interrupts */
1274 SETPORT(SIMODE0, 0);
1275 SETPORT(SIMODE1, 0);
1277 ok = request_irq(shpnt->irq, swintr, SA_INTERRUPT, "aha152x", shpnt);
1278 if (ok < 0) {
1279 if (ok==-EINVAL)
1280 printk(KERN_ERR "aha152x%d: bad IRQ %d.\n", HOSTNO, shpnt->irq);
1281 else if(ok==-EBUSY)
1282 printk(KERN_ERR "aha152x%d: IRQ %d already in use.\n", HOSTNO, shpnt->irq);
1283 else
1284 printk(KERN_ERR "aha152x%d: Unexpected error code %d on requesting IRQ %d.\n", HOSTNO, ok, shpnt->irq);
1286 printk(KERN_ERR "aha152x%d: driver needs an IRQ.\n", HOSTNO);
1288 scsi_unregister(shpnt);
1289 registered_count--;
1290 release_region(shpnt->io_port, IO_RANGE);
1291 aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1292 shpnt = 0;
1293 continue;
1295 HOSTDATA(shpnt)->swint = 0;
1297 printk(KERN_INFO "aha152x%d: trying software interrupt, ", HOSTNO);
1298 SETPORT(DMACNTRL0, SWINT|INTEN);
1299 spin_unlock_irq(&io_request_lock);
1300 mdelay(1000);
1301 spin_lock_irq(&io_request_lock);
1302 free_irq(shpnt->irq, shpnt);
1304 if (!HOSTDATA(shpnt)->swint) {
1305 if (TESTHI(DMASTAT, INTSTAT)) {
1306 printk("lost.\n");
1307 } else {
1308 printk("failed.\n");
1311 printk(KERN_ERR "aha152x%d: IRQ %d possibly wrong. Please verify.\n", HOSTNO, shpnt->irq);
1313 registered_count--;
1314 release_region(shpnt->io_port, IO_RANGE);
1315 aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1316 scsi_unregister(shpnt);
1317 shpnt=NULL;
1318 continue;
1320 printk("ok.\n");
1322 SETPORT(DMACNTRL0, INTEN);
1324 /* clear interrupts */
1325 SETPORT(SSTAT0, 0x7f);
1326 SETPORT(SSTAT1, 0xef);
1328 if (request_irq(shpnt->irq, intr, SA_INTERRUPT, "aha152x", shpnt) < 0) {
1329 printk(KERN_ERR "aha152x%d: failed to reassign interrupt.\n", HOSTNO);
1331 scsi_unregister(shpnt);
1332 registered_count--;
1333 release_region(shpnt->io_port, IO_RANGE);
1334 shpnt = aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1335 continue;
1339 return registered_count>0;
1343 int aha152x_release(struct Scsi_Host *shpnt)
1345 if (shpnt->irq)
1346 free_irq(shpnt->irq, shpnt);
1348 if (shpnt->io_port)
1349 release_region(shpnt->io_port, IO_RANGE);
1351 scsi_unregister(shpnt);
1353 return 0;
1357 * setup controller to generate interrupts depending
1358 * on current state (lock has to be acquired)
1361 static int setup_expected_interrupts(struct Scsi_Host *shpnt)
1363 ASSERT_LOCK(&QLOCK,1);
1365 if(CURRENT_SC) {
1366 CURRENT_SC->SCp.phase |= 1 << 16;
1368 if(CURRENT_SC->SCp.phase & selecting) {
1369 DPRINTK(debug_intr, DEBUG_LEAD "expecting: (seldo) (seltimo) (seldi)\n", CMDINFO(CURRENT_SC));
1370 SETPORT(SSTAT1, SELTO);
1371 SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1372 SETPORT(SIMODE1, ENSELTIMO);
1373 } else {
1374 DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (busfree) %s\n", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.phase & spiordy ? "(spiordy)" : "");
1375 SETPORT(SIMODE0, (CURRENT_SC->SCp.phase & spiordy) ? ENSPIORDY : 0);
1376 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1378 } else if(STATE==seldi) {
1379 DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (identify)\n", CMDINFO(CURRENT_SC));
1380 SETPORT(SIMODE0, 0);
1381 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1382 } else {
1383 DPRINTK(debug_intr, DEBUG_LEAD "expecting: %s %s\n",
1384 CMDINFO(CURRENT_SC),
1385 DISCONNECTED_SC ? "(reselection)" : "",
1386 ISSUE_SC ? "(busfree)" : "");
1387 SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1388 SETPORT(SIMODE1, ENSCSIRST | ( (ISSUE_SC||DONE_SC) ? ENBUSFREE : 0));
1391 if(!HOSTDATA(shpnt)->in_intr)
1392 SETBITS(DMACNTRL0, INTEN);
1394 return TESTHI(DMASTAT, INTSTAT);
1399 * Queue a command and setup interrupts for a free bus.
1401 int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct semaphore *sem, int phase, Scsi_Cmnd *done_SC, void (*done)(Scsi_Cmnd *))
1403 struct Scsi_Host *shpnt = SCpnt->host;
1404 unsigned long flags;
1406 #if defined(AHA152X_DEBUG)
1407 if (HOSTDATA(shpnt)->debug & debug_queue) {
1408 printk(INFO_LEAD "queue: cmd_len=%d pieces=%d size=%u cmnd=",
1409 CMDINFO(SCpnt), SCpnt->cmd_len, SCpnt->use_sg, SCpnt->request_bufflen);
1410 print_command(SCpnt->cmnd);
1412 #endif
1414 SCpnt->scsi_done = done;
1415 SCpnt->resid = SCpnt->request_bufflen;
1416 SCpnt->SCp.phase = not_issued | phase;
1417 SCpnt->SCp.Status = CHECK_CONDITION;
1418 SCpnt->SCp.Message = 0;
1419 SCpnt->SCp.have_data_in = 0;
1420 SCpnt->SCp.sent_command = 0;
1421 SCpnt->host_scribble = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
1422 if(!SCpnt->host_scribble) {
1423 printk(ERR_LEAD "allocation failed\n", CMDINFO(SCpnt));
1424 return FAILED;
1427 SCNEXT(SCpnt) = 0;
1428 SCDONE(SCpnt) = done_SC;
1429 SCSEM(SCpnt) = sem;
1431 /* setup scratch area
1432 SCp.ptr : buffer pointer
1433 SCp.this_residual : buffer length
1434 SCp.buffer : next buffer
1435 SCp.buffers_residual : left buffers in list
1436 SCp.phase : current state of the command */
1437 if (SCpnt->use_sg) {
1438 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
1439 SCpnt->SCp.ptr = SCpnt->SCp.buffer->address;
1440 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
1441 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
1442 } else {
1443 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
1444 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
1445 SCpnt->SCp.buffer = NULL;
1446 SCpnt->SCp.buffers_residual = 0;
1449 DO_LOCK(flags);
1451 #if defined(AHA152X_STAT)
1452 HOSTDATA(shpnt)->total_commands++;
1453 #endif
1455 /* Turn led on, when this is the first command. */
1456 HOSTDATA(shpnt)->commands++;
1457 if (HOSTDATA(shpnt)->commands==1)
1458 SETPORT(PORTA, 1);
1460 append_SC(&ISSUE_SC, SCpnt);
1462 if(!HOSTDATA(shpnt)->in_intr)
1463 setup_expected_interrupts(shpnt);
1465 DO_UNLOCK(flags);
1467 return 0;
1470 int aha152x_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1472 if(*SCpnt->cmnd == REQUEST_SENSE) {
1473 SCpnt->result = 0;
1474 done(SCpnt);
1476 return SUCCESS;
1479 return aha152x_internal_queue(SCpnt, 0, 0, 0, done);
1484 * run a command
1487 void internal_done(Scsi_Cmnd *SCpnt)
1489 #if 0
1490 struct Scsi_Host *shpnt = SCpnt->host;
1492 DPRINTK(debug_eh, INFO_LEAD "internal_done called\n", CMDINFO(SCpnt));
1493 #endif
1494 if(SCSEM(SCpnt))
1495 up(SCSEM(SCpnt));
1498 int aha152x_command(Scsi_Cmnd * SCpnt)
1500 DECLARE_MUTEX_LOCKED(sem);
1502 aha152x_internal_queue(SCpnt, &sem, 0, 0, internal_done);
1503 down(&sem);
1505 return SUCCESS;
1509 * Abort a command
1512 int aha152x_abort(Scsi_Cmnd *SCpnt)
1514 struct Scsi_Host *shpnt = SCpnt->host;
1515 Scsi_Cmnd *ptr;
1516 unsigned long flags;
1518 if(!shpnt) {
1519 printk(ERR_LEAD "abort(%p): no host structure\n", CMDINFO(SCpnt), SCpnt);
1520 return FAILED;
1523 #if defined(AHA152X_DEBUG)
1524 if(HOSTDATA(shpnt)->debug & debug_eh) {
1525 printk(DEBUG_LEAD "abort(%p)", CMDINFO(SCpnt), SCpnt);
1526 show_queues(shpnt);
1527 mdelay(1000);
1529 #endif
1531 DO_LOCK(flags);
1533 ptr=remove_SC(&ISSUE_SC, SCpnt);
1535 if(ptr) {
1536 DPRINTK(debug_eh, DEBUG_LEAD "not yet issued - SUCCESS\n", CMDINFO(SCpnt));
1538 HOSTDATA(shpnt)->commands--;
1539 if (!HOSTDATA(shpnt)->commands)
1540 SETPORT(PORTA, 0);
1541 DO_UNLOCK(flags);
1543 kfree(SCpnt->host_scribble);
1544 SCpnt->host_scribble=0;
1546 return SUCCESS;
1549 DO_UNLOCK(flags);
1552 * FIXME:
1553 * for current command: queue ABORT for message out and raise ATN
1554 * for disconnected command: pseudo SC with ABORT message or ABORT on reselection?
1558 printk(ERR_LEAD "cannot abort running or disconnected command\n", CMDINFO(SCpnt));
1560 return FAILED;
1563 static void timer_expired(unsigned long p)
1565 struct semaphore *sem = (void *)p;
1567 printk(KERN_INFO "aha152x: timer expired\n");
1568 up(sem);
1572 * Reset a device
1574 * FIXME: never seen this live. might lockup...
1577 int aha152x_device_reset(Scsi_Cmnd * SCpnt)
1579 struct Scsi_Host *shpnt = SCpnt->host;
1580 DECLARE_MUTEX_LOCKED(sem);
1581 struct timer_list timer;
1582 Scsi_Cmnd cmnd;
1584 #if defined(AHA152X_DEBUG)
1585 if(HOSTDATA(shpnt)->debug & debug_eh) {
1586 printk(INFO_LEAD "aha152x_device_reset(%p)", CMDINFO(SCpnt), SCpnt);
1587 show_queues(shpnt);
1588 mdelay(1000);
1590 #endif
1592 if(CURRENT_SC==SCpnt) {
1593 printk(ERR_LEAD "cannot reset current device\n", CMDINFO(SCpnt));
1594 return FAILED;
1597 cmnd.cmd_len = 0;
1598 cmnd.host = SCpnt->host;
1599 cmnd.target = SCpnt->target;
1600 cmnd.lun = SCpnt->lun;
1601 cmnd.use_sg = 0;
1602 cmnd.request_buffer = 0;
1603 cmnd.request_bufflen = 0;
1605 init_timer(&timer);
1606 timer.data = (unsigned long) &sem;
1607 timer.expires = jiffies + 10000; /* 10s */
1608 timer.function = (void (*)(unsigned long)) timer_expired;
1609 add_timer(&timer);
1611 aha152x_internal_queue(&cmnd, &sem, resetting, 0, internal_done);
1613 down(&sem);
1615 del_timer(&timer);
1617 if(cmnd.SCp.phase & resetted) {
1618 return SUCCESS;
1619 } else {
1620 return FAILED;
1624 void free_hard_reset_SCs(struct Scsi_Host *shpnt, Scsi_Cmnd **SCs)
1626 Scsi_Cmnd *ptr;
1627 unsigned long flags;
1629 DO_LOCK(flags);
1631 ptr=*SCs;
1632 while(ptr) {
1633 Scsi_Cmnd *next = SCNEXT(ptr);
1635 if (!ptr->device->soft_reset) {
1636 DPRINTK(debug_eh, DEBUG_LEAD "disconnected command %p removed\n", CMDINFO(ptr), ptr);
1637 remove_SC(SCs, ptr);
1638 HOSTDATA(shpnt)->commands--;
1639 kfree(ptr->host_scribble);
1640 ptr->host_scribble=0;
1643 ptr = next;
1646 DO_UNLOCK(flags);
1650 * Reset the bus
1653 int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
1655 struct Scsi_Host *shpnt = SCpnt->host;
1656 unsigned long flags;
1658 #if defined(AHA152X_DEBUG)
1659 if(HOSTDATA(shpnt)->debug & debug_eh) {
1660 printk(DEBUG_LEAD "aha152x_bus_reset(%p)", CMDINFO(SCpnt), SCpnt);
1661 show_queues(shpnt);
1662 mdelay(1000);
1664 #endif
1666 free_hard_reset_SCs(shpnt, &ISSUE_SC);
1667 free_hard_reset_SCs(shpnt, &DISCONNECTED_SC);
1669 DPRINTK(debug_eh, DEBUG_LEAD "resetting bus\n", CMDINFO(SCpnt));
1671 SETPORT(SCSISEQ, SCSIRSTO);
1672 mdelay(256);
1673 SETPORT(SCSISEQ, 0);
1674 mdelay(DELAY);
1676 DPRINTK(debug_eh, DEBUG_LEAD "bus reset returns\n", CMDINFO(SCpnt));
1678 DO_LOCK(flags);
1679 setup_expected_interrupts(shpnt);
1680 if(HOSTDATA(shpnt)->commands==0)
1681 SETPORT(PORTA, 0);
1682 DO_UNLOCK(flags);
1684 return SUCCESS;
1689 * Restore default values to the AIC-6260 registers and reset the fifos
1692 static void reset_ports(struct Scsi_Host *shpnt)
1694 unsigned long flags;
1696 /* disable interrupts */
1697 SETPORT(DMACNTRL0, RSTFIFO);
1699 SETPORT(SCSISEQ, 0);
1701 SETPORT(SXFRCTL1, 0);
1702 SETPORT(SCSISIG, 0);
1703 SETRATE(0);
1705 /* clear all interrupt conditions */
1706 SETPORT(SSTAT0, 0x7f);
1707 SETPORT(SSTAT1, 0xef);
1709 SETPORT(SSTAT4, SYNCERR | FWERR | FRERR);
1711 SETPORT(DMACNTRL0, 0);
1712 SETPORT(DMACNTRL1, 0);
1714 SETPORT(BRSTCNTRL, 0xf1);
1716 /* clear SCSI fifos and transfer count */
1717 SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1718 SETPORT(SXFRCTL0, CH1);
1720 DO_LOCK(flags);
1721 setup_expected_interrupts(shpnt);
1722 DO_UNLOCK(flags);
1726 * Reset the host (bus and controller)
1729 int aha152x_host_reset(Scsi_Cmnd * SCpnt)
1731 #if defined(AHA152X_DEBUG)
1732 struct Scsi_Host *shpnt = SCpnt->host;
1733 #endif
1735 DPRINTK(debug_eh, DEBUG_LEAD "aha152x_host_reset(%p)\n", CMDINFO(SCpnt), SCpnt);
1737 aha152x_bus_reset(SCpnt);
1739 DPRINTK(debug_eh, DEBUG_LEAD "resetting ports\n", CMDINFO(SCpnt));
1740 reset_ports(SCpnt->host);
1742 return SUCCESS;
1746 * Return the "logical geometry"
1749 int aha152x_biosparam(Scsi_Disk * disk, kdev_t dev, int *info_array)
1751 struct Scsi_Host *shpnt = disk->device->host;
1753 /* try default translation */
1754 info_array[0] = 64;
1755 info_array[1] = 32;
1756 info_array[2] = disk->capacity / (64 * 32);
1758 /* for disks >1GB do some guessing */
1759 if (info_array[2] >= 1024) {
1760 int info[3];
1762 /* try to figure out the geometry from the partition table */
1763 if (scsicam_bios_param(disk, dev, info) < 0 ||
1764 !((info[0] == 64 && info[1] == 32) || (info[0] == 255 && info[1] == 63))) {
1765 if (EXT_TRANS) {
1766 printk(KERN_NOTICE
1767 "aha152x: unable to verify geometry for disk with >1GB.\n"
1768 " using extended translation.\n");
1769 info_array[0] = 255;
1770 info_array[1] = 63;
1771 info_array[2] = disk->capacity / (255 * 63);
1772 } else {
1773 printk(KERN_NOTICE
1774 "aha152x: unable to verify geometry for disk with >1GB.\n"
1775 " Using default translation. Please verify yourself.\n"
1776 " Perhaps you need to enable extended translation in the driver.\n"
1777 " See /usr/src/linux/drivers/scsi/README.aha152x for details.\n");
1779 } else {
1780 info_array[0] = info[0];
1781 info_array[1] = info[1];
1782 info_array[2] = info[2];
1784 if (info[0] == 255 && !EXT_TRANS) {
1785 printk(KERN_NOTICE
1786 "aha152x: current partition table is using extended translation.\n"
1787 " using it also, although it's not explictly enabled.\n");
1792 return 0;
1796 * Internal done function
1799 static void done(struct Scsi_Host *shpnt, int error)
1801 if (CURRENT_SC) {
1802 if(DONE_SC)
1803 printk(ERR_LEAD "there's already a completed command %p - will cause abort\n", CMDINFO(CURRENT_SC), DONE_SC);
1805 DONE_SC = CURRENT_SC;
1806 CURRENT_SC = 0;
1807 DONE_SC->result = error;
1808 } else
1809 printk(KERN_ERR "aha152x: done() called outside of command\n");
1812 static struct tq_struct aha152x_tq;
1815 * Run service completions on the card with interrupts enabled.
1818 static void run(void)
1820 int i;
1821 for (i = 0; i < IRQS; i++) {
1822 struct Scsi_Host *shpnt = aha152x_host[i];
1823 if (shpnt && HOSTDATA(shpnt)->service) {
1824 HOSTDATA(shpnt)->service=0;
1825 complete(shpnt);
1831 * Interrupts handler
1835 static void intr(int irqno, void *dev_id, struct pt_regs *regs)
1837 struct Scsi_Host *shpnt = aha152x_host[irqno - IRQ_MIN];
1839 if (!shpnt) {
1840 printk(KERN_ERR "aha152x: catched interrupt for unknown controller.\n");
1841 return;
1844 /* no more interrupts from the controller, while we're busy.
1845 INTEN is restored by the BH handler */
1846 CLRBITS(DMACNTRL0, INTEN);
1848 #if 0
1849 /* check if there is already something to be
1850 serviced; should not happen */
1851 if(HOSTDATA(shpnt)->service) {
1852 printk(KERN_ERR "aha152x%d: lost interrupt (%d)\n", HOSTNO, HOSTDATA(shpnt)->service);
1853 show_queues(shpnt);
1855 #endif
1857 /* Poke the BH handler */
1858 HOSTDATA(shpnt)->service++;
1859 aha152x_tq.routine = (void *) run;
1860 queue_task(&aha152x_tq, &tq_immediate);
1861 mark_bh(IMMEDIATE_BH);
1865 * busfree phase
1866 * - handle completition/disconnection/error of current command
1867 * - start selection for next command (if any)
1869 static void busfree_run(struct Scsi_Host *shpnt)
1871 unsigned long flags;
1872 #if defined(AHA152X_STAT)
1873 int action=0;
1874 #endif
1876 SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1877 SETPORT(SXFRCTL0, CH1);
1879 SETPORT(SSTAT1, CLRBUSFREE);
1881 if(CURRENT_SC) {
1882 #if defined(AHA152X_STAT)
1883 action++;
1884 #endif
1885 CURRENT_SC->SCp.phase &= ~syncneg;
1887 if(CURRENT_SC->SCp.phase & completed) {
1888 /* target sent COMMAND COMPLETE */
1889 done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16));
1891 } else if(CURRENT_SC->SCp.phase & aborted) {
1892 DPRINTK(debug_eh, DEBUG_LEAD "ABORT sent\n", CMDINFO(CURRENT_SC));
1893 done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_ABORT << 16));
1895 } else if(CURRENT_SC->SCp.phase & resetted) {
1896 DPRINTK(debug_eh, DEBUG_LEAD "BUS DEVICE RESET sent\n", CMDINFO(CURRENT_SC));
1897 done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_RESET << 16));
1899 } else if(CURRENT_SC->SCp.phase & disconnected) {
1900 /* target sent DISCONNECT */
1901 DPRINTK(debug_selection, DEBUG_LEAD "target disconnected at %d/%d\n",
1902 CMDINFO(CURRENT_SC),
1903 CURRENT_SC->resid,
1904 CURRENT_SC->request_bufflen);
1905 #if defined(AHA152X_STAT)
1906 HOSTDATA(shpnt)->disconnections++;
1907 #endif
1908 append_SC(&DISCONNECTED_SC, CURRENT_SC);
1909 CURRENT_SC->SCp.phase |= 1 << 16;
1910 CURRENT_SC = 0;
1912 } else {
1913 done(shpnt, DID_ERROR << 16);
1915 #if defined(AHA152X_STAT)
1916 } else {
1917 HOSTDATA(shpnt)->busfree_without_old_command++;
1918 #endif
1921 DO_LOCK(flags);
1923 if(DONE_SC) {
1924 #if defined(AHA152X_STAT)
1925 action++;
1926 #endif
1927 if(SCDONE(DONE_SC)) {
1928 Scsi_Cmnd *ptr=DONE_SC;
1929 DONE_SC=SCDONE(DONE_SC);
1931 #if 0
1932 if(HOSTDATA(shpnt)->debug & debug_eh) {
1933 printk(ERR_LEAD "received sense: ", CMDINFO(ptr));
1934 print_sense("bh", DONE_SC);
1936 #endif
1938 HOSTDATA(shpnt)->commands--;
1939 if (!HOSTDATA(shpnt)->commands)
1940 SETPORT(PORTA, 0); /* turn led off */
1942 kfree(ptr->host_scribble);
1943 kfree(ptr);
1944 } else if(DONE_SC->SCp.Status==0x02) {
1945 #if defined(AHA152X_STAT)
1946 HOSTDATA(shpnt)->busfree_with_check_condition++;
1947 #endif
1948 #if 0
1949 DPRINTK(debug_eh, ERR_LEAD "CHECK CONDITION found\n", CMDINFO(DONE_SC));
1950 #endif
1952 if(!(DONE_SC->SCp.Status & not_issued)) {
1953 Scsi_Cmnd *cmnd = kmalloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
1955 if(cmnd) {
1956 Scsi_Cmnd *ptr=DONE_SC;
1957 DONE_SC=0;
1959 #if 0
1960 DPRINTK(debug_eh, ERR_LEAD "requesting sense\n", CMDINFO(ptr));
1961 #endif
1963 cmnd->cmnd[0] = REQUEST_SENSE;
1964 cmnd->cmnd[1] = 0;
1965 cmnd->cmnd[2] = 0;
1966 cmnd->cmnd[3] = 0;
1967 cmnd->cmnd[4] = sizeof(ptr->sense_buffer);
1968 cmnd->cmnd[5] = 0;
1969 cmnd->cmd_len = 6;
1970 cmnd->host = ptr->host;
1971 cmnd->target = ptr->target;
1972 cmnd->lun = ptr->lun;
1973 cmnd->use_sg = 0;
1974 cmnd->request_buffer = ptr->sense_buffer;
1975 cmnd->request_bufflen = sizeof(ptr->sense_buffer);
1977 DO_UNLOCK(flags);
1978 aha152x_internal_queue(cmnd, 0, 0, ptr, internal_done);
1979 DO_LOCK(flags);
1980 } else {
1981 printk(ERR_LEAD "allocation failed\n", CMDINFO(CURRENT_SC));
1982 if(cmnd)
1983 kfree(cmnd);
1985 } else {
1986 #if 0
1987 DPRINTK(debug_eh, ERR_LEAD "command not issued - CHECK CONDITION ignored\n", CMDINFO(DONE_SC));
1988 #endif
1992 if(DONE_SC && DONE_SC->scsi_done) {
1993 /* turn led off, when no commands are in the driver */
1994 HOSTDATA(shpnt)->commands--;
1995 if (!HOSTDATA(shpnt)->commands)
1996 SETPORT(PORTA, 0); /* turn led off */
1998 kfree(DONE_SC->host_scribble);
1999 DONE_SC->host_scribble=0;
2001 DO_UNLOCK(flags);
2002 DPRINTK(debug_done, DEBUG_LEAD "calling scsi_done(%p)\n", CMDINFO(DONE_SC), DONE_SC);
2003 DONE_SC->scsi_done(DONE_SC);
2004 DPRINTK(debug_done, DEBUG_LEAD "scsi_done(%p) returned\n", CMDINFO(DONE_SC), DONE_SC);
2005 DO_LOCK(flags);
2008 DONE_SC=0;
2009 #if defined(AHA152X_STAT)
2010 } else {
2011 HOSTDATA(shpnt)->busfree_without_done_command++;
2012 #endif
2015 if(ISSUE_SC)
2016 CURRENT_SC = remove_first_SC(&ISSUE_SC);
2018 DO_UNLOCK(flags);
2020 if(CURRENT_SC) {
2021 #if defined(AHA152X_STAT)
2022 action++;
2023 #endif
2024 CURRENT_SC->SCp.phase |= selecting;
2026 DPRINTK(debug_selection, DEBUG_LEAD "selecting target\n", CMDINFO(CURRENT_SC));
2028 /* clear selection timeout */
2029 SETPORT(SSTAT1, SELTO);
2031 SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
2032 SETPORT(SXFRCTL1, (PARITY ? ENSPCHK : 0 ) | ENSTIMER);
2033 SETPORT(SCSISEQ, ENSELO | ENAUTOATNO | (DISCONNECTED_SC ? ENRESELI : 0));
2034 } else {
2035 #if defined(AHA152X_STAT)
2036 HOSTDATA(shpnt)->busfree_without_new_command++;
2037 #endif
2038 SETPORT(SCSISEQ, DISCONNECTED_SC ? ENRESELI : 0);
2041 #if defined(AHA152X_STAT)
2042 if(!action)
2043 HOSTDATA(shpnt)->busfree_without_any_action++;
2044 #endif
2048 * Selection done (OUT)
2049 * - queue IDENTIFY message and SDTR to selected target for message out
2050 * (ATN asserted automagically via ENAUTOATNO in busfree())
2052 static void seldo_run(struct Scsi_Host *shpnt)
2054 SETPORT(SCSISIG, 0);
2055 SETPORT(SSTAT1, CLRBUSFREE);
2056 SETPORT(SSTAT1, CLRPHASECHG);
2058 CURRENT_SC->SCp.phase &= ~(selecting|not_issued);
2060 SETPORT(SCSISEQ, 0);
2062 if (TESTLO(SSTAT0, SELDO)) {
2063 printk(ERR_LEAD "aha152x: passing bus free condition\n", CMDINFO(CURRENT_SC));
2064 done(shpnt, DID_NO_CONNECT << 16);
2065 return;
2068 SETPORT(SSTAT0, CLRSELDO);
2070 ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->lun));
2072 if (CURRENT_SC->SCp.phase & aborting) {
2073 ADDMSGO(ABORT);
2074 } else if (CURRENT_SC->SCp.phase & resetting) {
2075 ADDMSGO(BUS_DEVICE_RESET);
2076 } else if (SYNCNEG==0 && SYNCHRONOUS) {
2077 CURRENT_SC->SCp.phase |= syncneg;
2078 ADDMSGO(EXTENDED_MESSAGE);
2079 ADDMSGO(3);
2080 ADDMSGO(EXTENDED_SDTR);
2081 ADDMSGO(50); /* 200ns */
2082 ADDMSGO(8); /* 8 byte req/ack offset */
2084 SYNCNEG=1; /* negotiation in progress */
2087 SETRATE(SYNCRATE);
2091 * Selection timeout
2092 * - return command to mid-level with failure cause
2095 static void selto_run(struct Scsi_Host *shpnt)
2097 SETPORT(SCSISEQ, 0);
2098 SETPORT(SSTAT1, CLRSELTIMO);
2100 DPRINTK(debug_selection, DEBUG_LEAD "selection timeout\n", CMDINFO(CURRENT_SC));
2102 if(!CURRENT_SC) {
2103 DPRINTK(debug_selection, DEBUG_LEAD "!CURRENT_SC\n", CMDINFO(CURRENT_SC));
2104 return;
2107 CURRENT_SC->SCp.phase &= ~selecting;
2109 if (CURRENT_SC->SCp.phase & aborted) {
2110 DPRINTK(debug_selection, DEBUG_LEAD "aborted\n", CMDINFO(CURRENT_SC));
2111 done(shpnt, DID_ABORT << 16);
2112 } else if (TESTLO(SSTAT0, SELINGO)) {
2113 DPRINTK(debug_selection, DEBUG_LEAD "arbitration not won\n", CMDINFO(CURRENT_SC));
2114 done(shpnt, DID_BUS_BUSY << 16);
2115 } else {
2116 /* ARBITRATION won, but SELECTION failed */
2117 DPRINTK(debug_selection, DEBUG_LEAD "selection failed\n", CMDINFO(CURRENT_SC));
2118 done(shpnt, DID_NO_CONNECT << 16);
2123 * Selection in done
2124 * - put current command back to issue queue
2125 * (reconnection of a disconnected nexus instead
2126 * of successful selection out)
2129 static void seldi_run(struct Scsi_Host *shpnt)
2131 int selid;
2132 int target;
2133 unsigned long flags;
2135 SETPORT(SCSISIG, 0);
2136 SETPORT(SSTAT0, CLRSELDI);
2137 SETPORT(SSTAT1, CLRBUSFREE);
2138 SETPORT(SSTAT1, CLRPHASECHG);
2140 if(CURRENT_SC) {
2141 if(!(CURRENT_SC->SCp.phase & not_issued))
2142 printk(ERR_LEAD "command should not have been issued yet\n", CMDINFO(CURRENT_SC));
2144 DPRINTK(debug_selection, ERR_LEAD "command requeued - reselection\n", CMDINFO(CURRENT_SC));
2146 DO_LOCK(flags);
2147 append_SC(&ISSUE_SC, CURRENT_SC);
2148 DO_UNLOCK(flags);
2150 CURRENT_SC = 0;
2153 if(!DISCONNECTED_SC) {
2154 DPRINTK(debug_selection, DEBUG_LEAD "unexpected SELDI ", CMDINFO(CURRENT_SC));
2155 return;
2158 RECONN_TARGET=-1;
2160 selid = GETPORT(SELID) & ~(1 << shpnt->this_id);
2162 if (selid==0) {
2163 printk("aha152x%d: target id unknown (%02x)\n", HOSTNO, selid);
2164 return;
2167 for(target=7; !(selid & (1 << target)); target--)
2170 if(selid & ~(1 << target)) {
2171 printk("aha152x%d: multiple targets reconnected (%02x)\n",
2172 HOSTNO, selid);
2176 SETPORT(SCSIID, (shpnt->this_id << OID_) | target);
2177 SETPORT(SCSISEQ, 0);
2179 SETRATE(HOSTDATA(shpnt)->syncrate[target]);
2181 RECONN_TARGET=target;
2182 DPRINTK(debug_selection, DEBUG_LEAD "target %d reselected (%02x).\n", CMDINFO(CURRENT_SC), target, selid);
2186 * message in phase
2187 * - handle initial message after reconnection to identify
2188 * reconnecting nexus
2189 * - queue command on DISCONNECTED_SC on DISCONNECT message
2190 * - set completed flag on COMMAND COMPLETE
2191 * (other completition code moved to busfree_run)
2192 * - handle response to SDTR
2193 * - clear synchronous transfer agreements on BUS RESET
2195 * FIXME: what about SAVE POINTERS, RESTORE POINTERS?
2198 static void msgi_run(struct Scsi_Host *shpnt)
2200 for(;;) {
2201 int sstat1 = GETPORT(SSTAT1);
2203 if(sstat1 & (PHASECHG|PHASEMIS|BUSFREE) || !(sstat1 & REQINIT))
2204 return;
2206 if(TESTLO(SSTAT0,SPIORDY)) {
2207 DPRINTK(debug_msgi, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2208 return;
2211 ADDMSGI(GETPORT(SCSIDAT));
2213 #if defined(AHA152X_DEBUG)
2214 if (HOSTDATA(shpnt)->debug & debug_msgi) {
2215 printk(INFO_LEAD "inbound message %02x ", CMDINFO(CURRENT_SC), MSGI(0));
2216 print_msg(&MSGI(0));
2217 printk("\n");
2219 #endif
2221 if(!CURRENT_SC) {
2222 if(LASTSTATE!=seldi) {
2223 printk(KERN_ERR "aha152x%d: message in w/o current command not after reselection\n", HOSTNO);
2227 * Handle reselection
2229 if(!(MSGI(0) & IDENTIFY_BASE)) {
2230 printk(KERN_ERR "aha152x%d: target didn't identify after reselection\n", HOSTNO);
2231 continue;
2234 CURRENT_SC = remove_lun_SC(&DISCONNECTED_SC, RECONN_TARGET, MSGI(0) & 0x3f);
2236 if (!CURRENT_SC) {
2237 show_queues(shpnt);
2238 printk(KERN_ERR "aha152x%d: no disconnected command for target %d/%d\n", HOSTNO, RECONN_TARGET, MSGI(0) & 0x3f);
2239 continue;
2242 DPRINTK(debug_msgi, DEBUG_LEAD "target reconnected\n", CMDINFO(CURRENT_SC));
2244 CURRENT_SC->SCp.Message = MSGI(0);
2245 CURRENT_SC->SCp.phase &= ~disconnected;
2247 MSGILEN=0;
2249 /* next message if any */
2250 continue;
2253 CURRENT_SC->SCp.Message = MSGI(0);
2255 switch (MSGI(0)) {
2256 case DISCONNECT:
2257 if (!RECONNECT)
2258 printk(WARN_LEAD "target was not allowed to disconnect\n", CMDINFO(CURRENT_SC));
2260 CURRENT_SC->SCp.phase |= disconnected;
2261 break;
2263 case COMMAND_COMPLETE:
2264 if(CURRENT_SC->SCp.phase & completed)
2265 DPRINTK(debug_msgi, DEBUG_LEAD "again COMMAND COMPLETE\n", CMDINFO(CURRENT_SC));
2267 CURRENT_SC->SCp.phase |= completed;
2268 break;
2270 case MESSAGE_REJECT:
2271 if (SYNCNEG==1) {
2272 printk(INFO_LEAD "Synchronous Data Transfer Request was rejected\n", CMDINFO(CURRENT_SC));
2273 SYNCNEG=2; /* negotiation completed */
2274 } else
2275 printk(INFO_LEAD "inbound message (MESSAGE REJECT)\n", CMDINFO(CURRENT_SC));
2276 break;
2278 case SAVE_POINTERS:
2279 break;
2281 case RESTORE_POINTERS:
2282 break;
2284 case EXTENDED_MESSAGE:
2285 if(MSGILEN<2 || MSGILEN<MSGI(1)+2) {
2286 /* not yet completed */
2287 continue;
2290 switch (MSGI(2)) {
2291 case EXTENDED_SDTR:
2293 long ticks;
2295 if (MSGI(1) != 3) {
2296 printk(ERR_LEAD "SDTR message length!=3\n", CMDINFO(CURRENT_SC));
2297 break;
2300 if (!HOSTDATA(shpnt)->synchronous)
2301 break;
2303 printk(INFO_LEAD, CMDINFO(CURRENT_SC));
2304 print_msg(&MSGI(0));
2305 printk("\n");
2307 ticks = (MSGI(3) * 4 + 49) / 50;
2309 if (syncneg) {
2310 /* negotiation in progress */
2311 if (ticks > 9 || MSGI(4) < 1 || MSGI(4) > 8) {
2312 ADDMSGO(MESSAGE_REJECT);
2313 printk(INFO_LEAD "received Synchronous Data Transfer Request invalid - rejected\n", CMDINFO(CURRENT_SC));
2314 break;
2317 SYNCRATE |= ((ticks - 2) << 4) + MSGI(4);
2318 } else if (ticks <= 9 && MSGI(4) >= 1) {
2319 ADDMSGO(EXTENDED_MESSAGE);
2320 ADDMSGO(3);
2321 ADDMSGO(EXTENDED_SDTR);
2322 if (ticks < 4) {
2323 ticks = 4;
2324 ADDMSGO(50);
2325 } else
2326 ADDMSGO(MSGI(3));
2328 if (MSGI(4) > 8)
2329 MSGI(4) = 8;
2331 ADDMSGO(MSGI(4));
2333 SYNCRATE |= ((ticks - 2) << 4) + MSGI(4);
2334 } else {
2335 /* requested SDTR is too slow, do it asynchronously */
2336 printk(INFO_LEAD "Synchronous Data Transfer Request too slow - Rejecting\n", CMDINFO(CURRENT_SC));
2337 ADDMSGO(MESSAGE_REJECT);
2340 SYNCNEG=2; /* negotiation completed */
2341 SETRATE(SYNCRATE);
2343 break;
2345 case BUS_DEVICE_RESET:
2347 int i;
2349 for(i=0; i<8; i++) {
2350 HOSTDATA(shpnt)->syncrate[i]=0;
2351 HOSTDATA(shpnt)->syncneg[i]=0;
2355 break;
2357 case EXTENDED_MODIFY_DATA_POINTER:
2358 case EXTENDED_EXTENDED_IDENTIFY:
2359 case EXTENDED_WDTR:
2360 default:
2361 ADDMSGO(MESSAGE_REJECT);
2362 break;
2364 break;
2367 MSGILEN=0;
2371 static void msgi_end(struct Scsi_Host *shpnt)
2373 if(MSGILEN>0)
2374 printk(WARN_LEAD "target left before message completed (%d)\n", CMDINFO(CURRENT_SC), MSGILEN);
2376 if (MSGOLEN > 0 && !(GETPORT(SSTAT1) & BUSFREE)) {
2377 DPRINTK(debug_msgi, DEBUG_LEAD "msgo pending\n", CMDINFO(CURRENT_SC));
2378 SETPORT(SCSISIG, P_MSGI | SIG_ATNO);
2383 * message out phase
2386 static void msgo_init(struct Scsi_Host *shpnt)
2388 if(MSGOLEN==0) {
2389 if((CURRENT_SC->SCp.phase & syncneg) && SYNCNEG==2 && SYNCRATE==0) {
2390 ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->lun));
2391 } else {
2392 printk(INFO_LEAD "unexpected MESSAGE OUT phase; rejecting\n", CMDINFO(CURRENT_SC));
2393 ADDMSGO(MESSAGE_REJECT);
2397 #if defined(AHA152X_DEBUG)
2398 if(HOSTDATA(shpnt)->debug & debug_msgo) {
2399 int i;
2401 printk(DEBUG_LEAD "messages( ", CMDINFO(CURRENT_SC));
2402 for (i=0; i<MSGOLEN; i+=print_msg(&MSGO(i)), printk(" "))
2404 printk(")\n");
2406 #endif
2410 * message out phase
2413 static void msgo_run(struct Scsi_Host *shpnt)
2415 if(MSGO_I==MSGOLEN)
2416 DPRINTK(debug_msgo, DEBUG_LEAD "messages all sent (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO_I, MSGOLEN);
2418 while(MSGO_I<MSGOLEN) {
2419 DPRINTK(debug_msgo, DEBUG_LEAD "message byte %02x (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO(MSGO_I), MSGO_I, MSGOLEN);
2421 if(TESTLO(SSTAT0, SPIORDY)) {
2422 DPRINTK(debug_msgo, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2423 return;
2426 if (MSGO_I==MSGOLEN-1) {
2427 /* Leave MESSAGE OUT after transfer */
2428 SETPORT(SSTAT1, CLRATNO);
2432 if (MSGO(MSGO_I) & IDENTIFY_BASE)
2433 CURRENT_SC->SCp.phase |= identified;
2435 if (MSGO(MSGO_I)==ABORT)
2436 CURRENT_SC->SCp.phase |= aborted;
2438 if (MSGO(MSGO_I)==BUS_DEVICE_RESET)
2439 CURRENT_SC->SCp.phase |= resetted;
2441 SETPORT(SCSIDAT, MSGO(MSGO_I++));
2445 static void msgo_end(struct Scsi_Host *shpnt)
2447 if(MSGO_I<MSGOLEN) {
2448 printk(ERR_LEAD "message sent incompletely (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO_I, MSGOLEN);
2449 if(SYNCNEG==1) {
2450 printk(INFO_LEAD "Synchronous Data Transfer Request was rejected\n", CMDINFO(CURRENT_SC));
2451 SYNCNEG=2;
2455 MSGO_I = 0;
2456 MSGOLEN = 0;
2460 * command phase
2463 static void cmd_init(struct Scsi_Host *shpnt)
2465 if (CURRENT_SC->SCp.sent_command) {
2466 printk(ERR_LEAD "command already sent\n", CMDINFO(CURRENT_SC));
2467 done(shpnt, DID_ERROR << 16);
2468 return;
2471 #if defined(AHA152X_DEBUG)
2472 if (HOSTDATA(shpnt)->debug & debug_cmd) {
2473 printk(DEBUG_LEAD "cmd_init: ", CMDINFO(CURRENT_SC));
2474 print_command(CURRENT_SC->cmnd);
2476 #endif
2478 CMD_I=0;
2482 * command phase
2485 static void cmd_run(struct Scsi_Host *shpnt)
2487 if(CMD_I==CURRENT_SC->cmd_len) {
2488 DPRINTK(debug_cmd, DEBUG_LEAD "command already completely sent (%d/%d)", CMDINFO(CURRENT_SC), CMD_I, CURRENT_SC->cmd_len);
2489 disp_ports(shpnt);
2492 while(CMD_I<CURRENT_SC->cmd_len) {
2493 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);
2495 if(TESTLO(SSTAT0, SPIORDY)) {
2496 DPRINTK(debug_cmd, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2497 return;
2500 SETPORT(SCSIDAT, CURRENT_SC->cmnd[CMD_I++]);
2504 static void cmd_end(struct Scsi_Host *shpnt)
2506 if(CMD_I<CURRENT_SC->cmd_len)
2507 printk(ERR_LEAD "command sent incompletely (%d/%d)\n", CMDINFO(CURRENT_SC), CMD_I, CURRENT_SC->cmd_len);
2508 else
2509 CURRENT_SC->SCp.sent_command++;
2513 * status phase
2516 static void status_run(struct Scsi_Host *shpnt)
2518 if(TESTLO(SSTAT0,SPIORDY)) {
2519 DPRINTK(debug_status, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2520 return;
2523 CURRENT_SC->SCp.Status = GETPORT(SCSIDAT);
2525 #if defined(AHA152X_DEBUG)
2526 if (HOSTDATA(shpnt)->debug & debug_status) {
2527 printk(DEBUG_LEAD "inbound status %02x ", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.Status);
2528 print_status(CURRENT_SC->SCp.Status);
2529 printk("\n");
2531 #endif
2535 * data in phase
2538 static void datai_init(struct Scsi_Host *shpnt)
2540 SETPORT(DMACNTRL0, RSTFIFO);
2541 SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
2543 SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2544 SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2546 SETPORT(SIMODE0, 0);
2547 SETPORT(SIMODE1, ENSCSIPERR | ENSCSIRST | ENPHASEMIS | ENBUSFREE);
2549 DATA_LEN=0;
2550 DPRINTK(debug_datai,
2551 DEBUG_LEAD "datai_init: request_bufflen=%d resid=%d\n",
2552 CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid);
2555 static void datai_run(struct Scsi_Host *shpnt)
2557 unsigned int the_time;
2558 int fifodata, data_count;
2561 * loop while the phase persists or the fifos are not empty
2564 while(TESTLO(DMASTAT, INTSTAT) || TESTLO(DMASTAT, DFIFOEMP) || TESTLO(SSTAT2, SEMPTY)) {
2565 /* FIXME: maybe this should be done by setting up
2566 * STCNT to trigger ENSWRAP interrupt, instead of
2567 * polling for DFIFOFULL
2569 the_time=jiffies + 10*HZ;
2570 while(TESTLO(DMASTAT, DFIFOFULL|INTSTAT) && time_before(jiffies,the_time))
2571 barrier();
2573 if(TESTLO(DMASTAT, DFIFOFULL|INTSTAT)) {
2574 printk(ERR_LEAD "datai timeout", CMDINFO(CURRENT_SC));
2575 disp_ports(shpnt);
2576 break;
2579 if(TESTHI(DMASTAT, DFIFOFULL)) {
2580 fifodata = 128;
2581 } else {
2582 the_time=jiffies + 100;
2583 while(TESTLO(SSTAT2, SEMPTY) && time_before(jiffies,the_time))
2584 barrier();
2586 if(TESTLO(SSTAT2, SEMPTY)) {
2587 printk(ERR_LEAD "datai sempty timeout", CMDINFO(CURRENT_SC));
2588 disp_ports(shpnt);
2589 break;
2592 fifodata = GETPORT(FIFOSTAT);
2595 if(CURRENT_SC->SCp.this_residual>0) {
2596 while(fifodata>0 && CURRENT_SC->SCp.this_residual>0) {
2597 data_count = fifodata>CURRENT_SC->SCp.this_residual ?
2598 CURRENT_SC->SCp.this_residual :
2599 fifodata;
2600 fifodata -= data_count;
2602 if(data_count & 1) {
2603 DPRINTK(debug_datai, DEBUG_LEAD "8bit\n", CMDINFO(CURRENT_SC));
2604 SETPORT(DMACNTRL0, ENDMA|_8BIT);
2605 *CURRENT_SC->SCp.ptr++ = GETPORT(DATAPORT);
2606 CURRENT_SC->SCp.this_residual--;
2607 DATA_LEN++;
2608 SETPORT(DMACNTRL0, ENDMA);
2611 if(data_count > 1) {
2612 DPRINTK(debug_datai, DEBUG_LEAD "16bit(%d)\n", CMDINFO(CURRENT_SC), data_count);
2613 data_count >>= 1;
2614 insw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2615 CURRENT_SC->SCp.ptr += 2 * data_count;
2616 CURRENT_SC->SCp.this_residual -= 2 * data_count;
2617 DATA_LEN += 2 * data_count;
2620 if(CURRENT_SC->SCp.this_residual==0 && CURRENT_SC->SCp.buffers_residual>0) {
2621 /* advance to next buffer */
2622 CURRENT_SC->SCp.buffers_residual--;
2623 CURRENT_SC->SCp.buffer++;
2624 CURRENT_SC->SCp.ptr = CURRENT_SC->SCp.buffer->address;
2625 CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2628 } else if(fifodata>0) {
2629 printk(ERR_LEAD "no buffers left for %d(%d) bytes (data overrun!?)\n", CMDINFO(CURRENT_SC), fifodata, GETPORT(FIFOSTAT));
2630 SETPORT(DMACNTRL0, ENDMA|_8BIT);
2631 while(fifodata>0) {
2632 int data;
2633 data=GETPORT(DATAPORT);
2634 DPRINTK(debug_datai, DEBUG_LEAD "data=%02x\n", CMDINFO(CURRENT_SC), data);
2635 fifodata--;
2636 DATA_LEN++;
2638 SETPORT(DMACNTRL0, ENDMA|_8BIT);
2642 if(TESTLO(DMASTAT, INTSTAT) ||
2643 TESTLO(DMASTAT, DFIFOEMP) ||
2644 TESTLO(SSTAT2, SEMPTY) ||
2645 GETPORT(FIFOSTAT)>0) {
2647 * something went wrong, if there's something left in the fifos
2648 * or the phase didn't change
2650 printk(ERR_LEAD "fifos should be empty and phase should have changed\n", CMDINFO(CURRENT_SC));
2651 disp_ports(shpnt);
2654 if(DATA_LEN!=GETSTCNT()) {
2655 printk(ERR_LEAD
2656 "manual transfer count differs from automatic (count=%d;stcnt=%d;diff=%d;fifostat=%d)",
2657 CMDINFO(CURRENT_SC), DATA_LEN, GETSTCNT(), GETSTCNT()-DATA_LEN, GETPORT(FIFOSTAT));
2658 disp_ports(shpnt);
2659 mdelay(10000);
2663 static void datai_end(struct Scsi_Host *shpnt)
2665 CURRENT_SC->resid -= GETSTCNT();
2667 DPRINTK(debug_datai,
2668 DEBUG_LEAD "datai_end: request_bufflen=%d resid=%d stcnt=%d\n",
2669 CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid, GETSTCNT());
2671 SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2672 SETPORT(DMACNTRL0, 0);
2676 * data out phase
2679 static void datao_init(struct Scsi_Host *shpnt)
2681 SETPORT(DMACNTRL0, WRITE_READ | RSTFIFO);
2682 SETPORT(DMACNTRL0, WRITE_READ | ENDMA);
2684 SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2685 SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2687 SETPORT(SIMODE0, 0);
2688 SETPORT(SIMODE1, ENSCSIPERR | ENSCSIRST | ENPHASEMIS | ENBUSFREE );
2690 DATA_LEN = CURRENT_SC->resid;
2692 DPRINTK(debug_datao,
2693 DEBUG_LEAD "datao_init: request_bufflen=%d; resid=%d\n",
2694 CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid);
2697 static void datao_run(struct Scsi_Host *shpnt)
2699 unsigned int the_time;
2700 int data_count;
2702 /* until phase changes or all data sent */
2703 while(TESTLO(DMASTAT, INTSTAT) && CURRENT_SC->SCp.this_residual>0) {
2704 data_count = 128;
2705 if(data_count > CURRENT_SC->SCp.this_residual)
2706 data_count=CURRENT_SC->SCp.this_residual;
2708 if(TESTLO(DMASTAT, DFIFOEMP)) {
2709 printk(ERR_LEAD "datao fifo not empty (%d)", CMDINFO(CURRENT_SC), GETPORT(FIFOSTAT));
2710 disp_ports(shpnt);
2711 break;
2714 if(data_count & 1) {
2715 SETPORT(DMACNTRL0,WRITE_READ|ENDMA|_8BIT);
2716 SETPORT(DATAPORT, *CURRENT_SC->SCp.ptr++);
2717 CURRENT_SC->SCp.this_residual--;
2718 CURRENT_SC->resid--;
2719 SETPORT(DMACNTRL0,WRITE_READ|ENDMA);
2722 if(data_count > 1) {
2723 data_count >>= 1;
2724 outsw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2725 CURRENT_SC->SCp.ptr += 2 * data_count;
2726 CURRENT_SC->SCp.this_residual -= 2 * data_count;
2727 CURRENT_SC->resid -= 2 * data_count;
2730 if(CURRENT_SC->SCp.this_residual==0 && CURRENT_SC->SCp.buffers_residual>0) {
2731 /* advance to next buffer */
2732 CURRENT_SC->SCp.buffers_residual--;
2733 CURRENT_SC->SCp.buffer++;
2734 CURRENT_SC->SCp.ptr = CURRENT_SC->SCp.buffer->address;
2735 CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2738 the_time=jiffies+10*HZ;
2739 while(TESTLO(DMASTAT, DFIFOEMP|INTSTAT) && time_before(jiffies,the_time))
2740 barrier();
2742 if(TESTLO(DMASTAT, DFIFOEMP|INTSTAT)) {
2743 printk(ERR_LEAD "dataout timeout", CMDINFO(CURRENT_SC));
2744 disp_ports(shpnt);
2745 break;
2750 static void datao_end(struct Scsi_Host *shpnt)
2752 if(TESTLO(DMASTAT, DFIFOEMP)) {
2753 int data_count = (DATA_LEN - CURRENT_SC->resid) - GETSTCNT();
2755 DPRINTK(debug_datao, DEBUG_LEAD "datao: %d bytes to resend (%d written, %d transfered)\n",
2756 CMDINFO(CURRENT_SC),
2757 data_count,
2758 DATA_LEN-CURRENT_SC->resid,
2759 GETSTCNT());
2761 CURRENT_SC->resid += data_count;
2763 if(CURRENT_SC->use_sg) {
2764 data_count -= CURRENT_SC->SCp.ptr - CURRENT_SC->SCp.buffer->address;
2765 while(data_count>0) {
2766 CURRENT_SC->SCp.buffer--;
2767 CURRENT_SC->SCp.buffers_residual++;
2768 data_count -= CURRENT_SC->SCp.buffer->length;
2770 CURRENT_SC->SCp.ptr = CURRENT_SC->SCp.buffer->address - data_count;
2771 CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length + data_count;
2772 } else {
2773 CURRENT_SC->SCp.ptr -= data_count;
2774 CURRENT_SC->SCp.this_residual += data_count;
2778 DPRINTK(debug_datao, DEBUG_LEAD "datao_end: request_bufflen=%d; resid=%d; stcnt=%d\n",
2779 CMDINFO(CURRENT_SC),
2780 CURRENT_SC->request_bufflen,
2781 CURRENT_SC->resid,
2782 GETSTCNT());
2784 SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
2785 SETPORT(SXFRCTL0, CH1);
2787 SETPORT(DMACNTRL0, 0);
2791 * figure out what state we're in
2794 static int update_state(struct Scsi_Host *shpnt)
2796 int dataphase=0;
2797 unsigned int stat0 = GETPORT(SSTAT0);
2798 unsigned int stat1 = GETPORT(SSTAT1);
2800 PREVSTATE = STATE;
2801 STATE=unknown;
2803 if(stat1 & SCSIRSTI) {
2804 STATE=rsti;
2805 SETPORT(SCSISEQ,0);
2806 SETPORT(SSTAT1,SCSIRSTI);
2807 } else if(stat0 & SELDI && PREVSTATE==busfree) {
2808 STATE=seldi;
2809 } else if(stat0 & SELDO && CURRENT_SC && (CURRENT_SC->SCp.phase & selecting)) {
2810 STATE=seldo;
2811 } else if(stat1 & SELTO) {
2812 STATE=selto;
2813 } else if(stat1 & BUSFREE) {
2814 STATE=busfree;
2815 SETPORT(SSTAT1,BUSFREE);
2816 } else if(stat1 & SCSIPERR) {
2817 STATE=parerr;
2818 SETPORT(SSTAT1,SCSIPERR);
2819 } else if(stat1 & REQINIT) {
2820 switch(GETPORT(SCSISIG) & P_MASK) {
2821 case P_MSGI: STATE=msgi; break;
2822 case P_MSGO: STATE=msgo; break;
2823 case P_DATAO: STATE=datao; break;
2824 case P_DATAI: STATE=datai; break;
2825 case P_STATUS: STATE=status; break;
2826 case P_CMD: STATE=cmd; break;
2828 dataphase=1;
2831 if((stat0 & SELDI) && STATE!=seldi && !dataphase) {
2832 printk(INFO_LEAD "reselection missed?", CMDINFO(CURRENT_SC));
2833 disp_ports(shpnt);
2836 if(STATE!=PREVSTATE) {
2837 LASTSTATE=PREVSTATE;
2840 return dataphase;
2844 * handle parity error
2846 * FIXME: in which phase?
2849 static void parerr_run(struct Scsi_Host *shpnt)
2851 printk(ERR_LEAD "parity error\n", CMDINFO(CURRENT_SC));
2852 done(shpnt, DID_PARITY << 16);
2856 * handle reset in
2859 static void rsti_run(struct Scsi_Host *shpnt)
2861 Scsi_Cmnd *ptr;
2863 printk(KERN_NOTICE "aha152x%d: scsi reset in\n", HOSTNO);
2865 ptr=DISCONNECTED_SC;
2866 while(ptr) {
2867 Scsi_Cmnd *next = SCNEXT(ptr);
2869 if (!ptr->device->soft_reset) {
2870 remove_SC(&DISCONNECTED_SC, ptr);
2872 kfree(ptr->host_scribble);
2873 ptr->host_scribble=0;
2875 ptr->result = DID_RESET << 16;
2876 ptr->scsi_done(ptr);
2879 ptr = next;
2882 if(CURRENT_SC && !CURRENT_SC->device->soft_reset)
2883 done(shpnt, DID_RESET << 16 );
2888 * bottom-half handler
2891 static void complete(struct Scsi_Host *shpnt)
2893 int dataphase;
2894 unsigned long flags;
2895 int pending;
2897 DO_LOCK(flags);
2898 if(HOSTDATA(shpnt)->in_intr!=0)
2899 aha152x_error(shpnt, "bottom-half already running!?");
2900 HOSTDATA(shpnt)->in_intr++;
2901 DO_UNLOCK(flags);
2904 * loop while there are interrupt conditions pending
2907 do {
2908 unsigned long start = jiffies;
2909 dataphase=update_state(shpnt);
2911 DPRINTK(debug_phases, LEAD "start %s %s(%s)\n", CMDINFO(CURRENT_SC), states[STATE].name, states[PREVSTATE].name, states[LASTSTATE].name);
2914 * end previous state
2917 if(PREVSTATE!=STATE && states[PREVSTATE].end)
2918 states[PREVSTATE].end(shpnt);
2921 * disable SPIO mode if previous phase used it
2922 * and this one doesn't
2925 if(states[PREVSTATE].spio && !states[STATE].spio) {
2926 SETPORT(SXFRCTL0, CH1);
2927 SETPORT(DMACNTRL0, 0);
2928 if(CURRENT_SC)
2929 CURRENT_SC->SCp.phase &= ~spiordy;
2933 * accept current dataphase phase
2936 if(dataphase) {
2937 SETPORT(SSTAT0, REQINIT);
2938 SETPORT(SCSISIG, GETPORT(SCSISIG) & P_MASK);
2939 SETPORT(SSTAT1, PHASECHG);
2943 * enable SPIO mode if previous didn't use it
2944 * and this one does
2947 if(!states[PREVSTATE].spio && states[STATE].spio) {
2948 SETPORT(DMACNTRL0, 0);
2949 SETPORT(SXFRCTL0, CH1|SPIOEN);
2950 if(CURRENT_SC)
2951 CURRENT_SC->SCp.phase |= spiordy;
2955 * initialize for new state
2958 if(PREVSTATE!=STATE && states[STATE].init)
2959 states[STATE].init(shpnt);
2962 * handle current state
2965 if(states[STATE].run)
2966 states[STATE].run(shpnt);
2967 else
2968 printk(ERR_LEAD "unexpected state (%x)\n", CMDINFO(CURRENT_SC), STATE);
2971 * setup controller to interrupt on
2972 * the next expected condition and
2973 * loop if it's already there
2976 DO_LOCK(flags);
2977 pending=setup_expected_interrupts(shpnt);
2978 #if defined(AHA152X_STAT)
2979 HOSTDATA(shpnt)->count[STATE]++;
2980 if(PREVSTATE!=STATE)
2981 HOSTDATA(shpnt)->count_trans[STATE]++;
2982 HOSTDATA(shpnt)->time[STATE] += jiffies-start;
2983 #endif
2984 DO_UNLOCK(flags);
2986 DPRINTK(debug_phases, LEAD "end %s %s(%s)\n", CMDINFO(CURRENT_SC), states[STATE].name, states[PREVSTATE].name, states[LASTSTATE].name);
2987 } while(pending);
2990 * enable interrupts and leave bottom-half
2993 DO_LOCK(flags);
2994 HOSTDATA(shpnt)->in_intr--;
2995 SETBITS(DMACNTRL0, INTEN);
2996 DO_UNLOCK(flags);
3001 * Dump the current driver status and panic
3003 static void aha152x_error(struct Scsi_Host *shpnt, char *msg)
3005 printk(KERN_EMERG "\naha152x%d: %s\n", HOSTNO, msg);
3006 show_queues(shpnt);
3007 panic("aha152x panic\n");
3011 * Display registers of AIC-6260
3013 static void disp_ports(struct Scsi_Host *shpnt)
3015 #if defined(AHA152X_DEBUG)
3016 int s;
3018 printk("\n%s: %s(%s) ",
3019 CURRENT_SC ? "busy" : "waiting",
3020 states[STATE].name,
3021 states[PREVSTATE].name);
3023 s = GETPORT(SCSISEQ);
3024 printk("SCSISEQ( ");
3025 if (s & TEMODEO)
3026 printk("TARGET MODE ");
3027 if (s & ENSELO)
3028 printk("SELO ");
3029 if (s & ENSELI)
3030 printk("SELI ");
3031 if (s & ENRESELI)
3032 printk("RESELI ");
3033 if (s & ENAUTOATNO)
3034 printk("AUTOATNO ");
3035 if (s & ENAUTOATNI)
3036 printk("AUTOATNI ");
3037 if (s & ENAUTOATNP)
3038 printk("AUTOATNP ");
3039 if (s & SCSIRSTO)
3040 printk("SCSIRSTO ");
3041 printk(");");
3043 printk(" SCSISIG(");
3044 s = GETPORT(SCSISIG);
3045 switch (s & P_MASK) {
3046 case P_DATAO:
3047 printk("DATA OUT");
3048 break;
3049 case P_DATAI:
3050 printk("DATA IN");
3051 break;
3052 case P_CMD:
3053 printk("COMMAND");
3054 break;
3055 case P_STATUS:
3056 printk("STATUS");
3057 break;
3058 case P_MSGO:
3059 printk("MESSAGE OUT");
3060 break;
3061 case P_MSGI:
3062 printk("MESSAGE IN");
3063 break;
3064 default:
3065 printk("*illegal*");
3066 break;
3069 printk("); ");
3071 printk("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3073 printk("SSTAT( ");
3074 s = GETPORT(SSTAT0);
3075 if (s & TARGET)
3076 printk("TARGET ");
3077 if (s & SELDO)
3078 printk("SELDO ");
3079 if (s & SELDI)
3080 printk("SELDI ");
3081 if (s & SELINGO)
3082 printk("SELINGO ");
3083 if (s & SWRAP)
3084 printk("SWRAP ");
3085 if (s & SDONE)
3086 printk("SDONE ");
3087 if (s & SPIORDY)
3088 printk("SPIORDY ");
3089 if (s & DMADONE)
3090 printk("DMADONE ");
3092 s = GETPORT(SSTAT1);
3093 if (s & SELTO)
3094 printk("SELTO ");
3095 if (s & ATNTARG)
3096 printk("ATNTARG ");
3097 if (s & SCSIRSTI)
3098 printk("SCSIRSTI ");
3099 if (s & PHASEMIS)
3100 printk("PHASEMIS ");
3101 if (s & BUSFREE)
3102 printk("BUSFREE ");
3103 if (s & SCSIPERR)
3104 printk("SCSIPERR ");
3105 if (s & PHASECHG)
3106 printk("PHASECHG ");
3107 if (s & REQINIT)
3108 printk("REQINIT ");
3109 printk("); ");
3112 printk("SSTAT( ");
3114 s = GETPORT(SSTAT0) & GETPORT(SIMODE0);
3116 if (s & TARGET)
3117 printk("TARGET ");
3118 if (s & SELDO)
3119 printk("SELDO ");
3120 if (s & SELDI)
3121 printk("SELDI ");
3122 if (s & SELINGO)
3123 printk("SELINGO ");
3124 if (s & SWRAP)
3125 printk("SWRAP ");
3126 if (s & SDONE)
3127 printk("SDONE ");
3128 if (s & SPIORDY)
3129 printk("SPIORDY ");
3130 if (s & DMADONE)
3131 printk("DMADONE ");
3133 s = GETPORT(SSTAT1) & GETPORT(SIMODE1);
3135 if (s & SELTO)
3136 printk("SELTO ");
3137 if (s & ATNTARG)
3138 printk("ATNTARG ");
3139 if (s & SCSIRSTI)
3140 printk("SCSIRSTI ");
3141 if (s & PHASEMIS)
3142 printk("PHASEMIS ");
3143 if (s & BUSFREE)
3144 printk("BUSFREE ");
3145 if (s & SCSIPERR)
3146 printk("SCSIPERR ");
3147 if (s & PHASECHG)
3148 printk("PHASECHG ");
3149 if (s & REQINIT)
3150 printk("REQINIT ");
3151 printk("); ");
3153 printk("SXFRCTL0( ");
3155 s = GETPORT(SXFRCTL0);
3156 if (s & SCSIEN)
3157 printk("SCSIEN ");
3158 if (s & DMAEN)
3159 printk("DMAEN ");
3160 if (s & CH1)
3161 printk("CH1 ");
3162 if (s & CLRSTCNT)
3163 printk("CLRSTCNT ");
3164 if (s & SPIOEN)
3165 printk("SPIOEN ");
3166 if (s & CLRCH1)
3167 printk("CLRCH1 ");
3168 printk("); ");
3170 printk("SIGNAL( ");
3172 s = GETPORT(SCSISIG);
3173 if (s & SIG_ATNI)
3174 printk("ATNI ");
3175 if (s & SIG_SELI)
3176 printk("SELI ");
3177 if (s & SIG_BSYI)
3178 printk("BSYI ");
3179 if (s & SIG_REQI)
3180 printk("REQI ");
3181 if (s & SIG_ACKI)
3182 printk("ACKI ");
3183 printk("); ");
3185 printk("SELID (%02x), ", GETPORT(SELID));
3187 printk("STCNT (%d), ", GETSTCNT());
3189 printk("SSTAT2( ");
3191 s = GETPORT(SSTAT2);
3192 if (s & SOFFSET)
3193 printk("SOFFSET ");
3194 if (s & SEMPTY)
3195 printk("SEMPTY ");
3196 if (s & SFULL)
3197 printk("SFULL ");
3198 printk("); SFCNT (%d); ", s & (SFULL | SFCNT));
3200 s = GETPORT(SSTAT3);
3201 printk("SCSICNT (%d), OFFCNT(%d), ", (s & 0xf0) >> 4, s & 0x0f);
3203 printk("SSTAT4( ");
3204 s = GETPORT(SSTAT4);
3205 if (s & SYNCERR)
3206 printk("SYNCERR ");
3207 if (s & FWERR)
3208 printk("FWERR ");
3209 if (s & FRERR)
3210 printk("FRERR ");
3211 printk("); ");
3213 printk("DMACNTRL0( ");
3214 s = GETPORT(DMACNTRL0);
3215 printk("%s ", s & _8BIT ? "8BIT" : "16BIT");
3216 printk("%s ", s & DMA ? "DMA" : "PIO");
3217 printk("%s ", s & WRITE_READ ? "WRITE" : "READ");
3218 if (s & ENDMA)
3219 printk("ENDMA ");
3220 if (s & INTEN)
3221 printk("INTEN ");
3222 if (s & RSTFIFO)
3223 printk("RSTFIFO ");
3224 if (s & SWINT)
3225 printk("SWINT ");
3226 printk("); ");
3228 printk("DMASTAT( ");
3229 s = GETPORT(DMASTAT);
3230 if (s & ATDONE)
3231 printk("ATDONE ");
3232 if (s & WORDRDY)
3233 printk("WORDRDY ");
3234 if (s & DFIFOFULL)
3235 printk("DFIFOFULL ");
3236 if (s & DFIFOEMP)
3237 printk("DFIFOEMP ");
3238 printk(")\n");
3239 #endif
3243 * display enabled interrupts
3245 static void disp_enintr(struct Scsi_Host *shpnt)
3247 int s;
3249 printk(KERN_DEBUG "enabled interrupts ( ");
3251 s = GETPORT(SIMODE0);
3252 if (s & ENSELDO)
3253 printk("ENSELDO ");
3254 if (s & ENSELDI)
3255 printk("ENSELDI ");
3256 if (s & ENSELINGO)
3257 printk("ENSELINGO ");
3258 if (s & ENSWRAP)
3259 printk("ENSWRAP ");
3260 if (s & ENSDONE)
3261 printk("ENSDONE ");
3262 if (s & ENSPIORDY)
3263 printk("ENSPIORDY ");
3264 if (s & ENDMADONE)
3265 printk("ENDMADONE ");
3267 s = GETPORT(SIMODE1);
3268 if (s & ENSELTIMO)
3269 printk("ENSELTIMO ");
3270 if (s & ENATNTARG)
3271 printk("ENATNTARG ");
3272 if (s & ENPHASEMIS)
3273 printk("ENPHASEMIS ");
3274 if (s & ENBUSFREE)
3275 printk("ENBUSFREE ");
3276 if (s & ENSCSIPERR)
3277 printk("ENSCSIPERR ");
3278 if (s & ENPHASECHG)
3279 printk("ENPHASECHG ");
3280 if (s & ENREQINIT)
3281 printk("ENREQINIT ");
3282 printk(")\n");
3286 * Show the command data of a command
3288 static void show_command(Scsi_Cmnd *ptr)
3290 printk(KERN_DEBUG "0x%08x: target=%d; lun=%d; cmnd=(",
3291 (unsigned int) ptr, ptr->target, ptr->lun);
3293 print_command(ptr->cmnd);
3295 printk(KERN_DEBUG "); request_bufflen=%d; resid=%d; phase |",
3296 ptr->request_bufflen, ptr->resid);
3298 if (ptr->SCp.phase & not_issued)
3299 printk("not issued|");
3300 if (ptr->SCp.phase & selecting)
3301 printk("selecting|");
3302 if (ptr->SCp.phase & identified)
3303 printk("identified|");
3304 if (ptr->SCp.phase & disconnected)
3305 printk("disconnected|");
3306 if (ptr->SCp.phase & completed)
3307 printk("completed|");
3308 if (ptr->SCp.phase & spiordy)
3309 printk("spiordy|");
3310 if (ptr->SCp.phase & syncneg)
3311 printk("syncneg|");
3312 if (ptr->SCp.phase & aborted)
3313 printk("aborted|");
3314 if (ptr->SCp.phase & resetted)
3315 printk("resetted|");
3316 printk("; next=0x%p\n", SCNEXT(ptr));
3320 * Dump the queued data
3322 static void show_queues(struct Scsi_Host *shpnt)
3324 Scsi_Cmnd *ptr;
3325 unsigned long flags;
3327 DO_LOCK(flags);
3328 printk(KERN_DEBUG "\nqueue status:\nissue_SC:\n");
3329 for (ptr = ISSUE_SC; ptr; ptr = SCNEXT(ptr))
3330 show_command(ptr);
3331 DO_UNLOCK(flags);
3333 printk(KERN_DEBUG "current_SC:\n");
3334 if (CURRENT_SC)
3335 show_command(CURRENT_SC);
3336 else
3337 printk(KERN_DEBUG "none\n");
3339 printk(KERN_DEBUG "disconnected_SC:\n");
3340 for (ptr = DISCONNECTED_SC; ptr; ptr = SCNEXT(ptr))
3341 show_command(ptr);
3343 disp_ports(shpnt);
3344 disp_enintr(shpnt);
3347 #undef SPRINTF
3348 #define SPRINTF(args...) pos += sprintf(pos, ## args)
3350 static int get_command(char *pos, Scsi_Cmnd * ptr)
3352 char *start = pos;
3353 int i;
3355 SPRINTF("0x%08x: target=%d; lun=%d; cmnd=( ",
3356 (unsigned int) ptr, ptr->target, ptr->lun);
3358 for (i = 0; i < COMMAND_SIZE(ptr->cmnd[0]); i++)
3359 SPRINTF("0x%02x ", ptr->cmnd[i]);
3361 SPRINTF("); resid=%d; residual=%d; buffers=%d; phase |",
3362 ptr->resid, ptr->SCp.this_residual, ptr->SCp.buffers_residual);
3364 if (ptr->SCp.phase & not_issued)
3365 SPRINTF("not issued|");
3366 if (ptr->SCp.phase & selecting)
3367 SPRINTF("selecting|");
3368 if (ptr->SCp.phase & disconnected)
3369 SPRINTF("disconnected|");
3370 if (ptr->SCp.phase & aborted)
3371 SPRINTF("aborted|");
3372 if (ptr->SCp.phase & identified)
3373 SPRINTF("identified|");
3374 if (ptr->SCp.phase & completed)
3375 SPRINTF("completed|");
3376 if (ptr->SCp.phase & spiordy)
3377 SPRINTF("spiordy|");
3378 if (ptr->SCp.phase & syncneg)
3379 SPRINTF("syncneg|");
3380 SPRINTF("; next=0x%p\n", SCNEXT(ptr));
3382 return (pos - start);
3385 static int get_ports(struct Scsi_Host *shpnt, char *pos)
3387 char *start = pos;
3388 int s;
3390 SPRINTF("\n%s: %s(%s) ", CURRENT_SC ? "on bus" : "waiting", states[STATE].name, states[PREVSTATE].name);
3392 s = GETPORT(SCSISEQ);
3393 SPRINTF("SCSISEQ( ");
3394 if (s & TEMODEO)
3395 SPRINTF("TARGET MODE ");
3396 if (s & ENSELO)
3397 SPRINTF("SELO ");
3398 if (s & ENSELI)
3399 SPRINTF("SELI ");
3400 if (s & ENRESELI)
3401 SPRINTF("RESELI ");
3402 if (s & ENAUTOATNO)
3403 SPRINTF("AUTOATNO ");
3404 if (s & ENAUTOATNI)
3405 SPRINTF("AUTOATNI ");
3406 if (s & ENAUTOATNP)
3407 SPRINTF("AUTOATNP ");
3408 if (s & SCSIRSTO)
3409 SPRINTF("SCSIRSTO ");
3410 SPRINTF(");");
3412 SPRINTF(" SCSISIG(");
3413 s = GETPORT(SCSISIG);
3414 switch (s & P_MASK) {
3415 case P_DATAO:
3416 SPRINTF("DATA OUT");
3417 break;
3418 case P_DATAI:
3419 SPRINTF("DATA IN");
3420 break;
3421 case P_CMD:
3422 SPRINTF("COMMAND");
3423 break;
3424 case P_STATUS:
3425 SPRINTF("STATUS");
3426 break;
3427 case P_MSGO:
3428 SPRINTF("MESSAGE OUT");
3429 break;
3430 case P_MSGI:
3431 SPRINTF("MESSAGE IN");
3432 break;
3433 default:
3434 SPRINTF("*illegal*");
3435 break;
3438 SPRINTF("); ");
3440 SPRINTF("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3442 SPRINTF("SSTAT( ");
3443 s = GETPORT(SSTAT0);
3444 if (s & TARGET)
3445 SPRINTF("TARGET ");
3446 if (s & SELDO)
3447 SPRINTF("SELDO ");
3448 if (s & SELDI)
3449 SPRINTF("SELDI ");
3450 if (s & SELINGO)
3451 SPRINTF("SELINGO ");
3452 if (s & SWRAP)
3453 SPRINTF("SWRAP ");
3454 if (s & SDONE)
3455 SPRINTF("SDONE ");
3456 if (s & SPIORDY)
3457 SPRINTF("SPIORDY ");
3458 if (s & DMADONE)
3459 SPRINTF("DMADONE ");
3461 s = GETPORT(SSTAT1);
3462 if (s & SELTO)
3463 SPRINTF("SELTO ");
3464 if (s & ATNTARG)
3465 SPRINTF("ATNTARG ");
3466 if (s & SCSIRSTI)
3467 SPRINTF("SCSIRSTI ");
3468 if (s & PHASEMIS)
3469 SPRINTF("PHASEMIS ");
3470 if (s & BUSFREE)
3471 SPRINTF("BUSFREE ");
3472 if (s & SCSIPERR)
3473 SPRINTF("SCSIPERR ");
3474 if (s & PHASECHG)
3475 SPRINTF("PHASECHG ");
3476 if (s & REQINIT)
3477 SPRINTF("REQINIT ");
3478 SPRINTF("); ");
3481 SPRINTF("SSTAT( ");
3483 s = GETPORT(SSTAT0) & GETPORT(SIMODE0);
3485 if (s & TARGET)
3486 SPRINTF("TARGET ");
3487 if (s & SELDO)
3488 SPRINTF("SELDO ");
3489 if (s & SELDI)
3490 SPRINTF("SELDI ");
3491 if (s & SELINGO)
3492 SPRINTF("SELINGO ");
3493 if (s & SWRAP)
3494 SPRINTF("SWRAP ");
3495 if (s & SDONE)
3496 SPRINTF("SDONE ");
3497 if (s & SPIORDY)
3498 SPRINTF("SPIORDY ");
3499 if (s & DMADONE)
3500 SPRINTF("DMADONE ");
3502 s = GETPORT(SSTAT1) & GETPORT(SIMODE1);
3504 if (s & SELTO)
3505 SPRINTF("SELTO ");
3506 if (s & ATNTARG)
3507 SPRINTF("ATNTARG ");
3508 if (s & SCSIRSTI)
3509 SPRINTF("SCSIRSTI ");
3510 if (s & PHASEMIS)
3511 SPRINTF("PHASEMIS ");
3512 if (s & BUSFREE)
3513 SPRINTF("BUSFREE ");
3514 if (s & SCSIPERR)
3515 SPRINTF("SCSIPERR ");
3516 if (s & PHASECHG)
3517 SPRINTF("PHASECHG ");
3518 if (s & REQINIT)
3519 SPRINTF("REQINIT ");
3520 SPRINTF("); ");
3522 SPRINTF("SXFRCTL0( ");
3524 s = GETPORT(SXFRCTL0);
3525 if (s & SCSIEN)
3526 SPRINTF("SCSIEN ");
3527 if (s & DMAEN)
3528 SPRINTF("DMAEN ");
3529 if (s & CH1)
3530 SPRINTF("CH1 ");
3531 if (s & CLRSTCNT)
3532 SPRINTF("CLRSTCNT ");
3533 if (s & SPIOEN)
3534 SPRINTF("SPIOEN ");
3535 if (s & CLRCH1)
3536 SPRINTF("CLRCH1 ");
3537 SPRINTF("); ");
3539 SPRINTF("SIGNAL( ");
3541 s = GETPORT(SCSISIG);
3542 if (s & SIG_ATNI)
3543 SPRINTF("ATNI ");
3544 if (s & SIG_SELI)
3545 SPRINTF("SELI ");
3546 if (s & SIG_BSYI)
3547 SPRINTF("BSYI ");
3548 if (s & SIG_REQI)
3549 SPRINTF("REQI ");
3550 if (s & SIG_ACKI)
3551 SPRINTF("ACKI ");
3552 SPRINTF("); ");
3554 SPRINTF("SELID(%02x), ", GETPORT(SELID));
3556 SPRINTF("STCNT(%d), ", GETSTCNT());
3558 SPRINTF("SSTAT2( ");
3560 s = GETPORT(SSTAT2);
3561 if (s & SOFFSET)
3562 SPRINTF("SOFFSET ");
3563 if (s & SEMPTY)
3564 SPRINTF("SEMPTY ");
3565 if (s & SFULL)
3566 SPRINTF("SFULL ");
3567 SPRINTF("); SFCNT (%d); ", s & (SFULL | SFCNT));
3569 s = GETPORT(SSTAT3);
3570 SPRINTF("SCSICNT (%d), OFFCNT(%d), ", (s & 0xf0) >> 4, s & 0x0f);
3572 SPRINTF("SSTAT4( ");
3573 s = GETPORT(SSTAT4);
3574 if (s & SYNCERR)
3575 SPRINTF("SYNCERR ");
3576 if (s & FWERR)
3577 SPRINTF("FWERR ");
3578 if (s & FRERR)
3579 SPRINTF("FRERR ");
3580 SPRINTF("); ");
3582 SPRINTF("DMACNTRL0( ");
3583 s = GETPORT(DMACNTRL0);
3584 SPRINTF("%s ", s & _8BIT ? "8BIT" : "16BIT");
3585 SPRINTF("%s ", s & DMA ? "DMA" : "PIO");
3586 SPRINTF("%s ", s & WRITE_READ ? "WRITE" : "READ");
3587 if (s & ENDMA)
3588 SPRINTF("ENDMA ");
3589 if (s & INTEN)
3590 SPRINTF("INTEN ");
3591 if (s & RSTFIFO)
3592 SPRINTF("RSTFIFO ");
3593 if (s & SWINT)
3594 SPRINTF("SWINT ");
3595 SPRINTF("); ");
3597 SPRINTF("DMASTAT( ");
3598 s = GETPORT(DMASTAT);
3599 if (s & ATDONE)
3600 SPRINTF("ATDONE ");
3601 if (s & WORDRDY)
3602 SPRINTF("WORDRDY ");
3603 if (s & DFIFOFULL)
3604 SPRINTF("DFIFOFULL ");
3605 if (s & DFIFOEMP)
3606 SPRINTF("DFIFOEMP ");
3607 SPRINTF(")\n");
3609 SPRINTF("enabled interrupts( ");
3611 s = GETPORT(SIMODE0);
3612 if (s & ENSELDO)
3613 SPRINTF("ENSELDO ");
3614 if (s & ENSELDI)
3615 SPRINTF("ENSELDI ");
3616 if (s & ENSELINGO)
3617 SPRINTF("ENSELINGO ");
3618 if (s & ENSWRAP)
3619 SPRINTF("ENSWRAP ");
3620 if (s & ENSDONE)
3621 SPRINTF("ENSDONE ");
3622 if (s & ENSPIORDY)
3623 SPRINTF("ENSPIORDY ");
3624 if (s & ENDMADONE)
3625 SPRINTF("ENDMADONE ");
3627 s = GETPORT(SIMODE1);
3628 if (s & ENSELTIMO)
3629 SPRINTF("ENSELTIMO ");
3630 if (s & ENATNTARG)
3631 SPRINTF("ENATNTARG ");
3632 if (s & ENPHASEMIS)
3633 SPRINTF("ENPHASEMIS ");
3634 if (s & ENBUSFREE)
3635 SPRINTF("ENBUSFREE ");
3636 if (s & ENSCSIPERR)
3637 SPRINTF("ENSCSIPERR ");
3638 if (s & ENPHASECHG)
3639 SPRINTF("ENPHASECHG ");
3640 if (s & ENREQINIT)
3641 SPRINTF("ENREQINIT ");
3642 SPRINTF(")\n");
3644 return (pos - start);
3647 int aha152x_set_info(char *buffer, int length, struct Scsi_Host *shpnt)
3649 if(!shpnt || !buffer || length<8 || strncmp("aha152x ", buffer, 8)!=0)
3650 return -EINVAL;
3652 #if defined(AHA152X_DEBUG)
3653 if(length>14 && strncmp("debug ", buffer+8, 6)==0) {
3654 int debug = HOSTDATA(shpnt)->debug;
3656 HOSTDATA(shpnt)->debug = simple_strtoul(buffer+14, NULL, 0);
3658 printk(KERN_INFO "aha152x%d: debugging options set to 0x%04x (were 0x%04x)\n", HOSTNO, HOSTDATA(shpnt)->debug, debug);
3659 } else
3660 #endif
3661 #if defined(AHA152X_STAT)
3662 if(length>13 && strncmp("reset", buffer+8, 5)==0) {
3663 int i;
3665 HOSTDATA(shpnt)->total_commands=0;
3666 HOSTDATA(shpnt)->disconnections=0;
3667 HOSTDATA(shpnt)->busfree_without_any_action=0;
3668 HOSTDATA(shpnt)->busfree_without_old_command=0;
3669 HOSTDATA(shpnt)->busfree_without_new_command=0;
3670 HOSTDATA(shpnt)->busfree_without_done_command=0;
3671 HOSTDATA(shpnt)->busfree_with_check_condition=0;
3672 for (i = idle; i<maxstate; i++) {
3673 HOSTDATA(shpnt)->count[i]=0;
3674 HOSTDATA(shpnt)->count_trans[i]=0;
3675 HOSTDATA(shpnt)->time[i]=0;
3678 printk(KERN_INFO "aha152x%d: stats reseted.\n", HOSTNO);
3680 } else
3681 #endif
3683 return -EINVAL;
3687 return length;
3690 #undef SPRINTF
3691 #define SPRINTF(args...) \
3692 do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
3694 int aha152x_proc_info(char *buffer, char **start,
3695 off_t offset, int length, int hostno, int inout)
3697 int i;
3698 char *pos = buffer;
3699 struct Scsi_Host *shpnt;
3700 Scsi_Cmnd *ptr;
3701 unsigned long flags;
3702 int thislength;
3704 for (i = 0, shpnt = (struct Scsi_Host *) NULL; i < IRQS; i++)
3705 if (aha152x_host[i] && aha152x_host[i]->host_no == hostno)
3706 shpnt = aha152x_host[i];
3708 if (!shpnt)
3709 return -ESRCH;
3711 DPRINTK(debug_procinfo,
3712 KERN_DEBUG "aha152x_proc_info: buffer=%p offset=%ld length=%d hostno=%d inout=%d\n",
3713 buffer, offset, length, hostno, inout);
3716 if (inout)
3717 return aha152x_set_info(buffer, length, shpnt);
3719 SPRINTF(AHA152X_REVID "\n");
3721 SPRINTF("ioports 0x%04lx to 0x%04lx\n",
3722 shpnt->io_port, shpnt->io_port + shpnt->n_io_port - 1);
3723 SPRINTF("interrupt 0x%02x\n", shpnt->irq);
3724 SPRINTF("disconnection/reconnection %s\n",
3725 RECONNECT ? "enabled" : "disabled");
3726 SPRINTF("parity checking %s\n",
3727 PARITY ? "enabled" : "disabled");
3728 SPRINTF("synchronous transfers %s\n",
3729 SYNCHRONOUS ? "enabled" : "disabled");
3730 SPRINTF("%d commands currently queued\n", HOSTDATA(shpnt)->commands);
3732 if(SYNCHRONOUS) {
3733 SPRINTF("synchronously operating targets (tick=50 ns):\n");
3734 for (i = 0; i < 8; i++)
3735 if (HOSTDATA(shpnt)->syncrate[i] & 0x7f)
3736 SPRINTF("target %d: period %dT/%dns; req/ack offset %d\n",
3738 (((HOSTDATA(shpnt)->syncrate[i] & 0x70) >> 4) + 2),
3739 (((HOSTDATA(shpnt)->syncrate[i] & 0x70) >> 4) + 2) * 50,
3740 HOSTDATA(shpnt)->syncrate[i] & 0x0f);
3742 #if defined(AHA152X_DEBUG)
3743 #define PDEBUG(flags,txt) \
3744 if(HOSTDATA(shpnt)->debug & flags) SPRINTF("(%s) ", txt);
3746 SPRINTF("enabled debugging options: ");
3748 PDEBUG(debug_procinfo, "procinfo");
3749 PDEBUG(debug_queue, "queue");
3750 PDEBUG(debug_intr, "interrupt");
3751 PDEBUG(debug_selection, "selection");
3752 PDEBUG(debug_msgo, "message out");
3753 PDEBUG(debug_msgi, "message in");
3754 PDEBUG(debug_status, "status");
3755 PDEBUG(debug_cmd, "command");
3756 PDEBUG(debug_datai, "data in");
3757 PDEBUG(debug_datao, "data out");
3758 PDEBUG(debug_eh, "eh");
3759 PDEBUG(debug_locks, "locks");
3760 PDEBUG(debug_phases, "phases");
3762 SPRINTF("\n");
3763 #endif
3765 SPRINTF("\nqueue status:\n");
3766 DO_LOCK(flags);
3767 if (ISSUE_SC) {
3768 SPRINTF("not yet issued commands:\n");
3769 for (ptr = ISSUE_SC; ptr; ptr = SCNEXT(ptr))
3770 pos += get_command(pos, ptr);
3771 } else
3772 SPRINTF("no not yet issued commands\n");
3773 DO_UNLOCK(flags);
3775 if (CURRENT_SC) {
3776 SPRINTF("current command:\n");
3777 pos += get_command(pos, CURRENT_SC);
3778 } else
3779 SPRINTF("no current command\n");
3781 if (DISCONNECTED_SC) {
3782 SPRINTF("disconnected commands:\n");
3783 for (ptr = DISCONNECTED_SC; ptr; ptr = SCNEXT(ptr))
3784 pos += get_command(pos, ptr);
3785 } else
3786 SPRINTF("no disconnected commands\n");
3788 pos += get_ports(shpnt, pos);
3790 #if defined(AHA152X_STAT)
3791 SPRINTF("statistics:\n"
3792 "total commands: %d\n"
3793 "disconnections: %d\n"
3794 "busfree with check condition: %d\n"
3795 "busfree without old command: %d\n"
3796 "busfree without new command: %d\n"
3797 "busfree without done command: %d\n"
3798 "busfree without any action: %d\n"
3799 "state "
3800 "transitions "
3801 "count "
3802 "time\n",
3803 HOSTDATA(shpnt)->total_commands,
3804 HOSTDATA(shpnt)->disconnections,
3805 HOSTDATA(shpnt)->busfree_with_check_condition,
3806 HOSTDATA(shpnt)->busfree_without_old_command,
3807 HOSTDATA(shpnt)->busfree_without_new_command,
3808 HOSTDATA(shpnt)->busfree_without_done_command,
3809 HOSTDATA(shpnt)->busfree_without_any_action);
3810 for(i=0; i<maxstate; i++) {
3811 SPRINTF("%-10s %-12d %-12d %-12ld\n",
3812 states[i].name,
3813 HOSTDATA(shpnt)->count_trans[i],
3814 HOSTDATA(shpnt)->count[i],
3815 HOSTDATA(shpnt)->time[i]);
3817 #endif
3819 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: pos=%p\n", pos);
3821 thislength = pos - (buffer + offset);
3822 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: length=%d thislength=%d\n", length, thislength);
3824 if(thislength<0) {
3825 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: output too short\n");
3826 *start = 0;
3827 return 0;
3830 thislength = thislength<length ? thislength : length;
3832 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: return %d\n", thislength);
3834 *start = buffer + offset;
3835 return thislength < length ? thislength : length;
3838 /* Eventually this will go into an include file, but this will be later */
3839 static Scsi_Host_Template driver_template = AHA152X;
3841 #include "scsi_module.c"