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

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