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 (ericy@cais.com) 
 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)
     /* [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 = DID_ABORT << 16;
 813       ptr->done(ptr);
 814       return SCSI_ABORT_SUCCESS;
 815     }
 816 
 817   /* Fail abortion, if we're on the bus */
 818   if (current_SC)
 819     {
 820        sti();
 821        return SCSI_ABORT_BUSY;
 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=SCSI_ABORT_SUCCESS;
 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 SCSI_ABORT_NOT_RUNNING;
 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        /* FIXME - if the device implements soft resets, the command will still
 926           be running after the bus reset. In this case we should do nothing
 927           and let the command continue. -ERY */
 928 
 929        if(current_SC)
 930          {
 931            current_SC->host_scribble = NULL;
 932            current_SC->result = DID_RESET << 16;
 933            current_SC->done(current_SC);
 934            current_SC=NULL;
 935          }
 936 
 937        while(disconnected_SC)
 938          {
 939            ptr = disconnected_SC;
 940            disconnected_SC = (Scsi_Cmnd *) ptr->host_scribble;
 941            ptr->host_scribble = NULL;
 942            ptr->result = DID_RESET << 16;
 943            ptr->done(ptr);
 944          }
 945 
 946        /* RESET OUT */
 947        SETPORT(SCSISEQ, SCSIRSTO);
 948        do_pause(5);
 949        SETPORT(SCSISEQ, 0);
 950        do_pause(10);
 951 
 952        SETPORT(SIMODE0, 0 );
 953        SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
 954 
 955        SETPORT( DMACNTRL0, INTEN );
 956     }
 957 
 958   return SCSI_RESET_SUCCESS;
 959 }
 960 
 961 /*
 962  * Return the "logical geometry"
 963  */
 964 int aha152x_biosparam( int size, int dev, int *info_array )
     /* [previous][next][first][last][top][bottom][index][help] */
 965 {
 966 #if defined(DEBUG_BIOSPARAM)
 967   printk("aha152x_biosparam: dev=%x, size=%d, ", dev, size);
 968 #endif
 969   
 970 /* I took this from other SCSI drivers, since it provides
 971    the correct data for my devices. */
 972   info_array[0]=64;
 973   info_array[1]=32;
 974   info_array[2]=size>>11;
 975 
 976 #if defined(DEBUG_BIOSPARAM)
 977   printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
 978          info_array[0], info_array[1], info_array[2]);
 979   printk("WARNING: check, if the bios geometry is correct.\n");
 980 #endif
 981 
 982   return 0;
 983 }
 984 
 985 /*
 986  *  Internal done function
 987  */
 988 void aha152x_done( int error )
     /* [previous][next][first][last][top][bottom][index][help] */
 989 {
 990   Scsi_Cmnd *done_SC;
 991 
 992 #if defined(DEBUG_DONE)
 993   printk("\naha152x: done(), ");
 994   disp_ports();
 995 #endif
 996 
 997   if (current_SC)
 998     {
 999 #if defined(DEBUG_DONE)
1000       printk("done(%x), ", error);
1001 #endif
1002 
1003       cli();
1004 
1005       done_SC = current_SC;
1006       current_SC = NULL;
1007 
1008       /* turn led off, when no commands are in the driver */
1009       commands--;
1010       if(!commands)
1011         SETPORT( PORTA, 0 );                                  /* turn led off */
1012 
1013 #if defined(DEBUG_QUEUES)
1014       printk("ok (%d), ", commands);
1015 #endif
1016       sti();
1017 
1018       SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1019       SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
1020 
1021 #if defined(DEBUG_PHASES)
1022       printk("BUS FREE loop, ");
1023 #endif
1024       while( TESTLO( SSTAT1, BUSFREE ) )
1025         ;
1026 #if defined(DEBUG_PHASES)
1027       printk("BUS FREE\n");
1028 #endif
1029 
1030       done_SC->result = error;
1031       if(done_SC->scsi_done)
1032         {
1033 #if defined(DEBUG_DONE)
1034           printk("calling scsi_done, ");
1035 #endif
1036           done_SC->scsi_done( done_SC );
1037 #if defined(DEBUG_DONE)
1038           printk("done returned, ");
1039 #endif
1040         }
1041       else
1042         panic( "aha152x: current_SC->scsi_done() == NULL" );
1043     }
1044   else
1045     aha152x_panic( "done() called outside of command" );
1046 }
1047 
1048 /*
1049  * Interrupts handler (main routine of the driver)
1050  */
1051 void aha152x_intr( int irqno )
     /* [previous][next][first][last][top][bottom][index][help] */
