root/drivers/scsi/aha152x.c

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

DEFINITIONS

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

   1 /* aha152x.c -- Adaptec AHA-152x driver
   2  * Author: Juergen E. Fischer, fischer@server.et-inf.fho-emden.de
   3  * Copyright 1993, 1994 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.0 1994/03/25 12:52:00 root Exp $
  24  *
  25 
  26  * $Log: aha152x.c,v $
  27  * Revision 1.0  1994/03/25  12:52:00  root
  28  * - Fixed "more data than expected" problem
  29  * - added new BIOS signatures
  30  *
  31  * Revision 0.102  1994/01/31  20:44:12  root
  32  * - minor changes in insw/outsw handling
  33  *
  34  * Revision 0.101  1993/12/13  01:16:27  root
  35  * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
  36  *   fixes problems with CD-ROM sector size detection & media change)
  37  *
  38  * Revision 0.100  1993/12/10  16:58:47  root
  39  * - fix for unsuccessful selections in case of non-continuous id assignments
  40  *   on the scsi bus.
  41  *
  42  * Revision 0.99  1993/10/24  16:19:59  root
  43  * - fixed DATA IN (rare read errors gone)
  44  *
  45  * Revision 0.98  1993/10/17  12:54:44  root
  46  * - fixed some recent fixes (shame on me)
  47  * - moved initialization of scratch area to aha152x_queue
  48  *
  49  * Revision 0.97  1993/10/09  18:53:53  root
  50  * - DATA IN fixed. Rarely left data in the fifo.
  51  *
  52  * Revision 0.96  1993/10/03  00:53:59  root
  53  * - minor changes on DATA IN
  54  *
  55  * Revision 0.95  1993/09/24  10:36:01  root
  56  * - change handling of MSGI after reselection
  57  * - fixed sti/cli
  58  * - minor changes
  59  *
  60  * Revision 0.94  1993/09/18  14:08:22  root
  61  * - fixed bug in multiple outstanding command code
  62  * - changed detection
  63  * - support for kernel command line configuration
  64  * - reset corrected
  65  * - changed message handling
  66  *
  67  * Revision 0.93  1993/09/15  20:41:19  root
  68  * - fixed bugs with multiple outstanding commands
  69  *
  70  * Revision 0.92  1993/09/13  02:46:33  root
  71  * - multiple outstanding commands work (no problems with IBM drive)
  72  *
  73  * Revision 0.91  1993/09/12  20:51:46  root
  74  * added multiple outstanding commands
  75  * (some problem with this $%&? IBM device remain)
  76  *
  77  * Revision 0.9  1993/09/12  11:11:22  root
  78  * - corrected auto-configuration
  79  * - changed the auto-configuration (added some '#define's)
  80  * - added support for dis-/reconnection
  81  *
  82  * Revision 0.8  1993/09/06  23:09:39  root
  83  * - added support for the drive activity light
  84  * - minor changes
  85  *
  86  * Revision 0.7  1993/09/05  14:30:15  root
  87  * - improved phase detection
  88  * - now using the new snarf_region code of 0.99pl13
  89  *
  90  * Revision 0.6  1993/09/02  11:01:38  root
  91  * first public release; added some signatures and biosparam()
  92  *
  93  * Revision 0.5  1993/08/30  10:23:30  root
  94  * fixed timing problems with my IBM drive
  95  *
  96  * Revision 0.4  1993/08/29  14:06:52  root
  97  * fixed some problems with timeouts due incomplete commands
  98  *
  99  * Revision 0.3  1993/08/28  15:55:03  root
 100  * writing data works too.  mounted and worked on a dos partition
 101  *
 102  * Revision 0.2  1993/08/27  22:42:07  root
 103  * reading data works.  Mounted a msdos partition.
 104  *
 105  * Revision 0.1  1993/08/25  13:38:30  root
 106  * first "damn thing doesn't work" version
 107  *
 108  * Revision 0.0  1993/08/14  19:54:25  root
 109  * empty function bodies; detect() works.
 110  *
 111 
 112  **************************************************************************
 113 
 114 
 115  
 116  DESCRIPTION:
 117 
 118  This is the Linux low-level SCSI driver for Adaptec AHA-1520/1522
 119  SCSI host adapters.
 120 
 121 
 122  PER-DEFINE CONFIGURABLE OPTIONS:
 123 
 124  AUTOCONF       : use configuration the controller reports (only 152x)
 125  IRQ            : override interrupt channel (9,10,11 or 12) (default 11)
 126  SCSI_ID        : override scsiid of AIC-6260 (0-7) (default 7)
 127  RECONNECT      : override target dis-/reconnection/multiple outstanding commands
 128  SKIP_BIOSTEST  : Don't test for BIOS signature (AHA-1510 or disabled BIOS)
 129  PORTBASE       : Force port base. Don't try to probe
 130 
 131 
 132  LILO COMMAND LINE OPTIONS:
 133 
 134  aha152x=<PORTBASE>,<IRQ>,<SCSI-ID>,<RECONNECT>
 135 
 136  The normal configuration can be overridden by specifying a command line.
 137  When you do this, the BIOS test is skipped. Entered values have to be
 138  valid (known). Don't use values that aren't support under normal operation.
 139  If you think that you need other value: contact me.
 140 
 141 
 142  REFERENCES USED:
 143 
 144  "AIC-6260 SCSI Chip Specification", Adaptec Corporation.
 145 
 146  "SCSI COMPUTER SYSTEM INTERFACE - 2 (SCSI-2)", X3T9.2/86-109 rev. 10h
 147 
 148  "Writing a SCSI device driver for Linux", Rik Faith (faith@cs.unc.edu)
 149 
 150  "Kernel Hacker's Guide", Michael K. Johnson (johnsonm@sunsite.unc.edu)
 151 
 152  "Adaptec 1520/1522 User's Guide", Adaptec Corporation.
 153  
 154  Michael K. Johnson (johnsonm@sunsite.unc.edu)
 155 
 156  Drew Eckhardt (drew@cs.colorado.edu)
 157 
 158  Eric Youngdale (eric@tantalus.nrl.navy.mil) 
 159 
 160  special thanks to Eric Youngdale for the free(!) supplying the
 161  documentation on the chip.
 162 
 163  **************************************************************************/
 164 
 165 #include "aha152x.h"
 166 
 167 #include <linux/sched.h>
 168 #include <asm/io.h>
 169 #include "../block/blk.h"
 170 #include "scsi.h"
 171 #include "hosts.h"
 172 #include "constants.h"
 173 #include <asm/system.h>
 174 #include <linux/errno.h>
 175 #include <linux/string.h>
 176 #include <linux/wait.h>
 177 #include <linux/ioport.h>
 178 
 179 /* DEFINES */
 180 
 181 
 182 /* If auto configuration is disabled, IRQ, SCSI_ID and RECONNECT have to
 183    be predefined */
 184 #if !defined(AUTOCONF)
 185 #if !defined(IRQ)
 186 #error undefined IRQ; define AUTOCONF or IRQ
 187 #endif
 188 #if !defined(SCSI_ID)
 189 #error undefined SCSI_ID; define AUTOCONF or SCSI_ID
 190 #endif
 191 #if !defined(RECONNECT)
 192 #error undefined RECONNECT; define AUTOCONF or RECONNECT
 193 #endif
 194 #endif
 195 
 196 /* I use this when I'm looking for weird bugs */
 197 #define DEBUG_TIMING 
 198 
 199 #if defined(DEBUG_AHA152X)
 200 
 201 #undef  SKIP_PORTS              /* don't display ports */
 202 
 203 #undef  DEBUG_QUEUE             /* debug queue() */
 204 #undef  DEBUG_RESET             /* debug reset() */
 205 #undef  DEBUG_INTR              /* debug intr() */
 206 #undef  DEBUG_SELECTION         /* debug selection part in intr() */
 207 #undef  DEBUG_MSGO              /* debug message out phase in intr() */
 208 #undef  DEBUG_MSGI              /* debug message in phase in intr() */
 209 #undef  DEBUG_STATUS            /* debug status phase in intr() */
 210 #undef  DEBUG_CMD               /* debug command phase in intr() */
 211 #undef  DEBUG_DATAI             /* debug data in phase in intr() */
 212 #undef  DEBUG_DATAO             /* debug data out phase in intr() */
 213 #undef  DEBUG_ABORT             /* debug abort() */
 214 #undef  DEBUG_DONE              /* debug done() */
 215 #undef  DEBUG_BIOSPARAM         /* debug biosparam() */
 216 
 217 #undef  DEBUG_RACE              /* debug race conditions */
 218 #undef  DEBUG_PHASES            /* debug phases (useful to trace) */
 219 #undef  DEBUG_QUEUES            /* debug reselection */
 220 
 221 /* recently used for debugging */
 222 #if 0
 223 #define DEBUG_PHASES
 224 #define DEBUG_DATAI
 225 #endif
 226 
 227 #endif
 228 
 229 #define DEBUG_RESET             /* resets should be rare */
 230 #define DEBUG_ABORT             /* aborts too */
 231 
 232 /* END OF DEFINES */
 233 
 234 /* some additional "phases" for getphase() */
 235 #define P_BUSFREE  1
 236 #define P_PARITY   2
 237 
 238 static char *aha152x_id = AHA152X_REVID;
 239 
 240 static int port_base      = 0;
 241 static int this_host      = 0;
 242 static int can_disconnect = 0;
 243 static int commands       = 0;
 244 
 245 /* set by aha152x_setup according to the command line */
 246 static int setup_called    = 0;
 247 static int setup_portbase  = 0;
 248 static int setup_irq       = 0;
 249 static int setup_scsiid    = 0;
 250 static int setup_reconnect = 0;
 251 
 252 static char *setup_str = (char *)NULL;
 253 
 254 enum {
 255    not_issued   = 0x01,
 256    in_selection = 0x02,
 257    disconnected = 0x04,
 258    aborted      = 0x08,
 259    sent_ident   = 0x10,
 260    in_other     = 0x20,
 261 };
 262  
 263 /*
 264  * Command queues:
 265  * issue_SC        : commands that are queued to be issued 
 266  * current_SC      : command that's currently using the bus
 267  * disconnected_SC : commands that that have been disconnected 
 268  */
 269 static Scsi_Cmnd            *issue_SC        = NULL;
 270 static Scsi_Cmnd            *current_SC      = NULL;
 271 static Scsi_Cmnd            *disconnected_SC = NULL;
 272 
 273 static struct wait_queue    *abortion_complete;
 274 static int                  abort_result;
 275 
 276 void aha152x_intr( int irqno );
 277 void aha152x_done( int error );
 278 void aha152x_setup( char *str, int *ints );
 279 
 280 static void aha152x_reset_ports(void);
 281 static void aha152x_panic(char *msg);
 282 
 283 static void disp_ports(void);
 284 static void show_command(Scsi_Cmnd *ptr);
 285 static void show_queues(void);
 286 static void disp_enintr(void);
 287 
 288 #if defined(DEBUG_RACE)
 289 static void enter_driver(const char *);
 290 static void leave_driver(const char *);
 291 #endif
 292 
 293 /* possible locations for the Adaptec BIOS */
 294 static void *addresses[] =
 295 {
 296   (void *) 0xdc000,   /* default first */
 297   (void *) 0xc8000,
 298   (void *) 0xcc000,
 299   (void *) 0xd0000,
 300   (void *) 0xd4000,
 301   (void *) 0xd8000,
 302   (void *) 0xe0000,
 303   (void *) 0xf0000,
 304 };
 305 #define ADDRESS_COUNT (sizeof( addresses ) / sizeof( unsigned ))
 306 
 307 /* possible i/o adresses for the AIC-6260 */
 308 static unsigned short ports[] =
 309 {
 310   0x340,      /* default first */
 311   0x140
 312 };
 313 #define PORT_COUNT (sizeof( ports ) / sizeof( unsigned short ))
 314 
 315 /* possible interrupt channels */
 316 static unsigned short ints[] = { 9, 10, 11, 12 };
 317 
 318 /* signatures for various AIC-6[23]60 based controllers.
 319    The point in detecting signatures is to avoid useless
 320    and maybe harmful probes on ports. I'm not sure that
 321    all listed boards pass auto-configuration. For those
 322    which fail the BIOS signature is obsolete, because
 323    user intervention to supply the configuration is 
 324    needed anyway. */
 325 static struct signature {
 326   char *signature;
 327   int  sig_offset;
 328   int  sig_length;
 329 } signatures[] =
 330 {
 331   { "Adaptec AHA-1520 BIOS",      0x102e, 21 },  /* Adaptec 152x */
 332   { "Adaptec ASW-B626 BIOS",      0x1029, 21 },  /* on-board controller */
 333   { "Adaptec BIOS: ASW-B626",       0x0f, 22 },  /* on-board controller */
 334   { "Adaptec ASW-B626 S2",        0x2e6c, 19 },  /* on-board controller */
 335   { "Adaptec BIOS:AIC-6360",         0xc, 21 },  /* on-board controller */
 336   { "ScsiPro SP-360 BIOS",        0x2873, 19 },  /* ScsiPro-Controller with AIC-6360 */
 337   { "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },  /* Gigabyte Local-Bus-SCSI */
 338 };
 339 #define SIGNATURE_COUNT (sizeof( signatures ) / sizeof( struct signature ))
 340 
 341 
 342 static void do_pause( unsigned amount ) /* Pause for amount*10 milliseconds */
 343 {
 344    unsigned long the_time = jiffies + amount; /* 0.01 seconds per jiffy */
 345 
 346    while (jiffies < the_time)
 347      ;
 348 }
 349 
 350 /*
 351  *  queue services:
 352  */
 353 static inline void append_SC( Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
     /* [previous][next][first][last][top][bottom][index][help] */
 354 {
 355   Scsi_Cmnd *end;
 356 
 357   new_SC->host_scribble = (unsigned char *) NULL;
 358   if(!*SC)
 359     *SC=new_SC;
 360   else
 361     {
 362       for( end=*SC;
 363            end->host_scribble;
 364            end = (Scsi_Cmnd *) end->host_scribble )
 365         ;
 366       end->host_scribble = (unsigned char *) new_SC;
 367     }
 368 }
 369 
 370 static inline Scsi_Cmnd *remove_first_SC( Scsi_Cmnd **SC )
     /* [previous][next][first][last][top][bottom][index][help] */
 371 {
 372   Scsi_Cmnd *ptr;
 373 
 374   ptr=*SC;
 375   if(ptr)
 376     *SC= (Scsi_Cmnd *) (*SC)->host_scribble;
 377   return ptr;
 378 }
 379 
 380 static inline Scsi_Cmnd *remove_SC( Scsi_Cmnd **SC, int target, int lun )
     /* [previous][next][first][last][top][bottom][index][help] */
 381 {
 382   Scsi_Cmnd *ptr, *prev;
 383 
 384   for( ptr=*SC, prev=NULL;
 385        ptr && ((ptr->target!=target) || (ptr->lun!=lun));
 386        prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble )
 387     ;
 388 
 389   if(ptr)
 390     if(prev)
 391       prev->host_scribble = ptr->host_scribble;
 392     else
 393       *SC= (Scsi_Cmnd *) ptr->host_scribble;
 394   return ptr;
 395 }
 396 
 397 /*
 398  * read inbound byte and wait for ACK to get low
 399  */
 400 static void make_acklow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402   SETPORT( SXFRCTL0, CH1|SPIOEN );
 403   GETPORT(SCSIDAT);
 404   SETPORT( SXFRCTL0, CH1 );
 405 
 406   while( TESTHI( SCSISIG, ACKI ) )
 407     ;
 408 }
 409 
 410 /*
 411  * detect current phase more reliable:
 412  * phase is valid, when the target asserts REQ after we've deasserted ACK.
 413  *
 414  * return value is a valid phase or an error code.
 415  *
 416  * errorcodes:
 417  *   P_BUSFREE   BUS FREE phase detected
 418  *   P_PARITY    parity error in DATA phase
 419  */
 420 static int getphase(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422   int phase, sstat1;
 423   
 424   while( 1 )
 425     {
 426       do
 427         {
 428           while( !( ( sstat1 = GETPORT( SSTAT1 ) ) & (BUSFREE|SCSIRSTI|REQINIT ) ) )
 429             ;
 430           if( sstat1 & BUSFREE )
 431             return P_BUSFREE;
 432           if( sstat1 & SCSIRSTI )
 433             {
 434               /* IBM drive responds with RSTI to RSTO */
 435               printk("aha152x: RESET IN\n");
 436               SETPORT( SSTAT1, SCSIRSTI );
 437             }
 438         }
 439       while( TESTHI( SCSISIG, ACKI ) || TESTLO( SSTAT1, REQINIT ) );
 440 
 441       SETPORT( SSTAT1, CLRSCSIPERR );
 442   
 443       phase = GETPORT( SCSISIG ) & P_MASK ;
 444 
 445       if( TESTHI( SSTAT1, SCSIPERR ) )
 446         {
 447           if( (phase & (CDO|MSGO))==0 )                         /* DATA phase */
 448             return P_PARITY;
 449 
 450           make_acklow();
 451         }
 452       else
 453         return phase;
 454     }
 455 }
 456 
 457 /* called from init/main.c */
 458 void aha152x_setup( char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
 459 {
 460   if(setup_called)
 461     panic("aha152x: aha152x_setup called twice.\n");
 462 
 463   setup_called=ints[0];
 464   setup_str=str;
 465 
 466   if(ints[0] != 4)
 467     return; 
 468 
 469   setup_portbase  = ints[1];
 470   setup_irq       = ints[2];
 471   setup_scsiid    = ints[3];
 472   setup_reconnect = ints[4];
 473 }
 474 
 475 /*
 476    Test, if port_base is valid.
 477  */
 478 static int aha152x_porttest(int port_base)
     /* [previous][next][first][last][top][bottom][index][help] */
 479 {
 480   int i;
 481 
 482   if(check_region(port_base, TEST-SCSISEQ))
 483     return 0;
 484 
 485   SETPORT( DMACNTRL1, 0 );          /* reset stack pointer */
 486   for(i=0; i<16; i++)
 487     SETPORT( STACK, i );
 488 
 489   SETPORT( DMACNTRL1, 0 );          /* reset stack pointer */
 490   for(i=0; i<16 && GETPORT(STACK)==i; i++)
 491     ;
 492 
 493   return(i==16);
 494 }
 495 
 496 int aha152x_detect(int hostno)
     /* [previous][next][first][last][top][bottom][index][help] */
 497 {
 498   int                 i, j,  ok;
 499   aha152x_config      conf;
 500   struct sigaction    sa;
 501   int                 interrupt_level;
 502   
 503 #if defined(DEBUG_RACE)
 504   enter_driver("detect");
 505 #endif
 506   
 507   if(setup_called)
 508     {
 509       printk("aha152x: processing commandline: ");
 510    
 511       if(setup_called!=4)
 512         {
 513           printk("\naha152x: %s\n", setup_str );
 514           printk("aha152x: usage: aha152x=<PORTBASE>,<IRQ>,<SCSI ID>,<RECONNECT>\n");
 515           panic("aha152x panics in line %d", __LINE__);
 516         }
 517 
 518       port_base       = setup_portbase;
 519       interrupt_level = setup_irq;
 520       this_host       = setup_scsiid;
 521       can_disconnect  = setup_reconnect;
 522 
 523       for( i=0; i<PORT_COUNT && (port_base != ports[i]); i++)
 524         ;
 525 
 526       if(i==PORT_COUNT)
 527         {
 528           printk("unknown portbase 0x%03x\n", port_base);
 529           panic("aha152x panics in line %d", __LINE__);
 530         }
 531 
 532       if(!aha152x_porttest(port_base))
 533         {
 534           printk("portbase 0x%03x fails probe\n", port_base);
 535           panic("aha152x panics in line %d", __LINE__);
 536         }
 537 
 538       i=0;
 539       while(ints[i] && (interrupt_level!=ints[i]))
 540         i++;
 541       if(!ints[i])
 542         {
 543           printk("illegal IRQ %d\n", interrupt_level);
 544           panic("aha152x panics in line %d", __LINE__);
 545         }
 546 
 547       if( (this_host < 0) || (this_host > 7) )
 548         {
 549           printk("illegal SCSI ID %d\n", this_host);
 550           panic("aha152x panics in line %d", __LINE__);
 551         }
 552 
 553       if( (can_disconnect < 0) || (can_disconnect > 1) )
 554         {
 555           printk("reconnect %d should be 0 or 1\n", can_disconnect);
 556           panic("aha152x panics in line %d", __LINE__);
 557         }
 558       printk("ok\n");
 559     }
 560   else
 561     {
 562 #if !defined(SKIP_BIOSTEST)
 563       ok=0;
 564       for( i=0; i < ADDRESS_COUNT && !ok; i++)
 565         for( j=0; (j < SIGNATURE_COUNT) && !ok; j++)
 566           ok=!memcmp((void *) addresses[i]+signatures[j].sig_offset,
 567                      (void *) signatures[j].signature,
 568                      (int) signatures[j].sig_length);
 569 
 570       if(!ok)
 571         {
 572 #if defined(DEBUG_RACE)
 573           leave_driver("(1) detect");
 574 #endif
 575           printk("failed\n");
 576           return 0;
 577         }
 578       printk("aha152x: BIOS test: passed, ");
 579 #else
 580       printk("aha152x: ");
 581 #endif /* !SKIP_BIOSTEST */
 582  
 583 #if !defined(PORTBASE)
 584       printk("porttest: ");
 585       for( i=0; i<PORT_COUNT && !aha152x_porttest(ports[i]); i++)
 586         ;
 587 
 588       if(i==PORT_COUNT)
 589         {
 590           printk("failed\n");
 591 #if defined(DEBUG_RACE)
 592           leave_driver("(2) detect");
 593 #endif
 594           return 0;
 595         }
 596       else
 597         port_base=ports[i];
 598       printk("ok, ");
 599 #else
 600       port_base=PORTBASE;
 601 #endif /* !PORTBASE */
 602 
 603 #if defined(AUTOCONF)
 604 
 605       conf.cf_port = (GETPORT(PORTA)<<8) + GETPORT(PORTB);
 606 
 607       interrupt_level = ints[conf.cf_irq];
 608       this_host       = conf.cf_id;
 609       can_disconnect  = conf.cf_tardisc;
 610 
 611       printk("auto configuration: ok, ");
 612 
 613 #endif /* AUTOCONF */
 614 
 615 #if defined(IRQ)
 616       interrupt_level = IRQ; 
 617 #endif
 618 
 619 #if defined(SCSI_ID)
 620       this_host = SCSI_ID;
 621 #endif
 622 
 623 #if defined(RECONNECT)
 624       can_disconnect=RECONNECT;
 625 #endif
 626     }
 627 
 628   printk("detection complete\n");
 629  
 630   sa.sa_handler  = aha152x_intr;
 631   sa.sa_flags    = SA_INTERRUPT;
 632   sa.sa_mask     = 0;
 633   sa.sa_restorer = NULL;
 634   
 635   ok = irqaction( interrupt_level, &sa);
 636   
 637   if(ok<0)
 638     {
 639       if(ok == -EINVAL)
 640         {
 641            printk("aha152x: bad IRQ %d.\n", interrupt_level);
 642            printk("         Contact author.\n");
 643         }
 644       else
 645         if( ok == -EBUSY)
 646           printk( "aha152x: IRQ %d already in use. Configure another.\n",
 647                   interrupt_level);
 648         else
 649           {
 650             printk( "\naha152x: Unexpected error code on requesting IRQ %d.\n",
 651                     interrupt_level);
 652             printk("         Contact author.\n");
 653           }
 654       panic("aha152x: driver needs an IRQ.\n");
 655     }
 656 
 657   SETPORT( SCSIID, this_host << 4 );
 658   scsi_hosts[hostno].this_id=this_host;
 659   
 660   if(can_disconnect)
 661     scsi_hosts[hostno].can_queue=AHA152X_MAXQUEUE;
 662 
 663   /* RESET OUT */
 664   SETBITS(SCSISEQ, SCSIRSTO );
 665   do_pause(5);
 666   CLRBITS(SCSISEQ, SCSIRSTO );
 667   do_pause(10);
 668 
 669   aha152x_reset(NULL);
 670 
 671   printk("aha152x: vital data: PORTBASE=0x%03x, IRQ=%d, SCSI ID=%d, reconnect=%s, parity=enabled\n",
 672          port_base, interrupt_level, this_host, can_disconnect ? "enabled" : "disabled" );
 673 
 674   snarf_region(port_base, TEST-SCSISEQ);        /* Register */
 675   
 676   /* not expecting any interrupts */
 677   SETPORT(SIMODE0, 0);
 678   SETPORT(SIMODE1, 0);
 679 
 680 #if defined(DEBUG_RACE)
 681   leave_driver("(3) detect");
 682 #endif
 683 
 684   SETBITS( DMACNTRL0, INTEN);
 685   return 1;
 686 }
 687 
 688 /*
 689  *  return the name of the thing
 690  */
 691 const char *aha152x_info(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 692 {
 693   return(aha152x_id);
 694 }
 695 
 696 /* 
 697  *  Queue a command and setup interrupts for a free bus.
 698  */
 699 int aha152x_queue( Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 700 {
 701 #if defined(DEBUG_RACE)
 702   enter_driver("queue");
 703 #else
 704 #if defined(DEBUG_QUEUE)
 705   printk("aha152x: queue(), ");
 706 #endif
 707 #endif
 708 
 709 
 710 #if defined(DEBUG_QUEUE)
 711   printk( "SCpnt (target = %d lun = %d cmnd = 0x%02x pieces = %d size = %u), ",
 712           SCpnt->target,
 713           SCpnt->lun,
 714           *(unsigned char *)SCpnt->cmnd,
 715           SCpnt->use_sg,
 716           SCpnt->request_bufflen );
 717   disp_ports();
 718 #endif
 719 
 720   SCpnt->scsi_done =       done;
 721 
 722   /* setup scratch area
 723      SCp.ptr              : buffer pointer
 724      SCp.this_residual    : buffer length
 725      SCp.buffer           : next buffer
 726      SCp.buffers_residual : left buffers in list
 727      SCp.phase            : current state of the command */
 728   SCpnt->SCp.phase = not_issued;
 729   if (SCpnt->use_sg)
 730     {
 731       SCpnt->SCp.buffer           = (struct scatterlist *)SCpnt->request_buffer;
 732       SCpnt->SCp.ptr              = SCpnt->SCp.buffer->address;
 733       SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
 734       SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
 735     }
 736   else
 737     {
 738       SCpnt->SCp.ptr              = (char *)SCpnt->request_buffer;
 739       SCpnt->SCp.this_residual    = SCpnt->request_bufflen;
 740       SCpnt->SCp.buffer           = NULL;
 741       SCpnt->SCp.buffers_residual = 0;
 742     }
 743           
 744   SCpnt->SCp.Status              = CHECK_CONDITION;
 745   SCpnt->SCp.Message             = 0;
 746   SCpnt->SCp.have_data_in        = 0;
 747   SCpnt->SCp.sent_command        = 0;
 748 
 749   /* Turn led on, when this is the first command. */
 750   cli();
 751   commands++;
 752   if(commands==1)
 753     SETPORT( PORTA, 1 );
 754 
 755 #if defined(DEBUG_QUEUES)
 756   printk("i+ (%d), ", commands );
 757 #endif
 758   append_SC( &issue_SC, SCpnt);
 759   
 760   /* Enable bus free interrupt, when we aren't currently on the bus */
 761   if(!current_SC)
 762     {
 763       SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
 764       SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
 765     }
 766   sti();
 767 
 768   return 0;
 769 }
 770 
 771 /*
 772  *  We only support command in interrupt-driven fashion
 773  */
 774 int aha152x_command( Scsi_Cmnd *SCpnt )
     /* [previous][next][first][last][top][bottom][index][help] */
 775 {
 776   printk( "aha152x: interrupt driven driver; use aha152x_queue()\n" );
 777   return -1;
 778 }
 779 
 780 /*
 781  *  Abort a queued command
 782  *  (commands that are on the bus can't be aborted easily)
 783  */
 784 int aha152x_abort( Scsi_Cmnd *SCpnt, int code )
     /* [previous][next][first][last][top][bottom][index][help] */
 785 {
 786   Scsi_Cmnd *ptr, *prev;
 787 
 788   cli();
 789 
 790 #if defined(DEBUG_ABORT)
 791   printk("aha152x: abort(), SCpnt=0x%08x, ", (unsigned int) SCpnt );
 792 #endif
 793 
 794   show_queues();
 795 
 796   /* look for command in issue queue */
 797   for( ptr=issue_SC, prev=NULL;
 798        ptr && ptr!=SCpnt;
 799        prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
 800     ;
 801 
 802   if(ptr)
 803     {
 804       /* dequeue */
 805       if(prev)
 806         prev->host_scribble = ptr->host_scribble;
 807       else
 808         issue_SC = (Scsi_Cmnd *) ptr->host_scribble;
 809       sti();
 810 
 811       ptr->host_scribble = NULL;
 812       ptr->result = (code ? code : DID_ABORT ) << 16;
 813       ptr->done(ptr);
 814       return 0;
 815     }
 816 
 817   /* Fail abortion, if we're on the bus */
 818   if (current_SC)
 819     {
 820        sti();
 821        return -1;
 822     }
 823 
 824   /* look for command in disconnected queue */
 825   for( ptr=disconnected_SC, prev=NULL;
 826        ptr && ptr!=SCpnt;
 827        prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
 828     ;
 829 
 830   if(ptr && TESTLO(SSTAT1, BUSFREE) )
 831     printk("bus busy but no current command, ");
 832 
 833   if(ptr && TESTHI(SSTAT1, BUSFREE) )
 834     {
 835       /* dequeue */
 836       if(prev)
 837         prev->host_scribble = ptr->host_scribble;
 838       else
 839         issue_SC = (Scsi_Cmnd *) ptr->host_scribble;
 840 
 841       /* set command current and initiate selection,
 842          let the interrupt routine take care of the abortion */
 843       current_SC     = ptr;
 844       ptr->SCp.phase = in_selection|aborted;
 845       SETPORT( SCSIID, (this_host << OID_) | current_SC->target );
 846 
 847       /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
 848       SETPORT( SIMODE0, ENSELDO | (disconnected_SC ? ENSELDI : 0) );
 849       SETPORT( SIMODE1, ENSELTIMO );
 850 
 851       /* Enable SELECTION OUT sequence */
 852       SETBITS(SCSISEQ, ENSELO | ENAUTOATNO );
 853 
 854       SETBITS( DMACNTRL0, INTEN );
 855       abort_result=0;
 856       sti();
 857 
 858       /* sleep until the abortion is complete */
 859       sleep_on( &abortion_complete );
 860       return abort_result;
 861     }
 862   else
 863     printk("aha152x: bus busy but no current command\n");
 864 
 865   /* command wasn't found */
 866   sti();
 867   return 0;
 868 }
 869 
 870 /*
 871  *  Restore default values to the AIC-6260 registers and reset the fifos
 872  */
 873 static void aha152x_reset_ports(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 874 {
 875   /* disable interrupts */
 876   SETPORT(DMACNTRL0, RSTFIFO);
 877 
 878   SETPORT(SCSISEQ, 0);
 879 
 880   SETPORT(SXFRCTL1, 0);
 881   SETPORT( SCSISIG, 0);
 882   SETPORT(SCSIRATE, 0);
 883 
 884   /* clear all interrupt conditions */
 885   SETPORT(SSTAT0, 0x7f);
 886   SETPORT(SSTAT1, 0xef);
 887 
 888   SETPORT(SSTAT4, SYNCERR|FWERR|FRERR);
 889 
 890   SETPORT(DMACNTRL0, 0);
 891   SETPORT(DMACNTRL1, 0);
 892 
 893   SETPORT(BRSTCNTRL, 0xf1);
 894 
 895   /* clear SCSI fifo and transfer count */
 896   SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
 897   SETPORT(SXFRCTL0, CH1);
 898 
 899   /* enable interrupts */
 900   SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
 901   SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
 902 }
 903 
 904 /*
 905  *  Reset registers, reset a hanging bus and
 906  *  kill active and disconnected commands
 907  */
 908 int aha152x_reset(Scsi_Cmnd * __unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 909 {
 910   Scsi_Cmnd *ptr;
 911 
 912   aha152x_reset_ports();
 913 
 914   /* Reset, if bus hangs */
 915   if( TESTLO( SSTAT1, BUSFREE ) )
 916     {
 917        CLRBITS( DMACNTRL0, INTEN );
 918 
 919 #if defined( DEBUG_RESET )
 920        printk("aha152x: reset(), bus not free: SCSI RESET OUT\n");
 921 #endif
 922 
 923        show_queues();
 924 
 925        if(current_SC)
 926          {
 927            current_SC->host_scribble = NULL;
 928            current_SC->result = DID_RESET << 16;
 929            current_SC->done(current_SC);
 930            current_SC=NULL;
 931          }
 932 
 933        while(disconnected_SC)
 934          {
 935            ptr = disconnected_SC;
 936            disconnected_SC = (Scsi_Cmnd *) ptr->host_scribble;
 937            ptr->host_scribble = NULL;
 938            ptr->result = DID_RESET << 16;
 939            ptr->done(ptr);
 940          }
 941 
 942        /* RESET OUT */
 943        SETPORT(SCSISEQ, SCSIRSTO);
 944        do_pause(5);
 945        SETPORT(SCSISEQ, 0);
 946        do_pause(10);
 947 
 948        SETPORT(SIMODE0, 0 );
 949        SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
 950 
 951        SETPORT( DMACNTRL0, INTEN );
 952     }
 953 
 954   return 0;
 955 }
 956 
 957 /*
 958  * Return the "logical geometry"
 959  */
 960 int aha152x_biosparam( int size, int dev, int *info_array )
     /* [previous][next][first][last][top][bottom][index][help] */
 961 {
 962 #if defined(DEBUG_BIOSPARAM)
 963   printk("aha152x_biosparam: dev=%x, size=%d, ", dev, size);
 964 #endif
 965   
 966 /* I took this from other SCSI drivers, since it provides
 967    the correct data for my devices. */
 968   info_array[0]=64;
 969   info_array[1]=32;
 970   info_array[2]=size>>11;
 971 
 972 #if defined(DEBUG_BIOSPARAM)
 973   printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
 974          info_array[0], info_array[1], info_array[2]);
 975   printk("WARNING: check, if the bios geometry is correct.\n");
 976 #endif
 977 
 978   return 0;
 979 }
 980 
 981 /*
 982  *  Internal done function
 983  */
 984 void aha152x_done( int error )
     /* [previous][next][first][last][top][bottom][index][help] */
 985 {
 986   Scsi_Cmnd *done_SC;
 987 
 988 #if defined(DEBUG_DONE)
 989   printk("\naha152x: done(), ");
 990   disp_ports();
 991 #endif
 992 
 993   if (current_SC)
 994     {
 995 #if defined(DEBUG_DONE)
 996       printk("done(%x), ", error);
 997 #endif
 998 
 999       cli();
1000 
1001       done_SC = current_SC;
1002       current_SC = NULL;
1003 
1004       /* turn led off, when no commands are in the driver */
1005       commands--;
1006       if(!commands)
1007         SETPORT( PORTA, 0 );                                  /* turn led off */
1008 
1009 #if defined(DEBUG_QUEUES)
1010       printk("ok (%d), ", commands);
1011 #endif
1012       sti();
1013 
1014       SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1015       SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
1016 
1017 #if defined(DEBUG_PHASES)
1018       printk("BUS FREE loop, ");
1019 #endif
1020       while( TESTLO( SSTAT1, BUSFREE ) )
1021         ;
1022 #if defined(DEBUG_PHASES)
1023       printk("BUS FREE\n");
1024 #endif
1025 
1026       done_SC->result = error;
1027       if(done_SC->scsi_done)
1028         {
1029 #if defined(DEBUG_DONE)
1030           printk("calling scsi_done, ");
1031 #endif
1032           done_SC->scsi_done( done_SC );
1033 #if defined(DEBUG_DONE)
1034           printk("done returned, ");
1035 #endif
1036         }
1037       else
1038         panic( "aha152x: current_SC->scsi_done() == NULL" );
1039     }
1040   else
1041     aha152x_panic( "done() called outside of command" );
1042 }
1043 
1044 /*
1045  * Interrupts handler (main routine of the driver)
1046  */
1047 void aha152x_intr( int irqno )
     /* [previous][next][first][last][top][bottom][index][help] */
1048 {
1049   int done=0, phase;
1050 
1051 #if defined(DEBUG_RACE)
1052   enter_driver("intr");
1053 #else
1054 #if defined(DEBUG_INTR)
1055   printk("\naha152x: intr(), ");
1056 #endif
1057 #endif
1058 
1059   /* no more interrupts from the controller, while we busy.
1060      INTEN has to be restored, when we're ready to leave
1061      intr(). To avoid race conditions we have to return
1062      immediately afterwards. */
1063   CLRBITS( DMACNTRL0, INTEN);
1064   sti();
1065 
1066   /* disconnected target is trying to reconnect.
1067      Only possible, if we have disconnected nexuses and
1068      nothing is occuping the bus.
1069   */
1070   if( TESTHI( SSTAT0, SELDI ) &&
1071       disconnected_SC &&
1072       ( !current_SC || ( current_SC->SCp.phase & in_selection ) )
1073     )
1074     {
1075       int identify_msg, target, i;
1076 
1077       /* Avoid conflicts when a target reconnects
1078          while we are trying to connect to another. */
1079       if(current_SC)
1080         {
1081 #if defined(DEBUG_QUEUES)
1082           printk("i+, ");
1083 #endif
1084           cli();
1085           append_SC( &issue_SC, current_SC);
1086           current_SC=NULL;
1087           sti();
1088         }
1089 
1090       /* disable sequences */
1091       SETPORT( SCSISEQ, 0 );
1092       SETPORT( SSTAT0, CLRSELDI );
1093       SETPORT( SSTAT1, CLRBUSFREE );
1094 
1095 #if defined(DEBUG_QUEUES) || defined(DEBUG_PHASES)
1096       printk("reselected, ");
1097 #endif
1098 
1099       i = GETPORT(SELID) & ~(1 << this_host);
1100       target=0;
1101       if(i)
1102         for( ; (i & 1)==0; target++, i>>=1)
1103           ;
1104       else
1105         aha152x_panic("reconnecting target unknown");
1106 
1107 #if defined(DEBUG_QUEUES)
1108       printk("SELID=%02x, target=%d, ", GETPORT(SELID), target );
1109 #endif
1110       SETPORT( SCSIID, (this_host << OID_) | target );
1111       SETPORT( SCSISEQ, ENRESELI );
1112 
1113       if(TESTLO( SSTAT0, SELDI ))
1114         aha152x_panic("RESELI failed");
1115 
1116       SETPORT( SCSISIG, P_MSGI );
1117 
1118       /* Get identify message */
1119       if((i=getphase())!=P_MSGI)
1120         {
1121           printk("target doesn't enter MSGI to identify (phase=%02x)\n", i);
1122           aha152x_panic("unknown lun");
1123         }
1124       SETPORT( SCSISEQ, 0 );
1125 
1126       SETPORT( SXFRCTL0, CH1);
1127 
1128       identify_msg = GETPORT(SCSIBUS);
1129 
1130       if(!(identify_msg & IDENTIFY_BASE))
1131         {
1132           printk("target=%d, inbound message (%02x) != IDENTIFY\n",
1133                  target, identify_msg);
1134           aha152x_panic("unknown lun");
1135         }
1136 
1137       make_acklow();
1138       getphase();
1139 
1140 #if defined(DEBUG_QUEUES)
1141       printk("identify=%02x, lun=%d, ", identify_msg, identify_msg & 0x3f );
1142 #endif
1143 
1144       cli();
1145 #if defined(DEBUG_QUEUES)
1146       printk("d-, ");
1147 #endif
1148       current_SC = remove_SC( &disconnected_SC,
1149                               target,
1150                               identify_msg & 0x3f );
1151 
1152       if(!current_SC)
1153         {
1154           printk("lun=%d, ", identify_msg & 0x3f );
1155           aha152x_panic("no disconnected command for that lun");
1156         }
1157 
1158       current_SC->SCp.phase &= ~disconnected;
1159       sti();
1160 
1161       SETPORT( SIMODE0, 0 );
1162       SETPORT( SIMODE1, ENPHASEMIS );
1163 #if defined(DEBUG_RACE)
1164       leave_driver("(reselected) intr");
1165 #endif
1166       SETBITS( DMACNTRL0, INTEN);
1167       return;
1168     }
1169   
1170   /* Check, if we aren't busy with a command */
1171   if(!current_SC)
1172     {
1173       /* bus is free to issue a queued command */
1174       if(TESTHI( SSTAT1, BUSFREE) && issue_SC)
1175         {
1176           cli();
1177 #if defined(DEBUG_QUEUES)
1178           printk("i-, ");
1179 #endif
1180           current_SC = remove_first_SC( &issue_SC );
1181           sti();
1182 
1183 #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1184           printk("issueing command, ");
1185 #endif
1186           current_SC->SCp.phase = in_selection;
1187 
1188   #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1189           printk("selecting %d, ", current_SC->target); 
1190   #endif
1191           SETPORT( SCSIID, (this_host << OID_) | current_SC->target );
1192 
1193           /* Enable interrupts for SELECTION OUT DONE and SELECTION OUT INITIATED */
1194           SETPORT( SXFRCTL1, ENSPCHK|ENSTIMER);
1195 
1196           /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
1197           SETPORT( SIMODE0, ENSELDO | (disconnected_SC ? ENSELDI : 0) );
1198           SETPORT( SIMODE1, ENSELTIMO );
1199 
1200           /* Enable SELECTION OUT sequence */
1201           SETBITS(SCSISEQ, ENSELO | ENAUTOATNO );
1202         
1203   #if defined(DEBUG_RACE)
1204           leave_driver("(selecting) intr");
1205   #endif
1206           SETBITS( DMACNTRL0, INTEN );
1207           return;
1208         }
1209 
1210       /* No command we are busy with and no new to issue */
1211       printk("aha152x: ignoring spurious interrupt, nothing to do\n");
1212       return;
1213     }
1214 
1215   /* the bus is busy with something */
1216 
1217 #if defined(DEBUG_INTR)
1218   disp_ports();
1219 #endif
1220 
1221   /* we are waiting for the result of a selection attempt */
1222   if(current_SC->SCp.phase & in_selection)
1223     {
1224       if( TESTLO( SSTAT1, SELTO ) )
1225         /* no timeout */
1226         if( TESTHI( SSTAT0, SELDO ) )
1227           {
1228             /* clear BUS FREE interrupt */
1229             SETPORT( SSTAT1, CLRBUSFREE);
1230 
1231             /* Disable SELECTION OUT sequence */
1232             CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO );
1233 
1234             /* Disable SELECTION OUT DONE interrupt */
1235             CLRBITS(SIMODE0, ENSELDO);
1236             CLRBITS(SIMODE1, ENSELTIMO);
1237 
1238             if( TESTLO(SSTAT0, SELDO) )
1239               {
1240                 printk("aha152x: passing bus free condition\n");
1241 
1242 #if defined(DEBUG_RACE)
1243                 leave_driver("(passing bus free) intr");
1244 #endif
1245                 SETBITS( DMACNTRL0, INTEN);
1246 
1247                 if(current_SC->SCp.phase & aborted)
1248                   {
1249                     abort_result=1;
1250                     wake_up( &abortion_complete );
1251                   }
1252 
1253                 aha152x_done( DID_NO_CONNECT << 16 );
1254                 return;
1255               }
1256 #if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1257             printk("SELDO (SELID=%x), ", GETPORT(SELID));
1258 #endif
1259 
1260             /* selection was done */
1261             SETPORT( SSTAT0, CLRSELDO );
1262 
1263 #if defined(DEBUG_ABORT)
1264             if(current_SC->SCp.phase & aborted)
1265               printk("(ABORT) target selected, ");
1266 #endif
1267 
1268             current_SC->SCp.phase &= ~in_selection;
1269             current_SC->SCp.phase |= in_other;
1270 
1271 #if defined(DEBUG_RACE)
1272             leave_driver("(SELDO) intr");
1273 #endif
1274 
1275             SETPORT( SCSISIG, P_MSGO );
1276 
1277             SETPORT( SIMODE0, 0 );
1278             SETPORT( SIMODE1, ENREQINIT );
1279             SETBITS( DMACNTRL0, INTEN);
1280             return;
1281           }
1282         else
1283           aha152x_panic("neither timeout nor selection\007");
1284       else
1285         {
1286 #if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1287           printk("SELTO, ");
1288 #endif
1289           /* end selection attempt */
1290           CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO );
1291 
1292           /* timeout */
1293           SETPORT( SSTAT1, CLRSELTIMO );
1294 
1295           SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1296           SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
1297           SETBITS( DMACNTRL0, INTEN );
1298 #if defined(DEBUG_RACE)
1299           leave_driver("(SELTO) intr");
1300 #endif
1301 
1302           if(current_SC->SCp.phase & aborted)
1303             {
1304 #if defined(DEBUG_ABORT)
1305               printk("(ABORT) selection timeout, ");
1306 #endif
1307               abort_result=1;
1308               wake_up( &abortion_complete );
1309             }
1310 
1311           if( TESTLO( SSTAT0, SELINGO ) )
1312             /* ARBITRATION not won */
1313             aha152x_done( DID_BUS_BUSY << 16 );
1314           else
1315             /* ARBITRATION won, but SELECTION failed */
1316             aha152x_done( DID_NO_CONNECT << 16 );
1317           return;
1318         }
1319     }
1320 
1321   /* enable interrupt, when target leaves current phase */
1322   phase = getphase();
1323   if(!(phase & ~P_MASK))                                      /* "real" phase */
1324     SETPORT(SCSISIG, phase);
1325   SETPORT(SSTAT1, CLRPHASECHG);
1326   current_SC->SCp.phase =
1327     (current_SC->SCp.phase & ~((P_MASK|1)<<16)) | (phase << 16 );
1328 
1329   /* information transfer phase */
1330   switch( phase )
1331     {
1332     case P_MSGO:                                               /* MESSAGE OUT */
1333       {
1334         unsigned char message;
1335 
1336 #if defined(DEBUG_INTR) || defined(DEBUG_MSGO) || defined(DEBUG_PHASES)
1337         printk("MESSAGE OUT, ");
1338 #endif
1339 
1340         if( current_SC->SCp.phase & aborted )
1341           {
1342 #if defined(DEBUG_MSGO) || defined(DEBUG_ABORT)
1343             printk("ABORT, ");
1344 #endif
1345             message=ABORT;
1346           }
1347         else
1348           /* If we didn't identify yet, do it. Otherwise there's nothing to do,
1349              but reject (probably we got an message before, that we have to
1350              reject (SDTR, WDTR, etc.) */
1351           if( !(current_SC->SCp.phase & sent_ident))
1352             {
1353               message=IDENTIFY(can_disconnect,current_SC->lun);
1354 #if defined(DEBUG_MSGO)
1355               printk("IDENTIFY (reconnect=%s;lun=%d), ", 
1356                       can_disconnect ? "enabled" : "disabled", current_SC->lun);
1357 #endif
1358             }
1359           else
1360             {
1361               message=MESSAGE_REJECT;
1362 #if defined(DEBUG_MSGO)
1363               printk("REJECT, ");
1364 #endif
1365             }
1366           
1367         CLRBITS( SXFRCTL0, ENDMA);
1368 
1369         SETPORT( SIMODE0, 0 );
1370         SETPORT( SIMODE1, ENPHASEMIS|ENREQINIT );
1371 
1372         /* wait for data latch to become ready or a phase change */
1373         while( TESTLO( DMASTAT, INTSTAT ) )
1374           ;
1375 
1376         if( TESTHI( SSTAT1, PHASEMIS ) )
1377           aha152x_panic("unable to send message");
1378 
1379         /* Leave MESSAGE OUT after transfer */
1380         SETPORT( SSTAT1, CLRATNO);
1381 
1382         SETPORT( SCSIDAT, message );
1383 
1384         make_acklow();
1385         getphase();
1386 
1387         if(message==IDENTIFY(can_disconnect,current_SC->lun))
1388           current_SC->SCp.phase |= sent_ident;
1389 
1390         if(message==ABORT)
1391           {
1392             /* revive abort(); abort() enables interrupts */
1393             abort_result=0;
1394             wake_up( &abortion_complete );
1395 
1396             current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));
1397 
1398             /* exit */
1399             SETBITS( DMACNTRL0, INTEN );
1400 #if defined(DEBUG_RACE)
1401             leave_driver("(ABORT) intr");
1402 #endif
1403             aha152x_done(DID_ABORT<<16);
1404             return;
1405           }
1406       }
1407       break;
1408 
1409     case P_CMD:                                          /* COMMAND phase */
1410 #if defined(DEBUG_INTR) || defined(DEBUG_CMD) || defined(DEBUG_PHASES)
1411       printk("COMMAND, ");
1412 #endif
1413       if( !(current_SC->SCp.sent_command) )
1414         {
1415           if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
1416             printk("aha152x: P_CMD: %d(%d) bytes left in FIFO, resetting\n",
1417                    GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
1418 
1419           /* reset fifo and enable writes */
1420           SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
1421           SETPORT(DMACNTRL0, ENDMA|WRITE_READ);
1422 
1423           /* clear transfer count and scsi fifo */
1424           SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1 );
1425           SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
1426   
1427           /* missing phase raises INTSTAT */
1428           SETPORT( SIMODE0, 0 );
1429           SETPORT( SIMODE1, ENPHASEMIS );
1430   
1431 #if defined(DEBUG_CMD)
1432           printk("waiting, ");
1433 #endif
1434           /* wait for FIFO to get empty */
1435           while( TESTLO ( DMASTAT, DFIFOEMP|INTSTAT ) )
1436             ;
1437   
1438           if( TESTHI( SSTAT1, PHASEMIS ) )
1439             aha152x_panic("target left COMMAND phase");
1440 
1441 #if defined(DEBUG_CMD)
1442           printk("DFIFOEMP, outsw (%d words), ",
1443                  COMMAND_SIZE(current_SC->cmnd[0])>>1);
1444           disp_ports();
1445 #endif
1446   
1447           outsw( DATAPORT,
1448                  &current_SC->cmnd,
1449                  COMMAND_SIZE(current_SC->cmnd[0])>>1 );
1450 
1451 #if defined(DEBUG_CMD)
1452           printk("FCNT=%d, STCNT=%d, ", GETPORT(FIFOSTAT), GETSTCNT() );
1453           disp_ports();
1454 #endif
1455 
1456           /* wait for SCSI FIFO to get empty.
1457              very important to send complete commands. */
1458           while( TESTLO ( SSTAT2, SEMPTY ) )
1459             ;
1460 
1461           CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
1462           /* transfer can be considered ended, when SCSIEN reads back zero */
1463           while( TESTHI( SXFRCTL0, SCSIEN ) )
1464             ;
1465 
1466           CLRBITS(DMACNTRL0, ENDMA);
1467 
1468 #if defined(DEBUG_CMD) || defined(DEBUG_INTR)
1469           printk("sent %d/%d command bytes, ", GETSTCNT(),
1470                  COMMAND_SIZE(current_SC->cmnd[0]));
1471 #endif
1472 
1473         }
1474       else
1475         aha152x_panic("Nothing to sent while in COMMAND OUT");
1476       break;
1477 
1478     case P_MSGI:                                          /* MESSAGE IN phase */
1479 #if defined(DEBUG_INTR) || defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1480       printk("MESSAGE IN, ");
1481 #endif
1482       SETPORT( SXFRCTL0, CH1);
1483 
1484       SETPORT( SIMODE0, 0);
1485       SETPORT( SIMODE1, ENBUSFREE);
1486   
1487       while( phase == P_MSGI ) 
1488         {
1489           current_SC->SCp.Message = GETPORT( SCSIBUS );
1490           switch(current_SC->SCp.Message)
1491             {
1492             case DISCONNECT:
1493 #if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1494               printk("target disconnected, ");
1495 #endif
1496               current_SC->SCp.Message = 0;
1497               current_SC->SCp.phase   |= disconnected;
1498               if(!can_disconnect)
1499                 aha152x_panic("target was not allowed to disconnect");
1500               break;
1501         
1502             case COMMAND_COMPLETE:
1503 #if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1504               printk("inbound message ( COMMAND COMPLETE ), ");
1505 #endif
1506               done++;
1507               break;
1508 
1509             case MESSAGE_REJECT:
1510 #if defined(DEBUG_MSGI) || defined(DEBUG_TIMING)
1511               printk("inbound message ( MESSAGE REJECT ), ");
1512 #endif
1513               break;
1514 
1515             case SAVE_POINTERS:
1516 #if defined(DEBUG_MSGI)
1517               printk("inbound message ( SAVE DATA POINTERS ), ");
1518 #endif
1519               break;
1520 
1521             case EXTENDED_MESSAGE:
1522               { 
1523                 int           i, code;
1524 
1525 #if defined(DEBUG_MSGI)
1526                 printk("inbound message ( EXTENDED MESSAGE ), ");
1527 #endif
1528                 make_acklow();
1529                 if(getphase()!=P_MSGI)
1530                   break;
1531   
1532                 i=GETPORT(SCSIBUS);
1533 
1534 #if defined(DEBUG_MSGI)
1535                 printk("length (%d), ", i);
1536 #endif
1537 
1538 #if defined(DEBUG_MSGI)
1539                 printk("code ( ");
1540 #endif
1541 
1542                 make_acklow();
1543                 if(getphase()!=P_MSGI)
1544                   break;
1545 
1546                 code = GETPORT(SCSIBUS);
1547 
1548                 switch( code )
1549                   {
1550                   case 0x00:
1551 #if defined(DEBUG_MSGI)
1552                     printk("MODIFY DATA POINTER ");
1553 #endif
1554                     SETPORT(SCSISIG, P_MSGI|ATNO);
1555                     break;
1556                   case 0x01:
1557 #if defined(DEBUG_MSGI)
1558                     printk("SYNCHRONOUS DATA TRANSFER REQUEST ");
1559 #endif
1560                     SETPORT(SCSISIG, P_MSGI|ATNO);
1561                     break;
1562                   case 0x02:
1563 #if defined(DEBUG_MSGI)
1564                     printk("EXTENDED IDENTIFY ");
1565 #endif
1566                     break;
1567                   case 0x03:
1568 #if defined(DEBUG_MSGI)
1569                     printk("WIDE DATA TRANSFER REQUEST ");
1570 #endif
1571                     SETPORT(SCSISIG, P_MSGI|ATNO);
1572                     break;
1573                   default:
1574 #if defined(DEBUG_MSGI)
1575                     if( code & 0x80 )
1576                       printk("reserved (%d) ", code );
1577                     else
1578                       printk("vendor specific (%d) ", code);
1579 #endif
1580                     SETPORT(SCSISIG, P_MSGI|ATNO);
1581                     break;
1582                   }
1583 #if defined(DEBUG_MSGI)
1584                 printk(" ), data ( ");
1585 #endif
1586                 while( --i && (make_acklow(), getphase()==P_MSGI))
1587                   {
1588 #if defined(DEBUG_MSGI)
1589                     printk("%x ", GETPORT(SCSIBUS) );
1590 #else
1591                     GETPORT(SCSIBUS);
1592 #endif
1593                   }
1594 #if defined(DEBUG_MSGI)
1595                 printk(" ), ");
1596 #endif
1597                 /* We reject all extended messages. To do this
1598                    we just enter MSGO by asserting ATN. Since
1599                    we have already identified a REJECT message
1600                    will be sent. */
1601                 SETPORT(SCSISIG, P_MSGI|ATNO);
1602               }
1603               break;
1604        
1605             default:
1606               printk("unsupported inbound message %x, ", current_SC->SCp.Message);
1607               break;
1608 
1609             }
1610 
1611           make_acklow();
1612           phase=getphase();
1613         } 
1614 
1615       /* clear SCSI fifo on BUSFREE */
1616       if(phase==P_BUSFREE)
1617         SETPORT(SXFRCTL0, CH1|CLRCH1);
1618 
1619       if(current_SC->SCp.phase & disconnected)
1620         {
1621           cli();
1622 #if defined(DEBUG_QUEUES)
1623           printk("d+, ");
1624 #endif
1625           append_SC( &disconnected_SC, current_SC);
1626           current_SC = NULL;
1627           sti();
1628 
1629           SETBITS( SCSISEQ, ENRESELI );
1630 
1631           SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1632           SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
1633 
1634           SETBITS( DMACNTRL0, INTEN );
1635           return;
1636         }
1637       break;
1638 
1639     case P_STATUS:                                         /* STATUS IN phase */
1640 #if defined(DEBUG_STATUS) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1641       printk("STATUS, ");
1642 #endif
1643       SETPORT( SXFRCTL0, CH1);
1644 
1645       SETPORT( SIMODE0, 0 );
1646       SETPORT( SIMODE1, ENREQINIT );
1647 
1648       if( TESTHI( SSTAT1, PHASEMIS ) )
1649         printk("aha152x: passing STATUS phase");
1650         
1651       current_SC->SCp.Status = GETPORT( SCSIBUS );
1652       make_acklow();
1653       getphase();
1654 
1655 #if defined(DEBUG_STATUS)
1656       printk("inbound status ");
1657       print_status( current_SC->SCp.Status );
1658       printk(", ");
1659 #endif
1660       break;
1661 
1662     case P_DATAI:                                            /* DATA IN phase */
1663       {
1664         int fifodata, data_count, done;
1665 
1666 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1667         printk("DATA IN, ");
1668 #endif
1669 
1670         if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
1671           printk("aha152x: P_DATAI: %d(%d) bytes left in FIFO, resetting\n",
1672                  GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
1673 
1674         /* reset host fifo */
1675         SETPORT(DMACNTRL0, RSTFIFO);
1676         SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
1677 
1678         SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN );
1679 
1680         SETPORT( SIMODE0, 0 );
1681         SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );
1682 
1683         /* done is set when the FIFO is empty after the target left DATA IN */
1684         done=0;
1685       
1686         /* while the target stays in DATA to transfer data */
1687         while ( !done ) 
1688           {
1689 #if defined(DEBUG_DATAI)
1690             printk("expecting data, ");
1691 #endif
1692             /* wait for PHASEMIS or full FIFO */
1693             while( TESTLO ( DMASTAT, DFIFOFULL|INTSTAT ) )
1694               ;
1695 
1696             if( TESTHI( DMASTAT, DFIFOFULL ) )
1697               fifodata=GETPORT(FIFOSTAT);
1698             else
1699               {
1700                 /* wait for SCSI fifo to get empty */
1701                 while( TESTLO( SSTAT2, SEMPTY ) )
1702                   ;
1703 
1704                 /* rest of data in FIFO */
1705                 fifodata=GETPORT(FIFOSTAT);
1706 #if defined(DEBUG_DATAI)
1707                 printk("last transfer, ");
1708 #endif
1709                 done=1;
1710               }
1711   
1712 #if defined(DEBUG_DATAI)
1713             printk("fifodata=%d, ", fifodata);
1714 #endif
1715 
1716             while( fifodata && current_SC->SCp.this_residual )
1717               {
1718                 data_count=fifodata;
1719   
1720                 /* limit data transfer to size of first sg buffer */
1721                 if (data_count > current_SC->SCp.this_residual)
1722                   data_count = current_SC->SCp.this_residual;
1723   
1724                 fifodata -= data_count;
1725 
1726 #if defined(DEBUG_DATAI)
1727                 printk("data_count=%d, ", data_count);
1728 #endif
1729   
1730                 if(data_count == 1)
1731                   {
1732                     /* get a single byte in byte mode */
1733                     SETBITS(DMACNTRL0, _8BIT );
1734                     *current_SC->SCp.ptr++ = GETPORT( DATAPORT );
1735                     current_SC->SCp.this_residual--;
1736                   }
1737                 else
1738                   {
1739                     CLRBITS(DMACNTRL0, _8BIT );
1740                     data_count >>= 1; /* Number of words */
1741                     insw( DATAPORT, current_SC->SCp.ptr, data_count );
1742 #if defined(DEBUG_DATAI)
1743 /* show what comes with the last transfer */
1744                     if(done)
1745                       {
1746                         int           i;
1747                         unsigned char *data;
1748 
1749                         printk("data on last transfer (%d bytes: ",
1750                                2*data_count);
1751                         data = (unsigned char *) current_SC->SCp.ptr;
1752                         for( i=0; i<2*data_count; i++)
1753                           printk("%2x ", *data++);
1754                         printk("), ");
1755                       }
1756 #endif
1757                     current_SC->SCp.ptr           += 2 * data_count;
1758                     current_SC->SCp.this_residual -= 2 * data_count;
1759                   }
1760               
1761                 /* if this buffer is full and there are more buffers left */
1762                 if (!current_SC->SCp.this_residual &&
1763                      current_SC->SCp.buffers_residual)
1764                   {
1765                     /* advance to next buffer */
1766                     current_SC->SCp.buffers_residual--;
1767                     current_SC->SCp.buffer++;
1768                     current_SC->SCp.ptr =
1769                       current_SC->SCp.buffer->address;
1770                     current_SC->SCp.this_residual =
1771                       current_SC->SCp.buffer->length;
1772                   } 
1773               }
1774  
1775             /*
1776              * Fifo should be empty
1777              */
1778             if(fifodata>0)
1779               {
1780                 printk("aha152x: more data than expected (%d bytes)\n",
1781                        GETPORT(FIFOSTAT));
1782                 SETBITS(DMACNTRL0, _8BIT );
1783                 printk("aha152x: data ( ");
1784                 while(fifodata--)
1785                   printk("%2x ", GETPORT( DATAPORT ));
1786                 printk(")\n");
1787               }
1788 
1789 #if defined(DEBUG_DATAI)
1790             if(!fifodata)
1791               printk("fifo empty, ");
1792             else
1793               printk("something left in fifo, ");
1794 #endif
1795           }
1796 
1797 #if defined(DEBUG_DATAI)
1798         if(current_SC->SCp.buffers_residual || current_SC->SCp.this_residual)
1799           printk("left buffers (buffers=%d, bytes=%d), ",
1800                  current_SC->SCp.buffers_residual, 
1801                  current_SC->SCp.this_residual);
1802 #endif
1803         /* transfer can be considered ended, when SCSIEN reads back zero */
1804         CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
1805         while( TESTHI( SXFRCTL0, SCSIEN ) )
1806           ;
1807         CLRBITS(DMACNTRL0, ENDMA );
1808 
1809 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR)
1810         printk("got %d bytes, ", GETSTCNT());
1811 #endif
1812 
1813         current_SC->SCp.have_data_in++;
1814       }
1815       break;
1816 
1817     case P_DATAO:                                           /* DATA OUT phase */
1818       {
1819         int data_count;
1820 
1821 #if defined(DEBUG_DATAO) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1822         printk("DATA OUT, ");
1823 #endif
1824 #if defined(DEBUG_DATAO)
1825         printk("got data to send (bytes=%d, buffers=%d), ",
1826                current_SC->SCp.this_residual,
1827                current_SC->SCp.buffers_residual );
1828 #endif
1829 
1830         if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT) )
1831           {
1832             printk("%d(%d) left in FIFO, ", GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT) );
1833             aha152x_panic("FIFO should be empty");
1834           }
1835 
1836         SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
1837         SETPORT(DMACNTRL0, ENDMA|WRITE_READ);
1838 
1839         SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1 );
1840         SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
1841  
1842         SETPORT( SIMODE0, 0 );
1843         SETPORT( SIMODE1, ENPHASEMIS );
1844 
1845         /* while current buffer is not empty or
1846            there are more buffers to transfer */
1847         while( TESTLO( SSTAT1, PHASEMIS ) &&
1848                  (current_SC->SCp.this_residual ||
1849                   current_SC->SCp.buffers_residual) )
1850           {
1851 #if defined(DEBUG_DATAO)
1852             printk("sending data (left: bytes=%d, buffers=%d), waiting, ",
1853                     current_SC->SCp.this_residual,
1854                     current_SC->SCp.buffers_residual);
1855 #endif
1856             /* transfer rest of buffer, but max. 128 byte */
1857             data_count = current_SC->SCp.this_residual > 128 ?
1858                          128 : current_SC->SCp.this_residual ;
1859 
1860 #if defined(DEBUG_DATAO)
1861             printk("data_count=%d, ", data_count);
1862 #endif
1863   
1864             if(data_count == 1)
1865               {
1866                 /* put a single byte in byte mode */
1867                 SETBITS(DMACNTRL0, _8BIT );
1868                 SETPORT(DATAPORT, *current_SC->SCp.ptr++);
1869                 current_SC->SCp.this_residual--;
1870               }
1871             else
1872               {
1873                 CLRBITS(DMACNTRL0, _8BIT );
1874                 data_count >>= 1; /* Number of words */
1875                 outsw( DATAPORT, current_SC->SCp.ptr, data_count );
1876                 current_SC->SCp.ptr           += 2 * data_count;
1877                 current_SC->SCp.this_residual -= 2 * data_count;
1878               }
1879 
1880             /* wait for FIFO to get empty */
1881             while( TESTLO ( DMASTAT, DFIFOEMP|INTSTAT ) )
1882               ;
1883 
1884 #if defined(DEBUG_DATAO)
1885             printk("fifo (%d bytes), transfered (%d bytes), ",
1886                    GETPORT(FIFOSTAT), GETSTCNT() );
1887 #endif
1888 
1889             /* if this buffer is empty and there are more buffers left */
1890             if ( TESTLO( SSTAT1, PHASEMIS ) &&
1891                  !current_SC->SCp.this_residual &&
1892                   current_SC->SCp.buffers_residual)
1893               {
1894                  /* advance to next buffer */
1895                  current_SC->SCp.buffers_residual--;
1896                  current_SC->SCp.buffer++;
1897                  current_SC->SCp.ptr =
1898                    current_SC->SCp.buffer->address;
1899                  current_SC->SCp.this_residual =
1900                  current_SC->SCp.buffer->length;
1901               }
1902           }
1903 
1904         if ( current_SC->SCp.this_residual ||
1905              current_SC->SCp.buffers_residual )
1906           {
1907             /* target leaves DATA OUT for an other phase
1908                (perhaps disconnect) */
1909 
1910             /* data in fifos has to be resend */
1911             data_count = GETPORT(SSTAT2) & (SFULL|SFCNT);
1912 
1913             data_count += GETPORT(FIFOSTAT) ;
1914             current_SC->SCp.ptr           -= data_count;
1915             current_SC->SCp.this_residual += data_count;
1916 #if defined(DEBUG_DATAO)
1917             printk("left data (bytes=%d, buffers=%d), fifos (bytes=%d), transfer incomplete, resetting fifo, ",
1918                    current_SC->SCp.this_residual,
1919                    current_SC->SCp.buffers_residual,
1920                    data_count );
1921 #endif
1922             SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
1923             CLRBITS(SXFRCTL0, SCSIEN|DMAEN );
1924             CLRBITS(DMACNTRL0, ENDMA);
1925           }
1926         else
1927           {
1928 #if defined(DEBUG_DATAO)
1929             printk("waiting for SCSI fifo to get empty, ");
1930 #endif
1931             /* wait for SCSI fifo to get empty */
1932             while( TESTLO( SSTAT2, SEMPTY ) )
1933               ;
1934 #if defined(DEBUG_DATAO)
1935             printk("ok, ");
1936 #endif
1937 
1938 #if defined(DEBUG_DATAO)
1939             printk("left data (bytes=%d, buffers=%d) ",
1940                    current_SC->SCp.this_residual,
1941                    current_SC->SCp.buffers_residual);
1942 #endif
1943             CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
1944 
1945             /* transfer can be considered ended, when SCSIEN reads back zero */
1946             while( TESTHI( SXFRCTL0, SCSIEN ) )
1947               ;
1948 
1949             CLRBITS(DMACNTRL0, ENDMA);
1950           }
1951 
1952 #if defined(DEBUG_DATAO) || defined(DEBUG_INTR)
1953         printk("sent %d data bytes, ", GETSTCNT() );
1954 #endif
1955       }
1956       break;
1957 
1958     case P_BUSFREE:                                                /* BUSFREE */
1959 #if defined(DEBUG_RACE)
1960       leave_driver("(BUSFREE) intr");
1961 #endif
1962 #if defined(DEBUG_PHASES)
1963       printk("unexpected BUS FREE, ");
1964 #endif
1965       current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));
1966 
1967       aha152x_done( DID_ERROR << 16 );               /* Don't know any better */
1968       return;
1969       break;
1970 
1971     case P_PARITY:                              /* parity error in DATA phase */
1972 #if defined(DEBUG_RACE)
1973       leave_driver("(DID_PARITY) intr");
1974 #endif
1975       printk("PARITY error in DATA phase, ");
1976 
1977       current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));
1978 
1979       SETBITS( DMACNTRL0, INTEN );
1980       aha152x_done( DID_PARITY << 16 );
1981       return;
1982       break;
1983 
1984     default:
1985       printk("aha152x: unexpected phase\n");
1986       break;
1987     }
1988 
1989   if(done)
1990     {
1991 #if defined(DEBUG_INTR)
1992       printk("command done.\n");
1993 #endif
1994 #if defined(DEBUG_RACE)
1995       leave_driver("(done) intr");
1996 #endif
1997 
1998       SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1999       SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
2000       SETPORT( SCSISEQ, disconnected_SC ? ENRESELI : 0 );
2001 
2002       SETBITS( DMACNTRL0, INTEN );
2003 
2004       aha152x_done(   (current_SC->SCp.Status  & 0xff)
2005                     | ( (current_SC->SCp.Message & 0xff) << 8)
2006                     | ( DID_OK << 16) );
2007 
2008 #if defined(DEBUG_RACE)
2009       printk("done returned (DID_OK: Status=%x; Message=%x).\n",
2010              current_SC->SCp.Status, current_SC->SCp.Message);
2011 #endif
2012       return;
2013     }
2014 
2015   if(current_SC)
2016     current_SC->SCp.phase |= 1<<16 ;
2017 
2018   SETPORT( SIMODE0, 0 );
2019   SETPORT( SIMODE1, ENPHASEMIS );
2020 #if defined(DEBUG_INTR)
2021   disp_enintr();
2022 #endif
2023 #if defined(DEBUG_RACE)
2024   leave_driver("(PHASEEND) intr");
2025 #endif
2026 
2027   SETBITS( DMACNTRL0, INTEN);
2028   return;
2029 }
2030 
2031 /* 
2032  * Dump the current driver status and panic...
2033  */
2034 static void aha152x_panic(char *msg)
     /* [previous][next][first][last][top][bottom][index][help] */
