home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / drivers / scsi / aha152x.c next >
Encoding:
C/C++ Source or Header  |  1994-01-03  |  62.7 KB  |  2,378 lines

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