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