2035 {
2036   printk("\naha152x_panic: %s\n", msg);
2037   show_queues();
2038   panic("aha152x panic");
2039 }
2040 
2041 /*
2042  * Display registers of AIC-6260
2043  */
2044 static void disp_ports(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2045 {
2046 #if !defined(SKIP_PORTS)
2047   int s;
2048 
2049   printk("\n%s: ", current_SC ? "on bus" : "waiting");
2050 
2051   s=GETPORT(SCSISEQ);
2052   printk("SCSISEQ ( ");
2053   if( s & TEMODEO )     printk("TARGET MODE ");
2054   if( s & ENSELO )      printk("SELO ");
2055   if( s & ENSELI )      printk("SELI ");
2056   if( s & ENRESELI )    printk("RESELI ");
2057   if( s & ENAUTOATNO )  printk("AUTOATNO ");
2058   if( s & ENAUTOATNI )  printk("AUTOATNI ");
2059   if( s & ENAUTOATNP )  printk("AUTOATNP ");
2060   if( s & SCSIRSTO )    printk("SCSIRSTO ");
2061   printk(");");
2062 
2063   printk(" SCSISIG ( ");
2064   s=GETPORT(SCSISIG);
2065   switch(s & P_MASK)
2066     {
2067     case P_DATAO:
2068       printk("DATA OUT");
2069       break;
2070     case P_DATAI:
2071       printk("DATA IN");
2072       break;
2073     case P_CMD:
2074       printk("COMMAND"); 
2075       break;
2076     case P_STATUS:
2077       printk("STATUS"); 
2078       break;
2079     case P_MSGO:
2080       printk("MESSAGE OUT");
2081       break;
2082     case P_MSGI:
2083       printk("MESSAGE IN");
2084       break;
2085     default:
2086       printk("*illegal*");
2087       break;
2088     }
2089   
2090   printk(" ); ");
2091 
2092   printk("INTSTAT ( %s ); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
2093 
2094   printk("SSTAT ( ");
2095   s=GETPORT(SSTAT0);
2096   if( s & TARGET )   printk("TARGET ");
2097   if( s & SELDO )    printk("SELDO ");
2098   if( s & SELDI )    printk("SELDI ");
2099   if( s & SELINGO )  printk("SELINGO ");
2100   if( s & SWRAP )    printk("SWRAP ");
2101   if( s & SDONE )    printk("SDONE ");
2102   if( s & SPIORDY )  printk("SPIORDY ");
2103   if( s & DMADONE )  printk("DMADONE ");
2104 
2105   s=GETPORT(SSTAT1);
2106   if( s & SELTO )     printk("SELTO ");
2107   if( s & ATNTARG )   printk("ATNTARG ");
2108   if( s & SCSIRSTI )  printk("SCSIRSTI ");
2109   if( s & PHASEMIS )  printk("PHASEMIS ");
2110   if( s & BUSFREE )   printk("BUSFREE ");
2111   if( s & SCSIPERR )  printk("SCSIPERR ");
2112   if( s & PHASECHG )  printk("PHASECHG ");
2113   if( s & REQINIT )   printk("REQINIT ");
2114   printk("); ");
2115 
2116 
2117   printk("SSTAT ( ");
2118 
2119   s=GETPORT(SSTAT0) & GETPORT(SIMODE0);
2120 
2121   if( s & TARGET )    printk("TARGET ");
2122   if( s & SELDO )     printk("SELDO ");
2123   if( s & SELDI )     printk("SELDI ");
2124   if( s & SELINGO )   printk("SELINGO ");
2125   if( s & SWRAP )     printk("SWRAP ");
2126   if( s & SDONE )     printk("SDONE ");
2127   if( s & SPIORDY )   printk("SPIORDY ");
2128   if( s & DMADONE )   printk("DMADONE ");
2129 
2130   s=GETPORT(SSTAT1) & GETPORT(SIMODE1);
2131 
2132   if( s & SELTO )     printk("SELTO ");
2133   if( s & ATNTARG )   printk("ATNTARG ");
2134   if( s & SCSIRSTI )  printk("SCSIRSTI ");
2135   if( s & PHASEMIS )  printk("PHASEMIS ");
2136   if( s & BUSFREE )   printk("BUSFREE ");
2137   if( s & SCSIPERR )  printk("SCSIPERR ");
2138   if( s & PHASECHG )  printk("PHASECHG ");
2139   if( s & REQINIT )   printk("REQINIT ");
2140   printk("); ");
2141 
2142   printk("SXFRCTL0 ( ");
2143 
2144   s=GETPORT(SXFRCTL0);
2145   if( s & SCSIEN )    printk("SCSIEN ");
2146   if( s & DMAEN )     printk("DMAEN ");
2147   if( s & CH1 )       printk("CH1 ");
2148   if( s & CLRSTCNT )  printk("CLRSTCNT ");
2149   if( s & SPIOEN )    printk("SPIOEN ");
2150   if( s & CLRCH1 )    printk("CLRCH1 ");
2151   printk("); ");
2152 
2153   printk("SIGNAL ( ");
2154 
2155   s=GETPORT(SCSISIG);
2156   if( s & ATNI )  printk("ATNI ");
2157   if( s & SELI )  printk("SELI ");
2158   if( s & BSYI )  printk("BSYI ");
2159   if( s & REQI )  printk("REQI ");
2160   if( s & ACKI )  printk("ACKI ");
2161   printk("); ");
2162 
2163   printk("SELID ( %02x ), ", GETPORT(SELID) );
2164 
2165   printk("SSTAT2 ( ");
2166 
2167   s=GETPORT(SSTAT2);
2168   if( s & SOFFSET)  printk("SOFFSET ");
2169   if( s & SEMPTY)   printk("SEMPTY ");
2170   if( s & SFULL)    printk("SFULL ");
2171   printk("); SFCNT ( %d ); ", s & (SFULL|SFCNT) );
2172 
2173 #if 0
2174   printk("SSTAT4 ( ");
2175   s=GETPORT(SSTAT4);
2176   if( s & SYNCERR)   printk("SYNCERR ");
2177   if( s & FWERR)     printk("FWERR ");
2178   if( s & FRERR)     printk("FRERR ");
2179   printk("); ");
2180 #endif
2181 
2182   printk("FCNT ( %d ); ", GETPORT(FIFOSTAT) );
2183 
2184   printk("DMACNTRL0 ( ");
2185   s=GETPORT(DMACNTRL0);
2186   printk( "%s ", s & _8BIT      ? "8BIT"  : "16BIT" );
2187   printk( "%s ", s & DMA        ? "DMA"   : "PIO"   );
2188   printk( "%s ", s & WRITE_READ ? "WRITE" : "READ"  );
2189   if( s & ENDMA )    printk("ENDMA ");
2190   if( s & INTEN )    printk("INTEN ");
2191   if( s & RSTFIFO )  printk("RSTFIFO ");
2192   if( s & SWINT )    printk("SWINT ");
2193   printk("); ");
2194 
2195 
2196 #if 0
2197   printk("DMACNTRL1 ( ");
2198 
2199   s=GETPORT(DMACNTRL1);
2200   if( s & PWRDWN )    printk("PWRDN ");
2201   printk("); ");
2202 
2203 
2204   printk("STK ( %d ); ", s & 0xf);
2205 
2206   printk("DMASTAT (");
2207   s=GETPORT(DMASTAT);
2208   if( s & ATDONE )     printk("ATDONE ");
2209   if( s & WORDRDY )    printk("WORDRDY ");
2210   if( s & DFIFOFULL )  printk("DFIFOFULL ");
2211   if( s & DFIFOEMP )   printk("DFIFOEMP ");
2212   printk(")");
2213 
2214 #endif
2215 
2216   printk("\n");
2217 #endif
2218 }
2219 
2220 /*
2221  * display enabled interrupts
2222  */
2223 static void disp_enintr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2224 {
2225   int s;
2226 
2227   printk("enabled interrupts ( ");
2228   
2229   s=GETPORT(SIMODE0);
2230   if( s & ENSELDO )    printk("ENSELDO ");
2231   if( s & ENSELDI )    printk("ENSELDI ");
2232   if( s & ENSELINGO )  printk("ENSELINGO ");
2233   if( s & ENSWRAP )    printk("ENSWRAP ");
2234   if( s & ENSDONE )    printk("ENSDONE ");
2235   if( s & ENSPIORDY )  printk("ENSPIORDY ");
2236   if( s & ENDMADONE )  printk("ENDMADONE ");
2237 
2238   s=GETPORT(SIMODE1);
2239   if( s & ENSELTIMO )    printk("ENSELTIMO ");
2240   if( s & ENATNTARG )    printk("ENATNTARG ");
2241   if( s & ENPHASEMIS )   printk("ENPHASEMIS ");
2242   if( s & ENBUSFREE )    printk("ENBUSFREE ");
2243   if( s & ENSCSIPERR )   printk("ENSCSIPERR ");
2244   if( s & ENPHASECHG )   printk("ENPHASECHG ");
2245   if( s & ENREQINIT )    printk("ENREQINIT ");
2246   printk(")\n");
2247 }
2248 
2249 #if defined(DEBUG_RACE)
2250 
2251 static const char *should_leave;
2252 static int in_driver=0;
2253 
2254 /*
2255  * Only one routine can be in the driver at once.
2256  */
2257 static void enter_driver(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
2258 {
2259   cli();
2260   printk("aha152x: entering %s() (%x)\n", func, jiffies);
2261   if(in_driver)
2262     {
2263       printk("%s should leave first.\n", should_leave);
2264       panic("aha152x: already in driver\n");
2265     }
2266 
2267   in_driver++;
2268   should_leave=func;
2269   sti();
2270 }
2271 
2272 static void leave_driver(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
2273 {
2274   cli();
2275   printk("\naha152x: leaving %s() (%x)\n", func, jiffies);
2276   if(!in_driver)
2277     {
2278       printk("aha152x: %s already left.\n", should_leave);
2279       panic("aha152x: %s already left driver.\n");
2280     }
2281 
2282   in_driver--;
2283   should_leave=func;
2284   sti();
2285 }
2286 #endif
2287 
2288 /*
2289  * Show the command data of a command
2290  */
2291 static void show_command(Scsi_Cmnd *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2292 {
2293   int i;
2294 
2295   printk("0x%08x: target=%d; lun=%d; cmnd=( ",
2296          (unsigned int) ptr, ptr->target, ptr->lun);
2297   
2298   for(i=0; i<COMMAND_SIZE(ptr->cmnd[0]); i++)
2299     printk("%02x ", ptr->cmnd[i]);
2300 
2301   printk("); residual=%d; buffers=%d; phase |",
2302          ptr->SCp.this_residual, ptr->SCp.buffers_residual);
2303 
2304   if( ptr->SCp.phase & not_issued   )  printk("not issued|");
2305   if( ptr->SCp.phase & in_selection )  printk("in selection|");
2306   if( ptr->SCp.phase & disconnected )  printk("disconnected|");
2307   if( ptr->SCp.phase & aborted      )  printk("aborted|");
2308   if( ptr->SCp.phase & sent_ident   )  printk("send_ident|");
2309   if( ptr->SCp.phase & in_other )
2310     { 
2311       printk("; in other(");
2312       switch( (ptr->SCp.phase >> 16) & P_MASK )
2313         {
2314         case P_DATAO:
2315           printk("DATA OUT");
2316           break;
2317         case P_DATAI:
2318           printk("DATA IN");
2319           break;
2320         case P_CMD:
2321           printk("COMMAND");
2322           break;
2323         case P_STATUS:
2324           printk("STATUS");
2325           break;
2326         case P_MSGO:
2327           printk("MESSAGE OUT");
2328           break;
2329         case P_MSGI:
2330           printk("MESSAGE IN");
2331           break;
2332         default: 
2333           printk("*illegal*");
2334           break;
2335         }
2336       printk(")");
2337       if(ptr->SCp.phase & (1<<16))
2338         printk("; phaseend");
2339     }
2340   printk("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
2341 }
2342  
2343 /*
2344  * Dump the queued data
2345  */
2346 static void show_queues(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2347 {
2348   Scsi_Cmnd *ptr;
2349 
2350   cli();
2351   printk("QUEUE STATUS:\nissue_SC:\n");
2352   for(ptr=issue_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble )
2353     show_command(ptr);
2354 
2355   printk("current_SC:\n");
2356   if(current_SC)
2357     show_command(current_SC);
2358   else
2359     printk("none\n");
2360 
2361   printk("disconnected_SC:\n");
2362   for(ptr=disconnected_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble )
2363     show_command(ptr);
2364 
2365   disp_ports();
2366   disp_enintr();
2367   sti();
2368 }

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