1052 {
1053   int done=0, phase;
1054 
1055 #if defined(DEBUG_RACE)
1056   enter_driver("intr");
1057 #else
1058 #if defined(DEBUG_INTR)
1059   printk("\naha152x: intr(), ");
1060 #endif
1061 #endif
1062 
1063   /* no more interrupts from the controller, while we busy.
1064      INTEN has to be restored, when we're ready to leave
1065      intr(). To avoid race conditions we have to return
1066      immediately afterwards. */
1067   CLRBITS( DMACNTRL0, INTEN);
1068   sti();
1069 
1070   /* disconnected target is trying to reconnect.
1071      Only possible, if we have disconnected nexuses and
1072      nothing is occuping the bus.
1073   */
1074   if( TESTHI( SSTAT0, SELDI ) &&
1075       disconnected_SC &&
1076       ( !current_SC || ( current_SC->SCp.phase & in_selection ) )
1077     )
1078     {
1079       int identify_msg, target, i;
1080 
1081       /* Avoid conflicts when a target reconnects
1082          while we are trying to connect to another. */
1083       if(current_SC)
1084         {
1085 #if defined(DEBUG_QUEUES)
1086           printk("i+, ");
1087 #endif
1088           cli();
1089           append_SC( &issue_SC, current_SC);
1090           current_SC=NULL;
1091           sti();
1092         }
1093 
1094       /* disable sequences */
1095       SETPORT( SCSISEQ, 0 );
1096       SETPORT( SSTAT0, CLRSELDI );
1097       SETPORT( SSTAT1, CLRBUSFREE );
1098 
1099 #if defined(DEBUG_QUEUES) || defined(DEBUG_PHASES)
1100       printk("reselected, ");
1101 #endif
1102 
1103       i = GETPORT(SELID) & ~(1 << this_host);
1104       target=0;
1105       if(i)
1106         for( ; (i & 1)==0; target++, i>>=1)
1107           ;
1108       else
1109         aha152x_panic("reconnecting target unknown");
1110 
1111 #if defined(DEBUG_QUEUES)
1112       printk("SELID=%02x, target=%d, ", GETPORT(SELID), target );
1113 #endif
1114       SETPORT( SCSIID, (this_host << OID_) | target );
1115       SETPORT( SCSISEQ, ENRESELI );
1116 
1117       if(TESTLO( SSTAT0, SELDI ))
1118         aha152x_panic("RESELI failed");
1119 
1120       SETPORT( SCSISIG, P_MSGI );
1121 
1122       /* Get identify message */
1123       if((i=getphase())!=P_MSGI)
1124         {
1125           printk("target doesn't enter MSGI to identify (phase=%02x)\n", i);
1126           aha152x_panic("unknown lun");
1127         }
1128       SETPORT( SCSISEQ, 0 );
1129 
1130       SETPORT( SXFRCTL0, CH1);
1131 
1132       identify_msg = GETPORT(SCSIBUS);
1133 
1134       if(!(identify_msg & IDENTIFY_BASE))
1135         {
1136           printk("target=%d, inbound message (%02x) != IDENTIFY\n",
1137                  target, identify_msg);
1138           aha152x_panic("unknown lun");
1139         }
1140 
1141       make_acklow();
1142       getphase();
1143 
1144 #if defined(DEBUG_QUEUES)
1145       printk("identify=%02x, lun=%d, ", identify_msg, identify_msg & 0x3f );
1146 #endif
1147 
1148       cli();
1149 #if defined(DEBUG_QUEUES)
1150       printk("d-, ");
1151 #endif
1152       current_SC = remove_SC( &disconnected_SC,
1153                               target,
1154                               identify_msg & 0x3f );
1155 
1156       if(!current_SC)
1157         {
1158           printk("lun=%d, ", identify_msg & 0x3f );
1159           aha152x_panic("no disconnected command for that lun");
1160         }
1161 
1162       current_SC->SCp.phase &= ~disconnected;
1163       sti();
1164 
1165       SETPORT( SIMODE0, 0 );
1166       SETPORT( SIMODE1, ENPHASEMIS );
1167 #if defined(DEBUG_RACE)
1168       leave_driver("(reselected) intr");
1169 #endif
1170       SETBITS( DMACNTRL0, INTEN);
1171       return;
1172     }
1173   
1174   /* Check, if we aren't busy with a command */
1175   if(!current_SC)
1176     {
1177       /* bus is free to issue a queued command */
1178       if(TESTHI( SSTAT1, BUSFREE) && issue_SC)
1179         {
1180           cli();
1181 #if defined(DEBUG_QUEUES)
1182           printk("i-, ");
1183 #endif
1184           current_SC = remove_first_SC( &issue_SC );
1185           sti();
1186 
1187 #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1188           printk("issueing command, ");
1189 #endif
1190           current_SC->SCp.phase = in_selection;
1191 
1192   #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1193           printk("selecting %d, ", current_SC->target); 
1194   #endif
1195           SETPORT( SCSIID, (this_host << OID_) | current_SC->target );
1196 
1197           /* Enable interrupts for SELECTION OUT DONE and SELECTION OUT INITIATED */
1198           SETPORT( SXFRCTL1, ENSPCHK|ENSTIMER);
1199 
1200           /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
1201           SETPORT( SIMODE0, ENSELDO | (disconnected_SC ? ENSELDI : 0) );
1202           SETPORT( SIMODE1, ENSELTIMO );
1203 
1204           /* Enable SELECTION OUT sequence */
1205           SETBITS(SCSISEQ, ENSELO | ENAUTOATNO );
1206         
1207   #if defined(DEBUG_RACE)
1208           leave_driver("(selecting) intr");
1209   #endif
1210           SETBITS( DMACNTRL0, INTEN );
1211           return;
1212         }
1213 
1214       /* No command we are busy with and no new to issue */
1215       printk("aha152x: ignoring spurious interrupt, nothing to do\n");
1216       return;
1217     }
1218 
1219   /* the bus is busy with something */
1220 
1221 #if defined(DEBUG_INTR)
1222   disp_ports();
1223 #endif
1224 
1225   /* we are waiting for the result of a selection attempt */
1226   if(current_SC->SCp.phase & in_selection)
1227     {
1228       if( TESTLO( SSTAT1, SELTO ) )
1229         /* no timeout */
1230         if( TESTHI( SSTAT0, SELDO ) )
1231           {
1232             /* clear BUS FREE interrupt */
1233             SETPORT( SSTAT1, CLRBUSFREE);
1234 
1235             /* Disable SELECTION OUT sequence */
1236             CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO );
1237 
1238             /* Disable SELECTION OUT DONE interrupt */
1239             CLRBITS(SIMODE0, ENSELDO);
1240             CLRBITS(SIMODE1, ENSELTIMO);
1241 
1242             if( TESTLO(SSTAT0, SELDO) )
1243               {
1244                 printk("aha152x: passing bus free condition\n");
1245 
1246 #if defined(DEBUG_RACE)
1247                 leave_driver("(passing bus free) intr");
1248 #endif
1249                 SETBITS( DMACNTRL0, INTEN);
1250 
1251                 if(current_SC->SCp.phase & aborted)
1252                   {
1253                     abort_result=SCSI_ABORT_ERROR;
1254                     wake_up( &abortion_complete );
1255                   }
1256 
1257                 aha152x_done( DID_NO_CONNECT << 16 );
1258                 return;
1259               }
1260 #if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1261             printk("SELDO (SELID=%x), ", GETPORT(SELID));
1262 #endif
1263 
1264             /* selection was done */
1265             SETPORT( SSTAT0, CLRSELDO );
1266 
1267 #if defined(DEBUG_ABORT)
1268             if(current_SC->SCp.phase & aborted)
1269               printk("(ABORT) target selected, ");
1270 #endif
1271 
1272             current_SC->SCp.phase &= ~in_selection;
1273             current_SC->SCp.phase |= in_other;
1274 
1275 #if defined(DEBUG_RACE)
1276             leave_driver("(SELDO) intr");
1277 #endif
1278 
1279             SETPORT( SCSISIG, P_MSGO );
1280 
1281             SETPORT( SIMODE0, 0 );
1282             SETPORT( SIMODE1, ENREQINIT );
1283             SETBITS( DMACNTRL0, INTEN);
1284             return;
1285           }
1286         else
1287           aha152x_panic("neither timeout nor selection\007");
1288       else
1289         {
1290 #if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
1291           printk("SELTO, ");
1292 #endif
1293           /* end selection attempt */
1294           CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO );
1295 
1296           /* timeout */
1297           SETPORT( SSTAT1, CLRSELTIMO );
1298 
1299           SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1300           SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
1301           SETBITS( DMACNTRL0, INTEN );
1302 #if defined(DEBUG_RACE)
1303           leave_driver("(SELTO) intr");
1304 #endif
1305 
1306           if(current_SC->SCp.phase & aborted)
1307             {
1308 #if defined(DEBUG_ABORT)
1309               printk("(ABORT) selection timeout, ");
1310 #endif
1311               abort_result=SCSI_ABORT_ERROR;
1312               wake_up( &abortion_complete );
1313             }
1314 
1315           if( TESTLO( SSTAT0, SELINGO ) )
1316             /* ARBITRATION not won */
1317             aha152x_done( DID_BUS_BUSY << 16 );
1318           else
1319             /* ARBITRATION won, but SELECTION failed */
1320             aha152x_done( DID_NO_CONNECT << 16 );
1321           return;
1322         }
1323     }
1324 
1325   /* enable interrupt, when target leaves current phase */
1326   phase = getphase();
1327   if(!(phase & ~P_MASK))                                      /* "real" phase */
1328     SETPORT(SCSISIG, phase);
1329   SETPORT(SSTAT1, CLRPHASECHG);
1330   current_SC->SCp.phase =
1331     (current_SC->SCp.phase & ~((P_MASK|1)<<16)) | (phase << 16 );
1332 
1333   /* information transfer phase */
1334   switch( phase )
1335     {
1336     case P_MSGO:                                               /* MESSAGE OUT */
1337       {
1338         unsigned char message;
1339 
1340 #if defined(DEBUG_INTR) || defined(DEBUG_MSGO) || defined(DEBUG_PHASES)
1341         printk("MESSAGE OUT, ");
1342 #endif
1343 
1344         if( current_SC->SCp.phase & aborted )
1345           {
1346 #if defined(DEBUG_MSGO) || defined(DEBUG_ABORT)
1347             printk("ABORT, ");
1348 #endif
1349             message=ABORT;
1350           }
1351         else
1352           /* If we didn't identify yet, do it. Otherwise there's nothing to do,
1353              but reject (probably we got an message before, that we have to
1354              reject (SDTR, WDTR, etc.) */
1355           if( !(current_SC->SCp.phase & sent_ident))
1356             {
1357               message=IDENTIFY(can_disconnect,current_SC->lun);
1358 #if defined(DEBUG_MSGO)
1359               printk("IDENTIFY (reconnect=%s;lun=%d), ", 
1360                       can_disconnect ? "enabled" : "disabled", current_SC->lun);
1361 #endif
1362             }
1363           else
1364             {
1365               message=MESSAGE_REJECT;
1366 #if defined(DEBUG_MSGO)
1367               printk("REJECT, ");
1368 #endif
1369             }
1370           
1371         CLRBITS( SXFRCTL0, ENDMA);
1372 
1373         SETPORT( SIMODE0, 0 );
1374         SETPORT( SIMODE1, ENPHASEMIS|ENREQINIT );
1375 
1376         /* wait for data latch to become ready or a phase change */
1377         while( TESTLO( DMASTAT, INTSTAT ) )
1378           ;
1379 
1380         if( TESTHI( SSTAT1, PHASEMIS ) )
1381           aha152x_panic("unable to send message");
1382 
1383         /* Leave MESSAGE OUT after transfer */
1384         SETPORT( SSTAT1, CLRATNO);
1385 
1386         SETPORT( SCSIDAT, message );
1387 
1388         make_acklow();
1389         getphase();
1390 
1391         if(message==IDENTIFY(can_disconnect,current_SC->lun))
1392           current_SC->SCp.phase |= sent_ident;
1393 
1394         if(message==ABORT)
1395           {
1396             /* revive abort(); abort() enables interrupts */
1397             abort_result=SCSI_ABORT_SUCCESS;
1398             wake_up( &abortion_complete );
1399 
1400             current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));
1401 
1402             /* exit */
1403             SETBITS( DMACNTRL0, INTEN );
1404 #if defined(DEBUG_RACE)
1405             leave_driver("(ABORT) intr");
1406 #endif
1407             aha152x_done(DID_ABORT<<16);
1408             return;
1409           }
1410       }
1411       break;
1412 
1413     case P_CMD:                                          /* COMMAND phase */
1414 #if defined(DEBUG_INTR) || defined(DEBUG_CMD) || defined(DEBUG_PHASES)
1415       printk("COMMAND, ");
1416 #endif
1417       if( !(current_SC->SCp.sent_command) )
1418         {
1419           if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
1420             printk("aha152x: P_CMD: %d(%d) bytes left in FIFO, resetting\n",
1421                    GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
1422 
1423           /* reset fifo and enable writes */
1424           SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
1425           SETPORT(DMACNTRL0, ENDMA|WRITE_READ);
1426 
1427           /* clear transfer count and scsi fifo */
1428           SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1 );
1429           SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
1430   
1431           /* missing phase raises INTSTAT */
1432           SETPORT( SIMODE0, 0 );
1433           SETPORT( SIMODE1, ENPHASEMIS );
1434   
1435 #if defined(DEBUG_CMD)
1436           printk("waiting, ");
1437 #endif
1438           /* wait for FIFO to get empty */
1439           while( TESTLO ( DMASTAT, DFIFOEMP|INTSTAT ) )
1440             ;
1441   
1442           if( TESTHI( SSTAT1, PHASEMIS ) )
1443             aha152x_panic("target left COMMAND phase");
1444 
1445 #if defined(DEBUG_CMD)
1446           printk("DFIFOEMP, outsw (%d words), ",
1447                  COMMAND_SIZE(current_SC->cmnd[0])>>1);
1448           disp_ports();
1449 #endif
1450   
1451           outsw( DATAPORT,
1452                  &current_SC->cmnd,
1453                  COMMAND_SIZE(current_SC->cmnd[0])>>1 );
1454 
1455 #if defined(DEBUG_CMD)
1456           printk("FCNT=%d, STCNT=%d, ", GETPORT(FIFOSTAT), GETSTCNT() );
1457           disp_ports();
1458 #endif
1459 
1460           /* wait for SCSI FIFO to get empty.
1461              very important to send complete commands. */
1462           while( TESTLO ( SSTAT2, SEMPTY ) )
1463             ;
1464 
1465           CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
1466           /* transfer can be considered ended, when SCSIEN reads back zero */
1467           while( TESTHI( SXFRCTL0, SCSIEN ) )
1468             ;
1469 
1470           CLRBITS(DMACNTRL0, ENDMA);
1471 
1472 #if defined(DEBUG_CMD) || defined(DEBUG_INTR)
1473           printk("sent %d/%d command bytes, ", GETSTCNT(),
1474                  COMMAND_SIZE(current_SC->cmnd[0]));
1475 #endif
1476 
1477         }
1478       else
1479         aha152x_panic("Nothing to sent while in COMMAND OUT");
1480       break;
1481 
1482     case P_MSGI:                                          /* MESSAGE IN phase */
1483 #if defined(DEBUG_INTR) || defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1484       printk("MESSAGE IN, ");
1485 #endif
1486       SETPORT( SXFRCTL0, CH1);
1487 
1488       SETPORT( SIMODE0, 0);
1489       SETPORT( SIMODE1, ENBUSFREE);
1490   
1491       while( phase == P_MSGI ) 
1492         {
1493           current_SC->SCp.Message = GETPORT( SCSIBUS );
1494           switch(current_SC->SCp.Message)
1495             {
1496             case DISCONNECT:
1497 #if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1498               printk("target disconnected, ");
1499 #endif
1500               current_SC->SCp.Message = 0;
1501               current_SC->SCp.phase   |= disconnected;
1502               if(!can_disconnect)
1503                 aha152x_panic("target was not allowed to disconnect");
1504               break;
1505         
1506             case COMMAND_COMPLETE:
1507 #if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
1508               printk("inbound message ( COMMAND COMPLETE ), ");
1509 #endif
1510               done++;
1511               break;
1512 
1513             case MESSAGE_REJECT:
1514 #if defined(DEBUG_MSGI) || defined(DEBUG_TIMING)
1515               printk("inbound message ( MESSAGE REJECT ), ");
1516 #endif
1517               break;
1518 
1519             case SAVE_POINTERS:
1520 #if defined(DEBUG_MSGI)
1521               printk("inbound message ( SAVE DATA POINTERS ), ");
1522 #endif
1523               break;
1524 
1525             case EXTENDED_MESSAGE:
1526               { 
1527                 int           i, code;
1528 
1529 #if defined(DEBUG_MSGI)
1530                 printk("inbound message ( EXTENDED MESSAGE ), ");
1531 #endif
1532                 make_acklow();
1533                 if(getphase()!=P_MSGI)
1534                   break;
1535   
1536                 i=GETPORT(SCSIBUS);
1537 
1538 #if defined(DEBUG_MSGI)
1539                 printk("length (%d), ", i);
1540 #endif
1541 
1542 #if defined(DEBUG_MSGI)
1543                 printk("code ( ");
1544 #endif
1545 
1546                 make_acklow();
1547                 if(getphase()!=P_MSGI)
1548                   break;
1549 
1550                 code = GETPORT(SCSIBUS);
1551 
1552                 switch( code )
1553                   {
1554                   case 0x00:
1555 #if defined(DEBUG_MSGI)
1556                     printk("MODIFY DATA POINTER ");
1557 #endif
1558                     SETPORT(SCSISIG, P_MSGI|ATNO);
1559                     break;
1560                   case 0x01:
1561 #if defined(DEBUG_MSGI)
1562                     printk("SYNCHRONOUS DATA TRANSFER REQUEST ");
1563 #endif
1564                     SETPORT(SCSISIG, P_MSGI|ATNO);
1565                     break;
1566                   case 0x02:
1567 #if defined(DEBUG_MSGI)
1568                     printk("EXTENDED IDENTIFY ");
1569 #endif
1570                     break;
1571                   case 0x03:
1572 #if defined(DEBUG_MSGI)
1573                     printk("WIDE DATA TRANSFER REQUEST ");
1574 #endif
1575                     SETPORT(SCSISIG, P_MSGI|ATNO);
1576                     break;
1577                   default:
1578 #if defined(DEBUG_MSGI)
1579                     if( code & 0x80 )
1580                       printk("reserved (%d) ", code );
1581                     else
1582                       printk("vendor specific (%d) ", code);
1583 #endif
1584                     SETPORT(SCSISIG, P_MSGI|ATNO);
1585                     break;
1586                   }
1587 #if defined(DEBUG_MSGI)
1588                 printk(" ), data ( ");
1589 #endif
1590                 while( --i && (make_acklow(), getphase()==P_MSGI))
1591                   {
1592 #if defined(DEBUG_MSGI)
1593                     printk("%x ", GETPORT(SCSIBUS) );
1594 #else
1595                     GETPORT(SCSIBUS);
1596 #endif
1597                   }
1598 #if defined(DEBUG_MSGI)
1599                 printk(" ), ");
1600 #endif
1601                 /* We reject all extended messages. To do this
1602                    we just enter MSGO by asserting ATN. Since
1603                    we have already identified a REJECT message
1604                    will be sent. */
1605                 SETPORT(SCSISIG, P_MSGI|ATNO);
1606               }
1607               break;
1608        
1609             default:
1610               printk("unsupported inbound message %x, ", current_SC->SCp.Message);
1611               break;
1612 
1613             }
1614 
1615           make_acklow();
1616           phase=getphase();
1617         } 
1618 
1619       /* clear SCSI fifo on BUSFREE */
1620       if(phase==P_BUSFREE)
1621         SETPORT(SXFRCTL0, CH1|CLRCH1);
1622 
1623       if(current_SC->SCp.phase & disconnected)
1624         {
1625           cli();
1626 #if defined(DEBUG_QUEUES)
1627           printk("d+, ");
1628 #endif
1629           append_SC( &disconnected_SC, current_SC);
1630           current_SC = NULL;
1631           sti();
1632 
1633           SETBITS( SCSISEQ, ENRESELI );
1634 
1635           SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
1636           SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
1637 
1638           SETBITS( DMACNTRL0, INTEN );
1639           return;
1640         }
1641       break;
1642 
1643     case P_STATUS:                                         /* STATUS IN phase */
1644 #if defined(DEBUG_STATUS) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1645       printk("STATUS, ");
1646 #endif
1647       SETPORT( SXFRCTL0, CH1);
1648 
1649       SETPORT( SIMODE0, 0 );
1650       SETPORT( SIMODE1, ENREQINIT );
1651 
1652       if( TESTHI( SSTAT1, PHASEMIS ) )
1653         printk("aha152x: passing STATUS phase");
1654         
1655       current_SC->SCp.Status = GETPORT( SCSIBUS );
1656       make_acklow();
1657       getphase();
1658 
1659 #if defined(DEBUG_STATUS)
1660       printk("inbound status ");
1661       print_status( current_SC->SCp.Status );
1662       printk(", ");
1663 #endif
1664       break;
1665 
1666     case P_DATAI:                                            /* DATA IN phase */
1667       {
1668         int fifodata, data_count, done;
1669 
1670 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1671         printk("DATA IN, ");
1672 #endif
1673 
1674         if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
1675           printk("aha152x: P_DATAI: %d(%d) bytes left in FIFO, resetting\n",
1676                  GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));
1677 
1678         /* reset host fifo */
1679         SETPORT(DMACNTRL0, RSTFIFO);
1680         SETPORT(DMACNTRL0, RSTFIFO|ENDMA);
1681 
1682         SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN );
1683 
1684         SETPORT( SIMODE0, 0 );
1685         SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );
1686 
1687         /* done is set when the FIFO is empty after the target left DATA IN */
1688         done=0;
1689       
1690         /* while the target stays in DATA to transfer data */
1691         while ( !done ) 
1692           {
1693 #if defined(DEBUG_DATAI)
1694             printk("expecting data, ");
1695 #endif
1696             /* wait for PHASEMIS or full FIFO */
1697             while( TESTLO ( DMASTAT, DFIFOFULL|INTSTAT ) )
1698               ;
1699 
1700             if( TESTHI( DMASTAT, DFIFOFULL ) )
1701               fifodata=GETPORT(FIFOSTAT);
1702             else
1703               {
1704                 /* wait for SCSI fifo to get empty */
1705                 while( TESTLO( SSTAT2, SEMPTY ) )
1706                   ;
1707 
1708                 /* rest of data in FIFO */
1709                 fifodata=GETPORT(FIFOSTAT);
1710 #if defined(DEBUG_DATAI)
1711                 printk("last transfer, ");
1712 #endif
1713                 done=1;
1714               }
1715   
1716 #if defined(DEBUG_DATAI)
1717             printk("fifodata=%d, ", fifodata);
1718 #endif
1719 
1720             while( fifodata && current_SC->SCp.this_residual )
1721               {
1722                 data_count=fifodata;
1723   
1724                 /* limit data transfer to size of first sg buffer */
1725                 if (data_count > current_SC->SCp.this_residual)
1726                   data_count = current_SC->SCp.this_residual;
1727   
1728                 fifodata -= data_count;
1729 
1730 #if defined(DEBUG_DATAI)
1731                 printk("data_count=%d, ", data_count);
1732 #endif
1733   
1734                 if(data_count == 1)
1735                   {
1736                     /* get a single byte in byte mode */
1737                     SETBITS(DMACNTRL0, _8BIT );
1738                     *current_SC->SCp.ptr++ = GETPORT( DATAPORT );
1739                     current_SC->SCp.this_residual--;
1740                   }
1741                 else
1742                   {
1743                     CLRBITS(DMACNTRL0, _8BIT );
1744                     data_count >>= 1; /* Number of words */
1745                     insw( DATAPORT, current_SC->SCp.ptr, data_count );
1746 #if defined(DEBUG_DATAI)
1747 /* show what comes with the last transfer */
1748                     if(done)
1749                       {
1750                         int           i;
1751                         unsigned char *data;
1752 
1753                         printk("data on last transfer (%d bytes: ",
1754                                2*data_count);
1755                         data = (unsigned char *) current_SC->SCp.ptr;
1756                         for( i=0; i<2*data_count; i++)
1757                           printk("%2x ", *data++);
1758                         printk("), ");
1759                       }
1760 #endif
1761                     current_SC->SCp.ptr           += 2 * data_count;
1762                     current_SC->SCp.this_residual -= 2 * data_count;
1763                   }
1764               
1765                 /* if this buffer is full and there are more buffers left */
1766                 if (!current_SC->SCp.this_residual &&
1767                      current_SC->SCp.buffers_residual)
1768                   {
1769                     /* advance to next buffer */
1770                     current_SC->SCp.buffers_residual--;
1771                     current_SC->SCp.buffer++;
1772                     current_SC->SCp.ptr =
1773                       current_SC->SCp.buffer->address;
1774                     current_SC->SCp.this_residual =
1775                       current_SC->SCp.buffer->length;
1776                   } 
1777               }
1778  
1779             /*
1780              * Fifo should be empty
1781              */
1782             if(fifodata>0)
1783               {
1784                 printk("aha152x: more data than expected (%d bytes)\n",
1785                        GETPORT(FIFOSTAT));
1786                 SETBITS(DMACNTRL0, _8BIT );
1787                 printk("aha152x: data ( ");
1788                 while(fifodata--)
1789                   printk("%2x ", GETPORT( DATAPORT ));
1790                 printk(")\n");
1791               }
1792 
1793 #if defined(DEBUG_DATAI)
1794             if(!fifodata)
1795               printk("fifo empty, ");
1796             else
1797               printk("something left in fifo, ");
1798 #endif
1799           }
1800 
1801 #if defined(DEBUG_DATAI)
1802         if(current_SC->SCp.buffers_residual || current_SC->SCp.this_residual)
1803           printk("left buffers (buffers=%d, bytes=%d), ",
1804                  current_SC->SCp.buffers_residual, 
1805                  current_SC->SCp.this_residual);
1806 #endif
1807         /* transfer can be considered ended, when SCSIEN reads back zero */
1808         CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
1809         while( TESTHI( SXFRCTL0, SCSIEN ) )
1810           ;
1811         CLRBITS(DMACNTRL0, ENDMA );
1812 
1813 #if defined(DEBUG_DATAI) || defined(DEBUG_INTR)
1814         printk("got %d bytes, ", GETSTCNT());
1815 #endif
1816 
1817         current_SC->SCp.have_data_in++;
1818       }
1819       break;
1820 
1821     case P_DATAO:                                           /* DATA OUT phase */
1822       {
1823         int data_count;
1824 
1825 #if defined(DEBUG_DATAO) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
1826         printk("DATA OUT, ");
1827 #endif
1828 #if defined(DEBUG_DATAO)
1829         printk("got data to send (bytes=%d, buffers=%d), ",
1830                current_SC->SCp.this_residual,
1831                current_SC->SCp.buffers_residual );
1832 #endif
1833 
1834         if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT) )
1835           {
1836             printk("%d(%d) left in FIFO, ", GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT) );
1837             aha152x_panic("FIFO should be empty");
1838           }
1839 
1840         SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
1841         SETPORT(DMACNTRL0, ENDMA|WRITE_READ);
1842 
1843         SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1 );
1844         SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
1845  
1846         SETPORT( SIMODE0, 0 );
1847         SETPORT( SIMODE1, ENPHASEMIS );
1848 
1849         /* while current buffer is not empty or
1850            there are more buffers to transfer */
1851         while( TESTLO( SSTAT1, PHASEMIS ) &&
1852                  (current_SC->SCp.this_residual ||
1853                   current_SC->SCp.buffers_residual) )
1854           {
1855 #if defined(DEBUG_DATAO)
1856             printk("sending data (left: bytes=%d, buffers=%d), waiting, ",
1857                     current_SC->SCp.this_residual,
1858                     current_SC->SCp.buffers_residual);
1859 #endif
1860             /* transfer rest of buffer, but max. 128 byte */
1861             data_count = current_SC->SCp.this_residual > 128 ?
1862                          128 : current_SC->SCp.this_residual ;
1863 
1864 #if defined(DEBUG_DATAO)
1865             printk("data_count=%d, ", data_count);
1866 #endif
1867   
1868             if(data_count == 1)
1869               {
1870                 /* put a single byte in byte mode */
1871                 SETBITS(DMACNTRL0, _8BIT );
1872                 SETPORT(DATAPORT, *current_SC->SCp.ptr++);
1873                 current_SC->SCp.this_residual--;
1874               }
1875             else
1876               {
1877                 CLRBITS(DMACNTRL0, _8BIT );
1878                 data_count >>= 1; /* Number of words */
1879                 outsw( DATAPORT, current_SC->SCp.ptr, data_count );
1880                 current_SC->SCp.ptr           += 2 * data_count;
1881                 current_SC->SCp.this_residual -= 2 * data_count;
1882               }
1883 
1884             /* wait for FIFO to get empty */
1885             while( TESTLO ( DMASTAT, DFIFOEMP|INTSTAT ) )
1886               ;
1887 
1888 #if defined(DEBUG_DATAO)
1889             printk("fifo (%d bytes), transfered (%d bytes), ",
1890                    GETPORT(FIFOSTAT), GETSTCNT() );
1891 #endif
1892 
1893             /* if this buffer is empty and there are more buffers left */
1894             if ( TESTLO( SSTAT1, PHASEMIS ) &&
1895                  !current_SC->SCp.this_residual &&
1896                   current_SC->SCp.buffers_residual)
1897               {
1898                  /* advance to next buffer */
1899                  current_SC->SCp.buffers_residual--;
1900                  current_SC->SCp.buffer++;
1901                  current_SC->SCp.ptr =
1902                    current_SC->SCp.buffer->address;
1903                  current_SC->SCp.this_residual =
1904                  current_SC->SCp.buffer->length;
1905               }
1906           }
1907 
1908         if ( current_SC->SCp.this_residual ||
1909              current_SC->SCp.buffers_residual )
1910           {
1911             /* target leaves DATA OUT for an other phase
1912                (perhaps disconnect) */
1913 
1914             /* data in fifos has to be resend */
1915             data_count = GETPORT(SSTAT2) & (SFULL|SFCNT);
1916 
1917             data_count += GETPORT(FIFOSTAT) ;
1918             current_SC->SCp.ptr           -= data_count;
1919             current_SC->SCp.this_residual += data_count;
1920 #if defined(DEBUG_DATAO)
1921             printk("left data (bytes=%d, buffers=%d), fifos (bytes=%d), transfer incomplete, resetting fifo, ",
1922                    current_SC->SCp.this_residual,
1923                    current_SC->SCp.buffers_residual,
1924                    data_count );
1925 #endif
1926             SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
1927             CLRBITS(SXFRCTL0, SCSIEN|DMAEN );
1928             CLRBITS(DMACNTRL0, ENDMA);
1929           }
1930         else
1931           {
1932 #if defined(DEBUG_DATAO)
1933             printk("waiting for SCSI fifo to get empty, ");
1934 #endif
1935             /* wait for SCSI fifo to get empty */
1936             while( TESTLO( SSTAT2, SEMPTY ) )
1937               ;
1938 #if defined(DEBUG_DATAO)
1939             printk("ok, ");
1940 #endif
1941 
1942 #if defined(DEBUG_DATAO)
1943             printk("left data (bytes=%d, buffers=%d) ",
1944                    current_SC->SCp.this_residual,
1945                    current_SC->SCp.buffers_residual);
1946 #endif
1947             CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
1948 
1949             /* transfer can be considered ended, when SCSIEN reads back zero */
1950             while( TESTHI( SXFRCTL0, SCSIEN ) )
1951               ;
1952 
1953             CLRBITS(DMACNTRL0, ENDMA);
1954           }
1955 
1956 #if defined(DEBUG_DATAO) || defined(DEBUG_INTR)
1957         printk("sent %d data bytes, ", GETSTCNT() );
1958 #endif
1959       }
1960       break;
1961 
1962     case P_BUSFREE:                                                /* BUSFREE */
1963 #if defined(DEBUG_RACE)
1964       leave_driver("(BUSFREE) intr");
1965 #endif
1966 #if defined(DEBUG_PHASES)
1967       printk("unexpected BUS FREE, ");
1968 #endif
1969       current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));
1970 
1971       aha152x_done( DID_ERROR << 16 );               /* Don't know any better */
1972       return;
1973       break;
1974 
1975     case P_PARITY:                              /* parity error in DATA phase */
1976 #if defined(DEBUG_RACE)
1977       leave_driver("(DID_PARITY) intr");
1978 #endif
1979       printk("PARITY error in DATA phase, ");
1980 
1981       current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));
1982 
1983       SETBITS( DMACNTRL0, INTEN );
1984       aha152x_done( DID_PARITY << 16 );
1985       return;
1986       break;
1987 
1988     default:
1989       printk("aha152x: unexpected phase\n");
1990       break;
1991     }
1992 
1993   if(done)
1994     {
1995 #if defined(DEBUG_INTR)
1996       printk("command done.\n");
1997 #endif
1998 #if defined(DEBUG_RACE)
1999       leave_driver("(done) intr");
2000 #endif
2001 
2002       SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
2003       SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
2004       SETPORT( SCSISEQ, disconnected_SC ? ENRESELI : 0 );
2005 
2006       SETBITS( DMACNTRL0, INTEN );
2007 
2008       aha152x_done(   (current_SC->SCp.Status  & 0xff)
2009                     | ( (current_SC->SCp.Message & 0xff) << 8)
2010                     | ( DID_OK << 16) );
2011 
2012 #if defined(DEBUG_RACE)
2013       printk("done returned (DID_OK: Status=%x; Message=%x).\n",
2014              current_SC->SCp.Status, current_SC->SCp.Message);
2015 #endif
2016       return;
2017     }
2018 
2019   if(current_SC)
2020     current_SC->SCp.phase |= 1<<16 ;
2021 
2022   SETPORT( SIMODE0, 0 );
2023   SETPORT( SIMODE1, ENPHASEMIS );
2024 #if defined(DEBUG_INTR)
2025   disp_enintr();
2026 #endif
2027 #if defined(DEBUG_RACE)
2028   leave_driver("(PHASEEND) intr");
2029 #endif
2030 
2031   SETBITS( DMACNTRL0, INTEN);
2032   return;
2033 }
2034 
2035 /* 
2036  * Dump the current driver status and panic...
2037  */
2038 static void aha152x_panic(char *msg)
     /* [previous][next][first][last][top][bottom][index][help] */
