Linux 2.4.0-test7pre1
[davej-history.git] / drivers / scsi / aha152x.c
blob3f17ad86f1ac6ce930628d5cd1b8291498caf9e9
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.1 2000/05/17 16:23:17 fischer Exp fischer $
18 * $Log: aha152x.c,v $
19 * Revision 2.1 2000/05/17 16:23:17 fischer
20 * - signature update
21 * - fix for data out w/o scatter gather
23 * Revision 2.0 1999/12/25 15:07:32 fischer
24 * - interrupt routine completly reworked
25 * - basic support for new eh code
27 * Revision 1.21 1999/11/10 23:46:36 fischer
28 * - default to synchronous operation
29 * - synchronous negotiation fixed
30 * - added timeout to loops
31 * - debugging output can be controlled through procfs
33 * Revision 1.20 1999/11/07 18:37:31 fischer
34 * - synchronous operation works
35 * - resid support for sg driver
37 * Revision 1.19 1999/11/02 22:39:59 fischer
38 * - moved leading comments to README.aha152x
39 * - new additional module parameters
40 * - updates for 2.3
41 * - support for the Tripace TC1550 controller
42 * - interrupt handling changed
44 * Revision 1.18 1996/09/07 20:10:40 fischer
45 * - fixed can_queue handling (multiple outstanding commands working again)
47 * Revision 1.17 1996/08/17 16:05:14 fischer
48 * - biosparam improved
49 * - interrupt verification
50 * - updated documentation
51 * - cleanups
53 * Revision 1.16 1996/06/09 00:04:56 root
54 * - added configuration symbols for insmod (aha152x/aha152x1)
56 * Revision 1.15 1996/04/30 14:52:06 fischer
57 * - proc info fixed
58 * - support for extended translation for >1GB disks
60 * Revision 1.14 1996/01/17 15:11:20 fischer
61 * - fixed lockup in MESSAGE IN phase after reconnection
63 * Revision 1.13 1996/01/09 02:15:53 fischer
64 * - some cleanups
65 * - moved request_irq behind controller initialization
66 * (to avoid spurious interrupts)
68 * Revision 1.12 1995/12/16 12:26:07 fischer
69 * - barrier()s added
70 * - configurable RESET delay added
72 * Revision 1.11 1995/12/06 21:18:35 fischer
73 * - some minor updates
75 * Revision 1.10 1995/07/22 19:18:45 fischer
76 * - support for 2 controllers
77 * - started synchronous data transfers (not working yet)
79 * Revision 1.9 1995/03/18 09:20:24 root
80 * - patches for PCMCIA and modules
82 * Revision 1.8 1995/01/21 22:07:19 root
83 * - snarf_region => request_region
84 * - aha152x_intr interface change
86 * Revision 1.7 1995/01/02 23:19:36 root
87 * - updated COMMAND_SIZE to cmd_len
88 * - changed sti() to restore_flags()
89 * - fixed some #ifdef which generated warnings
91 * Revision 1.6 1994/11/24 20:35:27 root
92 * - problem with odd number of bytes in fifo fixed
94 * Revision 1.5 1994/10/30 14:39:56 root
95 * - abort code fixed
96 * - debugging improved
98 * Revision 1.4 1994/09/12 11:33:01 root
99 * - irqaction to request_irq
100 * - abortion updated
102 * Revision 1.3 1994/08/04 13:53:05 root
103 * - updates for mid-level-driver changes
104 * - accept unexpected BUSFREE phase as error condition
105 * - parity check now configurable
107 * Revision 1.2 1994/07/03 12:56:36 root
108 * - cleaned up debugging code
109 * - more tweaking on reset delays
110 * - updated abort/reset code (pretty untested...)
112 * Revision 1.1 1994/05/28 21:18:49 root
113 * - update for mid-level interface change (abort-reset)
114 * - delays after resets adjusted for some slow devices
116 * Revision 1.0 1994/03/25 12:52:00 root
117 * - Fixed "more data than expected" problem
118 * - added new BIOS signatures
120 * Revision 0.102 1994/01/31 20:44:12 root
121 * - minor changes in insw/outsw handling
123 * Revision 0.101 1993/12/13 01:16:27 root
124 * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
125 * fixes problems with CD-ROM sector size detection & media change)
127 * Revision 0.100 1993/12/10 16:58:47 root
128 * - fix for unsuccessful selections in case of non-continuous id assignments
129 * on the scsi bus.
131 * Revision 0.99 1993/10/24 16:19:59 root
132 * - fixed DATA IN (rare read errors gone)
134 * Revision 0.98 1993/10/17 12:54:44 root
135 * - fixed some recent fixes (shame on me)
136 * - moved initialization of scratch area to aha152x_queue
138 * Revision 0.97 1993/10/09 18:53:53 root
139 * - DATA IN fixed. Rarely left data in the fifo.
141 * Revision 0.96 1993/10/03 00:53:59 root
142 * - minor changes on DATA IN
144 * Revision 0.95 1993/09/24 10:36:01 root
145 * - change handling of MSGI after reselection
146 * - fixed sti/cli
147 * - minor changes
149 * Revision 0.94 1993/09/18 14:08:22 root
150 * - fixed bug in multiple outstanding command code
151 * - changed detection
152 * - support for kernel command line configuration
153 * - reset corrected
154 * - changed message handling
156 * Revision 0.93 1993/09/15 20:41:19 root
157 * - fixed bugs with multiple outstanding commands
159 * Revision 0.92 1993/09/13 02:46:33 root
160 * - multiple outstanding commands work (no problems with IBM drive)
162 * Revision 0.91 1993/09/12 20:51:46 root
163 * added multiple outstanding commands
164 * (some problem with this $%&? IBM device remain)
166 * Revision 0.9 1993/09/12 11:11:22 root
167 * - corrected auto-configuration
168 * - changed the auto-configuration (added some '#define's)
169 * - added support for dis-/reconnection
171 * Revision 0.8 1993/09/06 23:09:39 root
172 * - added support for the drive activity light
173 * - minor changes
175 * Revision 0.7 1993/09/05 14:30:15 root
176 * - improved phase detection
177 * - now using the new snarf_region code of 0.99pl13
179 * Revision 0.6 1993/09/02 11:01:38 root
180 * first public release; added some signatures and biosparam()
182 * Revision 0.5 1993/08/30 10:23:30 root
183 * fixed timing problems with my IBM drive
185 * Revision 0.4 1993/08/29 14:06:52 root
186 * fixed some problems with timeouts due incomplete commands
188 * Revision 0.3 1993/08/28 15:55:03 root
189 * writing data works too. mounted and worked on a dos partition
191 * Revision 0.2 1993/08/27 22:42:07 root
192 * reading data works. Mounted a msdos partition.
194 * Revision 0.1 1993/08/25 13:38:30 root
195 * first "damn thing doesn't work" version
197 * Revision 0.0 1993/08/14 19:54:25 root
198 * empty function bodies; detect() works.
201 **************************************************************************
203 see README.aha152x for configuration details
205 **************************************************************************/
207 #include <linux/module.h>
209 #if defined(PCMCIA)
210 #undef MODULE
211 #endif
213 #include <linux/sched.h>
214 #include <asm/io.h>
215 #include <linux/blk.h>
216 #include "scsi.h"
217 #include "sd.h"
218 #include "hosts.h"
219 #include "constants.h"
220 #include <asm/system.h>
221 #include <linux/errno.h>
222 #include <linux/string.h>
223 #include <linux/wait.h>
224 #include <linux/ioport.h>
225 #include <linux/delay.h>
226 #include <linux/proc_fs.h>
227 #include <linux/interrupt.h>
228 #include <linux/init.h>
229 #include <linux/kernel.h>
230 #include <asm/semaphore.h>
231 #include <linux/spinlock.h>
233 #include "aha152x.h"
234 #include <linux/stat.h>
236 #include <scsi/scsicam.h>
238 /* DEFINES */
240 /* For PCMCIA cards, always use AUTOCONF */
241 #if defined(PCMCIA) || defined(MODULE)
242 #if !defined(AUTOCONF)
243 #define AUTOCONF
244 #endif
245 #endif
247 #if !defined(AUTOCONF) && !defined(SETUP0)
248 #error define AUTOCONF or SETUP0
249 #endif
251 #if defined(AHA152X_DEBUG)
252 #define DEBUG_DEFAULT debug_eh
254 #define DPRINTK(when,msgs...) \
255 do { if(HOSTDATA(shpnt)->debug & (when)) printk(msgs); } while(0)
257 #define DO_LOCK(flags) \
258 do { \
259 if(QLOCK.lock) { \
260 DPRINTK(debug_intr, DEBUG_LEAD "(%s:%d) already locked at %s:%d\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__, QLOCKER, QLOCKERL); \
262 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) locking\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
263 spin_lock_irqsave(&QLOCK,flags); \
264 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) locked\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
265 QLOCKER=__FUNCTION__; \
266 QLOCKERL=__LINE__; \
267 } while(0)
269 #define DO_UNLOCK(flags) \
270 do { \
271 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) unlocking (locked at %s:%d)\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__, QLOCKER, QLOCKERL); \
272 spin_unlock_irqrestore(&QLOCK,flags); \
273 DPRINTK(debug_locks, DEBUG_LEAD "(%s:%d) unlocked\n", CMDINFO(CURRENT_SC), __FUNCTION__, __LINE__); \
274 QLOCKER="(not locked)"; \
275 QLOCKERL=0; \
276 } while(0)
278 #else
279 #define DPRINTK(when,msgs...)
280 #define DO_LOCK(flags) spin_lock_irqsave(&QLOCK,flags)
281 #define DO_UNLOCK(flags) spin_unlock_irqrestore(&QLOCK,flags)
282 #endif
284 #define LEAD "(scsi%d:%d:%d) "
285 #define WARN_LEAD KERN_WARNING LEAD
286 #define INFO_LEAD KERN_INFO LEAD
287 #define NOTE_LEAD KERN_NOTICE LEAD
288 #define ERR_LEAD KERN_ERR LEAD
289 #define DEBUG_LEAD KERN_DEBUG LEAD
290 #define CMDINFO(cmd) \
291 (cmd) ? ((cmd)->host->host_no) : -1, \
292 (cmd) ? ((cmd)->target & 0x0f) : -1, \
293 (cmd) ? ((cmd)->lun & 0x07) : -1
295 #define DELAY_DEFAULT 100
296 #define DEBUG_DEFAULT 0
298 /* possible irq range */
299 #if defined(PCMCIA)
300 #define IRQ_MIN 0
301 #define IRQ_MAX 16
302 #else
303 #define IRQ_MIN 9
304 #define IRQ_MAX 12
305 #endif
306 #define IRQS IRQ_MAX-IRQ_MIN+1
308 enum {
309 not_issued = 0x0001, /* command not yet issued */
310 selecting = 0x0002, /* target is beeing selected */
311 identified = 0x0004, /* IDENTIFY was sent */
312 disconnected = 0x0008, /* target disconnected */
313 completed = 0x0010, /* target sent COMMAND COMPLETE */
314 aborted = 0x0020, /* ABORT was sent */
315 resetted = 0x0040, /* BUS DEVICE RESET was sent */
316 spiordy = 0x0080, /* waiting for SPIORDY to raise */
317 syncneg = 0x0100, /* synchronous negotiation in progress */
318 aborting = 0x0200, /* ABORT is pending */
319 resetting = 0x0400, /* BUS DEVICE RESET is pending */
322 #if defined(MODULE)
323 MODULE_AUTHOR("Jürgen Fischer");
324 MODULE_DESCRIPTION(AHA152X_REVID);
325 MODULE_PARM(io, "1-2i");
326 MODULE_PARM_DESC(io,"base io address of controller");
327 static int io[] = {0, 0};
329 MODULE_PARM(irq, "1-2i");
330 MODULE_PARM_DESC(irq,"interrupt for controller");
331 static int irq[] = {0, 0};
333 MODULE_PARM(scsiid, "1-2i");
334 MODULE_PARM_DESC(scsiid,"scsi id of controller");
335 static int scsiid[] = {7, 7};
337 MODULE_PARM(reconnect, "1-2i");
338 MODULE_PARM_DESC(reconnect,"allow targets to disconnect");
339 static int reconnect[] = {1, 1};
341 MODULE_PARM(parity, "1-2i");
342 MODULE_PARM_DESC(parity,"use scsi parity");
343 static int parity[] = {1, 1};
345 MODULE_PARM(sync, "1-2i");
346 MODULE_PARM_DESC(sync,"use synchronous transfers");
347 static int sync[] = {1, 1};
349 MODULE_PARM(delay, "1-2i");
350 MODULE_PARM_DESC(delay,"scsi reset delay");
351 static int delay[] = {DELAY_DEFAULT, DELAY_DEFAULT};
353 MODULE_PARM(exttrans, "1-2i");
354 MODULE_PARM_DESC(exttrans,"use extended translation");
355 static int exttrans[] = {0, 0};
357 #if !defined(AHA152X_DEBUG)
358 MODULE_PARM(aha152x, "1-8i");
359 MODULE_PARM_DESC(aha152x, "parameters for first controller");
360 static int aha152x[] = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
362 MODULE_PARM(aha152x1, "1-8i");
363 MODULE_PARM_DESC(aha152x1, "parameters for second controller");
364 static int aha152x1[] = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
365 #else
366 MODULE_PARM(debug, "1-2i");
367 MODULE_PARM_DESC(debug, "flags for driver debugging");
368 static int debug[] = {DEBUG_DEFAULT, DEBUG_DEFAULT};
370 MODULE_PARM(aha152x, "1-9i");
371 MODULE_PARM_DESC(aha152x, "parameters for first controller");
372 static int aha152x[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
374 MODULE_PARM(aha152x1, "1-9i");
375 MODULE_PARM_DESC(aha152x1, "parameters for second controller");
376 static int aha152x1[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
377 #endif /* !defined(AHA152X_DEBUG) */
378 #endif /* MODULE */
380 /* set by aha152x_setup according to the command line */
381 static int setup_count = 0;
382 static int registered_count = 0;
383 static struct aha152x_setup {
384 int io_port;
385 int irq;
386 int scsiid;
387 int reconnect;
388 int parity;
389 int synchronous;
390 int delay;
391 int ext_trans;
392 int tc1550;
393 #if defined(AHA152X_DEBUG)
394 int debug;
395 #endif
396 char *conf;
397 } setup[2];
399 static struct Scsi_Host *aha152x_host[IRQS];
402 * internal states of the host
405 enum aha152x_state {
406 idle=0,
407 unknown,
408 seldo,
409 seldi,
410 selto,
411 busfree,
412 msgo,
413 cmd,
414 msgi,
415 status,
416 datai,
417 datao,
418 parerr,
419 rsti,
420 maxstate
424 * current state information of the host
427 struct aha152x_hostdata {
428 Scsi_Cmnd *issue_SC;
429 /* pending commands to issue */
431 Scsi_Cmnd *current_SC;
432 /* current command on the bus */
434 Scsi_Cmnd *disconnected_SC;
435 /* commands that disconnected */
437 Scsi_Cmnd *done_SC;
438 /* command that was completed */
440 spinlock_t lock;
441 /* host lock */
443 #if defined(AHA152X_DEBUG)
444 char *locker; /* which function has the lock */
445 int lockerl; /* where did it get it */
447 int debug; /* current debugging setting */
448 #endif
450 #if defined(AHA152X_STAT)
451 int total_commands;
452 int disconnections;
453 int busfree_without_any_action;
454 int busfree_without_old_command;
455 int busfree_without_new_command;
456 int busfree_without_done_command;
457 int busfree_with_check_condition;
458 int count[maxstate];
459 int count_trans[maxstate];
460 unsigned long time[maxstate];
461 #endif
463 int commands; /* current number of commands */
465 int reconnect; /* disconnection allowed */
466 int parity; /* parity checking enabled */
467 int synchronous; /* synchronous transferes enabled */
468 int delay; /* reset out delay */
469 int ext_trans; /* extended translation enabled */
471 int swint; /* software-interrupt was fired during detect() */
472 int service; /* bh needs to be run */
473 int in_intr; /* bh is running */
475 /* current state,
476 previous state,
477 last state different from current state */
478 enum aha152x_state state, prevstate, laststate;
480 int target;
481 /* reconnecting target */
483 unsigned char syncrate[8];
484 /* current synchronous transfer agreements */
486 unsigned char syncneg[8];
487 /* 0: no negotiation;
488 * 1: negotiation in progress;
489 * 2: negotiation completed
492 int cmd_i;
493 /* number of sent bytes of current command */
495 int msgi_len;
496 /* number of received message bytes */
497 unsigned char msgi[256];
498 /* received message bytes */
500 int msgo_i, msgo_len;
501 /* number of sent bytes and length of current messages */
502 unsigned char msgo[256];
503 /* pending messages */
505 int data_len;
506 /* number of sent/received bytes in dataphase */
508 unsigned long io_port0;
509 unsigned long io_port1;
514 * host specific command extension
517 struct aha152x_scdata {
518 Scsi_Cmnd *next; /* next sc in queue */
519 Scsi_Cmnd *done; /* done command */
520 struct semaphore *sem; /* semaphore to block on */
524 /* access macros for hostdata */
526 #define HOSTDATA(shpnt) ((struct aha152x_hostdata *) &shpnt->hostdata)
528 #define HOSTNO ((shpnt)->host_no)
530 #define CURRENT_SC (HOSTDATA(shpnt)->current_SC)
531 #define DONE_SC (HOSTDATA(shpnt)->done_SC)
532 #define ISSUE_SC (HOSTDATA(shpnt)->issue_SC)
533 #define DISCONNECTED_SC (HOSTDATA(shpnt)->disconnected_SC)
534 #define QLOCK (HOSTDATA(shpnt)->lock)
535 #define QLOCKER (HOSTDATA(shpnt)->locker)
536 #define QLOCKERL (HOSTDATA(shpnt)->lockerl)
538 #define STATE (HOSTDATA(shpnt)->state)
539 #define PREVSTATE (HOSTDATA(shpnt)->prevstate)
540 #define LASTSTATE (HOSTDATA(shpnt)->laststate)
542 #define RECONN_TARGET (HOSTDATA(shpnt)->target)
544 #define CMD_I (HOSTDATA(shpnt)->cmd_i)
546 #define MSGO(i) (HOSTDATA(shpnt)->msgo[i])
547 #define MSGO_I (HOSTDATA(shpnt)->msgo_i)
548 #define MSGOLEN (HOSTDATA(shpnt)->msgo_len)
549 #define ADDMSGO(x) (MSGOLEN<256 ? MSGO(MSGOLEN++)=x : aha152x_error(shpnt,"MSGO overflow"))
551 #define MSGI(i) (HOSTDATA(shpnt)->msgi[i])
552 #define MSGILEN (HOSTDATA(shpnt)->msgi_len)
553 #define ADDMSGI(x) (MSGILEN<256 ? MSGI(MSGILEN++)=x : aha152x_error(shpnt,"MSGI overflow"))
555 #define DATA_LEN (HOSTDATA(shpnt)->data_len)
557 #define SYNCRATE (HOSTDATA(shpnt)->syncrate[CURRENT_SC->target])
558 #define SYNCNEG (HOSTDATA(shpnt)->syncneg[CURRENT_SC->target])
560 #define DELAY (HOSTDATA(shpnt)->delay)
561 #define EXT_TRANS (HOSTDATA(shpnt)->ext_trans)
562 #define TC1550 (HOSTDATA(shpnt)->tc1550)
563 #define RECONNECT (HOSTDATA(shpnt)->reconnect)
564 #define PARITY (HOSTDATA(shpnt)->parity)
565 #define SYNCHRONOUS (HOSTDATA(shpnt)->synchronous)
567 #define HOSTIOPORT0 (HOSTDATA(shpnt)->io_port0)
568 #define HOSTIOPORT1 (HOSTDATA(shpnt)->io_port1)
570 #define SCDATA(SCpnt) ((struct aha152x_scdata *) (SCpnt)->host_scribble)
571 #define SCNEXT(SCpnt) SCDATA(SCpnt)->next
572 #define SCDONE(SCpnt) SCDATA(SCpnt)->done
573 #define SCSEM(SCpnt) SCDATA(SCpnt)->sem
576 /* state handling */
577 static void seldi_run(struct Scsi_Host *shpnt);
578 static void seldo_run(struct Scsi_Host *shpnt);
579 static void selto_run(struct Scsi_Host *shpnt);
580 static void busfree_run(struct Scsi_Host *shpnt);
582 static void msgo_init(struct Scsi_Host *shpnt);
583 static void msgo_run(struct Scsi_Host *shpnt);
584 static void msgo_end(struct Scsi_Host *shpnt);
586 static void cmd_init(struct Scsi_Host *shpnt);
587 static void cmd_run(struct Scsi_Host *shpnt);
588 static void cmd_end(struct Scsi_Host *shpnt);
590 static void datai_init(struct Scsi_Host *shpnt);
591 static void datai_run(struct Scsi_Host *shpnt);
592 static void datai_end(struct Scsi_Host *shpnt);
594 static void datao_init(struct Scsi_Host *shpnt);
595 static void datao_run(struct Scsi_Host *shpnt);
596 static void datao_end(struct Scsi_Host *shpnt);
598 static void status_run(struct Scsi_Host *shpnt);
600 static void msgi_run(struct Scsi_Host *shpnt);
601 static void msgi_end(struct Scsi_Host *shpnt);
603 static void parerr_run(struct Scsi_Host *shpnt);
604 static void rsti_run(struct Scsi_Host *shpnt);
606 static void complete(struct Scsi_Host *shpnt);
609 * driver states
612 static struct {
613 char *name;
614 void (*init)(struct Scsi_Host *);
615 void (*run)(struct Scsi_Host *);
616 void (*end)(struct Scsi_Host *);
617 int spio;
618 } states[] = {
619 { "idle", 0, 0, 0, 0},
620 { "unknown", 0, 0, 0, 0},
621 { "seldo", 0, seldo_run, 0, 0},
622 { "seldi", 0, seldi_run, 0, 0},
623 { "selto", 0, selto_run, 0, 0},
624 { "busfree", 0, busfree_run, 0, 0},
625 { "msgo", msgo_init, msgo_run, msgo_end, 1},
626 { "cmd", cmd_init, cmd_run, cmd_end, 1},
627 { "msgi", 0, msgi_run, msgi_end, 1},
628 { "status", 0, status_run, 0, 1},
629 { "datai", datai_init, datai_run, datai_end, 0},
630 { "datao", datao_init, datao_run, datao_end, 0},
631 { "parerr", 0, parerr_run, 0, 0},
632 { "rsti", 0, rsti_run, 0, 0},
635 /* setup & interrupt */
636 static void intr(int irq, void *dev_id, struct pt_regs *);
637 static void reset_ports(struct Scsi_Host *shpnt);
638 static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
639 static void done(struct Scsi_Host *shpnt, int error);
640 static int checksetup(struct aha152x_setup *setup);
642 /* diagnostics */
643 static void disp_ports(struct Scsi_Host *shpnt);
644 static void show_command(Scsi_Cmnd * ptr);
645 static void show_queues(struct Scsi_Host *shpnt);
646 static void disp_enintr(struct Scsi_Host *shpnt);
648 /* possible i/o addresses for the AIC-6260; default first */
649 static unsigned short ports[] = { 0x340, 0x140 };
650 #define PORT_COUNT (sizeof(ports) / sizeof(unsigned short))
652 #if !defined(SKIP_BIOSTEST)
653 /* possible locations for the Adaptec BIOS; defaults first */
654 static unsigned int addresses[] =
656 0xdc000, /* default first */
657 0xc8000,
658 0xcc000,
659 0xd0000,
660 0xd4000,
661 0xd8000,
662 0xe0000,
663 0xeb800, /* VTech Platinum SMP */
664 0xf0000,
666 #define ADDRESS_COUNT (sizeof(addresses) / sizeof(unsigned int))
668 /* signatures for various AIC-6[23]60 based controllers.
669 The point in detecting signatures is to avoid useless and maybe
670 harmful probes on ports. I'm not sure that all listed boards pass
671 auto-configuration. For those which fail the BIOS signature is
672 obsolete, because user intervention to supply the configuration is
673 needed anyway. May be an information whether or not the BIOS supports
674 extended translation could be also useful here. */
675 static struct signature {
676 unsigned char *signature;
677 int sig_offset;
678 int sig_length;
679 } signatures[] =
681 { "Adaptec AHA-1520 BIOS", 0x102e, 21 },
682 /* Adaptec 152x */
683 { "Adaptec AHA-1520B", 0x000b, 17 },
684 /* Adaptec 152x rev B */
685 { "Adaptec AHA-1520B", 0x0026, 17 },
686 /* Iomega Jaz Jet ISA (AIC6370Q) */
687 { "Adaptec ASW-B626 BIOS", 0x1029, 21 },
688 /* on-board controller */
689 { "Adaptec BIOS: ASW-B626", 0x000f, 22 },
690 /* on-board controller */
691 { "Adaptec ASW-B626 S2", 0x2e6c, 19 },
692 /* on-board controller */
693 { "Adaptec BIOS:AIC-6360", 0x000c, 21 },
694 /* on-board controller */
695 { "ScsiPro SP-360 BIOS", 0x2873, 19 },
696 /* ScsiPro-Controller */
697 { "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },
698 /* Gigabyte Local-Bus-SCSI */
699 { "Adaptec BIOS:AVA-282X", 0x000c, 21 },
700 /* Adaptec 282x */
701 { "Adaptec IBM Dock II SCSI", 0x2edd, 24 },
702 /* IBM Thinkpad Dock II */
703 { "Adaptec BIOS:AHA-1532P", 0x001c, 22 },
704 /* IBM Thinkpad Dock II SCSI */
705 { "DTC3520A Host Adapter BIOS", 0x318a, 26 },
706 /* DTC 3520A ISA SCSI */
709 #define SIGNATURE_COUNT (sizeof(signatures) / sizeof(struct signature))
710 #endif
714 * queue services:
717 static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
719 Scsi_Cmnd *end;
721 SCNEXT(new_SC) = NULL;
722 if (!*SC)
723 *SC = new_SC;
724 else {
725 for (end = *SC; SCNEXT(end); end = SCNEXT(end))
727 SCNEXT(end) = new_SC;
731 static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd ** SC)
733 Scsi_Cmnd *ptr;
735 ptr = *SC;
736 if (ptr) {
737 *SC = SCNEXT(*SC);
738 SCNEXT(ptr)=NULL;
740 return ptr;
743 static inline Scsi_Cmnd *remove_lun_SC(Scsi_Cmnd ** SC, int target, int lun)
745 Scsi_Cmnd *ptr, *prev;
747 for (ptr = *SC, prev = NULL;
748 ptr && ((ptr->target != target) || (ptr->lun != lun));
749 prev = ptr, ptr = SCNEXT(ptr))
752 if (ptr) {
753 if (prev)
754 SCNEXT(prev) = SCNEXT(ptr);
755 else
756 *SC = SCNEXT(ptr);
758 SCNEXT(ptr)=NULL;
761 return ptr;
764 static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, Scsi_Cmnd *SCp)
766 Scsi_Cmnd *ptr, *prev;
768 for (ptr = *SC, prev = NULL;
769 ptr && SCp!=ptr;
770 prev = ptr, ptr = SCNEXT(ptr))
773 if (ptr) {
774 if (prev)
775 SCNEXT(prev) = SCNEXT(ptr);
776 else
777 *SC = SCNEXT(ptr);
779 SCNEXT(ptr)=NULL;
782 return ptr;
785 #if defined(PCMCIA) || !defined(MODULE)
786 void aha152x_setup(char *str, int *ints)
788 if(setup_count>2) {
789 printk(KERN_ERR "aha152x: you can only configure up to two controllers\n");
790 return;
793 setup[setup_count].conf = str;
794 setup[setup_count].io_port = ints[0] >= 1 ? ints[1] : 0x340;
795 setup[setup_count].irq = ints[0] >= 2 ? ints[2] : 11;
796 setup[setup_count].scsiid = ints[0] >= 3 ? ints[3] : 7;
797 setup[setup_count].reconnect = ints[0] >= 4 ? ints[4] : 1;
798 setup[setup_count].parity = ints[0] >= 5 ? ints[5] : 1;
799 setup[setup_count].synchronous = ints[0] >= 6 ? ints[6] : 1;
800 setup[setup_count].delay = ints[0] >= 7 ? ints[7] : DELAY_DEFAULT;
801 setup[setup_count].ext_trans = ints[0] >= 8 ? ints[8] : 0;
802 #if defined(AHA152X_DEBUG)
803 setup[setup_count].debug = ints[0] >= 9 ? ints[9] : DEBUG_DEFAULT;
804 if (ints[0] > 9) {
805 printk(KERN_NOTICE "aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
806 "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>[,<DEBUG>]]]]]]]]\n");
807 #else
808 if (ints[0] > 8) { /*}*/
809 printk(KERN_NOTICE "aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
810 "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<EXT_TRANS>]]]]]]]\n");
811 #endif
812 return;
813 } else {
814 setup_count++;
815 return;
818 #endif
820 #if !defined(MODULE)
821 static int __init do_setup(char *str)
824 #if defined(AHA152X_DEBUG)
825 int ints[11];
826 #else
827 int ints[10];
828 #endif
829 int count=setup_count;
831 get_options(str, sizeof(ints)/sizeof(int), ints);
832 aha152x_setup(str,ints);
834 return count<setup_count;
837 __setup("aha152x=", do_setup);
838 #endif
841 * Test, if port_base is valid.
844 static int aha152x_porttest(int io_port)
846 int i;
848 if (check_region(io_port, IO_RANGE))
849 return 0;
851 SETPORT(io_port + O_DMACNTRL1, 0); /* reset stack pointer */
852 for (i = 0; i < 16; i++)
853 SETPORT(io_port + O_STACK, i);
855 SETPORT(io_port + O_DMACNTRL1, 0); /* reset stack pointer */
856 for (i = 0; i < 16 && GETPORT(io_port + O_STACK) == i; i++)
859 return (i == 16);
862 static int tc1550_porttest(int io_port)
864 int i;
866 if (check_region(io_port, IO_RANGE))
867 return 0;
869 SETPORT(io_port + O_TC_DMACNTRL1, 0); /* reset stack pointer */
870 for (i = 0; i < 16; i++)
871 SETPORT(io_port + O_STACK, i);
873 SETPORT(io_port + O_TC_DMACNTRL1, 0); /* reset stack pointer */
874 for (i = 0; i < 16 && GETPORT(io_port + O_TC_STACK) == i; i++)
877 return (i == 16);
880 static int checksetup(struct aha152x_setup *setup)
883 #if !defined(PCMCIA)
884 int i;
885 for (i = 0; i < PORT_COUNT && (setup->io_port != ports[i]); i++)
888 if (i == PORT_COUNT)
889 return 0;
890 #endif
892 if(aha152x_porttest(setup->io_port)) {
893 setup->tc1550=0;
894 } else if(tc1550_porttest(setup->io_port)) {
895 setup->tc1550=1;
896 } else
897 return 0;
899 if ((setup->irq < IRQ_MIN) || (setup->irq > IRQ_MAX))
900 return 0;
902 if ((setup->scsiid < 0) || (setup->scsiid > 7))
903 return 0;
905 if ((setup->reconnect < 0) || (setup->reconnect > 1))
906 return 0;
908 if ((setup->parity < 0) || (setup->parity > 1))
909 return 0;
911 if ((setup->synchronous < 0) || (setup->synchronous > 1))
912 return 0;
914 if ((setup->ext_trans < 0) || (setup->ext_trans > 1))
915 return 0;
918 return 1;
921 static void swintr(int irqno, void *dev_id, struct pt_regs *regs)
923 struct Scsi_Host *shpnt = aha152x_host[irqno - IRQ_MIN];
925 if (!shpnt)
926 printk(KERN_ERR "aha152x%d: catched software interrupt for unknown controller.\n", HOSTNO);
928 HOSTDATA(shpnt)->swint++;
932 int aha152x_detect(Scsi_Host_Template * tpnt)
934 int i, j, ok;
935 #if defined(AUTOCONF)
936 aha152x_config conf;
937 #endif
938 tpnt->proc_name = "aha152x";
940 for (i = 0; i < IRQS; i++)
941 aha152x_host[i] = (struct Scsi_Host *) NULL;
943 if (setup_count) {
944 printk(KERN_INFO "aha152x: processing commandline: ");
946 for (i = 0; i < setup_count; i++)
947 if (!checksetup(&setup[i])) {
948 printk(KERN_ERR "\naha152x: %s\n", setup[i].conf);
949 printk(KERN_ERR "aha152x: invalid line\n");
951 printk("ok\n");
953 #if defined(SETUP0)
954 if (setup_count < 2) {
955 struct aha152x_setup override = SETUP0;
957 if (setup_count == 0 || (override.io_port != setup[0].io_port))
958 if (!checksetup(&override)) {
959 printk(KERN_ERR "\naha152x: invalid override SETUP0={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
960 override.io_port,
961 override.irq,
962 override.scsiid,
963 override.reconnect,
964 override.parity,
965 override.synchronous,
966 override.delay,
967 override.ext_trans);
968 } else
969 setup[setup_count++] = override;
971 #endif
973 #if defined(SETUP1)
974 if (setup_count < 2) {
975 struct aha152x_setup override = SETUP1;
977 if (setup_count == 0 || (override.io_port != setup[0].io_port))
978 if (!checksetup(&override)) {
979 printk(KERN_ERR "\naha152x: invalid override SETUP1={0x%x,%d,%d,%d,%d,%d,%d,%d}\n",
980 override.io_port,
981 override.irq,
982 override.scsiid,
983 override.reconnect,
984 override.parity,
985 override.synchronous,
986 override.delay,
987 override.ext_trans);
988 } else
989 setup[setup_count++] = override;
991 #endif
993 #if defined(MODULE)
994 if (setup_count<2 && (aha152x[0]!=0 || io[0]!=0 || irq[0]!=0)) {
995 if(aha152x[0]!=0) {
996 setup[setup_count].conf = "";
997 setup[setup_count].io_port = aha152x[0];
998 setup[setup_count].irq = aha152x[1];
999 setup[setup_count].scsiid = aha152x[2];
1000 setup[setup_count].reconnect = aha152x[3];
1001 setup[setup_count].parity = aha152x[4];
1002 setup[setup_count].synchronous = aha152x[5];
1003 setup[setup_count].delay = aha152x[6];
1004 setup[setup_count].ext_trans = aha152x[7];
1005 #if defined(AHA152X_DEBUG)
1006 setup[setup_count].debug = aha152x[8];
1007 #endif
1008 } else if(io[0]!=0 || irq[0]!=0) {
1009 if(io[0]!=0) setup[setup_count].io_port = io[0];
1010 if(irq[0]!=0) setup[setup_count].irq = irq[0];
1012 setup[setup_count].scsiid = scsiid[0];
1013 setup[setup_count].reconnect = reconnect[0];
1014 setup[setup_count].parity = parity[0];
1015 setup[setup_count].synchronous = sync[0];
1016 setup[setup_count].delay = delay[0];
1017 setup[setup_count].ext_trans = exttrans[0];
1018 #if defined(AHA152X_DEBUG)
1019 setup[setup_count].debug = debug[0];
1020 #endif
1023 if (checksetup(&setup[setup_count]))
1024 setup_count++;
1025 else
1026 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",
1027 setup[setup_count].io_port,
1028 setup[setup_count].irq,
1029 setup[setup_count].scsiid,
1030 setup[setup_count].reconnect,
1031 setup[setup_count].parity,
1032 setup[setup_count].synchronous,
1033 setup[setup_count].delay,
1034 setup[setup_count].ext_trans);
1037 if (setup_count < 2 && (aha152x1[0]!=0 || io[1]!=0 || irq[1]!=0)) {
1038 if(aha152x1[0]!=0) {
1039 setup[setup_count].conf = "";
1040 setup[setup_count].io_port = aha152x1[0];
1041 setup[setup_count].irq = aha152x1[1];
1042 setup[setup_count].scsiid = aha152x1[2];
1043 setup[setup_count].reconnect = aha152x1[3];
1044 setup[setup_count].parity = aha152x1[4];
1045 setup[setup_count].synchronous = aha152x1[5];
1046 setup[setup_count].delay = aha152x1[6];
1047 setup[setup_count].ext_trans = aha152x1[7];
1048 #if defined(AHA152X_DEBUG)
1049 setup[setup_count].debug = aha152x1[8];
1050 #endif
1051 } else if(io[1]!=0 || irq[1]!=0) {
1052 if(io[1]!=0) setup[setup_count].io_port = io[1];
1053 if(irq[1]!=0) setup[setup_count].irq = irq[1];
1055 setup[setup_count].scsiid = scsiid[1];
1056 setup[setup_count].reconnect = reconnect[1];
1057 setup[setup_count].parity = parity[1];
1058 setup[setup_count].synchronous = sync[1];
1059 setup[setup_count].delay = delay[1];
1060 setup[setup_count].ext_trans = exttrans[1];
1061 #if defined(AHA152X_DEBUG)
1062 setup[setup_count].debug = debug[1];
1063 #endif
1065 if (checksetup(&setup[setup_count]))
1066 setup_count++;
1067 else
1068 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",
1069 setup[setup_count].io_port,
1070 setup[setup_count].irq,
1071 setup[setup_count].scsiid,
1072 setup[setup_count].reconnect,
1073 setup[setup_count].parity,
1074 setup[setup_count].synchronous,
1075 setup[setup_count].delay,
1076 setup[setup_count].ext_trans);
1078 #endif
1080 #if defined(AUTOCONF)
1081 if (setup_count < 2) {
1082 #if !defined(SKIP_BIOSTEST)
1083 ok = 0;
1084 for (i = 0; i < ADDRESS_COUNT && !ok; i++)
1085 for (j = 0; (j < SIGNATURE_COUNT) && !ok; j++)
1086 ok = isa_check_signature(addresses[i] + signatures[j].sig_offset,
1087 signatures[j].signature, signatures[j].sig_length);
1089 if (!ok && setup_count == 0)
1090 return 0;
1092 printk(KERN_INFO "aha152x: BIOS test: passed, ");
1093 #else
1094 printk(KERN_INFO "aha152x: ");
1095 #endif /* !SKIP_BIOSTEST */
1097 ok = 0;
1098 for (i = 0; i < PORT_COUNT && setup_count < 2; i++) {
1099 if ((setup_count == 1) && (setup[0].io_port == ports[i]))
1100 continue;
1102 if (aha152x_porttest(ports[i])) {
1103 ok++;
1104 setup[setup_count].io_port = ports[i];
1105 setup[setup_count].tc1550 = 0;
1107 conf.cf_port =
1108 (GETPORT(ports[i] + O_PORTA) << 8) + GETPORT(ports[i] + O_PORTB);
1110 setup[setup_count].irq = IRQ_MIN + conf.cf_irq;
1111 setup[setup_count].scsiid = conf.cf_id;
1112 setup[setup_count].reconnect = conf.cf_tardisc;
1113 setup[setup_count].parity = !conf.cf_parity;
1114 setup[setup_count].synchronous = conf.cf_syncneg;
1115 setup[setup_count].delay = DELAY_DEFAULT;
1116 setup[setup_count].ext_trans = 0;
1117 #if defined(AHA152X_DEBUG)
1118 setup[setup_count].debug = DEBUG_DEFAULT;
1119 #endif
1120 setup_count++;
1121 } else if (tc1550_porttest(ports[i])) {
1122 ok++;
1123 setup[setup_count].io_port = ports[i];
1124 setup[setup_count].tc1550 = 1;
1126 conf.cf_port =
1127 (GETPORT(ports[i] + O_PORTA) << 8) + GETPORT(ports[i] + O_PORTB);
1129 setup[setup_count].irq = IRQ_MIN + conf.cf_irq;
1130 setup[setup_count].scsiid = conf.cf_id;
1131 setup[setup_count].reconnect = conf.cf_tardisc;
1132 setup[setup_count].parity = !conf.cf_parity;
1133 setup[setup_count].synchronous = conf.cf_syncneg;
1134 setup[setup_count].delay = DELAY_DEFAULT;
1135 setup[setup_count].ext_trans = 0;
1136 #if defined(AHA152X_DEBUG)
1137 setup[setup_count].debug = DEBUG_DEFAULT;
1138 #endif
1139 setup_count++;
1143 if (ok)
1144 printk("auto configuration: ok, ");
1146 #endif
1148 printk("detected %d controller(s)\n", setup_count);
1150 for (i=0; i<setup_count; i++) {
1151 struct Scsi_Host *shpnt;
1153 aha152x_host[setup[i].irq - IRQ_MIN] = shpnt =
1154 scsi_register(tpnt, sizeof(struct aha152x_hostdata));
1156 if(!shpnt) {
1157 printk(KERN_ERR "aha152x: scsi_register failed\n");
1158 continue;
1161 registered_count++;
1163 shpnt->io_port = setup[i].io_port;
1164 shpnt->n_io_port = IO_RANGE;
1165 shpnt->irq = setup[i].irq;
1167 if(!setup[i].tc1550) {
1168 HOSTIOPORT0 = setup[i].io_port;
1169 HOSTIOPORT1 = setup[i].io_port;
1170 } else {
1171 HOSTIOPORT0 = setup[i].io_port+0x10;
1172 HOSTIOPORT1 = setup[i].io_port-0x10;
1175 ISSUE_SC = 0;
1176 CURRENT_SC = 0;
1177 DONE_SC = 0;
1178 DISCONNECTED_SC = 0;
1180 QLOCK = SPIN_LOCK_UNLOCKED;
1182 STATE = 0;
1183 PREVSTATE = 0;
1184 LASTSTATE = 0;
1186 MSGILEN = 0;
1187 MSGOLEN = 0;
1189 RECONNECT = setup[i].reconnect;
1190 SYNCHRONOUS = setup[i].synchronous;
1191 PARITY = setup[i].parity;
1192 DELAY = setup[i].delay;
1193 EXT_TRANS = setup[i].ext_trans;
1194 #if defined(AHA152X_DEBUG)
1195 HOSTDATA(shpnt)->debug = setup[i].debug;
1196 #endif
1197 HOSTDATA(shpnt)->in_intr = 0;
1198 HOSTDATA(shpnt)->commands = 0;
1200 #if defined(AHA152X_STAT)
1201 HOSTDATA(shpnt)->total_commands=0;
1202 HOSTDATA(shpnt)->disconnections=0;
1203 HOSTDATA(shpnt)->busfree_without_any_action=0;
1204 HOSTDATA(shpnt)->busfree_without_old_command=0;
1205 HOSTDATA(shpnt)->busfree_without_new_command=0;
1206 HOSTDATA(shpnt)->busfree_without_done_command=0;
1207 HOSTDATA(shpnt)->busfree_with_check_condition=0;
1208 for (j = idle; j<maxstate; j++) {
1209 HOSTDATA(shpnt)->count[j]=0;
1210 HOSTDATA(shpnt)->count_trans[j]=0;
1211 HOSTDATA(shpnt)->time[j]=0;
1213 #endif
1215 for (j = 0; j < 8; j++) {
1216 HOSTDATA(shpnt)->syncrate[j] = 0;
1217 HOSTDATA(shpnt)->syncneg[j] = 0;
1220 SETPORT(SCSIID, setup[i].scsiid << 4);
1221 shpnt->this_id = setup[i].scsiid;
1223 if (setup[i].reconnect)
1224 shpnt->can_queue = AHA152X_MAXQUEUE;
1226 #if 0
1227 if(!shpnt->hostt->use_new_eh_code) {
1228 #endif
1229 /* RESET OUT */
1230 printk("aha152x: resetting bus...\n");
1231 SETPORT(SCSISEQ, SCSIRSTO);
1232 mdelay(256);
1233 SETPORT(SCSISEQ, 0);
1234 mdelay(DELAY);
1235 #if 0
1237 #endif
1239 reset_ports(shpnt);
1241 printk(KERN_INFO
1242 "aha152x%d%s: "
1243 "vital data: rev=%x, "
1244 "io=0x%03lx (0x%03lx/0x%03lx), "
1245 "irq=%d, "
1246 "scsiid=%d, "
1247 "reconnect=%s, "
1248 "parity=%s, "
1249 "synchronous=%s, "
1250 "delay=%d, "
1251 "extended translation=%s\n",
1252 HOSTNO, setup[i].tc1550 ? " (tc1550 mode)" : "",
1253 GETPORT(REV) & 0x7,
1254 shpnt->io_port, HOSTIOPORT0, HOSTIOPORT1,
1255 shpnt->irq,
1256 shpnt->this_id,
1257 RECONNECT ? "enabled" : "disabled",
1258 PARITY ? "enabled" : "disabled",
1259 SYNCHRONOUS ? "enabled" : "disabled",
1260 DELAY,
1261 EXT_TRANS ? "enabled" : "disabled");
1263 request_region(shpnt->io_port, IO_RANGE, "aha152x");
1265 /* not expecting any interrupts */
1266 SETPORT(SIMODE0, 0);
1267 SETPORT(SIMODE1, 0);
1269 ok = request_irq(shpnt->irq, swintr, SA_INTERRUPT, "aha152x", shpnt);
1270 if (ok < 0) {
1271 if (ok==-EINVAL)
1272 printk(KERN_ERR "aha152x%d: bad IRQ %d.\n", HOSTNO, shpnt->irq);
1273 else if(ok==-EBUSY)
1274 printk(KERN_ERR "aha152x%d: IRQ %d already in use.\n", HOSTNO, shpnt->irq);
1275 else
1276 printk(KERN_ERR "aha152x%d: Unexpected error code %d on requesting IRQ %d.\n", HOSTNO, ok, shpnt->irq);
1278 printk(KERN_ERR "aha152x%d: driver needs an IRQ.\n", HOSTNO);
1280 scsi_unregister(shpnt);
1281 registered_count--;
1282 release_region(shpnt->io_port, IO_RANGE);
1283 aha152x_host[shpnt->irq - IRQ_MIN] = shpnt = 0;
1284 continue;
1286 HOSTDATA(shpnt)->swint = 0;
1288 printk(KERN_INFO "aha152x%d: trying software interrupt, ", HOSTNO);
1289 SETPORT(DMACNTRL0, SWINT|INTEN);
1290 spin_unlock_irq(&io_request_lock);
1291 mdelay(1000);
1292 spin_lock_irq(&io_request_lock);
1293 free_irq(shpnt->irq, shpnt);
1295 if (!HOSTDATA(shpnt)->swint) {
1296 if (TESTHI(DMASTAT, INTSTAT)) {
1297 printk("lost.\n");
1298 } else {
1299 printk("failed.\n");
1302 printk(KERN_ERR "aha152x%d: IRQ %d possibly wrong. Please verify.\n", HOSTNO, shpnt->irq);
1304 registered_count--;
1305 release_region(shpnt->io_port, IO_RANGE);
1306 aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1307 scsi_unregister(shpnt);
1308 shpnt=NULL;
1309 continue;
1311 printk("ok.\n");
1313 SETPORT(DMACNTRL0, INTEN);
1315 /* clear interrupts */
1316 SETPORT(SSTAT0, 0x7f);
1317 SETPORT(SSTAT1, 0xef);
1319 if (request_irq(shpnt->irq, intr, SA_INTERRUPT, "aha152x", shpnt) < 0) {
1320 printk(KERN_ERR "aha152x%d: failed to reassign interrupt.\n", HOSTNO);
1322 scsi_unregister(shpnt);
1323 registered_count--;
1324 release_region(shpnt->io_port, IO_RANGE);
1325 shpnt = aha152x_host[shpnt->irq - IRQ_MIN] = 0;
1326 continue;
1330 return registered_count>0;
1334 int aha152x_release(struct Scsi_Host *shpnt)
1336 if (shpnt->irq)
1337 free_irq(shpnt->irq, shpnt);
1339 if (shpnt->io_port)
1340 release_region(shpnt->io_port, IO_RANGE);
1342 scsi_unregister(shpnt);
1344 return 0;
1348 * setup controller to generate interrupts depending
1349 * on current state (lock has to be aquired)
1352 static int setup_expected_interrupts(struct Scsi_Host *shpnt)
1354 ASSERT_LOCK(&QLOCK,1);
1356 if(CURRENT_SC) {
1357 CURRENT_SC->SCp.phase |= 1 << 16;
1359 if(CURRENT_SC->SCp.phase & selecting) {
1360 DPRINTK(debug_intr, DEBUG_LEAD "expecting: (seldo) (seltimo) (seldi)\n", CMDINFO(CURRENT_SC));
1361 SETPORT(SSTAT1, SELTO);
1362 SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1363 SETPORT(SIMODE1, ENSELTIMO);
1364 } else {
1365 DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (busfree) %s\n", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.phase & spiordy ? "(spiordy)" : "");
1366 SETPORT(SIMODE0, (CURRENT_SC->SCp.phase & spiordy) ? ENSPIORDY : 0);
1367 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1369 } else if(STATE==seldi) {
1370 DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (identify)\n", CMDINFO(CURRENT_SC));
1371 SETPORT(SIMODE0, 0);
1372 SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
1373 } else {
1374 DPRINTK(debug_intr, DEBUG_LEAD "expecting: %s %s\n",
1375 CMDINFO(CURRENT_SC),
1376 DISCONNECTED_SC ? "(reselection)" : "",
1377 ISSUE_SC ? "(busfree)" : "");
1378 SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1379 SETPORT(SIMODE1, ENSCSIRST | ( (ISSUE_SC||DONE_SC) ? ENBUSFREE : 0));
1382 if(!HOSTDATA(shpnt)->in_intr)
1383 SETBITS(DMACNTRL0, INTEN);
1385 return TESTHI(DMASTAT, INTSTAT);
1390 * Queue a command and setup interrupts for a free bus.
1392 int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct semaphore *sem, int phase, Scsi_Cmnd *done_SC, void (*done)(Scsi_Cmnd *))
1394 struct Scsi_Host *shpnt = SCpnt->host;
1395 unsigned long flags;
1397 #if defined(AHA152X_DEBUG)
1398 if (HOSTDATA(shpnt)->debug & debug_queue) {
1399 printk(INFO_LEAD "queue: cmd_len=%d pieces=%d size=%u cmnd=",
1400 CMDINFO(SCpnt), SCpnt->cmd_len, SCpnt->use_sg, SCpnt->request_bufflen);
1401 print_command(SCpnt->cmnd);
1403 #endif
1405 SCpnt->scsi_done = done;
1406 SCpnt->resid = SCpnt->request_bufflen;
1407 SCpnt->SCp.phase = not_issued | phase;
1408 SCpnt->SCp.Status = CHECK_CONDITION;
1409 SCpnt->SCp.Message = 0;
1410 SCpnt->SCp.have_data_in = 0;
1411 SCpnt->SCp.sent_command = 0;
1412 SCpnt->host_scribble = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
1413 if(!SCpnt->host_scribble) {
1414 printk(ERR_LEAD "allocation failed\n", CMDINFO(SCpnt));
1415 return FAILED;
1418 SCNEXT(SCpnt) = 0;
1419 SCDONE(SCpnt) = done_SC;
1420 SCSEM(SCpnt) = sem;
1422 /* setup scratch area
1423 SCp.ptr : buffer pointer
1424 SCp.this_residual : buffer length
1425 SCp.buffer : next buffer
1426 SCp.buffers_residual : left buffers in list
1427 SCp.phase : current state of the command */
1428 if (SCpnt->use_sg) {
1429 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
1430 SCpnt->SCp.ptr = SCpnt->SCp.buffer->address;
1431 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
1432 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
1433 } else {
1434 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
1435 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
1436 SCpnt->SCp.buffer = NULL;
1437 SCpnt->SCp.buffers_residual = 0;
1440 DO_LOCK(flags);
1442 #if defined(AHA152X_STAT)
1443 HOSTDATA(shpnt)->total_commands++;
1444 #endif
1446 /* Turn led on, when this is the first command. */
1447 HOSTDATA(shpnt)->commands++;
1448 if (HOSTDATA(shpnt)->commands==1)
1449 SETPORT(PORTA, 1);
1451 append_SC(&ISSUE_SC, SCpnt);
1453 if(!HOSTDATA(shpnt)->in_intr)
1454 setup_expected_interrupts(shpnt);
1456 DO_UNLOCK(flags);
1458 return 0;
1461 int aha152x_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
1463 if(*SCpnt->cmnd == REQUEST_SENSE) {
1464 SCpnt->result = 0;
1465 done(SCpnt);
1467 return SUCCESS;
1470 return aha152x_internal_queue(SCpnt, 0, 0, 0, done);
1475 * run a command
1478 void internal_done(Scsi_Cmnd *SCpnt)
1480 #if 0
1481 struct Scsi_Host *shpnt = SCpnt->host;
1483 DPRINTK(debug_eh, INFO_LEAD "internal_done called\n", CMDINFO(SCpnt));
1484 #endif
1485 if(SCSEM(SCpnt))
1486 up(SCSEM(SCpnt));
1489 int aha152x_command(Scsi_Cmnd * SCpnt)
1491 DECLARE_MUTEX_LOCKED(sem);
1493 aha152x_internal_queue(SCpnt, &sem, 0, 0, internal_done);
1494 down(&sem);
1496 return SUCCESS;
1500 * Abort a command
1503 int aha152x_abort(Scsi_Cmnd *SCpnt)
1505 struct Scsi_Host *shpnt = SCpnt->host;
1506 Scsi_Cmnd *ptr;
1507 unsigned long flags;
1509 if(!shpnt) {
1510 printk(ERR_LEAD "abort(%p): no host structure\n", CMDINFO(SCpnt), SCpnt);
1511 return FAILED;
1514 #if defined(AHA152X_DEBUG)
1515 if(HOSTDATA(shpnt)->debug & debug_eh) {
1516 printk(DEBUG_LEAD "abort(%p)", CMDINFO(SCpnt), SCpnt);
1517 show_queues(shpnt);
1518 mdelay(1000);
1520 #endif
1522 DO_LOCK(flags);
1524 ptr=remove_SC(&ISSUE_SC, SCpnt);
1526 if(ptr) {
1527 DPRINTK(debug_eh, DEBUG_LEAD "not yet issued - SUCCESS\n", CMDINFO(SCpnt));
1529 HOSTDATA(shpnt)->commands--;
1530 if (!HOSTDATA(shpnt)->commands)
1531 SETPORT(PORTA, 0);
1532 DO_UNLOCK(flags);
1534 kfree(SCpnt->host_scribble);
1535 SCpnt->host_scribble=0;
1537 return SUCCESS;
1540 DO_UNLOCK(flags);
1543 * FIXME:
1544 * for current command: queue ABORT for message out and raise ATN
1545 * for disconnected command: pseudo SC with ABORT message or ABORT on reselection?
1549 printk(ERR_LEAD "cannot abort running or disconnected command\n", CMDINFO(SCpnt));
1551 return FAILED;
1554 static void timer_expired(unsigned long p)
1556 struct semaphore *sem = (void *)p;
1558 printk(KERN_INFO "aha152x: timer expired\n");
1559 up(sem);
1563 * Reset a device
1565 * FIXME: never seen this live. might lockup...
1568 int aha152x_device_reset(Scsi_Cmnd * SCpnt)
1570 struct Scsi_Host *shpnt = SCpnt->host;
1571 DECLARE_MUTEX_LOCKED(sem);
1572 struct timer_list timer;
1573 Scsi_Cmnd cmnd;
1575 #if defined(AHA152X_DEBUG)
1576 if(HOSTDATA(shpnt)->debug & debug_eh) {
1577 printk(INFO_LEAD "aha152x_device_reset(%p)", CMDINFO(SCpnt), SCpnt);
1578 show_queues(shpnt);
1579 mdelay(1000);
1581 #endif
1583 if(CURRENT_SC==SCpnt) {
1584 printk(ERR_LEAD "cannot reset current device\n", CMDINFO(SCpnt));
1585 return FAILED;
1588 cmnd.cmd_len = 0;
1589 cmnd.host = SCpnt->host;
1590 cmnd.target = SCpnt->target;
1591 cmnd.lun = SCpnt->lun;
1592 cmnd.use_sg = 0;
1593 cmnd.request_buffer = 0;
1594 cmnd.request_bufflen = 0;
1596 init_timer(&timer);
1597 timer.data = (unsigned long) &sem;
1598 timer.expires = jiffies + 10000; /* 10s */
1599 timer.function = (void (*)(unsigned long)) timer_expired;
1600 add_timer(&timer);
1602 aha152x_internal_queue(&cmnd, &sem, resetting, 0, internal_done);
1604 down(&sem);
1606 del_timer(&timer);
1608 if(cmnd.SCp.phase & resetted) {
1609 return SUCCESS;
1610 } else {
1611 return FAILED;
1615 void free_hard_reset_SCs(struct Scsi_Host *shpnt, Scsi_Cmnd **SCs)
1617 Scsi_Cmnd *ptr;
1618 unsigned long flags;
1620 DO_LOCK(flags);
1622 ptr=*SCs;
1623 while(ptr) {
1624 Scsi_Cmnd *next = SCNEXT(ptr);
1626 if (!ptr->device->soft_reset) {
1627 DPRINTK(debug_eh, DEBUG_LEAD "disconnected command %p removed\n", CMDINFO(ptr), ptr);
1628 remove_SC(SCs, ptr);
1629 HOSTDATA(shpnt)->commands--;
1630 kfree(ptr->host_scribble);
1631 ptr->host_scribble=0;
1634 ptr = next;
1637 DO_UNLOCK(flags);
1641 * Reset the bus
1644 int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
1646 struct Scsi_Host *shpnt = SCpnt->host;
1647 unsigned long flags;
1649 #if defined(AHA152X_DEBUG)
1650 if(HOSTDATA(shpnt)->debug & debug_eh) {
1651 printk(DEBUG_LEAD "aha152x_bus_reset(%p)", CMDINFO(SCpnt), SCpnt);
1652 show_queues(shpnt);
1653 mdelay(1000);
1655 #endif
1657 free_hard_reset_SCs(shpnt, &ISSUE_SC);
1658 free_hard_reset_SCs(shpnt, &DISCONNECTED_SC);
1660 DPRINTK(debug_eh, DEBUG_LEAD "resetting bus\n", CMDINFO(SCpnt));
1662 SETPORT(SCSISEQ, SCSIRSTO);
1663 mdelay(256);
1664 SETPORT(SCSISEQ, 0);
1665 mdelay(DELAY);
1667 DPRINTK(debug_eh, DEBUG_LEAD "bus reset returns\n", CMDINFO(SCpnt));
1669 DO_LOCK(flags);
1670 setup_expected_interrupts(shpnt);
1671 if(HOSTDATA(shpnt)->commands==0)
1672 SETPORT(PORTA, 0);
1673 DO_UNLOCK(flags);
1675 return SUCCESS;
1680 * Restore default values to the AIC-6260 registers and reset the fifos
1683 static void reset_ports(struct Scsi_Host *shpnt)
1685 unsigned long flags;
1687 /* disable interrupts */
1688 SETPORT(DMACNTRL0, RSTFIFO);
1690 SETPORT(SCSISEQ, 0);
1692 SETPORT(SXFRCTL1, 0);
1693 SETPORT(SCSISIG, 0);
1694 SETRATE(0);
1696 /* clear all interrupt conditions */
1697 SETPORT(SSTAT0, 0x7f);
1698 SETPORT(SSTAT1, 0xef);
1700 SETPORT(SSTAT4, SYNCERR | FWERR | FRERR);
1702 SETPORT(DMACNTRL0, 0);
1703 SETPORT(DMACNTRL1, 0);
1705 SETPORT(BRSTCNTRL, 0xf1);
1707 /* clear SCSI fifos and transfer count */
1708 SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1709 SETPORT(SXFRCTL0, CH1);
1711 DO_LOCK(flags);
1712 setup_expected_interrupts(shpnt);
1713 DO_UNLOCK(flags);
1717 * Reset the host (bus and controller)
1720 int aha152x_host_reset(Scsi_Cmnd * SCpnt)
1722 #if defined(AHA152X_DEBUG)
1723 struct Scsi_Host *shpnt = SCpnt->host;
1724 #endif
1726 DPRINTK(debug_eh, DEBUG_LEAD "aha152x_host_reset(%p)\n", CMDINFO(SCpnt), SCpnt);
1728 aha152x_bus_reset(SCpnt);
1730 DPRINTK(debug_eh, DEBUG_LEAD "resetting ports\n", CMDINFO(SCpnt));
1731 reset_ports(SCpnt->host);
1733 return SUCCESS;
1737 * Return the "logical geometry"
1740 int aha152x_biosparam(Scsi_Disk * disk, kdev_t dev, int *info_array)
1742 struct Scsi_Host *shpnt = disk->device->host;
1744 /* try default translation */
1745 info_array[0] = 64;
1746 info_array[1] = 32;
1747 info_array[2] = disk->capacity / (64 * 32);
1749 /* for disks >1GB do some guessing */
1750 if (info_array[2] >= 1024) {
1751 int info[3];
1753 /* try to figure out the geometry from the partition table */
1754 if (scsicam_bios_param(disk, dev, info) < 0 ||
1755 !((info[0] == 64 && info[1] == 32) || (info[0] == 255 && info[1] == 63))) {
1756 if (EXT_TRANS) {
1757 printk(KERN_NOTICE
1758 "aha152x: unable to verify geometry for disk with >1GB.\n"
1759 " using extended translation.\n");
1760 info_array[0] = 255;
1761 info_array[1] = 63;
1762 info_array[2] = disk->capacity / (255 * 63);
1763 } else {
1764 printk(KERN_NOTICE
1765 "aha152x: unable to verify geometry for disk with >1GB.\n"
1766 " Using default translation. Please verify yourself.\n"
1767 " Perhaps you need to enable extended translation in the driver.\n"
1768 " See /usr/src/linux/drivers/scsi/README.aha152x for details.\n");
1770 } else {
1771 info_array[0] = info[0];
1772 info_array[1] = info[1];
1773 info_array[2] = info[2];
1775 if (info[0] == 255 && !EXT_TRANS) {
1776 printk(KERN_NOTICE
1777 "aha152x: current partition table is using extended translation.\n"
1778 " using it also, although it's not explictly enabled.\n");
1783 return 0;
1787 * Internal done function
1790 static void done(struct Scsi_Host *shpnt, int error)
1792 if (CURRENT_SC) {
1793 if(DONE_SC)
1794 printk(ERR_LEAD "there's already a completed command %p - will cause abort\n", CMDINFO(CURRENT_SC), DONE_SC);
1796 DONE_SC = CURRENT_SC;
1797 CURRENT_SC = 0;
1798 DONE_SC->result = error;
1799 } else
1800 printk(KERN_ERR "aha152x: done() called outside of command\n");
1803 static struct tq_struct aha152x_tq;
1806 * Run service completions on the card with interrupts enabled.
1809 static void run(void)
1811 int i;
1812 for (i = 0; i < IRQS; i++) {
1813 struct Scsi_Host *shpnt = aha152x_host[i];
1814 if (shpnt && HOSTDATA(shpnt)->service) {
1815 HOSTDATA(shpnt)->service=0;
1816 complete(shpnt);
1822 * Interrupts handler
1826 static void intr(int irqno, void *dev_id, struct pt_regs *regs)
1828 struct Scsi_Host *shpnt = aha152x_host[irqno - IRQ_MIN];
1830 if (!shpnt) {
1831 printk(KERN_ERR "aha152x: catched interrupt for unknown controller.\n");
1832 return;
1835 /* no more interrupts from the controller, while we're busy.
1836 INTEN is restored by the BH handler */
1837 CLRBITS(DMACNTRL0, INTEN);
1839 #if 0
1840 /* check if there is already something to be
1841 serviced; should not happen */
1842 if(HOSTDATA(shpnt)->service) {
1843 printk(KERN_ERR "aha152x%d: lost interrupt (%d)\n", HOSTNO, HOSTDATA(shpnt)->service);
1844 show_queues(shpnt);
1846 #endif
1848 /* Poke the BH handler */
1849 HOSTDATA(shpnt)->service++;
1850 aha152x_tq.routine = (void *) run;
1851 queue_task(&aha152x_tq, &tq_immediate);
1852 mark_bh(IMMEDIATE_BH);
1856 * busfree phase
1857 * - handle completition/disconnection/error of current command
1858 * - start selection for next command (if any)
1860 static void busfree_run(struct Scsi_Host *shpnt)
1862 unsigned long flags;
1863 #if defined(AHA152X_STAT)
1864 int action=0;
1865 #endif
1867 SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1868 SETPORT(SXFRCTL0, CH1);
1870 SETPORT(SSTAT1, CLRBUSFREE);
1872 if(CURRENT_SC) {
1873 #if defined(AHA152X_STAT)
1874 action++;
1875 #endif
1876 CURRENT_SC->SCp.phase &= ~syncneg;
1878 if(CURRENT_SC->SCp.phase & completed) {
1879 /* target sent COMMAND COMPLETE */
1880 done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16));
1882 } else if(CURRENT_SC->SCp.phase & aborted) {
1883 DPRINTK(debug_eh, DEBUG_LEAD "ABORT sent\n", CMDINFO(CURRENT_SC));
1884 done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_ABORT << 16));
1886 } else if(CURRENT_SC->SCp.phase & resetted) {
1887 DPRINTK(debug_eh, DEBUG_LEAD "BUS DEVICE RESET sent\n", CMDINFO(CURRENT_SC));
1888 done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_RESET << 16));
1890 } else if(CURRENT_SC->SCp.phase & disconnected) {
1891 /* target sent DISCONNECT */
1892 DPRINTK(debug_selection, DEBUG_LEAD "target disconnected at %d/%d\n",
1893 CMDINFO(CURRENT_SC),
1894 CURRENT_SC->resid,
1895 CURRENT_SC->request_bufflen);
1896 #if defined(AHA152X_STAT)
1897 HOSTDATA(shpnt)->disconnections++;
1898 #endif
1899 append_SC(&DISCONNECTED_SC, CURRENT_SC);
1900 CURRENT_SC->SCp.phase |= 1 << 16;
1901 CURRENT_SC = 0;
1903 } else {
1904 done(shpnt, DID_ERROR << 16);
1906 #if defined(AHA152X_STAT)
1907 } else {
1908 HOSTDATA(shpnt)->busfree_without_old_command++;
1909 #endif
1912 DO_LOCK(flags);
1914 if(DONE_SC) {
1915 #if defined(AHA152X_STAT)
1916 action++;
1917 #endif
1918 if(SCDONE(DONE_SC)) {
1919 Scsi_Cmnd *ptr=DONE_SC;
1920 DONE_SC=SCDONE(DONE_SC);
1922 #if 0
1923 if(HOSTDATA(shpnt)->debug & debug_eh) {
1924 printk(ERR_LEAD "received sense: ", CMDINFO(ptr));
1925 print_sense("bh", DONE_SC);
1927 #endif
1929 HOSTDATA(shpnt)->commands--;
1930 if (!HOSTDATA(shpnt)->commands)
1931 SETPORT(PORTA, 0); /* turn led off */
1933 kfree(ptr->host_scribble);
1934 kfree(ptr);
1935 } else if(DONE_SC->SCp.Status==0x02) {
1936 #if defined(AHA152X_STAT)
1937 HOSTDATA(shpnt)->busfree_with_check_condition++;
1938 #endif
1939 #if 0
1940 DPRINTK(debug_eh, ERR_LEAD "CHECK CONDITION found\n", CMDINFO(DONE_SC));
1941 #endif
1943 if(!(DONE_SC->SCp.Status & not_issued)) {
1944 Scsi_Cmnd *cmnd = kmalloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
1946 if(cmnd) {
1947 Scsi_Cmnd *ptr=DONE_SC;
1948 DONE_SC=0;
1950 #if 0
1951 DPRINTK(debug_eh, ERR_LEAD "requesting sense\n", CMDINFO(ptr));
1952 #endif
1954 cmnd->cmnd[0] = REQUEST_SENSE;
1955 cmnd->cmnd[1] = 0;
1956 cmnd->cmnd[2] = 0;
1957 cmnd->cmnd[3] = 0;
1958 cmnd->cmnd[4] = sizeof(ptr->sense_buffer);
1959 cmnd->cmnd[5] = 0;
1960 cmnd->cmd_len = 6;
1961 cmnd->host = ptr->host;
1962 cmnd->target = ptr->target;
1963 cmnd->lun = ptr->lun;
1964 cmnd->use_sg = 0;
1965 cmnd->request_buffer = ptr->sense_buffer;
1966 cmnd->request_bufflen = sizeof(ptr->sense_buffer);
1968 DO_UNLOCK(flags);
1969 aha152x_internal_queue(cmnd, 0, 0, ptr, internal_done);
1970 DO_LOCK(flags);
1971 } else {
1972 printk(ERR_LEAD "allocation failed\n", CMDINFO(CURRENT_SC));
1973 if(cmnd)
1974 kfree(cmnd);
1976 } else {
1977 #if 0
1978 DPRINTK(debug_eh, ERR_LEAD "command not issued - CHECK CONDITION ignored\n", CMDINFO(DONE_SC));
1979 #endif
1983 if(DONE_SC && DONE_SC->scsi_done) {
1984 /* turn led off, when no commands are in the driver */
1985 HOSTDATA(shpnt)->commands--;
1986 if (!HOSTDATA(shpnt)->commands)
1987 SETPORT(PORTA, 0); /* turn led off */
1989 kfree(DONE_SC->host_scribble);
1990 DONE_SC->host_scribble=0;
1992 DO_UNLOCK(flags);
1993 DPRINTK(debug_done, DEBUG_LEAD "calling scsi_done(%p)\n", CMDINFO(DONE_SC), DONE_SC);
1994 DONE_SC->scsi_done(DONE_SC);
1995 DPRINTK(debug_done, DEBUG_LEAD "scsi_done(%p) returned\n", CMDINFO(DONE_SC), DONE_SC);
1996 DO_LOCK(flags);
1999 DONE_SC=0;
2000 #if defined(AHA152X_STAT)
2001 } else {
2002 HOSTDATA(shpnt)->busfree_without_done_command++;
2003 #endif
2006 if(ISSUE_SC)
2007 CURRENT_SC = remove_first_SC(&ISSUE_SC);
2009 DO_UNLOCK(flags);
2011 if(CURRENT_SC) {
2012 #if defined(AHA152X_STAT)
2013 action++;
2014 #endif
2015 CURRENT_SC->SCp.phase |= selecting;
2017 DPRINTK(debug_selection, DEBUG_LEAD "selecting target\n", CMDINFO(CURRENT_SC));
2019 /* clear selection timeout */
2020 SETPORT(SSTAT1, SELTO);
2022 SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
2023 SETPORT(SXFRCTL1, (PARITY ? ENSPCHK : 0 ) | ENSTIMER);
2024 SETPORT(SCSISEQ, ENSELO | ENAUTOATNO | (DISCONNECTED_SC ? ENRESELI : 0));
2025 } else {
2026 #if defined(AHA152X_STAT)
2027 HOSTDATA(shpnt)->busfree_without_new_command++;
2028 #endif
2029 SETPORT(SCSISEQ, DISCONNECTED_SC ? ENRESELI : 0);
2032 #if defined(AHA152X_STAT)
2033 if(!action)
2034 HOSTDATA(shpnt)->busfree_without_any_action++;
2035 #endif
2039 * Selection done (OUT)
2040 * - queue IDENTIFY message and SDTR to selected target for message out
2041 * (ATN asserted automagically via ENAUTOATNO in busfree())
2043 static void seldo_run(struct Scsi_Host *shpnt)
2045 SETPORT(SCSISIG, 0);
2046 SETPORT(SSTAT1, CLRBUSFREE);
2047 SETPORT(SSTAT1, CLRPHASECHG);
2049 CURRENT_SC->SCp.phase &= ~(selecting|not_issued);
2051 SETPORT(SCSISEQ, 0);
2053 if (TESTLO(SSTAT0, SELDO)) {
2054 printk(ERR_LEAD "aha152x: passing bus free condition\n", CMDINFO(CURRENT_SC));
2055 done(shpnt, DID_NO_CONNECT << 16);
2056 return;
2059 SETPORT(SSTAT0, CLRSELDO);
2061 ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->lun));
2063 if (CURRENT_SC->SCp.phase & aborting) {
2064 ADDMSGO(ABORT);
2065 } else if (CURRENT_SC->SCp.phase & resetting) {
2066 ADDMSGO(BUS_DEVICE_RESET);
2067 } else if (SYNCNEG==0 && SYNCHRONOUS) {
2068 CURRENT_SC->SCp.phase |= syncneg;
2069 ADDMSGO(EXTENDED_MESSAGE);
2070 ADDMSGO(3);
2071 ADDMSGO(EXTENDED_SDTR);
2072 ADDMSGO(50); /* 200ns */
2073 ADDMSGO(8); /* 8 byte req/ack offset */
2075 SYNCNEG=1; /* negotiation in progress */
2078 SETRATE(SYNCRATE);
2082 * Selection timeout
2083 * - return command to mid-level with failure cause
2086 static void selto_run(struct Scsi_Host *shpnt)
2088 SETPORT(SCSISEQ, 0);
2089 SETPORT(SSTAT1, CLRSELTIMO);
2091 DPRINTK(debug_selection, DEBUG_LEAD "selection timeout\n", CMDINFO(CURRENT_SC));
2093 if(!CURRENT_SC) {
2094 DPRINTK(debug_selection, DEBUG_LEAD "!CURRENT_SC\n", CMDINFO(CURRENT_SC));
2095 return;
2098 CURRENT_SC->SCp.phase &= ~selecting;
2100 if (CURRENT_SC->SCp.phase & aborted) {
2101 DPRINTK(debug_selection, DEBUG_LEAD "aborted\n", CMDINFO(CURRENT_SC));
2102 done(shpnt, DID_ABORT << 16);
2103 } else if (TESTLO(SSTAT0, SELINGO)) {
2104 DPRINTK(debug_selection, DEBUG_LEAD "arbitration not won\n", CMDINFO(CURRENT_SC));
2105 done(shpnt, DID_BUS_BUSY << 16);
2106 } else {
2107 /* ARBITRATION won, but SELECTION failed */
2108 DPRINTK(debug_selection, DEBUG_LEAD "selection failed\n", CMDINFO(CURRENT_SC));
2109 done(shpnt, DID_NO_CONNECT << 16);
2114 * Selection in done
2115 * - put current command back to issue queue
2116 * (reconnection of a disconnected nexus instead
2117 * of successful selection out)
2120 static void seldi_run(struct Scsi_Host *shpnt)
2122 int selid;
2123 int target;
2124 unsigned long flags;
2126 SETPORT(SCSISIG, 0);
2127 SETPORT(SSTAT0, CLRSELDI);
2128 SETPORT(SSTAT1, CLRBUSFREE);
2129 SETPORT(SSTAT1, CLRPHASECHG);
2131 if(CURRENT_SC) {
2132 if(!(CURRENT_SC->SCp.phase & not_issued))
2133 printk(ERR_LEAD "command should not have been issued yet\n", CMDINFO(CURRENT_SC));
2135 DPRINTK(debug_selection, ERR_LEAD "command requeued - reselection\n", CMDINFO(CURRENT_SC));
2137 DO_LOCK(flags);
2138 append_SC(&ISSUE_SC, CURRENT_SC);
2139 DO_UNLOCK(flags);
2141 CURRENT_SC = 0;
2144 if(!DISCONNECTED_SC) {
2145 DPRINTK(debug_selection, DEBUG_LEAD "unexpected SELDI ", CMDINFO(CURRENT_SC));
2146 return;
2149 RECONN_TARGET=-1;
2151 selid = GETPORT(SELID) & ~(1 << shpnt->this_id);
2153 if (selid==0) {
2154 printk("aha152x%d: target id unknown (%02x)\n", HOSTNO, selid);
2155 return;
2158 for(target=7; !(selid & (1 << target)); target--)
2161 if(selid & ~(1 << target)) {
2162 printk("aha152x%d: multiple targets reconnected (%02x)\n",
2163 HOSTNO, selid);
2167 SETPORT(SCSIID, (shpnt->this_id << OID_) | target);
2168 SETPORT(SCSISEQ, 0);
2170 SETRATE(HOSTDATA(shpnt)->syncrate[target]);
2172 RECONN_TARGET=target;
2173 DPRINTK(debug_selection, DEBUG_LEAD "target %d reselected (%02x).\n", CMDINFO(CURRENT_SC), target, selid);
2177 * message in phase
2178 * - handle initial message after reconnection to identify
2179 * reconnecting nexus
2180 * - queue command on DISCONNECTED_SC on DISCONNECT message
2181 * - set completed flag on COMMAND COMPLETE
2182 * (other completition code moved to busfree_run)
2183 * - handle response to SDTR
2184 * - clear synchronous transfer agreements on BUS RESET
2186 * FIXME: what about SAVE POINTERS, RESTORE POINTERS?
2189 static void msgi_run(struct Scsi_Host *shpnt)
2191 for(;;) {
2192 int sstat1 = GETPORT(SSTAT1);
2194 if(sstat1 & (PHASECHG|PHASEMIS|BUSFREE) || !(sstat1 & REQINIT))
2195 return;
2197 if(TESTLO(SSTAT0,SPIORDY)) {
2198 DPRINTK(debug_msgi, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2199 return;
2202 ADDMSGI(GETPORT(SCSIDAT));
2204 #if defined(AHA152X_DEBUG)
2205 if (HOSTDATA(shpnt)->debug & debug_msgi) {
2206 printk(INFO_LEAD "inbound message %02x ", CMDINFO(CURRENT_SC), MSGI(0));
2207 print_msg(&MSGI(0));
2208 printk("\n");
2210 #endif
2212 if(!CURRENT_SC) {
2213 if(LASTSTATE!=seldi) {
2214 printk(KERN_ERR "aha152x%d: message in w/o current command not after reselection\n", HOSTNO);
2218 * Handle reselection
2220 if(!(MSGI(0) & IDENTIFY_BASE)) {
2221 printk(KERN_ERR "aha152x%d: target didn't identify after reselection\n", HOSTNO);
2222 continue;
2225 CURRENT_SC = remove_lun_SC(&DISCONNECTED_SC, RECONN_TARGET, MSGI(0) & 0x3f);
2227 if (!CURRENT_SC) {
2228 show_queues(shpnt);
2229 printk(KERN_ERR "aha152x%d: no disconnected command for target %d/%d\n", HOSTNO, RECONN_TARGET, MSGI(0) & 0x3f);
2230 continue;
2233 DPRINTK(debug_msgi, DEBUG_LEAD "target reconnected\n", CMDINFO(CURRENT_SC));
2235 CURRENT_SC->SCp.Message = MSGI(0);
2236 CURRENT_SC->SCp.phase &= ~disconnected;
2238 MSGILEN=0;
2240 /* next message if any */
2241 continue;
2244 CURRENT_SC->SCp.Message = MSGI(0);
2246 switch (MSGI(0)) {
2247 case DISCONNECT:
2248 if (!RECONNECT)
2249 printk(WARN_LEAD "target was not allowed to disconnect\n", CMDINFO(CURRENT_SC));
2251 CURRENT_SC->SCp.phase |= disconnected;
2252 break;
2254 case COMMAND_COMPLETE:
2255 if(CURRENT_SC->SCp.phase & completed)
2256 DPRINTK(debug_msgi, DEBUG_LEAD "again COMMAND COMPLETE\n", CMDINFO(CURRENT_SC));
2258 CURRENT_SC->SCp.phase |= completed;
2259 break;
2261 case MESSAGE_REJECT:
2262 if (SYNCNEG==1) {
2263 printk(INFO_LEAD "Synchronous Data Transfer Request was rejected\n", CMDINFO(CURRENT_SC));
2264 SYNCNEG=2; /* negotiation completed */
2265 } else
2266 printk(INFO_LEAD "inbound message (MESSAGE REJECT)\n", CMDINFO(CURRENT_SC));
2267 break;
2269 case SAVE_POINTERS:
2270 break;
2272 case RESTORE_POINTERS:
2273 break;
2275 case EXTENDED_MESSAGE:
2276 if(MSGILEN<2 || MSGILEN<MSGI(1)+2) {
2277 /* not yet completed */
2278 continue;
2281 switch (MSGI(2)) {
2282 case EXTENDED_SDTR:
2284 long ticks;
2286 if (MSGI(1) != 3) {
2287 printk(ERR_LEAD "SDTR message length!=3\n", CMDINFO(CURRENT_SC));
2288 break;
2291 if (!HOSTDATA(shpnt)->synchronous)
2292 break;
2294 printk(INFO_LEAD, CMDINFO(CURRENT_SC));
2295 print_msg(&MSGI(0));
2296 printk("\n");
2298 ticks = (MSGI(3) * 4 + 49) / 50;
2300 if (syncneg) {
2301 /* negotiation in progress */
2302 if (ticks > 9 || MSGI(4) < 1 || MSGI(4) > 8) {
2303 ADDMSGO(MESSAGE_REJECT);
2304 printk(INFO_LEAD "received Synchronous Data Transfer Request invalid - rejected\n", CMDINFO(CURRENT_SC));
2305 break;
2308 SYNCRATE |= ((ticks - 2) << 4) + MSGI(4);
2309 } else if (ticks <= 9 && MSGI(4) >= 1) {
2310 ADDMSGO(EXTENDED_MESSAGE);
2311 ADDMSGO(3);
2312 ADDMSGO(EXTENDED_SDTR);
2313 if (ticks < 4) {
2314 ticks = 4;
2315 ADDMSGO(50);
2316 } else
2317 ADDMSGO(MSGI(3));
2319 if (MSGI(4) > 8)
2320 MSGI(4) = 8;
2322 ADDMSGO(MSGI(4));
2324 SYNCRATE |= ((ticks - 2) << 4) + MSGI(4);
2325 } else {
2326 /* requested SDTR is too slow, do it asynchronously */
2327 printk(INFO_LEAD "Synchronous Data Transfer Request too slow - Rejecting\n", CMDINFO(CURRENT_SC));
2328 ADDMSGO(MESSAGE_REJECT);
2331 SYNCNEG=2; /* negotiation completed */
2332 SETRATE(SYNCRATE);
2334 break;
2336 case BUS_DEVICE_RESET:
2338 int i;
2340 for(i=0; i<8; i++) {
2341 HOSTDATA(shpnt)->syncrate[i]=0;
2342 HOSTDATA(shpnt)->syncneg[i]=0;
2346 break;
2348 case EXTENDED_MODIFY_DATA_POINTER:
2349 case EXTENDED_EXTENDED_IDENTIFY:
2350 case EXTENDED_WDTR:
2351 default:
2352 ADDMSGO(MESSAGE_REJECT);
2353 break;
2355 break;
2358 MSGILEN=0;
2362 static void msgi_end(struct Scsi_Host *shpnt)
2364 if(MSGILEN>0)
2365 printk(WARN_LEAD "target left before message completed (%d)\n", CMDINFO(CURRENT_SC), MSGILEN);
2367 if (MSGOLEN > 0 && !(GETPORT(SSTAT1) & BUSFREE)) {
2368 DPRINTK(debug_msgi, DEBUG_LEAD "msgo pending\n", CMDINFO(CURRENT_SC));
2369 SETPORT(SCSISIG, P_MSGI | SIG_ATNO);
2374 * message out phase
2377 static void msgo_init(struct Scsi_Host *shpnt)
2379 if(MSGOLEN==0) {
2380 if((CURRENT_SC->SCp.phase & syncneg) && SYNCNEG==2 && SYNCRATE==0) {
2381 ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->lun));
2382 } else {
2383 printk(INFO_LEAD "unexpected MESSAGE OUT phase; rejecting\n", CMDINFO(CURRENT_SC));
2384 ADDMSGO(MESSAGE_REJECT);
2388 #if defined(AHA152X_DEBUG)
2389 if(HOSTDATA(shpnt)->debug & debug_msgo) {
2390 int i;
2392 printk(DEBUG_LEAD "messages( ", CMDINFO(CURRENT_SC));
2393 for (i=0; i<MSGOLEN; i+=print_msg(&MSGO(i)), printk(" "))
2395 printk(")\n");
2397 #endif
2401 * message out phase
2404 static void msgo_run(struct Scsi_Host *shpnt)
2406 if(MSGO_I==MSGOLEN)
2407 DPRINTK(debug_msgo, DEBUG_LEAD "messages all sent (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO_I, MSGOLEN);
2409 while(MSGO_I<MSGOLEN) {
2410 DPRINTK(debug_msgo, DEBUG_LEAD "message byte %02x (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO(MSGO_I), MSGO_I, MSGOLEN);
2412 if(TESTLO(SSTAT0, SPIORDY)) {
2413 DPRINTK(debug_msgo, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2414 return;
2417 if (MSGO_I==MSGOLEN-1) {
2418 /* Leave MESSAGE OUT after transfer */
2419 SETPORT(SSTAT1, CLRATNO);
2423 if (MSGO(MSGO_I) & IDENTIFY_BASE)
2424 CURRENT_SC->SCp.phase |= identified;
2426 if (MSGO(MSGO_I)==ABORT)
2427 CURRENT_SC->SCp.phase |= aborted;
2429 if (MSGO(MSGO_I)==BUS_DEVICE_RESET)
2430 CURRENT_SC->SCp.phase |= resetted;
2432 SETPORT(SCSIDAT, MSGO(MSGO_I++));
2436 static void msgo_end(struct Scsi_Host *shpnt)
2438 if(MSGO_I<MSGOLEN)
2439 printk(ERR_LEAD "message sent incompletely (%d/%d)\n", CMDINFO(CURRENT_SC), MSGO_I, MSGOLEN);
2441 MSGO_I = 0;
2442 MSGOLEN = 0;
2446 * command phase
2449 static void cmd_init(struct Scsi_Host *shpnt)
2451 if (CURRENT_SC->SCp.sent_command) {
2452 printk(ERR_LEAD "command already sent\n", CMDINFO(CURRENT_SC));
2453 done(shpnt, DID_ERROR << 16);
2454 return;
2457 #if defined(AHA152X_DEBUG)
2458 if (HOSTDATA(shpnt)->debug & debug_cmd) {
2459 printk(DEBUG_LEAD "cmd_init: ", CMDINFO(CURRENT_SC));
2460 print_command(CURRENT_SC->cmnd);
2462 #endif
2464 CMD_I=0;
2468 * command phase
2471 static void cmd_run(struct Scsi_Host *shpnt)
2473 if(CMD_I==CURRENT_SC->cmd_len) {
2474 DPRINTK(debug_cmd, DEBUG_LEAD "command already completely sent (%d/%d)", CMDINFO(CURRENT_SC), CMD_I, CURRENT_SC->cmd_len);
2475 disp_ports(shpnt);
2478 while(CMD_I<CURRENT_SC->cmd_len) {
2479 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);
2481 if(TESTLO(SSTAT0, SPIORDY)) {
2482 DPRINTK(debug_cmd, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2483 return;
2486 SETPORT(SCSIDAT, CURRENT_SC->cmnd[CMD_I++]);
2490 static void cmd_end(struct Scsi_Host *shpnt)
2492 if(CMD_I<CURRENT_SC->cmd_len)
2493 printk(ERR_LEAD "command sent incompletely (%d/%d)\n", CMDINFO(CURRENT_SC), CMD_I, CURRENT_SC->cmd_len);
2494 else
2495 CURRENT_SC->SCp.sent_command++;
2499 * status phase
2502 static void status_run(struct Scsi_Host *shpnt)
2504 if(TESTLO(SSTAT0,SPIORDY)) {
2505 DPRINTK(debug_status, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
2506 return;
2509 CURRENT_SC->SCp.Status = GETPORT(SCSIDAT);
2511 #if defined(AHA152X_DEBUG)
2512 if (HOSTDATA(shpnt)->debug & debug_status) {
2513 printk(DEBUG_LEAD "inbound status %02x ", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.Status);
2514 print_status(CURRENT_SC->SCp.Status);
2515 printk("\n");
2517 #endif
2521 * data in phase
2524 static void datai_init(struct Scsi_Host *shpnt)
2526 SETPORT(DMACNTRL0, RSTFIFO);
2527 SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
2529 SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2530 SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2532 SETPORT(SIMODE0, 0);
2533 SETPORT(SIMODE1, ENSCSIPERR | ENSCSIRST | ENPHASEMIS | ENBUSFREE);
2535 DATA_LEN=0;
2536 DPRINTK(debug_datai,
2537 DEBUG_LEAD "datai_init: request_bufflen=%d resid=%d\n",
2538 CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid);
2541 static void datai_run(struct Scsi_Host *shpnt)
2543 unsigned int the_time;
2544 int fifodata, data_count;
2547 * loop while the phase persists or the fifos are not empty
2550 while(TESTLO(DMASTAT, INTSTAT) || TESTLO(DMASTAT, DFIFOEMP) || TESTLO(SSTAT2, SEMPTY)) {
2551 /* FIXME: maybe this should be done by setting up
2552 * STCNT to trigger ENSWRAP interrupt, instead of
2553 * polling for DFIFOFULL
2555 the_time=jiffies + 100;
2556 while(TESTLO(DMASTAT, DFIFOFULL|INTSTAT) && time_before(jiffies,the_time))
2557 barrier();
2559 if(TESTLO(DMASTAT, DFIFOFULL|INTSTAT)) {
2560 printk(ERR_LEAD "datai timeout", CMDINFO(CURRENT_SC));
2561 disp_ports(shpnt);
2562 break;
2565 if(TESTHI(DMASTAT, DFIFOFULL)) {
2566 fifodata = 128;
2567 } else {
2568 the_time=jiffies + 100;
2569 while(TESTLO(SSTAT2, SEMPTY) && time_before(jiffies,the_time))
2570 barrier();
2572 if(TESTLO(SSTAT2, SEMPTY)) {
2573 printk(ERR_LEAD "datai sempty timeout", CMDINFO(CURRENT_SC));
2574 disp_ports(shpnt);
2575 break;
2578 fifodata = GETPORT(FIFOSTAT);
2581 while(fifodata>0 && CURRENT_SC->SCp.this_residual>0) {
2582 data_count = fifodata>CURRENT_SC->SCp.this_residual ?
2583 CURRENT_SC->SCp.this_residual :
2584 fifodata;
2585 fifodata -= data_count;
2587 if(data_count & 1) {
2588 DPRINTK(debug_datai, DEBUG_LEAD "8bit\n", CMDINFO(CURRENT_SC));
2589 SETPORT(DMACNTRL0, ENDMA|_8BIT);
2590 *CURRENT_SC->SCp.ptr++ = GETPORT(DATAPORT);
2591 CURRENT_SC->SCp.this_residual--;
2592 DATA_LEN++;
2593 SETPORT(DMACNTRL0, ENDMA);
2596 if(data_count > 1) {
2597 DPRINTK(debug_datai, DEBUG_LEAD "16bit(%d)\n", CMDINFO(CURRENT_SC), data_count);
2598 data_count >>= 1;
2599 insw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2600 CURRENT_SC->SCp.ptr += 2 * data_count;
2601 CURRENT_SC->SCp.this_residual -= 2 * data_count;
2602 DATA_LEN += 2 * data_count;
2605 if(CURRENT_SC->SCp.this_residual==0 && CURRENT_SC->SCp.buffers_residual>0) {
2606 /* advance to next buffer */
2607 CURRENT_SC->SCp.buffers_residual--;
2608 CURRENT_SC->SCp.buffer++;
2609 CURRENT_SC->SCp.ptr = CURRENT_SC->SCp.buffer->address;
2610 CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2614 if(fifodata>0 && CURRENT_SC->SCp.this_residual==0) {
2615 printk(ERR_LEAD "no buffers left for %d(%d) bytes (data overrun!?)\n", CMDINFO(CURRENT_SC), fifodata, GETPORT(FIFOSTAT));
2616 break;
2620 if(TESTLO(DMASTAT, INTSTAT) ||
2621 TESTLO(DMASTAT, DFIFOEMP) ||
2622 TESTLO(SSTAT2, SEMPTY) ||
2623 GETPORT(FIFOSTAT)>0) {
2625 * something went wrong, if there's something left in the fifos
2626 * or the phase didn't change
2628 printk(ERR_LEAD "fifos should be empty and phase should have changed\n", CMDINFO(CURRENT_SC));
2629 disp_ports(shpnt);
2632 if(DATA_LEN!=GETSTCNT()) {
2633 printk(ERR_LEAD
2634 "manual transfer count differs from automatic (count=%d;stcnt=%d;diff=%d;fifostat=%d)",
2635 CMDINFO(CURRENT_SC), DATA_LEN, GETSTCNT(), GETSTCNT()-DATA_LEN, GETPORT(FIFOSTAT));
2636 disp_ports(shpnt);
2637 mdelay(10000);
2641 static void datai_end(struct Scsi_Host *shpnt)
2643 CURRENT_SC->resid -= GETSTCNT();
2645 DPRINTK(debug_datai,
2646 DEBUG_LEAD "datai_end: request_bufflen=%d resid=%d stcnt=%d\n",
2647 CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid, GETSTCNT());
2649 SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2650 SETPORT(DMACNTRL0, 0);
2654 * data out phase
2657 static void datao_init(struct Scsi_Host *shpnt)
2659 SETPORT(DMACNTRL0, WRITE_READ | RSTFIFO);
2660 SETPORT(DMACNTRL0, WRITE_READ | ENDMA);
2662 SETPORT(SXFRCTL0, CH1|CLRSTCNT);
2663 SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2665 SETPORT(SIMODE0, 0);
2666 SETPORT(SIMODE1, ENSCSIPERR | ENSCSIRST | ENPHASEMIS | ENBUSFREE );
2668 DATA_LEN = CURRENT_SC->resid;
2670 DPRINTK(debug_datao,
2671 DEBUG_LEAD "datao_init: request_bufflen=%d; resid=%d\n",
2672 CMDINFO(CURRENT_SC), CURRENT_SC->request_bufflen, CURRENT_SC->resid);
2675 static void datao_run(struct Scsi_Host *shpnt)
2677 unsigned int the_time;
2678 int data_count;
2680 /* until phase changes or all data sent */
2681 while(TESTLO(DMASTAT, INTSTAT) && CURRENT_SC->SCp.this_residual>0) {
2682 data_count = 128;
2683 if(data_count > CURRENT_SC->SCp.this_residual)
2684 data_count=CURRENT_SC->SCp.this_residual;
2686 if(TESTLO(DMASTAT, DFIFOEMP)) {
2687 printk(ERR_LEAD "datao fifo not empty (%d)", CMDINFO(CURRENT_SC), GETPORT(FIFOSTAT));
2688 disp_ports(shpnt);
2689 break;
2692 if(data_count & 1) {
2693 SETPORT(DMACNTRL0,WRITE_READ|ENDMA|_8BIT);
2694 SETPORT(DATAPORT, *CURRENT_SC->SCp.ptr++);
2695 CURRENT_SC->SCp.this_residual--;
2696 CURRENT_SC->resid--;
2697 SETPORT(DMACNTRL0,WRITE_READ|ENDMA);
2700 if(data_count > 1) {
2701 data_count >>= 1;
2702 outsw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2703 CURRENT_SC->SCp.ptr += 2 * data_count;
2704 CURRENT_SC->SCp.this_residual -= 2 * data_count;
2705 CURRENT_SC->resid -= 2 * data_count;
2708 if(CURRENT_SC->SCp.this_residual==0 && CURRENT_SC->SCp.buffers_residual>0) {
2709 /* advance to next buffer */
2710 CURRENT_SC->SCp.buffers_residual--;
2711 CURRENT_SC->SCp.buffer++;
2712 CURRENT_SC->SCp.ptr = CURRENT_SC->SCp.buffer->address;
2713 CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length;
2716 the_time=jiffies+100;
2717 while(TESTLO(DMASTAT, DFIFOEMP|INTSTAT) && time_before(jiffies,the_time))
2718 barrier();
2720 if(TESTLO(DMASTAT, DFIFOEMP|INTSTAT)) {
2721 printk(ERR_LEAD "dataout timeout", CMDINFO(CURRENT_SC));
2722 disp_ports(shpnt);
2723 break;
2728 static void datao_end(struct Scsi_Host *shpnt)
2730 if(TESTLO(DMASTAT, DFIFOEMP)) {
2731 int data_count = (DATA_LEN - CURRENT_SC->resid) - GETSTCNT();
2733 DPRINTK(debug_datao, DEBUG_LEAD "datao: %d bytes to resend (%d written, %d transfered)\n",
2734 CMDINFO(CURRENT_SC),
2735 data_count,
2736 DATA_LEN-CURRENT_SC->resid,
2737 GETSTCNT());
2739 CURRENT_SC->resid += data_count;
2741 if(CURRENT_SC->use_sg) {
2742 data_count -= CURRENT_SC->SCp.ptr - CURRENT_SC->SCp.buffer->address;
2743 while(data_count>0) {
2744 CURRENT_SC->SCp.buffer--;
2745 CURRENT_SC->SCp.buffers_residual++;
2746 data_count -= CURRENT_SC->SCp.buffer->length;
2748 CURRENT_SC->SCp.ptr = CURRENT_SC->SCp.buffer->address - data_count;
2749 CURRENT_SC->SCp.this_residual = CURRENT_SC->SCp.buffer->length + data_count;
2750 } else {
2751 CURRENT_SC->SCp.ptr -= data_count;
2752 CURRENT_SC->SCp.this_residual += data_count;
2756 DPRINTK(debug_datao, DEBUG_LEAD "datao_end: request_bufflen=%d; resid=%d; stcnt=%d\n",
2757 CMDINFO(CURRENT_SC),
2758 CURRENT_SC->request_bufflen,
2759 CURRENT_SC->resid,
2760 GETSTCNT());
2762 SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
2763 SETPORT(SXFRCTL0, CH1);
2765 SETPORT(DMACNTRL0, 0);
2769 * figure out what state we're in
2772 static int update_state(struct Scsi_Host *shpnt)
2774 int dataphase=0;
2775 unsigned int stat0 = GETPORT(SSTAT0);
2776 unsigned int stat1 = GETPORT(SSTAT1);
2778 PREVSTATE = STATE;
2779 STATE=unknown;
2781 if(stat1 & SCSIRSTI) {
2782 STATE=rsti;
2783 SETPORT(SCSISEQ,0);
2784 SETPORT(SSTAT1,SCSIRSTI);
2785 } else if(stat0 & SELDI && PREVSTATE==busfree) {
2786 STATE=seldi;
2787 } else if(stat0 & SELDO && CURRENT_SC && (CURRENT_SC->SCp.phase & selecting)) {
2788 STATE=seldo;
2789 } else if(stat1 & SELTO) {
2790 STATE=selto;
2791 } else if(stat1 & BUSFREE) {
2792 STATE=busfree;
2793 SETPORT(SSTAT1,BUSFREE);
2794 } else if(stat1 & SCSIPERR) {
2795 STATE=parerr;
2796 SETPORT(SSTAT1,SCSIPERR);
2797 } else if(stat1 & REQINIT) {
2798 switch(GETPORT(SCSISIG) & P_MASK) {
2799 case P_MSGI: STATE=msgi; break;
2800 case P_MSGO: STATE=msgo; break;
2801 case P_DATAO: STATE=datao; break;
2802 case P_DATAI: STATE=datai; break;
2803 case P_STATUS: STATE=status; break;
2804 case P_CMD: STATE=cmd; break;
2806 dataphase=1;
2809 if((stat0 & SELDI) && STATE!=seldi && !dataphase) {
2810 printk(INFO_LEAD "reselection missed?", CMDINFO(CURRENT_SC));
2811 disp_ports(shpnt);
2814 if(STATE!=PREVSTATE) {
2815 LASTSTATE=PREVSTATE;
2818 return dataphase;
2822 * handle parity error
2824 * FIXME: in which phase?
2827 static void parerr_run(struct Scsi_Host *shpnt)
2829 printk(ERR_LEAD "parity error\n", CMDINFO(CURRENT_SC));
2830 done(shpnt, DID_PARITY << 16);
2834 * handle reset in
2837 static void rsti_run(struct Scsi_Host *shpnt)
2839 Scsi_Cmnd *ptr;
2841 printk(KERN_NOTICE "aha152x%d: scsi reset in\n", HOSTNO);
2843 ptr=DISCONNECTED_SC;
2844 while(ptr) {
2845 Scsi_Cmnd *next = SCNEXT(ptr);
2847 if (!ptr->device->soft_reset) {
2848 remove_SC(&DISCONNECTED_SC, ptr);
2850 kfree(ptr->host_scribble);
2851 ptr->host_scribble=0;
2853 ptr->result = DID_RESET << 16;
2854 ptr->scsi_done(ptr);
2857 ptr = next;
2860 if(CURRENT_SC && !CURRENT_SC->device->soft_reset)
2861 done(shpnt, DID_RESET << 16 );
2866 * bottom-half handler
2869 static void complete(struct Scsi_Host *shpnt)
2871 int dataphase;
2872 unsigned long flags;
2873 int pending;
2875 DO_LOCK(flags);
2876 if(HOSTDATA(shpnt)->in_intr!=0)
2877 aha152x_error(shpnt, "bottom-half already running!?");
2878 HOSTDATA(shpnt)->in_intr++;
2879 DO_UNLOCK(flags);
2882 * loop while there are interrupt conditions pending
2885 do {
2886 unsigned long start = jiffies;
2887 dataphase=update_state(shpnt);
2889 DPRINTK(debug_phases, LEAD "start %s %s(%s)\n", CMDINFO(CURRENT_SC), states[STATE].name, states[PREVSTATE].name, states[LASTSTATE].name);
2892 * end previous state
2895 if(PREVSTATE!=STATE && states[PREVSTATE].end)
2896 states[PREVSTATE].end(shpnt);
2899 * disable SPIO mode if previous phase used it
2900 * and this one doesn't
2903 if(states[PREVSTATE].spio && !states[STATE].spio) {
2904 SETPORT(SXFRCTL0, CH1);
2905 SETPORT(DMACNTRL0, 0);
2906 if(CURRENT_SC)
2907 CURRENT_SC->SCp.phase &= ~spiordy;
2911 * accept current dataphase phase
2914 if(dataphase) {
2915 SETPORT(SSTAT0, REQINIT);
2916 SETPORT(SCSISIG, GETPORT(SCSISIG) & P_MASK);
2917 SETPORT(SSTAT1, PHASECHG);
2921 * enable SPIO mode if previous didn't use it
2922 * and this one does
2925 if(!states[PREVSTATE].spio && states[STATE].spio) {
2926 SETPORT(DMACNTRL0, 0);
2927 SETPORT(SXFRCTL0, CH1|SPIOEN);
2928 if(CURRENT_SC)
2929 CURRENT_SC->SCp.phase |= spiordy;
2933 * initialize for new state
2936 if(PREVSTATE!=STATE && states[STATE].init)
2937 states[STATE].init(shpnt);
2940 * handle current state
2943 if(states[STATE].run)
2944 states[STATE].run(shpnt);
2945 else
2946 printk(ERR_LEAD "unexpected state (%x)\n", CMDINFO(CURRENT_SC), STATE);
2949 * setup controller to interrupt on
2950 * the next expected condition and
2951 * loop if it's already there
2954 DO_LOCK(flags);
2955 pending=setup_expected_interrupts(shpnt);
2956 #if defined(AHA152X_STAT)
2957 HOSTDATA(shpnt)->count[STATE]++;
2958 if(PREVSTATE!=STATE)
2959 HOSTDATA(shpnt)->count_trans[STATE]++;
2960 HOSTDATA(shpnt)->time[STATE] += jiffies-start;
2961 #endif
2962 DO_UNLOCK(flags);
2964 DPRINTK(debug_phases, LEAD "end %s %s(%s)\n", CMDINFO(CURRENT_SC), states[STATE].name, states[PREVSTATE].name, states[LASTSTATE].name);
2965 } while(pending);
2968 * enable interrupts and leave bottom-half
2971 DO_LOCK(flags);
2972 HOSTDATA(shpnt)->in_intr--;
2973 SETBITS(DMACNTRL0, INTEN);
2974 DO_UNLOCK(flags);
2979 * Dump the current driver status and panic
2981 static void aha152x_error(struct Scsi_Host *shpnt, char *msg)
2983 printk(KERN_EMERG "\naha152x%d: %s\n", HOSTNO, msg);
2984 show_queues(shpnt);
2985 panic("aha152x panic\n");
2989 * Display registers of AIC-6260
2991 static void disp_ports(struct Scsi_Host *shpnt)
2993 #if defined(AHA152X_DEBUG)
2994 int s;
2996 printk("\n%s: %s(%s) ",
2997 CURRENT_SC ? "busy" : "waiting",
2998 states[STATE].name,
2999 states[PREVSTATE].name);
3001 s = GETPORT(SCSISEQ);
3002 printk("SCSISEQ( ");
3003 if (s & TEMODEO)
3004 printk("TARGET MODE ");
3005 if (s & ENSELO)
3006 printk("SELO ");
3007 if (s & ENSELI)
3008 printk("SELI ");
3009 if (s & ENRESELI)
3010 printk("RESELI ");
3011 if (s & ENAUTOATNO)
3012 printk("AUTOATNO ");
3013 if (s & ENAUTOATNI)
3014 printk("AUTOATNI ");
3015 if (s & ENAUTOATNP)
3016 printk("AUTOATNP ");
3017 if (s & SCSIRSTO)
3018 printk("SCSIRSTO ");
3019 printk(");");
3021 printk(" SCSISIG(");
3022 s = GETPORT(SCSISIG);
3023 switch (s & P_MASK) {
3024 case P_DATAO:
3025 printk("DATA OUT");
3026 break;
3027 case P_DATAI:
3028 printk("DATA IN");
3029 break;
3030 case P_CMD:
3031 printk("COMMAND");
3032 break;
3033 case P_STATUS:
3034 printk("STATUS");
3035 break;
3036 case P_MSGO:
3037 printk("MESSAGE OUT");
3038 break;
3039 case P_MSGI:
3040 printk("MESSAGE IN");
3041 break;
3042 default:
3043 printk("*illegal*");
3044 break;
3047 printk("); ");
3049 printk("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3051 printk("SSTAT( ");
3052 s = GETPORT(SSTAT0);
3053 if (s & TARGET)
3054 printk("TARGET ");
3055 if (s & SELDO)
3056 printk("SELDO ");
3057 if (s & SELDI)
3058 printk("SELDI ");
3059 if (s & SELINGO)
3060 printk("SELINGO ");
3061 if (s & SWRAP)
3062 printk("SWRAP ");
3063 if (s & SDONE)
3064 printk("SDONE ");
3065 if (s & SPIORDY)
3066 printk("SPIORDY ");
3067 if (s & DMADONE)
3068 printk("DMADONE ");
3070 s = GETPORT(SSTAT1);
3071 if (s & SELTO)
3072 printk("SELTO ");
3073 if (s & ATNTARG)
3074 printk("ATNTARG ");
3075 if (s & SCSIRSTI)
3076 printk("SCSIRSTI ");
3077 if (s & PHASEMIS)
3078 printk("PHASEMIS ");
3079 if (s & BUSFREE)
3080 printk("BUSFREE ");
3081 if (s & SCSIPERR)
3082 printk("SCSIPERR ");
3083 if (s & PHASECHG)
3084 printk("PHASECHG ");
3085 if (s & REQINIT)
3086 printk("REQINIT ");
3087 printk("); ");
3090 printk("SSTAT( ");
3092 s = GETPORT(SSTAT0) & GETPORT(SIMODE0);
3094 if (s & TARGET)
3095 printk("TARGET ");
3096 if (s & SELDO)
3097 printk("SELDO ");
3098 if (s & SELDI)
3099 printk("SELDI ");
3100 if (s & SELINGO)
3101 printk("SELINGO ");
3102 if (s & SWRAP)
3103 printk("SWRAP ");
3104 if (s & SDONE)
3105 printk("SDONE ");
3106 if (s & SPIORDY)
3107 printk("SPIORDY ");
3108 if (s & DMADONE)
3109 printk("DMADONE ");
3111 s = GETPORT(SSTAT1) & GETPORT(SIMODE1);
3113 if (s & SELTO)
3114 printk("SELTO ");
3115 if (s & ATNTARG)
3116 printk("ATNTARG ");
3117 if (s & SCSIRSTI)
3118 printk("SCSIRSTI ");
3119 if (s & PHASEMIS)
3120 printk("PHASEMIS ");
3121 if (s & BUSFREE)
3122 printk("BUSFREE ");
3123 if (s & SCSIPERR)
3124 printk("SCSIPERR ");
3125 if (s & PHASECHG)
3126 printk("PHASECHG ");
3127 if (s & REQINIT)
3128 printk("REQINIT ");
3129 printk("); ");
3131 printk("SXFRCTL0( ");
3133 s = GETPORT(SXFRCTL0);
3134 if (s & SCSIEN)
3135 printk("SCSIEN ");
3136 if (s & DMAEN)
3137 printk("DMAEN ");
3138 if (s & CH1)
3139 printk("CH1 ");
3140 if (s & CLRSTCNT)
3141 printk("CLRSTCNT ");
3142 if (s & SPIOEN)
3143 printk("SPIOEN ");
3144 if (s & CLRCH1)
3145 printk("CLRCH1 ");
3146 printk("); ");
3148 printk("SIGNAL( ");
3150 s = GETPORT(SCSISIG);
3151 if (s & SIG_ATNI)
3152 printk("ATNI ");
3153 if (s & SIG_SELI)
3154 printk("SELI ");
3155 if (s & SIG_BSYI)
3156 printk("BSYI ");
3157 if (s & SIG_REQI)
3158 printk("REQI ");
3159 if (s & SIG_ACKI)
3160 printk("ACKI ");
3161 printk("); ");
3163 printk("SELID (%02x), ", GETPORT(SELID));
3165 printk("STCNT (%d), ", GETSTCNT());
3167 printk("SSTAT2( ");
3169 s = GETPORT(SSTAT2);
3170 if (s & SOFFSET)
3171 printk("SOFFSET ");
3172 if (s & SEMPTY)
3173 printk("SEMPTY ");
3174 if (s & SFULL)
3175 printk("SFULL ");
3176 printk("); SFCNT (%d); ", s & (SFULL | SFCNT));
3178 s = GETPORT(SSTAT3);
3179 printk("SCSICNT (%d), OFFCNT(%d), ", (s & 0xf0) >> 4, s & 0x0f);
3181 printk("SSTAT4( ");
3182 s = GETPORT(SSTAT4);
3183 if (s & SYNCERR)
3184 printk("SYNCERR ");
3185 if (s & FWERR)
3186 printk("FWERR ");
3187 if (s & FRERR)
3188 printk("FRERR ");
3189 printk("); ");
3191 printk("DMACNTRL0( ");
3192 s = GETPORT(DMACNTRL0);
3193 printk("%s ", s & _8BIT ? "8BIT" : "16BIT");
3194 printk("%s ", s & DMA ? "DMA" : "PIO");
3195 printk("%s ", s & WRITE_READ ? "WRITE" : "READ");
3196 if (s & ENDMA)
3197 printk("ENDMA ");
3198 if (s & INTEN)
3199 printk("INTEN ");
3200 if (s & RSTFIFO)
3201 printk("RSTFIFO ");
3202 if (s & SWINT)
3203 printk("SWINT ");
3204 printk("); ");
3206 printk("DMASTAT( ");
3207 s = GETPORT(DMASTAT);
3208 if (s & ATDONE)
3209 printk("ATDONE ");
3210 if (s & WORDRDY)
3211 printk("WORDRDY ");
3212 if (s & DFIFOFULL)
3213 printk("DFIFOFULL ");
3214 if (s & DFIFOEMP)
3215 printk("DFIFOEMP ");
3216 printk(")\n");
3217 #endif
3221 * display enabled interrupts
3223 static void disp_enintr(struct Scsi_Host *shpnt)
3225 int s;
3227 printk(KERN_DEBUG "enabled interrupts ( ");
3229 s = GETPORT(SIMODE0);
3230 if (s & ENSELDO)
3231 printk("ENSELDO ");
3232 if (s & ENSELDI)
3233 printk("ENSELDI ");
3234 if (s & ENSELINGO)
3235 printk("ENSELINGO ");
3236 if (s & ENSWRAP)
3237 printk("ENSWRAP ");
3238 if (s & ENSDONE)
3239 printk("ENSDONE ");
3240 if (s & ENSPIORDY)
3241 printk("ENSPIORDY ");
3242 if (s & ENDMADONE)
3243 printk("ENDMADONE ");
3245 s = GETPORT(SIMODE1);
3246 if (s & ENSELTIMO)
3247 printk("ENSELTIMO ");
3248 if (s & ENATNTARG)
3249 printk("ENATNTARG ");
3250 if (s & ENPHASEMIS)
3251 printk("ENPHASEMIS ");
3252 if (s & ENBUSFREE)
3253 printk("ENBUSFREE ");
3254 if (s & ENSCSIPERR)
3255 printk("ENSCSIPERR ");
3256 if (s & ENPHASECHG)
3257 printk("ENPHASECHG ");
3258 if (s & ENREQINIT)
3259 printk("ENREQINIT ");
3260 printk(")\n");
3264 * Show the command data of a command
3266 static void show_command(Scsi_Cmnd *ptr)
3268 printk(KERN_DEBUG "0x%08x: target=%d; lun=%d; cmnd=(",
3269 (unsigned int) ptr, ptr->target, ptr->lun);
3271 print_command(ptr->cmnd);
3273 printk(KERN_DEBUG "); request_bufflen=%d; resid=%d; phase |",
3274 ptr->request_bufflen, ptr->resid);
3276 if (ptr->SCp.phase & not_issued)
3277 printk("not issued|");
3278 if (ptr->SCp.phase & selecting)
3279 printk("selecting|");
3280 if (ptr->SCp.phase & identified)
3281 printk("identified|");
3282 if (ptr->SCp.phase & disconnected)
3283 printk("disconnected|");
3284 if (ptr->SCp.phase & completed)
3285 printk("completed|");
3286 if (ptr->SCp.phase & spiordy)
3287 printk("spiordy|");
3288 if (ptr->SCp.phase & syncneg)
3289 printk("syncneg|");
3290 if (ptr->SCp.phase & aborted)
3291 printk("aborted|");
3292 if (ptr->SCp.phase & resetted)
3293 printk("resetted|");
3294 printk("; next=0x%p\n", SCNEXT(ptr));
3298 * Dump the queued data
3300 static void show_queues(struct Scsi_Host *shpnt)
3302 Scsi_Cmnd *ptr;
3303 unsigned long flags;
3305 DO_LOCK(flags);
3306 printk(KERN_DEBUG "\nqueue status:\nissue_SC:\n");
3307 for (ptr = ISSUE_SC; ptr; ptr = SCNEXT(ptr))
3308 show_command(ptr);
3309 DO_UNLOCK(flags);
3311 printk(KERN_DEBUG "current_SC:\n");
3312 if (CURRENT_SC)
3313 show_command(CURRENT_SC);
3314 else
3315 printk(KERN_DEBUG "none\n");
3317 printk(KERN_DEBUG "disconnected_SC:\n");
3318 for (ptr = DISCONNECTED_SC; ptr; ptr = SCNEXT(ptr))
3319 show_command(ptr);
3321 disp_ports(shpnt);
3322 disp_enintr(shpnt);
3325 #undef SPRINTF
3326 #define SPRINTF(args...) pos += sprintf(pos, ## args)
3328 static int get_command(char *pos, Scsi_Cmnd * ptr)
3330 char *start = pos;
3331 int i;
3333 SPRINTF("0x%08x: target=%d; lun=%d; cmnd=( ",
3334 (unsigned int) ptr, ptr->target, ptr->lun);
3336 for (i = 0; i < COMMAND_SIZE(ptr->cmnd[0]); i++)
3337 SPRINTF("0x%02x ", ptr->cmnd[i]);
3339 SPRINTF("); resid=%d; residual=%d; buffers=%d; phase |",
3340 ptr->resid, ptr->SCp.this_residual, ptr->SCp.buffers_residual);
3342 if (ptr->SCp.phase & not_issued)
3343 SPRINTF("not issued|");
3344 if (ptr->SCp.phase & selecting)
3345 SPRINTF("selecting|");
3346 if (ptr->SCp.phase & disconnected)
3347 SPRINTF("disconnected|");
3348 if (ptr->SCp.phase & aborted)
3349 SPRINTF("aborted|");
3350 if (ptr->SCp.phase & identified)
3351 SPRINTF("identified|");
3352 if (ptr->SCp.phase & completed)
3353 SPRINTF("completed|");
3354 if (ptr->SCp.phase & spiordy)
3355 SPRINTF("spiordy|");
3356 if (ptr->SCp.phase & syncneg)
3357 SPRINTF("syncneg|");
3358 SPRINTF("; next=0x%p\n", SCNEXT(ptr));
3360 return (pos - start);
3363 static int get_ports(struct Scsi_Host *shpnt, char *pos)
3365 char *start = pos;
3366 int s;
3368 SPRINTF("\n%s: %s(%s) ", CURRENT_SC ? "on bus" : "waiting", states[STATE].name, states[PREVSTATE].name);
3370 s = GETPORT(SCSISEQ);
3371 SPRINTF("SCSISEQ( ");
3372 if (s & TEMODEO)
3373 SPRINTF("TARGET MODE ");
3374 if (s & ENSELO)
3375 SPRINTF("SELO ");
3376 if (s & ENSELI)
3377 SPRINTF("SELI ");
3378 if (s & ENRESELI)
3379 SPRINTF("RESELI ");
3380 if (s & ENAUTOATNO)
3381 SPRINTF("AUTOATNO ");
3382 if (s & ENAUTOATNI)
3383 SPRINTF("AUTOATNI ");
3384 if (s & ENAUTOATNP)
3385 SPRINTF("AUTOATNP ");
3386 if (s & SCSIRSTO)
3387 SPRINTF("SCSIRSTO ");
3388 SPRINTF(");");
3390 SPRINTF(" SCSISIG(");
3391 s = GETPORT(SCSISIG);
3392 switch (s & P_MASK) {
3393 case P_DATAO:
3394 SPRINTF("DATA OUT");
3395 break;
3396 case P_DATAI:
3397 SPRINTF("DATA IN");
3398 break;
3399 case P_CMD:
3400 SPRINTF("COMMAND");
3401 break;
3402 case P_STATUS:
3403 SPRINTF("STATUS");
3404 break;
3405 case P_MSGO:
3406 SPRINTF("MESSAGE OUT");
3407 break;
3408 case P_MSGI:
3409 SPRINTF("MESSAGE IN");
3410 break;
3411 default:
3412 SPRINTF("*illegal*");
3413 break;
3416 SPRINTF("); ");
3418 SPRINTF("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
3420 SPRINTF("SSTAT( ");
3421 s = GETPORT(SSTAT0);
3422 if (s & TARGET)
3423 SPRINTF("TARGET ");
3424 if (s & SELDO)
3425 SPRINTF("SELDO ");
3426 if (s & SELDI)
3427 SPRINTF("SELDI ");
3428 if (s & SELINGO)
3429 SPRINTF("SELINGO ");
3430 if (s & SWRAP)
3431 SPRINTF("SWRAP ");
3432 if (s & SDONE)
3433 SPRINTF("SDONE ");
3434 if (s & SPIORDY)
3435 SPRINTF("SPIORDY ");
3436 if (s & DMADONE)
3437 SPRINTF("DMADONE ");
3439 s = GETPORT(SSTAT1);
3440 if (s & SELTO)
3441 SPRINTF("SELTO ");
3442 if (s & ATNTARG)
3443 SPRINTF("ATNTARG ");
3444 if (s & SCSIRSTI)
3445 SPRINTF("SCSIRSTI ");
3446 if (s & PHASEMIS)
3447 SPRINTF("PHASEMIS ");
3448 if (s & BUSFREE)
3449 SPRINTF("BUSFREE ");
3450 if (s & SCSIPERR)
3451 SPRINTF("SCSIPERR ");
3452 if (s & PHASECHG)
3453 SPRINTF("PHASECHG ");
3454 if (s & REQINIT)
3455 SPRINTF("REQINIT ");
3456 SPRINTF("); ");
3459 SPRINTF("SSTAT( ");
3461 s = GETPORT(SSTAT0) & GETPORT(SIMODE0);
3463 if (s & TARGET)
3464 SPRINTF("TARGET ");
3465 if (s & SELDO)
3466 SPRINTF("SELDO ");
3467 if (s & SELDI)
3468 SPRINTF("SELDI ");
3469 if (s & SELINGO)
3470 SPRINTF("SELINGO ");
3471 if (s & SWRAP)
3472 SPRINTF("SWRAP ");
3473 if (s & SDONE)
3474 SPRINTF("SDONE ");
3475 if (s & SPIORDY)
3476 SPRINTF("SPIORDY ");
3477 if (s & DMADONE)
3478 SPRINTF("DMADONE ");
3480 s = GETPORT(SSTAT1) & GETPORT(SIMODE1);
3482 if (s & SELTO)
3483 SPRINTF("SELTO ");
3484 if (s & ATNTARG)
3485 SPRINTF("ATNTARG ");
3486 if (s & SCSIRSTI)
3487 SPRINTF("SCSIRSTI ");
3488 if (s & PHASEMIS)
3489 SPRINTF("PHASEMIS ");
3490 if (s & BUSFREE)
3491 SPRINTF("BUSFREE ");
3492 if (s & SCSIPERR)
3493 SPRINTF("SCSIPERR ");
3494 if (s & PHASECHG)
3495 SPRINTF("PHASECHG ");
3496 if (s & REQINIT)
3497 SPRINTF("REQINIT ");
3498 SPRINTF("); ");
3500 SPRINTF("SXFRCTL0( ");
3502 s = GETPORT(SXFRCTL0);
3503 if (s & SCSIEN)
3504 SPRINTF("SCSIEN ");
3505 if (s & DMAEN)
3506 SPRINTF("DMAEN ");
3507 if (s & CH1)
3508 SPRINTF("CH1 ");
3509 if (s & CLRSTCNT)
3510 SPRINTF("CLRSTCNT ");
3511 if (s & SPIOEN)
3512 SPRINTF("SPIOEN ");
3513 if (s & CLRCH1)
3514 SPRINTF("CLRCH1 ");
3515 SPRINTF("); ");
3517 SPRINTF("SIGNAL( ");
3519 s = GETPORT(SCSISIG);
3520 if (s & SIG_ATNI)
3521 SPRINTF("ATNI ");
3522 if (s & SIG_SELI)
3523 SPRINTF("SELI ");
3524 if (s & SIG_BSYI)
3525 SPRINTF("BSYI ");
3526 if (s & SIG_REQI)
3527 SPRINTF("REQI ");
3528 if (s & SIG_ACKI)
3529 SPRINTF("ACKI ");
3530 SPRINTF("); ");
3532 SPRINTF("SELID(%02x), ", GETPORT(SELID));
3534 SPRINTF("STCNT(%d), ", GETSTCNT());
3536 SPRINTF("SSTAT2( ");
3538 s = GETPORT(SSTAT2);
3539 if (s & SOFFSET)
3540 SPRINTF("SOFFSET ");
3541 if (s & SEMPTY)
3542 SPRINTF("SEMPTY ");
3543 if (s & SFULL)
3544 SPRINTF("SFULL ");
3545 SPRINTF("); SFCNT (%d); ", s & (SFULL | SFCNT));
3547 s = GETPORT(SSTAT3);
3548 SPRINTF("SCSICNT (%d), OFFCNT(%d), ", (s & 0xf0) >> 4, s & 0x0f);
3550 SPRINTF("SSTAT4( ");
3551 s = GETPORT(SSTAT4);
3552 if (s & SYNCERR)
3553 SPRINTF("SYNCERR ");
3554 if (s & FWERR)
3555 SPRINTF("FWERR ");
3556 if (s & FRERR)
3557 SPRINTF("FRERR ");
3558 SPRINTF("); ");
3560 SPRINTF("DMACNTRL0( ");
3561 s = GETPORT(DMACNTRL0);
3562 SPRINTF("%s ", s & _8BIT ? "8BIT" : "16BIT");
3563 SPRINTF("%s ", s & DMA ? "DMA" : "PIO");
3564 SPRINTF("%s ", s & WRITE_READ ? "WRITE" : "READ");
3565 if (s & ENDMA)
3566 SPRINTF("ENDMA ");
3567 if (s & INTEN)
3568 SPRINTF("INTEN ");
3569 if (s & RSTFIFO)
3570 SPRINTF("RSTFIFO ");
3571 if (s & SWINT)
3572 SPRINTF("SWINT ");
3573 SPRINTF("); ");
3575 SPRINTF("DMASTAT( ");
3576 s = GETPORT(DMASTAT);
3577 if (s & ATDONE)
3578 SPRINTF("ATDONE ");
3579 if (s & WORDRDY)
3580 SPRINTF("WORDRDY ");
3581 if (s & DFIFOFULL)
3582 SPRINTF("DFIFOFULL ");
3583 if (s & DFIFOEMP)
3584 SPRINTF("DFIFOEMP ");
3585 SPRINTF(")\n");
3587 SPRINTF("enabled interrupts( ");
3589 s = GETPORT(SIMODE0);
3590 if (s & ENSELDO)
3591 SPRINTF("ENSELDO ");
3592 if (s & ENSELDI)
3593 SPRINTF("ENSELDI ");
3594 if (s & ENSELINGO)
3595 SPRINTF("ENSELINGO ");
3596 if (s & ENSWRAP)
3597 SPRINTF("ENSWRAP ");
3598 if (s & ENSDONE)
3599 SPRINTF("ENSDONE ");
3600 if (s & ENSPIORDY)
3601 SPRINTF("ENSPIORDY ");
3602 if (s & ENDMADONE)
3603 SPRINTF("ENDMADONE ");
3605 s = GETPORT(SIMODE1);
3606 if (s & ENSELTIMO)
3607 SPRINTF("ENSELTIMO ");
3608 if (s & ENATNTARG)
3609 SPRINTF("ENATNTARG ");
3610 if (s & ENPHASEMIS)
3611 SPRINTF("ENPHASEMIS ");
3612 if (s & ENBUSFREE)
3613 SPRINTF("ENBUSFREE ");
3614 if (s & ENSCSIPERR)
3615 SPRINTF("ENSCSIPERR ");
3616 if (s & ENPHASECHG)
3617 SPRINTF("ENPHASECHG ");
3618 if (s & ENREQINIT)
3619 SPRINTF("ENREQINIT ");
3620 SPRINTF(")\n");
3622 return (pos - start);
3625 int aha152x_set_info(char *buffer, int length, struct Scsi_Host *shpnt)
3627 if(!shpnt || !buffer || length<8 || strncmp("aha152x ", buffer, 8)!=0)
3628 return -EINVAL;
3630 #if defined(AHA152X_DEBUG)
3631 if(length>14 && strncmp("debug ", buffer+8, 6)==0) {
3632 int debug = HOSTDATA(shpnt)->debug;
3634 HOSTDATA(shpnt)->debug = simple_strtoul(buffer+14, NULL, 0);
3636 printk(KERN_INFO "aha152x%d: debugging options set to 0x%04x (were 0x%04x)\n", HOSTNO, HOSTDATA(shpnt)->debug, debug);
3637 } else
3638 #endif
3639 #if defined(AHA152X_STAT)
3640 if(length>13 && strncmp("reset", buffer+8, 5)==0) {
3641 int i;
3643 HOSTDATA(shpnt)->total_commands=0;
3644 HOSTDATA(shpnt)->disconnections=0;
3645 HOSTDATA(shpnt)->busfree_without_any_action=0;
3646 HOSTDATA(shpnt)->busfree_without_old_command=0;
3647 HOSTDATA(shpnt)->busfree_without_new_command=0;
3648 HOSTDATA(shpnt)->busfree_without_done_command=0;
3649 HOSTDATA(shpnt)->busfree_with_check_condition=0;
3650 for (i = idle; i<maxstate; i++) {
3651 HOSTDATA(shpnt)->count[i]=0;
3652 HOSTDATA(shpnt)->count_trans[i]=0;
3653 HOSTDATA(shpnt)->time[i]=0;
3656 printk(KERN_INFO "aha152x%d: stats reseted.\n", HOSTNO);
3658 } else
3659 #endif
3661 return -EINVAL;
3665 return length;
3668 #undef SPRINTF
3669 #define SPRINTF(args...) \
3670 do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
3672 int aha152x_proc_info(char *buffer, char **start,
3673 off_t offset, int length, int hostno, int inout)
3675 int i;
3676 char *pos = buffer;
3677 struct Scsi_Host *shpnt;
3678 Scsi_Cmnd *ptr;
3679 unsigned long flags;
3680 int thislength;
3682 for (i = 0, shpnt = (struct Scsi_Host *) NULL; i < IRQS; i++)
3683 if (aha152x_host[i] && aha152x_host[i]->host_no == hostno)
3684 shpnt = aha152x_host[i];
3686 if (!shpnt)
3687 return -ESRCH;
3689 DPRINTK(debug_procinfo,
3690 KERN_DEBUG "aha152x_proc_info: buffer=%p offset=%ld length=%d hostno=%d inout=%d\n",
3691 buffer, offset, length, hostno, inout);
3694 if (inout)
3695 return aha152x_set_info(buffer, length, shpnt);
3697 SPRINTF(AHA152X_REVID "\n");
3699 SPRINTF("ioports 0x%04lx to 0x%04lx\n",
3700 shpnt->io_port, shpnt->io_port + shpnt->n_io_port - 1);
3701 SPRINTF("interrupt 0x%02x\n", shpnt->irq);
3702 SPRINTF("disconnection/reconnection %s\n",
3703 RECONNECT ? "enabled" : "disabled");
3704 SPRINTF("parity checking %s\n",
3705 PARITY ? "enabled" : "disabled");
3706 SPRINTF("synchronous transfers %s\n",
3707 SYNCHRONOUS ? "enabled" : "disabled");
3708 SPRINTF("%d commands currently queued\n", HOSTDATA(shpnt)->commands);
3710 if(SYNCHRONOUS) {
3711 SPRINTF("synchronously operating targets (tick=50 ns):\n");
3712 for (i = 0; i < 8; i++)
3713 if (HOSTDATA(shpnt)->syncrate[i] & 0x7f)
3714 SPRINTF("target %d: period %dT/%dns; req/ack offset %d\n",
3716 (((HOSTDATA(shpnt)->syncrate[i] & 0x70) >> 4) + 2),
3717 (((HOSTDATA(shpnt)->syncrate[i] & 0x70) >> 4) + 2) * 50,
3718 HOSTDATA(shpnt)->syncrate[i] & 0x0f);
3720 #if defined(AHA152X_DEBUG)
3721 #define PDEBUG(flags,txt) \
3722 if(HOSTDATA(shpnt)->debug & flags) SPRINTF("(%s) ", txt);
3724 SPRINTF("enabled debugging options: ");
3726 PDEBUG(debug_procinfo, "procinfo");
3727 PDEBUG(debug_queue, "queue");
3728 PDEBUG(debug_intr, "interrupt");
3729 PDEBUG(debug_selection, "selection");
3730 PDEBUG(debug_msgo, "message out");
3731 PDEBUG(debug_msgi, "message in");
3732 PDEBUG(debug_status, "status");
3733 PDEBUG(debug_cmd, "command");
3734 PDEBUG(debug_datai, "data in");
3735 PDEBUG(debug_datao, "data out");
3736 PDEBUG(debug_eh, "eh");
3737 PDEBUG(debug_locks, "locks");
3738 PDEBUG(debug_phases, "phases");
3740 SPRINTF("\n");
3741 #endif
3743 SPRINTF("\nqueue status:\n");
3744 DO_LOCK(flags);
3745 if (ISSUE_SC) {
3746 SPRINTF("not yet issued commands:\n");
3747 for (ptr = ISSUE_SC; ptr; ptr = SCNEXT(ptr))
3748 pos += get_command(pos, ptr);
3749 } else
3750 SPRINTF("no not yet issued commands\n");
3751 DO_UNLOCK(flags);
3753 if (CURRENT_SC) {
3754 SPRINTF("current command:\n");
3755 pos += get_command(pos, CURRENT_SC);
3756 } else
3757 SPRINTF("no current command\n");
3759 if (DISCONNECTED_SC) {
3760 SPRINTF("disconnected commands:\n");
3761 for (ptr = DISCONNECTED_SC; ptr; ptr = SCNEXT(ptr))
3762 pos += get_command(pos, ptr);
3763 } else
3764 SPRINTF("no disconnected commands\n");
3766 pos += get_ports(shpnt, pos);
3768 #if defined(AHA152X_STAT)
3769 SPRINTF("statistics:\n"
3770 "total commands: %d\n"
3771 "disconnections: %d\n"
3772 "busfree with check condition: %d\n"
3773 "busfree without old command: %d\n"
3774 "busfree without new command: %d\n"
3775 "busfree without done command: %d\n"
3776 "busfree without any action: %d\n"
3777 "state "
3778 "transitions "
3779 "count "
3780 "time\n",
3781 HOSTDATA(shpnt)->total_commands,
3782 HOSTDATA(shpnt)->disconnections,
3783 HOSTDATA(shpnt)->busfree_with_check_condition,
3784 HOSTDATA(shpnt)->busfree_without_old_command,
3785 HOSTDATA(shpnt)->busfree_without_new_command,
3786 HOSTDATA(shpnt)->busfree_without_done_command,
3787 HOSTDATA(shpnt)->busfree_without_any_action);
3788 for(i=0; i<maxstate; i++) {
3789 SPRINTF("%-10s %-12d %-12d %-12ld\n",
3790 states[i].name,
3791 HOSTDATA(shpnt)->count_trans[i],
3792 HOSTDATA(shpnt)->count[i],
3793 HOSTDATA(shpnt)->time[i]);
3795 #endif
3797 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: pos=%p\n", pos);
3799 thislength = pos - (buffer + offset);
3800 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: length=%d thislength=%d\n", length, thislength);
3802 if(thislength<0) {
3803 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: output too short\n");
3804 *start = 0;
3805 return 0;
3808 thislength = thislength<length ? thislength : length;
3810 DPRINTK(debug_procinfo, KERN_DEBUG "aha152x_proc_info: return %d\n", thislength);
3812 *start = buffer + offset;
3813 return thislength < length ? thislength : length;
3816 #if defined(MODULE)
3817 /* Eventually this will go into an include file, but this will be later */
3818 Scsi_Host_Template driver_template = AHA152X;
3820 #include "scsi_module.c"
3821 #endif