Home | History | Annotate | Line # | Download | only in ic
aic7xxx.c revision 1.6
      1  1.1  mycroft /*
      2  1.1  mycroft  * Generic driver for the aic7xxx based adaptec SCSI controllers
      3  1.1  mycroft  * Product specific probe and attach routines can be found in:
      4  1.6  mycroft  * i386/eisa/aic7770.c	27/284X and aic7770 motherboard controllers
      5  1.6  mycroft  * pci/aic7870.c	3940, 2940, aic7870 and aic7850 controllers
      6  1.1  mycroft  *
      7  1.6  mycroft  * Copyright (c) 1994, 1995, 1996 Justin T. Gibbs.
      8  1.6  mycroft  * All rights reserved.
      9  1.1  mycroft  *
     10  1.6  mycroft  * Redistribution and use in source and binary forms, with or without
     11  1.6  mycroft  * modification, are permitted provided that the following conditions
     12  1.6  mycroft  * are met:
     13  1.6  mycroft  * 1. Redistributions of source code must retain the above copyright
     14  1.6  mycroft  *    notice immediately at the beginning of the file, without modification,
     15  1.6  mycroft  *    this list of conditions, and the following disclaimer.
     16  1.6  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.6  mycroft  *    notice, this list of conditions and the following disclaimer in the
     18  1.6  mycroft  *    documentation and/or other materials provided with the distribution.
     19  1.6  mycroft  * 3. The name of the author may not be used to endorse or promote products
     20  1.6  mycroft  *    derived from this software without specific prior written permission.
     21  1.1  mycroft  *
     22  1.6  mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     23  1.6  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.6  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.6  mycroft  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     26  1.6  mycroft  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.6  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.6  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.6  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.6  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.6  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.6  mycroft  * SUCH DAMAGE.
     33  1.1  mycroft  *
     34  1.6  mycroft  *      $Id: aic7xxx.c,v 1.6 1996/05/16 03:59:03 mycroft Exp $
     35  1.1  mycroft  */
     36  1.1  mycroft /*
     37  1.1  mycroft  * TODO:
     38  1.1  mycroft  *	Implement Target Mode
     39  1.6  mycroft  *
     40  1.6  mycroft  * A few notes on how SCB paging works...
     41  1.6  mycroft  *
     42  1.6  mycroft  * SCB paging takes advantage of the fact that devices stay disconnected
     43  1.6  mycroft  * from the bus a relatively long time and that while they're disconnected,
     44  1.6  mycroft  * having the SCBs for that device down on the host adapter is of little use.
     45  1.6  mycroft  * Instead we copy the SCB back up into kernel memory and reuse the SCB slot
     46  1.6  mycroft  * on the card to schedule another transaction.  This can be a real payoff
     47  1.6  mycroft  * when doing random I/O to tagged queueing devices since there are more
     48  1.6  mycroft  * transactions active at once for the device to sort for optimal seek
     49  1.6  mycroft  * reduction. The algorithm goes like this...
     50  1.6  mycroft  *
     51  1.6  mycroft  * At the sequencer level:
     52  1.6  mycroft  * 1) Disconnected SCBs are threaded onto a doubly linked list, headed by
     53  1.6  mycroft  *    DISCONNECTED_SCBH using the SCB_NEXT and SCB_PREV fields.  The most
     54  1.6  mycroft  *    recently disconnected device is always at the head.
     55  1.6  mycroft  *
     56  1.6  mycroft  * 2) The SCB has an added field SCB_TAG that corresponds to the kernel
     57  1.6  mycroft  *    SCB number (ie 0-254).
     58  1.6  mycroft  *
     59  1.6  mycroft  * 3) When a command is queued, the hardware index of the SCB it was downloaded
     60  1.6  mycroft  *    into is placed into the QINFIFO for easy indexing by the sequencer.
     61  1.6  mycroft  *
     62  1.6  mycroft  * 4) The tag field is used as the tag for tagged-queueing, for determining
     63  1.6  mycroft  *    the related kernel SCB, and is the value put into the QOUTFIFO
     64  1.6  mycroft  *    so the kernel doesn't have to upload the SCB to determine the kernel SCB
     65  1.6  mycroft  *    that completed on command completes.
     66  1.6  mycroft  *
     67  1.6  mycroft  * 5) When a reconnect occurs, the sequencer must scan the SCB array (even
     68  1.6  mycroft  *    in the tag case) looking for the appropriate SCB and if it can't find
     69  1.6  mycroft  *    it, it interrupts the kernel so it can page the SCB in.
     70  1.6  mycroft  *
     71  1.6  mycroft  * 6) If the sequencer is successful in finding the SCB, it removes it from
     72  1.6  mycroft  *    the doubly linked list of disconnected SCBS.
     73  1.6  mycroft  *
     74  1.6  mycroft  * At the kernel level:
     75  1.6  mycroft  * 1) There are four queues that a kernel SCB may reside on:
     76  1.6  mycroft  *	free_scbs - SCBs that are not in use and have a hardware slot assigned
     77  1.6  mycroft  *		    to them.
     78  1.6  mycroft  *      page_scbs - SCBs that are not in use and need to have a hardware slot
     79  1.6  mycroft  *		    assigned to them (i.e. they will most likely cause a page
     80  1.6  mycroft  *		    out event).
     81  1.6  mycroft  *	waiting_scbs - SCBs that are active, don't have an assigned hardware
     82  1.6  mycroft  *		    slot assigned to them and are waiting for either a
     83  1.6  mycroft  *		    disconnection or a command complete to free up a slot.
     84  1.6  mycroft  *	assigned_scbs - SCBs that were in the waiting_scbs queue, but were
     85  1.6  mycroft  *		    assigned a slot by ahc_free_scb.
     86  1.6  mycroft  *
     87  1.6  mycroft  * 2) When a new request comes in, an SCB is allocated from the free_scbs or
     88  1.6  mycroft  *    page_scbs queue with preference to SCBs on the free_scbs queue.
     89  1.6  mycroft  *
     90  1.6  mycroft  * 3) If there are no free slots (we retrieved the SCB off of the page_scbs
     91  1.6  mycroft  *    queue), the SCB is inserted onto the tail of the waiting_scbs list and
     92  1.6  mycroft  *    we attempt to run this queue down.
     93  1.6  mycroft  *
     94  1.6  mycroft  * 4) ahc_run_waiing_queues() looks at both the assigned_scbs and waiting_scbs
     95  1.6  mycroft  *    queues.  In the case of the assigned_scbs, the commands are immediately
     96  1.6  mycroft  *    downloaded and started.  For waiting_scbs, we page in all that we can
     97  1.6  mycroft  *    ensuring we don't create a resource deadlock (see comments in
     98  1.6  mycroft  *    ahc_run_waing_queues()).
     99  1.6  mycroft  *
    100  1.6  mycroft  * 5) After we handle a bunch of command completes, we also try running the
    101  1.6  mycroft  *    queues since many SCBs may have disconnected since the last command
    102  1.6  mycroft  *    was started and we have at least one free slot on the card.
    103  1.6  mycroft  *
    104  1.6  mycroft  * 6) ahc_free_scb looks at the waiting_scbs queue for a transaction
    105  1.6  mycroft  *    requiring a slot and moves it to the assigned_scbs queue if it
    106  1.6  mycroft  *    finds one.  Otherwise it puts the current SCB onto the free_scbs
    107  1.6  mycroft  *    queue for later use.
    108  1.6  mycroft  *
    109  1.6  mycroft  * 7) The driver handles page-in requests from the sequencer in response to
    110  1.6  mycroft  *    the NO_MATCH sequencer interrupt.  For tagged commands, the approprite
    111  1.6  mycroft  *    SCB is easily found since the tag is a direct index into our kernel SCB
    112  1.6  mycroft  *    array.  For non-tagged commands, we keep a separate array of 16 pointers
    113  1.6  mycroft  *    that point to the single possible SCB that was paged out for that target.
    114  1.1  mycroft  */
    115  1.1  mycroft 
    116  1.1  mycroft #include <sys/param.h>
    117  1.1  mycroft #include <sys/systm.h>
    118  1.6  mycroft #if defined(__NetBSD__)
    119  1.1  mycroft #include <sys/device.h>
    120  1.6  mycroft #include <machine/bus.h>
    121  1.6  mycroft #include <machine/intr.h>
    122  1.6  mycroft #endif /* defined(__NetBSD__) */
    123  1.6  mycroft 
    124  1.1  mycroft #include <sys/malloc.h>
    125  1.1  mycroft #include <sys/buf.h>
    126  1.1  mycroft #include <sys/proc.h>
    127  1.1  mycroft 
    128  1.1  mycroft #include <scsi/scsi_all.h>
    129  1.6  mycroft #if defined(__NetBSD__)
    130  1.1  mycroft #include <scsi/scsi_debug.h>
    131  1.6  mycroft #endif
    132  1.1  mycroft #include <scsi/scsiconf.h>
    133  1.1  mycroft 
    134  1.6  mycroft #if defined(__FreeBSD__)
    135  1.6  mycroft #include <machine/clock.h>
    136  1.5  mycroft #endif
    137  1.1  mycroft 
    138  1.6  mycroft #include <vm/vm.h>
    139  1.6  mycroft #include <vm/vm_param.h>
    140  1.6  mycroft #include <vm/pmap.h>
    141  1.1  mycroft 
    142  1.6  mycroft #if defined(__FreeBSD__)
    143  1.6  mycroft #include <i386/scsi/aic7xxx.h>
    144  1.1  mycroft 
    145  1.6  mycroft #include <dev/aic7xxx/aic7xxx_reg.h>
    146  1.6  mycroft #endif /* defined(__FreeBSD__) */
    147  1.1  mycroft 
    148  1.6  mycroft #if defined(__NetBSD__)
    149  1.6  mycroft #include <dev/ic/aic7xxxreg.h>
    150  1.6  mycroft #include <dev/ic/aic7xxxvar.h>
    151  1.1  mycroft 
    152  1.6  mycroft #define bootverbose	1
    153  1.1  mycroft 
    154  1.6  mycroft #define DEBUGTARG	DEBUGTARGET
    155  1.6  mycroft #if DEBUGTARG < 0	/* Negative numbrs for disabling cause warnings */
    156  1.6  mycroft #undef DEBUGTARG
    157  1.6  mycroft #define DEBUGTARG	9
    158  1.6  mycroft #endif
    159  1.6  mycroft #endif /* defined(__NetBSD__) */
    160  1.1  mycroft 
    161  1.6  mycroft #define PAGESIZ NBPG
    162  1.1  mycroft 
    163  1.6  mycroft #include <sys/kernel.h>
    164  1.6  mycroft #define KVTOPHYS(x)   vtophys(x)
    165  1.1  mycroft 
    166  1.6  mycroft #define MIN(a,b) ((a < b) ? a : b)
    167  1.6  mycroft #define ALL_TARGETS -1
    168  1.1  mycroft 
    169  1.6  mycroft #if defined(__FreeBSD__)
    170  1.6  mycroft u_long ahc_unit = 0;
    171  1.6  mycroft #endif
    172  1.1  mycroft 
    173  1.6  mycroft #ifdef AHC_DEBUG
    174  1.6  mycroft static int     ahc_debug = AHC_SHOWSENSE;
    175  1.6  mycroft #endif
    176  1.1  mycroft 
    177  1.6  mycroft #ifdef AHC_BROKEN_CACHE
    178  1.6  mycroft int ahc_broken_cache = 1;
    179  1.1  mycroft 
    180  1.1  mycroft /*
    181  1.6  mycroft  * "wbinvd" cause writing back whole cache (both CPU internal & external)
    182  1.6  mycroft  * to memory, so that the instruction takes a lot of time.
    183  1.6  mycroft  * This makes machine slow.
    184  1.1  mycroft  */
    185  1.6  mycroft #define	INVALIDATE_CACHE()	__asm __volatile("wbinvd")
    186  1.6  mycroft #endif
    187  1.1  mycroft 
    188  1.6  mycroft /**** bit definitions for SCSIDEF ****/
    189  1.6  mycroft #define	HSCSIID		0x07		/* our SCSI ID */
    190  1.6  mycroft #define HWSCSIID	0x0f		/* our SCSI ID if Wide Bus */
    191  1.1  mycroft 
    192  1.6  mycroft static void	 ahcminphys __P((struct buf *bp));
    193  1.6  mycroft static int32_t	 ahc_scsi_cmd __P((struct scsi_xfer *xs));
    194  1.1  mycroft 
    195  1.6  mycroft static struct scsi_adapter ahc_switch =
    196  1.6  mycroft {
    197  1.6  mycroft         ahc_scsi_cmd,
    198  1.6  mycroft         ahcminphys,
    199  1.6  mycroft         0,
    200  1.6  mycroft         0,
    201  1.6  mycroft #if defined(__FreeBSD__)
    202  1.6  mycroft         0,
    203  1.6  mycroft         "ahc",
    204  1.6  mycroft         { 0, 0 }
    205  1.6  mycroft #endif
    206  1.6  mycroft };
    207  1.1  mycroft 
    208  1.6  mycroft /* the below structure is so we have a default dev struct for our link struct */
    209  1.6  mycroft static struct scsi_device ahc_dev =
    210  1.6  mycroft {
    211  1.6  mycroft     NULL,                       /* Use default error handler */
    212  1.6  mycroft     NULL,                       /* have a queue, served by this */
    213  1.6  mycroft     NULL,                       /* have no async handler */
    214  1.6  mycroft     NULL,                       /* Use default 'done' routine */
    215  1.6  mycroft #if defined(__FreeBSD__)
    216  1.6  mycroft     "ahc",
    217  1.6  mycroft     0,
    218  1.6  mycroft     { 0, 0 }
    219  1.6  mycroft #endif
    220  1.6  mycroft };
    221  1.1  mycroft 
    222  1.1  mycroft /*
    223  1.6  mycroft  * Since the sequencer can disable pausing in a critical section, we
    224  1.6  mycroft  * must loop until it actually stops.
    225  1.6  mycroft  * XXX Should add a timeout in here??
    226  1.1  mycroft  */
    227  1.6  mycroft #define PAUSE_SEQUENCER(ahc)					\
    228  1.6  mycroft 	AHC_OUTB(ahc, HCNTRL, ahc->pause);			\
    229  1.6  mycroft 								\
    230  1.6  mycroft 	while ((AHC_INB(ahc, HCNTRL) & PAUSE) == 0)		\
    231  1.6  mycroft 		;
    232  1.1  mycroft 
    233  1.6  mycroft #define UNPAUSE_SEQUENCER(ahc)					\
    234  1.6  mycroft 	AHC_OUTB(ahc, HCNTRL, ahc->unpause )
    235  1.1  mycroft 
    236  1.1  mycroft /*
    237  1.6  mycroft  * Restart the sequencer program from address zero
    238  1.1  mycroft  */
    239  1.6  mycroft #define RESTART_SEQUENCER(ahc)						\
    240  1.6  mycroft 	do {								\
    241  1.6  mycroft 		AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);		\
    242  1.6  mycroft 	} while (AHC_INB(ahc, SEQADDR0) != 0 &&				\
    243  1.6  mycroft 		 AHC_INB(ahc, SEQADDR1) != 0);				\
    244  1.6  mycroft 									\
    245  1.6  mycroft 	UNPAUSE_SEQUENCER(ahc);
    246  1.1  mycroft 
    247  1.6  mycroft #if defined(__NetBSD__)
    248  1.1  mycroft /*
    249  1.6  mycroft  * Is device which is pointed by sc_link connected on second scsi bus ?
    250  1.1  mycroft  */
    251  1.6  mycroft #define	IS_SCSIBUS_B(ahc, sc_link)	\
    252  1.6  mycroft 	((sc_link)->scsibus == (ahc)->sc_link_b.scsibus)
    253  1.1  mycroft 
    254  1.1  mycroft /*
    255  1.6  mycroft  * convert FreeBSD's SCSI symbols to NetBSD's
    256  1.6  mycroft  */
    257  1.6  mycroft #define	SCSI_NOMASK	SCSI_POLL
    258  1.6  mycroft #define	opennings	openings
    259  1.6  mycroft #endif
    260  1.6  mycroft 
    261  1.6  mycroft static u_char	ahc_abort_wscb __P((struct ahc_data *ahc, struct scb *scbp,
    262  1.6  mycroft 				    u_char prev,
    263  1.6  mycroft 				    u_char timedout_scb, u_int32_t xs_error));
    264  1.6  mycroft static void	ahc_add_waiting_scb __P((struct ahc_data *ahc,
    265  1.6  mycroft 					 struct scb *scb));
    266  1.6  mycroft static void	ahc_done __P((struct ahc_data *ahc, struct scb *scbp));
    267  1.6  mycroft static void	ahc_free_scb __P((struct ahc_data *ahc, struct scb *scb,
    268  1.6  mycroft 				  int flags));
    269  1.6  mycroft static inline void ahc_send_scb __P((struct ahc_data *ahc, struct scb *scb));
    270  1.6  mycroft static inline void ahc_fetch_scb __P((struct ahc_data *ahc, struct scb *scb));
    271  1.6  mycroft static inline void ahc_page_scb __P((struct ahc_data *ahc, struct scb *out_scb,
    272  1.6  mycroft 				struct scb *in_scb));
    273  1.6  mycroft static inline void ahc_run_waiting_queues __P((struct ahc_data *ahc));
    274  1.6  mycroft static struct scb *
    275  1.6  mycroft 		ahc_get_scb __P((struct ahc_data *ahc, int flags));
    276  1.6  mycroft static void	ahc_loadseq __P((struct ahc_data *ahc));
    277  1.6  mycroft static int	ahc_match_scb __P((struct scb *scb, int target, char channel));
    278  1.6  mycroft static int	ahc_poll __P((struct ahc_data *ahc, int wait));
    279  1.6  mycroft #ifdef AHC_DEBUG
    280  1.6  mycroft static void	ahc_print_scb __P((struct scb *scb));
    281  1.6  mycroft #endif
    282  1.6  mycroft static int	ahc_reset_channel __P((struct ahc_data *ahc, char channel,
    283  1.6  mycroft 				       u_char timedout_scb, u_int32_t xs_error,
    284  1.6  mycroft 				       u_char initiate_reset));
    285  1.6  mycroft static int	ahc_reset_device __P((struct ahc_data *ahc, int target,
    286  1.6  mycroft 				      char channel, u_char timedout_scb,
    287  1.6  mycroft 				      u_int32_t xs_error));
    288  1.6  mycroft static void	ahc_reset_current_bus __P((struct ahc_data *ahc));
    289  1.6  mycroft static void	ahc_run_done_queue __P((struct ahc_data *ahc));
    290  1.6  mycroft static void	ahc_scsirate __P((struct ahc_data* ahc, u_char *scsirate,
    291  1.6  mycroft 				  int period, int offset, int target));
    292  1.6  mycroft #if defined(__FreeBSD__)
    293  1.6  mycroft static timeout_t
    294  1.6  mycroft 		ahc_timeout;
    295  1.6  mycroft #elif defined(__NetBSD__)
    296  1.6  mycroft static void	ahc_timeout __P((void *));
    297  1.6  mycroft #endif
    298  1.6  mycroft static void	ahc_busy_target __P((struct ahc_data *ahc,
    299  1.6  mycroft 				     int target, char channel));
    300  1.6  mycroft static void	ahc_unbusy_target __P((struct ahc_data *ahc,
    301  1.6  mycroft 				       int target, char channel));
    302  1.1  mycroft 
    303  1.6  mycroft #if defined(__FreeBSD__)
    304  1.1  mycroft 
    305  1.6  mycroft char *ahc_name(ahc)
    306  1.6  mycroft 	struct ahc_data *ahc;
    307  1.6  mycroft {
    308  1.6  mycroft 	static char name[10];
    309  1.1  mycroft 
    310  1.6  mycroft 	sprintf(name, "ahc%d", ahc->unit);
    311  1.6  mycroft 	return (name);
    312  1.6  mycroft }
    313  1.1  mycroft 
    314  1.6  mycroft #elif defined(__NetBSD__)
    315  1.6  mycroft struct cfdriver ahc_cd = {
    316  1.6  mycroft 	NULL, "ahc", DV_DULL
    317  1.6  mycroft };
    318  1.6  mycroft #endif
    319  1.1  mycroft 
    320  1.1  mycroft #ifdef  AHC_DEBUG
    321  1.6  mycroft static void
    322  1.1  mycroft ahc_print_scb(scb)
    323  1.6  mycroft         struct scb *scb;
    324  1.1  mycroft {
    325  1.6  mycroft         printf("scb:%p control:0x%x tcl:0x%x cmdlen:%d cmdpointer:0x%lx\n"
    326  1.6  mycroft 	    ,scb
    327  1.6  mycroft 	    ,scb->control
    328  1.6  mycroft 	    ,scb->tcl
    329  1.6  mycroft 	    ,scb->cmdlen
    330  1.6  mycroft 	    ,scb->cmdpointer );
    331  1.6  mycroft         printf("        datlen:%d data:0x%lx segs:0x%x segp:0x%lx\n"
    332  1.6  mycroft 	    ,scb->datalen
    333  1.6  mycroft 	    ,scb->data
    334  1.6  mycroft 	    ,scb->SG_segment_count
    335  1.6  mycroft 	    ,scb->SG_list_pointer);
    336  1.6  mycroft 	printf("	sg_addr:%lx sg_len:%ld\n"
    337  1.6  mycroft 	    ,scb->ahc_dma[0].addr
    338  1.6  mycroft 	    ,scb->ahc_dma[0].len);
    339  1.1  mycroft }
    340  1.1  mycroft 
    341  1.1  mycroft #endif
    342  1.1  mycroft 
    343  1.1  mycroft static struct {
    344  1.6  mycroft         u_char errno;
    345  1.1  mycroft 	char *errmesg;
    346  1.1  mycroft } hard_error[] = {
    347  1.1  mycroft 	{ ILLHADDR,  "Illegal Host Access" },
    348  1.1  mycroft 	{ ILLSADDR,  "Illegal Sequencer Address referrenced" },
    349  1.1  mycroft 	{ ILLOPCODE, "Illegal Opcode in sequencer program" },
    350  1.1  mycroft 	{ PARERR,    "Sequencer Ram Parity Error" }
    351  1.1  mycroft };
    352  1.1  mycroft 
    353  1.1  mycroft 
    354  1.1  mycroft /*
    355  1.1  mycroft  * Valid SCSIRATE values.  (p. 3-17)
    356  1.1  mycroft  * Provides a mapping of tranfer periods in ns to the proper value to
    357  1.1  mycroft  * stick in the scsiscfr reg to use that transfer rate.
    358  1.1  mycroft  */
    359  1.1  mycroft static struct {
    360  1.6  mycroft 	short sxfr;
    361  1.6  mycroft 	/* Rates in Ultra mode have bit 8 of sxfr set */
    362  1.6  mycroft #define		ULTRA_SXFR 0x100
    363  1.6  mycroft 	short period; /* in ns */
    364  1.1  mycroft 	char *rate;
    365  1.1  mycroft } ahc_syncrates[] = {
    366  1.6  mycroft 	{ 0x100,  50, "20.0"  },
    367  1.6  mycroft 	{ 0x110,  62, "16.0"  },
    368  1.6  mycroft 	{ 0x120,  75, "13.4"  },
    369  1.6  mycroft 	{ 0x130, 175,  "5.7"  },
    370  1.6  mycroft 	{ 0x140, 200,  "5.0"  },
    371  1.6  mycroft 	{ 0x150, 225,  "4.4"  },
    372  1.6  mycroft 	{ 0x160, 250,  "4.0"  },
    373  1.6  mycroft 	{ 0x170, 275,  "3.6"  },
    374  1.6  mycroft 	{ 0x000, 100, "10.0"  },
    375  1.6  mycroft 	{ 0x010, 125,  "8.0"  },
    376  1.6  mycroft 	{ 0x020, 150,  "6.67" },
    377  1.6  mycroft 	{ 0x030, 175,  "5.7"  },
    378  1.6  mycroft 	{ 0x040, 200,  "5.0"  },
    379  1.6  mycroft 	{ 0x050, 225,  "4.4"  },
    380  1.6  mycroft 	{ 0x060, 250,  "4.0"  },
    381  1.6  mycroft 	{ 0x070, 275,  "3.6"  }
    382  1.1  mycroft };
    383  1.1  mycroft 
    384  1.1  mycroft static int ahc_num_syncrates =
    385  1.1  mycroft 	sizeof(ahc_syncrates) / sizeof(ahc_syncrates[0]);
    386  1.1  mycroft 
    387  1.1  mycroft /*
    388  1.6  mycroft  * Allocate a controller structures for a new device and initialize it.
    389  1.6  mycroft  * ahc_reset should be called before now since we assume that the card
    390  1.6  mycroft  * is paused.
    391  1.6  mycroft  *
    392  1.1  mycroft  */
    393  1.6  mycroft #if defined(__FreeBSD__)
    394  1.6  mycroft struct ahc_data *
    395  1.6  mycroft ahc_alloc(unit, iobase, type, flags)
    396  1.6  mycroft 	int unit;
    397  1.6  mycroft 	u_long iobase;
    398  1.6  mycroft #elif defined(__NetBSD__)
    399  1.6  mycroft void
    400  1.6  mycroft ahc_construct(ahc, bc, ioh, type, flags)
    401  1.6  mycroft 	struct  ahc_data *ahc;
    402  1.6  mycroft 	bus_chipset_tag_t bc;
    403  1.6  mycroft 	bus_io_handle_t ioh;
    404  1.6  mycroft #endif
    405  1.6  mycroft 	ahc_type type;
    406  1.6  mycroft 	ahc_flag flags;
    407  1.6  mycroft {
    408  1.1  mycroft 
    409  1.6  mycroft 	/*
    410  1.6  mycroft 	 * find unit and check we have that many defined
    411  1.6  mycroft 	 */
    412  1.1  mycroft 
    413  1.6  mycroft #if defined(__FreeBSD__)
    414  1.6  mycroft 	struct  ahc_data *ahc;
    415  1.1  mycroft 
    416  1.1  mycroft 	/*
    417  1.6  mycroft 	 * Allocate a storage area for us
    418  1.1  mycroft 	 */
    419  1.1  mycroft 
    420  1.6  mycroft 	ahc = malloc(sizeof(struct ahc_data), M_TEMP, M_NOWAIT);
    421  1.6  mycroft 	if (!ahc) {
    422  1.6  mycroft 		printf("ahc%d: cannot malloc!\n", unit);
    423  1.6  mycroft 		return NULL;
    424  1.6  mycroft 	}
    425  1.6  mycroft 	bzero(ahc, sizeof(struct ahc_data));
    426  1.6  mycroft #endif
    427  1.6  mycroft 	STAILQ_INIT(&ahc->free_scbs);
    428  1.6  mycroft 	STAILQ_INIT(&ahc->page_scbs);
    429  1.6  mycroft 	STAILQ_INIT(&ahc->waiting_scbs);
    430  1.6  mycroft 	STAILQ_INIT(&ahc->assigned_scbs);
    431  1.6  mycroft #if defined(__FreeBSD__)
    432  1.6  mycroft 	ahc->unit = unit;
    433  1.6  mycroft 	ahc->baseport = iobase;
    434  1.6  mycroft #elif defined(__NetBSD__)
    435  1.6  mycroft 	ahc->sc_bc = bc;
    436  1.6  mycroft 	ahc->sc_ioh = ioh;
    437  1.6  mycroft #endif
    438  1.6  mycroft 	ahc->type = type;
    439  1.6  mycroft 	ahc->flags = flags;
    440  1.6  mycroft 	ahc->unpause = (AHC_INB(ahc, HCNTRL) & IRQMS) | INTEN;
    441  1.6  mycroft 	ahc->pause = ahc->unpause | PAUSE;
    442  1.6  mycroft 
    443  1.6  mycroft #if defined(__FreeBSD__)
    444  1.6  mycroft 	return (ahc);
    445  1.6  mycroft #endif
    446  1.6  mycroft }
    447  1.1  mycroft 
    448  1.6  mycroft void
    449  1.6  mycroft ahc_free(ahc)
    450  1.6  mycroft 	struct ahc_data *ahc;
    451  1.6  mycroft {
    452  1.6  mycroft #if defined(__FreeBSD__)
    453  1.6  mycroft 	free(ahc, M_DEVBUF);
    454  1.6  mycroft 	return;
    455  1.6  mycroft #endif
    456  1.1  mycroft }
    457  1.1  mycroft 
    458  1.6  mycroft void
    459  1.6  mycroft #if defined(__FreeBSD__)
    460  1.6  mycroft ahc_reset(iobase)
    461  1.6  mycroft 	u_long iobase;
    462  1.6  mycroft #elif defined(__NetBSD__)
    463  1.6  mycroft ahc_reset(devname, bc, ioh)
    464  1.6  mycroft 	char *devname;
    465  1.6  mycroft 	bus_chipset_tag_t bc;
    466  1.6  mycroft 	bus_io_handle_t ioh;
    467  1.6  mycroft #endif
    468  1.6  mycroft {
    469  1.6  mycroft         u_char hcntrl;
    470  1.6  mycroft 	int wait;
    471  1.6  mycroft 
    472  1.6  mycroft 	/* Retain the IRQ type accross the chip reset */
    473  1.6  mycroft #if defined(__FreeBSD__)
    474  1.6  mycroft 	hcntrl = (inb(HCNTRL + iobase) & IRQMS) | INTEN;
    475  1.6  mycroft 
    476  1.6  mycroft 	outb(HCNTRL + iobase, CHIPRST | PAUSE);
    477  1.6  mycroft #elif defined(__NetBSD__)
    478  1.6  mycroft 	hcntrl = (bus_io_read_1(bc, ioh, HCNTRL) & IRQMS) | INTEN;
    479  1.6  mycroft 
    480  1.6  mycroft 	bus_io_write_1(bc, ioh, HCNTRL, CHIPRST | PAUSE);
    481  1.6  mycroft #endif
    482  1.6  mycroft 	/*
    483  1.6  mycroft 	 * Ensure that the reset has finished
    484  1.6  mycroft 	 */
    485  1.6  mycroft 	wait = 1000;
    486  1.6  mycroft #if defined(__FreeBSD__)
    487  1.6  mycroft 	while (--wait && !(inb(HCNTRL + iobase) & CHIPRSTACK))
    488  1.6  mycroft #elif defined(__NetBSD__)
    489  1.6  mycroft 	while (--wait && !(bus_io_read_1(bc, ioh, HCNTRL) & CHIPRSTACK))
    490  1.6  mycroft #endif
    491  1.6  mycroft 		DELAY(1000);
    492  1.6  mycroft 	if(wait == 0) {
    493  1.6  mycroft #if defined(__FreeBSD__)
    494  1.6  mycroft 		printf("ahc at 0x%lx: WARNING - Failed chip reset!  "
    495  1.6  mycroft 		       "Trying to initialize anyway.\n", iobase);
    496  1.6  mycroft #elif defined(__NetBSD__)
    497  1.6  mycroft 		printf("%s: WARNING - Failed chip reset!  "
    498  1.6  mycroft 		       "Trying to initialize anyway.\n", devname);
    499  1.6  mycroft #endif
    500  1.6  mycroft 	}
    501  1.6  mycroft #if defined(__FreeBSD__)
    502  1.6  mycroft 	outb(HCNTRL + iobase, hcntrl | PAUSE);
    503  1.6  mycroft #elif defined(__NetBSD__)
    504  1.6  mycroft 	bus_io_write_1(bc, ioh, HCNTRL, hcntrl | PAUSE);
    505  1.6  mycroft #endif
    506  1.6  mycroft }
    507  1.1  mycroft 
    508  1.1  mycroft /*
    509  1.1  mycroft  * Look up the valid period to SCSIRATE conversion in our table.
    510  1.1  mycroft  */
    511  1.6  mycroft static void
    512  1.6  mycroft ahc_scsirate(ahc, scsirate, period, offset, target )
    513  1.6  mycroft 	struct	ahc_data *ahc;
    514  1.6  mycroft 	u_char	*scsirate;
    515  1.6  mycroft 	short	period;
    516  1.6  mycroft 	u_char	offset;
    517  1.6  mycroft 	int	target;
    518  1.1  mycroft {
    519  1.1  mycroft 	int i;
    520  1.1  mycroft 
    521  1.1  mycroft 	for (i = 0; i < ahc_num_syncrates; i++) {
    522  1.6  mycroft 
    523  1.1  mycroft 		if ((ahc_syncrates[i].period - period) >= 0) {
    524  1.6  mycroft 			/*
    525  1.6  mycroft 			 * Watch out for Ultra speeds when ultra is not
    526  1.6  mycroft 			 * enabled and vice-versa.
    527  1.6  mycroft 			 */
    528  1.6  mycroft 			if (ahc->type & AHC_ULTRA) {
    529  1.6  mycroft 				if (!(ahc_syncrates[i].sxfr & ULTRA_SXFR))
    530  1.6  mycroft 					break; /* Use Async */
    531  1.6  mycroft 			}
    532  1.6  mycroft 			else {
    533  1.6  mycroft 				if (ahc_syncrates[i].sxfr & ULTRA_SXFR) {
    534  1.6  mycroft 					/*
    535  1.6  mycroft 					 * This should only happen if the
    536  1.6  mycroft 					 * drive is the first to negotiate
    537  1.6  mycroft 					 * and chooses a high rate.  We'll
    538  1.6  mycroft 					 * just move down the table util
    539  1.6  mycroft 					 * we hit a non ultra speed.
    540  1.6  mycroft 					 */
    541  1.6  mycroft 					continue;
    542  1.6  mycroft 				}
    543  1.6  mycroft 			}
    544  1.6  mycroft 			*scsirate = (ahc_syncrates[i].sxfr) | (offset & 0x0f);
    545  1.6  mycroft 			if(bootverbose) {
    546  1.6  mycroft 				printf("%s: target %d synchronous at %sMHz,"
    547  1.6  mycroft 				       " offset = 0x%x\n",
    548  1.6  mycroft 				        ahc_name(ahc), target,
    549  1.6  mycroft 					ahc_syncrates[i].rate, offset );
    550  1.6  mycroft 			}
    551  1.6  mycroft 			return;
    552  1.1  mycroft 		}
    553  1.1  mycroft 	}
    554  1.1  mycroft 	/* Default to asyncronous transfers.  Also reject this SDTR request. */
    555  1.6  mycroft 	*scsirate = 0;
    556  1.6  mycroft 	if(bootverbose) {
    557  1.6  mycroft 		printf("%s: target %d using asyncronous transfers\n",
    558  1.6  mycroft 			ahc_name(ahc), target );
    559  1.6  mycroft 	}
    560  1.1  mycroft }
    561  1.1  mycroft 
    562  1.6  mycroft #if defined(__NetBSD__)
    563  1.4  mycroft int
    564  1.4  mycroft ahcprint(aux, name)
    565  1.4  mycroft 	void *aux;
    566  1.4  mycroft 	char *name;
    567  1.1  mycroft {
    568  1.1  mycroft 
    569  1.4  mycroft 	if (name != NULL)
    570  1.4  mycroft 		printf("%s: scsibus ", name);
    571  1.4  mycroft 	return UNCONF;
    572  1.1  mycroft }
    573  1.6  mycroft #endif
    574  1.1  mycroft 
    575  1.1  mycroft /*
    576  1.1  mycroft  * Attach all the sub-devices we can find
    577  1.1  mycroft  */
    578  1.1  mycroft int
    579  1.6  mycroft ahc_attach(ahc)
    580  1.6  mycroft 	struct ahc_data *ahc;
    581  1.1  mycroft {
    582  1.6  mycroft 	struct scsibus_data *scbus;
    583  1.1  mycroft 
    584  1.6  mycroft #ifdef AHC_BROKEN_CACHE
    585  1.6  mycroft 	if (cpu_class == CPUCLASS_386)	/* doesn't have "wbinvd" instruction */
    586  1.6  mycroft 		ahc_broken_cache = 0;
    587  1.6  mycroft #endif
    588  1.1  mycroft 	/*
    589  1.6  mycroft 	 * fill in the prototype scsi_links.
    590  1.1  mycroft 	 */
    591  1.6  mycroft #if defined(__FreeBSD__)
    592  1.6  mycroft 	ahc->sc_link.adapter_unit = ahc->unit;
    593  1.6  mycroft 	ahc->sc_link.adapter_targ = ahc->our_id;
    594  1.6  mycroft 	ahc->sc_link.fordriver = 0;
    595  1.6  mycroft #elif defined(__NetBSD__)
    596  1.6  mycroft 	ahc->sc_link.adapter_target = ahc->our_id;
    597  1.6  mycroft #endif
    598  1.1  mycroft 	ahc->sc_link.adapter_softc = ahc;
    599  1.1  mycroft 	ahc->sc_link.adapter = &ahc_switch;
    600  1.6  mycroft 	ahc->sc_link.opennings = 2;
    601  1.1  mycroft 	ahc->sc_link.device = &ahc_dev;
    602  1.1  mycroft 	ahc->sc_link.flags = DEBUGLEVEL;
    603  1.6  mycroft 
    604  1.6  mycroft 	if(ahc->type & AHC_TWIN) {
    605  1.6  mycroft 		/* Configure the second scsi bus */
    606  1.6  mycroft 		ahc->sc_link_b = ahc->sc_link;
    607  1.6  mycroft #if defined(__FreeBSD__)
    608  1.6  mycroft 		ahc->sc_link_b.adapter_targ = ahc->our_id_b;
    609  1.6  mycroft 		ahc->sc_link_b.adapter_bus = 1;
    610  1.6  mycroft 		ahc->sc_link_b.fordriver = (void *)SELBUSB;
    611  1.6  mycroft #elif defined(__NetBSD__)
    612  1.6  mycroft 		ahc->sc_link_b.adapter_target = ahc->our_id_b;
    613  1.6  mycroft #endif
    614  1.6  mycroft 	}
    615  1.6  mycroft 
    616  1.6  mycroft 
    617  1.6  mycroft #if defined(__FreeBSD__)
    618  1.6  mycroft 	/*
    619  1.6  mycroft 	 * Prepare the scsibus_data area for the upperlevel
    620  1.6  mycroft 	 * scsi code.
    621  1.6  mycroft 	 */
    622  1.6  mycroft 	scbus = scsi_alloc_bus();
    623  1.6  mycroft 	if(!scbus)
    624  1.6  mycroft 		return 0;
    625  1.6  mycroft 	scbus->adapter_link = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ?
    626  1.6  mycroft 				&ahc->sc_link_b : &ahc->sc_link;
    627  1.6  mycroft 	if(ahc->type & AHC_WIDE)
    628  1.6  mycroft 		scbus->maxtarg = 15;
    629  1.6  mycroft 
    630  1.6  mycroft 	/*
    631  1.6  mycroft 	 * ask the adapter what subunits are present
    632  1.6  mycroft 	 */
    633  1.6  mycroft 	if(bootverbose)
    634  1.6  mycroft 		printf("ahc%d: Probing channel %c\n", ahc->unit,
    635  1.6  mycroft 			(ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 'B' : 'A');
    636  1.6  mycroft 	scsi_attachdevs(scbus);
    637  1.6  mycroft 	scbus = NULL;	/* Upper-level SCSI code owns this now */
    638  1.6  mycroft 
    639  1.6  mycroft 	if(ahc->type & AHC_TWIN) {
    640  1.6  mycroft 		scbus =  scsi_alloc_bus();
    641  1.6  mycroft 		if(!scbus)
    642  1.6  mycroft 			return 0;
    643  1.6  mycroft 		scbus->adapter_link = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ?
    644  1.6  mycroft 					&ahc->sc_link : &ahc->sc_link_b;
    645  1.6  mycroft 		if(ahc->type & AHC_WIDE)
    646  1.6  mycroft 			scbus->maxtarg = 15;
    647  1.6  mycroft 		if(bootverbose)
    648  1.6  mycroft 			printf("ahc%d: Probing Channel %c\n", ahc->unit,
    649  1.6  mycroft 			       (ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 'A': 'B');
    650  1.6  mycroft 		scsi_attachdevs(scbus);
    651  1.6  mycroft 		scbus = NULL;	/* Upper-level SCSI code owns this now */
    652  1.6  mycroft 	}
    653  1.6  mycroft #elif defined(__NetBSD__)
    654  1.6  mycroft 	/*
    655  1.6  mycroft 	 * XXX - Update MI SCSI code
    656  1.6  mycroft 	 *
    657  1.6  mycroft 	 * if(ahc->type & AHC_WIDE)
    658  1.6  mycroft 	 *	max target of both channel A and B = 15;
    659  1.6  mycroft 	 */
    660  1.1  mycroft 
    661  1.1  mycroft 	/*
    662  1.1  mycroft 	 * ask the adapter what subunits are present
    663  1.1  mycroft 	 */
    664  1.6  mycroft 	if ((ahc->flags & AHC_CHANNEL_B_PRIMARY) == 0) {
    665  1.6  mycroft 		/* make IS_SCSIBUS_B() == false, while probing channel A */
    666  1.6  mycroft 		ahc->sc_link_b.scsibus = 0xff;
    667  1.6  mycroft 
    668  1.6  mycroft 		if (ahc->type & AHC_TWIN)
    669  1.6  mycroft 			printf("%s: Probing channel A\n", ahc_name(ahc));
    670  1.6  mycroft 		config_found((void *)ahc, &ahc->sc_link, ahcprint);
    671  1.6  mycroft 		if (ahc->type & AHC_TWIN) {
    672  1.6  mycroft 			printf("%s: Probing channel B\n", ahc_name(ahc));
    673  1.6  mycroft 			config_found((void *)ahc, &ahc->sc_link_b, ahcprint);
    674  1.6  mycroft 		}
    675  1.6  mycroft 	} else {
    676  1.6  mycroft 		/*
    677  1.6  mycroft 		 * if implementation of IS_SCSIBUS_B() is changed to use
    678  1.6  mycroft 		 * ahc->sc_link.scsibus, then "ahc->sc_link.scsibus = 0xff;"
    679  1.6  mycroft 		 * is needed, here.
    680  1.6  mycroft 		 */
    681  1.6  mycroft 
    682  1.6  mycroft 		/* assert(ahc->type & AHC_TWIN); */
    683  1.6  mycroft 		printf("%s: Probing channel B\n", ahc_name(ahc));
    684  1.1  mycroft 		config_found((void *)ahc, &ahc->sc_link_b, ahcprint);
    685  1.6  mycroft 		printf("%s: Probing channel A\n", ahc_name(ahc));
    686  1.6  mycroft 		config_found((void *)ahc, &ahc->sc_link, ahcprint);
    687  1.1  mycroft 	}
    688  1.6  mycroft #endif
    689  1.1  mycroft 	return 1;
    690  1.1  mycroft }
    691  1.1  mycroft 
    692  1.6  mycroft /*
    693  1.6  mycroft  * Send an SCB down to the card via PIO.
    694  1.6  mycroft  * We assume that the proper SCB is already selected in SCBPTR.
    695  1.6  mycroft  */
    696  1.6  mycroft static inline void
    697  1.1  mycroft ahc_send_scb(ahc, scb)
    698  1.6  mycroft         struct	ahc_data *ahc;
    699  1.6  mycroft         struct	scb *scb;
    700  1.1  mycroft {
    701  1.6  mycroft 	AHC_OUTB(ahc, SCBCNT, SCBAUTO);
    702  1.6  mycroft 	if( ahc->type == AHC_284 )
    703  1.6  mycroft 		/* Can only do 8bit PIO */
    704  1.6  mycroft 		AHC_OUTSB(ahc, SCBARRAY, scb, SCB_PIO_TRANSFER_SIZE);
    705  1.6  mycroft 	else
    706  1.6  mycroft 		AHC_OUTSL(ahc, SCBARRAY, scb,
    707  1.6  mycroft 		      (SCB_PIO_TRANSFER_SIZE + 3) / 4);
    708  1.6  mycroft 	AHC_OUTB(ahc, SCBCNT, 0);
    709  1.6  mycroft }
    710  1.6  mycroft 
    711  1.6  mycroft /*
    712  1.6  mycroft  * Retrieve an SCB from the card via PIO.
    713  1.6  mycroft  * We assume that the proper SCB is already selected in SCBPTR.
    714  1.6  mycroft  */
    715  1.6  mycroft static inline void
    716  1.6  mycroft ahc_fetch_scb(ahc, scb)
    717  1.6  mycroft 	struct	ahc_data *ahc;
    718  1.6  mycroft 	struct	scb *scb;
    719  1.6  mycroft {
    720  1.6  mycroft 	AHC_OUTB(ahc, SCBCNT, 0x80);	/* SCBAUTO */
    721  1.6  mycroft 
    722  1.6  mycroft 	/* Can only do 8bit PIO for reads */
    723  1.6  mycroft 	AHC_INSB(ahc, SCBARRAY, scb, SCB_PIO_TRANSFER_SIZE);
    724  1.6  mycroft 
    725  1.6  mycroft 	AHC_OUTB(ahc, SCBCNT, 0);
    726  1.6  mycroft }
    727  1.6  mycroft 
    728  1.6  mycroft /*
    729  1.6  mycroft  * Swap in_scbp for out_scbp down in the cards SCB array.
    730  1.6  mycroft  * We assume that the SCB for out_scbp is already selected in SCBPTR.
    731  1.6  mycroft  */
    732  1.6  mycroft static inline void
    733  1.6  mycroft ahc_page_scb(ahc, out_scbp, in_scbp)
    734  1.6  mycroft 	struct ahc_data *ahc;
    735  1.6  mycroft 	struct scb *out_scbp;
    736  1.6  mycroft 	struct scb *in_scbp;
    737  1.6  mycroft {
    738  1.6  mycroft 	/* Page-out */
    739  1.6  mycroft 	ahc_fetch_scb(ahc, out_scbp);
    740  1.6  mycroft 	out_scbp->flags |= SCB_PAGED_OUT;
    741  1.6  mycroft 	if(!(out_scbp->control & TAG_ENB))
    742  1.6  mycroft 	{
    743  1.6  mycroft 		/* Stick in non-tagged array */
    744  1.6  mycroft 		int index =  (out_scbp->tcl >> 4)
    745  1.6  mycroft 			   | (out_scbp->tcl & SELBUSB);
    746  1.6  mycroft 		ahc->pagedout_ntscbs[index] = out_scbp;
    747  1.6  mycroft 	}
    748  1.6  mycroft 
    749  1.6  mycroft 	/* Page-in */
    750  1.6  mycroft 	in_scbp->position = out_scbp->position;
    751  1.6  mycroft 	out_scbp->position = SCB_LIST_NULL;
    752  1.6  mycroft 	ahc_send_scb(ahc, in_scbp);
    753  1.6  mycroft 	in_scbp->flags &= ~SCB_PAGED_OUT;
    754  1.6  mycroft }
    755  1.6  mycroft 
    756  1.6  mycroft static inline void
    757  1.6  mycroft ahc_run_waiting_queues(ahc)
    758  1.6  mycroft 	struct ahc_data *ahc;
    759  1.6  mycroft {
    760  1.6  mycroft 	struct scb* scb;
    761  1.6  mycroft 	u_char cur_scb;
    762  1.6  mycroft 
    763  1.6  mycroft 	if(!(ahc->assigned_scbs.stqh_first || ahc->waiting_scbs.stqh_first))
    764  1.6  mycroft 		return;
    765  1.1  mycroft 
    766  1.1  mycroft 	PAUSE_SEQUENCER(ahc);
    767  1.6  mycroft 	cur_scb = AHC_INB(ahc, SCBPTR);
    768  1.6  mycroft 
    769  1.6  mycroft 	/*
    770  1.6  mycroft 	 * First handle SCBs that are waiting but have been
    771  1.6  mycroft 	 * assigned a slot.
    772  1.6  mycroft 	 */
    773  1.6  mycroft 	while((scb = ahc->assigned_scbs.stqh_first) != NULL) {
    774  1.6  mycroft 		STAILQ_REMOVE_HEAD(&ahc->assigned_scbs, links);
    775  1.6  mycroft 		AHC_OUTB(ahc, SCBPTR, scb->position);
    776  1.6  mycroft 		ahc_send_scb(ahc, scb);
    777  1.6  mycroft 
    778  1.6  mycroft 		/* Mark this as an active command */
    779  1.6  mycroft 		scb->flags = SCB_ACTIVE;
    780  1.6  mycroft 
    781  1.6  mycroft 		AHC_OUTB(ahc, QINFIFO, scb->position);
    782  1.6  mycroft 		if (!(scb->xs->flags & SCSI_NOMASK)) {
    783  1.6  mycroft 			timeout(ahc_timeout, (caddr_t)scb,
    784  1.6  mycroft 				(scb->xs->timeout * hz) / 1000);
    785  1.6  mycroft 		}
    786  1.6  mycroft 		SC_DEBUG(scb->xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
    787  1.6  mycroft 	}
    788  1.6  mycroft 	/* Now deal with SCBs that require paging */
    789  1.6  mycroft 	if((scb = ahc->waiting_scbs.stqh_first) != NULL) {
    790  1.6  mycroft 		u_char disc_scb = AHC_INB(ahc, DISCONNECTED_SCBH);
    791  1.6  mycroft 		u_char active = AHC_INB(ahc, FLAGS) & (SELECTED|IDENTIFY_SEEN);
    792  1.6  mycroft 		int count = 0;
    793  1.6  mycroft 
    794  1.6  mycroft 		do {
    795  1.6  mycroft 			u_char next_scb;
    796  1.6  mycroft 
    797  1.6  mycroft 			/* Attempt to page this SCB in */
    798  1.6  mycroft 			if(disc_scb == SCB_LIST_NULL)
    799  1.6  mycroft 				break;
    800  1.6  mycroft 
    801  1.6  mycroft 			/*
    802  1.6  mycroft 			 * Advance disc_scb to the next on in the
    803  1.6  mycroft 			 * list.
    804  1.6  mycroft 			 */
    805  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, disc_scb);
    806  1.6  mycroft 			next_scb = AHC_INB(ahc, SCB_NEXT);
    807  1.6  mycroft 
    808  1.6  mycroft 			/*
    809  1.6  mycroft 			 * We have to be careful about when we allow
    810  1.6  mycroft 			 * an SCB to be paged out.  There must always
    811  1.6  mycroft 			 * be at least one slot availible for a
    812  1.6  mycroft 			 * reconnecting target in case it references
    813  1.6  mycroft 			 * an SCB that has been paged out.  Our
    814  1.6  mycroft 			 * heuristic is that either the disconnected
    815  1.6  mycroft 			 * list has at least two entries in it or
    816  1.6  mycroft 			 * there is one entry and the sequencer is
    817  1.6  mycroft 			 * activily working on an SCB which implies that
    818  1.6  mycroft 			 * it will either complete or disconnect before
    819  1.6  mycroft 			 * another reconnection can occur.
    820  1.6  mycroft 			 */
    821  1.6  mycroft 			if((next_scb != SCB_LIST_NULL) || active)
    822  1.6  mycroft 			{
    823  1.6  mycroft 				u_char out_scbi;
    824  1.6  mycroft 				struct scb* out_scbp;
    825  1.6  mycroft 
    826  1.6  mycroft 				STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
    827  1.6  mycroft 
    828  1.6  mycroft 				/*
    829  1.6  mycroft 				 * Find the in-core SCB for the one
    830  1.6  mycroft 				 * we're paging out.
    831  1.6  mycroft 				 */
    832  1.6  mycroft 				out_scbi = AHC_INB(ahc, SCB_TAG);
    833  1.6  mycroft 				out_scbp = ahc->scbarray[out_scbi];
    834  1.6  mycroft 
    835  1.6  mycroft 				/* Do the page out */
    836  1.6  mycroft 				ahc_page_scb(ahc, out_scbp, scb);
    837  1.6  mycroft 
    838  1.6  mycroft 				/* Mark this as an active command */
    839  1.6  mycroft 				scb->flags = SCB_ACTIVE;
    840  1.6  mycroft 
    841  1.6  mycroft 				/* Queue the command */
    842  1.6  mycroft 				AHC_OUTB(ahc, QINFIFO, scb->position);
    843  1.6  mycroft 				if (!(scb->xs->flags & SCSI_NOMASK)) {
    844  1.6  mycroft 					timeout(ahc_timeout, (caddr_t)scb,
    845  1.6  mycroft 						(scb->xs->timeout * hz) / 1000);
    846  1.6  mycroft 				}
    847  1.6  mycroft 				SC_DEBUG(scb->xs->sc_link, SDEV_DB3,
    848  1.6  mycroft 					("cmd_paged-in\n"));
    849  1.6  mycroft 				count++;
    850  1.6  mycroft 
    851  1.6  mycroft 				/* Advance to the next disconnected SCB */
    852  1.6  mycroft 				disc_scb = next_scb;
    853  1.6  mycroft 			}
    854  1.6  mycroft 			else
    855  1.6  mycroft 				break;
    856  1.6  mycroft 		} while((scb = ahc->waiting_scbs.stqh_first) != NULL);
    857  1.6  mycroft 
    858  1.6  mycroft 		if(count) {
    859  1.6  mycroft 			/*
    860  1.6  mycroft 			 * Update the head of the disconnected list.
    861  1.6  mycroft 			 */
    862  1.6  mycroft 			AHC_OUTB(ahc, DISCONNECTED_SCBH, disc_scb);
    863  1.6  mycroft 			if(disc_scb != SCB_LIST_NULL) {
    864  1.6  mycroft 				AHC_OUTB(ahc, SCBPTR, disc_scb);
    865  1.6  mycroft 				AHC_OUTB(ahc, SCB_PREV, SCB_LIST_NULL);
    866  1.6  mycroft 			}
    867  1.6  mycroft 		}
    868  1.6  mycroft 	}
    869  1.6  mycroft 	/* Restore old position */
    870  1.6  mycroft 	AHC_OUTB(ahc, SCBPTR, cur_scb);
    871  1.1  mycroft 	UNPAUSE_SEQUENCER(ahc);
    872  1.1  mycroft }
    873  1.1  mycroft 
    874  1.6  mycroft /*
    875  1.6  mycroft  * Add this SCB to the head of the "waiting for selection" list.
    876  1.6  mycroft  */
    877  1.6  mycroft static
    878  1.6  mycroft void ahc_add_waiting_scb(ahc, scb)
    879  1.6  mycroft 	struct ahc_data *ahc;
    880  1.6  mycroft 	struct scb *scb;
    881  1.1  mycroft {
    882  1.6  mycroft 	u_char next;
    883  1.6  mycroft 	u_char curscb;
    884  1.1  mycroft 
    885  1.6  mycroft 	curscb = AHC_INB(ahc, SCBPTR);
    886  1.6  mycroft 	next = AHC_INB(ahc, WAITING_SCBH);
    887  1.6  mycroft 
    888  1.6  mycroft 	AHC_OUTB(ahc, SCBPTR, scb->position);
    889  1.6  mycroft 	AHC_OUTB(ahc, SCB_NEXT, next);
    890  1.6  mycroft 	AHC_OUTB(ahc, WAITING_SCBH, scb->position);
    891  1.6  mycroft 
    892  1.6  mycroft 	AHC_OUTB(ahc, SCBPTR, curscb);
    893  1.1  mycroft }
    894  1.1  mycroft 
    895  1.1  mycroft /*
    896  1.6  mycroft  * Catch an interrupt from the adapter
    897  1.1  mycroft  */
    898  1.6  mycroft #if defined(__FreeBSD__)
    899  1.6  mycroft void
    900  1.6  mycroft #elif defined (__NetBSD__)
    901  1.1  mycroft int
    902  1.6  mycroft #endif
    903  1.6  mycroft ahc_intr(arg)
    904  1.6  mycroft         void *arg;
    905  1.1  mycroft {
    906  1.6  mycroft 	int     intstat;
    907  1.6  mycroft 	u_char	status;
    908  1.6  mycroft 	struct scb *scb = NULL;
    909  1.1  mycroft 	struct scsi_xfer *xs = NULL;
    910  1.6  mycroft 	struct ahc_data *ahc = (struct ahc_data *)arg;
    911  1.6  mycroft 
    912  1.6  mycroft         intstat = AHC_INB(ahc, INTSTAT);
    913  1.1  mycroft 	/*
    914  1.1  mycroft 	 * Is this interrupt for me? or for
    915  1.1  mycroft 	 * someone who is sharing my interrupt
    916  1.1  mycroft 	 */
    917  1.6  mycroft 	if (!(intstat & INT_PEND))
    918  1.6  mycroft #if defined(__FreeBSD__)
    919  1.6  mycroft 		return;
    920  1.6  mycroft #elif defined(__NetBSD__)
    921  1.1  mycroft 		return 0;
    922  1.6  mycroft #endif
    923  1.6  mycroft 
    924  1.6  mycroft         if (intstat & BRKADRINT) {
    925  1.1  mycroft 		/* We upset the sequencer :-( */
    926  1.1  mycroft 
    927  1.1  mycroft 		/* Lookup the error message */
    928  1.6  mycroft 		int i, error = AHC_INB(ahc, ERROR);
    929  1.1  mycroft 		int num_errors =  sizeof(hard_error)/sizeof(hard_error[0]);
    930  1.6  mycroft 		for(i = 0; error != 1 && i < num_errors; i++)
    931  1.1  mycroft 			error >>= 1;
    932  1.6  mycroft                 panic("%s: brkadrint, %s at seqaddr = 0x%x\n",
    933  1.6  mycroft 		      ahc_name(ahc), hard_error[i].errmesg,
    934  1.6  mycroft 		      (AHC_INB(ahc, SEQADDR1) << 8) |
    935  1.6  mycroft 		      AHC_INB(ahc, SEQADDR0));
    936  1.6  mycroft         }
    937  1.6  mycroft         if (intstat & SEQINT) {
    938  1.6  mycroft 		/*
    939  1.6  mycroft 		 * This code isn't used by the SCB page-in code.  It
    940  1.6  mycroft 		 * should probably be moved to cut out the extra
    941  1.6  mycroft 		 * inb.
    942  1.6  mycroft 		 */
    943  1.6  mycroft 		u_short targ_mask;
    944  1.6  mycroft 		u_char target = (AHC_INB(ahc, SCSIID) >> 4) & 0x0f;
    945  1.6  mycroft 		u_char scratch_offset = target;
    946  1.6  mycroft 		char channel =
    947  1.6  mycroft 			AHC_INB(ahc, SBLKCTL) & SELBUSB ? 'B': 'A';
    948  1.6  mycroft 
    949  1.6  mycroft 		if (channel == 'B')
    950  1.6  mycroft 			scratch_offset += 8;
    951  1.6  mycroft 		targ_mask = (0x01 << scratch_offset);
    952  1.6  mycroft 
    953  1.6  mycroft                 switch (intstat & SEQINT_MASK) {
    954  1.6  mycroft                     case BAD_PHASE:
    955  1.6  mycroft                         panic("%s:%c:%d: unknown scsi bus phase.  "
    956  1.1  mycroft 			      "Attempting to continue\n",
    957  1.6  mycroft 			      ahc_name(ahc), channel, target);
    958  1.6  mycroft                         break;
    959  1.6  mycroft                     case SEND_REJECT:
    960  1.6  mycroft 			{
    961  1.6  mycroft 				u_char rejbyte = AHC_INB(ahc, REJBYTE);
    962  1.6  mycroft 				if(( rejbyte & 0xf0) == 0x20) {
    963  1.6  mycroft 					/* Tagged Message */
    964  1.6  mycroft 					printf("\n%s:%c:%d: Tagged message "
    965  1.6  mycroft 						"received without identify. "
    966  1.6  mycroft 						"Disabling tagged commands "
    967  1.6  mycroft 						"for this target.\n",
    968  1.6  mycroft 						ahc_name(ahc),
    969  1.6  mycroft 					        channel, target);
    970  1.6  mycroft 					ahc->tagenable &= ~targ_mask;
    971  1.6  mycroft 				}
    972  1.6  mycroft 				else
    973  1.6  mycroft 					printf("%s:%c:%d: Warning - "
    974  1.6  mycroft 					       "unknown message recieved from "
    975  1.6  mycroft 					       "target (0x%x - 0x%x).  Rejecting\n",
    976  1.6  mycroft 						ahc_name(ahc), channel, target,
    977  1.6  mycroft 						rejbyte,
    978  1.6  mycroft 					        AHC_INB(ahc, REJBYTE_EXT));
    979  1.6  mycroft 				break;
    980  1.1  mycroft 			}
    981  1.6  mycroft                     case NO_IDENT:
    982  1.6  mycroft                         panic("%s:%c:%d: Target did not send an IDENTIFY "
    983  1.6  mycroft 			      "message. SAVED_TCL == 0x%x\n",
    984  1.6  mycroft                               ahc_name(ahc), channel, target,
    985  1.6  mycroft 			      AHC_INB(ahc, SAVED_TCL));
    986  1.1  mycroft 			break;
    987  1.6  mycroft                     case NO_MATCH:
    988  1.6  mycroft 			if(ahc->flags & AHC_PAGESCBS) {
    989  1.6  mycroft 				/* SCB Page-in request */
    990  1.6  mycroft 				u_char tag;
    991  1.6  mycroft 				u_char next;
    992  1.6  mycroft 				u_char disc_scb;
    993  1.6  mycroft 				struct scb *outscb;
    994  1.6  mycroft 				u_char arg_1 = AHC_INB(ahc, ARG_1);
    995  1.6  mycroft 				if(arg_1 == SCB_LIST_NULL) {
    996  1.6  mycroft 					/* Non-tagged command */
    997  1.6  mycroft 					int index = target |
    998  1.6  mycroft 						(channel == 'B' ? SELBUSB : 0);
    999  1.6  mycroft 					scb = ahc->pagedout_ntscbs[index];
   1000  1.6  mycroft 				}
   1001  1.6  mycroft 				else
   1002  1.6  mycroft 					scb = ahc->scbarray[arg_1];
   1003  1.1  mycroft 
   1004  1.1  mycroft 				/*
   1005  1.6  mycroft 				 * Now to pick the SCB to page out.
   1006  1.6  mycroft 				 * Either take a free SCB, an assigned SCB,
   1007  1.6  mycroft 				 * an SCB that just completed, the first
   1008  1.6  mycroft 				 * one on the disconnected SCB list, or
   1009  1.6  mycroft 				 * as a last resort a queued SCB.
   1010  1.1  mycroft 				 */
   1011  1.6  mycroft 				if(ahc->free_scbs.stqh_first) {
   1012  1.6  mycroft 					outscb = ahc->free_scbs.stqh_first;
   1013  1.6  mycroft 					STAILQ_REMOVE_HEAD(&ahc->free_scbs,
   1014  1.6  mycroft 							   links);
   1015  1.6  mycroft 					scb->position = outscb->position;
   1016  1.6  mycroft 					outscb->position = SCB_LIST_NULL;
   1017  1.6  mycroft 					STAILQ_INSERT_HEAD(&ahc->page_scbs,
   1018  1.6  mycroft 							   outscb, links);
   1019  1.6  mycroft 					AHC_OUTB(ahc, SCBPTR, scb->position);
   1020  1.6  mycroft 					ahc_send_scb(ahc, scb);
   1021  1.6  mycroft 					scb->flags &= ~SCB_PAGED_OUT;
   1022  1.6  mycroft 					goto pagein_done;
   1023  1.6  mycroft 				}
   1024  1.6  mycroft 				if(ahc->assigned_scbs.stqh_first) {
   1025  1.6  mycroft 					outscb = ahc->assigned_scbs.stqh_first;
   1026  1.6  mycroft 					STAILQ_REMOVE_HEAD(&ahc->assigned_scbs,
   1027  1.6  mycroft 							   links);
   1028  1.6  mycroft 					scb->position = outscb->position;
   1029  1.6  mycroft 					outscb->position = SCB_LIST_NULL;
   1030  1.6  mycroft 					STAILQ_INSERT_HEAD(&ahc->waiting_scbs,
   1031  1.6  mycroft 							   outscb, links);
   1032  1.6  mycroft 					outscb->flags = SCB_WAITINGQ;
   1033  1.6  mycroft 					AHC_OUTB(ahc, SCBPTR, scb->position);
   1034  1.6  mycroft 					ahc_send_scb(ahc, scb);
   1035  1.6  mycroft 					scb->flags &= ~SCB_PAGED_OUT;
   1036  1.6  mycroft 					goto pagein_done;
   1037  1.6  mycroft 				}
   1038  1.6  mycroft 				if(intstat & CMDCMPLT) {
   1039  1.6  mycroft 					int   scb_index;
   1040  1.6  mycroft 
   1041  1.6  mycroft 					printf("PIC\n");
   1042  1.6  mycroft 					AHC_OUTB(ahc, CLRINT, CLRCMDINT);
   1043  1.6  mycroft 					scb_index = AHC_INB(ahc, QOUTFIFO);
   1044  1.6  mycroft 					if(!(AHC_INB(ahc, QOUTCNT) & ahc->qcntmask))
   1045  1.6  mycroft 						intstat &= ~CMDCMPLT;
   1046  1.6  mycroft 
   1047  1.6  mycroft 					outscb = ahc->scbarray[scb_index];
   1048  1.6  mycroft 					if (!outscb || !(outscb->flags & SCB_ACTIVE)) {
   1049  1.6  mycroft 						printf("%s: WARNING "
   1050  1.6  mycroft 						       "no command for scb %d (cmdcmplt)\n",
   1051  1.6  mycroft 							ahc_name(ahc),
   1052  1.6  mycroft 						        scb_index);
   1053  1.6  mycroft 						/* Fall through in hopes of finding another SCB */
   1054  1.6  mycroft 					}
   1055  1.6  mycroft 					else {
   1056  1.6  mycroft 						scb->position = outscb->position;
   1057  1.6  mycroft 						outscb->position = SCB_LIST_NULL;
   1058  1.6  mycroft 						AHC_OUTB(ahc, SCBPTR, scb->position);
   1059  1.6  mycroft 						ahc_send_scb(ahc, scb);
   1060  1.6  mycroft 						scb->flags &= ~SCB_PAGED_OUT;
   1061  1.6  mycroft 						untimeout(ahc_timeout, (caddr_t)outscb);
   1062  1.6  mycroft 						ahc_done(ahc, outscb);
   1063  1.6  mycroft 						goto pagein_done;
   1064  1.6  mycroft 					}
   1065  1.6  mycroft 				}
   1066  1.6  mycroft 				disc_scb = AHC_INB(ahc, DISCONNECTED_SCBH);
   1067  1.6  mycroft 				if(disc_scb != SCB_LIST_NULL) {
   1068  1.6  mycroft 					AHC_OUTB(ahc, SCBPTR, disc_scb);
   1069  1.6  mycroft 					tag = AHC_INB(ahc, SCB_TAG);
   1070  1.6  mycroft 					outscb = ahc->scbarray[tag];
   1071  1.6  mycroft 					next = AHC_INB(ahc, SCB_NEXT);
   1072  1.6  mycroft 					if(next != SCB_LIST_NULL) {
   1073  1.6  mycroft 						AHC_OUTB(ahc, SCBPTR, next);
   1074  1.6  mycroft 						AHC_OUTB(ahc, SCB_PREV,
   1075  1.6  mycroft 						     SCB_LIST_NULL);
   1076  1.6  mycroft 						AHC_OUTB(ahc, SCBPTR, disc_scb);
   1077  1.6  mycroft 					}
   1078  1.6  mycroft 					AHC_OUTB(ahc, DISCONNECTED_SCBH, next);
   1079  1.6  mycroft 					ahc_page_scb(ahc, outscb, scb);
   1080  1.6  mycroft 				}
   1081  1.6  mycroft 				else if(AHC_INB(ahc, QINCNT) & ahc->qcntmask) {
   1082  1.6  mycroft 					/* Pull one of our queued commands as a last resort */
   1083  1.6  mycroft 					disc_scb = AHC_INB(ahc, QINFIFO);
   1084  1.6  mycroft 					AHC_OUTB(ahc, SCBPTR, disc_scb);
   1085  1.6  mycroft 					tag = AHC_INB(ahc, SCB_TAG);
   1086  1.6  mycroft 					outscb = ahc->scbarray[tag];
   1087  1.6  mycroft 					if((outscb->control & 0x23) != TAG_ENB) {
   1088  1.6  mycroft 						/*
   1089  1.6  mycroft 						 * This is not a simple tagged command
   1090  1.6  mycroft 						 * so its position in the queue
   1091  1.6  mycroft 						 * matters.  Take the command at the
   1092  1.6  mycroft 						 * end of the queue instead.
   1093  1.6  mycroft 						 */
   1094  1.6  mycroft 						int i;
   1095  1.6  mycroft 						int saved_queue[AHC_SCB_MAX];
   1096  1.6  mycroft 						int queued = AHC_INB(ahc, QINCNT) & ahc->qcntmask;
   1097  1.6  mycroft 
   1098  1.6  mycroft 						/* Count the command we removed already */
   1099  1.6  mycroft 						saved_queue[0] = disc_scb;
   1100  1.6  mycroft 						queued++;
   1101  1.6  mycroft 
   1102  1.6  mycroft 						/* Empty the input queue */
   1103  1.6  mycroft 						for (i = 1; i < queued; i++)
   1104  1.6  mycroft 							saved_queue[i] = AHC_INB(ahc, QINFIFO);
   1105  1.6  mycroft 
   1106  1.6  mycroft 						/* Put everyone back put the last entry */
   1107  1.6  mycroft 						queued--;
   1108  1.6  mycroft 						for (i = 0; i < queued; i++)
   1109  1.6  mycroft 							AHC_OUTB(ahc, QINFIFO, saved_queue[i]);
   1110  1.6  mycroft 
   1111  1.6  mycroft 						AHC_OUTB(ahc, SCBPTR, saved_queue[queued]);
   1112  1.6  mycroft 						tag = AHC_INB(ahc, SCB_TAG);
   1113  1.6  mycroft 						outscb = ahc->scbarray[tag];
   1114  1.6  mycroft 					}
   1115  1.6  mycroft 					untimeout(ahc_timeout, (caddr_t)outscb);
   1116  1.6  mycroft 					scb->position = outscb->position;
   1117  1.6  mycroft 					outscb->position = SCB_LIST_NULL;
   1118  1.6  mycroft 					STAILQ_INSERT_HEAD(&ahc->waiting_scbs,
   1119  1.6  mycroft 							   outscb, links);
   1120  1.6  mycroft 					outscb->flags = SCB_WAITINGQ;
   1121  1.6  mycroft 					ahc_send_scb(ahc, scb);
   1122  1.6  mycroft 					scb->flags &= ~SCB_PAGED_OUT;
   1123  1.6  mycroft 				}
   1124  1.6  mycroft 				else
   1125  1.6  mycroft 					panic("Page-in request with no candidates");
   1126  1.6  mycroft pagein_done:
   1127  1.6  mycroft 				AHC_OUTB(ahc, RETURN_1, SCB_PAGEDIN);
   1128  1.6  mycroft 			}
   1129  1.6  mycroft 			else {
   1130  1.6  mycroft 				printf("%s:%c:%d: no active SCB for "
   1131  1.6  mycroft 				       "reconnecting target - "
   1132  1.6  mycroft 				       "issuing ABORT\n",
   1133  1.6  mycroft 				       ahc_name(ahc), channel, target);
   1134  1.6  mycroft 				printf("SAVED_TCL == 0x%x\n",
   1135  1.6  mycroft 					AHC_INB(ahc, SAVED_TCL));
   1136  1.6  mycroft 				ahc_unbusy_target(ahc, target, channel);
   1137  1.6  mycroft 				AHC_OUTB(ahc, SCB_CONTROL, 0);
   1138  1.6  mycroft 				AHC_OUTB(ahc, CLRSINT1, CLRSELTIMEO);
   1139  1.6  mycroft 				AHC_OUTB(ahc, RETURN_1, 0);
   1140  1.6  mycroft 			}
   1141  1.6  mycroft 			break;
   1142  1.6  mycroft                     case SDTR_MSG:
   1143  1.6  mycroft 			{
   1144  1.6  mycroft 				short period;
   1145  1.6  mycroft 				u_char offset, rate;
   1146  1.6  mycroft 				u_char targ_scratch;
   1147  1.6  mycroft 				u_char maxoffset;
   1148  1.6  mycroft 	                        /*
   1149  1.6  mycroft 				 * Help the sequencer to translate the
   1150  1.6  mycroft 				 * negotiated transfer rate.  Transfer is
   1151  1.6  mycroft 				 * 1/4 the period in ns as is returned by
   1152  1.6  mycroft 				 * the sync negotiation message.  So, we must
   1153  1.6  mycroft 				 * multiply by four
   1154  1.1  mycroft 				 */
   1155  1.6  mycroft 	                        period = AHC_INB(ahc, ARG_1) << 2;
   1156  1.6  mycroft 				offset = AHC_INB(ahc, ACCUM);
   1157  1.6  mycroft 				targ_scratch = AHC_INB(ahc, TARG_SCRATCH
   1158  1.6  mycroft 						   + scratch_offset);
   1159  1.6  mycroft 				if(targ_scratch & WIDEXFER)
   1160  1.6  mycroft 					maxoffset = 0x08;
   1161  1.6  mycroft 				else
   1162  1.6  mycroft 					maxoffset = 0x0f;
   1163  1.6  mycroft 				ahc_scsirate(ahc, &rate, period,
   1164  1.6  mycroft 					     MIN(offset,maxoffset),
   1165  1.6  mycroft 					    target);
   1166  1.6  mycroft 				/* Preserve the WideXfer flag */
   1167  1.6  mycroft 				targ_scratch = rate | (targ_scratch & WIDEXFER);
   1168  1.6  mycroft 				AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
   1169  1.6  mycroft 				     targ_scratch);
   1170  1.6  mycroft 				AHC_OUTB(ahc, SCSIRATE, targ_scratch);
   1171  1.6  mycroft 				if( (targ_scratch & 0x0f) == 0 )
   1172  1.6  mycroft 				{
   1173  1.6  mycroft 					/*
   1174  1.6  mycroft 					 * The requested rate was so low
   1175  1.6  mycroft 					 * that asyncronous transfers are
   1176  1.6  mycroft 					 * faster (not to mention the
   1177  1.6  mycroft 					 * controller won't support them),
   1178  1.6  mycroft 					 * so we issue a message reject to
   1179  1.6  mycroft 					 * ensure we go to asyncronous
   1180  1.6  mycroft 					 * transfers.
   1181  1.6  mycroft 					 */
   1182  1.6  mycroft 					AHC_OUTB(ahc, RETURN_1, SEND_REJ);
   1183  1.6  mycroft 				}
   1184  1.6  mycroft 				/* See if we initiated Sync Negotiation */
   1185  1.6  mycroft 				else if(ahc->sdtrpending & targ_mask)
   1186  1.6  mycroft 				{
   1187  1.6  mycroft 					/*
   1188  1.6  mycroft 					 * Don't send an SDTR back to
   1189  1.6  mycroft 					 * the target
   1190  1.6  mycroft 					 */
   1191  1.6  mycroft 					AHC_OUTB(ahc, RETURN_1, 0);
   1192  1.6  mycroft 				}
   1193  1.6  mycroft 				else{
   1194  1.6  mycroft 					/*
   1195  1.6  mycroft 					 * Send our own SDTR in reply
   1196  1.6  mycroft 					 */
   1197  1.6  mycroft #ifdef AHC_DEBUG
   1198  1.6  mycroft 					if(ahc_debug & AHC_SHOWMISC)
   1199  1.6  mycroft 						printf("Sending SDTR!!\n");
   1200  1.6  mycroft #endif
   1201  1.6  mycroft 					AHC_OUTB(ahc, RETURN_1, SEND_SDTR);
   1202  1.6  mycroft 				}
   1203  1.1  mycroft 				/*
   1204  1.6  mycroft 				 * Negate the flags
   1205  1.1  mycroft 				 */
   1206  1.6  mycroft 				ahc->needsdtr &= ~targ_mask;
   1207  1.6  mycroft 				ahc->sdtrpending &= ~targ_mask;
   1208  1.6  mycroft 	                        break;
   1209  1.1  mycroft 			}
   1210  1.6  mycroft                     case WDTR_MSG:
   1211  1.6  mycroft 			{
   1212  1.6  mycroft 				u_char scratch, bus_width;
   1213  1.1  mycroft 
   1214  1.6  mycroft 				bus_width = AHC_INB(ahc, ARG_1);
   1215  1.1  mycroft 
   1216  1.6  mycroft 				scratch = AHC_INB(ahc, TARG_SCRATCH
   1217  1.6  mycroft 					      + scratch_offset);
   1218  1.1  mycroft 
   1219  1.6  mycroft 				if(ahc->wdtrpending & targ_mask)
   1220  1.6  mycroft 				{
   1221  1.6  mycroft 					/*
   1222  1.6  mycroft 					 * Don't send a WDTR back to the
   1223  1.6  mycroft 					 * target, since we asked first.
   1224  1.6  mycroft 					 */
   1225  1.6  mycroft 					AHC_OUTB(ahc, RETURN_1, 0);
   1226  1.6  mycroft 					switch(bus_width)
   1227  1.6  mycroft 					{
   1228  1.6  mycroft 						case BUS_8_BIT:
   1229  1.6  mycroft 						    scratch &= 0x7f;
   1230  1.6  mycroft 						    break;
   1231  1.6  mycroft 						case BUS_16_BIT:
   1232  1.6  mycroft 						    if(bootverbose)
   1233  1.6  mycroft 		        				printf("%s: target "
   1234  1.6  mycroft 							       "%d using 16Bit "
   1235  1.6  mycroft 							       "transfers\n",
   1236  1.6  mycroft 							       ahc_name(ahc),
   1237  1.6  mycroft 							       target);
   1238  1.6  mycroft 						    scratch |= 0x80;
   1239  1.6  mycroft 						    break;
   1240  1.6  mycroft 						case BUS_32_BIT:
   1241  1.6  mycroft 						    /*
   1242  1.6  mycroft 						     * How can we do 32bit
   1243  1.6  mycroft 						     * transfers on a 16bit
   1244  1.6  mycroft 						     * bus?
   1245  1.6  mycroft 						     */
   1246  1.6  mycroft 						    AHC_OUTB(ahc, RETURN_1,
   1247  1.6  mycroft 							 SEND_REJ);
   1248  1.6  mycroft 		        			    printf("%s: target "
   1249  1.6  mycroft 						           "%d requested 32Bit "
   1250  1.6  mycroft 						           "transfers.  "
   1251  1.6  mycroft 							   "Rejecting...\n",
   1252  1.6  mycroft 							   ahc_name(ahc),
   1253  1.6  mycroft 							   target);
   1254  1.6  mycroft 						    break;
   1255  1.6  mycroft 						default:
   1256  1.6  mycroft 						    break;
   1257  1.6  mycroft 					}
   1258  1.6  mycroft 				}
   1259  1.6  mycroft 				else {
   1260  1.6  mycroft 					/*
   1261  1.6  mycroft 					 * Send our own WDTR in reply
   1262  1.6  mycroft 					 */
   1263  1.6  mycroft 					switch(bus_width)
   1264  1.6  mycroft 					{
   1265  1.6  mycroft 						case BUS_8_BIT:
   1266  1.6  mycroft 							scratch &= 0x7f;
   1267  1.6  mycroft 							break;
   1268  1.6  mycroft 						case BUS_32_BIT:
   1269  1.6  mycroft 						case BUS_16_BIT:
   1270  1.6  mycroft 						    if(ahc->type & AHC_WIDE) {
   1271  1.6  mycroft 							/* Negotiate 16_BITS */
   1272  1.6  mycroft 							bus_width = BUS_16_BIT;
   1273  1.6  mycroft 							if(bootverbose)
   1274  1.6  mycroft 							    printf("%s: "
   1275  1.6  mycroft 								"target %d "
   1276  1.6  mycroft 								"using 16Bit "
   1277  1.6  mycroft 							        "transfers\n",
   1278  1.6  mycroft 								ahc_name(ahc),
   1279  1.6  mycroft 								target);
   1280  1.6  mycroft 						 	scratch |= 0x80;
   1281  1.6  mycroft 						    }
   1282  1.6  mycroft 						    else
   1283  1.6  mycroft 							bus_width = BUS_8_BIT;
   1284  1.6  mycroft 						    break;
   1285  1.6  mycroft 						default:
   1286  1.6  mycroft 						    break;
   1287  1.6  mycroft 					}
   1288  1.6  mycroft 					AHC_OUTB(ahc, RETURN_1,
   1289  1.6  mycroft 						bus_width | SEND_WDTR);
   1290  1.1  mycroft 				}
   1291  1.6  mycroft 				ahc->needwdtr &= ~targ_mask;
   1292  1.6  mycroft 				ahc->wdtrpending &= ~targ_mask;
   1293  1.6  mycroft 				AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
   1294  1.6  mycroft 				     scratch);
   1295  1.6  mycroft 				AHC_OUTB(ahc, SCSIRATE, scratch);
   1296  1.6  mycroft 	                        break;
   1297  1.6  mycroft 			}
   1298  1.6  mycroft 		    case REJECT_MSG:
   1299  1.6  mycroft 			{
   1300  1.1  mycroft 				/*
   1301  1.6  mycroft 				 * What we care about here is if we had an
   1302  1.6  mycroft 				 * outstanding SDTR or WDTR message for this
   1303  1.6  mycroft 				 * target.  If we did, this is a signal that
   1304  1.6  mycroft 				 * the target is refusing negotiation.
   1305  1.1  mycroft 				 */
   1306  1.1  mycroft 
   1307  1.6  mycroft 				u_char targ_scratch;
   1308  1.6  mycroft 
   1309  1.6  mycroft 				targ_scratch = AHC_INB(ahc, TARG_SCRATCH
   1310  1.6  mycroft 						   + scratch_offset);
   1311  1.1  mycroft 
   1312  1.6  mycroft 				if(ahc->wdtrpending & targ_mask){
   1313  1.6  mycroft 					/* note 8bit xfers and clear flag */
   1314  1.6  mycroft 					targ_scratch &= 0x7f;
   1315  1.6  mycroft 					ahc->needwdtr &= ~targ_mask;
   1316  1.6  mycroft 					ahc->wdtrpending &= ~targ_mask;
   1317  1.6  mycroft         				printf("%s:%c:%d: refuses "
   1318  1.6  mycroft 					       "WIDE negotiation.  Using "
   1319  1.6  mycroft 					       "8bit transfers\n",
   1320  1.6  mycroft 						ahc_name(ahc),
   1321  1.6  mycroft 					        channel, target);
   1322  1.6  mycroft 				}
   1323  1.6  mycroft 				else if(ahc->sdtrpending & targ_mask){
   1324  1.6  mycroft 					/* note asynch xfers and clear flag */
   1325  1.6  mycroft 					targ_scratch &= 0xf0;
   1326  1.6  mycroft 					ahc->needsdtr &= ~targ_mask;
   1327  1.6  mycroft 					ahc->sdtrpending &= ~targ_mask;
   1328  1.6  mycroft         				printf("%s:%c:%d: refuses "
   1329  1.6  mycroft 					       "syncronous negotiation.  Using "
   1330  1.6  mycroft 					       "asyncronous transfers\n",
   1331  1.6  mycroft 						ahc_name(ahc),
   1332  1.6  mycroft 					        channel, target);
   1333  1.6  mycroft 				}
   1334  1.6  mycroft 				else {
   1335  1.6  mycroft 					/*
   1336  1.6  mycroft 					 * Otherwise, we ignore it.
   1337  1.6  mycroft 					 */
   1338  1.1  mycroft #ifdef AHC_DEBUG
   1339  1.6  mycroft 					if(ahc_debug & AHC_SHOWMISC)
   1340  1.6  mycroft 						printf("%s:%c:%d: Message "
   1341  1.6  mycroft 						       "reject -- ignored\n",
   1342  1.6  mycroft 							ahc_name(ahc),
   1343  1.6  mycroft 						        channel, target);
   1344  1.1  mycroft #endif
   1345  1.6  mycroft 					break;
   1346  1.6  mycroft 				}
   1347  1.6  mycroft 				AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
   1348  1.6  mycroft 				     targ_scratch);
   1349  1.6  mycroft 				AHC_OUTB(ahc, SCSIRATE, targ_scratch);
   1350  1.1  mycroft 				break;
   1351  1.1  mycroft 			}
   1352  1.6  mycroft                     case BAD_STATUS:
   1353  1.6  mycroft 			{
   1354  1.6  mycroft 			  int	scb_index;
   1355  1.6  mycroft 
   1356  1.6  mycroft 			  /* The sequencer will notify us when a command
   1357  1.6  mycroft 			   * has an error that would be of interest to
   1358  1.6  mycroft 			   * the kernel.  This allows us to leave the sequencer
   1359  1.6  mycroft 			   * running in the common case of command completes
   1360  1.6  mycroft 			   * without error.
   1361  1.6  mycroft 			   */
   1362  1.6  mycroft 
   1363  1.6  mycroft   			  scb_index = AHC_INB(ahc, SCB_TAG);
   1364  1.6  mycroft 			  scb = ahc->scbarray[scb_index];
   1365  1.6  mycroft 
   1366  1.6  mycroft 			  /*
   1367  1.6  mycroft 			   * Set the default return value to 0 (don't
   1368  1.6  mycroft 			   * send sense).  The sense code will change
   1369  1.6  mycroft 			   * this if needed and this reduces code
   1370  1.6  mycroft 			   * duplication.
   1371  1.6  mycroft 			   */
   1372  1.6  mycroft 			  AHC_OUTB(ahc, RETURN_1, 0);
   1373  1.6  mycroft 		 	  if (!(scb && (scb->flags & SCB_ACTIVE))) {
   1374  1.6  mycroft 				printf("%s:%c:%d: ahc_intr - referenced scb "
   1375  1.6  mycroft 				       "not valid during seqint 0x%x scb(%d)\n",
   1376  1.6  mycroft 				       ahc_name(ahc),
   1377  1.6  mycroft 				       channel, target, intstat,
   1378  1.6  mycroft 				       scb_index);
   1379  1.6  mycroft 			      goto clear;
   1380  1.6  mycroft 			  }
   1381  1.1  mycroft 
   1382  1.6  mycroft 			  xs = scb->xs;
   1383  1.1  mycroft 
   1384  1.6  mycroft 			  scb->status = AHC_INB(ahc, SCB_TARGET_STATUS);
   1385  1.1  mycroft 
   1386  1.6  mycroft #ifdef AHC_DEBUG
   1387  1.6  mycroft 			  if((ahc_debug & AHC_SHOWSCBS)
   1388  1.6  mycroft 			    && xs->sc_link->target == DEBUGTARG)
   1389  1.1  mycroft 				ahc_print_scb(scb);
   1390  1.1  mycroft #endif
   1391  1.6  mycroft 			  xs->status = scb->status;
   1392  1.6  mycroft 			  switch(scb->status){
   1393  1.6  mycroft 			    case SCSI_OK:
   1394  1.6  mycroft 				printf("%s: Interrupted for staus of"
   1395  1.6  mycroft 					" 0???\n", ahc_name(ahc));
   1396  1.1  mycroft 				break;
   1397  1.6  mycroft 			    case SCSI_CHECK:
   1398  1.1  mycroft #ifdef AHC_DEBUG
   1399  1.6  mycroft 				if(ahc_debug & AHC_SHOWSENSE)
   1400  1.6  mycroft 				{
   1401  1.6  mycroft 					sc_print_addr(xs->sc_link);
   1402  1.6  mycroft 					printf("requests Check Status\n");
   1403  1.6  mycroft 				}
   1404  1.1  mycroft #endif
   1405  1.1  mycroft 
   1406  1.6  mycroft 				if((xs->error == XS_NOERROR) &&
   1407  1.6  mycroft 				    !(scb->flags & SCB_SENSE)) {
   1408  1.1  mycroft 					struct ahc_dma_seg *sg = scb->ahc_dma;
   1409  1.1  mycroft 					struct scsi_sense *sc = &(scb->sense_cmd);
   1410  1.1  mycroft #ifdef AHC_DEBUG
   1411  1.6  mycroft 					if(ahc_debug & AHC_SHOWSENSE)
   1412  1.6  mycroft 					{
   1413  1.6  mycroft 						sc_print_addr(xs->sc_link);
   1414  1.6  mycroft 						printf("Sending Sense\n");
   1415  1.6  mycroft 					}
   1416  1.1  mycroft #endif
   1417  1.6  mycroft #if defined(__FreeBSD__)
   1418  1.6  mycroft 					sc->op_code = REQUEST_SENSE;
   1419  1.6  mycroft #elif defined(__NetBSD__)
   1420  1.1  mycroft 					sc->opcode = REQUEST_SENSE;
   1421  1.6  mycroft #endif
   1422  1.1  mycroft 					sc->byte2 =  xs->sc_link->lun << 5;
   1423  1.1  mycroft 					sc->length = sizeof(struct scsi_sense_data);
   1424  1.1  mycroft 					sc->control = 0;
   1425  1.1  mycroft 
   1426  1.6  mycroft 					sg->addr = KVTOPHYS(&xs->sense);
   1427  1.6  mycroft 					sg->len = sizeof(struct scsi_sense_data);
   1428  1.1  mycroft 
   1429  1.6  mycroft 					scb->control &= DISCENB;
   1430  1.6  mycroft 					scb->status = 0;
   1431  1.1  mycroft 					scb->SG_segment_count = 1;
   1432  1.6  mycroft 					scb->SG_list_pointer = KVTOPHYS(sg);
   1433  1.6  mycroft 			                scb->data = sg->addr;
   1434  1.6  mycroft 					scb->datalen = sg->len;
   1435  1.6  mycroft #ifdef AHC_BROKEN_CACHE
   1436  1.6  mycroft 					if (ahc_broken_cache)
   1437  1.6  mycroft 						INVALIDATE_CACHE();
   1438  1.6  mycroft #endif
   1439  1.6  mycroft 					scb->cmdpointer = KVTOPHYS(sc);
   1440  1.1  mycroft 					scb->cmdlen = sizeof(*sc);
   1441  1.1  mycroft 
   1442  1.6  mycroft 					scb->flags |= SCB_SENSE;
   1443  1.6  mycroft 					ahc_send_scb(ahc, scb);
   1444  1.6  mycroft 					/*
   1445  1.6  mycroft 					 * Ensure that the target is "BUSY"
   1446  1.6  mycroft 					 * so we don't get overlapping
   1447  1.6  mycroft 					 * commands if we happen to be doing
   1448  1.6  mycroft 					 * tagged I/O.
   1449  1.6  mycroft 					 */
   1450  1.6  mycroft 					ahc_busy_target(ahc, target, channel);
   1451  1.1  mycroft 
   1452  1.1  mycroft 					/*
   1453  1.6  mycroft 					 * Make us the next command to run
   1454  1.1  mycroft 					 */
   1455  1.6  mycroft 					ahc_add_waiting_scb(ahc, scb);
   1456  1.6  mycroft 					AHC_OUTB(ahc, RETURN_1, SEND_SENSE);
   1457  1.1  mycroft 					break;
   1458  1.1  mycroft 				}
   1459  1.1  mycroft 				/*
   1460  1.6  mycroft 				 * Clear the SCB_SENSE Flag and have
   1461  1.6  mycroft 				 * the sequencer do a normal command
   1462  1.1  mycroft 				 * complete with either a "DRIVER_STUFFUP"
   1463  1.1  mycroft 				 * error or whatever other error condition
   1464  1.1  mycroft 				 * we already had.
   1465  1.1  mycroft 				 */
   1466  1.6  mycroft 				scb->flags &= ~SCB_SENSE;
   1467  1.6  mycroft 				if(xs->error == XS_NOERROR)
   1468  1.1  mycroft 					xs->error = XS_DRIVER_STUFFUP;
   1469  1.1  mycroft 				break;
   1470  1.6  mycroft 			    case SCSI_BUSY:
   1471  1.6  mycroft 				xs->error = XS_BUSY;
   1472  1.1  mycroft 				sc_print_addr(xs->sc_link);
   1473  1.1  mycroft 				printf("Target Busy\n");
   1474  1.1  mycroft 				break;
   1475  1.6  mycroft #if defined(__FreeBSD__)
   1476  1.6  mycroft 			    case SCSI_QUEUE_FULL:
   1477  1.1  mycroft 				/*
   1478  1.1  mycroft 				 * The upper level SCSI code will eventually
   1479  1.1  mycroft 				 * handle this properly.
   1480  1.1  mycroft 				 */
   1481  1.1  mycroft 				sc_print_addr(xs->sc_link);
   1482  1.1  mycroft 				printf("Queue Full\n");
   1483  1.6  mycroft 				scb->flags = SCB_ASSIGNEDQ;
   1484  1.6  mycroft 				STAILQ_INSERT_TAIL(&ahc->assigned_scbs,
   1485  1.6  mycroft 						   scb, links);
   1486  1.1  mycroft 				break;
   1487  1.6  mycroft #elif defined(__NetBSD__)
   1488  1.6  mycroft 				 /*
   1489  1.6  mycroft 				  * XXX -
   1490  1.6  mycroft 				  *	Do we need to handle this ?
   1491  1.6  mycroft 				  *	But FreeBSD MI SCSI code seems to
   1492  1.6  mycroft 				  *	do nothing about this.
   1493  1.6  mycroft 				  */
   1494  1.1  mycroft #endif
   1495  1.6  mycroft 			    default:
   1496  1.1  mycroft 				sc_print_addr(xs->sc_link);
   1497  1.1  mycroft 				printf("unexpected targ_status: %x\n",
   1498  1.6  mycroft 					scb->status);
   1499  1.1  mycroft 				xs->error = XS_DRIVER_STUFFUP;
   1500  1.1  mycroft 				break;
   1501  1.1  mycroft 			}
   1502  1.1  mycroft 			break;
   1503  1.6  mycroft 		  }
   1504  1.6  mycroft 		  case RESIDUAL:
   1505  1.6  mycroft 		  {
   1506  1.6  mycroft 			int   scb_index;
   1507  1.6  mycroft 			scb_index = AHC_INB(ahc, SCB_TAG);
   1508  1.1  mycroft 			scb = ahc->scbarray[scb_index];
   1509  1.6  mycroft 			xs = scb->xs;
   1510  1.1  mycroft 			/*
   1511  1.1  mycroft 			 * Don't clobber valid resid info with
   1512  1.1  mycroft 			 * a resid coming from a check sense
   1513  1.1  mycroft 			 * operation.
   1514  1.1  mycroft 			 */
   1515  1.6  mycroft 			if(!(scb->flags & SCB_SENSE)) {
   1516  1.6  mycroft 				int resid_sgs;
   1517  1.6  mycroft 
   1518  1.6  mycroft 				/*
   1519  1.6  mycroft 				 * Remainder of the SG where the transfer
   1520  1.6  mycroft 				 * stopped.
   1521  1.6  mycroft 				 */
   1522  1.6  mycroft 				xs->resid =
   1523  1.6  mycroft 					(AHC_INB(ahc, SCB_RESID_DCNT2)<<16) |
   1524  1.6  mycroft 					(AHC_INB(ahc, SCB_RESID_DCNT1)<<8)  |
   1525  1.6  mycroft 					 AHC_INB(ahc, SCB_RESID_DCNT0);
   1526  1.6  mycroft 
   1527  1.6  mycroft 				/*
   1528  1.6  mycroft 				 * Add up the contents of all residual
   1529  1.6  mycroft 				 * SG segments that are after the SG where
   1530  1.6  mycroft 				 * the transfer stopped.
   1531  1.6  mycroft 				 */
   1532  1.6  mycroft 				resid_sgs = AHC_INB(ahc, SCB_RESID_SGCNT) - 1;
   1533  1.6  mycroft 				while(resid_sgs > 0) {
   1534  1.6  mycroft 					int sg;
   1535  1.6  mycroft 
   1536  1.6  mycroft 					sg = scb->SG_segment_count - resid_sgs;
   1537  1.6  mycroft 					xs->resid += scb->ahc_dma[sg].len;
   1538  1.6  mycroft 					resid_sgs--;
   1539  1.6  mycroft 				}
   1540  1.6  mycroft 
   1541  1.6  mycroft #if defined(__FreeBSD__)
   1542  1.6  mycroft 				xs->flags |= SCSI_RESID_VALID;
   1543  1.6  mycroft #elif defined(__NetBSD__)
   1544  1.6  mycroft 				/* XXX - Update to do this right */
   1545  1.6  mycroft #endif
   1546  1.6  mycroft #ifdef AHC_DEBUG
   1547  1.6  mycroft 				if(ahc_debug & AHC_SHOWMISC) {
   1548  1.6  mycroft 					sc_print_addr(xs->sc_link);
   1549  1.6  mycroft 					printf("Handled Residual of %ld bytes\n"
   1550  1.6  mycroft 						,xs->resid);
   1551  1.6  mycroft 				}
   1552  1.1  mycroft #endif
   1553  1.6  mycroft 			}
   1554  1.1  mycroft 			break;
   1555  1.6  mycroft 		  }
   1556  1.6  mycroft 		  case ABORT_TAG:
   1557  1.6  mycroft 		  {
   1558  1.6  mycroft 			int   scb_index;
   1559  1.6  mycroft 			scb_index = AHC_INB(ahc, SCB_TAG);
   1560  1.1  mycroft 			scb = ahc->scbarray[scb_index];
   1561  1.6  mycroft 			xs = scb->xs;
   1562  1.1  mycroft 			/*
   1563  1.1  mycroft 			 * We didn't recieve a valid tag back from
   1564  1.1  mycroft 			 * the target on a reconnect.
   1565  1.1  mycroft 			 */
   1566  1.1  mycroft 			sc_print_addr(xs->sc_link);
   1567  1.6  mycroft 			printf("invalid tag recieved -- sending ABORT_TAG\n");
   1568  1.6  mycroft 			xs->error = XS_DRIVER_STUFFUP;
   1569  1.6  mycroft 			untimeout(ahc_timeout, (caddr_t)scb);
   1570  1.1  mycroft 			ahc_done(ahc, scb);
   1571  1.1  mycroft 			break;
   1572  1.6  mycroft 		  }
   1573  1.6  mycroft 		  case AWAITING_MSG:
   1574  1.6  mycroft 		  {
   1575  1.6  mycroft 			int   scb_index;
   1576  1.6  mycroft 			scb_index = AHC_INB(ahc, SCB_TAG);
   1577  1.6  mycroft 			scb = ahc->scbarray[scb_index];
   1578  1.6  mycroft 			/*
   1579  1.6  mycroft 			 * This SCB had a zero length command, informing
   1580  1.6  mycroft 			 * the sequencer that we wanted to send a special
   1581  1.6  mycroft 			 * message to this target.  We only do this for
   1582  1.6  mycroft 			 * BUS_DEVICE_RESET messages currently.
   1583  1.6  mycroft 			 */
   1584  1.6  mycroft 			if(scb->flags & SCB_DEVICE_RESET)
   1585  1.6  mycroft 			{
   1586  1.6  mycroft 				AHC_OUTB(ahc, MSG0,
   1587  1.6  mycroft 					MSG_BUS_DEVICE_RESET);
   1588  1.6  mycroft 				AHC_OUTB(ahc, MSG_LEN, 1);
   1589  1.6  mycroft 				printf("Bus Device Reset Message Sent\n");
   1590  1.6  mycroft 			}
   1591  1.6  mycroft 			else
   1592  1.6  mycroft 				panic("ahc_intr: AWAITING_MSG for an SCB that "
   1593  1.6  mycroft 					"does not have a waiting message");
   1594  1.6  mycroft 			break;
   1595  1.6  mycroft 		  }
   1596  1.6  mycroft 		  case IMMEDDONE:
   1597  1.6  mycroft 		  {
   1598  1.6  mycroft 			/*
   1599  1.6  mycroft 			 * Take care of device reset messages
   1600  1.6  mycroft 			 */
   1601  1.6  mycroft 			u_char scbindex = AHC_INB(ahc, SCB_TAG);
   1602  1.6  mycroft 			scb = ahc->scbarray[scbindex];
   1603  1.6  mycroft 			if(scb->flags & SCB_DEVICE_RESET) {
   1604  1.6  mycroft 				u_char targ_scratch;
   1605  1.6  mycroft 				int found;
   1606  1.6  mycroft 				/*
   1607  1.6  mycroft 				 * Go back to async/narrow transfers and
   1608  1.6  mycroft 				 * renegotiate.
   1609  1.6  mycroft 				 */
   1610  1.6  mycroft 				ahc_unbusy_target(ahc, target, channel);
   1611  1.6  mycroft 				ahc->needsdtr |= ahc->needsdtr_orig & targ_mask;
   1612  1.6  mycroft 				ahc->needwdtr |= ahc->needwdtr_orig & targ_mask;
   1613  1.6  mycroft 				ahc->sdtrpending &= ~targ_mask;
   1614  1.6  mycroft 				ahc->wdtrpending &= ~targ_mask;
   1615  1.6  mycroft 				targ_scratch = AHC_INB(ahc, TARG_SCRATCH
   1616  1.6  mycroft 							+ scratch_offset);
   1617  1.6  mycroft 				targ_scratch &= SXFR;
   1618  1.6  mycroft 				AHC_OUTB(ahc, TARG_SCRATCH + scratch_offset,
   1619  1.6  mycroft 					targ_scratch);
   1620  1.6  mycroft 				found = ahc_reset_device(ahc, target,
   1621  1.6  mycroft 						channel, SCB_LIST_NULL,
   1622  1.6  mycroft 						XS_NOERROR);
   1623  1.6  mycroft 				sc_print_addr(scb->xs->sc_link);
   1624  1.6  mycroft 				printf("Bus Device Reset delivered. "
   1625  1.6  mycroft 					"%d SCBs aborted\n", found);
   1626  1.6  mycroft 				ahc->in_timeout = FALSE;
   1627  1.6  mycroft 				ahc_run_done_queue(ahc);
   1628  1.6  mycroft 			}
   1629  1.6  mycroft 			else
   1630  1.6  mycroft 				panic("ahc_intr: Immediate complete for "
   1631  1.6  mycroft 				      "unknown operation.");
   1632  1.6  mycroft 			break;
   1633  1.6  mycroft 		  }
   1634  1.6  mycroft #if NOT_YET
   1635  1.6  mycroft 		  /* XXX Fill these in later */
   1636  1.6  mycroft 		  case MESG_BUFFER_BUSY:
   1637  1.6  mycroft 			break;
   1638  1.6  mycroft 		  case MSGIN_PHASEMIS:
   1639  1.6  mycroft 			break;
   1640  1.6  mycroft #endif
   1641  1.6  mycroft 		  default:
   1642  1.6  mycroft 			printf("ahc_intr: seqint, "
   1643  1.6  mycroft 			       "intstat == 0x%x, scsisigi = 0x%x\n",
   1644  1.6  mycroft 			       intstat, AHC_INB(ahc, SCSISIGI));
   1645  1.1  mycroft 			break;
   1646  1.1  mycroft 		}
   1647  1.6  mycroft clear:
   1648  1.1  mycroft 		/*
   1649  1.1  mycroft 		 * Clear the upper byte that holds SEQINT status
   1650  1.1  mycroft 		 * codes and clear the SEQINT bit.
   1651  1.1  mycroft 		 */
   1652  1.6  mycroft 		AHC_OUTB(ahc, CLRINT, CLRSEQINT);
   1653  1.1  mycroft 
   1654  1.1  mycroft 		/*
   1655  1.1  mycroft 		 *  The sequencer is paused immediately on
   1656  1.1  mycroft 		 *  a SEQINT, so we should restart it when
   1657  1.1  mycroft 		 *  we leave this section.
   1658  1.1  mycroft 		 */
   1659  1.1  mycroft 		UNPAUSE_SEQUENCER(ahc);
   1660  1.6  mycroft 	   }
   1661  1.6  mycroft 
   1662  1.6  mycroft 
   1663  1.6  mycroft 	   if (intstat & SCSIINT) {
   1664  1.6  mycroft 
   1665  1.6  mycroft 		int scb_index = AHC_INB(ahc, SCB_TAG);
   1666  1.6  mycroft 		status = AHC_INB(ahc, SSTAT1);
   1667  1.6  mycroft 
   1668  1.1  mycroft 		scb = ahc->scbarray[scb_index];
   1669  1.6  mycroft 		if (scb != NULL) /* XXX - is this case exist ? */
   1670  1.6  mycroft 			xs = scb->xs;
   1671  1.1  mycroft 
   1672  1.6  mycroft 		if (status & SCSIRSTI) {
   1673  1.6  mycroft 			char channel;
   1674  1.6  mycroft 			channel = AHC_INB(ahc, SBLKCTL);
   1675  1.6  mycroft 			channel = channel & SELBUSB ? 'B' : 'A';
   1676  1.6  mycroft 			printf("%s: Someone reset channel %c\n",
   1677  1.6  mycroft 				ahc_name(ahc), channel);
   1678  1.6  mycroft 			ahc_reset_channel(ahc,
   1679  1.6  mycroft 					  channel,
   1680  1.6  mycroft 					  SCB_LIST_NULL,
   1681  1.6  mycroft 					  XS_BUSY,
   1682  1.6  mycroft 					  /* Initiate Reset */FALSE);
   1683  1.6  mycroft 			scb = NULL;
   1684  1.6  mycroft 		}
   1685  1.6  mycroft 		else if (!(scb && (scb->flags & SCB_ACTIVE))){
   1686  1.6  mycroft 			printf("%s: ahc_intr - referenced scb not "
   1687  1.1  mycroft 			       "valid during scsiint 0x%x scb(%d)\n",
   1688  1.6  mycroft 				ahc_name(ahc), status, scb_index);
   1689  1.6  mycroft 			AHC_OUTB(ahc, CLRSINT1, status);
   1690  1.1  mycroft 			UNPAUSE_SEQUENCER(ahc);
   1691  1.6  mycroft 			AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   1692  1.1  mycroft 			scb = NULL;
   1693  1.1  mycroft 		}
   1694  1.6  mycroft 		else if (status & SCSIPERR) {
   1695  1.6  mycroft 			/*
   1696  1.6  mycroft 			 * Determine the bus phase and
   1697  1.6  mycroft 			 * queue an appropriate message
   1698  1.6  mycroft 			 */
   1699  1.6  mycroft 			char	*phase;
   1700  1.6  mycroft 			u_char	mesg_out = MSG_NOP;
   1701  1.6  mycroft 			u_char	lastphase = AHC_INB(ahc, LASTPHASE);
   1702  1.1  mycroft 
   1703  1.6  mycroft 			sc_print_addr(xs->sc_link);
   1704  1.6  mycroft 
   1705  1.6  mycroft 			switch(lastphase) {
   1706  1.6  mycroft 				case P_DATAOUT:
   1707  1.6  mycroft 					phase = "Data-Out";
   1708  1.6  mycroft 					break;
   1709  1.6  mycroft 				case P_DATAIN:
   1710  1.6  mycroft 					phase = "Data-In";
   1711  1.6  mycroft 					mesg_out = MSG_INITIATOR_DET_ERROR;
   1712  1.6  mycroft 					break;
   1713  1.6  mycroft 				case P_COMMAND:
   1714  1.6  mycroft 					phase = "Command";
   1715  1.6  mycroft 					break;
   1716  1.6  mycroft 				case P_MESGOUT:
   1717  1.6  mycroft 					phase = "Message-Out";
   1718  1.6  mycroft 					break;
   1719  1.6  mycroft 				case P_STATUS:
   1720  1.6  mycroft 					phase = "Status";
   1721  1.6  mycroft 					mesg_out = MSG_INITIATOR_DET_ERROR;
   1722  1.6  mycroft 					break;
   1723  1.6  mycroft 				case P_MESGIN:
   1724  1.6  mycroft 					phase = "Message-In";
   1725  1.6  mycroft 					mesg_out = MSG_MSG_PARITY_ERROR;
   1726  1.6  mycroft 					break;
   1727  1.6  mycroft 				default:
   1728  1.6  mycroft 					phase = "unknown";
   1729  1.6  mycroft 					break;
   1730  1.6  mycroft 			}
   1731  1.6  mycroft                         printf("parity error during %s phase.\n", phase);
   1732  1.1  mycroft 
   1733  1.6  mycroft 			/*
   1734  1.6  mycroft 			 * We've set the hardware to assert ATN if we
   1735  1.6  mycroft 			 * get a parity error on "in" phases, so all we
   1736  1.6  mycroft 			 * need to do is stuff the message buffer with
   1737  1.6  mycroft 			 * the appropriate message.  In phases have set
   1738  1.6  mycroft 			 * mesg_out to something other than MSG_NOP.
   1739  1.6  mycroft 			 */
   1740  1.6  mycroft 			if(mesg_out != MSG_NOP) {
   1741  1.6  mycroft 				AHC_OUTB(ahc, MSG0, mesg_out);
   1742  1.6  mycroft 				AHC_OUTB(ahc, MSG_LEN, 1);
   1743  1.6  mycroft 			}
   1744  1.6  mycroft 			else
   1745  1.6  mycroft 				/*
   1746  1.6  mycroft 				 * Should we allow the target to make
   1747  1.6  mycroft 				 * this decision for us?
   1748  1.6  mycroft 				 */
   1749  1.6  mycroft 				xs->error = XS_DRIVER_STUFFUP;
   1750  1.6  mycroft 		}
   1751  1.6  mycroft 		else if (status & SELTO) {
   1752  1.1  mycroft 			u_char waiting;
   1753  1.1  mycroft 			u_char flags;
   1754  1.6  mycroft                         xs->error = XS_SELTIMEOUT;
   1755  1.1  mycroft 			/*
   1756  1.1  mycroft 			 * Clear any pending messages for the timed out
   1757  1.1  mycroft 			 * target, and mark the target as free
   1758  1.1  mycroft 			 */
   1759  1.6  mycroft 			flags = AHC_INB(ahc, FLAGS);
   1760  1.6  mycroft 			AHC_OUTB(ahc, MSG_LEN, 0);
   1761  1.6  mycroft 			ahc_unbusy_target(ahc, xs->sc_link->target,
   1762  1.6  mycroft #if defined(__FreeBSD__)
   1763  1.6  mycroft 			 	((long)xs->sc_link->fordriver & SELBUSB)
   1764  1.6  mycroft #elif defined(__NetBSD__)
   1765  1.6  mycroft 				IS_SCSIBUS_B(ahc, xs->sc_link)
   1766  1.6  mycroft #endif
   1767  1.6  mycroft 				 	? 'B' : 'A');
   1768  1.1  mycroft 
   1769  1.6  mycroft 			AHC_OUTB(ahc, SCB_CONTROL, 0);
   1770  1.1  mycroft 
   1771  1.6  mycroft 			AHC_OUTB(ahc, CLRSINT1, CLRSELTIMEO);
   1772  1.1  mycroft 
   1773  1.6  mycroft 			AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   1774  1.1  mycroft 
   1775  1.1  mycroft 			/* Shift the waiting for selection queue forward */
   1776  1.6  mycroft 			waiting = AHC_INB(ahc, WAITING_SCBH);
   1777  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, waiting);
   1778  1.6  mycroft 			waiting = AHC_INB(ahc, SCB_NEXT);
   1779  1.6  mycroft 			AHC_OUTB(ahc, WAITING_SCBH, waiting);
   1780  1.1  mycroft 
   1781  1.1  mycroft 			RESTART_SEQUENCER(ahc);
   1782  1.6  mycroft 		}
   1783  1.6  mycroft 		else if (!(status & BUSFREE)) {
   1784  1.6  mycroft 		      sc_print_addr(xs->sc_link);
   1785  1.6  mycroft 		      printf("Unknown SCSIINT. Status = 0x%x\n", status);
   1786  1.6  mycroft 		      AHC_OUTB(ahc, CLRSINT1, status);
   1787  1.6  mycroft 		      UNPAUSE_SEQUENCER(ahc);
   1788  1.6  mycroft 		      AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   1789  1.6  mycroft 		      scb = NULL;
   1790  1.6  mycroft 		}
   1791  1.6  mycroft 		if(scb != NULL) {
   1792  1.6  mycroft 		    /* We want to process the command */
   1793  1.6  mycroft 		    untimeout(ahc_timeout, (caddr_t)scb);
   1794  1.6  mycroft 		    ahc_done(ahc, scb);
   1795  1.1  mycroft 		}
   1796  1.6  mycroft 	}
   1797  1.6  mycroft 	if (intstat & CMDCMPLT) {
   1798  1.6  mycroft 		int   scb_index;
   1799  1.1  mycroft 
   1800  1.6  mycroft 		do {
   1801  1.6  mycroft 			scb_index = AHC_INB(ahc, QOUTFIFO);
   1802  1.6  mycroft 			scb = ahc->scbarray[scb_index];
   1803  1.6  mycroft 			if (!scb || !(scb->flags & SCB_ACTIVE)) {
   1804  1.6  mycroft 				printf("%s: WARNING "
   1805  1.6  mycroft 				       "no command for scb %d (cmdcmplt)\n"
   1806  1.6  mycroft 				       "QOUTCNT == %d\n",
   1807  1.6  mycroft 					ahc_name(ahc), scb_index,
   1808  1.6  mycroft 					AHC_INB(ahc, QOUTCNT));
   1809  1.6  mycroft 				AHC_OUTB(ahc, CLRINT, CLRCMDINT);
   1810  1.6  mycroft 				continue;
   1811  1.6  mycroft 			}
   1812  1.6  mycroft 			AHC_OUTB(ahc, CLRINT, CLRCMDINT);
   1813  1.6  mycroft 			untimeout(ahc_timeout, (caddr_t)scb);
   1814  1.6  mycroft 			ahc_done(ahc, scb);
   1815  1.1  mycroft 
   1816  1.6  mycroft 		} while (AHC_INB(ahc, QOUTCNT) & ahc->qcntmask);
   1817  1.1  mycroft 
   1818  1.6  mycroft 		ahc_run_waiting_queues(ahc);
   1819  1.6  mycroft 	}
   1820  1.6  mycroft #if defined(__NetBSD__)
   1821  1.6  mycroft 	return 1;
   1822  1.1  mycroft #endif
   1823  1.1  mycroft }
   1824  1.1  mycroft 
   1825  1.1  mycroft /*
   1826  1.1  mycroft  * We have a scb which has been processed by the
   1827  1.1  mycroft  * adaptor, now we look to see how the operation
   1828  1.1  mycroft  * went.
   1829  1.1  mycroft  */
   1830  1.6  mycroft static void
   1831  1.1  mycroft ahc_done(ahc, scb)
   1832  1.6  mycroft 	struct ahc_data *ahc;
   1833  1.6  mycroft 	struct scb *scb;
   1834  1.1  mycroft {
   1835  1.1  mycroft 	struct scsi_xfer *xs = scb->xs;
   1836  1.6  mycroft 
   1837  1.6  mycroft 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_done\n"));
   1838  1.1  mycroft 	/*
   1839  1.1  mycroft 	 * Put the results of the operation
   1840  1.1  mycroft 	 * into the xfer and call whoever started it
   1841  1.1  mycroft 	 */
   1842  1.6  mycroft #if defined(__NetBSD__)
   1843  1.6  mycroft 	if (xs->error != XS_NOERROR) {
   1844  1.6  mycroft 		/* Don't override the error value. */
   1845  1.6  mycroft 	} else if (scb->flags & SCB_ABORTED) {
   1846  1.6  mycroft 		xs->error = XS_DRIVER_STUFFUP;
   1847  1.6  mycroft 	} else
   1848  1.6  mycroft #endif
   1849  1.6  mycroft 	if(scb->flags & SCB_SENSE)
   1850  1.6  mycroft 		xs->error = XS_SENSE;
   1851  1.6  mycroft 	if(scb->flags & SCB_SENTORDEREDTAG)
   1852  1.6  mycroft 		ahc->in_timeout = FALSE;
   1853  1.6  mycroft #if defined(__FreeBSD__)
   1854  1.6  mycroft 	if ((xs->flags & SCSI_ERR_OK) && !(xs->error == XS_SENSE)) {
   1855  1.6  mycroft 		/* All went correctly  OR errors expected */
   1856  1.6  mycroft 		xs->error = XS_NOERROR;
   1857  1.1  mycroft 	}
   1858  1.6  mycroft #elif defined(__NetBSD__)
   1859  1.6  mycroft 	/*
   1860  1.6  mycroft 	 * Since NetBSD doesn't have error ignoring operation mode
   1861  1.6  mycroft 	 * (SCSI_ERR_OK in FreeBSD), we don't have to care this case.
   1862  1.6  mycroft 	 */
   1863  1.6  mycroft #endif
   1864  1.1  mycroft 	xs->flags |= ITSDONE;
   1865  1.1  mycroft #ifdef AHC_TAGENABLE
   1866  1.6  mycroft 	if(xs->cmd->opcode == INQUIRY && xs->error == XS_NOERROR)
   1867  1.6  mycroft 	{
   1868  1.1  mycroft 		struct scsi_inquiry_data *inq_data;
   1869  1.1  mycroft 		u_short mask = 0x01 << (xs->sc_link->target |
   1870  1.6  mycroft 				(scb->tcl & 0x08));
   1871  1.1  mycroft 		/*
   1872  1.1  mycroft 		 * Sneak a look at the results of the SCSI Inquiry
   1873  1.6  mycroft 		 * command and see if we can do Tagged queing.  This
   1874  1.1  mycroft 		 * should really be done by the higher level drivers.
   1875  1.1  mycroft 		 */
   1876  1.1  mycroft 		inq_data = (struct scsi_inquiry_data *)xs->data;
   1877  1.6  mycroft 		if((inq_data->flags & SID_CmdQue) && !(ahc->tagenable & mask))
   1878  1.6  mycroft 		{
   1879  1.6  mycroft 		        printf("%s: target %d Tagged Queuing Device\n",
   1880  1.6  mycroft 				ahc_name(ahc), xs->sc_link->target);
   1881  1.1  mycroft 			ahc->tagenable |= mask;
   1882  1.6  mycroft 			if(ahc->maxhscbs >= 16 || (ahc->flags & AHC_PAGESCBS)) {
   1883  1.6  mycroft 				/* Default to 8 tags */
   1884  1.6  mycroft 				xs->sc_link->opennings += 6;
   1885  1.6  mycroft 			}
   1886  1.6  mycroft 			else
   1887  1.6  mycroft 			{
   1888  1.6  mycroft 				/*
   1889  1.6  mycroft 				 * Default to 4 tags on whimpy
   1890  1.6  mycroft 				 * cards that don't have much SCB
   1891  1.6  mycroft 				 * space and can't page.  This prevents
   1892  1.6  mycroft 				 * a single device from hogging all
   1893  1.6  mycroft 				 * slots.  We should really have a better
   1894  1.6  mycroft 				 * way of providing fairness.
   1895  1.6  mycroft 				 */
   1896  1.6  mycroft 				xs->sc_link->opennings += 2;
   1897  1.6  mycroft 			}
   1898  1.1  mycroft 		}
   1899  1.1  mycroft 	}
   1900  1.1  mycroft #endif
   1901  1.1  mycroft 	ahc_free_scb(ahc, scb, xs->flags);
   1902  1.1  mycroft 	scsi_done(xs);
   1903  1.1  mycroft }
   1904  1.1  mycroft 
   1905  1.1  mycroft /*
   1906  1.1  mycroft  * Start the board, ready for normal operation
   1907  1.1  mycroft  */
   1908  1.1  mycroft int
   1909  1.1  mycroft ahc_init(ahc)
   1910  1.6  mycroft 	struct  ahc_data *ahc;
   1911  1.1  mycroft {
   1912  1.6  mycroft 	u_char	scsi_conf, sblkctl, i;
   1913  1.6  mycroft 	int     max_targ = 15;
   1914  1.1  mycroft 	/*
   1915  1.6  mycroft 	 * Assume we have a board at this stage and it has been reset.
   1916  1.1  mycroft 	 */
   1917  1.1  mycroft 
   1918  1.6  mycroft 	/* Handle the SCBPAGING option */
   1919  1.6  mycroft #ifndef AHC_SCBPAGING_ENABLE
   1920  1.6  mycroft 	ahc->flags &= ~AHC_PAGESCBS;
   1921  1.6  mycroft #endif
   1922  1.6  mycroft 
   1923  1.6  mycroft 	/* Determine channel configuration and who we are on the scsi bus. */
   1924  1.6  mycroft 	switch ( (sblkctl = AHC_INB(ahc, SBLKCTL) & 0x0a) ) {
   1925  1.6  mycroft 	    case 0:
   1926  1.6  mycroft 		ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
   1927  1.6  mycroft 		ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
   1928  1.6  mycroft 		if(ahc->type == AHC_394)
   1929  1.6  mycroft 			printf("Channel %c, SCSI Id=%d, ",
   1930  1.6  mycroft 				ahc->flags & AHC_CHNLB ? 'B' : 'A',
   1931  1.6  mycroft 				ahc->our_id);
   1932  1.6  mycroft 		else
   1933  1.6  mycroft 			printf("Single Channel, SCSI Id=%d, ", ahc->our_id);
   1934  1.6  mycroft 		AHC_OUTB(ahc, FLAGS, SINGLE_BUS | (ahc->flags & AHC_PAGESCBS));
   1935  1.1  mycroft 		break;
   1936  1.6  mycroft 	    case 2:
   1937  1.6  mycroft 		ahc->our_id = (AHC_INB(ahc, SCSICONF + 1) & HWSCSIID);
   1938  1.6  mycroft 		ahc->flags &= ~AHC_CHANNEL_B_PRIMARY;
   1939  1.6  mycroft 		if(ahc->type == AHC_394)
   1940  1.6  mycroft 			printf("Wide Channel %c, SCSI Id=%d, ",
   1941  1.6  mycroft 				ahc->flags & AHC_CHNLB ? 'B' : 'A',
   1942  1.6  mycroft 				ahc->our_id);
   1943  1.1  mycroft 		else
   1944  1.6  mycroft 			printf("Wide Channel, SCSI Id=%d, ", ahc->our_id);
   1945  1.1  mycroft 		ahc->type |= AHC_WIDE;
   1946  1.6  mycroft 		AHC_OUTB(ahc, FLAGS, WIDE_BUS | (ahc->flags & AHC_PAGESCBS));
   1947  1.1  mycroft 		break;
   1948  1.6  mycroft 	    case 8:
   1949  1.6  mycroft 		ahc->our_id = (AHC_INB(ahc, SCSICONF) & HSCSIID);
   1950  1.6  mycroft 		ahc->our_id_b = (AHC_INB(ahc, SCSICONF + 1) & HSCSIID);
   1951  1.1  mycroft 		printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, ",
   1952  1.6  mycroft 			ahc->our_id, ahc->our_id_b);
   1953  1.1  mycroft 		ahc->type |= AHC_TWIN;
   1954  1.6  mycroft 		AHC_OUTB(ahc, FLAGS, TWIN_BUS | (ahc->flags & AHC_PAGESCBS));
   1955  1.1  mycroft 		break;
   1956  1.6  mycroft 	    default:
   1957  1.6  mycroft 		printf(" Unsupported adapter type.  Ignoring\n");
   1958  1.1  mycroft 		return(-1);
   1959  1.1  mycroft 	}
   1960  1.1  mycroft 
   1961  1.6  mycroft 	/* Determine the number of SCBs */
   1962  1.1  mycroft 
   1963  1.6  mycroft 	{
   1964  1.6  mycroft 		AHC_OUTB(ahc, SCBPTR, 0);
   1965  1.6  mycroft 		AHC_OUTB(ahc, SCB_CONTROL, 0);
   1966  1.6  mycroft 		for(i = 1; i < AHC_SCB_MAX; i++) {
   1967  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, i);
   1968  1.6  mycroft 			AHC_OUTB(ahc, SCB_CONTROL, i);
   1969  1.6  mycroft 			if(AHC_INB(ahc, SCB_CONTROL) != i)
   1970  1.6  mycroft 				break;
   1971  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, 0);
   1972  1.6  mycroft 			if(AHC_INB(ahc, SCB_CONTROL) != 0)
   1973  1.6  mycroft 				break;
   1974  1.6  mycroft 			/* Clear the control byte. */
   1975  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, i);
   1976  1.6  mycroft 			AHC_OUTB(ahc, SCB_CONTROL, 0);
   1977  1.1  mycroft 
   1978  1.6  mycroft 			ahc->qcntmask |= i;     /* Update the count mask. */
   1979  1.1  mycroft 		}
   1980  1.6  mycroft 
   1981  1.6  mycroft 		/* Ensure we clear the 0 SCB's control byte. */
   1982  1.6  mycroft 		AHC_OUTB(ahc, SCBPTR, 0);
   1983  1.6  mycroft 		AHC_OUTB(ahc, SCB_CONTROL, 0);
   1984  1.6  mycroft 
   1985  1.6  mycroft 		ahc->qcntmask |= i;
   1986  1.6  mycroft 		ahc->maxhscbs = i;
   1987  1.6  mycroft 	}
   1988  1.6  mycroft 
   1989  1.6  mycroft 	if((ahc->maxhscbs < AHC_SCB_MAX) && (ahc->flags & AHC_PAGESCBS))
   1990  1.6  mycroft 		ahc->maxscbs = AHC_SCB_MAX;
   1991  1.6  mycroft 	else {
   1992  1.6  mycroft 		ahc->maxscbs = ahc->maxhscbs;
   1993  1.6  mycroft 		ahc->flags &= ~AHC_PAGESCBS;
   1994  1.1  mycroft 	}
   1995  1.6  mycroft 
   1996  1.6  mycroft 	printf("%d SCBs\n", ahc->maxhscbs);
   1997  1.6  mycroft 
   1998  1.6  mycroft #ifdef AHC_DEBUG
   1999  1.6  mycroft 	if(ahc_debug & AHC_SHOWMISC) {
   2000  1.6  mycroft 		struct scb	test;
   2001  1.6  mycroft 		printf("%s: hardware scb %ld bytes; kernel scb; "
   2002  1.6  mycroft 		       "ahc_dma %d bytes\n",
   2003  1.6  mycroft 			ahc_name(ahc),
   2004  1.6  mycroft 		        (u_long)&(test.next) - (u_long)(&test),
   2005  1.6  mycroft 			sizeof(test),
   2006  1.6  mycroft 			sizeof(struct ahc_dma_seg));
   2007  1.6  mycroft 	}
   2008  1.6  mycroft #endif /* AHC_DEBUG */
   2009  1.6  mycroft 
   2010  1.6  mycroft 	/* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
   2011  1.6  mycroft 	if(ahc->type & AHC_TWIN)
   2012  1.6  mycroft 	{
   2013  1.1  mycroft 		/*
   2014  1.1  mycroft 		 * The device is gated to channel B after a chip reset,
   2015  1.1  mycroft 		 * so set those values first
   2016  1.1  mycroft 		 */
   2017  1.6  mycroft 		AHC_OUTB(ahc, SCSIID, ahc->our_id_b);
   2018  1.6  mycroft 		scsi_conf = AHC_INB(ahc, SCSICONF + 1);
   2019  1.6  mycroft 		AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
   2020  1.6  mycroft 					| ENSTIMER|ACTNEGEN|STPWEN);
   2021  1.6  mycroft 		AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
   2022  1.6  mycroft 		if(ahc->type & AHC_ULTRA)
   2023  1.6  mycroft 			AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
   2024  1.6  mycroft 		else
   2025  1.6  mycroft 			AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
   2026  1.6  mycroft 
   2027  1.6  mycroft 		if(scsi_conf & RESET_SCSI) {
   2028  1.6  mycroft 			/* Reset the bus */
   2029  1.6  mycroft 			if(bootverbose)
   2030  1.6  mycroft 				printf("%s: Reseting Channel B\n",
   2031  1.6  mycroft 				       ahc_name(ahc));
   2032  1.6  mycroft 			AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
   2033  1.6  mycroft 			DELAY(1000);
   2034  1.6  mycroft 			AHC_OUTB(ahc, SCSISEQ, 0);
   2035  1.6  mycroft 
   2036  1.6  mycroft 			/* Ensure we don't get a RSTI interrupt from this */
   2037  1.6  mycroft 			AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
   2038  1.6  mycroft 			AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   2039  1.6  mycroft 		}
   2040  1.6  mycroft 
   2041  1.1  mycroft 		/* Select Channel A */
   2042  1.6  mycroft 		AHC_OUTB(ahc, SBLKCTL, 0);
   2043  1.6  mycroft 	}
   2044  1.6  mycroft 	AHC_OUTB(ahc, SCSIID, ahc->our_id);
   2045  1.6  mycroft 	scsi_conf = AHC_INB(ahc, SCSICONF);
   2046  1.6  mycroft 	AHC_OUTB(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
   2047  1.6  mycroft 				| ENSTIMER|ACTNEGEN|STPWEN);
   2048  1.6  mycroft 	AHC_OUTB(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
   2049  1.6  mycroft 	if(ahc->type & AHC_ULTRA)
   2050  1.6  mycroft 		AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN|ULTRAEN);
   2051  1.6  mycroft 	else
   2052  1.6  mycroft 		AHC_OUTB(ahc, SXFRCTL0, DFON|SPIOEN);
   2053  1.6  mycroft 
   2054  1.6  mycroft 	if(scsi_conf & RESET_SCSI) {
   2055  1.6  mycroft 		/* Reset the bus */
   2056  1.6  mycroft 		if(bootverbose)
   2057  1.6  mycroft 			printf("%s: Reseting Channel A\n", ahc_name(ahc));
   2058  1.6  mycroft 
   2059  1.6  mycroft 		AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
   2060  1.6  mycroft 		DELAY(1000);
   2061  1.6  mycroft 		AHC_OUTB(ahc, SCSISEQ, 0);
   2062  1.6  mycroft 
   2063  1.6  mycroft 		/* Ensure we don't get a RSTI interrupt from this */
   2064  1.6  mycroft 		AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI);
   2065  1.6  mycroft 		AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   2066  1.1  mycroft 	}
   2067  1.6  mycroft 
   2068  1.1  mycroft 	/*
   2069  1.1  mycroft 	 * Look at the information that board initialization or
   2070  1.1  mycroft 	 * the board bios has left us.  In the lower four bits of each
   2071  1.1  mycroft 	 * target's scratch space any value other than 0 indicates
   2072  1.1  mycroft 	 * that we should initiate syncronous transfers.  If it's zero,
   2073  1.1  mycroft 	 * the user or the BIOS has decided to disable syncronous
   2074  1.6  mycroft 	 * negotiation to that target so we don't activate the needsdtr
   2075  1.1  mycroft 	 * flag.
   2076  1.1  mycroft 	 */
   2077  1.1  mycroft 	ahc->needsdtr_orig = 0;
   2078  1.1  mycroft 	ahc->needwdtr_orig = 0;
   2079  1.1  mycroft 
   2080  1.6  mycroft 	/* Grab the disconnection disable table and invert it for our needs */
   2081  1.6  mycroft 	if(ahc->flags & AHC_USEDEFAULTS) {
   2082  1.6  mycroft 		printf("%s: Host Adapter Bios disabled.  Using default SCSI "
   2083  1.6  mycroft 			"device parameters\n", ahc_name(ahc));
   2084  1.6  mycroft 		ahc->discenable = 0xff;
   2085  1.6  mycroft 	}
   2086  1.6  mycroft 	else
   2087  1.6  mycroft 		ahc->discenable = ~((AHC_INB(ahc, DISC_DSB + 1) << 8)
   2088  1.6  mycroft 				   | AHC_INB(ahc, DISC_DSB));
   2089  1.6  mycroft 
   2090  1.6  mycroft 	if(!(ahc->type & (AHC_WIDE|AHC_TWIN)))
   2091  1.6  mycroft 		max_targ = 7;
   2092  1.6  mycroft 
   2093  1.6  mycroft 	for(i = 0; i <= max_targ; i++){
   2094  1.6  mycroft 		u_char target_settings;
   2095  1.6  mycroft 		if (ahc->flags & AHC_USEDEFAULTS) {
   2096  1.6  mycroft 			target_settings = 0; /* 10MHz */
   2097  1.1  mycroft 			ahc->needsdtr_orig |= (0x01 << i);
   2098  1.6  mycroft 			ahc->needwdtr_orig |= (0x01 << i);
   2099  1.1  mycroft 		}
   2100  1.6  mycroft 		else {
   2101  1.6  mycroft 			/* Take the settings leftover in scratch RAM. */
   2102  1.6  mycroft 			target_settings = AHC_INB(ahc, TARG_SCRATCH + i);
   2103  1.6  mycroft 
   2104  1.6  mycroft 			if(target_settings & 0x0f){
   2105  1.6  mycroft 				ahc->needsdtr_orig |= (0x01 << i);
   2106  1.6  mycroft 				/*Default to a asyncronous transfers(0 offset)*/
   2107  1.6  mycroft 				target_settings &= 0xf0;
   2108  1.6  mycroft 			}
   2109  1.6  mycroft 			if(target_settings & 0x80){
   2110  1.6  mycroft 				ahc->needwdtr_orig |= (0x01 << i);
   2111  1.6  mycroft 				/*
   2112  1.6  mycroft 				 * We'll set the Wide flag when we
   2113  1.6  mycroft 				 * are successful with Wide negotiation.
   2114  1.6  mycroft 				 * Turn it off for now so we aren't
   2115  1.6  mycroft 				 * confused.
   2116  1.6  mycroft 				 */
   2117  1.6  mycroft 				target_settings &= 0x7f;
   2118  1.6  mycroft 			}
   2119  1.1  mycroft 		}
   2120  1.6  mycroft 		AHC_OUTB(ahc, TARG_SCRATCH+i,target_settings);
   2121  1.1  mycroft 	}
   2122  1.1  mycroft 	/*
   2123  1.1  mycroft 	 * If we are not a WIDE device, forget WDTR.  This
   2124  1.1  mycroft 	 * makes the driver work on some cards that don't
   2125  1.1  mycroft 	 * leave these fields cleared when the BIOS is not
   2126  1.1  mycroft 	 * installed.
   2127  1.1  mycroft 	 */
   2128  1.6  mycroft 	if(!(ahc->type & AHC_WIDE))
   2129  1.1  mycroft 		ahc->needwdtr_orig = 0;
   2130  1.1  mycroft 	ahc->needsdtr = ahc->needsdtr_orig;
   2131  1.1  mycroft 	ahc->needwdtr = ahc->needwdtr_orig;
   2132  1.1  mycroft 	ahc->sdtrpending = 0;
   2133  1.1  mycroft 	ahc->wdtrpending = 0;
   2134  1.1  mycroft 	ahc->tagenable = 0;
   2135  1.6  mycroft 	ahc->orderedtag = 0;
   2136  1.6  mycroft 
   2137  1.6  mycroft #ifdef AHC_DEBUG
   2138  1.6  mycroft 	/* How did we do? */
   2139  1.6  mycroft 	if(ahc_debug & AHC_SHOWMISC)
   2140  1.6  mycroft 		printf("NEEDSDTR == 0x%x\nNEEDWDTR == 0x%x\n"
   2141  1.6  mycroft 			"DISCENABLE == 0x%x\n", ahc->needsdtr,
   2142  1.6  mycroft 			ahc->needwdtr, ahc->discenable);
   2143  1.6  mycroft #endif
   2144  1.1  mycroft 	/*
   2145  1.6  mycroft 	 * Set the number of availible SCBs
   2146  1.1  mycroft 	 */
   2147  1.6  mycroft 	AHC_OUTB(ahc, SCBCOUNT, ahc->maxhscbs);
   2148  1.1  mycroft 
   2149  1.6  mycroft 	/*
   2150  1.6  mycroft 	 * 2's compliment of maximum tag value
   2151  1.6  mycroft 	 */
   2152  1.6  mycroft 	i = ahc->maxscbs;
   2153  1.6  mycroft 	AHC_OUTB(ahc, COMP_SCBCOUNT, -i & 0xff);
   2154  1.1  mycroft 
   2155  1.1  mycroft 	/*
   2156  1.6  mycroft 	 * QCount mask to deal with broken aic7850s that
   2157  1.6  mycroft 	 * sporatically get garbage in the upper bits of
   2158  1.6  mycroft 	 * their QCount registers.
   2159  1.1  mycroft 	 */
   2160  1.6  mycroft 	AHC_OUTB(ahc, QCNTMASK, ahc->qcntmask);
   2161  1.1  mycroft 
   2162  1.1  mycroft 	/* We don't have any busy targets right now */
   2163  1.6  mycroft 	AHC_OUTB(ahc, ACTIVE_A, 0);
   2164  1.6  mycroft 	AHC_OUTB(ahc, ACTIVE_B, 0);
   2165  1.6  mycroft 
   2166  1.1  mycroft 	/* We don't have any waiting selections */
   2167  1.6  mycroft 	AHC_OUTB(ahc, WAITING_SCBH, SCB_LIST_NULL);
   2168  1.6  mycroft 
   2169  1.6  mycroft 	/* Our disconnection list is empty too */
   2170  1.6  mycroft 	AHC_OUTB(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
   2171  1.6  mycroft 
   2172  1.6  mycroft 	/* Message out buffer starts empty */
   2173  1.6  mycroft 	AHC_OUTB(ahc, MSG_LEN, 0x00);
   2174  1.6  mycroft 
   2175  1.6  mycroft 	/*
   2176  1.6  mycroft 	 * Load the Sequencer program and Enable the adapter
   2177  1.6  mycroft 	 * in "fast" mode.
   2178  1.6  mycroft          */
   2179  1.6  mycroft 	if(bootverbose)
   2180  1.6  mycroft 		printf("%s: Downloading Sequencer Program...",
   2181  1.6  mycroft 		       ahc_name(ahc));
   2182  1.6  mycroft 
   2183  1.6  mycroft 	ahc_loadseq(ahc);
   2184  1.6  mycroft 
   2185  1.6  mycroft 	if(bootverbose)
   2186  1.6  mycroft 		printf("Done\n");
   2187  1.6  mycroft 
   2188  1.6  mycroft 	AHC_OUTB(ahc, SEQCTL, FASTMODE);
   2189  1.6  mycroft 
   2190  1.6  mycroft 	UNPAUSE_SEQUENCER(ahc);
   2191  1.6  mycroft 
   2192  1.1  mycroft 	/*
   2193  1.6  mycroft 	 * Note that we are going and return (to probe)
   2194  1.1  mycroft 	 */
   2195  1.6  mycroft 	ahc->flags |= AHC_INIT;
   2196  1.1  mycroft 	return (0);
   2197  1.1  mycroft }
   2198  1.1  mycroft 
   2199  1.6  mycroft static void
   2200  1.1  mycroft ahcminphys(bp)
   2201  1.6  mycroft         struct buf *bp;
   2202  1.1  mycroft {
   2203  1.6  mycroft /*
   2204  1.6  mycroft  * Even though the card can transfer up to 16megs per command
   2205  1.6  mycroft  * we are limited by the number of segments in the dma segment
   2206  1.6  mycroft  * list that we can hold.  The worst case is that all pages are
   2207  1.6  mycroft  * discontinuous physically, hense the "page per segment" limit
   2208  1.6  mycroft  * enforced here.
   2209  1.6  mycroft  */
   2210  1.6  mycroft         if (bp->b_bcount > ((AHC_NSEG - 1) * PAGESIZ)) {
   2211  1.6  mycroft                 bp->b_bcount = ((AHC_NSEG - 1) * PAGESIZ);
   2212  1.6  mycroft         }
   2213  1.6  mycroft #if defined(__NetBSD__)
   2214  1.1  mycroft 	minphys(bp);
   2215  1.6  mycroft #endif
   2216  1.1  mycroft }
   2217  1.1  mycroft 
   2218  1.1  mycroft /*
   2219  1.1  mycroft  * start a scsi operation given the command and
   2220  1.1  mycroft  * the data address, target, and lun all of which
   2221  1.1  mycroft  * are stored in the scsi_xfer struct
   2222  1.1  mycroft  */
   2223  1.6  mycroft static int32_t
   2224  1.1  mycroft ahc_scsi_cmd(xs)
   2225  1.6  mycroft         struct scsi_xfer *xs;
   2226  1.1  mycroft {
   2227  1.6  mycroft         struct	scb *scb;
   2228  1.6  mycroft         struct	ahc_dma_seg *sg;
   2229  1.6  mycroft         int     seg;            /* scatter gather seg being worked on */
   2230  1.6  mycroft         int     thiskv;
   2231  1.6  mycroft         physaddr thisphys, nextphys;
   2232  1.6  mycroft         int     bytes_this_seg, bytes_this_page, datalen, flags;
   2233  1.6  mycroft         struct	ahc_data *ahc;
   2234  1.6  mycroft 	u_short	mask;
   2235  1.6  mycroft         int     s;
   2236  1.6  mycroft 
   2237  1.6  mycroft 	ahc = (struct ahc_data *)xs->sc_link->adapter_softc;
   2238  1.6  mycroft 	mask  = (0x01 << (xs->sc_link->target
   2239  1.6  mycroft #if defined(__FreeBSD__)
   2240  1.6  mycroft 				| ((u_long)xs->sc_link->fordriver & 0x08)));
   2241  1.6  mycroft #elif defined(__NetBSD__)
   2242  1.6  mycroft 			| (IS_SCSIBUS_B(ahc, xs->sc_link) ? SELBUSB : 0) ));
   2243  1.6  mycroft #endif
   2244  1.6  mycroft         SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahc_scsi_cmd\n"));
   2245  1.6  mycroft         /*
   2246  1.6  mycroft          * get an scb to use. If the transfer
   2247  1.6  mycroft          * is from a buf (possibly from interrupt time)
   2248  1.6  mycroft          * then we can't allow it to sleep
   2249  1.6  mycroft          */
   2250  1.6  mycroft         flags = xs->flags;
   2251  1.6  mycroft         if (flags & ITSDONE) {
   2252  1.6  mycroft                 printf("%s: Already done?", ahc_name(ahc));
   2253  1.6  mycroft                 xs->flags &= ~ITSDONE;
   2254  1.6  mycroft         }
   2255  1.6  mycroft         if (!(flags & INUSE)) {
   2256  1.6  mycroft                 printf("%s: Not in use?", ahc_name(ahc));
   2257  1.6  mycroft                 xs->flags |= INUSE;
   2258  1.6  mycroft         }
   2259  1.6  mycroft         if (!(scb = ahc_get_scb(ahc, flags))) {
   2260  1.6  mycroft                 xs->error = XS_DRIVER_STUFFUP;
   2261  1.6  mycroft                 return (TRY_AGAIN_LATER);
   2262  1.6  mycroft         }
   2263  1.6  mycroft         SC_DEBUG(xs->sc_link, SDEV_DB3, ("start scb(%p)\n", scb));
   2264  1.6  mycroft         scb->xs = xs;
   2265  1.6  mycroft         if (flags & SCSI_RESET)
   2266  1.6  mycroft 		scb->flags |= SCB_DEVICE_RESET|SCB_IMMED;
   2267  1.6  mycroft         /*
   2268  1.6  mycroft          * Put all the arguments for the xfer in the scb
   2269  1.6  mycroft          */
   2270  1.6  mycroft 
   2271  1.6  mycroft 	if(ahc->tagenable & mask) {
   2272  1.6  mycroft 		scb->control |= TAG_ENB;
   2273  1.6  mycroft 		if(ahc->orderedtag & mask) {
   2274  1.6  mycroft 			printf("Ordered Tag sent\n");
   2275  1.6  mycroft 			scb->control |= 0x02;
   2276  1.6  mycroft 			ahc->orderedtag &= ~mask;
   2277  1.6  mycroft 		}
   2278  1.6  mycroft 	}
   2279  1.6  mycroft 	if(ahc->discenable & mask)
   2280  1.6  mycroft 		scb->control |= DISCENB;
   2281  1.6  mycroft 	if((ahc->needwdtr & mask) && !(ahc->wdtrpending & mask))
   2282  1.6  mycroft 	{
   2283  1.6  mycroft 		scb->control |= NEEDWDTR;
   2284  1.1  mycroft 		ahc->wdtrpending |= mask;
   2285  1.1  mycroft 	}
   2286  1.6  mycroft 	else if((ahc->needsdtr & mask) && !(ahc->sdtrpending & mask))
   2287  1.6  mycroft 	{
   2288  1.6  mycroft 		scb->control |= NEEDSDTR;
   2289  1.1  mycroft 		ahc->sdtrpending |= mask;
   2290  1.1  mycroft 	}
   2291  1.6  mycroft 	scb->tcl = ((xs->sc_link->target << 4) & 0xF0) |
   2292  1.6  mycroft #if defined(__FreeBSD__)
   2293  1.6  mycroft 				  ((u_long)xs->sc_link->fordriver & 0x08) |
   2294  1.6  mycroft #elif defined(__NetBSD__)
   2295  1.6  mycroft 				  (IS_SCSIBUS_B(ahc,xs->sc_link)? SELBUSB : 0)|
   2296  1.6  mycroft #endif
   2297  1.6  mycroft 				  (xs->sc_link->lun & 0x07);
   2298  1.1  mycroft 	scb->cmdlen = xs->cmdlen;
   2299  1.6  mycroft 	scb->cmdpointer = KVTOPHYS(xs->cmd);
   2300  1.1  mycroft 	xs->resid = 0;
   2301  1.6  mycroft 	xs->status = 0;
   2302  1.6  mycroft 	if (xs->datalen) {      /* should use S/G only if not zero length */
   2303  1.6  mycroft 		scb->SG_list_pointer = KVTOPHYS(scb->ahc_dma);
   2304  1.1  mycroft 		sg = scb->ahc_dma;
   2305  1.1  mycroft 		seg = 0;
   2306  1.6  mycroft 		/*
   2307  1.6  mycroft 		 * Set up the scatter gather block
   2308  1.6  mycroft 		 */
   2309  1.6  mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB4,
   2310  1.6  mycroft 			 ("%ld @%p:- ", xs->datalen, xs->data));
   2311  1.6  mycroft 		datalen = xs->datalen;
   2312  1.6  mycroft 		thiskv = (int) xs->data;
   2313  1.6  mycroft 		thisphys = KVTOPHYS(thiskv);
   2314  1.6  mycroft 
   2315  1.6  mycroft 		while ((datalen) && (seg < AHC_NSEG)) {
   2316  1.6  mycroft 			bytes_this_seg = 0;
   2317  1.6  mycroft 
   2318  1.6  mycroft 			/* put in the base address */
   2319  1.6  mycroft 			sg->addr = thisphys;
   2320  1.6  mycroft 
   2321  1.6  mycroft 			SC_DEBUGN(xs->sc_link, SDEV_DB4, ("0x%lx", thisphys));
   2322  1.6  mycroft 
   2323  1.6  mycroft 			/* do it at least once */
   2324  1.6  mycroft 			nextphys = thisphys;
   2325  1.6  mycroft 			while ((datalen) && (thisphys == nextphys)) {
   2326  1.1  mycroft 				/*
   2327  1.6  mycroft 				 * This page is contiguous (physically)
   2328  1.6  mycroft 				 * with the the last, just extend the
   2329  1.6  mycroft 				 * length
   2330  1.1  mycroft 				 */
   2331  1.6  mycroft 				/* how far to the end of the page */
   2332  1.6  mycroft 				nextphys = (thisphys & (~(PAGESIZ - 1)))
   2333  1.6  mycroft 					   + PAGESIZ;
   2334  1.6  mycroft 				bytes_this_page = nextphys - thisphys;
   2335  1.6  mycroft 				/**** or the data ****/
   2336  1.6  mycroft 				bytes_this_page = min(bytes_this_page ,datalen);
   2337  1.6  mycroft 				bytes_this_seg += bytes_this_page;
   2338  1.6  mycroft 				datalen -= bytes_this_page;
   2339  1.6  mycroft 
   2340  1.6  mycroft 				/* get more ready for the next page */
   2341  1.6  mycroft 				thiskv = (thiskv & (~(PAGESIZ - 1)))
   2342  1.6  mycroft 					 + PAGESIZ;
   2343  1.6  mycroft 				if (datalen)
   2344  1.6  mycroft 					thisphys = KVTOPHYS(thiskv);
   2345  1.1  mycroft 			}
   2346  1.6  mycroft 			/*
   2347  1.6  mycroft 			 * next page isn't contiguous, finish the seg
   2348  1.6  mycroft 			 */
   2349  1.6  mycroft 			SC_DEBUGN(xs->sc_link, SDEV_DB4,
   2350  1.6  mycroft 					("(0x%x)", bytes_this_seg));
   2351  1.6  mycroft 			sg->len = bytes_this_seg;
   2352  1.6  mycroft 			sg++;
   2353  1.6  mycroft 			seg++;
   2354  1.1  mycroft 		}
   2355  1.1  mycroft 		scb->SG_segment_count = seg;
   2356  1.6  mycroft 
   2357  1.6  mycroft 		/* Copy the first SG into the data pointer area */
   2358  1.6  mycroft 		scb->data = scb->ahc_dma->addr;
   2359  1.6  mycroft 		scb->datalen = scb->ahc_dma->len;
   2360  1.6  mycroft 		SC_DEBUGN(xs->sc_link, SDEV_DB4, ("\n"));
   2361  1.6  mycroft 		if (datalen) {
   2362  1.6  mycroft 			/* there's still data, must have run out of segs! */
   2363  1.6  mycroft 			printf("%s: ahc_scsi_cmd: more than %d DMA segs\n",
   2364  1.6  mycroft 				ahc_name(ahc), AHC_NSEG);
   2365  1.1  mycroft 			xs->error = XS_DRIVER_STUFFUP;
   2366  1.1  mycroft 			ahc_free_scb(ahc, scb, flags);
   2367  1.1  mycroft 			return (COMPLETE);
   2368  1.1  mycroft 		}
   2369  1.6  mycroft #ifdef AHC_BROKEN_CACHE
   2370  1.6  mycroft 		if (ahc_broken_cache)
   2371  1.6  mycroft 			INVALIDATE_CACHE();
   2372  1.6  mycroft #endif
   2373  1.6  mycroft 	}
   2374  1.6  mycroft 	else {
   2375  1.6  mycroft 		/*
   2376  1.6  mycroft 		 * No data xfer, use non S/G values
   2377  1.6  mycroft 	 	 */
   2378  1.1  mycroft 		scb->SG_segment_count = 0;
   2379  1.6  mycroft 		scb->SG_list_pointer = 0;
   2380  1.6  mycroft 		scb->data = 0;
   2381  1.6  mycroft 		scb->datalen = 0;
   2382  1.1  mycroft 	}
   2383  1.1  mycroft 
   2384  1.6  mycroft #ifdef AHC_DEBUG
   2385  1.6  mycroft 	if((ahc_debug & AHC_SHOWSCBS) && (xs->sc_link->target == DEBUGTARG))
   2386  1.1  mycroft 		ahc_print_scb(scb);
   2387  1.1  mycroft #endif
   2388  1.1  mycroft 	s = splbio();
   2389  1.1  mycroft 
   2390  1.6  mycroft 	if( scb->position != SCB_LIST_NULL )
   2391  1.6  mycroft 	{
   2392  1.6  mycroft 		/* We already have a valid slot */
   2393  1.6  mycroft 		u_char curscb;
   2394  1.1  mycroft 
   2395  1.6  mycroft 		PAUSE_SEQUENCER(ahc);
   2396  1.6  mycroft 		curscb = AHC_INB(ahc, SCBPTR);
   2397  1.6  mycroft 		AHC_OUTB(ahc, SCBPTR, scb->position);
   2398  1.6  mycroft 		ahc_send_scb(ahc, scb);
   2399  1.6  mycroft 		AHC_OUTB(ahc, SCBPTR, curscb);
   2400  1.6  mycroft 		AHC_OUTB(ahc, QINFIFO, scb->position);
   2401  1.6  mycroft 		UNPAUSE_SEQUENCER(ahc);
   2402  1.6  mycroft 		scb->flags = SCB_ACTIVE;
   2403  1.6  mycroft 		if (!(flags & SCSI_NOMASK)) {
   2404  1.6  mycroft 			timeout(ahc_timeout, (caddr_t)scb,
   2405  1.6  mycroft 				(xs->timeout * hz) / 1000);
   2406  1.6  mycroft 		}
   2407  1.6  mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_sent\n"));
   2408  1.6  mycroft 	}
   2409  1.6  mycroft 	else {
   2410  1.6  mycroft 		scb->flags = SCB_WAITINGQ;
   2411  1.6  mycroft 		STAILQ_INSERT_TAIL(&ahc->waiting_scbs, scb, links);
   2412  1.6  mycroft 		ahc_run_waiting_queues(ahc);
   2413  1.6  mycroft 	}
   2414  1.6  mycroft 	if (!(flags & SCSI_NOMASK)) {
   2415  1.1  mycroft 		splx(s);
   2416  1.1  mycroft 		return (SUCCESSFULLY_QUEUED);
   2417  1.1  mycroft 	}
   2418  1.1  mycroft 	/*
   2419  1.6  mycroft 	 * If we can't use interrupts, poll for completion
   2420  1.1  mycroft 	 */
   2421  1.6  mycroft 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_poll\n"));
   2422  1.6  mycroft 	do {
   2423  1.6  mycroft 		if (ahc_poll(ahc, xs->timeout)) {
   2424  1.6  mycroft 			if (!(xs->flags & SCSI_SILENT))
   2425  1.6  mycroft 				printf("cmd fail\n");
   2426  1.1  mycroft 			ahc_timeout(scb);
   2427  1.6  mycroft 			break;
   2428  1.6  mycroft 		}
   2429  1.6  mycroft 	} while (!(xs->flags & ITSDONE));  /* a non command complete intr */
   2430  1.6  mycroft 	splx(s);
   2431  1.1  mycroft 	return (COMPLETE);
   2432  1.1  mycroft }
   2433  1.1  mycroft 
   2434  1.1  mycroft 
   2435  1.1  mycroft /*
   2436  1.1  mycroft  * A scb (and hence an scb entry on the board is put onto the
   2437  1.1  mycroft  * free list.
   2438  1.1  mycroft  */
   2439  1.6  mycroft static void
   2440  1.1  mycroft ahc_free_scb(ahc, scb, flags)
   2441  1.6  mycroft         struct	ahc_data *ahc;
   2442  1.6  mycroft         int     flags;
   2443  1.6  mycroft         struct  scb *scb;
   2444  1.1  mycroft {
   2445  1.6  mycroft 	struct scb *wscb;
   2446  1.6  mycroft 	unsigned int opri;
   2447  1.1  mycroft 
   2448  1.6  mycroft 	opri = splbio();
   2449  1.1  mycroft 
   2450  1.1  mycroft 	scb->flags = SCB_FREE;
   2451  1.6  mycroft 	if(scb->position == SCB_LIST_NULL) {
   2452  1.6  mycroft 		STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
   2453  1.6  mycroft 		if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
   2454  1.6  mycroft 			/*
   2455  1.6  mycroft 			 * If there were no SCBs availible, wake anybody waiting
   2456  1.6  mycroft 			 * for one to come free.
   2457  1.6  mycroft 			 */
   2458  1.6  mycroft 			wakeup((caddr_t)&ahc->free_scbs);
   2459  1.6  mycroft 	}
   2460  1.6  mycroft 	/*
   2461  1.6  mycroft 	 * If there are any SCBS on the waiting queue,
   2462  1.6  mycroft 	 * assign the slot of this "freed" SCB to the first
   2463  1.6  mycroft 	 * one.  We'll run the waiting queues after all command
   2464  1.6  mycroft 	 * completes for a particular interrupt are completed
   2465  1.6  mycroft 	 * or when we start another command.
   2466  1.6  mycroft 	 */
   2467  1.6  mycroft 	else if((wscb = ahc->waiting_scbs.stqh_first) != NULL) {
   2468  1.6  mycroft 		wscb->position = scb->position;
   2469  1.6  mycroft 		STAILQ_REMOVE_HEAD(&ahc->waiting_scbs, links);
   2470  1.6  mycroft 		STAILQ_INSERT_HEAD(&ahc->assigned_scbs, wscb, links);
   2471  1.6  mycroft 		wscb->flags = SCB_ASSIGNEDQ;
   2472  1.6  mycroft 
   2473  1.6  mycroft 		/*
   2474  1.6  mycroft 		 * The "freed" SCB will need to be assigned a slot
   2475  1.6  mycroft 		 * before being used, so put it in the page_scbs
   2476  1.6  mycroft 		 * queue.
   2477  1.6  mycroft 		 */
   2478  1.6  mycroft 		scb->position = SCB_LIST_NULL;
   2479  1.6  mycroft 		STAILQ_INSERT_HEAD(&ahc->page_scbs, scb, links);
   2480  1.6  mycroft 		if(!scb->links.stqe_next && !ahc->free_scbs.stqh_first)
   2481  1.6  mycroft 			/*
   2482  1.6  mycroft 			 * If there were no SCBs availible, wake anybody waiting
   2483  1.6  mycroft 			 * for one to come free.
   2484  1.6  mycroft 			 */
   2485  1.6  mycroft 			wakeup((caddr_t)&ahc->free_scbs);
   2486  1.6  mycroft 	}
   2487  1.6  mycroft 	else {
   2488  1.6  mycroft 		STAILQ_INSERT_HEAD(&ahc->free_scbs, scb, links);
   2489  1.1  mycroft #ifdef AHC_DEBUG
   2490  1.6  mycroft 		ahc->activescbs--;
   2491  1.1  mycroft #endif
   2492  1.6  mycroft 		if(!scb->links.stqe_next && !ahc->page_scbs.stqh_first)
   2493  1.6  mycroft 			/*
   2494  1.6  mycroft 			 * If there were no SCBs availible, wake anybody waiting
   2495  1.6  mycroft 			 * for one to come free.
   2496  1.6  mycroft 			 */
   2497  1.6  mycroft 			wakeup((caddr_t)&ahc->free_scbs);
   2498  1.6  mycroft 	}
   2499  1.6  mycroft 	splx(opri);
   2500  1.1  mycroft }
   2501  1.1  mycroft 
   2502  1.1  mycroft /*
   2503  1.6  mycroft  * Get a free scb, either one already assigned to a hardware slot
   2504  1.6  mycroft  * on the adapter or one that will require an SCB to be paged out before
   2505  1.6  mycroft  * use. If there are none, see if we can allocate a new SCB.  Otherwise
   2506  1.6  mycroft  * either return an error or sleep.
   2507  1.1  mycroft  */
   2508  1.6  mycroft static struct scb *
   2509  1.1  mycroft ahc_get_scb(ahc, flags)
   2510  1.6  mycroft         struct	ahc_data *ahc;
   2511  1.6  mycroft         int	flags;
   2512  1.1  mycroft {
   2513  1.6  mycroft 	unsigned opri;
   2514  1.6  mycroft 	struct scb *scbp;
   2515  1.1  mycroft 
   2516  1.6  mycroft 	opri = splbio();
   2517  1.1  mycroft 	/*
   2518  1.1  mycroft 	 * If we can and have to, sleep waiting for one to come free
   2519  1.1  mycroft 	 * but only if we can't allocate a new one.
   2520  1.1  mycroft 	 */
   2521  1.6  mycroft 	while (1) {
   2522  1.6  mycroft 		if((scbp = ahc->free_scbs.stqh_first)) {
   2523  1.6  mycroft 			STAILQ_REMOVE_HEAD(&ahc->free_scbs, links);
   2524  1.6  mycroft 		}
   2525  1.6  mycroft 		else if((scbp = ahc->page_scbs.stqh_first)) {
   2526  1.6  mycroft 			STAILQ_REMOVE_HEAD(&ahc->page_scbs, links);
   2527  1.6  mycroft 		}
   2528  1.6  mycroft 		else if (ahc->numscbs < ahc->maxscbs) {
   2529  1.6  mycroft 			scbp = (struct scb *) malloc(sizeof(struct scb),
   2530  1.6  mycroft 				M_TEMP, M_NOWAIT);
   2531  1.6  mycroft 			if (scbp) {
   2532  1.6  mycroft 				bzero(scbp, sizeof(struct scb));
   2533  1.6  mycroft 				scbp->tag = ahc->numscbs;
   2534  1.6  mycroft 				if( ahc->numscbs < ahc->maxhscbs )
   2535  1.6  mycroft 					scbp->position = ahc->numscbs;
   2536  1.6  mycroft 				else
   2537  1.6  mycroft 					scbp->position = SCB_LIST_NULL;
   2538  1.6  mycroft 				ahc->numscbs++;
   2539  1.6  mycroft 				/*
   2540  1.6  mycroft 				 * Place in the scbarray
   2541  1.6  mycroft 				 * Never is removed.
   2542  1.6  mycroft 				 */
   2543  1.6  mycroft 				ahc->scbarray[scbp->tag] = scbp;
   2544  1.6  mycroft 			}
   2545  1.6  mycroft 			else {
   2546  1.6  mycroft 				printf("%s: Can't malloc SCB\n",
   2547  1.6  mycroft 				       ahc_name(ahc));
   2548  1.6  mycroft 			}
   2549  1.1  mycroft 		}
   2550  1.6  mycroft 		else {
   2551  1.6  mycroft 			if (!(flags & SCSI_NOSLEEP)) {
   2552  1.6  mycroft 				tsleep((caddr_t)&ahc->free_scbs, PRIBIO,
   2553  1.6  mycroft 					"ahcscb", 0);
   2554  1.6  mycroft 				continue;
   2555  1.1  mycroft 			}
   2556  1.1  mycroft 		}
   2557  1.6  mycroft 		break;
   2558  1.1  mycroft 	}
   2559  1.1  mycroft 
   2560  1.6  mycroft 	if (scbp) {
   2561  1.6  mycroft 		scbp->control = 0;
   2562  1.6  mycroft 		scbp->status = 0;
   2563  1.6  mycroft 		scbp->flags = 0;
   2564  1.1  mycroft #ifdef AHC_DEBUG
   2565  1.6  mycroft 		ahc->activescbs++;
   2566  1.6  mycroft 		if((ahc_debug & AHC_SHOWSCBCNT)
   2567  1.6  mycroft 		  && (ahc->activescbs == ahc->maxhscbs))
   2568  1.6  mycroft 			printf("%s: Max SCBs active\n", ahc_name(ahc));
   2569  1.1  mycroft #endif
   2570  1.6  mycroft 	}
   2571  1.6  mycroft 
   2572  1.6  mycroft 	splx(opri);
   2573  1.1  mycroft 
   2574  1.6  mycroft 	return (scbp);
   2575  1.1  mycroft }
   2576  1.1  mycroft 
   2577  1.6  mycroft static void ahc_loadseq(ahc)
   2578  1.6  mycroft 	struct ahc_data *ahc;
   2579  1.1  mycroft {
   2580  1.6  mycroft         static unsigned char seqprog[] = {
   2581  1.1  mycroft #               include "aic7xxx_seq.h"
   2582  1.1  mycroft 	};
   2583  1.1  mycroft 
   2584  1.6  mycroft 	AHC_OUTB(ahc, SEQCTL, PERRORDIS|SEQRESET|LOADRAM);
   2585  1.6  mycroft 
   2586  1.6  mycroft 	AHC_OUTSB(ahc, SEQRAM, seqprog, sizeof(seqprog));
   2587  1.6  mycroft 
   2588  1.6  mycroft 	AHC_OUTB(ahc, SEQCTL, FASTMODE|SEQRESET);
   2589  1.6  mycroft 	do {
   2590  1.6  mycroft 		AHC_OUTB(ahc, SEQCTL, SEQRESET|FASTMODE);
   2591  1.6  mycroft 
   2592  1.6  mycroft 	} while (AHC_INB(ahc, SEQADDR0) != 0 &&
   2593  1.6  mycroft 		 AHC_INB(ahc, SEQADDR1) != 0);
   2594  1.1  mycroft }
   2595  1.1  mycroft 
   2596  1.1  mycroft /*
   2597  1.6  mycroft  * Function to poll for command completion when
   2598  1.6  mycroft  * interrupts are disabled (crash dumps)
   2599  1.1  mycroft  */
   2600  1.6  mycroft static int
   2601  1.6  mycroft ahc_poll(ahc, wait)
   2602  1.6  mycroft 	struct	ahc_data *ahc;
   2603  1.6  mycroft 	int	wait; /* in msec */
   2604  1.6  mycroft {
   2605  1.6  mycroft 	while (--wait) {
   2606  1.6  mycroft 		DELAY(1000);
   2607  1.6  mycroft 		if (AHC_INB(ahc, INTSTAT) & INT_PEND)
   2608  1.6  mycroft 			break;
   2609  1.6  mycroft 	} if (wait == 0) {
   2610  1.6  mycroft 		printf("%s: board not responding\n", ahc_name(ahc));
   2611  1.6  mycroft 		return (EIO);
   2612  1.6  mycroft 	}
   2613  1.6  mycroft 	ahc_intr((void *)ahc);
   2614  1.6  mycroft 	return (0);
   2615  1.6  mycroft }
   2616  1.6  mycroft 
   2617  1.6  mycroft static void
   2618  1.6  mycroft ahc_timeout(arg)
   2619  1.6  mycroft 	void	*arg;
   2620  1.6  mycroft {
   2621  1.6  mycroft 	struct	scb *scb = (struct scb *)arg;
   2622  1.6  mycroft 	struct	ahc_data *ahc;
   2623  1.6  mycroft 	int	s, h, found;
   2624  1.6  mycroft 	u_char	bus_state;
   2625  1.6  mycroft 	char	channel;
   2626  1.6  mycroft 
   2627  1.6  mycroft 	s = splbio();
   2628  1.6  mycroft 
   2629  1.6  mycroft 	h = splhigh();
   2630  1.6  mycroft 
   2631  1.6  mycroft 	if (!(scb->flags & SCB_ACTIVE)) {
   2632  1.6  mycroft 		/* Previous timeout took care of me already */
   2633  1.6  mycroft 		splx(h);
   2634  1.6  mycroft 		splx(s);
   2635  1.6  mycroft 		return;
   2636  1.6  mycroft 	}
   2637  1.6  mycroft 
   2638  1.6  mycroft 	ahc = (struct ahc_data *)scb->xs->sc_link->adapter_softc;
   2639  1.6  mycroft 
   2640  1.6  mycroft 	if (ahc->in_timeout) {
   2641  1.6  mycroft 		/*
   2642  1.6  mycroft 		 * Some other SCB has started a recovery operation
   2643  1.6  mycroft 		 * and is still working on cleaning things up.
   2644  1.6  mycroft 		 */
   2645  1.6  mycroft 		if (scb->flags & SCB_TIMEDOUT) {
   2646  1.6  mycroft 			/*
   2647  1.6  mycroft 			 * This SCB has been here before and is not the
   2648  1.6  mycroft 			 * recovery SCB. Cut our losses and panic.  Its
   2649  1.6  mycroft 			 * better to do this than trash a filesystem.
   2650  1.6  mycroft 			 */
   2651  1.6  mycroft 			panic("%s: Timed-out command times out "
   2652  1.6  mycroft 				"again\n", ahc_name(ahc));
   2653  1.6  mycroft 		}
   2654  1.6  mycroft 		else if (!(scb->flags & SCB_ABORTED))
   2655  1.6  mycroft 		{
   2656  1.6  mycroft 			/*
   2657  1.6  mycroft 			 * This is not the SCB that started this timeout
   2658  1.6  mycroft 			 * processing.  Give this scb another lifetime so
   2659  1.6  mycroft 			 * that it can continue once we deal with the
   2660  1.6  mycroft 			 * timeout.
   2661  1.6  mycroft 			 */
   2662  1.6  mycroft 			scb->flags |= SCB_TIMEDOUT;
   2663  1.6  mycroft 			timeout(ahc_timeout, (caddr_t)scb,
   2664  1.6  mycroft 				(scb->xs->timeout * hz) / 1000);
   2665  1.6  mycroft 			splx(h);
   2666  1.6  mycroft 			splx(s);
   2667  1.6  mycroft 			return;
   2668  1.6  mycroft 		}
   2669  1.6  mycroft 	}
   2670  1.6  mycroft 	ahc->in_timeout = TRUE;
   2671  1.6  mycroft 	splx(h);
   2672  1.6  mycroft 
   2673  1.6  mycroft 	/*
   2674  1.6  mycroft 	 * Ensure that the card doesn't do anything
   2675  1.6  mycroft 	 * behind our back.
   2676  1.6  mycroft 	 */
   2677  1.6  mycroft 	PAUSE_SEQUENCER(ahc);
   2678  1.6  mycroft 
   2679  1.6  mycroft 	sc_print_addr(scb->xs->sc_link);
   2680  1.6  mycroft 	printf("timed out ");
   2681  1.6  mycroft 	/*
   2682  1.6  mycroft 	 * Take a snapshot of the bus state and print out
   2683  1.6  mycroft 	 * some information so we can track down driver bugs.
   2684  1.6  mycroft 	 */
   2685  1.6  mycroft 	bus_state = AHC_INB(ahc, LASTPHASE);
   2686  1.6  mycroft 
   2687  1.6  mycroft 	switch(bus_state & PHASE_MASK)
   2688  1.6  mycroft 	{
   2689  1.6  mycroft 		case P_DATAOUT:
   2690  1.6  mycroft 			printf("in dataout phase");
   2691  1.6  mycroft 			break;
   2692  1.6  mycroft 		case P_DATAIN:
   2693  1.6  mycroft 			printf("in datain phase");
   2694  1.6  mycroft 			break;
   2695  1.6  mycroft 		case P_COMMAND:
   2696  1.6  mycroft 			printf("in command phase");
   2697  1.6  mycroft 			break;
   2698  1.6  mycroft 		case P_MESGOUT:
   2699  1.6  mycroft 			printf("in message out phase");
   2700  1.6  mycroft 			break;
   2701  1.6  mycroft 		case P_STATUS:
   2702  1.6  mycroft 			printf("in status phase");
   2703  1.6  mycroft 			break;
   2704  1.6  mycroft 		case P_MESGIN:
   2705  1.6  mycroft 			printf("in message in phase");
   2706  1.6  mycroft 			break;
   2707  1.6  mycroft 		default:
   2708  1.6  mycroft 			printf("while idle, LASTPHASE == 0x%x",
   2709  1.6  mycroft 				bus_state);
   2710  1.6  mycroft 			/*
   2711  1.6  mycroft 			 * We aren't in a valid phase, so assume we're
   2712  1.6  mycroft 			 * idle.
   2713  1.6  mycroft 			 */
   2714  1.6  mycroft 			bus_state = 0;
   2715  1.6  mycroft 			break;
   2716  1.6  mycroft 	}
   2717  1.6  mycroft 
   2718  1.6  mycroft 	printf(", SCSISIGI == 0x%x\n", AHC_INB(ahc, SCSISIGI));
   2719  1.6  mycroft 
   2720  1.6  mycroft 	/* Decide our course of action */
   2721  1.6  mycroft 
   2722  1.6  mycroft 	if(scb->flags & SCB_ABORTED)
   2723  1.6  mycroft 	{
   2724  1.6  mycroft 		/*
   2725  1.6  mycroft 		 * Been down this road before.
   2726  1.6  mycroft 		 * Do a full bus reset.
   2727  1.6  mycroft 		 */
   2728  1.6  mycroft 		char channel = (scb->tcl & SELBUSB)
   2729  1.6  mycroft 			   ? 'B': 'A';
   2730  1.6  mycroft 		found = ahc_reset_channel(ahc, channel, scb->tag,
   2731  1.6  mycroft 					  XS_TIMEOUT, /*Initiate Reset*/TRUE);
   2732  1.6  mycroft 		printf("%s: Issued Channel %c Bus Reset #1. "
   2733  1.6  mycroft 		       "%d SCBs aborted\n", ahc_name(ahc), channel, found);
   2734  1.6  mycroft 		ahc->in_timeout = FALSE;
   2735  1.6  mycroft 	}
   2736  1.6  mycroft 	else if(scb->control & TAG_ENB) {
   2737  1.6  mycroft 		/*
   2738  1.6  mycroft 		 * We could be starving this command
   2739  1.6  mycroft 		 * try sending an ordered tag command
   2740  1.6  mycroft 		 * to the target we come from.
   2741  1.6  mycroft 		 */
   2742  1.6  mycroft 		scb->flags |= SCB_ABORTED|SCB_SENTORDEREDTAG;
   2743  1.6  mycroft 		ahc->orderedtag |= 0xFF;
   2744  1.6  mycroft 		timeout(ahc_timeout, (caddr_t)scb, (5 * hz));
   2745  1.6  mycroft 		UNPAUSE_SEQUENCER(ahc);
   2746  1.6  mycroft 		printf("Ordered Tag queued\n");
   2747  1.6  mycroft 		goto done;
   2748  1.6  mycroft 	}
   2749  1.6  mycroft 	else {
   2750  1.6  mycroft 		/*
   2751  1.6  mycroft 		 * Send a Bus Device Reset Message:
   2752  1.6  mycroft 		 * The target that is holding up the bus may not
   2753  1.6  mycroft 		 * be the same as the one that triggered this timeout
   2754  1.6  mycroft 		 * (different commands have different timeout lengths).
   2755  1.6  mycroft 		 * It is also impossible to get a message to a target
   2756  1.6  mycroft 		 * if we are in a "frozen" data transfer phase.  Our
   2757  1.6  mycroft 		 * strategy here is to queue a bus device reset message
   2758  1.6  mycroft 		 * to the timed out target if it is disconnected.
   2759  1.6  mycroft 		 * Otherwise, if we have an active target we stuff the
   2760  1.6  mycroft 		 * message buffer with a bus device reset message and
   2761  1.6  mycroft 		 * assert ATN in the hopes that the target will let go
   2762  1.6  mycroft 		 * of the bus and finally disconnect.  If this fails,
   2763  1.6  mycroft 		 * we'll get another timeout 2 seconds later which will
   2764  1.6  mycroft 		 * cause a bus reset.
   2765  1.6  mycroft 		 *
   2766  1.6  mycroft 		 * XXX If the SCB is paged out, we simply reset the
   2767  1.6  mycroft 		 *     bus.  We should probably queue a new command
   2768  1.6  mycroft 		 *     instead.
   2769  1.6  mycroft 		 */
   2770  1.6  mycroft 
   2771  1.6  mycroft 		/* Test to see if scb is disconnected */
   2772  1.6  mycroft 		if( !(scb->flags & SCB_PAGED_OUT ) ){
   2773  1.6  mycroft 			u_char active_scb;
   2774  1.6  mycroft 			struct scb *active_scbp;
   2775  1.6  mycroft 
   2776  1.6  mycroft 			active_scb = AHC_INB(ahc, SCBPTR);
   2777  1.6  mycroft 			active_scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
   2778  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, scb->position);
   2779  1.1  mycroft 
   2780  1.6  mycroft 			if(AHC_INB(ahc, SCB_CONTROL) & DISCONNECTED) {
   2781  1.6  mycroft 				if(ahc->flags & AHC_PAGESCBS) {
   2782  1.6  mycroft 					/*
   2783  1.6  mycroft 					 * Pull this SCB out of the
   2784  1.6  mycroft 					 * disconnected list.
   2785  1.6  mycroft 					 */
   2786  1.6  mycroft 					u_char prev = AHC_INB(ahc, SCB_PREV);
   2787  1.6  mycroft 					u_char next = AHC_INB(ahc, SCB_NEXT);
   2788  1.6  mycroft 					if(prev == SCB_LIST_NULL) {
   2789  1.6  mycroft 						/* At the head */
   2790  1.6  mycroft 						AHC_OUTB(ahc, DISCONNECTED_SCBH,
   2791  1.6  mycroft 						     next );
   2792  1.6  mycroft 					}
   2793  1.6  mycroft 					else {
   2794  1.6  mycroft 						AHC_OUTB(ahc, SCBPTR, prev);
   2795  1.6  mycroft 						AHC_OUTB(ahc, SCB_NEXT, next);
   2796  1.6  mycroft 						if(next != SCB_LIST_NULL) {
   2797  1.6  mycroft 							AHC_OUTB(ahc, SCBPTR,
   2798  1.6  mycroft 							     next);
   2799  1.6  mycroft 							AHC_OUTB(ahc, SCB_PREV,
   2800  1.6  mycroft 							     prev);
   2801  1.6  mycroft 						}
   2802  1.6  mycroft 						AHC_OUTB(ahc, SCBPTR,
   2803  1.6  mycroft 						     scb->position);
   2804  1.6  mycroft 					}
   2805  1.6  mycroft 				}
   2806  1.6  mycroft 				scb->flags |= SCB_DEVICE_RESET|SCB_ABORTED;
   2807  1.6  mycroft 				scb->control &= DISCENB;
   2808  1.6  mycroft 				scb->cmdlen = 0;
   2809  1.6  mycroft 				scb->SG_segment_count = 0;
   2810  1.6  mycroft 				scb->SG_list_pointer = 0;
   2811  1.6  mycroft 				scb->data = 0;
   2812  1.6  mycroft 				scb->datalen = 0;
   2813  1.6  mycroft 				ahc_send_scb(ahc, scb);
   2814  1.6  mycroft 				ahc_add_waiting_scb(ahc, scb);
   2815  1.6  mycroft 				timeout(ahc_timeout, (caddr_t)scb, (2 * hz));
   2816  1.6  mycroft 				sc_print_addr(scb->xs->sc_link);
   2817  1.6  mycroft 				printf("BUS DEVICE RESET message queued.\n");
   2818  1.6  mycroft 				AHC_OUTB(ahc, SCBPTR, active_scb);
   2819  1.6  mycroft 				UNPAUSE_SEQUENCER(ahc);
   2820  1.6  mycroft 				goto done;
   2821  1.6  mycroft 			}
   2822  1.6  mycroft 			/* Is the active SCB really active? */
   2823  1.6  mycroft 			else if((active_scbp->flags & SCB_ACTIVE) && bus_state){
   2824  1.6  mycroft 				AHC_OUTB(ahc, MSG_LEN, 1);
   2825  1.6  mycroft 				AHC_OUTB(ahc, MSG0, MSG_BUS_DEVICE_RESET);
   2826  1.6  mycroft 				AHC_OUTB(ahc, SCSISIGO, bus_state|ATNO);
   2827  1.6  mycroft 				sc_print_addr(active_scbp->xs->sc_link);
   2828  1.6  mycroft 				printf("asserted ATN - device reset in "
   2829  1.6  mycroft 				       "message buffer\n");
   2830  1.6  mycroft 				active_scbp->flags |=   SCB_DEVICE_RESET
   2831  1.6  mycroft 						      | SCB_ABORTED;
   2832  1.6  mycroft 				if(active_scbp != scb) {
   2833  1.6  mycroft 					untimeout(ahc_timeout,
   2834  1.6  mycroft 						  (caddr_t)active_scbp);
   2835  1.6  mycroft 					/* Give scb a new lease on life */
   2836  1.6  mycroft 					timeout(ahc_timeout, (caddr_t)scb,
   2837  1.6  mycroft 						(scb->xs->timeout * hz) / 1000);
   2838  1.6  mycroft 				}
   2839  1.6  mycroft 				timeout(ahc_timeout, (caddr_t)active_scbp,
   2840  1.6  mycroft 					(2 * hz));
   2841  1.6  mycroft 				AHC_OUTB(ahc, SCBPTR, active_scb);
   2842  1.6  mycroft 				UNPAUSE_SEQUENCER(ahc);
   2843  1.6  mycroft 				goto done;
   2844  1.6  mycroft 			}
   2845  1.6  mycroft 		}
   2846  1.1  mycroft 		/*
   2847  1.6  mycroft 		 * No active target or a paged out SCB.
   2848  1.6  mycroft 		 * Try reseting the bus.
   2849  1.1  mycroft 		 */
   2850  1.6  mycroft 		channel = (scb->tcl & SELBUSB) ? 'B': 'A';
   2851  1.6  mycroft 		found = ahc_reset_channel(ahc, channel, scb->tag,
   2852  1.6  mycroft 					  XS_TIMEOUT,
   2853  1.6  mycroft 					  /*Initiate Reset*/TRUE);
   2854  1.6  mycroft 		printf("%s: Issued Channel %c Bus Reset #2. "
   2855  1.6  mycroft 			"%d SCBs aborted\n", ahc_name(ahc), channel,
   2856  1.6  mycroft 			found);
   2857  1.6  mycroft 		ahc->in_timeout = FALSE;
   2858  1.1  mycroft 	}
   2859  1.6  mycroft done:
   2860  1.6  mycroft 	splx(s);
   2861  1.1  mycroft }
   2862  1.1  mycroft 
   2863  1.6  mycroft 
   2864  1.6  mycroft /*
   2865  1.6  mycroft  * The device at the given target/channel has been reset.  Abort
   2866  1.6  mycroft  * all active and queued scbs for that target/channel.
   2867  1.6  mycroft  */
   2868  1.6  mycroft static int
   2869  1.6  mycroft ahc_reset_device(ahc, target, channel, timedout_scb, xs_error)
   2870  1.6  mycroft 	struct ahc_data *ahc;
   2871  1.6  mycroft 	int target;
   2872  1.6  mycroft 	char channel;
   2873  1.6  mycroft 	u_char timedout_scb;
   2874  1.6  mycroft 	u_int32_t xs_error;
   2875  1.1  mycroft {
   2876  1.6  mycroft         struct scb *scbp;
   2877  1.6  mycroft 	u_char active_scb;
   2878  1.6  mycroft 	int i = 0;
   2879  1.1  mycroft 	int found = 0;
   2880  1.1  mycroft 
   2881  1.6  mycroft 	/* restore this when we're done */
   2882  1.6  mycroft 	active_scb = AHC_INB(ahc, SCBPTR);
   2883  1.6  mycroft 
   2884  1.1  mycroft 	/*
   2885  1.6  mycroft 	 * Search the QINFIFO.
   2886  1.1  mycroft 	 */
   2887  1.1  mycroft 	{
   2888  1.1  mycroft 		int saved_queue[AHC_SCB_MAX];
   2889  1.6  mycroft 		int queued = AHC_INB(ahc, QINCNT) & ahc->qcntmask;
   2890  1.1  mycroft 
   2891  1.1  mycroft 		for (i = 0; i < (queued - found); i++) {
   2892  1.6  mycroft 			saved_queue[i] = AHC_INB(ahc, QINFIFO);
   2893  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, saved_queue[i]);
   2894  1.6  mycroft 			scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
   2895  1.6  mycroft 			if (ahc_match_scb (scbp, target, channel)){
   2896  1.6  mycroft 				/*
   2897  1.6  mycroft 				 * We found an scb that needs to be aborted.
   2898  1.6  mycroft 				 */
   2899  1.6  mycroft 				scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
   2900  1.6  mycroft 				scbp->xs->error |= xs_error;
   2901  1.6  mycroft 				if(scbp->position != timedout_scb)
   2902  1.6  mycroft 					untimeout(ahc_timeout, (caddr_t)scbp);
   2903  1.6  mycroft 				AHC_OUTB(ahc, SCB_CONTROL, 0);
   2904  1.1  mycroft 				i--;
   2905  1.6  mycroft 				found++;
   2906  1.1  mycroft 			}
   2907  1.1  mycroft 		}
   2908  1.6  mycroft 		/* Now put the saved scbs back. */
   2909  1.6  mycroft 		for (queued = 0; queued < i; queued++) {
   2910  1.6  mycroft 			AHC_OUTB(ahc, QINFIFO, saved_queue[queued]);
   2911  1.6  mycroft 		}
   2912  1.6  mycroft 	}
   2913  1.1  mycroft 
   2914  1.6  mycroft 	/*
   2915  1.6  mycroft 	 * Search waiting for selection list.
   2916  1.6  mycroft 	 */
   2917  1.6  mycroft 	{
   2918  1.6  mycroft 		u_char next, prev;
   2919  1.6  mycroft 
   2920  1.6  mycroft 		next = AHC_INB(ahc, WAITING_SCBH);  /* Start at head of list. */
   2921  1.6  mycroft 		prev = SCB_LIST_NULL;
   2922  1.6  mycroft 
   2923  1.6  mycroft 		while (next != SCB_LIST_NULL) {
   2924  1.6  mycroft 			AHC_OUTB(ahc, SCBPTR, next);
   2925  1.6  mycroft 			scbp = ahc->scbarray[AHC_INB(ahc, SCB_TAG)];
   2926  1.6  mycroft 			/*
   2927  1.6  mycroft 			 * Select the SCB.
   2928  1.6  mycroft 			 */
   2929  1.6  mycroft 			if (ahc_match_scb(scbp, target, channel)) {
   2930  1.6  mycroft 				next = ahc_abort_wscb(ahc, scbp, prev,
   2931  1.6  mycroft 						timedout_scb, xs_error);
   2932  1.6  mycroft 				found++;
   2933  1.6  mycroft 			}
   2934  1.6  mycroft 			else {
   2935  1.6  mycroft 				prev = next;
   2936  1.6  mycroft 				next = AHC_INB(ahc, SCB_NEXT);
   2937  1.6  mycroft 			}
   2938  1.6  mycroft 		}
   2939  1.1  mycroft 	}
   2940  1.1  mycroft 	/*
   2941  1.6  mycroft 	 * Go through the entire SCB array now and look for
   2942  1.6  mycroft 	 * commands for this target that are active.  These
   2943  1.6  mycroft 	 * are other (most likely tagged) commands that
   2944  1.6  mycroft 	 * were disconnected when the reset occured.
   2945  1.6  mycroft 	 */
   2946  1.6  mycroft 	for(i = 0; i < ahc->numscbs; i++) {
   2947  1.6  mycroft 		scbp = ahc->scbarray[i];
   2948  1.6  mycroft 		if((scbp->flags & SCB_ACTIVE)
   2949  1.6  mycroft 		  && ahc_match_scb(scbp, target, channel)) {
   2950  1.6  mycroft 			/* Ensure the target is "free" */
   2951  1.6  mycroft 			ahc_unbusy_target(ahc, target, channel);
   2952  1.6  mycroft 			if( !(scbp->flags & SCB_PAGED_OUT) )
   2953  1.6  mycroft 			{
   2954  1.6  mycroft 				AHC_OUTB(ahc, SCBPTR, scbp->position);
   2955  1.6  mycroft 				AHC_OUTB(ahc, SCB_CONTROL, 0);
   2956  1.6  mycroft 			}
   2957  1.6  mycroft 			scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
   2958  1.6  mycroft 			scbp->xs->error |= xs_error;
   2959  1.6  mycroft 			if(scbp->tag != timedout_scb)
   2960  1.6  mycroft 				untimeout(ahc_timeout, (caddr_t)scbp);
   2961  1.6  mycroft 			found++;
   2962  1.6  mycroft 		}
   2963  1.6  mycroft 	}
   2964  1.6  mycroft 	AHC_OUTB(ahc, SCBPTR, active_scb);
   2965  1.6  mycroft 	return found;
   2966  1.6  mycroft }
   2967  1.6  mycroft 
   2968  1.6  mycroft /*
   2969  1.6  mycroft  * Manipulate the waiting for selection list and return the
   2970  1.6  mycroft  * scb that follows the one that we remove.
   2971  1.6  mycroft  */
   2972  1.6  mycroft static u_char
   2973  1.6  mycroft ahc_abort_wscb (ahc, scbp, prev, timedout_scb, xs_error)
   2974  1.6  mycroft 	struct ahc_data *ahc;
   2975  1.6  mycroft         struct scb *scbp;
   2976  1.6  mycroft 	u_char prev;
   2977  1.6  mycroft 	u_char timedout_scb;
   2978  1.6  mycroft 	u_int32_t xs_error;
   2979  1.6  mycroft {
   2980  1.6  mycroft 	u_char curscbp, next;
   2981  1.6  mycroft 	int target = ((scbp->tcl >> 4) & 0x0f);
   2982  1.6  mycroft 	char channel = (scbp->tcl & SELBUSB) ? 'B' : 'A';
   2983  1.6  mycroft 	/*
   2984  1.6  mycroft 	 * Select the SCB we want to abort and
   2985  1.6  mycroft 	 * pull the next pointer out of it.
   2986  1.6  mycroft 	 */
   2987  1.6  mycroft 	curscbp = AHC_INB(ahc, SCBPTR);
   2988  1.6  mycroft 	AHC_OUTB(ahc, SCBPTR, scbp->position);
   2989  1.6  mycroft 	next = AHC_INB(ahc, SCB_NEXT);
   2990  1.6  mycroft 
   2991  1.6  mycroft 	/* Clear the necessary fields */
   2992  1.6  mycroft 	AHC_OUTB(ahc, SCB_CONTROL, 0);
   2993  1.6  mycroft 	AHC_OUTB(ahc, SCB_NEXT, SCB_LIST_NULL);
   2994  1.6  mycroft 	ahc_unbusy_target(ahc, target, channel);
   2995  1.6  mycroft 
   2996  1.6  mycroft 	/* update the waiting list */
   2997  1.6  mycroft 	if( prev == SCB_LIST_NULL )
   2998  1.6  mycroft 		/* First in the list */
   2999  1.6  mycroft 		AHC_OUTB(ahc, WAITING_SCBH, next);
   3000  1.6  mycroft 	else {
   3001  1.1  mycroft 		/*
   3002  1.6  mycroft 		 * Select the scb that pointed to us
   3003  1.6  mycroft 		 * and update its next pointer.
   3004  1.1  mycroft 		 */
   3005  1.6  mycroft 		AHC_OUTB(ahc, SCBPTR, prev);
   3006  1.6  mycroft 		AHC_OUTB(ahc, SCB_NEXT, next);
   3007  1.1  mycroft 	}
   3008  1.1  mycroft 	/*
   3009  1.6  mycroft 	 * Point us back at the original scb position
   3010  1.6  mycroft 	 * and inform the SCSI system that the command
   3011  1.6  mycroft 	 * has been aborted.
   3012  1.6  mycroft 	 */
   3013  1.6  mycroft 	AHC_OUTB(ahc, SCBPTR, curscbp);
   3014  1.6  mycroft 	scbp->flags = SCB_ABORTED|SCB_QUEUED_FOR_DONE;
   3015  1.6  mycroft 	scbp->xs->error |= xs_error;
   3016  1.6  mycroft 	if(scbp->tag != timedout_scb)
   3017  1.6  mycroft 		untimeout(ahc_timeout, (caddr_t)scbp);
   3018  1.6  mycroft 	return next;
   3019  1.6  mycroft }
   3020  1.6  mycroft 
   3021  1.6  mycroft static void
   3022  1.6  mycroft ahc_busy_target(ahc, target, channel)
   3023  1.6  mycroft 	struct ahc_data *ahc;
   3024  1.6  mycroft 	u_char target;
   3025  1.6  mycroft 	char   channel;
   3026  1.6  mycroft {
   3027  1.6  mycroft 	u_char active;
   3028  1.6  mycroft 	u_long active_port = ACTIVE_A;
   3029  1.6  mycroft 
   3030  1.6  mycroft 	if(target > 0x07 || channel == 'B') {
   3031  1.6  mycroft 		/*
   3032  1.6  mycroft 		 * targets on the Second channel or
   3033  1.6  mycroft 		 * above id 7 store info in byte two
   3034  1.6  mycroft 		 * of HA_ACTIVE
   3035  1.1  mycroft 		 */
   3036  1.6  mycroft 		active_port++;
   3037  1.6  mycroft 	}
   3038  1.6  mycroft 	active = AHC_INB(ahc, active_port);
   3039  1.6  mycroft 	active |= (0x01 << (target & 0x07));
   3040  1.6  mycroft 	AHC_OUTB(ahc, active_port, active);
   3041  1.6  mycroft }
   3042  1.6  mycroft 
   3043  1.6  mycroft static void
   3044  1.6  mycroft ahc_unbusy_target(ahc, target, channel)
   3045  1.6  mycroft 	struct ahc_data *ahc;
   3046  1.6  mycroft 	u_char target;
   3047  1.6  mycroft 	char   channel;
   3048  1.6  mycroft {
   3049  1.6  mycroft 	u_char active;
   3050  1.6  mycroft 	u_long active_port = ACTIVE_A;
   3051  1.1  mycroft 
   3052  1.6  mycroft 	if(target > 0x07 || channel == 'B') {
   3053  1.6  mycroft 		/*
   3054  1.6  mycroft 		 * targets on the Second channel or
   3055  1.6  mycroft 		 * above id 7 store info in byte two
   3056  1.6  mycroft 		 * of HA_ACTIVE
   3057  1.6  mycroft 		 */
   3058  1.6  mycroft 		active_port++;
   3059  1.1  mycroft 	}
   3060  1.6  mycroft 	active = AHC_INB(ahc, active_port);
   3061  1.6  mycroft 	active &= ~(0x01 << (target & 0x07));
   3062  1.6  mycroft 	AHC_OUTB(ahc, active_port, active);
   3063  1.1  mycroft }
   3064  1.1  mycroft 
   3065  1.6  mycroft static void
   3066  1.6  mycroft ahc_reset_current_bus(ahc)
   3067  1.6  mycroft 	struct ahc_data *ahc;
   3068  1.1  mycroft {
   3069  1.6  mycroft 	AHC_OUTB(ahc, SCSISEQ, SCSIRSTO);
   3070  1.6  mycroft 	DELAY(1000);
   3071  1.6  mycroft 	AHC_OUTB(ahc, SCSISEQ, 0);
   3072  1.6  mycroft }
   3073  1.1  mycroft 
   3074  1.6  mycroft static int
   3075  1.6  mycroft ahc_reset_channel(ahc, channel, timedout_scb, xs_error, initiate_reset)
   3076  1.6  mycroft 	struct ahc_data *ahc;
   3077  1.6  mycroft 	char   channel;
   3078  1.6  mycroft 	u_char timedout_scb;
   3079  1.6  mycroft 	u_int32_t xs_error;
   3080  1.6  mycroft 	u_char initiate_reset;
   3081  1.6  mycroft {
   3082  1.6  mycroft 	u_char sblkctl;
   3083  1.6  mycroft 	char cur_channel;
   3084  1.6  mycroft 	u_long offset, offset_max;
   3085  1.6  mycroft 	int found;
   3086  1.6  mycroft 
   3087  1.6  mycroft 	/*
   3088  1.6  mycroft 	 * Clean up all the state information for the
   3089  1.6  mycroft 	 * pending transactions on this bus.
   3090  1.6  mycroft 	 */
   3091  1.6  mycroft 	found = ahc_reset_device(ahc, ALL_TARGETS, channel,
   3092  1.6  mycroft 				 timedout_scb, xs_error);
   3093  1.6  mycroft 	if(channel == 'B'){
   3094  1.6  mycroft 		ahc->needsdtr |= (ahc->needsdtr_orig & 0xff00);
   3095  1.6  mycroft 		ahc->sdtrpending &= 0x00ff;
   3096  1.6  mycroft 		AHC_OUTB(ahc, ACTIVE_B, 0);
   3097  1.6  mycroft 		offset = TARG_SCRATCH + 8;
   3098  1.6  mycroft 		offset_max = TARG_SCRATCH + 16;
   3099  1.6  mycroft 	}
   3100  1.6  mycroft 	else if (ahc->type & AHC_WIDE){
   3101  1.6  mycroft 		ahc->needsdtr = ahc->needsdtr_orig;
   3102  1.6  mycroft 		ahc->needwdtr = ahc->needwdtr_orig;
   3103  1.6  mycroft 		ahc->sdtrpending = 0;
   3104  1.6  mycroft 		ahc->wdtrpending = 0;
   3105  1.6  mycroft 		AHC_OUTB(ahc, ACTIVE_A, 0);
   3106  1.6  mycroft 		AHC_OUTB(ahc, ACTIVE_B, 0);
   3107  1.6  mycroft 		offset = TARG_SCRATCH;
   3108  1.6  mycroft 		offset_max = TARG_SCRATCH + 16;
   3109  1.6  mycroft 	}
   3110  1.6  mycroft 	else{
   3111  1.6  mycroft 		ahc->needsdtr |= (ahc->needsdtr_orig & 0x00ff);
   3112  1.6  mycroft 		ahc->sdtrpending &= 0xff00;
   3113  1.6  mycroft 		AHC_OUTB(ahc, ACTIVE_A, 0);
   3114  1.6  mycroft 		offset = TARG_SCRATCH;
   3115  1.6  mycroft 		offset_max = TARG_SCRATCH + 8;
   3116  1.6  mycroft 	}
   3117  1.6  mycroft 	for(;offset < offset_max;offset++) {
   3118  1.6  mycroft 		/*
   3119  1.6  mycroft 		 * Revert to async/narrow transfers
   3120  1.6  mycroft 		 * until we renegotiate.
   3121  1.6  mycroft 		 */
   3122  1.6  mycroft 		u_char targ_scratch;
   3123  1.1  mycroft 
   3124  1.6  mycroft 		targ_scratch = AHC_INB(ahc, offset);
   3125  1.6  mycroft 		targ_scratch &= SXFR;
   3126  1.6  mycroft 		AHC_OUTB(ahc, offset, targ_scratch);
   3127  1.6  mycroft 	}
   3128  1.1  mycroft 
   3129  1.6  mycroft 	/*
   3130  1.6  mycroft 	 * Reset the bus if we are initiating this reset and
   3131  1.6  mycroft 	 * restart/unpause the sequencer
   3132  1.6  mycroft 	 */
   3133  1.6  mycroft 	/* Case 1: Command for another bus is active */
   3134  1.6  mycroft 	sblkctl = AHC_INB(ahc, SBLKCTL);
   3135  1.6  mycroft 	cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
   3136  1.6  mycroft 	if(cur_channel != channel)
   3137  1.6  mycroft 	{
   3138  1.6  mycroft 		/*
   3139  1.6  mycroft 		 * Stealthily reset the other bus
   3140  1.6  mycroft 		 * without upsetting the current bus
   3141  1.6  mycroft 		 */
   3142  1.6  mycroft 		AHC_OUTB(ahc, SBLKCTL, sblkctl ^ SELBUSB);
   3143  1.6  mycroft 		if( initiate_reset )
   3144  1.6  mycroft 		{
   3145  1.6  mycroft 			ahc_reset_current_bus(ahc);
   3146  1.6  mycroft 		}
   3147  1.6  mycroft 		AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
   3148  1.6  mycroft 		AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   3149  1.6  mycroft 		AHC_OUTB(ahc, SBLKCTL, sblkctl);
   3150  1.6  mycroft 		UNPAUSE_SEQUENCER(ahc);
   3151  1.6  mycroft 	}
   3152  1.6  mycroft 	/* Case 2: A command from this bus is active or we're idle */
   3153  1.6  mycroft 	else {
   3154  1.6  mycroft 		if( initiate_reset )
   3155  1.6  mycroft 		{
   3156  1.6  mycroft 			ahc_reset_current_bus(ahc);
   3157  1.6  mycroft 		}
   3158  1.6  mycroft 		AHC_OUTB(ahc, CLRSINT1, CLRSCSIRSTI|CLRSELTIMEO);
   3159  1.6  mycroft 		AHC_OUTB(ahc, CLRINT, CLRSCSIINT);
   3160  1.6  mycroft 		RESTART_SEQUENCER(ahc);
   3161  1.1  mycroft 	}
   3162  1.6  mycroft 	ahc_run_done_queue(ahc);
   3163  1.6  mycroft 	return found;
   3164  1.6  mycroft }
   3165  1.1  mycroft 
   3166  1.6  mycroft void
   3167  1.6  mycroft ahc_run_done_queue(ahc)
   3168  1.6  mycroft 	struct ahc_data *ahc;
   3169  1.6  mycroft {
   3170  1.6  mycroft 	int i;
   3171  1.6  mycroft 	struct scb *scbp;
   3172  1.6  mycroft 
   3173  1.6  mycroft 	for(i = 0; i < ahc->numscbs; i++) {
   3174  1.6  mycroft 		scbp = ahc->scbarray[i];
   3175  1.6  mycroft 		if(scbp->flags & SCB_QUEUED_FOR_DONE)
   3176  1.6  mycroft 			ahc_done(ahc, scbp);
   3177  1.1  mycroft 	}
   3178  1.6  mycroft }
   3179  1.6  mycroft 
   3180  1.6  mycroft static int
   3181  1.6  mycroft ahc_match_scb (scb, target, channel)
   3182  1.6  mycroft         struct scb *scb;
   3183  1.6  mycroft         int target;
   3184  1.6  mycroft 	char channel;
   3185  1.6  mycroft {
   3186  1.6  mycroft 	int targ = (scb->tcl >> 4) & 0x0f;
   3187  1.6  mycroft 	char chan = (scb->tcl & SELBUSB) ? 'B' : 'A';
   3188  1.1  mycroft 
   3189  1.6  mycroft 	if (target == ALL_TARGETS)
   3190  1.6  mycroft 		return (chan == channel);
   3191  1.6  mycroft 	else
   3192  1.6  mycroft 		return ((chan == channel) && (targ == target));
   3193  1.1  mycroft }
   3194