2039 {
2040   printk("\naha152x_panic: %s\n", msg);
2041   show_queues();
2042   panic("aha152x panic");
2043 }
2044 
2045 /*
2046  * Display registers of AIC-6260
2047  */
2048 static void disp_ports(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2049 {
2050 #if !defined(SKIP_PORTS)
2051   int s;
2052 
2053   printk("\n%s: ", current_SC ? "on bus" : "waiting");
2054 
2055   s=GETPORT(SCSISEQ);
2056   printk("SCSISEQ ( ");
2057   if( s & TEMODEO )     printk("TARGET MODE ");
2058   if( s & ENSELO )      printk("SELO ");
2059   if( s & ENSELI )      printk("SELI ");
2060   if( s & ENRESELI )    printk("RESELI ");
2061   if( s & ENAUTOATNO )  printk("AUTOATNO ");
2062   if( s & ENAUTOATNI )  printk("AUTOATNI ");
2063   if( s & ENAUTOATNP )  printk("AUTOATNP ");
2064   if( s & SCSIRSTO )    printk("SCSIRSTO ");
2065   printk(");");
2066 
2067   printk(" SCSISIG ( ");
2068   s=GETPORT(SCSISIG);
2069   switch(s & P_MASK)
2070     {
2071     case P_DATAO:
2072       printk("DATA OUT");
2073       break;
2074     case P_DATAI:
2075       printk("DATA IN");
2076       break;
2077     case P_CMD:
2078       printk("COMMAND"); 
2079       break;
2080     case P_STATUS:
2081       printk("STATUS"); 
2082       break;
2083     case P_MSGO:
2084       printk("MESSAGE OUT");
2085       break;
2086     case P_MSGI:
2087       printk("MESSAGE IN");
2088       break;
2089     default:
2090       printk("*illegal*");
2091       break;
2092     }
2093   
2094   printk(" ); ");
2095 
2096   printk("INTSTAT ( %s ); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");
2097 
2098   printk("SSTAT ( ");
2099   s=GETPORT(SSTAT0);
2100   if( s & TARGET )   printk("TARGET ");
2101   if( s & SELDO )    printk("SELDO ");
2102   if( s & SELDI )    printk("SELDI ");
2103   if( s & SELINGO )  printk("SELINGO ");
2104   if( s & SWRAP )    printk("SWRAP ");
2105   if( s & SDONE )    printk("SDONE ");
2106   if( s & SPIORDY )  printk("SPIORDY ");
2107   if( s & DMADONE )  printk("DMADONE ");
2108 
2109   s=GETPORT(SSTAT1);
2110   if( s & SELTO )     printk("SELTO ");
2111   if( s & ATNTARG )   printk("ATNTARG ");
2112   if( s & SCSIRSTI )  printk("SCSIRSTI ");
2113   if( s & PHASEMIS )  printk("PHASEMIS ");
2114   if( s & BUSFREE )   printk("BUSFREE ");
2115   if( s & SCSIPERR )  printk("SCSIPERR ");
2116   if( s & PHASECHG )  printk("PHASECHG ");
2117   if( s & REQINIT )   printk("REQINIT ");
2118   printk("); ");
2119 
2120 
2121   printk("SSTAT ( ");
2122 
2123   s=GETPORT(SSTAT0) & GETPORT(SIMODE0);
2124 
2125   if( s & TARGET )    printk("TARGET ");
2126   if( s & SELDO )     printk("SELDO ");
2127   if( s & SELDI )     printk("SELDI ");
2128   if( s & SELINGO )   printk("SELINGO ");
2129   if( s & SWRAP )     printk("SWRAP ");
2130   if( s & SDONE )     printk("SDONE ");
2131   if( s & SPIORDY )   printk("SPIORDY ");
2132   if( s & DMADONE )   printk("DMADONE ");
2133 
2134   s=GETPORT(SSTAT1) & GETPORT(SIMODE1);
2135 
2136   if( s & SELTO )     printk("SELTO ");
2137   if( s & ATNTARG )   printk("ATNTARG ");
2138   if( s & SCSIRSTI )  printk("SCSIRSTI ");
2139   if( s & PHASEMIS )  printk("PHASEMIS ");
2140   if( s & BUSFREE )   printk("BUSFREE ");
2141   if( s & SCSIPERR )  printk("SCSIPERR ");
2142   if( s & PHASECHG )  printk("PHASECHG ");
2143   if( s & REQINIT )   printk("REQINIT ");
2144   printk("); ");
2145 
2146   printk("SXFRCTL0 ( ");
2147 
2148   s=GETPORT(SXFRCTL0);
2149   if( s & SCSIEN )    printk("SCSIEN ");
2150   if( s & DMAEN )     printk("DMAEN ");
2151   if( s & CH1 )       printk("CH1 ");
2152   if( s & CLRSTCNT )  printk("CLRSTCNT ");
2153   if( s & SPIOEN )    printk("SPIOEN ");
2154   if( s & CLRCH1 )    printk("CLRCH1 ");
2155   printk("); ");
2156 
2157   printk("SIGNAL ( ");
2158 
2159   s=GETPORT(SCSISIG);
2160   if( s & ATNI )  printk("ATNI ");
2161   if( s & SELI )  printk("SELI ");
2162   if( s & BSYI )  printk("BSYI ");
2163   if( s & REQI )  printk("REQI ");
2164   if( s & ACKI )  printk("ACKI ");
2165   printk("); ");
2166 
2167   printk("SELID ( %02x ), ", GETPORT(SELID) );
2168 
2169   printk("SSTAT2 ( ");
2170 
2171   s=GETPORT(SSTAT2);
2172   if( s & SOFFSET)  printk("SOFFSET ");
2173   if( s & SEMPTY)   printk("SEMPTY ");
2174   if( s & SFULL)    printk("SFULL ");
2175   printk("); SFCNT ( %d ); ", s & (SFULL|SFCNT) );
2176 
2177 #if 0
2178   printk("SSTAT4 ( ");
2179   s=GETPORT(SSTAT4);
2180   if( s & SYNCERR)   printk("SYNCERR ");
2181   if( s & FWERR)     printk("FWERR ");
2182   if( s & FRERR)     printk("FRERR ");
2183   printk("); ");
2184 #endif
2185 
2186   printk("FCNT ( %d ); ", GETPORT(FIFOSTAT) );
2187 
2188   printk("DMACNTRL0 ( ");
2189   s=GETPORT(DMACNTRL0);
2190   printk( "%s ", s & _8BIT      ? "8BIT"  : "16BIT" );
2191   printk( "%s ", s & DMA        ? "DMA"   : "PIO"   );
2192   printk( "%s ", s & WRITE_READ ? "WRITE" : "READ"  );
2193   if( s & ENDMA )    printk("ENDMA ");
2194   if( s & INTEN )    printk("INTEN ");
2195   if( s & RSTFIFO )  printk("RSTFIFO ");
2196   if( s & SWINT )    printk("SWINT ");
2197   printk("); ");
2198 
2199 
2200 #if 0
2201   printk("DMACNTRL1 ( ");
2202 
2203   s=GETPORT(DMACNTRL1);
2204   if( s & PWRDWN )    printk("PWRDN ");
2205   printk("); ");
2206 
2207 
2208   printk("STK ( %d ); ", s & 0xf);
2209 
2210   printk("DMASTAT (");
2211   s=GETPORT(DMASTAT);
2212   if( s & ATDONE )     printk("ATDONE ");
2213   if( s & WORDRDY )    printk("WORDRDY ");
2214   if( s & DFIFOFULL )  printk("DFIFOFULL ");
2215   if( s & DFIFOEMP )   printk("DFIFOEMP ");
2216   printk(")");
2217 
2218 #endif
2219 
2220   printk("\n");
2221 #endif
2222 }
2223 
2224 /*
2225  * display enabled interrupts
2226  */
2227 static void disp_enintr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2228 {
2229   int s;
2230 
2231   printk("enabled interrupts ( ");
2232   
2233   s=GETPORT(SIMODE0);
2234   if( s & ENSELDO )    printk("ENSELDO ");
2235   if( s & ENSELDI )    printk("ENSELDI ");
2236   if( s & ENSELINGO )  printk("ENSELINGO ");
2237   if( s & ENSWRAP )    printk("ENSWRAP ");
2238   if( s & ENSDONE )    printk("ENSDONE ");
2239   if( s & ENSPIORDY )  printk("ENSPIORDY ");
2240   if( s & ENDMADONE )  printk("ENDMADONE ");
2241 
2242   s=GETPORT(SIMODE1);
2243   if( s & ENSELTIMO )    printk("ENSELTIMO ");
2244   if( s & ENATNTARG )    printk("ENATNTARG ");
2245   if( s & ENPHASEMIS )   printk("ENPHASEMIS ");
2246   if( s & ENBUSFREE )    printk("ENBUSFREE ");
2247   if( s & ENSCSIPERR )   printk("ENSCSIPERR ");
2248   if( s & ENPHASECHG )   printk("ENPHASECHG ");
2249   if( s & ENREQINIT )    printk("ENREQINIT ");
2250   printk(")\n");
2251 }
2252 
2253 #if defined(DEBUG_RACE)
2254 
2255 static const char *should_leave;
2256 static int in_driver=0;
2257 
2258 /*
2259  * Only one routine can be in the driver at once.
2260  */
2261 static void enter_driver(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
2262 {
2263   cli();
2264   printk("aha152x: entering %s() (%x)\n", func, jiffies);
2265   if(in_driver)
2266     {
2267       printk("%s should leave first.\n", should_leave);
2268       panic("aha152x: already in driver\n");
2269     }
2270 
2271   in_driver++;
2272   should_leave=func;
2273   sti();
2274 }
2275 
2276 static void leave_driver(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
2277 {
2278   cli();
2279   printk("\naha152x: leaving %s() (%x)\n", func, jiffies);
2280   if(!in_driver)
2281     {
2282       printk("aha152x: %s already left.\n", should_leave);
2283       panic("aha152x: %s already left driver.\n");
2284     }
2285 
2286   in_driver--;
2287   should_leave=func;
2288   sti();
2289 }
2290 #endif
2291 
2292 /*
2293  * Show the command data of a command
2294  */
2295 static void show_command(Scsi_Cmnd *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2296 {
2297   int i;
2298 
2299   printk("0x%08x: target=%d; lun=%d; cmnd=( ",
2300          (unsigned int) ptr, ptr->target, ptr->lun);
2301   
2302   for(i=0; i<COMMAND_SIZE(ptr->cmnd[0]); i++)
2303     printk("%02x ", ptr->cmnd[i]);
2304 
2305   printk("); residual=%d; buffers=%d; phase |",
2306          ptr->SCp.this_residual, ptr->SCp.buffers_residual);
2307 
2308   if( ptr->SCp.phase & not_issued   )  printk("not issued|");
2309   if( ptr->SCp.phase & in_selection )  printk("in selection|");
2310   if( ptr->SCp.phase & disconnected )  printk("disconnected|");
2311   if( ptr->SCp.phase & aborted      )  printk("aborted|");
2312   if( ptr->SCp.phase & sent_ident   )  printk("send_ident|");
2313   if( ptr->SCp.phase & in_other )
2314     { 
2315       printk("; in other(");
2316       switch( (ptr->SCp.phase >> 16) & P_MASK )
2317         {
2318         case P_DATAO:
2319           printk("DATA OUT");
2320           break;
2321         case P_DATAI:
2322           printk("DATA IN");
2323           break;
2324         case P_CMD:
2325           printk("COMMAND");
2326           break;
2327         case P_STATUS:
2328           printk("STATUS");
2329           break;
2330         case P_MSGO:
2331           printk("MESSAGE OUT");
2332           break;
2333         case P_MSGI:
2334           printk("MESSAGE IN");
2335           break;
2336         default: 
2337           printk("*illegal*");
2338           break;
2339         }
2340       printk(")");
2341       if(ptr->SCp.phase & (1<<16))
2342         printk("; phaseend");
2343     }
2344   printk("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
2345 }
2346  
2347 /*
2348  * Dump the queued data
2349  */
2350 static void show_queues(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2351 {
2352   Scsi_Cmnd *ptr;
2353 
2354   cli();
2355   printk("QUEUE STATUS:\nissue_SC:\n");
2356   for(ptr=issue_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble )
2357     show_command(ptr);
2358 
2359   printk("current_SC:\n");
2360   if(current_SC)
2361     show_command(current_SC);
2362   else
2363     printk("none\n");
2364 
2365   printk("disconnected_SC:\n");
2366   for(ptr=disconnected_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble )
2367     show_command(ptr);
2368 
2369   disp_ports();
2370   disp_enintr();
2371   sti();
2372 }

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