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

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