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

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