root/drivers/scsi/aha152x.c

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

DEFINITIONS

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

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

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