Home | History | Annotate | Line # | Download | only in ic
aic6360.c revision 1.42
      1 /*	$NetBSD: aic6360.c,v 1.42 1996/04/03 10:33:45 mycroft Exp $	*/
      2 
      3 #define	integrate	static inline
      4 
      5 /*
      6  * Copyright (c) 1994, 1995, 1996 Charles M. Hannum.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Charles M. Hannum.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * Copyright (c) 1994 Jarle Greipsland
     23  * All rights reserved.
     24  *
     25  * Redistribution and use in source and binary forms, with or without
     26  * modification, are permitted provided that the following conditions
     27  * are met:
     28  * 1. Redistributions of source code must retain the above copyright
     29  *    notice, this list of conditions and the following disclaimer.
     30  * 2. Redistributions in binary form must reproduce the above copyright
     31  *    notice, this list of conditions and the following disclaimer in the
     32  *    documentation and/or other materials provided with the distribution.
     33  * 3. The name of the author may not be used to endorse or promote products
     34  *    derived from this software without specific prior written permission.
     35  *
     36  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     37  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     38  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     39  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     40  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     41  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     42  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     45  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     46  * POSSIBILITY OF SUCH DAMAGE.
     47  */
     48 
     49 /*
     50  * Acknowledgements: Many of the algorithms used in this driver are
     51  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     52  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     53  */
     54 
     55 /* TODO list:
     56  * 1) Get the DMA stuff working.
     57  * 2) Get the iov/uio stuff working. Is this a good thing ???
     58  * 3) Get the synch stuff working.
     59  * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
     60  */
     61 
     62 /*
     63  * A few customizable items:
     64  */
     65 
     66 /* Use doubleword transfers to/from SCSI chip.  Note: This requires
     67  * motherboard support.  Basicly, some motherboard chipsets are able to
     68  * split a 32 bit I/O operation into two 16 bit I/O operations,
     69  * transparently to the processor.  This speeds up some things, notably long
     70  * data transfers.
     71  */
     72 #define AIC_USE_DWORDS		0
     73 
     74 /* Synchronous data transfers? */
     75 #define AIC_USE_SYNCHRONOUS	1
     76 #define AIC_SYNC_REQ_ACK_OFS 	8
     77 
     78 /* Wide data transfers? */
     79 #define	AIC_USE_WIDE		0
     80 #define	AIC_MAX_WIDTH		0
     81 
     82 /* Max attempts made to transmit a message */
     83 #define AIC_MSG_MAX_ATTEMPT	3 /* Not used now XXX */
     84 
     85 /* Use DMA (else we do programmed I/O using string instructions) (not yet!)*/
     86 #define AIC_USE_EISA_DMA	0
     87 #define AIC_USE_ISA_DMA		0
     88 
     89 /* How to behave on the (E)ISA bus when/if DMAing (on<<4) + off in us */
     90 #define EISA_BRST_TIM ((15<<4) + 1)	/* 15us on, 1us off */
     91 
     92 /* Some spin loop parameters (essentially how long to wait some places)
     93  * The problem(?) is that sometimes we expect either to be able to transmit a
     94  * byte or to get a new one from the SCSI bus pretty soon.  In order to avoid
     95  * returning from the interrupt just to get yanked back for the next byte we
     96  * may spin in the interrupt routine waiting for this byte to come.  How long?
     97  * This is really (SCSI) device and processor dependent.  Tuneable, I guess.
     98  */
     99 #define AIC_MSGIN_SPIN		1 	/* Will spinwait upto ?ms for a new msg byte */
    100 #define AIC_MSGOUT_SPIN		1
    101 
    102 /* Include debug functions?  At the end of this file there are a bunch of
    103  * functions that will print out various information regarding queued SCSI
    104  * commands, driver state and chip contents.  You can call them from the
    105  * kernel debugger.  If you set AIC_DEBUG to 0 they are not included (the
    106  * kernel uses less memory) but you lose the debugging facilities.
    107  */
    108 #define AIC_DEBUG		1
    109 
    110 #define	AIC_ABORT_TIMEOUT	2000	/* time to wait for abort */
    111 
    112 /* End of customizable parameters */
    113 
    114 #if AIC_USE_EISA_DMA || AIC_USE_ISA_DMA
    115 #error "I said not yet! Start paying attention... grumble"
    116 #endif
    117 
    118 #include <sys/types.h>
    119 #include <sys/param.h>
    120 #include <sys/systm.h>
    121 #include <sys/kernel.h>
    122 #include <sys/errno.h>
    123 #include <sys/ioctl.h>
    124 #include <sys/device.h>
    125 #include <sys/buf.h>
    126 #include <sys/proc.h>
    127 #include <sys/user.h>
    128 #include <sys/queue.h>
    129 
    130 #include <machine/pio.h>
    131 
    132 #include <scsi/scsi_all.h>
    133 #include <scsi/scsi_message.h>
    134 #include <scsi/scsiconf.h>
    135 
    136 #include <dev/isa/isavar.h>
    137 
    138 /* Definitions, most of them has turned out to be unneccesary, but here they
    139  * are anyway.
    140  */
    141 
    142 /* AIC6360 definitions */
    143 #define SCSISEQ		0x00	/* SCSI sequence control */
    144 #define SXFRCTL0	0x01	/* SCSI transfer control 0 */
    145 #define SXFRCTL1	0x02	/* SCSI transfer control 1 */
    146 #define SCSISIG		0x03	/* SCSI signal in/out */
    147 #define SCSIRATE	0x04	/* SCSI rate control */
    148 #define SCSIID		0x05	/* SCSI ID */
    149 #define SELID		0x05	/* Selection/Reselection ID */
    150 #define SCSIDAT		0x06	/* SCSI Latched Data */
    151 #define SCSIBUS		0x07	/* SCSI Data Bus*/
    152 #define STCNT0		0x08	/* SCSI transfer count */
    153 #define STCNT1		0x09
    154 #define STCNT2		0x0a
    155 #define CLRSINT0	0x0b	/* Clear SCSI interrupts 0 */
    156 #define SSTAT0		0x0b	/* SCSI interrupt status 0 */
    157 #define CLRSINT1	0x0c	/* Clear SCSI interrupts 1 */
    158 #define SSTAT1		0x0c	/* SCSI status 1 */
    159 #define SSTAT2		0x0d	/* SCSI status 2 */
    160 #define SCSITEST	0x0e	/* SCSI test control */
    161 #define SSTAT3		0x0e	/* SCSI status 3 */
    162 #define CLRSERR		0x0f	/* Clear SCSI errors */
    163 #define SSTAT4		0x0f	/* SCSI status 4 */
    164 #define SIMODE0		0x10	/* SCSI interrupt mode 0 */
    165 #define SIMODE1		0x11	/* SCSI interrupt mode 1 */
    166 #define DMACNTRL0	0x12	/* DMA control 0 */
    167 #define DMACNTRL1	0x13	/* DMA control 1 */
    168 #define DMASTAT		0x14	/* DMA status */
    169 #define FIFOSTAT	0x15	/* FIFO status */
    170 #define DMADATA		0x16	/* DMA data */
    171 #define DMADATAL	0x16	/* DMA data low byte */
    172 #define DMADATAH	0x17	/* DMA data high byte */
    173 #define BRSTCNTRL	0x18	/* Burst Control */
    174 #define DMADATALONG	0x18
    175 #define PORTA		0x1a	/* Port A */
    176 #define PORTB		0x1b	/* Port B */
    177 #define REV		0x1c	/* Revision (001 for 6360) */
    178 #define STACK		0x1d	/* Stack */
    179 #define TEST		0x1e	/* Test register */
    180 #define ID		0x1f	/* ID register */
    181 
    182 #define IDSTRING "(C)1991ADAPTECAIC6360           "
    183 
    184 /* What all the bits do */
    185 
    186 /* SCSISEQ */
    187 #define TEMODEO		0x80
    188 #define ENSELO		0x40
    189 #define ENSELI		0x20
    190 #define ENRESELI	0x10
    191 #define ENAUTOATNO	0x08
    192 #define ENAUTOATNI	0x04
    193 #define ENAUTOATNP	0x02
    194 #define SCSIRSTO	0x01
    195 
    196 /* SXFRCTL0 */
    197 #define SCSIEN		0x80
    198 #define DMAEN		0x40
    199 #define CHEN		0x20
    200 #define CLRSTCNT	0x10
    201 #define SPIOEN		0x08
    202 #define CLRCH		0x02
    203 
    204 /* SXFRCTL1 */
    205 #define BITBUCKET	0x80
    206 #define SWRAPEN		0x40
    207 #define ENSPCHK		0x20
    208 #define STIMESEL1	0x10
    209 #define STIMESEL0	0x08
    210 #define STIMO_256ms	0x00
    211 #define STIMO_128ms	0x08
    212 #define STIMO_64ms	0x10
    213 #define STIMO_32ms	0x18
    214 #define ENSTIMER	0x04
    215 #define BYTEALIGN	0x02
    216 
    217 /* SCSISIG (in) */
    218 #define CDI		0x80
    219 #define IOI		0x40
    220 #define MSGI		0x20
    221 #define ATNI		0x10
    222 #define SELI		0x08
    223 #define BSYI		0x04
    224 #define REQI		0x02
    225 #define ACKI		0x01
    226 
    227 /* Important! The 3 most significant bits of this register, in initiator mode,
    228  * represents the "expected" SCSI bus phase and can be used to trigger phase
    229  * mismatch and phase change interrupts.  But more important:  If there is a
    230  * phase mismatch the chip will not transfer any data!  This is actually a nice
    231  * feature as it gives us a bit more control over what is happening when we are
    232  * bursting data (in) through the FIFOs and the phase suddenly changes from
    233  * DATA IN to STATUS or MESSAGE IN.  The transfer will stop and wait for the
    234  * proper phase to be set in this register instead of dumping the bits into the
    235  * FIFOs.
    236  */
    237 /* SCSISIG (out) */
    238 #define CDO		0x80
    239 #define IOO		0x40
    240 #define MSGO		0x20
    241 #define ATNO		0x10
    242 #define SELO		0x08
    243 #define BSYO		0x04
    244 #define REQO		0x02
    245 #define ACKO		0x01
    246 
    247 /* Information transfer phases */
    248 #define PH_DATAOUT	(0)
    249 #define PH_DATAIN	(IOI)
    250 #define PH_CMD		(CDI)
    251 #define PH_STAT		(CDI | IOI)
    252 #define PH_MSGOUT	(MSGI | CDI)
    253 #define PH_MSGIN	(MSGI | CDI | IOI)
    254 
    255 #define PH_MASK		(MSGI | CDI | IOI)
    256 
    257 #define	PH_INVALID	0xff
    258 
    259 /* SCSIRATE */
    260 #define SXFR2		0x40
    261 #define SXFR1		0x20
    262 #define SXFR0		0x10
    263 #define SOFS3		0x08
    264 #define SOFS2		0x04
    265 #define SOFS1		0x02
    266 #define SOFS0		0x01
    267 
    268 /* SCSI ID */
    269 #define OID2		0x40
    270 #define OID1		0x20
    271 #define OID0		0x10
    272 #define OID_S		4	/* shift value */
    273 #define TID2		0x04
    274 #define TID1		0x02
    275 #define TID0		0x01
    276 #define SCSI_ID_MASK	0x7
    277 
    278 /* SCSI selection/reselection ID (both target *and* initiator) */
    279 #define SELID7		0x80
    280 #define SELID6		0x40
    281 #define SELID5		0x20
    282 #define SELID4		0x10
    283 #define SELID3		0x08
    284 #define SELID2		0x04
    285 #define SELID1		0x02
    286 #define SELID0		0x01
    287 
    288 /* CLRSINT0                      Clears what? (interrupt and/or status bit) */
    289 #define SETSDONE	0x80
    290 #define CLRSELDO	0x40	/* I */
    291 #define CLRSELDI	0x20	/* I+ */
    292 #define CLRSELINGO	0x10	/* I */
    293 #define CLRSWRAP	0x08	/* I+S */
    294 #define CLRSDONE	0x04	/* I+S */
    295 #define CLRSPIORDY	0x02	/* I */
    296 #define CLRDMADONE	0x01	/* I */
    297 
    298 /* SSTAT0                          Howto clear */
    299 #define TARGET		0x80
    300 #define SELDO		0x40	/* Selfclearing */
    301 #define SELDI		0x20	/* Selfclearing when CLRSELDI is set */
    302 #define SELINGO		0x10	/* Selfclearing */
    303 #define SWRAP		0x08	/* CLRSWAP */
    304 #define SDONE		0x04	/* Not used in initiator mode */
    305 #define SPIORDY		0x02	/* Selfclearing (op on SCSIDAT) */
    306 #define DMADONE		0x01	/* Selfclearing (all FIFOs empty & T/C */
    307 
    308 /* CLRSINT1                      Clears what? */
    309 #define CLRSELTIMO	0x80	/* I+S */
    310 #define CLRATNO		0x40
    311 #define CLRSCSIRSTI	0x20	/* I+S */
    312 #define CLRBUSFREE	0x08	/* I+S */
    313 #define CLRSCSIPERR	0x04	/* I+S */
    314 #define CLRPHASECHG	0x02	/* I+S */
    315 #define CLRREQINIT	0x01	/* I+S */
    316 
    317 /* SSTAT1                       How to clear?  When set?*/
    318 #define SELTO		0x80	/* C		select out timeout */
    319 #define ATNTARG		0x40	/* Not used in initiator mode */
    320 #define SCSIRSTI	0x20	/* C		RST asserted */
    321 #define PHASEMIS	0x10	/* Selfclearing */
    322 #define BUSFREE		0x08	/* C		bus free condition */
    323 #define SCSIPERR	0x04	/* C		parity error on inbound data */
    324 #define PHASECHG	0x02	/* C	     phase in SCSISIG doesn't match */
    325 #define REQINIT		0x01	/* C or ACK	asserting edge of REQ */
    326 
    327 /* SSTAT2 */
    328 #define SOFFSET		0x20
    329 #define SEMPTY		0x10
    330 #define SFULL		0x08
    331 #define SFCNT2		0x04
    332 #define SFCNT1		0x02
    333 #define SFCNT0		0x01
    334 
    335 /* SCSITEST */
    336 #define SCTESTU		0x08
    337 #define SCTESTD		0x04
    338 #define STCTEST		0x01
    339 
    340 /* SSTAT3 */
    341 #define SCSICNT3	0x80
    342 #define SCSICNT2	0x40
    343 #define SCSICNT1	0x20
    344 #define SCSICNT0	0x10
    345 #define OFFCNT3		0x08
    346 #define OFFCNT2		0x04
    347 #define OFFCNT1		0x02
    348 #define OFFCNT0		0x01
    349 
    350 /* CLRSERR */
    351 #define CLRSYNCERR	0x04
    352 #define CLRFWERR	0x02
    353 #define CLRFRERR	0x01
    354 
    355 /* SSTAT4 */
    356 #define SYNCERR		0x04
    357 #define FWERR		0x02
    358 #define FRERR		0x01
    359 
    360 /* SIMODE0 */
    361 #define ENSELDO		0x40
    362 #define ENSELDI		0x20
    363 #define ENSELINGO	0x10
    364 #define	ENSWRAP		0x08
    365 #define ENSDONE		0x04
    366 #define ENSPIORDY	0x02
    367 #define ENDMADONE	0x01
    368 
    369 /* SIMODE1 */
    370 #define ENSELTIMO	0x80
    371 #define ENATNTARG	0x40
    372 #define ENSCSIRST	0x20
    373 #define ENPHASEMIS	0x10
    374 #define ENBUSFREE	0x08
    375 #define ENSCSIPERR	0x04
    376 #define ENPHASECHG	0x02
    377 #define ENREQINIT	0x01
    378 
    379 /* DMACNTRL0 */
    380 #define ENDMA		0x80
    381 #define B8MODE		0x40
    382 #define DMA		0x20
    383 #define DWORDPIO	0x10
    384 #define WRITE		0x08
    385 #define INTEN		0x04
    386 #define RSTFIFO		0x02
    387 #define SWINT		0x01
    388 
    389 /* DMACNTRL1 */
    390 #define PWRDWN		0x80
    391 #define ENSTK32		0x40
    392 #define STK4		0x10
    393 #define STK3		0x08
    394 #define STK2		0x04
    395 #define STK1		0x02
    396 #define STK0		0x01
    397 
    398 /* DMASTAT */
    399 #define ATDONE		0x80
    400 #define WORDRDY		0x40
    401 #define INTSTAT		0x20
    402 #define DFIFOFULL	0x10
    403 #define DFIFOEMP	0x08
    404 #define DFIFOHF		0x04
    405 #define DWORDRDY	0x02
    406 
    407 /* BRSTCNTRL */
    408 #define BON3		0x80
    409 #define BON2		0x40
    410 #define BON1		0x20
    411 #define BON0		0x10
    412 #define BOFF3		0x08
    413 #define BOFF2		0x04
    414 #define BOFF1		0x02
    415 #define BOFF0		0x01
    416 
    417 /* TEST */
    418 #define BOFFTMR		0x40
    419 #define BONTMR		0x20
    420 #define STCNTH		0x10
    421 #define STCNTM		0x08
    422 #define STCNTL		0x04
    423 #define SCSIBLK		0x02
    424 #define DMABLK		0x01
    425 
    426 #ifndef DDB
    428 #define	Debugger() panic("should call debugger here (aic6360.c)")
    429 #endif /* ! DDB */
    430 
    431 typedef u_long physaddr;
    432 typedef u_long physlen;
    433 
    434 struct aic_dma_seg {
    435 	physaddr seg_addr;
    436 	physlen seg_len;
    437 };
    438 
    439 #define AIC_NSEG	16
    440 
    441 /*
    442  * ACB. Holds additional information for each SCSI command Comments: We
    443  * need a separate scsi command block because we may need to overwrite it
    444  * with a request sense command.  Basicly, we refrain from fiddling with
    445  * the scsi_xfer struct (except do the expected updating of return values).
    446  * We'll generally update: xs->{flags,resid,error,sense,status} and
    447  * occasionally xs->retries.
    448  */
    449 struct aic_acb {
    450 	struct scsi_generic scsi_cmd;
    451 	int scsi_cmd_length;
    452 	u_char *data_addr;		/* Saved data pointer */
    453 	int data_length;		/* Residue */
    454 
    455 	u_char target_stat;		/* SCSI status byte */
    456 
    457 /*	struct aic_dma_seg dma[AIC_NSEG]; /* Physical addresses+len */
    458 
    459 	TAILQ_ENTRY(aic_acb) chain;
    460 	struct scsi_xfer *xs;	/* SCSI xfer ctrl block from above */
    461 	int flags;
    462 #define ACB_ALLOC	0x01
    463 #define	ACB_NEXUS	0x02
    464 #define ACB_SENSE	0x04
    465 #define	ACB_ABORT	0x40
    466 #define	ACB_RESET	0x80
    467 	int timeout;
    468 };
    469 
    470 /*
    471  * Some info about each (possible) target on the SCSI bus.  This should
    472  * probably have been a "per target+lunit" structure, but we'll leave it at
    473  * this for now.
    474  */
    475 struct aic_tinfo {
    476 	int	cmds;		/* #commands processed */
    477 	int	dconns;		/* #disconnects */
    478 	int	touts;		/* #timeouts */
    479 	int	perrs;		/* #parity errors */
    480 	int	senses;		/* #request sense commands sent */
    481 	ushort	lubusy;		/* What local units/subr. are busy? */
    482 	u_char  flags;
    483 #define DO_SYNC		0x01	/* (Re)Negotiate synchronous options */
    484 #define	DO_WIDE		0x02	/* (Re)Negotiate wide options */
    485 	u_char  period;		/* Period suggestion */
    486 	u_char  offset;		/* Offset suggestion */
    487 	u_char	width;		/* Width suggestion */
    488 } tinfo_t;
    489 
    490 struct aic_softc {
    491 	struct device sc_dev;
    492 	struct isadev sc_id;
    493 	void *sc_ih;
    494 
    495 	int sc_iobase;
    496 	int sc_irq, sc_drq;
    497 
    498 	struct scsi_link sc_link;	/* prototype for subdevs */
    499 
    500 	TAILQ_HEAD(, aic_acb) free_list, ready_list, nexus_list;
    501 	struct aic_acb *sc_nexus;	/* current command */
    502 	struct aic_acb sc_acb[8];
    503 	struct aic_tinfo sc_tinfo[8];
    504 
    505 	/* Data about the current nexus (updated for every cmd switch) */
    506 	u_char	*sc_dp;		/* Current data pointer */
    507 	size_t	sc_dleft;	/* Data bytes left to transfer */
    508 	u_char	*sc_cp;		/* Current command pointer */
    509 	size_t	sc_cleft;	/* Command bytes left to transfer */
    510 
    511 	/* Adapter state */
    512 	u_char	 sc_phase;	/* Current bus phase */
    513 	u_char	 sc_prevphase;	/* Previous bus phase */
    514 	u_char	 sc_state;	/* State applicable to the adapter */
    515 #define	AIC_INIT	0
    516 #define AIC_IDLE	1
    517 #define AIC_SELECTING	2	/* SCSI command is arbiting  */
    518 #define AIC_RESELECTED	3	/* Has been reselected */
    519 #define AIC_CONNECTED	4	/* Actively using the SCSI bus */
    520 #define	AIC_DISCONNECT	5	/* MSG_DISCONNECT received */
    521 #define	AIC_CMDCOMPLETE	6	/* MSG_CMDCOMPLETE received */
    522 #define AIC_CLEANING	7
    523 	u_char	 sc_flags;
    524 #define AIC_DROP_MSGIN	0x01	/* Discard all msgs (parity err detected) */
    525 #define	AIC_ABORTING	0x02	/* Bailing out */
    526 #define AIC_DOINGDMA	0x04	/* The FIFO data path is active! */
    527 	u_char	sc_selid;	/* Reselection ID */
    528 
    529 	/* Message stuff */
    530 	u_char	sc_msgpriq;	/* Messages we want to send */
    531 	u_char	sc_msgoutq;	/* Messages sent during last MESSAGE OUT */
    532 	u_char	sc_lastmsg;	/* Message last transmitted */
    533 	u_char	sc_currmsg;	/* Message currently ready to transmit */
    534 #define SEND_DEV_RESET		0x01
    535 #define SEND_PARITY_ERROR	0x02
    536 #define SEND_INIT_DET_ERR	0x04
    537 #define SEND_REJECT		0x08
    538 #define SEND_IDENTIFY  		0x10
    539 #define SEND_ABORT		0x20
    540 #define SEND_SDTR		0x40
    541 #define	SEND_WDTR		0x80
    542 #define AIC_MAX_MSG_LEN 8
    543 	u_char  sc_omess[AIC_MAX_MSG_LEN];
    544 	u_char	*sc_omp;		/* Outgoing message pointer */
    545 	u_char	sc_imess[AIC_MAX_MSG_LEN];
    546 	u_char	*sc_imp;		/* Incoming message pointer */
    547 
    548 	/* Hardware stuff */
    549 	int	sc_initiator;		/* Our scsi id */
    550 	int	sc_freq;		/* Clock frequency in MHz */
    551 	int	sc_minsync;		/* Minimum sync period / 4 */
    552 	int	sc_maxsync;		/* Maximum sync period / 4 */
    553 };
    554 
    555 #if AIC_DEBUG
    556 #define AIC_SHOWACBS	0x01
    557 #define AIC_SHOWINTS	0x02
    558 #define AIC_SHOWCMDS	0x04
    559 #define AIC_SHOWMISC	0x08
    560 #define AIC_SHOWTRACE	0x10
    561 #define AIC_SHOWSTART	0x20
    562 #define AIC_DOBREAK	0x40
    563 int aic_debug = 0x00; /* AIC_SHOWSTART|AIC_SHOWMISC|AIC_SHOWTRACE; /**/
    564 #define	AIC_PRINT(b, s)	do {if ((aic_debug & (b)) != 0) printf s;} while (0)
    565 #define	AIC_BREAK()	do {if ((aic_debug & AIC_DOBREAK) != 0) Debugger();} while (0)
    566 #define	AIC_ASSERT(x)	do {if (x) {} else {printf("%s at line %d: assertion failed\n", sc->sc_dev.dv_xname, __LINE__); Debugger();}} while (0)
    567 #else
    568 #define	AIC_PRINT(b, s)
    569 #define	AIC_BREAK()
    570 #define	AIC_ASSERT(x)
    571 #endif
    572 
    573 #define AIC_ACBS(s)	AIC_PRINT(AIC_SHOWACBS, s)
    574 #define AIC_INTS(s)	AIC_PRINT(AIC_SHOWINTS, s)
    575 #define AIC_CMDS(s)	AIC_PRINT(AIC_SHOWCMDS, s)
    576 #define AIC_MISC(s)	AIC_PRINT(AIC_SHOWMISC, s)
    577 #define AIC_TRACE(s)	AIC_PRINT(AIC_SHOWTRACE, s)
    578 #define AIC_START(s)	AIC_PRINT(AIC_SHOWSTART, s)
    579 
    580 int	aicprobe	__P((struct device *, void *, void *));
    581 void	aicattach	__P((struct device *, struct device *, void *));
    582 int	aicprint	__P((void *, char *));
    583 void	aic_minphys	__P((struct buf *));
    584 int	aicintr		__P((void *));
    585 void 	aic_init	__P((struct aic_softc *));
    586 void	aic_done	__P((struct aic_softc *, struct aic_acb *));
    587 void	aic_dequeue	__P((struct aic_softc *, struct aic_acb *));
    588 int	aic_scsi_cmd	__P((struct scsi_xfer *));
    589 int	aic_poll	__P((struct aic_softc *, struct scsi_xfer *, int));
    590 integrate void	aic_sched_msgout __P((struct aic_softc *, u_char));
    591 integrate void	aic_setsync	__P((struct aic_softc *, struct aic_tinfo *));
    592 void	aic_select	__P((struct aic_softc *, struct aic_acb *));
    593 void	aic_timeout	__P((void *));
    594 int	aic_find	__P((struct aic_softc *));
    595 void	aic_sched	__P((struct aic_softc *));
    596 void	aic_scsi_reset	__P((struct aic_softc *));
    597 void	aic_reset	__P((struct aic_softc *));
    598 #if AIC_DEBUG
    599 void	aic_print_active_acb();
    600 void	aic_dump_driver();
    601 void	aic_dump6360();
    602 #endif
    603 
    604 struct cfattach aic_ca = {
    605 	sizeof(struct aic_softc), aicprobe, aicattach
    606 };
    607 
    608 struct cfdriver aic_cd = {
    609 	NULL, "aic", DV_DULL
    610 };
    611 
    612 struct scsi_adapter aic_switch = {
    613 	aic_scsi_cmd,
    614 	aic_minphys,
    615 	0,
    616 	0,
    617 };
    618 
    619 struct scsi_device aic_dev = {
    620 	NULL,			/* Use default error handler */
    621 	NULL,			/* have a queue, served by this */
    622 	NULL,			/* have no async handler */
    623 	NULL,			/* Use default 'done' routine */
    624 };
    625 
    626 /*
    628  * INITIALIZATION ROUTINES (probe, attach ++)
    629  */
    630 
    631 /*
    632  * aicprobe: probe for AIC6360 SCSI-controller
    633  * returns non-zero value if a controller is found.
    634  */
    635 int
    636 aicprobe(parent, match, aux)
    637 	struct device *parent;
    638 	void *match, *aux;
    639 {
    640 	struct aic_softc *sc = match;
    641 	struct isa_attach_args *ia = aux;
    642 	int i, len, ic;
    643 
    644 #ifdef NEWCONFIG
    645 	if (ia->ia_iobase == IOBASEUNK)
    646 		return 0;
    647 #endif
    648 
    649 	sc->sc_iobase = ia->ia_iobase;
    650 	if (aic_find(sc) != 0)
    651 		return 0;
    652 
    653 #ifdef NEWCONFIG
    654 	if (ia->ia_irq != IRQUNK) {
    655 		if (ia->ia_irq != sc->sc_irq) {
    656 			printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
    657 			    sc->sc_dev.dv_xname, ia->ia_irq, sc->sc_irq);
    658 			return 0;
    659 		}
    660 	} else
    661 		ia->ia_irq = sc->sc_irq;
    662 
    663 	if (ia->ia_drq != DRQUNK) {
    664 		if (ia->ia_drq != sc->sc_drq) {
    665 			printf("%s: drq mismatch; kernel configured %d != board configured %d\n",
    666 			    sc->sc_dev.dv_xname, ia->ia_drq, sc->sc_drq);
    667 			return 0;
    668 		}
    669 	} else
    670 		ia->ia_drq = sc->sc_drq;
    671 #endif
    672 
    673 	ia->ia_msize = 0;
    674 	ia->ia_iosize = 0x20;
    675 	return 1;
    676 }
    677 
    678 /* Do the real search-for-device.
    679  * Prerequisite: sc->sc_iobase should be set to the proper value
    680  */
    681 int
    682 aic_find(sc)
    683 	struct aic_softc *sc;
    684 {
    685 	int iobase = sc->sc_iobase;
    686 	char chip_id[sizeof(IDSTRING)];	/* For chips that support it */
    687 	char *start;
    688 	int i;
    689 
    690 	/* Remove aic6360 from possible powerdown mode */
    691 	outb(iobase + DMACNTRL0, 0);
    692 
    693 	/* Thanks to mark (at) aggregate.com for the new method for detecting
    694 	 * whether the chip is present or not.  Bonus: may also work for
    695 	 * the AIC-6260!
    696  	 */
    697 	AIC_TRACE(("aic: probing for aic-chip at port 0x%x\n",
    698 	    sc->sc_iobase));
    699  	/*
    700  	 * Linux also init's the stack to 1-16 and then clears it,
    701      	 *  6260's don't appear to have an ID reg - mpg
    702  	 */
    703 	/* Push the sequence 0,1,..,15 on the stack */
    704 #define STSIZE 16
    705 	outb(iobase + DMACNTRL1, 0);	/* Reset stack pointer */
    706 	for (i = 0; i < STSIZE; i++)
    707 		outb(iobase + STACK, i);
    708 
    709 	/* See if we can pull out the same sequence */
    710 	outb(iobase + DMACNTRL1, 0);
    711  	for (i = 0; i < STSIZE && inb(iobase + STACK) == i; i++)
    712 		;
    713 	if (i != STSIZE) {
    714 		AIC_START(("STACK futzed at %d.\n", i));
    715 		return ENXIO;
    716 	}
    717 
    718 	/* See if we can pull the id string out of the ID register,
    719 	 * now only used for informational purposes.
    720 	 */
    721 	bzero(chip_id, sizeof(chip_id));
    722 	insb(iobase + ID, chip_id, sizeof(IDSTRING)-1);
    723 	AIC_START(("AIC found at 0x%x ", sc->sc_iobase));
    724 	AIC_START(("ID: %s ",chip_id));
    725 	AIC_START(("chip revision %d\n",(int)inb(iobase + REV)));
    726 
    727 	sc->sc_initiator = 7;
    728 	sc->sc_freq = 20;	/* XXXX Assume 20 MHz. */
    729 
    730 	/*
    731 	 * These are the bounds of the sync period, based on the frequency of
    732 	 * the chip's clock input and the size and offset of the sync period
    733 	 * register.
    734 	 *
    735 	 * For a 20Mhz clock, this gives us 25, or 100nS, or 10MB/s, as a
    736 	 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a
    737 	 * minimum transfer rate.
    738 	 */
    739 	sc->sc_minsync = (2 * 250) / sc->sc_freq;
    740 	sc->sc_maxsync = (9 * 250) / sc->sc_freq;
    741 
    742 	return 0;
    743 }
    744 
    745 int
    746 aicprint(aux, name)
    747 	void *aux;
    748 	char *name;
    749 {
    750 	if (name != NULL)
    751 		printf("%s: scsibus ", name);
    752 	return UNCONF;
    753 }
    754 
    755 /*
    756  * Attach the AIC6360, fill out some high and low level data structures
    757  */
    758 void
    759 aicattach(parent, self, aux)
    760 	struct device *parent, *self;
    761 	void *aux;
    762 {
    763 	struct isa_attach_args *ia = aux;
    764 	struct aic_softc *sc = (void *)self;
    765 
    766 	AIC_TRACE(("aicattach  "));
    767 	sc->sc_state = AIC_INIT;
    768 	aic_init(sc);	/* Init chip and driver */
    769 
    770 	/*
    771 	 * Fill in the prototype scsi_link
    772 	 */
    773 	sc->sc_link.adapter_softc = sc;
    774 	sc->sc_link.adapter_target = sc->sc_initiator;
    775 	sc->sc_link.adapter = &aic_switch;
    776 	sc->sc_link.device = &aic_dev;
    777 	sc->sc_link.openings = 2;
    778 
    779 	printf("\n");
    780 
    781 #ifdef NEWCONFIG
    782 	isa_establish(&sc->sc_id, &sc->sc_dev);
    783 #endif
    784 	sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_BIO, aicintr,
    785 	    sc);
    786 
    787 	config_found(self, &sc->sc_link, aicprint);
    788 }
    789 
    790 
    791 /* Initialize AIC6360 chip itself
    792  * The following conditions should hold:
    793  * aicprobe should have succeeded, i.e. the iobase address in aic_softc must
    794  * be valid.
    795  */
    796 void
    797 aic_reset(sc)
    798 	struct aic_softc *sc;
    799 {
    800 	int iobase = sc->sc_iobase;
    801 
    802 	outb(iobase + SCSITEST, 0);	/* Doc. recommends to clear these two */
    803 	outb(iobase + TEST, 0);		/* registers before operations commence */
    804 
    805 	/* Reset SCSI-FIFO and abort any transfers */
    806 	outb(iobase + SXFRCTL0, CHEN | CLRCH | CLRSTCNT);
    807 
    808 	/* Reset DMA-FIFO */
    809 	outb(iobase + DMACNTRL0, RSTFIFO);
    810 	outb(iobase + DMACNTRL1, 0);
    811 
    812 	outb(iobase + SCSISEQ, 0);	/* Disable all selection features */
    813 	outb(iobase + SXFRCTL1, 0);
    814 
    815 	outb(iobase + SIMODE0, 0x00);	/* Disable some interrupts */
    816 	outb(iobase + CLRSINT0, 0x7f);	/* Clear a slew of interrupts */
    817 
    818 	outb(iobase + SIMODE1, 0x00);	/* Disable some more interrupts */
    819 	outb(iobase + CLRSINT1, 0xef);	/* Clear another slew of interrupts */
    820 
    821 	outb(iobase + SCSIRATE, 0);	/* Disable synchronous transfers */
    822 
    823 	outb(iobase + CLRSERR, 0x07);	/* Haven't seen ant errors (yet) */
    824 
    825 	outb(iobase + SCSIID, sc->sc_initiator << OID_S); /* Set our SCSI-ID */
    826 	outb(iobase + BRSTCNTRL, EISA_BRST_TIM);
    827 }
    828 
    829 /* Pull the SCSI RST line for 500 us */
    830 void
    831 aic_scsi_reset(sc)
    832 	struct aic_softc *sc;
    833 {
    834 	int iobase = sc->sc_iobase;
    835 
    836 	outb(iobase + SCSISEQ, SCSIRSTO);
    837 	delay(500);
    838 	outb(iobase + SCSISEQ, 0);
    839 	delay(50);
    840 }
    841 
    842 /*
    843  * Initialize aic SCSI driver.
    844  */
    845 void
    846 aic_init(sc)
    847 	struct aic_softc *sc;
    848 {
    849 	int iobase = sc->sc_iobase;
    850 	struct aic_acb *acb;
    851 	int r;
    852 
    853 	aic_reset(sc);
    854 	aic_scsi_reset(sc);
    855 	aic_reset(sc);
    856 
    857 	if (sc->sc_state == AIC_INIT) {
    858 		/* First time through; initialize. */
    859 		TAILQ_INIT(&sc->ready_list);
    860 		TAILQ_INIT(&sc->nexus_list);
    861 		TAILQ_INIT(&sc->free_list);
    862 		sc->sc_nexus = NULL;
    863 		acb = sc->sc_acb;
    864 		bzero(acb, sizeof(sc->sc_acb));
    865 		for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
    866 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    867 			acb++;
    868 		}
    869 		bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
    870 	} else {
    871 		/* Cancel any active commands. */
    872 		sc->sc_state = AIC_CLEANING;
    873 		if ((acb = sc->sc_nexus) != NULL) {
    874 			acb->xs->error = XS_DRIVER_STUFFUP;
    875 			untimeout(aic_timeout, acb);
    876 			aic_done(sc, acb);
    877 		}
    878 		while (acb = sc->nexus_list.tqh_first) {
    879 			acb->xs->error = XS_DRIVER_STUFFUP;
    880 			untimeout(aic_timeout, acb);
    881 			aic_done(sc, acb);
    882 		}
    883 	}
    884 
    885 	sc->sc_prevphase = PH_INVALID;
    886 	for (r = 0; r < 8; r++) {
    887 		struct aic_tinfo *ti = &sc->sc_tinfo[r];
    888 
    889 		ti->flags = 0;
    890 #if AIC_USE_SYNCHRONOUS
    891 		ti->flags |= DO_SYNC;
    892 		ti->period = sc->sc_minsync;
    893 		ti->offset = AIC_SYNC_REQ_ACK_OFS;
    894 #else
    895 		ti->period = ti->offset = 0;
    896 #endif
    897 #if AIC_USE_WIDE
    898 		ti->flags |= DO_WIDE;
    899 		ti->width = AIC_MAX_WIDTH;
    900 #else
    901 		ti->width = 0;
    902 #endif
    903 	}
    904 
    905 	sc->sc_state = AIC_IDLE;
    906 	outb(iobase + DMACNTRL0, INTEN);
    907 }
    908 
    909 void
    910 aic_free_acb(sc, acb, flags)
    911 	struct aic_softc *sc;
    912 	struct aic_acb *acb;
    913 	int flags;
    914 {
    915 	int s;
    916 
    917 	s = splbio();
    918 
    919 	acb->flags = 0;
    920 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    921 
    922 	/*
    923 	 * If there were none, wake anybody waiting for one to come free,
    924 	 * starting with queued entries.
    925 	 */
    926 	if (acb->chain.tqe_next == 0)
    927 		wakeup(&sc->free_list);
    928 
    929 	splx(s);
    930 }
    931 
    932 struct aic_acb *
    933 aic_get_acb(sc, flags)
    934 	struct aic_softc *sc;
    935 	int flags;
    936 {
    937 	struct aic_acb *acb;
    938 	int s;
    939 
    940 	s = splbio();
    941 
    942 	while ((acb = sc->free_list.tqh_first) == NULL &&
    943 	       (flags & SCSI_NOSLEEP) == 0)
    944 		tsleep(&sc->free_list, PRIBIO, "aicacb", 0);
    945 	if (acb) {
    946 		TAILQ_REMOVE(&sc->free_list, acb, chain);
    947 		acb->flags |= ACB_ALLOC;
    948 	}
    949 
    950 	splx(s);
    951 	return acb;
    952 }
    953 
    954 /*
    956  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    957  */
    958 
    959 /*
    960  * Expected sequence:
    961  * 1) Command inserted into ready list
    962  * 2) Command selected for execution
    963  * 3) Command won arbitration and has selected target device
    964  * 4) Send message out (identify message, eventually also sync.negotiations)
    965  * 5) Send command
    966  * 5a) Receive disconnect message, disconnect.
    967  * 5b) Reselected by target
    968  * 5c) Receive identify message from target.
    969  * 6) Send or receive data
    970  * 7) Receive status
    971  * 8) Receive message (command complete etc.)
    972  * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
    973  *    Repeat 2-8 (no disconnects please...)
    974  */
    975 
    976 /*
    977  * Start a SCSI-command
    978  * This function is called by the higher level SCSI-driver to queue/run
    979  * SCSI-commands.
    980  */
    981 int
    982 aic_scsi_cmd(xs)
    983 	struct scsi_xfer *xs;
    984 {
    985 	struct scsi_link *sc_link = xs->sc_link;
    986 	struct aic_softc *sc = sc_link->adapter_softc;
    987 	struct aic_acb *acb;
    988 	int s, flags;
    989 
    990 	AIC_TRACE(("aic_scsi_cmd  "));
    991 	AIC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    992 	    sc_link->target));
    993 
    994 	flags = xs->flags;
    995 	if ((acb = aic_get_acb(sc, flags)) == NULL) {
    996 		xs->error = XS_DRIVER_STUFFUP;
    997 		return TRY_AGAIN_LATER;
    998 	}
    999 
   1000 	/* Initialize acb */
   1001 	acb->xs = xs;
   1002 	acb->timeout = xs->timeout;
   1003 
   1004 	if (xs->flags & SCSI_RESET) {
   1005 		acb->flags |= ACB_RESET;
   1006 		acb->scsi_cmd_length = 0;
   1007 		acb->data_length = 0;
   1008 	} else {
   1009 		bcopy(xs->cmd, &acb->scsi_cmd, xs->cmdlen);
   1010 		acb->scsi_cmd_length = xs->cmdlen;
   1011 		acb->data_addr = xs->data;
   1012 		acb->data_length = xs->datalen;
   1013 	}
   1014 	acb->target_stat = 0;
   1015 
   1016 	s = splbio();
   1017 
   1018 	TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
   1019 	if (sc->sc_state == AIC_IDLE)
   1020 		aic_sched(sc);
   1021 
   1022 	splx(s);
   1023 
   1024 	if ((flags & SCSI_POLL) == 0)
   1025 		return SUCCESSFULLY_QUEUED;
   1026 
   1027 	/* Not allowed to use interrupts, use polling instead */
   1028 	if (aic_poll(sc, xs, acb->timeout)) {
   1029 		aic_timeout(acb);
   1030 		if (aic_poll(sc, xs, acb->timeout))
   1031 			aic_timeout(acb);
   1032 	}
   1033 	return COMPLETE;
   1034 }
   1035 
   1036 /*
   1037  * Adjust transfer size in buffer structure
   1038  */
   1039 void
   1040 aic_minphys(bp)
   1041 	struct buf *bp;
   1042 {
   1043 
   1044 	AIC_TRACE(("aic_minphys  "));
   1045 	if (bp->b_bcount > (AIC_NSEG << PGSHIFT))
   1046 		bp->b_bcount = (AIC_NSEG << PGSHIFT);
   1047 	minphys(bp);
   1048 }
   1049 
   1050 /*
   1051  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
   1052  */
   1053 int
   1054 aic_poll(sc, xs, count)
   1055 	struct aic_softc *sc;
   1056 	struct scsi_xfer *xs;
   1057 	int count;
   1058 {
   1059 	int iobase = sc->sc_iobase;
   1060 
   1061 	AIC_TRACE(("aic_poll  "));
   1062 	while (count) {
   1063 		/*
   1064 		 * If we had interrupts enabled, would we
   1065 		 * have got an interrupt?
   1066 		 */
   1067 		if ((inb(iobase + DMASTAT) & INTSTAT) != 0)
   1068 			aicintr(sc);
   1069 		if ((xs->flags & ITSDONE) != 0)
   1070 			return 0;
   1071 		delay(1000);
   1072 		count--;
   1073 	}
   1074 	return 1;
   1075 }
   1076 
   1077 /*
   1079  * LOW LEVEL SCSI UTILITIES
   1080  */
   1081 
   1082 integrate void
   1083 aic_sched_msgout(sc, m)
   1084 	struct aic_softc *sc;
   1085 	u_char m;
   1086 {
   1087 	int iobase = sc->sc_iobase;
   1088 
   1089 	if (sc->sc_msgpriq == 0)
   1090 		outb(iobase + SCSISIG, sc->sc_phase | ATNO);
   1091 	sc->sc_msgpriq |= m;
   1092 }
   1093 
   1094 /*
   1095  * Set synchronous transfer offset and period.
   1096  */
   1097 integrate void
   1098 aic_setsync(sc, ti)
   1099 	struct aic_softc *sc;
   1100 	struct aic_tinfo *ti;
   1101 {
   1102 #if AIC_USE_SYNCHRONOUS
   1103 	int iobase = sc->sc_iobase;
   1104 
   1105 	if (ti->offset != 0)
   1106 		outb(iobase + SCSIRATE,
   1107 		    ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
   1108 	else
   1109 		outb(iobase + SCSIRATE, 0);
   1110 #endif
   1111 }
   1112 
   1113 /*
   1114  * Start a selection.  This is used by aic_sched() to select an idle target,
   1115  * and by aic_done() to immediately reselect a target to get sense information.
   1116  */
   1117 void
   1118 aic_select(sc, acb)
   1119 	struct aic_softc *sc;
   1120 	struct aic_acb *acb;
   1121 {
   1122 	struct scsi_link *sc_link = acb->xs->sc_link;
   1123 	int target = sc_link->target;
   1124 	struct aic_tinfo *ti = &sc->sc_tinfo[target];
   1125 	int iobase = sc->sc_iobase;
   1126 
   1127 	outb(iobase + SCSIID, sc->sc_initiator << OID_S | target);
   1128 	aic_setsync(sc, ti);
   1129 	outb(iobase + SXFRCTL1, STIMO_256ms | ENSTIMER);
   1130 
   1131 	/* Always enable reselections. */
   1132 	outb(iobase + SIMODE0, ENSELDI | ENSELDO);
   1133 	outb(iobase + SIMODE1, ENSCSIRST | ENSELTIMO);
   1134 	outb(iobase + SCSISEQ, ENRESELI | ENSELO | ENAUTOATNO);
   1135 
   1136 	sc->sc_state = AIC_SELECTING;
   1137 }
   1138 
   1139 int
   1140 aic_reselect(sc, message)
   1141 	struct aic_softc *sc;
   1142 	u_char message;
   1143 {
   1144 	u_char selid, target, lun;
   1145 	struct aic_acb *acb;
   1146 	struct scsi_link *sc_link;
   1147 	struct aic_tinfo *ti;
   1148 
   1149 	/*
   1150 	 * The SCSI chip made a snapshot of the data bus while the reselection
   1151 	 * was being negotiated.  This enables us to determine which target did
   1152 	 * the reselect.
   1153 	 */
   1154 	selid = sc->sc_selid & ~(1 << sc->sc_initiator);
   1155 	if (selid & (selid - 1)) {
   1156 		printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
   1157 		    sc->sc_dev.dv_xname, selid);
   1158 		AIC_BREAK();
   1159 		goto reset;
   1160 	}
   1161 
   1162 	/* Search wait queue for disconnected cmd
   1163 	 * The list should be short, so I haven't bothered with
   1164 	 * any more sophisticated structures than a simple
   1165 	 * singly linked list.
   1166 	 */
   1167 	target = ffs(selid) - 1;
   1168 	lun = message & 0x07;
   1169 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   1170 	     acb = acb->chain.tqe_next) {
   1171 		sc_link = acb->xs->sc_link;
   1172 		if (sc_link->target == target && sc_link->lun == lun)
   1173 			break;
   1174 	}
   1175 	if (acb == NULL) {
   1176 		printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
   1177 		    sc->sc_dev.dv_xname, target, lun);
   1178 		AIC_BREAK();
   1179 		goto abort;
   1180 	}
   1181 
   1182 	/* Make this nexus active again. */
   1183 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1184 	sc->sc_state = AIC_CONNECTED;
   1185 	sc->sc_nexus = acb;
   1186 	ti = &sc->sc_tinfo[target];
   1187 	ti->lubusy |= (1 << lun);
   1188 	aic_setsync(sc, ti);
   1189 
   1190 	if (acb->flags & ACB_RESET)
   1191 		aic_sched_msgout(sc, SEND_DEV_RESET);
   1192 	else if (acb->flags & ACB_ABORT)
   1193 		aic_sched_msgout(sc, SEND_ABORT);
   1194 
   1195 	/* Do an implicit RESTORE POINTERS. */
   1196 	sc->sc_dp = acb->data_addr;
   1197 	sc->sc_dleft = acb->data_length;
   1198 	sc->sc_cp = (u_char *)&acb->scsi_cmd;
   1199 	sc->sc_cleft = acb->scsi_cmd_length;
   1200 
   1201 	return (0);
   1202 
   1203 reset:
   1204 	aic_sched_msgout(sc, SEND_DEV_RESET);
   1205 	return (1);
   1206 
   1207 abort:
   1208 	aic_sched_msgout(sc, SEND_ABORT);
   1209 	return (1);
   1210 }
   1211 
   1212 /*
   1214  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
   1215  * handler so that we may call it from aic_scsi_cmd and aic_done.  This may
   1216  * save us an unecessary interrupt just to get things going.  Should only be
   1217  * called when state == AIC_IDLE and at bio pl.
   1218  */
   1219 void
   1220 aic_sched(sc)
   1221 	register struct aic_softc *sc;
   1222 {
   1223 	struct aic_acb *acb;
   1224 	struct scsi_link *sc_link;
   1225 	struct aic_tinfo *ti;
   1226 	int iobase = sc->sc_iobase;
   1227 
   1228 	/*
   1229 	 * Find first acb in ready queue that is for a target/lunit pair that
   1230 	 * is not busy.
   1231 	 */
   1232 	outb(iobase + CLRSINT1, CLRSELTIMO | CLRBUSFREE | CLRSCSIPERR);
   1233 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   1234 	    acb = acb->chain.tqe_next) {
   1235 		sc_link = acb->xs->sc_link;
   1236 		ti = &sc->sc_tinfo[sc_link->target];
   1237 		if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
   1238 			AIC_MISC(("selecting %d:%d  ",
   1239 			    sc_link->target, sc_link->lun));
   1240 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1241 			sc->sc_nexus = acb;
   1242 			aic_select(sc, acb);
   1243 			return;
   1244 		} else
   1245 			AIC_MISC(("%d:%d busy\n",
   1246 			    sc_link->target, sc_link->lun));
   1247 	}
   1248 	AIC_MISC(("idle  "));
   1249 	/* Nothing to start; just enable reselections and wait. */
   1250 	outb(iobase + SIMODE0, ENSELDI);
   1251 	outb(iobase + SIMODE1, ENSCSIRST);
   1252 	outb(iobase + SCSISEQ, ENRESELI);
   1253 }
   1254 
   1255 void
   1257 aic_sense(sc, acb)
   1258 	struct aic_softc *sc;
   1259 	struct aic_acb *acb;
   1260 {
   1261 	struct scsi_xfer *xs = acb->xs;
   1262 	struct scsi_link *sc_link = xs->sc_link;
   1263 	struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->target];
   1264 	struct scsi_sense *ss = (void *)&acb->scsi_cmd;
   1265 
   1266 	AIC_MISC(("requesting sense  "));
   1267 	/* Next, setup a request sense command block */
   1268 	bzero(ss, sizeof(*ss));
   1269 	ss->opcode = REQUEST_SENSE;
   1270 	ss->byte2 = sc_link->lun << 5;
   1271 	ss->length = sizeof(struct scsi_sense_data);
   1272 	acb->scsi_cmd_length = sizeof(*ss);
   1273 	acb->data_addr = (char *)&xs->sense;
   1274 	acb->data_length = sizeof(struct scsi_sense_data);
   1275 	acb->flags |= ACB_SENSE;
   1276 	ti->senses++;
   1277 	if (acb->flags & ACB_NEXUS)
   1278 		ti->lubusy &= ~(1 << sc_link->lun);
   1279 	if (acb == sc->sc_nexus) {
   1280 		aic_select(sc, acb);
   1281 	} else {
   1282 		aic_dequeue(sc, acb);
   1283 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   1284 		if (sc->sc_state == AIC_IDLE)
   1285 			aic_sched(sc);
   1286 	}
   1287 }
   1288 
   1289 /*
   1290  * POST PROCESSING OF SCSI_CMD (usually current)
   1291  */
   1292 void
   1293 aic_done(sc, acb)
   1294 	struct aic_softc *sc;
   1295 	struct aic_acb *acb;
   1296 {
   1297 	struct scsi_xfer *xs = acb->xs;
   1298 	struct scsi_link *sc_link = xs->sc_link;
   1299 	struct aic_tinfo *ti = &sc->sc_tinfo[sc_link->target];
   1300 
   1301 	AIC_TRACE(("aic_done  "));
   1302 
   1303 	/*
   1304 	 * Now, if we've come here with no error code, i.e. we've kept the
   1305 	 * initial XS_NOERROR, and the status code signals that we should
   1306 	 * check sense, we'll need to set up a request sense cmd block and
   1307 	 * push the command back into the ready queue *before* any other
   1308 	 * commands for this target/lunit, else we lose the sense info.
   1309 	 * We don't support chk sense conditions for the request sense cmd.
   1310 	 */
   1311 	if (xs->error == XS_NOERROR) {
   1312 		if (acb->flags & ACB_ABORT) {
   1313 			xs->error = XS_DRIVER_STUFFUP;
   1314 		} else if (acb->flags & ACB_SENSE) {
   1315 			xs->error = XS_SENSE;
   1316 		} else if (acb->target_stat == SCSI_CHECK) {
   1317 			/* First, save the return values */
   1318 			xs->resid = acb->data_length;
   1319 			xs->status = acb->target_stat;
   1320 			aic_sense(sc, acb);
   1321 			return;
   1322 		} else {
   1323 			xs->resid = acb->data_length;
   1324 		}
   1325 	}
   1326 
   1327 	xs->flags |= ITSDONE;
   1328 
   1329 #if AIC_DEBUG
   1330 	if ((aic_debug & AIC_SHOWMISC) != 0) {
   1331 		if (xs->resid != 0)
   1332 			printf("resid=%d ", xs->resid);
   1333 		if (xs->error == XS_SENSE)
   1334 			printf("sense=0x%02x\n", xs->sense.error_code);
   1335 		else
   1336 			printf("error=%d\n", xs->error);
   1337 	}
   1338 #endif
   1339 
   1340 	/*
   1341 	 * Remove the ACB from whatever queue it happens to be on.
   1342 	 */
   1343 	if (acb->flags & ACB_NEXUS)
   1344 		ti->lubusy &= ~(1 << sc_link->lun);
   1345 	if (acb == sc->sc_nexus) {
   1346 		sc->sc_nexus = NULL;
   1347 		sc->sc_state = AIC_IDLE;
   1348 		aic_sched(sc);
   1349 	} else
   1350 		aic_dequeue(sc, acb);
   1351 
   1352 	aic_free_acb(sc, acb, xs->flags);
   1353 	ti->cmds++;
   1354 	scsi_done(xs);
   1355 }
   1356 
   1357 void
   1358 aic_dequeue(sc, acb)
   1359 	struct aic_softc *sc;
   1360 	struct aic_acb *acb;
   1361 {
   1362 
   1363 	if (acb->flags & ACB_NEXUS) {
   1364 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1365 	} else {
   1366 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1367 	}
   1368 }
   1369 
   1370 /*
   1372  * INTERRUPT/PROTOCOL ENGINE
   1373  */
   1374 
   1375 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
   1376 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1377 #define ISEXTMSG(m) ((m) == 0x01)
   1378 
   1379 /*
   1380  * Precondition:
   1381  * The SCSI bus is already in the MSGI phase and there is a message byte
   1382  * on the bus, along with an asserted REQ signal.
   1383  */
   1384 void
   1385 aic_msgin(sc)
   1386 	register struct aic_softc *sc;
   1387 {
   1388 	int iobase = sc->sc_iobase;
   1389 	u_char sstat1;
   1390 	int n;
   1391 
   1392 	AIC_TRACE(("aic_msgin  "));
   1393 
   1394 	if (sc->sc_prevphase == PH_MSGIN) {
   1395 		/* This is a continuation of the previous message. */
   1396 		n = sc->sc_imp - sc->sc_imess;
   1397 		goto nextbyte;
   1398 	}
   1399 
   1400 	/* This is a new MESSAGE IN phase.  Clean up our state. */
   1401 	sc->sc_flags &= ~AIC_DROP_MSGIN;
   1402 
   1403 nextmsg:
   1404 	n = 0;
   1405 	sc->sc_imp = &sc->sc_imess[n];
   1406 
   1407 nextbyte:
   1408 	/*
   1409 	 * Read a whole message, but don't ack the last byte.  If we reject the
   1410 	 * message, we have to assert ATN during the message transfer phase
   1411 	 * itself.
   1412 	 */
   1413 	for (;;) {
   1414 		for (;;) {
   1415 			sstat1 = inb(iobase + SSTAT1);
   1416 			if ((sstat1 & (REQINIT | BUSFREE)) != 0)
   1417 				break;
   1418 			/* Wait for REQINIT.  XXX Need timeout. */
   1419 		}
   1420 		if ((sstat1 & (PHASECHG | BUSFREE)) != 0) {
   1421 			/*
   1422 			 * Target left MESSAGE IN, probably because it
   1423 			 * a) noticed our ATN signal, or
   1424 			 * b) ran out of messages.
   1425 			 */
   1426 			goto out;
   1427 		}
   1428 
   1429 		/* If parity error, just dump everything on the floor. */
   1430 		if ((sstat1 & SCSIPERR) != 0) {
   1431 			sc->sc_flags |= AIC_DROP_MSGIN;
   1432 			aic_sched_msgout(sc, SEND_PARITY_ERROR);
   1433 		}
   1434 
   1435 		/* Gather incoming message bytes if needed. */
   1436 		if ((sc->sc_flags & AIC_DROP_MSGIN) == 0) {
   1437 			if (n >= AIC_MAX_MSG_LEN) {
   1438 				(void) inb(iobase + SCSIDAT);
   1439 				sc->sc_flags |= AIC_DROP_MSGIN;
   1440 				aic_sched_msgout(sc, SEND_REJECT);
   1441 			} else {
   1442 				*sc->sc_imp++ = inb(iobase + SCSIDAT);
   1443 				n++;
   1444 				/*
   1445 				 * This testing is suboptimal, but most
   1446 				 * messages will be of the one byte variety, so
   1447 				 * it should not affect performance
   1448 				 * significantly.
   1449 				 */
   1450 				if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1451 					break;
   1452 				if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1453 					break;
   1454 				if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1455 				    n == sc->sc_imess[1] + 2)
   1456 					break;
   1457 			}
   1458 		} else
   1459 			(void) inb(iobase + SCSIDAT);
   1460 
   1461 		/*
   1462 		 * If we reach this spot we're either:
   1463 		 * a) in the middle of a multi-byte message, or
   1464 		 * b) dropping bytes.
   1465 		 */
   1466 		outb(iobase + SXFRCTL0, CHEN | SPIOEN);
   1467 		/* Ack the last byte read. */
   1468 		(void) inb(iobase + SCSIDAT);
   1469 		outb(iobase + SXFRCTL0, CHEN);
   1470 		while ((inb(iobase + SCSISIG) & ACKI) != 0)
   1471 			;
   1472 	}
   1473 
   1474 	AIC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1475 
   1476 	/* We now have a complete message.  Parse it. */
   1477 	switch (sc->sc_state) {
   1478 		struct aic_acb *acb;
   1479 		struct scsi_link *sc_link;
   1480 		struct aic_tinfo *ti;
   1481 
   1482 	case AIC_CONNECTED:
   1483 		AIC_ASSERT(sc->sc_nexus != NULL);
   1484 		acb = sc->sc_nexus;
   1485 		ti = &sc->sc_tinfo[acb->xs->sc_link->target];
   1486 
   1487 		switch (sc->sc_imess[0]) {
   1488 		case MSG_CMDCOMPLETE:
   1489 			if (sc->sc_dleft < 0) {
   1490 				sc_link = acb->xs->sc_link;
   1491 				printf("%s: %d extra bytes from %d:%d\n",
   1492 				    sc->sc_dev.dv_xname, -sc->sc_dleft,
   1493 				    sc_link->target, sc_link->lun);
   1494 				acb->data_length = 0;
   1495 			}
   1496 			acb->xs->resid = acb->data_length = sc->sc_dleft;
   1497 			sc->sc_state = AIC_CMDCOMPLETE;
   1498 			break;
   1499 
   1500 		case MSG_PARITY_ERROR:
   1501 			/* Resend the last message. */
   1502 			aic_sched_msgout(sc, sc->sc_lastmsg);
   1503 			break;
   1504 
   1505 		case MSG_MESSAGE_REJECT:
   1506 			AIC_MISC(("message rejected %02x  ", sc->sc_lastmsg));
   1507 			switch (sc->sc_lastmsg) {
   1508 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
   1509 			case SEND_IDENTIFY:
   1510 				ti->flags &= ~(DO_SYNC | DO_WIDE);
   1511 				ti->period = ti->offset = 0;
   1512 				aic_setsync(sc, ti);
   1513 				ti->width = 0;
   1514 				break;
   1515 #endif
   1516 #if AIC_USE_SYNCHRONOUS
   1517 			case SEND_SDTR:
   1518 				ti->flags &= ~DO_SYNC;
   1519 				ti->period = ti->offset = 0;
   1520 				aic_setsync(sc, ti);
   1521 				break;
   1522 #endif
   1523 #if AIC_USE_WIDE
   1524 			case SEND_WDTR:
   1525 				ti->flags &= ~DO_WIDE;
   1526 				ti->width = 0;
   1527 				break;
   1528 #endif
   1529 			case SEND_INIT_DET_ERR:
   1530 				aic_sched_msgout(sc, SEND_ABORT);
   1531 				break;
   1532 			}
   1533 			break;
   1534 
   1535 		case MSG_NOOP:
   1536 			break;
   1537 
   1538 		case MSG_DISCONNECT:
   1539 			ti->dconns++;
   1540 			sc->sc_state = AIC_DISCONNECT;
   1541 			break;
   1542 
   1543 		case MSG_SAVEDATAPOINTER:
   1544 			acb->data_addr = sc->sc_dp;
   1545 			acb->data_length = sc->sc_dleft;
   1546 			break;
   1547 
   1548 		case MSG_RESTOREPOINTERS:
   1549 			sc->sc_dp = acb->data_addr;
   1550 			sc->sc_dleft = acb->data_length;
   1551 			sc->sc_cp = (u_char *)&acb->scsi_cmd;
   1552 			sc->sc_cleft = acb->scsi_cmd_length;
   1553 			break;
   1554 
   1555 		case MSG_EXTENDED:
   1556 			switch (sc->sc_imess[2]) {
   1557 #if AIC_USE_SYNCHRONOUS
   1558 			case MSG_EXT_SDTR:
   1559 				if (sc->sc_imess[1] != 3)
   1560 					goto reject;
   1561 				ti->period = sc->sc_imess[3];
   1562 				ti->offset = sc->sc_imess[4];
   1563 				ti->flags &= ~DO_SYNC;
   1564 				if (ti->offset == 0) {
   1565 				} else if (ti->period < sc->sc_minsync ||
   1566 					   ti->period > sc->sc_maxsync ||
   1567 					   ti->offset > 8) {
   1568 					ti->period = ti->offset = 0;
   1569 					aic_sched_msgout(sc, SEND_SDTR);
   1570 				} else {
   1571 					sc_print_addr(acb->xs->sc_link);
   1572 					printf("sync, offset %d, period %dnsec\n",
   1573 					    ti->offset, ti->period * 4);
   1574 				}
   1575 				aic_setsync(sc, ti);
   1576 				break;
   1577 #endif
   1578 
   1579 #if AIC_USE_WIDE
   1580 			case MSG_EXT_WDTR:
   1581 				if (sc->sc_imess[1] != 2)
   1582 					goto reject;
   1583 				ti->width = sc->sc_imess[3];
   1584 				ti->flags &= ~DO_WIDE;
   1585 				if (ti->width == 0) {
   1586 				} else if (ti->width > AIC_MAX_WIDTH) {
   1587 					ti->width = 0;
   1588 					aic_sched_msgout(sc, SEND_WDTR);
   1589 				} else {
   1590 					sc_print_addr(acb->xs->sc_link);
   1591 					printf("wide, width %d\n",
   1592 					    1 << (3 + ti->width));
   1593 				}
   1594 				break;
   1595 #endif
   1596 
   1597 			default:
   1598 				printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
   1599 				    sc->sc_dev.dv_xname);
   1600 				AIC_BREAK();
   1601 				goto reject;
   1602 			}
   1603 			break;
   1604 
   1605 		default:
   1606 			printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1607 			    sc->sc_dev.dv_xname);
   1608 			AIC_BREAK();
   1609 		reject:
   1610 			aic_sched_msgout(sc, SEND_REJECT);
   1611 			break;
   1612 		}
   1613 		break;
   1614 
   1615 	case AIC_RESELECTED:
   1616 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1617 			printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
   1618 			    sc->sc_dev.dv_xname);
   1619 			AIC_BREAK();
   1620 			goto reset;
   1621 		}
   1622 
   1623 		(void) aic_reselect(sc, sc->sc_imess[0]);
   1624 		break;
   1625 
   1626 	default:
   1627 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1628 		    sc->sc_dev.dv_xname);
   1629 		AIC_BREAK();
   1630 	reset:
   1631 		aic_sched_msgout(sc, SEND_DEV_RESET);
   1632 		break;
   1633 
   1634 	abort:
   1635 		aic_sched_msgout(sc, SEND_ABORT);
   1636 		break;
   1637 	}
   1638 
   1639 	outb(iobase + SXFRCTL0, CHEN | SPIOEN);
   1640 	/* Ack the last message byte. */
   1641 	(void) inb(iobase + SCSIDAT);
   1642 	outb(iobase + SXFRCTL0, CHEN);
   1643 	while ((inb(iobase + SCSISIG) & ACKI) != 0)
   1644 		;
   1645 
   1646 	/* Go get the next message, if any. */
   1647 	goto nextmsg;
   1648 
   1649 out:
   1650 	AIC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1651 }
   1652 
   1653 /*
   1654  * Send the highest priority, scheduled message.
   1655  */
   1656 void
   1657 aic_msgout(sc)
   1658 	register struct aic_softc *sc;
   1659 {
   1660 	int iobase = sc->sc_iobase;
   1661 	struct aic_tinfo *ti;
   1662 	u_char sstat1;
   1663 	int n;
   1664 
   1665 	AIC_TRACE(("aic_msgout  "));
   1666 
   1667 	/*
   1668 	 * Set ATN.  If we're just sending a trivial 1-byte message, we'll
   1669 	 * clear ATN later on anyway.
   1670 	 */
   1671 	outb(iobase + SCSISIG, PH_MSGOUT | ATNO);
   1672 	/* Reset the FIFO. */
   1673 	outb(iobase + DMACNTRL0, RSTFIFO);
   1674 	/* Enable REQ/ACK protocol. */
   1675 	outb(iobase + SXFRCTL0, CHEN | SPIOEN);
   1676 
   1677 	if (sc->sc_prevphase == PH_MSGOUT) {
   1678 		if (sc->sc_omp == sc->sc_omess) {
   1679 			/*
   1680 			 * This is a retransmission.
   1681 			 *
   1682 			 * We get here if the target stayed in MESSAGE OUT
   1683 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1684 			 * that all of the previously transmitted messages must
   1685 			 * be sent again, in the same order.  Therefore, we
   1686 			 * requeue all the previously transmitted messages, and
   1687 			 * start again from the top.  Our simple priority
   1688 			 * scheme keeps the messages in the right order.
   1689 			 */
   1690 			AIC_MISC(("retransmitting  "));
   1691 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1692 		} else {
   1693 			/* This is a continuation of the previous message. */
   1694 			n = sc->sc_omp - sc->sc_omess;
   1695 			goto nextbyte;
   1696 		}
   1697 	}
   1698 
   1699 	/* No messages transmitted so far. */
   1700 	sc->sc_msgoutq = 0;
   1701 	sc->sc_lastmsg = 0;
   1702 
   1703 nextmsg:
   1704 	/* Pick up highest priority message. */
   1705 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1706 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1707 	sc->sc_msgoutq |= sc->sc_currmsg;
   1708 
   1709 	/* Build the outgoing message data. */
   1710 	switch (sc->sc_currmsg) {
   1711 	case SEND_IDENTIFY:
   1712 		AIC_ASSERT(sc->sc_nexus != NULL);
   1713 		sc->sc_omess[0] =
   1714 		    MSG_IDENTIFY(sc->sc_nexus->xs->sc_link->lun, 1);
   1715 		n = 1;
   1716 		break;
   1717 
   1718 #if AIC_USE_SYNCHRONOUS
   1719 	case SEND_SDTR:
   1720 		AIC_ASSERT(sc->sc_nexus != NULL);
   1721 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
   1722 		sc->sc_omess[4] = MSG_EXTENDED;
   1723 		sc->sc_omess[3] = 3;
   1724 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1725 		sc->sc_omess[1] = ti->period >> 2;
   1726 		sc->sc_omess[0] = ti->offset;
   1727 		n = 5;
   1728 		break;
   1729 #endif
   1730 
   1731 #if AIC_USE_WIDE
   1732 	case SEND_WDTR:
   1733 		AIC_ASSERT(sc->sc_nexus != NULL);
   1734 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
   1735 		sc->sc_omess[3] = MSG_EXTENDED;
   1736 		sc->sc_omess[2] = 2;
   1737 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1738 		sc->sc_omess[0] = ti->width;
   1739 		n = 4;
   1740 		break;
   1741 #endif
   1742 
   1743 	case SEND_DEV_RESET:
   1744 		sc->sc_flags |= AIC_ABORTING;
   1745 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1746 		n = 1;
   1747 		break;
   1748 
   1749 	case SEND_REJECT:
   1750 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1751 		n = 1;
   1752 		break;
   1753 
   1754 	case SEND_PARITY_ERROR:
   1755 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1756 		n = 1;
   1757 		break;
   1758 
   1759 	case SEND_INIT_DET_ERR:
   1760 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1761 		n = 1;
   1762 		break;
   1763 
   1764 	case SEND_ABORT:
   1765 		sc->sc_flags |= AIC_ABORTING;
   1766 		sc->sc_omess[0] = MSG_ABORT;
   1767 		n = 1;
   1768 		break;
   1769 
   1770 	default:
   1771 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1772 		    sc->sc_dev.dv_xname);
   1773 		AIC_BREAK();
   1774 		sc->sc_omess[0] = MSG_NOOP;
   1775 		n = 1;
   1776 		break;
   1777 	}
   1778 	sc->sc_omp = &sc->sc_omess[n];
   1779 
   1780 nextbyte:
   1781 	/* Send message bytes. */
   1782 	for (;;) {
   1783 		for (;;) {
   1784 			sstat1 = inb(iobase + SSTAT1);
   1785 			if ((sstat1 & (REQINIT | BUSFREE)) != 0)
   1786 				break;
   1787 			/* Wait for REQINIT.  XXX Need timeout. */
   1788 		}
   1789 		if ((sstat1 & (PHASECHG | BUSFREE)) != 0) {
   1790 			/*
   1791 			 * Target left MESSAGE OUT, possibly to reject
   1792 			 * our message.
   1793 			 *
   1794 			 * If this is the last message being sent, then we
   1795 			 * deassert ATN, since either the target is going to
   1796 			 * ignore this message, or it's going to ask for a
   1797 			 * retransmission via MESSAGE PARITY ERROR (in which
   1798 			 * case we reassert ATN anyway).
   1799 			 */
   1800 			if (sc->sc_msgpriq == 0)
   1801 				outb(iobase + CLRSINT1, CLRATNO);
   1802 			goto out;
   1803 		}
   1804 
   1805 		/* Clear ATN before last byte if this is the last message. */
   1806 		if (n == 1 && sc->sc_msgpriq == 0)
   1807 			outb(iobase + CLRSINT1, CLRATNO);
   1808 		/* Send message byte. */
   1809 		outb(iobase + SCSIDAT, *--sc->sc_omp);
   1810 		--n;
   1811 		/* Keep track of the last message we've sent any bytes of. */
   1812 		sc->sc_lastmsg = sc->sc_currmsg;
   1813 		/* Wait for ACK to be negated.  XXX Need timeout. */
   1814 		while ((inb(iobase + SCSISIG) & ACKI) != 0)
   1815 			;
   1816 
   1817 		if (n == 0)
   1818 			break;
   1819 	}
   1820 
   1821 	/* We get here only if the entire message has been transmitted. */
   1822 	if (sc->sc_msgpriq != 0) {
   1823 		/* There are more outgoing messages. */
   1824 		goto nextmsg;
   1825 	}
   1826 
   1827 	/*
   1828 	 * The last message has been transmitted.  We need to remember the last
   1829 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1830 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1831 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1832 	 * request a retransmit).
   1833 	 */
   1834 
   1835 out:
   1836 	/* Disable REQ/ACK protocol. */
   1837 	outb(iobase + SXFRCTL0, CHEN);
   1838 }
   1839 
   1840 /* aic_dataout_pio: perform a data transfer using the FIFO datapath in the aic6360
   1842  * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
   1843  * and ACK deasserted (i.e. waiting for a data byte)
   1844  * This new revision has been optimized (I tried) to make the common case fast,
   1845  * and the rarer cases (as a result) somewhat more comlex
   1846  */
   1847 int
   1848 aic_dataout_pio(sc, p, n)
   1849 	register struct aic_softc *sc;
   1850 	u_char *p;
   1851 	int n;
   1852 {
   1853 	int iobase = sc->sc_iobase;
   1854 	register u_char dmastat;
   1855 	int out = 0;
   1856 #define DOUTAMOUNT 128		/* Full FIFO */
   1857 
   1858 	/* Clear host FIFO and counter. */
   1859 	outb(iobase + DMACNTRL0, RSTFIFO | WRITE);
   1860 	/* Enable FIFOs. */
   1861 	outb(iobase + SXFRCTL0, SCSIEN | DMAEN | CHEN);
   1862 	outb(iobase + DMACNTRL0, ENDMA | DWORDPIO | WRITE);
   1863 
   1864 	/* Turn off ENREQINIT for now. */
   1865 	outb(iobase + SIMODE1,
   1866 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENPHASECHG);
   1867 
   1868 	/* I have tried to make the main loop as tight as possible.  This
   1869 	 * means that some of the code following the loop is a bit more
   1870 	 * complex than otherwise.
   1871 	 */
   1872 	while (n > 0) {
   1873 		for (;;) {
   1874 			dmastat = inb(iobase + DMASTAT);
   1875 			if ((dmastat & (DFIFOEMP | INTSTAT)) != 0)
   1876 				break;
   1877 		}
   1878 
   1879 		if ((dmastat & INTSTAT) != 0)
   1880 			goto phasechange;
   1881 
   1882 		if (n >= DOUTAMOUNT) {
   1883 			n -= DOUTAMOUNT;
   1884 			out += DOUTAMOUNT;
   1885 
   1886 #if AIC_USE_DWORDS
   1887 			outsl(iobase + DMADATALONG, p, DOUTAMOUNT >> 2);
   1888 #else
   1889 			outsw(iobase + DMADATA, p, DOUTAMOUNT >> 1);
   1890 #endif
   1891 
   1892 			p += DOUTAMOUNT;
   1893 		} else {
   1894 			register int xfer;
   1895 
   1896 			xfer = n;
   1897 			AIC_MISC(("%d> ", xfer));
   1898 
   1899 			n -= xfer;
   1900 			out += xfer;
   1901 
   1902 #if AIC_USE_DWORDS
   1903 			if (xfer >= 12) {
   1904 				outsl(iobase + DMADATALONG, p, xfer >> 2);
   1905 				p += xfer & ~3;
   1906 				xfer &= 3;
   1907 			}
   1908 #else
   1909 			if (xfer >= 8) {
   1910 				outsw(iobase + DMADATA, p, xfer >> 1);
   1911 				p += xfer & ~1;
   1912 				xfer &= 1;
   1913 			}
   1914 #endif
   1915 
   1916 			if (xfer > 0) {
   1917 				outb(iobase + DMACNTRL0, ENDMA | B8MODE | WRITE);
   1918 				outsb(iobase + DMADATA, p, xfer);
   1919 				p += xfer;
   1920 				outb(iobase + DMACNTRL0, ENDMA | DWORDPIO | WRITE);
   1921 			}
   1922 		}
   1923 	}
   1924 
   1925 	if (out == 0) {
   1926 		outb(iobase + SXFRCTL1, BITBUCKET);
   1927 		for (;;) {
   1928 			if ((inb(iobase + DMASTAT) & INTSTAT) != 0)
   1929 				break;
   1930 		}
   1931 		outb(iobase + SXFRCTL1, 0);
   1932 		AIC_MISC(("extra data  "));
   1933 	} else {
   1934 		/* See the bytes off chip */
   1935 		for (;;) {
   1936 			dmastat = inb(iobase + DMASTAT);
   1937 			if ((dmastat & INTSTAT) != 0)
   1938 				goto phasechange;
   1939 			if ((dmastat & DFIFOEMP) != 0 &&
   1940 			    (inb(iobase + SSTAT2) & SEMPTY) != 0)
   1941 				break;
   1942 		}
   1943 	}
   1944 
   1945 phasechange:
   1946 	/* Stop the FIFO data path. */
   1947 	outb(iobase + SXFRCTL0, CHEN);
   1948 	while ((inb(iobase + SXFRCTL0) & SCSIEN) != 0)
   1949 		;
   1950 
   1951 	if ((dmastat & INTSTAT) != 0) {
   1952 		/* Some sort of phase change. */
   1953 		int amount;
   1954 
   1955 		/* Stop transfers, do some accounting */
   1956 		amount = inb(iobase + FIFOSTAT) + (inb(iobase + SSTAT2) & 15);
   1957 		if (amount > 0) {
   1958 			out -= amount;
   1959 			outb(iobase + SXFRCTL0, CHEN | CLRSTCNT | CLRCH);
   1960 			AIC_MISC(("+%d ", amount));
   1961 		}
   1962 	}
   1963 
   1964 	/* Turn on ENREQINIT again. */
   1965 	outb(iobase + SIMODE1,
   1966 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
   1967 
   1968 	return out;
   1969 }
   1970 
   1971 /* aic_datain_pio: perform data transfers using the FIFO datapath in the aic6360
   1973  * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
   1974  * and ACK deasserted (i.e. at least one byte is ready).
   1975  * For now, uses a pretty dumb algorithm, hangs around until all data has been
   1976  * transferred.  This, is OK for fast targets, but not so smart for slow
   1977  * targets which don't disconnect or for huge transfers.
   1978  */
   1979 int
   1980 aic_datain_pio(sc, p, n)
   1981 	register struct aic_softc *sc;
   1982 	u_char *p;
   1983 	int n;
   1984 {
   1985 	int iobase = sc->sc_iobase;
   1986 	register u_char dmastat;
   1987 	int in = 0;
   1988 #define DINAMOUNT 128		/* Full FIFO */
   1989 
   1990 	/* Clear host FIFO and counter. */
   1991 	outb(iobase + DMACNTRL0, RSTFIFO);
   1992 	/* Enable FIFOs. */
   1993 	outb(iobase + SXFRCTL0, SCSIEN | DMAEN | CHEN);
   1994 	outb(iobase + DMACNTRL0, ENDMA | DWORDPIO);
   1995 
   1996 	/* Turn off ENREQINIT for now. */
   1997 	outb(iobase + SIMODE1,
   1998 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENPHASECHG);
   1999 
   2000 	/* We leave this loop if one or more of the following is true:
   2001 	 * a) phase != PH_DATAIN && FIFOs are empty
   2002 	 * b) SCSIRSTI is set (a reset has occurred) or busfree is detected.
   2003 	 */
   2004 	while (n > 0) {
   2005 		/* Wait for fifo half full or phase mismatch */
   2006 		for (;;) {
   2007 			dmastat = inb(iobase + DMASTAT);
   2008 			if ((dmastat & (DFIFOFULL | INTSTAT)) != 0)
   2009 				break;
   2010 		}
   2011 
   2012 		if ((dmastat & DFIFOFULL) != 0) {
   2013 			n -= DINAMOUNT;
   2014 			in += DINAMOUNT;
   2015 
   2016 #if AIC_USE_DWORDS
   2017 			insl(iobase + DMADATALONG, p, DINAMOUNT >> 2);
   2018 #else
   2019 			insw(iobase + DMADATA, p, DINAMOUNT >> 1);
   2020 #endif
   2021 
   2022 			p += DINAMOUNT;
   2023 		} else {
   2024 			register int xfer;
   2025 
   2026 			xfer = min(inb(iobase + FIFOSTAT), n);
   2027 			AIC_MISC((">%d ", xfer));
   2028 
   2029 			n -= xfer;
   2030 			in += xfer;
   2031 
   2032 #if AIC_USE_DWORDS
   2033 			if (xfer >= 12) {
   2034 				insl(iobase + DMADATALONG, p, xfer >> 2);
   2035 				p += xfer & ~3;
   2036 				xfer &= 3;
   2037 			}
   2038 #else
   2039 			if (xfer >= 8) {
   2040 				insw(iobase + DMADATA, p, xfer >> 1);
   2041 				p += xfer & ~1;
   2042 				xfer &= 1;
   2043 			}
   2044 #endif
   2045 
   2046 			if (xfer > 0) {
   2047 				outb(iobase + DMACNTRL0, ENDMA | B8MODE);
   2048 				insb(iobase + DMADATA, p, xfer);
   2049 				p += xfer;
   2050 				outb(iobase + DMACNTRL0, ENDMA | DWORDPIO);
   2051 			}
   2052 		}
   2053 
   2054 		if ((dmastat & INTSTAT) != 0)
   2055 			goto phasechange;
   2056 	}
   2057 
   2058 	/* Some SCSI-devices are rude enough to transfer more data than what
   2059 	 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
   2060 	 * requested 512.  Test for progress, i.e. real transfers.  If no real
   2061 	 * transfers have been performed (n is probably already zero) and the
   2062 	 * FIFO is not empty, waste some bytes....
   2063 	 */
   2064 	if (in == 0) {
   2065 		outb(iobase + SXFRCTL1, BITBUCKET);
   2066 		for (;;) {
   2067 			if ((inb(iobase + DMASTAT) & INTSTAT) != 0)
   2068 				break;
   2069 		}
   2070 		outb(iobase + SXFRCTL1, 0);
   2071 		AIC_MISC(("extra data  "));
   2072 	}
   2073 
   2074 phasechange:
   2075 	/* Stop the FIFO data path. */
   2076 	outb(iobase + SXFRCTL0, CHEN);
   2077 	while ((inb(iobase + SXFRCTL0) & SCSIEN) != 0)
   2078 		;
   2079 
   2080 	/* Turn on ENREQINIT again. */
   2081 	outb(iobase + SIMODE1,
   2082 	    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
   2083 
   2084 	return in;
   2085 }
   2086 
   2087 /*
   2089  * This is the workhorse routine of the driver.
   2090  * Deficiencies (for now):
   2091  * 1) always uses programmed I/O
   2092  */
   2093 int
   2094 aicintr(arg)
   2095 	void *arg;
   2096 {
   2097 	register struct aic_softc *sc = arg;
   2098 	int iobase = sc->sc_iobase;
   2099 	u_char sstat0, sstat1;
   2100 	register struct aic_acb *acb;
   2101 	register struct scsi_link *sc_link;
   2102 	struct aic_tinfo *ti;
   2103 	int n;
   2104 
   2105 	/*
   2106 	 * Clear INTEN.  We enable it again before returning.  This makes the
   2107 	 * interrupt esssentially level-triggered.
   2108 	 */
   2109 	outb(iobase + DMACNTRL0, 0);
   2110 
   2111 	AIC_TRACE(("aicintr  "));
   2112 
   2113 loop:
   2114 	/*
   2115 	 * First check for abnormal conditions, such as reset.
   2116 	 */
   2117 	sstat1 = inb(iobase + SSTAT1);
   2118 	AIC_MISC(("sstat1:0x%02x ", sstat1));
   2119 
   2120 	if ((sstat1 & SCSIRSTI) != 0) {
   2121 		printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
   2122 		goto reset;
   2123 	}
   2124 
   2125 	/*
   2126 	 * Check for less serious errors.
   2127 	 */
   2128 	if ((sstat1 & SCSIPERR) != 0) {
   2129 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   2130 		outb(iobase + CLRSINT1, CLRSCSIPERR);
   2131 		if (sc->sc_prevphase == PH_MSGIN) {
   2132 			sc->sc_flags |= AIC_DROP_MSGIN;
   2133 			aic_sched_msgout(sc, SEND_PARITY_ERROR);
   2134 		} else
   2135 			aic_sched_msgout(sc, SEND_INIT_DET_ERR);
   2136 	}
   2137 
   2138 	/*
   2139 	 * If we're not already busy doing something test for the following
   2140 	 * conditions:
   2141 	 * 1) We have been reselected by something
   2142 	 * 2) We have selected something successfully
   2143 	 * 3) Our selection process has timed out
   2144 	 * 4) This is really a bus free interrupt just to get a new command
   2145 	 *    going?
   2146 	 * 5) Spurious interrupt?
   2147 	 */
   2148 	switch (sc->sc_state) {
   2149 	case AIC_IDLE:
   2150 	case AIC_SELECTING:
   2151 		sstat0 = inb(iobase + SSTAT0);
   2152 		AIC_MISC(("sstat0:0x%02x ", sstat0));
   2153 
   2154 		if ((sstat0 & TARGET) != 0) {
   2155 			/*
   2156 			 * We don't currently support target mode.
   2157 			 */
   2158 			printf("%s: target mode selected; going to BUS FREE\n",
   2159 			    sc->sc_dev.dv_xname);
   2160 			outb(iobase + SCSISIG, 0);
   2161 
   2162 			goto sched;
   2163 		} else if ((sstat0 & SELDI) != 0) {
   2164 			AIC_MISC(("reselected  "));
   2165 
   2166 			/*
   2167 			 * If we're trying to select a target ourselves,
   2168 			 * push our command back into the ready list.
   2169 			 */
   2170 			if (sc->sc_state == AIC_SELECTING) {
   2171 				AIC_MISC(("backoff selector  "));
   2172 				AIC_ASSERT(sc->sc_nexus != NULL);
   2173 				acb = sc->sc_nexus;
   2174 				sc->sc_nexus = NULL;
   2175 				TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2176 			}
   2177 
   2178 			/* Save reselection ID. */
   2179 			sc->sc_selid = inb(iobase + SELID);
   2180 
   2181 			sc->sc_state = AIC_RESELECTED;
   2182 		} else if ((sstat0 & SELDO) != 0) {
   2183 			AIC_MISC(("selected  "));
   2184 
   2185 			/* We have selected a target. Things to do:
   2186 			 * a) Determine what message(s) to send.
   2187 			 * b) Verify that we're still selecting the target.
   2188 			 * c) Mark device as busy.
   2189 			 */
   2190 			if (sc->sc_state != AIC_SELECTING) {
   2191 				printf("%s: selection out while idle; resetting\n",
   2192 				    sc->sc_dev.dv_xname);
   2193 				AIC_BREAK();
   2194 				goto reset;
   2195 			}
   2196 			AIC_ASSERT(sc->sc_nexus != NULL);
   2197 			acb = sc->sc_nexus;
   2198 			sc_link = acb->xs->sc_link;
   2199 			ti = &sc->sc_tinfo[sc_link->target];
   2200 
   2201 			sc->sc_msgpriq = SEND_IDENTIFY;
   2202 			if (acb->flags & ACB_RESET)
   2203 				sc->sc_msgpriq |= SEND_DEV_RESET;
   2204 			else if (acb->flags & ACB_ABORT)
   2205 				sc->sc_msgpriq |= SEND_ABORT;
   2206 			else {
   2207 #if AIC_USE_SYNCHRONOUS
   2208 				if ((ti->flags & DO_SYNC) != 0)
   2209 					sc->sc_msgpriq |= SEND_SDTR;
   2210 #endif
   2211 #if AIC_USE_WIDE
   2212 				if ((ti->flags & DO_WIDE) != 0)
   2213 					sc->sc_msgpriq |= SEND_WDTR;
   2214 #endif
   2215 			}
   2216 
   2217 			acb->flags |= ACB_NEXUS;
   2218 			ti->lubusy |= (1 << sc_link->lun);
   2219 
   2220 			/* Do an implicit RESTORE POINTERS. */
   2221 			sc->sc_dp = acb->data_addr;
   2222 			sc->sc_dleft = acb->data_length;
   2223 			sc->sc_cp = (u_char *)&acb->scsi_cmd;
   2224 			sc->sc_cleft = acb->scsi_cmd_length;
   2225 
   2226 			/* On our first connection, schedule a timeout. */
   2227 			if ((acb->xs->flags & SCSI_POLL) == 0)
   2228 				timeout(aic_timeout, acb, (acb->timeout * hz) / 1000);
   2229 
   2230 			sc->sc_state = AIC_CONNECTED;
   2231 		} else if ((sstat1 & SELTO) != 0) {
   2232 			AIC_MISC(("selection timeout  "));
   2233 
   2234 			if (sc->sc_state != AIC_SELECTING) {
   2235 				printf("%s: selection timeout while idle; resetting\n",
   2236 				    sc->sc_dev.dv_xname);
   2237 				AIC_BREAK();
   2238 				goto reset;
   2239 			}
   2240 			AIC_ASSERT(sc->sc_nexus != NULL);
   2241 			acb = sc->sc_nexus;
   2242 
   2243 			outb(iobase + SXFRCTL1, 0);
   2244 			outb(iobase + SCSISEQ, ENRESELI);
   2245 			outb(iobase + CLRSINT1, CLRSELTIMO);
   2246 			delay(250);
   2247 
   2248 			acb->xs->error = XS_SELTIMEOUT;
   2249 			goto finish;
   2250 		} else {
   2251 			if (sc->sc_state != AIC_IDLE) {
   2252 				printf("%s: BUS FREE while not idle; state=%d\n",
   2253 				    sc->sc_dev.dv_xname, sc->sc_state);
   2254 				AIC_BREAK();
   2255 				goto out;
   2256 			}
   2257 
   2258 			goto sched;
   2259 		}
   2260 
   2261 		/*
   2262 		 * Turn off selection stuff, and prepare to catch bus free
   2263 		 * interrupts, parity errors, and phase changes.
   2264 		 */
   2265 		outb(iobase + SXFRCTL1, 0);
   2266 		outb(iobase + SCSISEQ, ENAUTOATNP);
   2267 		outb(iobase + CLRSINT0, CLRSELDI | CLRSELDO);
   2268 		outb(iobase + CLRSINT1, CLRBUSFREE | CLRPHASECHG);
   2269 		outb(iobase + SIMODE0, 0);
   2270 		outb(iobase + SIMODE1,
   2271 		    ENSCSIRST | ENSCSIPERR | ENBUSFREE | ENREQINIT | ENPHASECHG);
   2272 
   2273 		sc->sc_flags = 0;
   2274 		sc->sc_prevphase = PH_INVALID;
   2275 		goto dophase;
   2276 	}
   2277 
   2278 	if ((sstat1 & BUSFREE) != 0) {
   2279 		/* We've gone to BUS FREE phase. */
   2280 		outb(iobase + CLRSINT1, CLRBUSFREE | CLRPHASECHG);
   2281 
   2282 		switch (sc->sc_state) {
   2283 		case AIC_RESELECTED:
   2284 			goto sched;
   2285 
   2286 		case AIC_CONNECTED:
   2287 			AIC_ASSERT(sc->sc_nexus != NULL);
   2288 			acb = sc->sc_nexus;
   2289 
   2290 #if AIC_USE_SYNCHRONOUS + AIC_USE_WIDE
   2291 			if (sc->sc_prevphase == PH_MSGOUT) {
   2292 				/*
   2293 				 * If the target went to BUS FREE phase during
   2294 				 * or immediately after sending a SDTR or WDTR
   2295 				 * message, disable negotiation.
   2296 				 */
   2297 				sc_link = acb->xs->sc_link;
   2298 				ti = &sc->sc_tinfo[sc_link->target];
   2299 				switch (sc->sc_lastmsg) {
   2300 #if AIC_USE_SYNCHRONOUS
   2301 				case SEND_SDTR:
   2302 					ti->flags &= ~DO_SYNC;
   2303 					ti->period = ti->offset = 0;
   2304 					break;
   2305 #endif
   2306 #if AIC_USE_WIDE
   2307 				case SEND_WDTR:
   2308 					ti->flags &= ~DO_WIDE;
   2309 					ti->width = 0;
   2310 					break;
   2311 #endif
   2312 				}
   2313 			}
   2314 #endif
   2315 
   2316 			if ((sc->sc_flags & AIC_ABORTING) == 0) {
   2317 				/*
   2318 				 * Section 5.1.1 of the SCSI 2 spec suggests
   2319 				 * issuing a REQUEST SENSE following an
   2320 				 * unexpected disconnect.  Some devices go into
   2321 				 * a contingent allegiance condition when
   2322 				 * disconnecting, and this is necessary to
   2323 				 * clean up their state.
   2324 				 */
   2325 				printf("%s: unexpected disconnect; sending REQUEST SENSE\n",
   2326 				    sc->sc_dev.dv_xname);
   2327 				AIC_BREAK();
   2328 				aic_sense(sc, acb);
   2329 				goto out;
   2330 			}
   2331 
   2332 			acb->xs->error = XS_DRIVER_STUFFUP;
   2333 			goto finish;
   2334 
   2335 		case AIC_DISCONNECT:
   2336 			AIC_ASSERT(sc->sc_nexus != NULL);
   2337 			acb = sc->sc_nexus;
   2338 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   2339 			sc->sc_nexus = NULL;
   2340 			goto sched;
   2341 
   2342 		case AIC_CMDCOMPLETE:
   2343 			AIC_ASSERT(sc->sc_nexus != NULL);
   2344 			acb = sc->sc_nexus;
   2345 			goto finish;
   2346 		}
   2347 	}
   2348 
   2349 	outb(iobase + CLRSINT1, CLRPHASECHG);
   2350 
   2351 dophase:
   2352 	if ((sstat1 & REQINIT) == 0) {
   2353 		/* Wait for REQINIT. */
   2354 		goto out;
   2355 	}
   2356 
   2357 	sc->sc_phase = inb(iobase + SCSISIG) & PH_MASK;
   2358 	outb(iobase + SCSISIG, sc->sc_phase);
   2359 
   2360 	switch (sc->sc_phase) {
   2361 	case PH_MSGOUT:
   2362 		if (sc->sc_state != AIC_CONNECTED &&
   2363 		    sc->sc_state != AIC_RESELECTED)
   2364 			break;
   2365 		aic_msgout(sc);
   2366 		sc->sc_prevphase = PH_MSGOUT;
   2367 		goto loop;
   2368 
   2369 	case PH_MSGIN:
   2370 		if (sc->sc_state != AIC_CONNECTED &&
   2371 		    sc->sc_state != AIC_RESELECTED)
   2372 			break;
   2373 		aic_msgin(sc);
   2374 		sc->sc_prevphase = PH_MSGIN;
   2375 		goto loop;
   2376 
   2377 	case PH_CMD:
   2378 		if (sc->sc_state != AIC_CONNECTED)
   2379 			break;
   2380 #if AIC_DEBUG
   2381 		if ((aic_debug & AIC_SHOWMISC) != 0) {
   2382 			AIC_ASSERT(sc->sc_nexus != NULL);
   2383 			acb = sc->sc_nexus;
   2384 			printf("cmd=0x%02x+%d  ",
   2385 			    acb->scsi_cmd.opcode, acb->scsi_cmd_length-1);
   2386 		}
   2387 #endif
   2388 		n = aic_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
   2389 		sc->sc_cp += n;
   2390 		sc->sc_cleft -= n;
   2391 		sc->sc_prevphase = PH_CMD;
   2392 		goto loop;
   2393 
   2394 	case PH_DATAOUT:
   2395 		if (sc->sc_state != AIC_CONNECTED)
   2396 			break;
   2397 		AIC_MISC(("dataout dleft=%d  ", sc->sc_dleft));
   2398 		n = aic_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
   2399 		sc->sc_dp += n;
   2400 		sc->sc_dleft -= n;
   2401 		sc->sc_prevphase = PH_DATAOUT;
   2402 		goto loop;
   2403 
   2404 	case PH_DATAIN:
   2405 		if (sc->sc_state != AIC_CONNECTED)
   2406 			break;
   2407 		AIC_MISC(("datain  "));
   2408 		n = aic_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
   2409 		sc->sc_dp += n;
   2410 		sc->sc_dleft -= n;
   2411 		sc->sc_prevphase = PH_DATAIN;
   2412 		goto loop;
   2413 
   2414 	case PH_STAT:
   2415 		if (sc->sc_state != AIC_CONNECTED)
   2416 			break;
   2417 		AIC_ASSERT(sc->sc_nexus != NULL);
   2418 		acb = sc->sc_nexus;
   2419 		/* XXXX Don't clear FIFO.  Wait for byte to come in. */
   2420 		outb(iobase + SXFRCTL0, CHEN | SPIOEN);
   2421 		outb(iobase + DMACNTRL0, RSTFIFO);
   2422 		acb->target_stat = inb(iobase + SCSIDAT);
   2423 		outb(iobase + SXFRCTL0, CHEN);
   2424 		outb(iobase + DMACNTRL0, RSTFIFO);
   2425 		while ((inb(iobase + SXFRCTL0) & SCSIEN) != 0)
   2426 			;
   2427 		AIC_MISC(("target_stat=0x%02x  ", acb->target_stat));
   2428 		sc->sc_prevphase = PH_STAT;
   2429 		goto loop;
   2430 	}
   2431 
   2432 	printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
   2433 	AIC_BREAK();
   2434 reset:
   2435 	aic_init(sc);
   2436 	return 1;
   2437 
   2438 finish:
   2439 	untimeout(aic_timeout, acb);
   2440 	aic_done(sc, acb);
   2441 	goto out;
   2442 
   2443 sched:
   2444 	sc->sc_state = AIC_IDLE;
   2445 	aic_sched(sc);
   2446 	goto out;
   2447 
   2448 out:
   2449 	outb(iobase + DMACNTRL0, INTEN);
   2450 	return 1;
   2451 }
   2452 
   2453 void
   2454 aic_abort(sc, acb)
   2455 	struct aic_softc *sc;
   2456 	struct aic_acb *acb;
   2457 {
   2458 
   2459 	/* 2 secs for the abort */
   2460 	acb->timeout = AIC_ABORT_TIMEOUT;
   2461 	acb->flags |= ACB_ABORT;
   2462 
   2463 	if (acb == sc->sc_nexus) {
   2464 		/*
   2465 		 * If we're still selecting, the message will be scheduled
   2466 		 * after selection is complete.
   2467 		 */
   2468 		if (sc->sc_state == AIC_CONNECTED)
   2469 			aic_sched_msgout(sc, SEND_ABORT);
   2470 	} else {
   2471 		aic_dequeue(sc, acb);
   2472 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2473 		if (sc->sc_state == AIC_IDLE)
   2474 			aic_sched(sc);
   2475 	}
   2476 }
   2477 
   2478 void
   2479 aic_timeout(arg)
   2480 	void *arg;
   2481 {
   2482 	struct aic_acb *acb = arg;
   2483 	struct scsi_xfer *xs = acb->xs;
   2484 	struct scsi_link *sc_link = xs->sc_link;
   2485 	struct aic_softc *sc = sc_link->adapter_softc;
   2486 	int s;
   2487 
   2488 	sc_print_addr(sc_link);
   2489 	printf("timed out");
   2490 
   2491 	s = splbio();
   2492 
   2493 	if (acb->flags & ACB_ABORT) {
   2494 		/* abort timed out */
   2495 		printf(" AGAIN\n");
   2496 		/* XXX Must reset! */
   2497 	} else {
   2498 		/* abort the operation that has timed out */
   2499 		printf("\n");
   2500 		acb->xs->error = XS_TIMEOUT;
   2501 		aic_abort(sc, acb);
   2502 	}
   2503 
   2504 	splx(s);
   2505 }
   2506 
   2507 #ifdef AIC_DEBUG
   2509 /*
   2510  * The following functions are mostly used for debugging purposes, either
   2511  * directly called from the driver or from the kernel debugger.
   2512  */
   2513 
   2514 void
   2515 aic_show_scsi_cmd(acb)
   2516 	struct aic_acb *acb;
   2517 {
   2518 	u_char  *b = (u_char *)&acb->scsi_cmd;
   2519 	struct scsi_link *sc_link = acb->xs->sc_link;
   2520 	int i;
   2521 
   2522 	sc_print_addr(sc_link);
   2523 	if ((acb->xs->flags & SCSI_RESET) == 0) {
   2524 		for (i = 0; i < acb->scsi_cmd_length; i++) {
   2525 			if (i)
   2526 				printf(",");
   2527 			printf("%x", b[i]);
   2528 		}
   2529 		printf("\n");
   2530 	} else
   2531 		printf("RESET\n");
   2532 }
   2533 
   2534 void
   2535 aic_print_acb(acb)
   2536 	struct aic_acb *acb;
   2537 {
   2538 
   2539 	printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
   2540 	printf(" dp=%x dleft=%d target_stat=%x\n",
   2541 	    (long)acb->data_addr, acb->data_length, acb->target_stat);
   2542 	aic_show_scsi_cmd(acb);
   2543 }
   2544 
   2545 void
   2546 aic_print_active_acb()
   2547 {
   2548 	struct aic_acb *acb;
   2549 	struct aic_softc *sc = aic_cd.cd_devs[0];
   2550 
   2551 	printf("ready list:\n");
   2552 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   2553 	    acb = acb->chain.tqe_next)
   2554 		aic_print_acb(acb);
   2555 	printf("nexus:\n");
   2556 	if (sc->sc_nexus != NULL)
   2557 		aic_print_acb(sc->sc_nexus);
   2558 	printf("nexus list:\n");
   2559 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   2560 	    acb = acb->chain.tqe_next)
   2561 		aic_print_acb(acb);
   2562 }
   2563 
   2564 void
   2565 aic_dump6360(sc)
   2566 	struct aic_softc *sc;
   2567 {
   2568 	int iobase = sc->sc_iobase;
   2569 
   2570 	printf("aic6360: SCSISEQ=%x SXFRCTL0=%x SXFRCTL1=%x SCSISIG=%x\n",
   2571 	    inb(iobase + SCSISEQ), inb(iobase + SXFRCTL0),
   2572 	    inb(iobase + SXFRCTL1), inb(iobase + SCSISIG));
   2573 	printf("         SSTAT0=%x SSTAT1=%x SSTAT2=%x SSTAT3=%x SSTAT4=%x\n",
   2574 	    inb(iobase + SSTAT0), inb(iobase + SSTAT1), inb(iobase + SSTAT2),
   2575 	    inb(iobase + SSTAT3), inb(iobase + SSTAT4));
   2576 	printf("         SIMODE0=%x SIMODE1=%x DMACNTRL0=%x DMACNTRL1=%x DMASTAT=%x\n",
   2577 	    inb(iobase + SIMODE0), inb(iobase + SIMODE1),
   2578 	    inb(iobase + DMACNTRL0), inb(iobase + DMACNTRL1),
   2579 	    inb(iobase + DMASTAT));
   2580 	printf("         FIFOSTAT=%d SCSIBUS=0x%x\n",
   2581 	    inb(iobase + FIFOSTAT), inb(iobase + SCSIBUS));
   2582 }
   2583 
   2584 void
   2585 aic_dump_driver(sc)
   2586 	struct aic_softc *sc;
   2587 {
   2588 	struct aic_tinfo *ti;
   2589 	int i;
   2590 
   2591 	printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2592 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
   2593 	    sc->sc_state, sc->sc_imess[0],
   2594 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2595 	for (i = 0; i < 7; i++) {
   2596 		ti = &sc->sc_tinfo[i];
   2597 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2598 		    i, ti->cmds, ti->dconns, ti->touts);
   2599 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2600 	}
   2601 }
   2602 #endif
   2603