root/drivers/scsi/aha152x.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. do_pause
  2. append_SC
  3. remove_first_SC
  4. remove_SC
  5. make_acklow
  6. getphase
  7. aha152x_setup
  8. aha152x_porttest
  9. aha152x_checksetup
  10. aha152x_detect
  11. aha152x_queue
  12. aha152x_command
  13. aha152x_abort
  14. aha152x_reset_ports
  15. aha152x_reset
  16. aha152x_biosparam
  17. aha152x_done
  18. aha152x_intr
  19. aha152x_panic
  20. disp_ports
  21. disp_enintr
  22. enter_driver
  23. leave_driver
  24. show_command
  25. show_queues
  26. aha152x_set_info
  27. get_command
  28. aha152x_proc_info

   1 /* aha152x.c -- Adaptec AHA-152x driver
   2  * Author: Juergen E. Fischer, fischer@et-inf.fho-emden.de
   3  * Copyright 1993, 1994, 1995 Juergen E. Fischer
   4  *
   5  *
   6  * This driver is based on
   7  *   fdomain.c -- Future Domain TMC-16x0 driver
   8  * which is
   9  *   Copyright 1992, 1993 Rickard E. Faith (faith@cs.unc.edu)
  10  *
  11  *
  12  * This program is free software; you can redistribute it and/or modify it
  13  * under the terms of the GNU General Public License as published by the
  14  * Free Software Foundation; either version 2, or (at your option) any
  15  * later version.
  16  *
  17  * This program is distributed in the hope that it will be useful, but
  18  * WITHOUT ANY WARRANTY; without even the implied warranty of
  19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20  * General Public License for more details.
  21  *
  22  *
  23  * $Id: aha152x.c,v 1.13 1996/01/09 02:15:53 fischer Exp $
  24  *
  25  * $Log: aha152x.c,v $
  26  * Revision 1.13  1996/01/09  02:15:53  fischer
  27  * - some cleanups
  28  * - moved request_irq behind controller initialization
  29  *   (to avoid spurious interrupts)
  30  *
  31  * Revision 1.12  1995/12/16  12:26:07  fischer
  32  * - barrier()'s added
  33  * - configurable RESET delay added
  34  *
  35  * Revision 1.11  1995/12/06  21:18:35  fischer
  36  * - some minor updates
  37  *
  38  * Revision 1.10  1995/07/22  19:18:45  fischer
  39  * - support for 2 controllers
  40  * - started synchronous data transfers (not working yet)
  41  *
  42  * Revision 1.9  1995/03/18  09:20:24  root
  43  * - patches for PCMCIA and modules
  44  *
  45  * Revision 1.8  1995/01/21  22:07:19  root
  46  * - snarf_region => request_region
  47  * - aha152x_intr interface change
  48  *
  49  * Revision 1.7  1995/01/02  23:19:36  root
  50  * - updated COMMAND_SIZE to cmd_len
  51  * - changed sti() to restore_flags()
  52  * - fixed some #ifdef which generated warnings
  53  *
  54  * Revision 1.6  1994/11/24  20:35:27  root
  55  * - problem with odd number of bytes in fifo fixed
  56  *
  57  * Revision 1.5  1994/10/30  14:39:56  root
  58  * - abort code fixed
  59  * - debugging improved
  60  *
  61  * Revision 1.4  1994/09/12  11:33:01  root
  62  * - irqaction to request_irq
  63  * - abortion updated
  64  *
  65  * Revision 1.3  1994/08/04  13:53:05  root
  66  * - updates for mid-level-driver changes
  67  * - accept unexpected BUSFREE phase as error condition
  68  * - parity check now configurable
  69  *
  70  * Revision 1.2  1994/07/03  12:56:36  root
  71  * - cleaned up debugging code
  72  * - more tweaking on reset delays
  73  * - updated abort/reset code (pretty untested...)
  74  *
  75  * Revision 1.1  1994/05/28  21:18:49  root
  76  * - update for mid-level interface change (abort-reset)
  77  * - delays after resets adjusted for some slow devices
  78  *
  79  * Revision 1.0  1994/03/25  12:52:00  root
  80  * - Fixed "more data than expected" problem
  81  * - added new BIOS signatures
  82  *
  83  * Revision 0.102  1994/01/31  20:44:12  root
  84  * - minor changes in insw/outsw handling
  85  *
  86  * Revision 0.101  1993/12/13  01:16:27  root
  87  * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
  88  *   fixes problems with CD-ROM sector size detection & media change)
  89  *
  90  * Revision 0.100  1993/12/10  16:58:47  root
  91  * - fix for unsuccessful selections in case of non-continuous id assignments
  92  *   on the scsi bus.
  93  *
  94  * Revision 0.99  1993/10/24  16:19:59  root
  95  * - fixed DATA IN (rare read errors gone)
  96  *
  97  * Revision 0.98  1993/10/17  12:54:44  root
  98  * - fixed some recent fixes (shame on me)
  99  * - moved initialization of scratch area to aha152x_queue
 100  *
 101  * Revision 0.97  1993/10/09  18:53:53  root
 102  * - DATA IN fixed. Rarely left data in the fifo.
 103  *
 104  * Revision 0.96  1993/10/03  00:53:59  root
 105  * - minor changes on DATA IN
 106  *
 107  * Revision 0.95  1993/09/24  10:36:01  root
 108  * - change handling of MSGI after reselection
 109  * - fixed sti/cli
 110  * - minor changes
 111  *
 112  * Revision 0.94  1993/09/18  14:08:22  root
 113  * - fixed bug in multiple outstanding command code
 114  * - changed detection
 115  * - support for kernel command line configuration
 116  * - reset corrected
 117  * - changed message handling
 118  *
 119  * Revision 0.93  1993/09/15  20:41:19  root
 120  * - fixed bugs with multiple outstanding commands
 121  *
 122  * Revision 0.92  1993/09/13  02:46:33  root
 123  * - multiple outstanding commands work (no problems with IBM drive)
 124  *
 125  * Revision 0.91  1993/09/12  20:51:46  root
 126  * added multiple outstanding commands
 127  * (some problem with this $%&? IBM device remain)
 128  *
 129  * Revision 0.9  1993/09/12  11:11:22  root
 130  * - corrected auto-configuration
 131  * - changed the auto-configuration (added some '#define's)
 132  * - added support for dis-/reconnection
 133  *
 134  * Revision 0.8  1993/09/06  23:09:39  root
 135  * - added support for the drive activity light
 136  * - minor changes
 137  *
 138  * Revision 0.7  1993/09/05  14:30:15  root
 139  * - improved phase detection
 140  * - now using the new snarf_region code of 0.99pl13
 141  *
 142  * Revision 0.6  1993/09/02  11:01:38  root
 143  * first public release; added some signatures and biosparam()
 144  *
 145  * Revision 0.5  1993/08/30  10:23:30  root
 146  * fixed timing problems with my IBM drive
 147  *
 148  * Revision 0.4  1993/08/29  14:06:52  root
 149  * fixed some problems with timeouts due incomplete commands
 150  *
 151  * Revision 0.3  1993/08/28  15:55:03  root
 152  * writing data works too.  mounted and worked on a dos partition
 153  *
 154  * Revision 0.2  1993/08/27  22:42:07  root
 155  * reading data works.  Mounted a msdos partition.
 156  *
 157  * Revision 0.1  1993/08/25  13:38:30  root
 158  * first "damn thing doesn't work" version
 159  *
 160  * Revision 0.0  1993/08/14  19:54:25  root
 161  * empty function bodies; detect() works.
 162  *
 163  *
 164  **************************************************************************
 165 
 166 
 167  
 168  DESCRIPTION:
 169 
 170  This is the Linux low-level SCSI driver for Adaptec AHA-1520/1522
 171  SCSI host adapters.
 172 
 173 
 174  PER-DEFINE CONFIGURABLE OPTIONS:
 175 
 176  AUTOCONF:
 177    use configuration the controller reports (only 152x)
 178 
 179  SKIP_BIOSTEST:
 180    Don't test for BIOS signature (AHA-1510 or disabled BIOS)
 181 
 182  SETUP0 { IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS, DELAY }:
 183    override for the first controller
 184    
 185  SETUP1 { IOPORT, IRQ, SCSI_ID, RECONNECT, PARITY, SYNCHRONOUS, DELAY }:
 186    override for the second controller
 187 
 188 
 189  LILO COMMAND LINE OPTIONS:
 190 
 191  aha152x=<IOPORT>[,<IRQ>[,<SCSI-ID>[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>]]]]]]
 192 
 193  The normal configuration can be overridden by specifying a command line.
 194  When you do this, the BIOS test is skipped. Entered values have to be
 195  valid (known). Don't use values that aren't supported under normal operation.
 196  If you think that you need other values: contact me.  For two controllers
 197  use the aha152x statement twice.
 198 
 199 
 200  REFERENCES USED:
 201 
 202  "AIC-6260 SCSI Chip Specification", Adaptec Corporation.
 203 
 204  "SCSI COMPUTER SYSTEM INTERFACE - 2 (SCSI-2)", X3T9.2/86-109 rev. 10h
 205 
 206  "Writing a SCSI device driver for Linux", Rik Faith (faith@cs.unc.edu)
 207 
 208  "Kernel Hacker's Guide", Michael K. Johnson (johnsonm@sunsite.unc.edu)
 209 
 210  "Adaptec 1520/1522 User's Guide", Adaptec Corporation.
 211  
 212  Michael K. Johnson (johnsonm@sunsite.unc.edu)
 213 
 214  Drew Eckhardt (drew@cs.colorado.edu)
 215 
 216  Eric Youngdale (ericy@cais.com) 
 217 
 218  special thanks to Eric Youngdale for the free(!) supplying the
 219  documentation on the chip.
 220 
 221  **************************************************************************/
 222 
 223 #ifdef PCMCIA
 224 #define MODULE
 225 #endif
 226 
 227 #include <linux/module.h>
 228 
 229 #ifdef PCMCIA
 230 #undef MODULE
 231 #endif
 232 
 233 #include <linux/sched.h>
 234 #include <asm/io.h>
 235 #include <linux/blk.h>
 236 #include "scsi.h"
 237 #include "sd.h"
 238 #include "hosts.h"
 239 #include "constants.h"
 240 #include <asm/system.h>
 241 #include <linux/errno.h>
 242 #include <linux/string.h>
 243 #include <linux/wait.h>
 244 #include <linux/ioport.h>
 245 #include <linux/proc_fs.h>
 246 
 247 #include "aha152x.h"
 248 #include <linux/stat.h>
 249 
 250 struct proc_dir_entry proc_scsi_aha152x = {
 251     PROC_SCSI_AHA152X, 7, "aha152x",
 252     S_IFDIR | S_IRUGO | S_IXUGO, 2
 253 };
 254 
 255 /* DEFINES */
 256 
 257 /* For PCMCIA cards, always use AUTOCONF */
 258 #if defined(PCMCIA) || defined(MODULE)
 259 #if !defined(AUTOCONF)
 260 #define AUTOCONF
 261 #endif
 262 #endif
 263 
 264 #if !defined(AUTOCONF) && !defined(SETUP0)
 265 #error define AUTOCONF or SETUP0
 266 #endif
 267 
 268 #if defined(DEBUG_AHA152X)
 269 
 270 #undef  SKIP_PORTS              /* don't display ports */
 271 
 272 #undef  DEBUG_QUEUE             /* debug queue() */
 273 #undef  DEBUG_RESET             /* debug reset() */
 274 #undef  DEBUG_INTR              /* debug intr() */
 275 #undef  DEBUG_SELECTION         /* debug selection part in intr() */
 276 #undef  DEBUG_MSGO              /* debug message out phase in intr() */
 277 #undef  DEBUG_MSGI              /* debug message in phase in intr() */
 278 #undef  DEBUG_STATUS            /* debug status phase in intr() */
 279 #undef  DEBUG_CMD               /* debug command phase in intr() */
 280 #undef  DEBUG_DATAI             /* debug data in phase in intr() */
 281 #undef  DEBUG_DATAO             /* debug data out phase in intr() */
 282 #undef  DEBUG_ABORT             /* debug abort() */
 283 #undef  DEBUG_DONE              /* debug done() */
 284 #undef  DEBUG_BIOSPARAM         /* debug biosparam() */
 285 
 286 #undef  DEBUG_RACE              /* debug race conditions */
 287 #undef  DEBUG_PHASES            /* debug phases (useful to trace) */
 288 #undef  DEBUG_QUEUES            /* debug reselection */
 289 
 290 /* recently used for debugging */
 291 #if 0
 292 #endif
 293 
 294 #define DEBUG_SELECTION
 295 #define DEBUG_PHASES
 296 #define DEBUG_RESET
 297 #define DEBUG_ABORT
 298 
 299 #define DEBUG_DEFAULT (debug_reset|debug_abort)
 300 
 301 #endif
 302 
 303 /* END OF DEFINES */
 304 
 305 extern long loops_per_sec;
 306 
 307 #define DELAY_DEFAULT 100
 308 
 309 /* some additional "phases" for getphase() */
 310 #define P_BUSFREE  1
 311 #define P_PARITY   2
 312 
 313 /* possible irq range */
 314 #define IRQ_MIN 9
 315 #define IRQ_MAX 12
 316 #define IRQS    IRQ_MAX-IRQ_MIN+1
 317 
 318 enum {
 319   not_issued   = 0x0001,
 320   in_selection = 0x0002,
 321   disconnected = 0x0004,
 322   aborted      = 0x0008,
 323   sent_ident   = 0x0010,
 324   in_other     = 0x0020,
 325   in_sync      = 0x0040,
 326   sync_ok      = 0x0080,
 327 };
 328 
 329 /* set by aha152x_setup according to the command line */
 330 static int  setup_count=0;
 331 static struct aha152x_setup {
 332   int io_port;
 333   int irq;
 334   int scsiid;
 335   int reconnect;
 336   int parity;
 337   int synchronous;
 338   int delay;
 339 #ifdef DEBUG_AHA152X
 340   int debug;
 341 #endif
 342   char *conf;
 343 } setup[2];
 344 
 345 static struct Scsi_Host *aha152x_host[IRQS];
 346 
 347 #define HOSTDATA(shpnt)   ((struct aha152x_hostdata *) &shpnt->hostdata)
 348 #define CURRENT_SC        (HOSTDATA(shpnt)->current_SC)
 349 #define ISSUE_SC          (HOSTDATA(shpnt)->issue_SC)
 350 #define DISCONNECTED_SC   (HOSTDATA(shpnt)->disconnected_SC)
 351 #define DELAY             (HOSTDATA(shpnt)->delay)
 352 #define SYNCRATE          (HOSTDATA(shpnt)->syncrate[CURRENT_SC->target])
 353 #define MSG(i)            (HOSTDATA(shpnt)->message[i])
 354 #define MSGLEN            (HOSTDATA(shpnt)->message_len)
 355 #define ADDMSG(x)         (MSG(MSGLEN++)=x)
 356 
 357 struct aha152x_hostdata {
 358   Scsi_Cmnd     *issue_SC;
 359   Scsi_Cmnd     *current_SC;
 360   Scsi_Cmnd     *disconnected_SC;
 361   int           aborting;
 362   int           abortion_complete;
 363   int           abort_result;
 364   int           commands;
 365   
 366   int           reconnect;
 367   int           parity;
 368   int           synchronous;
 369   int           delay;
 370  
 371   unsigned char syncrate[8];
 372   
 373   unsigned char message[256];
 374   int           message_len;
 375 
 376 #ifdef DEBUG_AHA152X
 377   int           debug;
 378 #endif
 379 };
 380 
 381 void aha152x_intr(int irq, struct pt_regs *);
 382 void aha152x_done(struct Scsi_Host *shpnt, int error);
 383 void aha152x_setup(char *str, int *ints);
 384 int aha152x_checksetup(struct aha152x_setup *setup);
 385 
 386 static void aha152x_reset_ports(struct Scsi_Host *shpnt);
 387 static void aha152x_panic(struct Scsi_Host *shpnt, char *msg);
 388 
 389 static void disp_ports(struct Scsi_Host *shpnt);
 390 static void show_command(Scsi_Cmnd *ptr);
 391 static void show_queues(struct Scsi_Host *shpnt);
 392 static void disp_enintr(struct Scsi_Host *shpnt);
 393 
 394 #if defined(DEBUG_RACE)
 395 static void enter_driver(const char *);
 396 static void leave_driver(const char *);
 397 #endif
 398 
 399 /* possible i/o addresses for the AIC-6260 */
 400 static unsigned short ports[] =
 401 {
 402   0x340,      /* default first */
 403   0x140
 404 };
 405 #define PORT_COUNT (sizeof(ports) / sizeof(unsigned short))
 406 
 407 #if !defined(SKIP_BIOSTEST)
 408 /* possible locations for the Adaptec BIOS */
 409 static void *addresses[] =
 410 {
 411   (void *) 0xdc000,   /* default first */
 412   (void *) 0xc8000,
 413   (void *) 0xcc000,
 414   (void *) 0xd0000,
 415   (void *) 0xd4000,
 416   (void *) 0xd8000,
 417   (void *) 0xe0000,
 418   (void *) 0xeb800,   /* VTech Platinum SMP */
 419   (void *) 0xf0000,
 420 };
 421 #define ADDRESS_COUNT (sizeof(addresses) / sizeof(void *))
 422 
 423 /* signatures for various AIC-6[23]60 based controllers.
 424    The point in detecting signatures is to avoid useless
 425    and maybe harmful probes on ports. I'm not sure that
 426    all listed boards pass auto-configuration. For those
 427    which fail the BIOS signature is obsolete, because
 428    user intervention to supply the configuration is 
 429    needed anyway. */
 430 static struct signature {
 431   char *signature;
 432   int  sig_offset;
 433   int  sig_length;
 434 } signatures[] =
 435 {
 436   { "Adaptec AHA-1520 BIOS",      0x102e, 21 },  /* Adaptec 152x */
 437   { "Adaptec ASW-B626 BIOS",      0x1029, 21 },  /* on-board controller */
 438   { "Adaptec BIOS: ASW-B626",       0x0f, 22 },  /* on-board controller */
 439   { "Adaptec ASW-B626 S2",        0x2e6c, 19 },  /* on-board controller */
 440   { "Adaptec BIOS:AIC-6360",         0xc, 21 },  /* on-board controller */
 441   { "ScsiPro SP-360 BIOS",        0x2873, 19 },  /* ScsiPro-Controller  */
 442   { "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },  /* Gigabyte Local-Bus-SCSI */
 443   { "Adaptec BIOS:AVA-282X",         0xc, 21 },  /* Adaptec 282x */
 444   { "Adaptec IBM Dock II SCSI",   0x2edd, 24 },  /* IBM Thinkpad Dock II */
 445   { "Adaptec BIOS:AHA-1532P",       0x1c, 22 },  /* IBM Thinkpad Dock II SCSI */
 446 };
 447 #define SIGNATURE_COUNT (sizeof(signatures) / sizeof(struct signature))
 448 #endif
 449 
 450 
 451 static void do_pause(unsigned amount) /* Pause for amount*10 milliseconds */
     /* [previous][next][first][last][top][bottom][index][help] */
 452 {
 453    unsigned long the_time = jiffies + amount; /* 0.01 seconds per jiffy */
 454 
 455    while (jiffies < the_time)
 456     barrier();
 457 }
 458 
 459 /*
 460  *  queue services:
 461  */
 462 static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
     /* [previous][next][first][last][top][bottom][index][help] */
 463 {
 464   Scsi_Cmnd *end;
 465 
 466   new_SC->host_scribble = (unsigned char *) NULL;
 467   if(!*SC)
 468     *SC=new_SC;
 469   else
 470     {
 471       for(end=*SC; end->host_scribble; end = (Scsi_Cmnd *) end->host_scribble)
 472         ;
 473       end->host_scribble = (unsigned char *) new_SC;
 474     }
 475 }
 476 
 477 static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
     /* [previous][next][first][last][top][bottom][index][help] */
 478 {
 479   Scsi_Cmnd *ptr;
 480 
 481   ptr=*SC;
 482   if(ptr)
 483     *SC= (Scsi_Cmnd *) (*SC)->host_scribble;
 484   return ptr;
 485 }
 486 
 487 static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489   Scsi_Cmnd *ptr, *prev;
 490 
 491   for(ptr=*SC, prev=NULL;
 492        ptr && ((ptr->target!=target) || (ptr->lun!=lun));
 493       prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
 494     ;
 495 
 496   if(ptr)
 497     if(prev)
 498       prev->host_scribble = ptr->host_scribble;
 499     else
 500       *SC= (Scsi_Cmnd *) ptr->host_scribble;
 501   return ptr;
 502 }
 503 
 504 /*
 505  * read inbound byte and wait for ACK to get low
 506  */
 507 static void make_acklow(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 508 {
 509   SETPORT(SXFRCTL0, CH1|SPIOEN);
 510   GETPORT(SCSIDAT);
 511   SETPORT(SXFRCTL0, CH1);
 512 
 513   while(TESTHI(SCSISIG, ACKI))
 514     barrier();
 515 }
 516 
 517 /*
 518  * detect current phase more reliable:
 519  * phase is valid, when the target asserts REQ after we've deasserted ACK.
 520  *
 521  * return value is a valid phase or an error code.
 522  *
 523  * errorcodes:
 524  *   P_BUSFREE   BUS FREE phase detected
 525  *   P_PARITY    parity error in DATA phase
 526  */
 527 static int getphase(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 528 {
 529   int phase, sstat1;
 530   
 531   while(1)
 532     {
 533       do
 534         {
 535           while(!((sstat1 = GETPORT(SSTAT1)) & (BUSFREE|SCSIRSTI|REQINIT)))
 536             barrier();
 537           if(sstat1 & BUSFREE)
 538             return P_BUSFREE;
 539           if(sstat1 & SCSIRSTI)
 540             {
 541               printk("aha152x: RESET IN\n");
 542               SETPORT(SSTAT1, SCSIRSTI);
 543             }
 544         }
 545       while(TESTHI(SCSISIG, ACKI) || TESTLO(SSTAT1, REQINIT));
 546 
 547       SETPORT(SSTAT1, CLRSCSIPERR);
 548   
 549       phase = GETPORT(SCSISIG) & P_MASK ;
 550 
 551       if(TESTHI(SSTAT1, SCSIPERR))
 552         {
 553           if((phase & (CDO|MSGO))==0)                        /* DATA phase */
 554             return P_PARITY;
 555 
 556           make_acklow(shpnt);
 557         }
 558       else
 559         return phase;
 560     }
 561 }
 562 
 563 /* called from init/main.c */
 564 void aha152x_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 565 {
 566   if(setup_count>2)
 567     panic("aha152x: you can only configure up to two controllers\n");
 568 
 569   setup[setup_count].conf        = str;
 570   setup[setup_count].io_port     = ints[0] >= 1 ? ints[1] : 0x340;
 571   setup[setup_count].irq         = ints[0] >= 2 ? ints[2] : 11;
 572   setup[setup_count].scsiid      = ints[0] >= 3 ? ints[3] : 7;
 573   setup[setup_count].reconnect   = ints[0] >= 4 ? ints[4] : 1;
 574   setup[setup_count].parity      = ints[0] >= 5 ? ints[5] : 1;
 575   setup[setup_count].synchronous = ints[0] >= 6 ? ints[6] : 0 /* FIXME: 1 */;
 576   setup[setup_count].delay       = ints[0] >= 7 ? ints[7] : DELAY_DEFAULT;
 577 #ifdef DEBUG_AHA152X
 578   setup[setup_count].debug       = ints[0] >= 8 ? ints[8] : DEBUG_DEFAULT;
 579   if(ints[0]>8)
 580     { 
 581       printk("aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
 582                  "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>[,<DEBUG>]]]]]]]\n");
 583 #else
 584   if(ints[0]>7)
 585     {
 586       printk("aha152x: usage: aha152x=<IOBASE>[,<IRQ>[,<SCSI ID>"
 587              "[,<RECONNECT>[,<PARITY>[,<SYNCHRONOUS>[,<DELAY>]]]]]]\n");
 588 #endif
 589     }
 590   else 
 591     setup_count++;
 592 }
 593 
 594 /*
 595    Test, if port_base is valid.
 596  */
 597 static int aha152x_porttest(int io_port)
     /* [previous][next][first][last][top][bottom][index][help] */
 598 {
 599   int i;
 600 
 601   if(check_region(io_port, IO_RANGE))
 602     return 0;
 603 
 604   SETPORT(io_port+O_DMACNTRL1, 0);          /* reset stack pointer */
 605   for(i=0; i<16; i++)
 606     SETPORT(io_port+O_STACK, i);
 607 
 608   SETPORT(io_port+O_DMACNTRL1, 0);          /* reset stack pointer */
 609   for(i=0; i<16 && GETPORT(io_port+O_STACK)==i; i++)
 610     ;
 611 
 612   return(i==16);
 613 }
 614 
 615 int aha152x_checksetup(struct aha152x_setup *setup)
     /* [previous][next][first][last][top][bottom][index][help] */
 616 {
 617   int i;
 618   
 619 #ifndef PCMCIA
 620   for(i=0; i<PORT_COUNT && (setup->io_port != ports[i]); i++)
 621     ;
 622   
 623   if(i==PORT_COUNT)
 624     return 0;
 625 #endif
 626   
 627   if(!aha152x_porttest(setup->io_port))
 628     return 0;
 629   
 630   if((setup->irq < IRQ_MIN) && (setup->irq > IRQ_MAX))
 631     return 0;
 632   
 633   if((setup->scsiid < 0) || (setup->scsiid > 7))
 634     return 0;
 635   
 636   if((setup->reconnect < 0) || (setup->reconnect > 1))
 637     return 0;
 638   
 639   if((setup->parity < 0) || (setup->parity > 1))
 640     return 0;
 641   
 642   if((setup->synchronous < 0) || (setup->synchronous > 1))
 643     return 0;
 644   
 645   return 1;
 646 }
 647 
 648 
 649 int aha152x_detect(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 650 {
 651   int                 i, j, ok;
 652 #if defined(AUTOCONF)
 653   aha152x_config      conf;
 654 #endif
 655   
 656   tpnt->proc_dir = &proc_scsi_aha152x;
 657 
 658   for(i=0; i<IRQS; i++)
 659     aha152x_host[i] = (struct Scsi_Host *) NULL;
 660   
 661   if(setup_count)
 662     {
 663       printk("aha152x: processing commandline: ");
 664    
 665       for(i=0; i<setup_count; i++)
 666         if(!aha152x_checksetup(&setup[i]))
 667         {
 668                 printk("\naha152x: %s\n", setup[i].conf);
 669                 printk("aha152x: invalid line (controller=%d)\n", i+1);
 670         }
 671 
 672       printk("ok\n");
 673         }
 674 
 675 #ifdef SETUP0
 676   if(setup_count<2)
 677         {
 678       struct aha152x_setup override = SETUP0;
 679 
 680       if(setup_count==0 || (override.io_port != setup[0].io_port))
 681         if(!aha152x_checksetup(&override))
 682         {
 683                 printk("\naha152x: SETUP0 (0x%x, %d, %d, %d, %d, %d, %d) invalid\n",
 684                       override.io_port,
 685                       override.irq,
 686                       override.scsiid,
 687                       override.reconnect,
 688                       override.parity,
 689                       override.synchronous,
 690                       override.delay);
 691         }
 692         else
 693           setup[setup_count++] = override;
 694         }
 695 #endif
 696 
 697 #ifdef SETUP1
 698   if(setup_count<2)
 699         {
 700       struct aha152x_setup override = SETUP1;
 701 
 702       if(setup_count==0 || (override.io_port != setup[0].io_port))
 703         if(!aha152x_checksetup(&override))
 704         {
 705                 printk("\naha152x: SETUP1 (0x%x, %d, %d, %d, %d, %d, %d) invalid\n",
 706                        override.io_port,
 707                        override.irq,
 708                        override.scsiid,
 709                        override.reconnect,
 710                        override.parity,
 711                        override.synchronous,
 712                        override.delay);
 713     }
 714   else
 715           setup[setup_count++] = override;
 716     }
 717 #endif
 718   
 719 #if defined(AUTOCONF)
 720   if(setup_count<2)
 721     {
 722 #if !defined(SKIP_BIOSTEST)
 723       ok=0;
 724       for(i=0; i < ADDRESS_COUNT && !ok; i++)
 725             for(j=0; (j < SIGNATURE_COUNT) && !ok; j++)
 726           ok=!memcmp((void *) addresses[i]+signatures[j].sig_offset,
 727                      (void *) signatures[j].signature,
 728                      (int) signatures[j].sig_length);
 729 
 730       if(!ok && setup_count==0)
 731         return 0;
 732 
 733       printk("aha152x: BIOS test: passed, ");
 734 #else
 735       printk("aha152x: ");
 736 #endif /* !SKIP_BIOSTEST */
 737  
 738       for(i=0; i<PORT_COUNT && setup_count<2; i++)
 739             {
 740               if((setup_count==1) && (setup[0].io_port == ports[i]))
 741                 continue;
 742 
 743               if(aha152x_porttest(ports[i]))
 744         {
 745                   setup[setup_count].io_port = ports[i];
 746               
 747                   conf.cf_port =
 748                         (GETPORT(ports[i]+O_PORTA)<<8) + GETPORT(ports[i]+O_PORTB);
 749               
 750                   setup[setup_count].irq         = IRQ_MIN + conf.cf_irq;
 751                   setup[setup_count].scsiid      = conf.cf_id;
 752                   setup[setup_count].reconnect   = conf.cf_tardisc;
 753                   setup[setup_count].parity      = !conf.cf_parity;
 754                   setup[setup_count].synchronous = 0 /* FIXME: conf.cf_syncneg */;
 755                   setup[setup_count].delay       = DELAY_DEFAULT;
 756 #ifdef DEBUG_AHA152X
 757                   setup[setup_count].debug       = DEBUG_DEFAULT;
 758 #endif
 759                   setup_count++;
 760                 }
 761         }
 762 
 763       printk("auto configuration: ok, ");
 764     }
 765 #endif
 766 
 767   printk("detection complete\n");
 768 
 769   for(i=0; i<setup_count; i++)
 770     {
 771       struct Scsi_Host        *shpnt;
 772 
 773       shpnt = aha152x_host[setup[i].irq-IRQ_MIN] =
 774         scsi_register(tpnt, sizeof(struct aha152x_hostdata));
 775 
 776       shpnt->io_port                     = setup[i].io_port;
 777       shpnt->n_io_port                   = IO_RANGE;
 778       shpnt->irq                         = setup[i].irq;
 779 
 780       ISSUE_SC                           = (Scsi_Cmnd *) NULL;
 781       CURRENT_SC                         = (Scsi_Cmnd *) NULL;
 782       DISCONNECTED_SC                    = (Scsi_Cmnd *) NULL;
 783 
 784       HOSTDATA(shpnt)->reconnect         = setup[i].reconnect;
 785       HOSTDATA(shpnt)->parity            = setup[i].parity;
 786       HOSTDATA(shpnt)->synchronous       = setup[i].synchronous;
 787       HOSTDATA(shpnt)->delay             = setup[i].delay;
 788 #ifdef DEBUG_AHA152X
 789       HOSTDATA(shpnt)->debug             = setup[i].debug;
 790 #endif
 791 
 792       HOSTDATA(shpnt)->aborting          = 0;
 793       HOSTDATA(shpnt)->abortion_complete = 0;
 794       HOSTDATA(shpnt)->abort_result      = 0;
 795       HOSTDATA(shpnt)->commands          = 0;
 796 
 797       HOSTDATA(shpnt)->message_len       = 0;
 798 
 799       for(j=0; j<8; j++)
 800         HOSTDATA(shpnt)->syncrate[j] = 0;
 801  
 802       SETPORT(SCSIID, setup[i].scsiid << 4);
 803       shpnt->this_id=setup[i].scsiid;
 804   
 805       if(setup[i].reconnect)
 806         shpnt->hostt->can_queue=AHA152X_MAXQUEUE;
 807 
 808   /* RESET OUT */
 809       SETBITS(SCSISEQ, SCSIRSTO);
 810   do_pause(30);
 811       CLRBITS(SCSISEQ, SCSIRSTO);
 812       do_pause(setup[i].delay);
 813 
 814       aha152x_reset_ports(shpnt);
 815       
 816       printk("aha152x%d: vital data: PORTBASE=0x%03x, IRQ=%d, SCSI ID=%d,"
 817              " reconnect=%s, parity=%s, synchronous=%s, delay=%d\n",
 818              i,
 819              shpnt->io_port,
 820              shpnt->irq,
 821              shpnt->this_id,
 822              HOSTDATA(shpnt)->reconnect ? "enabled" : "disabled",
 823              HOSTDATA(shpnt)->parity ? "enabled" : "disabled",
 824              HOSTDATA(shpnt)->synchronous ? "enabled" : "disabled",
 825              HOSTDATA(shpnt)->delay);
 826 
 827       request_region(shpnt->io_port, IO_RANGE, "aha152x");  /* Register */
 828   
 829   /* not expecting any interrupts */
 830   SETPORT(SIMODE0, 0);
 831   SETPORT(SIMODE1, 0);
 832 
 833       SETBITS(DMACNTRL0, INTEN);
 834 
 835       ok = request_irq(setup[i].irq, aha152x_intr, SA_INTERRUPT, "aha152x");
 836       
 837       if(ok<0)
 838         {
 839           if(ok == -EINVAL)
 840             {
 841               printk("aha152x%d: bad IRQ %d.\n", i, setup[i].irq);
 842               printk("          Contact author.\n");
 843             }
 844           else
 845             if(ok == -EBUSY)
 846               printk("aha152x%d: IRQ %d already in use. Configure another.\n",
 847                      i, setup[i].irq);
 848             else
 849               {
 850                 printk("\naha152x%d: Unexpected error code on"
 851                        " requesting IRQ %d.\n", i, setup[i].irq);
 852                 printk("          Contact author.\n");
 853               }
 854           printk("aha152x: driver needs an IRQ.\n");
 855           continue;
 856         }
 857     }
 858   
 859   return (setup_count>0);
 860 }
 861 
 862 /* 
 863  *  Queue a command and setup interrupts for a free bus.
 864  */
 865 int aha152x_queue(Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 866 {
 867   struct Scsi_Host *shpnt = SCpnt->host;
 868   unsigned long flags;
 869 
 870 #if defined(DEBUG_RACE)
 871   enter_driver("queue");
 872 #else
 873 #if defined(DEBUG_QUEUE)
 874   if(HOSTDATA(shpnt)->debug & debug_queue)
 875     printk("aha152x: queue(), ");
 876 #endif
 877 #endif
 878 
 879 #if defined(DEBUG_QUEUE)
 880   if(HOSTDATA(shpnt)->debug & debug_queue)
 881   {
 882       printk("SCpnt (target = %d lun = %d cmnd = ",
 883              SCpnt->target, SCpnt->lun);
 884     print_command(SCpnt->cmnd);
 885       printk(", cmd_len=%d, pieces = %d size = %u), ",
 886              SCpnt->cmd_len, SCpnt->use_sg, SCpnt->request_bufflen);
 887       disp_ports(shpnt);
 888   }
 889 #endif
 890 
 891   SCpnt->scsi_done =       done;
 892 
 893   /* setup scratch area
 894      SCp.ptr              : buffer pointer
 895      SCp.this_residual    : buffer length
 896      SCp.buffer           : next buffer
 897      SCp.buffers_residual : left buffers in list
 898      SCp.phase            : current state of the command */
 899   SCpnt->SCp.phase = not_issued;
 900   if (SCpnt->use_sg)
 901     {
 902       SCpnt->SCp.buffer =
 903         (struct scatterlist *) SCpnt->request_buffer;
 904       SCpnt->SCp.ptr              = SCpnt->SCp.buffer->address;
 905       SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
 906       SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
 907     }
 908   else
 909     {
 910       SCpnt->SCp.ptr              = (char *)SCpnt->request_buffer;
 911       SCpnt->SCp.this_residual    = SCpnt->request_bufflen;
 912       SCpnt->SCp.buffer           = NULL;
 913       SCpnt->SCp.buffers_residual = 0;
 914     }
 915           
 916   SCpnt->SCp.Status              = CHECK_CONDITION;
 917   SCpnt->SCp.Message             = 0;
 918   SCpnt->SCp.have_data_in        = 0;
 919   SCpnt->SCp.sent_command        = 0;
 920 
 921   /* Turn led on, when this is the first command. */
 922   save_flags(flags);
 923   cli();
 924   HOSTDATA(shpnt)->commands++;
 925   if(HOSTDATA(shpnt)->commands==1)
 926     SETPORT(PORTA, 1);
 927 
 928 #if defined(DEBUG_QUEUES)
 929   if(HOSTDATA(shpnt)->debug & debug_queues)
 930     printk("i+ (%d), ", HOSTDATA(shpnt)->commands);
 931 #endif
 932   append_SC(&ISSUE_SC, SCpnt);
 933   
 934   /* Enable bus free interrupt, when we aren't currently on the bus */
 935   if(!CURRENT_SC)
 936     {
 937       SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
 938       SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
 939     }
 940   restore_flags(flags);
 941 
 942 #if defined(DEBUG_RACE)
 943   leave_driver("queue");
 944 #endif
 945 
 946   return 0;
 947 }
 948 
 949 /*
 950  *  We only support commands in interrupt-driven fashion
 951  */
 952 int aha152x_command(Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 953 {
 954   printk("aha152x: interrupt driven driver; use aha152x_queue()\n");
 955   return -1;
 956 }
 957 
 958 /*
 959  *  Abort a queued command
 960  *  (commands that are on the bus can't be aborted easily)
 961  */
 962 int aha152x_abort(Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 963 {
 964   struct Scsi_Host *shpnt = SCpnt->host;
 965   unsigned long flags;
 966   Scsi_Cmnd *ptr, *prev;
 967 
 968   save_flags(flags);
 969   cli();
 970 
 971 #if defined(DEBUG_ABORT)
 972   if(HOSTDATA(shpnt)->debug & debug_abort)
 973   { 
 974       printk("aha152x: abort(), SCpnt=0x%08x, ", (unsigned int) SCpnt);
 975       show_queues(shpnt);
 976   }
 977 #endif
 978 
 979   /* look for command in issue queue */
 980   for(ptr=ISSUE_SC, prev=NULL;
 981        ptr && ptr!=SCpnt;
 982        prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
 983     ;
 984 
 985   if(ptr)
 986     {
 987       /* dequeue */
 988       if(prev)
 989         prev->host_scribble = ptr->host_scribble;
 990       else
 991         ISSUE_SC = (Scsi_Cmnd *) ptr->host_scribble;
 992       restore_flags(flags);
 993 
 994       ptr->host_scribble = NULL;
 995       ptr->result = DID_ABORT << 16;
 996       ptr->scsi_done(ptr);
 997       return SCSI_ABORT_SUCCESS;
 998     }
 999 
1000   /* if the bus is busy or a command is currently processed,
1001      we can't do anything more */
1002   if (TESTLO(SSTAT1, BUSFREE) || (CURRENT_SC && CURRENT_SC!=SCpnt))
1003     {
1004       /* fail abortion, if bus is busy */
1005 
1006       if(!CURRENT_SC)
1007         printk("bus busy w/o current command, ");
1008  
1009       restore_flags(flags);
1010       return SCSI_ABORT_BUSY;
1011     }
1012 
1013   /* bus is free */
1014 
1015   if(CURRENT_SC)
1016   { 
1017     /* target entered bus free before COMMAND COMPLETE, nothing to abort */
1018     restore_flags(flags);
1019       CURRENT_SC->result = DID_ERROR << 16;
1020       CURRENT_SC->scsi_done(CURRENT_SC);
1021       CURRENT_SC = (Scsi_Cmnd *) NULL;
1022     return SCSI_ABORT_SUCCESS;
1023   }
1024 
1025   /* look for command in disconnected queue */
1026   for(ptr=DISCONNECTED_SC, prev=NULL;
1027        ptr && ptr!=SCpnt;
1028        prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
1029     ;
1030 
1031   if(ptr)
1032     if(!HOSTDATA(shpnt)->aborting)
1033       {
1034         /* dequeue */
1035         if(prev)
1036           prev->host_scribble = ptr->host_scribble;
1037         else
1038           DISCONNECTED_SC = (Scsi_Cmnd *) ptr->host_scribble;
1039   
1040         /* set command current and initiate selection,
1041            let the interrupt routine take care of the abortion */
1042         CURRENT_SC     = ptr;
1043         ptr->SCp.phase = in_selection|aborted;
1044         SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
1045         
1046         ADDMSG(ABORT);
1047   
1048         /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
1049         SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1050         SETPORT(SIMODE1, ENSELTIMO);
1051   
1052         /* Enable SELECTION OUT sequence */
1053         SETBITS(SCSISEQ, ENSELO | ENAUTOATNO);
1054   
1055         SETBITS(DMACNTRL0, INTEN);
1056         HOSTDATA(shpnt)->abort_result=SCSI_ABORT_SUCCESS;
1057         HOSTDATA(shpnt)->aborting++;
1058         HOSTDATA(shpnt)->abortion_complete=0;
1059 
1060         sti();  /* Hi Eric, guess what ;-) */
1061   
1062         /* sleep until the abortion is complete */
1063         while(!HOSTDATA(shpnt)->abortion_complete)
1064           barrier();
1065         HOSTDATA(shpnt)->aborting=0;
1066         return HOSTDATA(shpnt)->abort_result;
1067       }
1068     else
1069       {
1070         /* we're already aborting a command */
1071         restore_flags(flags);
1072         return SCSI_ABORT_BUSY;
1073       }
1074 
1075   /* command wasn't found */
1076   printk("command not found\n");
1077   restore_flags(flags);
1078   return SCSI_ABORT_NOT_RUNNING;
1079 }
1080 
1081 /*
1082  *  Restore default values to the AIC-6260 registers and reset the fifos
1083  */
1084 static void aha152x_reset_ports(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1085 {
1086   /* disable interrupts */
1087   SETPORT(DMACNTRL0, RSTFIFO);
1088 
1089   SETPORT(SCSISEQ, 0);
1090 
1091   SETPORT(SXFRCTL1, 0);
1092   SETPORT(SCSISIG, 0);
1093   SETPORT(SCSIRATE, 0);
1094 
1095   /* clear all interrupt conditions */
1096   SETPORT(SSTAT0, 0x7f);
1097   SETPORT(SSTAT1, 0xef);
1098 
1099   SETPORT(SSTAT4, SYNCERR|FWERR|FRERR);
1100 
1101   SETPORT(DMACNTRL0, 0);
1102   SETPORT(DMACNTRL1, 0);
1103 
1104   SETPORT(BRSTCNTRL, 0xf1);
1105 
1106   /* clear SCSI fifo and transfer count */
1107   SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
1108   SETPORT(SXFRCTL0, CH1);
1109 
1110   /* enable interrupts */
1111   SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1112   SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1113 }
1114 
1115 /*
1116  *  Reset registers, reset a hanging bus and
1117  *  kill active and disconnected commands for target w/o soft reset
1118  */
1119 int aha152x_reset(Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1120 {
1121   struct Scsi_Host *shpnt = SCpnt->host;
1122   unsigned long flags;
1123   Scsi_Cmnd *ptr, *prev, *next;
1124 
1125   aha152x_reset_ports(shpnt);
1126 
1127   /* Reset, if bus hangs */
1128   if(TESTLO(SSTAT1, BUSFREE))
1129     {
1130        CLRBITS(DMACNTRL0, INTEN);
1131 
1132 #if defined(DEBUG_RESET)
1133        if(HOSTDATA(shpnt)->debug & debug_reset)
1134   {
1135        printk("aha152x: reset(), bus not free: SCSI RESET OUT\n");
1136            show_queues(shpnt);
1137   }
1138 #endif
1139 
1140        ptr=CURRENT_SC;
1141        if(ptr && !ptr->device->soft_reset)
1142          {
1143            ptr->host_scribble = NULL;
1144            ptr->result = DID_RESET << 16;
1145            ptr->scsi_done(CURRENT_SC);
1146            CURRENT_SC=NULL;
1147          }
1148 
1149        save_flags(flags);
1150        cli();
1151        prev=NULL; ptr=DISCONNECTED_SC;
1152        while(ptr)
1153          {
1154            if(!ptr->device->soft_reset)
1155              {
1156                if(prev)
1157                  prev->host_scribble = ptr->host_scribble;
1158                else
1159                  DISCONNECTED_SC = (Scsi_Cmnd *) ptr->host_scribble;
1160 
1161                next = (Scsi_Cmnd *) ptr->host_scribble;
1162   
1163                ptr->host_scribble = NULL;
1164                ptr->result        = DID_RESET << 16;
1165                ptr->scsi_done(ptr);
1166   
1167                ptr = next; 
1168              }
1169            else
1170              {
1171                prev=ptr;
1172                ptr = (Scsi_Cmnd *) ptr->host_scribble;
1173              }
1174          }
1175        restore_flags(flags);
1176 
1177 #if defined(DEBUG_RESET)
1178        if(HOSTDATA(shpnt)->debug & debug_reset)
1179        {
1180          printk("commands on targets w/ soft-resets:\n");
1181            show_queues(shpnt);
1182        }
1183 #endif
1184 
1185        /* RESET OUT */
1186        SETPORT(SCSISEQ, SCSIRSTO);
1187        do_pause(30);
1188        SETPORT(SCSISEQ, 0);
1189        do_pause(DELAY);
1190 
1191        SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1192        SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1193 
1194        SETPORT(DMACNTRL0, INTEN);
1195     }
1196 
1197   return SCSI_RESET_SUCCESS;
1198 }
1199 
1200 /*
1201  * Return the "logical geometry"
1202  */
1203 int aha152x_biosparam(Scsi_Disk * disk, kdev_t dev, int *info_array)
     /* [previous][next][first][last][top][bottom][index][help] */
1204 {
1205   int size = disk->capacity;
1206 
1207 #if defined(DEBUG_BIOSPARAM)
1208   if(HOSTDATA(shpnt)->debug & debug_biosparam)
1209     printk("aha152x_biosparam: dev=%s, size=%d, ", kdevname(dev), size);
1210 #endif
1211   
1212 /* I took this from other SCSI drivers, since it provides
1213    the correct data for my devices. */
1214   info_array[0]=64;
1215   info_array[1]=32;
1216   info_array[2]=size>>11;
1217 
1218 #if defined(DEBUG_BIOSPARAM)
1219   if(HOSTDATA(shpnt)->debug & debug_biosparam)
1220   {
1221     printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
1222            info_array[0], info_array[1], info_array[2]);
1223     printk("WARNING: check, if the bios geometry is correct.\n");
1224   }
1225 #endif
1226 
1227   return 0;
1228 }
1229 
1230 /*
1231  *  Internal done function
1232  */
1233 void aha152x_done(struct Scsi_Host *shpnt, int error)
     /* [previous][next][first][last][top][bottom][index][help] */
1234 {
1235   unsigned long flags;
1236   Scsi_Cmnd *done_SC;
1237 
1238 #if defined(DEBUG_DONE)
1239   if(HOSTDATA(shpnt)->debug & debug_done)
1240   {
1241     printk("\naha152x: done(), ");
1242     disp_ports(shpnt);
1243   }
1244 #endif
1245 
1246   if (CURRENT_SC)
1247     {
1248 #if defined(DEBUG_DONE)
1249       if(HOSTDATA(shpnt)->debug & debug_done)
1250         printk("done(%x), ", error);
1251 #endif
1252 
1253       save_flags(flags);
1254       cli();
1255 
1256       done_SC = CURRENT_SC;
1257       CURRENT_SC = NULL;
1258 
1259       /* turn led off, when no commands are in the driver */
1260       HOSTDATA(shpnt)->commands--;
1261       if(!HOSTDATA(shpnt)->commands)
1262         SETPORT(PORTA, 0);                                  /* turn led off */
1263 
1264 #if defined(DEBUG_QUEUES)
1265       if(HOSTDATA(shpnt)->debug & debug_queues) 
1266         printk("ok (%d), ", HOSTDATA(shpnt)->commands);
1267 #endif
1268       restore_flags(flags);
1269 
1270       SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1271       SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1272 
1273 #if defined(DEBUG_PHASES)
1274       if(HOSTDATA(shpnt)->debug & debug_phases)
1275         printk("BUS FREE loop, ");
1276 #endif
1277       while(TESTLO(SSTAT1, BUSFREE))
1278         barrier();
1279 #if defined(DEBUG_PHASES)
1280       if(HOSTDATA(shpnt)->debug & debug_phases)
1281         printk("BUS FREE\n");
1282 #endif
1283 
1284       done_SC->result = error;
1285       if(done_SC->scsi_done)
1286         {
1287 #if defined(DEBUG_DONE)
1288           if(HOSTDATA(shpnt)->debug & debug_done)
1289             printk("calling scsi_done, ");
1290 #endif
1291           done_SC->scsi_done(done_SC);
1292 #if defined(DEBUG_DONE)
1293           if(HOSTDATA(shpnt)->debug & debug_done)
1294             printk("done returned, ");
1295 #endif
1296         }
1297       else
1298         panic("aha152x: current_SC->scsi_done() == NULL");
1299     }
1300   else
1301     aha152x_panic(shpnt, "done() called outside of command");
1302 }
1303 
1304 /*
1305  * Interrupts handler (main routine of the driver)
1306  */
1307 void aha152x_intr(int irqno, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1308 {
1309   struct Scsi_Host *shpnt = aha152x_host[irqno-IRQ_MIN];
1310   unsigned int flags;
1311   int done=0, phase;
1312 
1313 #if defined(DEBUG_RACE)
1314   enter_driver("intr");
1315 #else
1316 #if defined(DEBUG_INTR)
1317   if(HOSTDATA(shpnt)->debug & debug_intr)
1318     printk("\naha152x: intr(), ");
1319 #endif
1320 #endif
1321 
1322   /* no more interrupts from the controller, while we busy.
1323      INTEN has to be restored, when we're ready to leave
1324      intr(). To avoid race conditions we have to return
1325      immediately afterwards. */
1326   CLRBITS(DMACNTRL0, INTEN);
1327   sti();  /* Yes, sti() really needs to be here */
1328 
1329   /* disconnected target is trying to reconnect.
1330      Only possible, if we have disconnected nexuses and
1331      nothing is occupying the bus.
1332   */
1333   if(TESTHI(SSTAT0, SELDI) &&
1334       DISCONNECTED_SC &&
1335       (!CURRENT_SC || (CURRENT_SC->SCp.phase & in_selection)) )
1336     {
1337       int identify_msg, target, i;
1338 
1339       /* Avoid conflicts when a target reconnects
1340          while we are trying to connect to another. */
1341       if(CURRENT_SC)
1342         {
1343 #if defined(DEBUG_QUEUES)
1344           if(HOSTDATA(shpnt)->debug & debug_queues)
1345           printk("i+, ");
1346 #endif
1347           save_flags(flags);
1348           cli();
1349           append_SC(&ISSUE_SC, CURRENT_SC);
1350           CURRENT_SC=NULL;
1351           restore_flags(flags);
1352         }
1353 
1354       /* disable sequences */
1355       SETPORT(SCSISEQ, 0);
1356       SETPORT(SSTAT0, CLRSELDI);
1357       SETPORT(SSTAT1, CLRBUSFREE);
1358 
1359 #if defined(DEBUG_QUEUES) || defined(DEBUG_PHASES)
1360       if(HOSTDATA(shpnt)->debug & (debug_queues|debug_phases))
1361         printk("reselected, ");
1362 #endif
1363 
1364       i = GETPORT(SELID) & ~(1 << shpnt->this_id);
1365       target=0;
1366       if(i)
1367         for(; (i & 1)==0; target++, i>>=1)
1368           ;
1369       else
1370         aha152x_panic(shpnt, "reconnecting target unknown");
1371 
1372 #if defined(DEBUG_QUEUES)
1373       if(HOSTDATA(shpnt)->debug & debug_queues)
1374         printk("SELID=%02x, target=%d, ", GETPORT(SELID), target);
1375 #endif
1376       SETPORT(SCSIID, (shpnt->this_id << OID_) | target);
1377       SETPORT(SCSISEQ, ENRESELI);
1378 
1379       if(TESTLO(SSTAT0, SELDI))
1380         aha152x_panic(shpnt, "RESELI failed");
1381 
1382       SETPORT(SCSIRATE, HOSTDATA(shpnt)->syncrate[target]&0x7f);
1383 
1384       SETPORT(SCSISIG, P_MSGI);
1385 
1386       /* Get identify message */
1387       if((i=getphase(shpnt))!=P_MSGI)
1388         {
1389           printk("target doesn't enter MSGI to identify (phase=%02x)\n", i);
1390           aha152x_panic(shpnt, "unknown lun");
1391         }
1392       SETPORT(SCSISEQ, 0);
1393 
1394       SETPORT(SXFRCTL0, CH1);
1395 
1396       identify_msg = GETPORT(SCSIBUS);
1397 
1398       if(!(identify_msg & IDENTIFY_BASE))
1399         {
1400           printk("target=%d, inbound message (%02x) != IDENTIFY\n",
1401                  target, identify_msg);
1402           aha152x_panic(shpnt, "unknown lun");
1403         }
1404 
1405       make_acklow(shpnt);
1406       getphase(shpnt);
1407 
1408 #if defined(DEBUG_QUEUES)
1409       if(HOSTDATA(shpnt)->debug & debug_queues)
1410         printk("identify=%02x, lun=%d, ", identify_msg, identify_msg & 0x3f);
1411 #endif
1412 
1413       save_flags(flags);
1414       cli();
1415 
1416 #if defined(DEBUG_QUEUES)
1417       if(HOSTDATA(shpnt)->debug & debug_queues)
1418         printk("d-, ");
1419 #endif
1420       CURRENT_SC = remove_SC(&DISCONNECTED_SC,
1421                               target,
1422                              identify_msg & 0x3f);
1423 
1424       if(!CURRENT_SC)
1425         {
1426           printk("lun=%d, ", identify_msg & 0x3f);
1427           aha152x_panic(shpnt, "no disconnected command for that lun");
1428         }
1429 
1430       CURRENT_SC->SCp.phase &= ~disconnected;
1431       restore_flags(flags);
1432 
1433       SETPORT(SIMODE0, 0);
1434       SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
1435 #if defined(DEBUG_RACE)
1436       leave_driver("(reselected) intr");
1437 #endif
1438       SETBITS(DMACNTRL0, INTEN);
1439       return;
1440     }
1441   
1442   /* Check, if we aren't busy with a command */
1443   if(!CURRENT_SC)
1444     {
1445       /* bus is free to issue a queued command */
1446       if(TESTHI(SSTAT1, BUSFREE) && ISSUE_SC)
1447         {
1448           save_flags(flags);
1449           cli();
1450 #if defined(DEBUG_QUEUES)
1451           if(HOSTDATA(shpnt)->debug & debug_queues)
1452             printk("i-, ");
1453 #endif
1454           CURRENT_SC = remove_first_SC(&ISSUE_SC);
1455           restore_flags(flags);
1456 
1457 #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1458           if(HOSTDATA(shpnt)->debug & (debug_intr|debug_selection|debug_phases))
1459             printk("issuing command, ");
1460 #endif
1461           CURRENT_SC->SCp.phase = in_selection;
1462 
1463 #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1464           if(HOSTDATA(shpnt)->debug & (debug_intr|debug_selection|debug_phases))
1465             printk("selecting %d, ", CURRENT_SC->target); 
1466 #endif
1467           SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->target);
1468 
1469           /* Enable interrupts for SELECTION OUT DONE and SELECTION OUT INITIATED */
1470           SETPORT(SXFRCTL1, HOSTDATA(shpnt)->parity ? (ENSPCHK|ENSTIMER) : ENSTIMER);
1471 
1472           /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
1473           SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
1474           SETPORT(SIMODE1, ENSELTIMO);
1475 
1476           /* Enable SELECTION OUT sequence */
1477           SETBITS(SCSISEQ, ENSELO | ENAUTOATNO);
1478         
1479         }
1480       else
1481         {
1482           /* No command we are busy with and no new to issue */
1483           printk("aha152x: ignoring spurious interrupt, nothing to do\n");
1484           if(TESTHI(DMACNTRL0, SWINT)) {
1485             printk("aha152x: SWINT is set!  Why?\n");
1486             CLRBITS(DMACNTRL0, SWINT);
1487           }
1488           show_queues(shpnt);
1489         }
1490 
1491 #if defined(DEBUG_RACE)
1492           leave_driver("(selecting) intr");
1493 #endif
1494           SETBITS(DMACNTRL0, INTEN);
1495           return;
1496         }
1497 
1498   /* the bus is busy with something */
1499 
1500 #if defined(DEBUG_INTR)
1501   if(HOSTDATA(shpnt)->debug & debug_intr)
1502     disp_ports(shpnt);
1503 #endif
1504 
1505   /* we are waiting for the result of a selection attempt */
1506   if(CURRENT_SC->SCp.phase & in_selection)
1507     {
1508       if(TESTLO(SSTAT1, SELTO))
1509         /* no timeout */
1510         if(TESTHI(SSTAT0, SELDO))
1511           {
1512             /* clear BUS FREE interrupt */
1513             SETPORT(SSTAT1, CLRBUSFREE);
1514 
1515             /* Disable SELECTION OUT sequence */
1516             CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO);
1517 
1518             /* Disable SELECTION OUT DONE interrupt */
1519             CLRBITS(SIMODE0, ENSELDO);
1520             CLRBITS(SIMODE1, ENSELTIMO);
1521 
1522             if(TESTLO(SSTAT0, SELDO))
1523               {
1524                 printk("aha152x: passing bus free condition\n");
1525 
1526 #if defined(DEBUG_RACE)
1527                 leave_driver("(passing bus free) intr");
1528 #endif
1529                 SETBITS(DMACNTRL0, INTEN);
1530 
1531                 if(CURRENT_SC->SCp.phase & aborted)
1532                   {
1533                     HOSTDATA(shpnt)->abort_result=SCSI_ABORT_ERROR;
1534                     HOSTDATA(shpnt)->abortion_complete++;
1535                   }
1536 
1537                 aha152x_done(shpnt, DID_NO_CONNECT << 16);
1538                 return;
1539               }
1540 #if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1541             if(HOSTDATA(shpnt)->debug & (debug_selection|debug_phases))
1542               printk("SELDO (SELID=%x), ", GETPORT(SELID));
1543 #endif
1544 
1545             /* selection was done */
1546             SETPORT(SSTAT0, CLRSELDO);
1547 
1548 #if defined(DEBUG_ABORT)
1549             if((HOSTDATA(shpnt)->debug & debug_abort) && (CURRENT_SC->SCp.phase & aborted))
1550               printk("(ABORT) target selected, ");
1551 #endif
1552 
1553             CURRENT_SC->SCp.phase &= ~in_selection;
1554             CURRENT_SC->SCp.phase |= in_other;
1555 
1556             ADDMSG(IDENTIFY(HOSTDATA(shpnt)->reconnect,CURRENT_SC->lun));
1557 
1558             if(!(SYNCRATE&0x80) && HOSTDATA(shpnt)->synchronous)
1559               {
1560                 ADDMSG(EXTENDED_MESSAGE);
1561                 ADDMSG(3);
1562                 ADDMSG(EXTENDED_SDTR);
1563                 ADDMSG(50);
1564                 ADDMSG(8);
1565 
1566                 printk("outbound SDTR: ");
1567                 print_msg(&MSG(MSGLEN-5));
1568 
1569                 SYNCRATE=0x80;
1570                 CURRENT_SC->SCp.phase |= in_sync;
1571               }
1572 
1573 #if defined(DEBUG_RACE)
1574             leave_driver("(SELDO) intr");
1575 #endif
1576             SETPORT(SCSIRATE, SYNCRATE&0x7f);
1577 
1578             SETPORT(SCSISIG, P_MSGO);
1579 
1580             SETPORT(SIMODE0, 0);
1581             SETPORT(SIMODE1, ENREQINIT|ENBUSFREE);
1582             SETBITS(DMACNTRL0, INTEN);
1583             return;
1584           }
1585         else
1586           aha152x_panic(shpnt, "neither timeout nor selection\007");
1587       else
1588         {
1589 #if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1590           if(HOSTDATA(shpnt)->debug & (debug_selection|debug_phases))
1591           printk("SELTO, ");
1592 #endif
1593           /* end selection attempt */
1594           CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO);
1595 
1596           /* timeout */
1597           SETPORT(SSTAT1, CLRSELTIMO);
1598 
1599           SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1600           SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1601           SETBITS(DMACNTRL0, INTEN);
1602 #if defined(DEBUG_RACE)
1603           leave_driver("(SELTO) intr");
1604 #endif
1605 
1606           if(CURRENT_SC->SCp.phase & aborted)
1607             {
1608 #if defined(DEBUG_ABORT)
1609               if(HOSTDATA(shpnt)->debug & debug_abort)
1610                 printk("(ABORT) selection timeout, ");
1611 #endif
1612               HOSTDATA(shpnt)->abort_result=SCSI_ABORT_ERROR;
1613               HOSTDATA(shpnt)->abortion_complete++;
1614             }
1615 
1616           if(TESTLO(SSTAT0, SELINGO))
1617             /* ARBITRATION not won */
1618             aha152x_done(shpnt, DID_BUS_BUSY << 16);
1619           else
1620             /* ARBITRATION won, but SELECTION failed */
1621             aha152x_done(shpnt, DID_NO_CONNECT << 16);
1622 
1623           return;
1624         }
1625     }
1626 
1627   /* enable interrupt, when target leaves current phase */
1628   phase = getphase(shpnt);
1629   if(!(phase & ~P_MASK))                                      /* "real" phase */
1630     SETPORT(SCSISIG, phase);
1631   SETPORT(SSTAT1, CLRPHASECHG);
1632   CURRENT_SC->SCp.phase =
1633     (CURRENT_SC->SCp.phase & ~((P_MASK|1)<<16)) | (phase << 16);
1634 
1635   /* information transfer phase */
1636   switch(phase)
1637     {
1638     case P_MSGO:                                               /* MESSAGE OUT */
1639       {
1640         int i, identify=0, abort=0;
1641 
1642 #if defined(DEBUG_INTR) || defined(DEBUG_MSGO) || defined(DEBUG_PHASES)
1643         if(HOSTDATA(shpnt)->debug & (debug_intr|debug_msgo|debug_phases))
1644           printk("MESSAGE OUT, ");
1645 #endif
1646         if(MSGLEN==0)
1647           {
1648             ADDMSG(MESSAGE_REJECT);
1649 #if defined(DEBUG_MSGO)
1650             if(HOSTDATA(shpnt)->debug & debug_msgo)
1651               printk("unexpected MSGO; rejecting, ");
1652 #endif
1653           }
1654         
1655         
1656         CLRBITS(SXFRCTL0, ENDMA);
1657         
1658         SETPORT(SIMODE0, 0);
1659         SETPORT(SIMODE1, ENPHASEMIS|ENREQINIT|ENBUSFREE);
1660         
1661         /* wait for data latch to become ready or a phase change */
1662         while(TESTLO(DMASTAT, INTSTAT))
1663           barrier();
1664         
1665 #if defined(DEBUG_MSGO)
1666         if(HOSTDATA(shpnt)->debug & debug_msgo)
1667           {
1668             int i;
1669             
1670             printk("messages (");
1671             for(i=0; i<MSGLEN; i+=print_msg(&MSG(i)), printk(" "))
1672               ;
1673             printk("), ");
1674             }
1675 #endif
1676         
1677         for(i=0; i<MSGLEN && TESTLO(SSTAT1, PHASEMIS); i++)
1678             {
1679 #if defined(DEBUG_MSGO)
1680             if(HOSTDATA(shpnt)->debug & debug_msgo)
1681               printk("%x ", MSG(i));
1682 #endif
1683             if(i==MSGLEN-1)
1684               {
1685                 /* Leave MESSAGE OUT after transfer */
1686                 SETPORT(SSTAT1, CLRATNO);
1687             }
1688           
1689             SETPORT(SCSIDAT, MSG(i));
1690 
1691             make_acklow(shpnt);
1692             getphase(shpnt);
1693 
1694             if(MSG(i)==IDENTIFY(HOSTDATA(shpnt)->reconnect,CURRENT_SC->lun))
1695               identify++;
1696 
1697             if(MSG(i)==ABORT)
1698               abort++;
1699 
1700           }
1701 
1702         MSGLEN=0;
1703 
1704         if(identify)
1705           CURRENT_SC->SCp.phase |= sent_ident;
1706 
1707         if(abort)
1708           {
1709             /* revive abort(); abort() enables interrupts */
1710             HOSTDATA(shpnt)->abort_result=SCSI_ABORT_SUCCESS;
1711             HOSTDATA(shpnt)->abortion_complete++;
1712 
1713             CURRENT_SC->SCp.phase &= ~(P_MASK<<16);
1714 
1715             /* exit */
1716             SETBITS(DMACNTRL0, INTEN);
1717 #if defined(DEBUG_RACE)
1718             leave_driver("(ABORT) intr");
1719 #endif
1720             aha152x_done(shpnt, DID_ABORT<<16);
1721             return;
1722           }
1723       }
1724       break;
1725 
1726     case P_CMD:                                          /* COMMAND phase */
1727 #if defined(DEBUG_INTR) || defined(DEBUG_CMD) || defined(DEBUG_PHASES)
1728       if(HOSTDATA(shpnt)->debug & (debug_intr|debug_cmd|debug_phases))
1729         printk("COMMAND, ");
1730 #endif
1731       if(!(CURRENT_SC->SCp.sent_command))
1732         {
1733           int i;
1734 
1735           CLRBITS(SXFRCTL0, ENDMA);
1736 
1737           SETPORT(SIMODE0, 0);
1738           SETPORT(SIMODE1, ENPHASEMIS|ENREQINIT|ENBUSFREE);
1739   
1740           /* wait for data latch to become ready or a phase change */
1741           while(TESTLO(DMASTAT, INTSTAT))
1742             barrier();
1743   
1744           for(i=0; i<CURRENT_SC->cmd_len && TESTLO(SSTAT1, PHASEMIS); i++)
1745           {
1746               SETPORT(SCSIDAT, CURRENT_SC->cmnd[i]);
1747 
1748               make_acklow(shpnt);
1749               getphase(shpnt);
1750           }
1751 
1752           if(i<CURRENT_SC->cmd_len && TESTHI(SSTAT1, PHASEMIS))
1753             aha152x_panic(shpnt, "target left COMMAND");
1754 
1755           CURRENT_SC->SCp.sent_command++;
1756         }
1757       else
1758         aha152x_panic(shpnt, "Nothing to send while in COMMAND");
1759       break;
1760 
1761     case P_MSGI:                                          /* MESSAGE IN phase */
1762       {
1763         int start_sync=0;
1764         
1765 #if defined(DEBUG_INTR) || defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1766         if(HOSTDATA(shpnt)->debug & (debug_intr|debug_msgi|debug_phases))
1767         printk("MESSAGE IN, ");
1768 #endif
1769         SETPORT(SXFRCTL0, CH1);
1770 
1771         SETPORT(SIMODE0, 0);
1772         SETPORT(SIMODE1, ENBUSFREE);
1773   
1774         while(phase == P_MSGI) 
1775         {
1776             CURRENT_SC->SCp.Message = GETPORT(SCSIDAT);
1777             switch(CURRENT_SC->SCp.Message)
1778             {
1779             case DISCONNECT:
1780 #if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1781                 if(HOSTDATA(shpnt)->debug & (debug_msgi|debug_phases))
1782                 printk("target disconnected, ");
1783 #endif
1784                 CURRENT_SC->SCp.Message = 0;
1785                 CURRENT_SC->SCp.phase   |= disconnected;
1786                 if(!HOSTDATA(shpnt)->reconnect)
1787                   aha152x_panic(shpnt, "target was not allowed to disconnect");
1788               break;
1789         
1790             case COMMAND_COMPLETE:
1791 #if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1792                 if(HOSTDATA(shpnt)->debug & (debug_msgi|debug_phases))
1793                   printk("inbound message (COMMAND COMPLETE), ");
1794 #endif
1795               done++;
1796               break;
1797 
1798             case MESSAGE_REJECT:
1799                 if(CURRENT_SC->SCp.phase & in_sync)
1800                   { 
1801                     CURRENT_SC->SCp.phase &= ~in_sync;
1802                     SYNCRATE=0x80;
1803                     printk("synchronous rejected, ");
1804                   }
1805                 else
1806                   printk("inbound message (MESSAGE REJECT), ");
1807 #if defined(DEBUG_MSGI)
1808                 if(HOSTDATA(shpnt)->debug & debug_msgi)
1809                   printk("inbound message (MESSAGE REJECT), ");
1810 #endif
1811               break;
1812 
1813             case SAVE_POINTERS:
1814 #if defined(DEBUG_MSGI)
1815                 if(HOSTDATA(shpnt)->debug & debug_msgi)
1816                   printk("inbound message (SAVE DATA POINTERS), ");
1817 #endif
1818               break;
1819 
1820             case EXTENDED_MESSAGE:
1821               { 
1822                   char buffer[16];
1823                   int  i;
1824 
1825 #if defined(DEBUG_MSGI)
1826                   if(HOSTDATA(shpnt)->debug & debug_msgi)
1827                     printk("inbound message (EXTENDED MESSAGE), ");
1828 #endif
1829                   make_acklow(shpnt);
1830                   if(getphase(shpnt)!=P_MSGI)
1831                   break;
1832   
1833                   buffer[0]=EXTENDED_MESSAGE;
1834                   buffer[1]=GETPORT(SCSIDAT);
1835                   
1836                   for(i=0; i<buffer[1] &&
1837                       (make_acklow(shpnt), getphase(shpnt)==P_MSGI); i++)
1838                     buffer[2+i]=GETPORT(SCSIDAT);
1839 
1840 #if defined(DEBUG_MSGI)
1841                   if(HOSTDATA(shpnt)->debug & debug_msgi)
1842                     print_msg(buffer);
1843 #endif
1844 
1845                   switch(buffer [2])
1846                     {
1847                     case EXTENDED_SDTR:
1848                       {
1849                         long ticks;
1850                         
1851                         if(buffer[1]!=3)
1852                           aha152x_panic(shpnt, "SDTR message length != 3");
1853                         
1854                         if(!HOSTDATA(shpnt)->synchronous)
1855                   break;
1856 
1857                         printk("inbound SDTR: "); print_msg(buffer);
1858                         
1859                         ticks=(buffer[3]*4+49)/50;
1860 
1861                         if(CURRENT_SC->SCp.phase & in_sync)
1862                   {
1863                             /* we initiated SDTR */
1864                             if(ticks>9 || buffer[4]<1 || buffer[4]>8)
1865                               aha152x_panic(shpnt, "received SDTR invalid");
1866                             
1867                             SYNCRATE |= ((ticks-2)<<4) + buffer[4];
1868                           }
1869                         else if(ticks<=9 && buffer[4]>=1)
1870                           {
1871                             if(buffer[4]>8)
1872                               buffer[4]=8;
1873                             
1874                             ADDMSG(EXTENDED_MESSAGE);
1875                             ADDMSG(3);
1876                             ADDMSG(EXTENDED_SDTR);
1877                             if(ticks<4)
1878                               {
1879                                     ticks=4;
1880                                     ADDMSG(50);
1881                               }
1882                       else
1883                               ADDMSG(buffer[3]);
1884                             
1885                             ADDMSG(buffer[4]);
1886                             
1887                             printk("outbound SDTR: ");
1888                             print_msg(&MSG(MSGLEN-5));
1889                             
1890                             CURRENT_SC->SCp.phase |= in_sync;
1891                             
1892                             SYNCRATE |= ((ticks-2)<<4) + buffer[4];
1893                             
1894                             start_sync++;
1895                   }
1896                         else
1897                   {
1898                             /* requested SDTR is too slow, do it asynchronously */
1899                             ADDMSG(MESSAGE_REJECT);
1900                             SYNCRATE = 0;
1901                           } 
1902                         
1903                         SETPORT(SCSIRATE, SYNCRATE&0x7f);
1904                       }
1905                       break;
1906                       
1907                     case EXTENDED_MODIFY_DATA_POINTER:
1908                     case EXTENDED_EXTENDED_IDENTIFY:
1909                     case EXTENDED_WDTR:
1910                     default:
1911                       ADDMSG(MESSAGE_REJECT);
1912                       break;
1913                   }
1914               }
1915               break;
1916        
1917             default:
1918                 printk("unsupported inbound message %x, ", 
1919                        CURRENT_SC->SCp.Message);
1920               break;
1921 
1922             }
1923 
1924             make_acklow(shpnt);
1925             phase=getphase(shpnt);
1926         } 
1927 
1928         if(start_sync)
1929           CURRENT_SC->SCp.phase |= in_sync;
1930         else
1931           CURRENT_SC->SCp.phase &= ~in_sync;
1932         
1933         if(MSGLEN>0)
1934           SETPORT(SCSISIG, P_MSGI|ATNO);
1935         
1936       /* clear SCSI fifo on BUSFREE */
1937       if(phase==P_BUSFREE)
1938         SETPORT(SXFRCTL0, CH1|CLRCH1);
1939 
1940         if(CURRENT_SC->SCp.phase & disconnected)
1941         {
1942           save_flags(flags);
1943           cli();
1944 #if defined(DEBUG_QUEUES)
1945             if(HOSTDATA(shpnt)->debug & debug_queues)
1946             printk("d+, ");
1947 #endif
1948             append_SC(&DISCONNECTED_SC, CURRENT_SC);
1949             CURRENT_SC = NULL;
1950           restore_flags(flags);
1951 
1952             SETBITS(SCSISEQ, ENRESELI);
1953 
1954             SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
1955             SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
1956 
1957             SETBITS(DMACNTRL0, INTEN);
1958           return;
1959         }
1960       }
1961       break;
1962 
1963     case P_STATUS:                                         /* STATUS IN phase */
1964 #if defined(DEBUG_STATUS) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1965       if(HOSTDATA(shpnt)->debug & (debug_status|debug_intr|debug_phases))
1966         printk("STATUS, ");
1967 #endif
1968       SETPORT(SXFRCTL0, CH1);
1969 
1970       SETPORT(SIMODE0, 0);
1971       SETPORT(SIMODE1, ENREQINIT|ENBUSFREE);
1972 
1973       if(TESTHI(SSTAT1, PHASEMIS))
1974         printk("aha152x: passing STATUS phase");
1975         
1976       CURRENT_SC->SCp.Status = GETPORT(SCSIBUS);
1977       make_acklow(shpnt);
1978       getphase(shpnt);
1979 
1980 #if defined(DEBUG_STATUS)
1981       if(HOSTDATA(shpnt)->debug & debug_status)
1982       {
1983         printk("inbound status ");
1984           print_status(CURRENT_SC->SCp.Status);
1985         printk(", ");
1986       }
1987 #endif
1988       break;
1989 
1990     case P_DATAI:                                            /* DATA IN phase */
1991       {
1992         int fifodata, data_count, done;
1993 
1994 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1995         if(HOSTDATA(shpnt)->debug & (debug_datai|debug_intr|debug_phases))
1996           printk("DATA IN, ");
1997 #endif
1998 
1999 #if 0
2000         if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
2001           printk("aha152x: P_DATAI: %d(%d) bytes left in FIFO, resetting\n",
2002                  GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
2003 #endif
2004 
2005         /* reset host fifo */
2006         SETPORT(DMACNTRL0, RSTFIFO);
2007         SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
2008 
2009         SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN);
2010 
2011         SETPORT(SIMODE0, 0);
2012         SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
2013 
2014         /* done is set when the FIFO is empty after the target left DATA IN */
2015         done=0;
2016       
2017         /* while the target stays in DATA to transfer data */
2018         while (!done) 
2019           {
2020 #if defined(DEBUG_DATAI)
2021             if(HOSTDATA(shpnt)->debug & debug_datai)
2022               printk("expecting data, ");
2023 #endif
2024             /* wait for PHASEMIS or full FIFO */
2025             while(TESTLO (DMASTAT, DFIFOFULL|INTSTAT))
2026               barrier();
2027 
2028 #if defined(DEBUG_DATAI)
2029             if(HOSTDATA(shpnt)->debug & debug_datai)
2030               printk("ok, ");
2031 #endif
2032             
2033             if(TESTHI(DMASTAT, DFIFOFULL))
2034               fifodata=GETPORT(FIFOSTAT);
2035             else
2036               {
2037                 /* wait for SCSI fifo to get empty */
2038                 while(TESTLO(SSTAT2, SEMPTY))
2039                   barrier();
2040 
2041                 /* rest of data in FIFO */
2042                 fifodata=GETPORT(FIFOSTAT);
2043 #if defined(DEBUG_DATAI)
2044                 if(HOSTDATA(shpnt)->debug & debug_datai)
2045                   printk("last transfer, ");
2046 #endif
2047                 done=1;
2048               }
2049   
2050 #if defined(DEBUG_DATAI)
2051             if(HOSTDATA(shpnt)->debug & debug_datai)
2052               printk("fifodata=%d, ", fifodata);
2053 #endif
2054 
2055             while(fifodata && CURRENT_SC->SCp.this_residual)
2056               {
2057                 data_count=fifodata;
2058   
2059                 /* limit data transfer to size of first sg buffer */
2060                 if (data_count > CURRENT_SC->SCp.this_residual)
2061                   data_count = CURRENT_SC->SCp.this_residual;
2062   
2063                 fifodata -= data_count;
2064 
2065 #if defined(DEBUG_DATAI)
2066                 if(HOSTDATA(shpnt)->debug & debug_datai)
2067                   printk("data_count=%d, ", data_count);
2068 #endif
2069   
2070                 if(data_count&1)
2071                   {
2072                     /* get a single byte in byte mode */
2073                     SETBITS(DMACNTRL0, _8BIT);
2074                     *CURRENT_SC->SCp.ptr++ = GETPORT(DATAPORT);
2075                     CURRENT_SC->SCp.this_residual--;
2076                   }
2077                 if(data_count>1)
2078                   {
2079                     CLRBITS(DMACNTRL0, _8BIT);
2080                     data_count >>= 1; /* Number of words */
2081                     insw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2082 #if defined(DEBUG_DATAI)
2083                     if(HOSTDATA(shpnt)->debug & debug_datai)
2084                       /* show what comes with the last transfer */
2085                       if(done)
2086                         {
2087 #ifdef 0
2088                           int           i;
2089                           unsigned char *data;
2090 #endif
2091   
2092                           printk("data on last transfer (%d bytes) ",
2093                                  2*data_count);
2094 #ifdef 0
2095                           printk("data on last transfer (%d bytes: ",
2096                                  2*data_count);
2097                           data = (unsigned char *) CURRENT_SC->SCp.ptr;
2098                           for(i=0; i<2*data_count; i++)
2099                             printk("%2x ", *data++);
2100                           printk("), ");
2101 #endif
2102                         }
2103 #endif
2104                     CURRENT_SC->SCp.ptr           += 2 * data_count;
2105                     CURRENT_SC->SCp.this_residual -= 2 * data_count;
2106                   }
2107               
2108                 /* if this buffer is full and there are more buffers left */
2109                 if (!CURRENT_SC->SCp.this_residual &&
2110                     CURRENT_SC->SCp.buffers_residual)
2111                   {
2112                     /* advance to next buffer */
2113                     CURRENT_SC->SCp.buffers_residual--;
2114                     CURRENT_SC->SCp.buffer++;
2115                     CURRENT_SC->SCp.ptr =
2116                       CURRENT_SC->SCp.buffer->address;
2117                     CURRENT_SC->SCp.this_residual =
2118                       CURRENT_SC->SCp.buffer->length;
2119                   } 
2120               }
2121  
2122             /*
2123              * Fifo should be empty
2124              */
2125             if(fifodata>0)
2126               {
2127                 printk("aha152x: more data than expected (%d bytes)\n",
2128                        GETPORT(FIFOSTAT));
2129                 SETBITS(DMACNTRL0, _8BIT);
2130                 printk("aha152x: data (");
2131                 while(fifodata--)
2132                   printk("%2x ", GETPORT(DATAPORT));
2133                 printk(")\n");
2134               }
2135 
2136 #if defined(DEBUG_DATAI)
2137             if(HOSTDATA(shpnt)->debug & debug_datai)
2138               if(!fifodata)
2139                 printk("fifo empty, ");
2140               else
2141                 printk("something left in fifo, ");
2142 #endif
2143           }
2144 
2145 #if defined(DEBUG_DATAI)
2146         if((HOSTDATA(shpnt)->debug & debug_datai) &&
2147            (CURRENT_SC->SCp.buffers_residual ||
2148             CURRENT_SC->SCp.this_residual))
2149           printk("left buffers (buffers=%d, bytes=%d), ",
2150                  CURRENT_SC->SCp.buffers_residual, 
2151                  CURRENT_SC->SCp.this_residual);
2152 #endif
2153         /* transfer can be considered ended, when SCSIEN reads back zero */
2154         CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
2155         while(TESTHI(SXFRCTL0, SCSIEN))
2156           barrier();
2157         CLRBITS(DMACNTRL0, ENDMA);
2158 
2159 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR)
2160         if(HOSTDATA(shpnt)->debug & (debug_datai|debug_intr))
2161           printk("got %d bytes, ", GETSTCNT());
2162 #endif
2163 
2164         CURRENT_SC->SCp.have_data_in++;
2165       }
2166       break;
2167 
2168     case P_DATAO:                                           /* DATA OUT phase */
2169       {
2170         int data_count;
2171 
2172 #if defined(DEBUG_DATAO) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
2173         if(HOSTDATA(shpnt)->debug & (debug_datao|debug_intr|debug_phases))
2174           printk("DATA OUT, ");
2175 #endif
2176 #if defined(DEBUG_DATAO)
2177         if(HOSTDATA(shpnt)->debug & debug_datao)
2178           printk("got data to send (bytes=%d, buffers=%d), ",
2179                  CURRENT_SC->SCp.this_residual,
2180                  CURRENT_SC->SCp.buffers_residual);
2181 #endif
2182 
2183         if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
2184           {
2185             printk("%d(%d) left in FIFO, ",
2186                    GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
2187             aha152x_panic(shpnt, "FIFO should be empty");
2188           }
2189 
2190         SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1);
2191         SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
2192         
2193         SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
2194         SETPORT(DMACNTRL0, ENDMA|WRITE_READ);
2195 
2196         SETPORT(SIMODE0, 0);
2197         SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
2198 
2199         /* while current buffer is not empty or
2200            there are more buffers to transfer */
2201         while(TESTLO(SSTAT1, PHASEMIS) &&
2202               (CURRENT_SC->SCp.this_residual ||
2203                CURRENT_SC->SCp.buffers_residual))
2204           {
2205 #if defined(DEBUG_DATAO)
2206             if(HOSTDATA(shpnt)->debug & debug_datao)
2207               printk("sending data (left: bytes=%d, buffers=%d), waiting, ",
2208                      CURRENT_SC->SCp.this_residual,
2209                      CURRENT_SC->SCp.buffers_residual);
2210 #endif
2211             /* transfer rest of buffer, but max. 128 byte */
2212             data_count =
2213               CURRENT_SC->SCp.this_residual > 128 ?
2214               128 : CURRENT_SC->SCp.this_residual ;
2215 
2216 #if defined(DEBUG_DATAO)
2217             if(HOSTDATA(shpnt)->debug & debug_datao)
2218               printk("data_count=%d, ", data_count);
2219 #endif
2220   
2221             if(data_count&1)
2222               {
2223                 /* put a single byte in byte mode */
2224                 SETBITS(DMACNTRL0, _8BIT);
2225                 SETPORT(DATAPORT, *CURRENT_SC->SCp.ptr++);
2226                 CURRENT_SC->SCp.this_residual--;
2227               }
2228             if(data_count>1)
2229               {
2230                 CLRBITS(DMACNTRL0, _8BIT);
2231                 data_count >>= 1; /* number of words */
2232                 outsw(DATAPORT, CURRENT_SC->SCp.ptr, data_count);
2233                 CURRENT_SC->SCp.ptr           += 2 * data_count;
2234                 CURRENT_SC->SCp.this_residual -= 2 * data_count;
2235               }
2236 
2237             /* wait for FIFO to get empty */
2238             while(TESTLO(DMASTAT, DFIFOEMP|INTSTAT))
2239               barrier();
2240 
2241 #if defined(DEBUG_DATAO)
2242             if(HOSTDATA(shpnt)->debug & debug_datao)
2243               printk("fifo (%d bytes), transfered (%d bytes), ",
2244                      GETPORT(FIFOSTAT), GETSTCNT());
2245 #endif
2246 
2247             /* if this buffer is empty and there are more buffers left */
2248             if (TESTLO(SSTAT1, PHASEMIS) &&
2249                 !CURRENT_SC->SCp.this_residual &&
2250                 CURRENT_SC->SCp.buffers_residual)
2251               {
2252                  /* advance to next buffer */
2253                 CURRENT_SC->SCp.buffers_residual--;
2254                 CURRENT_SC->SCp.buffer++;
2255                 CURRENT_SC->SCp.ptr =
2256                   CURRENT_SC->SCp.buffer->address;
2257                 CURRENT_SC->SCp.this_residual =
2258                   CURRENT_SC->SCp.buffer->length;
2259               }
2260           }
2261 
2262         if (CURRENT_SC->SCp.this_residual || CURRENT_SC->SCp.buffers_residual)
2263           {
2264             /* target leaves DATA OUT for an other phase
2265                (perhaps disconnect) */
2266 
2267             /* data in fifos has to be resend */
2268             data_count = GETPORT(SSTAT2) & (SFULL|SFCNT);
2269 
2270             data_count += GETPORT(FIFOSTAT) ;
2271             CURRENT_SC->SCp.ptr           -= data_count;
2272             CURRENT_SC->SCp.this_residual += data_count;
2273 #if defined(DEBUG_DATAO)
2274             if(HOSTDATA(shpnt)->debug & debug_datao)
2275               printk("left data (bytes=%d, buffers=%d), fifos (bytes=%d), "
2276                      "transfer incomplete, resetting fifo, ",
2277                      CURRENT_SC->SCp.this_residual,
2278                      CURRENT_SC->SCp.buffers_residual,
2279                      data_count);
2280 #endif
2281             SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
2282             CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
2283             CLRBITS(DMACNTRL0, ENDMA);
2284           }
2285         else
2286           {
2287 #if defined(DEBUG_DATAO)
2288             if(HOSTDATA(shpnt)->debug & debug_datao)
2289               printk("waiting for SCSI fifo to get empty, ");
2290 #endif
2291             /* wait for SCSI fifo to get empty */
2292             while(TESTLO(SSTAT2, SEMPTY))
2293               barrier();
2294 #if defined(DEBUG_DATAO)
2295             if(HOSTDATA(shpnt)->debug & debug_datao)
2296               printk("ok, left data (bytes=%d, buffers=%d) ",
2297                      CURRENT_SC->SCp.this_residual,
2298                      CURRENT_SC->SCp.buffers_residual);
2299 #endif
2300             CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
2301 
2302             /* transfer can be considered ended, when SCSIEN reads back zero */
2303             while(TESTHI(SXFRCTL0, SCSIEN))
2304               barrier();
2305 
2306             CLRBITS(DMACNTRL0, ENDMA);
2307           }
2308 
2309 #if defined(DEBUG_DATAO) || defined(DEBUG_INTR)
2310         if(HOSTDATA(shpnt)->debug & (debug_datao|debug_intr))
2311           printk("sent %d data bytes, ", GETSTCNT());
2312 #endif
2313       }
2314       break;
2315 
2316     case P_BUSFREE:                                                /* BUSFREE */
2317 #if defined(DEBUG_RACE)
2318       leave_driver("(BUSFREE) intr");
2319 #endif
2320 #if defined(DEBUG_PHASES)
2321       if(HOSTDATA(shpnt)->debug & debug_phases)
2322         printk("unexpected BUS FREE, ");
2323 #endif
2324       CURRENT_SC->SCp.phase &= ~(P_MASK<<16);
2325 
2326       aha152x_done(shpnt, DID_ERROR << 16);         /* Don't know any better */
2327       return;
2328       break;
2329 
2330     case P_PARITY:                              /* parity error in DATA phase */
2331 #if defined(DEBUG_RACE)
2332       leave_driver("(DID_PARITY) intr");
2333 #endif
2334       printk("PARITY error in DATA phase, ");
2335 
2336       CURRENT_SC->SCp.phase &= ~(P_MASK<<16);
2337 
2338       SETBITS(DMACNTRL0, INTEN);
2339       aha152x_done(shpnt, DID_PARITY << 16);
2340       return;
2341       break;
2342 
2343     default:
2344       printk("aha152x: unexpected phase\n");
2345       break;
2346     }
2347 
2348   if(done)
2349     {
2350 #if defined(DEBUG_INTR)
2351       if(HOSTDATA(shpnt)->debug & debug_intr)
2352         printk("command done.\n");
2353 #endif
2354 #if defined(DEBUG_RACE)
2355       leave_driver("(done) intr");
2356 #endif
2357 
2358       SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
2359       SETPORT(SIMODE1, ISSUE_SC ? ENBUSFREE : 0);
2360       SETPORT(SCSISEQ, DISCONNECTED_SC ? ENRESELI : 0);
2361       
2362       SETBITS(DMACNTRL0, INTEN);
2363       
2364       aha152x_done(shpnt,
2365                    (CURRENT_SC->SCp.Status  & 0xff)
2366                    | ((CURRENT_SC->SCp.Message & 0xff) << 8)
2367                    | (DID_OK << 16));
2368 
2369 #if defined(DEBUG_RACE)
2370       printk("done returned (DID_OK: Status=%x; Message=%x).\n",
2371              CURRENT_SC->SCp.Status, CURRENT_SC->SCp.Message);
2372 #endif
2373       return;
2374     }
2375 
2376   if(CURRENT_SC)
2377     CURRENT_SC->SCp.phase |= 1<<16 ;
2378 
2379   SETPORT(SIMODE0, 0);
2380   SETPORT(SIMODE1, ENPHASEMIS|ENBUSFREE);
2381 #if defined(DEBUG_INTR)
2382   if(HOSTDATA(shpnt)->debug & debug_intr)
2383     disp_enintr(shpnt);
2384 #endif
2385 #if defined(DEBUG_RACE)
2386   leave_driver("(PHASEEND) intr");
2387 #endif
2388 
2389   SETBITS(DMACNTRL0, INTEN);
2390   return;
2391 }
2392 
2393 /* 
2394  * Dump the current driver status and panic...
2395  */
2396 static void aha152x_panic(struct Scsi_Host *shpnt, char *msg)
     /* [previous][next][first][last][top][bottom][index][help] */
2397 {
2398   printk("\naha152x: %s\n", msg);
2399   show_queues(shpnt);
2400   panic("aha152x panic");
2401 }
2402 
2403 /*
2404  * Display registers of AIC-6260
2405  */
2406 static void disp_ports(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2407 {
2408 #ifdef DEBUG_AHA152X
2409   int s;
2410 
2411 #ifdef SKIP_PORTS
2412   if(HOSTDATA(shpnt)->debug & debug_skipports)
2413         return;
2414 #endif
2415 
2416   printk("\n%s: ", CURRENT_SC ? "on bus" : "waiting");
2417 
2418   s=GETPORT(SCSISEQ);
2419   printk("SCSISEQ (");
2420   if(s & TEMODEO)     printk("TARGET MODE ");
2421   if(s & ENSELO)      printk("SELO ");
2422   if(s & ENSELI)      printk("SELI ");
2423   if(s & ENRESELI)    printk("RESELI ");
2424   if(s & ENAUTOATNO)  printk("AUTOATNO ");
2425   if(s & ENAUTOATNI)  printk("AUTOATNI ");
2426   if(s & ENAUTOATNP)  printk("AUTOATNP ");
2427   if(s & SCSIRSTO)    printk("SCSIRSTO ");
2428   printk(");");
2429 
2430   printk(" SCSISIG (");
2431   s=GETPORT(SCSISIG);
2432   switch(s & P_MASK)
2433     {
2434     case P_DATAO:
2435       printk("DATA OUT");
2436       break;
2437     case P_DATAI:
2438       printk("DATA IN");
2439       break;
2440     case P_CMD:
2441       printk("COMMAND"); 
2442       break;
2443     case P_STATUS:
2444       printk("STATUS"); 
2445       break;
2446     case P_MSGO:
2447       printk("MESSAGE OUT");
2448       break;
2449     case P_MSGI:
2450       printk("MESSAGE IN");
2451       break;
2452     default:
2453       printk("*illegal*");
2454       break;
2455     }
2456   
2457   printk("); ");
2458 
2459   printk("INTSTAT (%s); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
2460 
2461   printk("SSTAT (");
2462   s=GETPORT(SSTAT0);
2463   if(s & TARGET)   printk("TARGET ");
2464   if(s & SELDO)    printk("SELDO ");
2465   if(s & SELDI)    printk("SELDI ");
2466   if(s & SELINGO)  printk("SELINGO ");
2467   if(s & SWRAP)    printk("SWRAP ");
2468   if(s & SDONE)    printk("SDONE ");
2469   if(s & SPIORDY)  printk("SPIORDY ");
2470   if(s & DMADONE)  printk("DMADONE ");
2471 
2472   s=GETPORT(SSTAT1);
2473   if(s & SELTO)     printk("SELTO ");
2474   if(s & ATNTARG)   printk("ATNTARG ");
2475   if(s & SCSIRSTI)  printk("SCSIRSTI ");
2476   if(s & PHASEMIS)  printk("PHASEMIS ");
2477   if(s & BUSFREE)   printk("BUSFREE ");
2478   if(s & SCSIPERR)  printk("SCSIPERR ");
2479   if(s & PHASECHG)  printk("PHASECHG ");
2480   if(s & REQINIT)   printk("REQINIT ");
2481   printk("); ");
2482 
2483 
2484   printk("SSTAT (");
2485 
2486   s=GETPORT(SSTAT0) & GETPORT(SIMODE0);
2487 
2488   if(s & TARGET)    printk("TARGET ");
2489   if(s & SELDO)     printk("SELDO ");
2490   if(s & SELDI)     printk("SELDI ");
2491   if(s & SELINGO)   printk("SELINGO ");
2492   if(s & SWRAP)     printk("SWRAP ");
2493   if(s & SDONE)     printk("SDONE ");
2494   if(s & SPIORDY)   printk("SPIORDY ");
2495   if(s & DMADONE)   printk("DMADONE ");
2496 
2497   s=GETPORT(SSTAT1) & GETPORT(SIMODE1);
2498 
2499   if(s & SELTO)     printk("SELTO ");
2500   if(s & ATNTARG)   printk("ATNTARG ");
2501   if(s & SCSIRSTI)  printk("SCSIRSTI ");
2502   if(s & PHASEMIS)  printk("PHASEMIS ");
2503   if(s & BUSFREE)   printk("BUSFREE ");
2504   if(s & SCSIPERR)  printk("SCSIPERR ");
2505   if(s & PHASECHG)  printk("PHASECHG ");
2506   if(s & REQINIT)   printk("REQINIT ");
2507   printk("); ");
2508 
2509   printk("SXFRCTL0 (");
2510 
2511   s=GETPORT(SXFRCTL0);
2512   if(s & SCSIEN)    printk("SCSIEN ");
2513   if(s & DMAEN)     printk("DMAEN ");
2514   if(s & CH1)       printk("CH1 ");
2515   if(s & CLRSTCNT)  printk("CLRSTCNT ");
2516   if(s & SPIOEN)    printk("SPIOEN ");
2517   if(s & CLRCH1)    printk("CLRCH1 ");
2518   printk("); ");
2519 
2520   printk("SIGNAL (");
2521 
2522   s=GETPORT(SCSISIG);
2523   if(s & ATNI)  printk("ATNI ");
2524   if(s & SELI)  printk("SELI ");
2525   if(s & BSYI)  printk("BSYI ");
2526   if(s & REQI)  printk("REQI ");
2527   if(s & ACKI)  printk("ACKI ");
2528   printk("); ");
2529 
2530   printk("SELID (%02x), ", GETPORT(SELID));
2531 
2532   printk("SSTAT2 (");
2533 
2534   s=GETPORT(SSTAT2);
2535   if(s & SOFFSET)  printk("SOFFSET ");
2536   if(s & SEMPTY)   printk("SEMPTY ");
2537   if(s & SFULL)    printk("SFULL ");
2538   printk("); SFCNT (%d); ", s & (SFULL|SFCNT));
2539 
2540   s=GETPORT(SSTAT3);
2541   printk("SCSICNT (%d), OFFCNT(%d), ", (s&0xf0)>>4, s&0x0f);
2542   
2543   printk("SSTAT4 (");
2544   s=GETPORT(SSTAT4);
2545   if(s & SYNCERR)   printk("SYNCERR ");
2546   if(s & FWERR)     printk("FWERR ");
2547   if(s & FRERR)     printk("FRERR ");
2548   printk("); ");
2549 
2550   printk("DMACNTRL0 (");
2551   s=GETPORT(DMACNTRL0);
2552   printk("%s ", s & _8BIT      ? "8BIT"  : "16BIT");
2553   printk("%s ", s & DMA        ? "DMA"   : "PIO"  );
2554   printk("%s ", s & WRITE_READ ? "WRITE" : "READ" );
2555   if(s & ENDMA)    printk("ENDMA ");
2556   if(s & INTEN)    printk("INTEN ");
2557   if(s & RSTFIFO)  printk("RSTFIFO ");
2558   if(s & SWINT)    printk("SWINT ");
2559   printk("); ");
2560 
2561 
2562 #if 0
2563   printk("DMACNTRL1 (");
2564 
2565   s=GETPORT(DMACNTRL1);
2566   if(s & PWRDWN)    printk("PWRDN ");
2567   printk("); ");
2568 
2569 
2570   printk("STK (%d); ", s & 0xf);
2571   
2572 #endif
2573 
2574   printk("DMASTAT (");
2575   s=GETPORT(DMASTAT);
2576   if(s & ATDONE)     printk("ATDONE ");
2577   if(s & WORDRDY)    printk("WORDRDY ");
2578   if(s & DFIFOFULL)  printk("DFIFOFULL ");
2579   if(s & DFIFOEMP)   printk("DFIFOEMP ");
2580   printk(")");
2581 
2582   printk("\n");
2583 #endif
2584 }
2585 
2586 /*
2587  * display enabled interrupts
2588  */
2589 static void disp_enintr(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2590 {
2591   int s;
2592 
2593   printk("enabled interrupts (");
2594   
2595   s=GETPORT(SIMODE0);
2596   if(s & ENSELDO)    printk("ENSELDO ");
2597   if(s & ENSELDI)    printk("ENSELDI ");
2598   if(s & ENSELINGO)  printk("ENSELINGO ");
2599   if(s & ENSWRAP)    printk("ENSWRAP ");
2600   if(s & ENSDONE)    printk("ENSDONE ");
2601   if(s & ENSPIORDY)  printk("ENSPIORDY ");
2602   if(s & ENDMADONE)  printk("ENDMADONE ");
2603 
2604   s=GETPORT(SIMODE1);
2605   if(s & ENSELTIMO)    printk("ENSELTIMO ");
2606   if(s & ENATNTARG)    printk("ENATNTARG ");
2607   if(s & ENPHASEMIS)   printk("ENPHASEMIS ");
2608   if(s & ENBUSFREE)    printk("ENBUSFREE ");
2609   if(s & ENSCSIPERR)   printk("ENSCSIPERR ");
2610   if(s & ENPHASECHG)   printk("ENPHASECHG ");
2611   if(s & ENREQINIT)    printk("ENREQINIT ");
2612   printk(")\n");
2613 }
2614 
2615 #if defined(DEBUG_RACE)
2616 
2617 static const char *should_leave;
2618 static int in_driver=0;
2619 
2620 /*
2621  * Only one routine can be in the driver at once.
2622  */
2623 static void enter_driver(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
2624 {
2625   unsigned long flags;
2626 
2627   save_flags(flags);
2628   cli();
2629   printk("aha152x: entering %s() (%x)\n", func, jiffies);
2630   if(in_driver)
2631     {
2632       printk("%s should leave first.\n", should_leave);
2633       panic("aha152x: already in driver\n");
2634     }
2635 
2636   in_driver++;
2637   should_leave=func;
2638   restore_flags(flags);
2639 }
2640 
2641 static void leave_driver(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
2642 {
2643   unsigned long flags;
2644 
2645   save_flags(flags);
2646   cli();
2647   printk("\naha152x: leaving %s() (%x)\n", func, jiffies);
2648   if(!in_driver)
2649     {
2650       printk("aha152x: %s already left.\n", should_leave);
2651       panic("aha152x: %s already left driver.\n");
2652     }
2653 
2654   in_driver--;
2655   should_leave=func;
2656   restore_flags(flags);
2657 }
2658 #endif
2659 
2660 /*
2661  * Show the command data of a command
2662  */
2663 static void show_command(Scsi_Cmnd *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2664 {
2665   printk("0x%08x: target=%d; lun=%d; cmnd=(",
2666          (unsigned int) ptr, ptr->target, ptr->lun);
2667   
2668   print_command(ptr->cmnd);
2669 
2670   printk("); residual=%d; buffers=%d; phase |",
2671          ptr->SCp.this_residual, ptr->SCp.buffers_residual);
2672 
2673   if(ptr->SCp.phase & not_issued  )  printk("not issued|");
2674   if(ptr->SCp.phase & in_selection)  printk("in selection|");
2675   if(ptr->SCp.phase & disconnected)  printk("disconnected|");
2676   if(ptr->SCp.phase & aborted     )  printk("aborted|");
2677   if(ptr->SCp.phase & sent_ident  )  printk("send_ident|");
2678   if(ptr->SCp.phase & in_other)
2679     { 
2680       printk("; in other(");
2681       switch((ptr->SCp.phase >> 16) & P_MASK)
2682         {
2683         case P_DATAO:
2684           printk("DATA OUT");
2685           break;
2686         case P_DATAI:
2687           printk("DATA IN");
2688           break;
2689         case P_CMD:
2690           printk("COMMAND");
2691           break;
2692         case P_STATUS:
2693           printk("STATUS");
2694           break;
2695         case P_MSGO:
2696           printk("MESSAGE OUT");
2697           break;
2698         case P_MSGI:
2699           printk("MESSAGE IN");
2700           break;
2701         default: 
2702           printk("*illegal*");
2703           break;
2704         }
2705       printk(")");
2706       if(ptr->SCp.phase & (1<<16))
2707         printk("; phaseend");
2708     }
2709   printk("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
2710 }
2711  
2712 /*
2713  * Dump the queued data
2714  */
2715 static void show_queues(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2716 {
2717   unsigned long flags;
2718   Scsi_Cmnd *ptr;
2719 
2720   save_flags(flags);
2721   cli();
2722   printk("QUEUE STATUS:\nissue_SC:\n");
2723   for(ptr=ISSUE_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
2724     show_command(ptr);
2725 
2726   printk("current_SC:\n");
2727   if(CURRENT_SC)
2728     show_command(CURRENT_SC);
2729   else
2730     printk("none\n");
2731 
2732   printk("disconnected_SC:\n");
2733   for(ptr=DISCONNECTED_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
2734     show_command(ptr);
2735 
2736   disp_ports(shpnt);
2737   disp_enintr(shpnt);
2738   restore_flags(flags);
2739 }
2740 
2741 int aha152x_set_info(char *buffer, int length, struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2742 {
2743   return(-ENOSYS);  /* Currently this is a no-op */
2744 }
2745 
2746 #undef SPRINTF
2747 #define SPRINTF(args...) pos += sprintf(pos, ## args)
2748 
2749 static int get_command(char *pos, Scsi_Cmnd *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2750 {
2751   char *start = pos;
2752   int i;
2753   
2754   SPRINTF("0x%08x: target=%d; lun=%d; cmnd=(",
2755           (unsigned int) ptr, ptr->target, ptr->lun);
2756   
2757   for(i=0; i<COMMAND_SIZE(ptr->cmnd[0]); i++)
2758     SPRINTF("0x%02x", ptr->cmnd[i]);
2759   
2760   SPRINTF("); residual=%d; buffers=%d; phase |",
2761           ptr->SCp.this_residual, ptr->SCp.buffers_residual);
2762   
2763   if(ptr->SCp.phase & not_issued  )  SPRINTF("not issued|");
2764   if(ptr->SCp.phase & in_selection)  SPRINTF("in selection|");
2765   if(ptr->SCp.phase & disconnected)  SPRINTF("disconnected|");
2766   if(ptr->SCp.phase & aborted     )  SPRINTF("aborted|");
2767   if(ptr->SCp.phase & sent_ident  )  SPRINTF("send_ident|");
2768   if(ptr->SCp.phase & in_other)
2769     { 
2770       SPRINTF("; in other(");
2771       switch((ptr->SCp.phase >> 16) & P_MASK)
2772         {
2773         case P_DATAO:
2774           SPRINTF("DATA OUT");
2775           break;
2776         case P_DATAI:
2777           SPRINTF("DATA IN");
2778           break;
2779         case P_CMD:
2780           SPRINTF("COMMAND");
2781           break;
2782         case P_STATUS:
2783           SPRINTF("STATUS");
2784           break;
2785         case P_MSGO:
2786           SPRINTF("MESSAGE OUT");
2787           break;
2788         case P_MSGI:
2789           SPRINTF("MESSAGE IN");
2790           break;
2791         default: 
2792           SPRINTF("*illegal*");
2793           break;
2794         }
2795       SPRINTF(")");
2796       if(ptr->SCp.phase & (1<<16))
2797         SPRINTF("; phaseend");
2798     }
2799   SPRINTF("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
2800   
2801   return(pos-start);
2802 }
2803 
2804 #undef SPRINTF
2805 #define SPRINTF(args...) do { if(pos < buffer + length) pos += sprintf(pos, ## args); } while(0)
2806 
2807 int aha152x_proc_info(
     /* [previous][next][first][last][top][bottom][index][help] */
2808                       char *buffer,
2809                       char **start,
2810                       off_t offset,
2811                       int length,
2812                       int hostno,
2813                       int inout
2814                       )
2815 {
2816   int i;
2817   char *pos = buffer;
2818   Scsi_Device *scd;
2819   struct Scsi_Host *shpnt;
2820   unsigned long flags;
2821   Scsi_Cmnd *ptr;
2822   
2823   for(i=0, shpnt= (struct Scsi_Host *) NULL; i<IRQS; i++)
2824     if(aha152x_host[i] && aha152x_host[i]->host_no == hostno)
2825       shpnt=aha152x_host[i];
2826   
2827   if(!shpnt)
2828     return(-ESRCH);
2829   
2830   if(inout) /* Has data been written to the file ? */ 
2831     return(aha152x_set_info(buffer, length, shpnt));
2832   
2833   SPRINTF(AHA152X_REVID "\n");
2834   
2835   save_flags(flags);
2836   cli();
2837   
2838   SPRINTF("vital data:\nioports 0x%04x to 0x%04x\n",
2839           shpnt->io_port, shpnt->io_port+shpnt->n_io_port-1);
2840   SPRINTF("interrupt 0x%02x\n", shpnt->irq);
2841   SPRINTF("disconnection/reconnection %s\n", 
2842           HOSTDATA(shpnt)->reconnect ? "enabled" : "disabled");
2843   SPRINTF("parity checking %s\n", 
2844           HOSTDATA(shpnt)->parity ? "enabled" : "disabled");
2845   SPRINTF("synchronous transfers %s\n", 
2846           HOSTDATA(shpnt)->synchronous ? "enabled" : "disabled");
2847   SPRINTF("current queued %d commands\n",
2848           HOSTDATA(shpnt)->commands);
2849   
2850 #if 0
2851   SPRINTF("synchronously operating targets (tick=%ld ns):\n",
2852           250000000/loops_per_sec);
2853   for(i=0; i<8; i++)
2854     if(HOSTDATA(shpnt)->syncrate[i]&0x7f)
2855       SPRINTF("target %d: period %dT/%ldns; req/ack offset %d\n",
2856               i,
2857               (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2),
2858                        (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2)*
2859                          250000000/loops_per_sec,
2860                        HOSTDATA(shpnt)->syncrate[i]&0x0f);
2861 #else
2862   SPRINTF("synchronously operating targets (tick=50 ns):\n");
2863   for(i=0; i<8; i++)
2864     if(HOSTDATA(shpnt)->syncrate[i]&0x7f)
2865       SPRINTF("target %d: period %dT/%dns; req/ack offset %d\n",
2866               i,
2867               (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2),
2868               (((HOSTDATA(shpnt)->syncrate[i]&0x70)>>4)+2)*50,
2869               HOSTDATA(shpnt)->syncrate[i]&0x0f);
2870 #endif
2871   
2872 #ifdef DEBUG_AHA152X
2873 #define PDEBUG(flags,txt) if(HOSTDATA(shpnt)->debug & flags) SPRINTF("(%s) ", txt);
2874   
2875   SPRINTF("enabled debugging options:\n");
2876   
2877   PDEBUG(debug_skipports, "skip ports");
2878   PDEBUG(debug_queue, "queue");
2879   PDEBUG(debug_intr, "interrupt");
2880   PDEBUG(debug_selection, "selection");
2881   PDEBUG(debug_msgo, "message out");
2882   PDEBUG(debug_msgi, "message in");
2883   PDEBUG(debug_status, "status");
2884   PDEBUG(debug_cmd, "command");
2885   PDEBUG(debug_datai, "data in");
2886   PDEBUG(debug_datao, "data out");
2887   PDEBUG(debug_abort, "abort");
2888   PDEBUG(debug_done, "done");
2889   PDEBUG(debug_biosparam, "bios parameters");
2890   PDEBUG(debug_phases, "phases");
2891   PDEBUG(debug_queues, "queues");
2892   PDEBUG(debug_reset, "reset");
2893   
2894   SPRINTF("\n");
2895 #endif
2896   
2897   SPRINTF("queue status:\nnot yet issued commands:\n");
2898   for(ptr=ISSUE_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
2899     pos += get_command(pos, ptr);
2900   
2901   if(CURRENT_SC)
2902     {
2903       SPRINTF("current command:\n");
2904       pos += get_command(pos, CURRENT_SC);
2905     }
2906   
2907   SPRINTF("disconnected commands:\n");
2908   for(ptr=DISCONNECTED_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
2909     pos += get_command(pos, ptr);
2910   
2911   restore_flags(flags);
2912   
2913   scd = scsi_devices;
2914   
2915   SPRINTF("Attached devices: %s\n", (scd)?"":"none");
2916   
2917   while (scd) {
2918     if (scd->host == shpnt) {
2919       
2920       SPRINTF("Channel: %02d Id: %02d Lun: %02d\n  Vendor: ",
2921               scd->channel, scd->id, scd->lun);
2922       for (i=0; i<8; i++) {
2923         if (scd->vendor[i] >= 0x20)
2924           SPRINTF("%c", scd->vendor[i]);
2925         else
2926           SPRINTF(" ");
2927       }
2928       SPRINTF(" Model: ");
2929       for (i = 0; i < 16; i++) {
2930         if (scd->model[i] >= 0x20)
2931           SPRINTF("%c", scd->model[i]);
2932         else
2933           SPRINTF(" ");
2934       }
2935       SPRINTF(" Rev: ");
2936       for (i = 0; i < 4; i++) {
2937         if (scd->rev[i] >= 0x20)
2938           SPRINTF("%c", scd->rev[i]);
2939         else
2940           SPRINTF(" ");
2941       }
2942       SPRINTF("\n");
2943       
2944       SPRINTF("  Type:   %d ", scd->type);
2945       SPRINTF("               ANSI SCSI revision: %02x",
2946               (scd->scsi_level < 3)?1:2);
2947       
2948       if (scd->scsi_level == 2)
2949         SPRINTF(" CCS\n");
2950       else
2951         SPRINTF("\n");
2952     }
2953     scd = scd->next;
2954   }
2955   
2956   *start=buffer;
2957   return (pos-buffer < length ? pos-buffer : length);     
2958 }
2959 
2960 #ifdef MODULE
2961 /* Eventually this will go into an include file, but this will be later */
2962 Scsi_Host_Template driver_template = AHA152X;
2963 
2964 #include "scsi_module.c"
2965 #endif

/* [previous][next][first][last][top][bottom][index][help] */