Home | History | Annotate | Line # | Download | only in dev
mha.c revision 1.17.2.2
      1 /*	$NetBSD: mha.c,v 1.17.2.2 2001/03/27 13:16:26 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996-1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum, Masaru Oki, Takumi Nakamura, Masanobu Saitoh and
      9  * Minoura Makoto.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38 */
     39 
     40 /*-
     41  * Copyright (c) 1994 Jarle Greipsland
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. The name of the author may not be used to endorse or promote products
     53  *    derived from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     56  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     57  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     58  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     59  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     60  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     61  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     63  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     64  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #include "opt_ddb.h"
     69 
     70 /* Synchronous data transfers? */
     71 #define SPC_USE_SYNCHRONOUS	0
     72 #define SPC_SYNC_REQ_ACK_OFS 	8
     73 
     74 /* Default DMA mode? */
     75 #define MHA_DMA_LIMIT_XFER	1
     76 #define MHA_DMA_BURST_XFER	1
     77 #define MHA_DMA_SHORT_BUS_CYCLE	1
     78 
     79 #define MHA_DMA_DATAIN	(0 | (MHA_DMA_LIMIT_XFER << 1)		\
     80 			   | (MHA_DMA_BURST_XFER << 2)		\
     81 			   | (MHA_DMA_SHORT_BUS_CYCLE << 3))
     82 #define MHA_DMA_DATAOUT	(1 | (MHA_DMA_LIMIT_XFER << 1)		\
     83 			   | (MHA_DMA_BURST_XFER << 2)		\
     84 			   | (MHA_DMA_SHORT_BUS_CYCLE << 3))
     85 
     86 /* Include debug functions?  At the end of this file there are a bunch of
     87  * functions that will print out various information regarding queued SCSI
     88  * commands, driver state and chip contents.  You can call them from the
     89  * kernel debugger.  If you set SPC_DEBUG to 0 they are not included (the
     90  * kernel uses less memory) but you lose the debugging facilities.
     91  */
     92 #define SPC_DEBUG		0
     93 
     94 /* End of customizable parameters */
     95 
     96 /*
     97  * MB86601A SCSI Protocol Controller (SPC) routines for MANKAI Mach-2
     98  */
     99 
    100 #include <sys/types.h>
    101 #include <sys/param.h>
    102 #include <sys/systm.h>
    103 #include <sys/kernel.h>
    104 #include <sys/errno.h>
    105 #include <sys/ioctl.h>
    106 #include <sys/device.h>
    107 #include <sys/buf.h>
    108 #include <sys/proc.h>
    109 #include <sys/user.h>
    110 #include <sys/queue.h>
    111 
    112 #include <machine/bus.h>
    113 
    114 #include <dev/scsipi/scsi_all.h>
    115 #include <dev/scsipi/scsipi_all.h>
    116 #include <dev/scsipi/scsi_message.h>
    117 #include <dev/scsipi/scsiconf.h>
    118 
    119 #include <x68k/x68k/iodevice.h>
    120 #include <x68k/dev/mb86601reg.h>
    121 #include <x68k/dev/mhavar.h>
    122 #include <x68k/dev/intiovar.h>
    123 #include <x68k/dev/scsiromvar.h>
    124 
    125 #if 0
    126 #define WAIT {if (sc->sc_pc[2]) {printf("[W_%d", __LINE__); while (sc->sc_pc[2] & 0x40);printf("]");}}
    127 #else
    128 #define WAIT {while (sc->sc_pc[2] & 0x40);}
    129 #endif
    130 
    131 #define SSR	(sc->sc_pc[2])
    132 #define	SS_IREQUEST	0x80
    133 #define	SS_BUSY		0x40
    134 #define	SS_DREG_FULL	0x02
    135 
    136 #define	NSR	(sc->sc_pc[3])
    137 
    138 #define	SIR	(sc->sc_pc[4])
    139 
    140 #define	CMR	(sc->sc_pc[5])
    141 #define	CMD_SEL_AND_CMD	0x00
    142 #define	CMD_SELECT	0x09
    143 #define	CMD_SET_ATN	0x0a
    144 #define	CMD_RESET_ATN	0x0b
    145 #define	CMD_RESET_ACK	0x0d
    146 #define	CMD_SEND_FROM_MPU	0x10
    147 #define	CMD_SEND_FROM_DMA	0x11
    148 #define	CMD_RECEIVE_TO_MPU	0x12
    149 #define	CMD_RECEIVE_TO_DMA	0x13
    150 #define	CMD_RECEIVE_MSG	0x1a
    151 #define	CMD_RECEIVE_STS	0x1c
    152 #define	CMD_SOFT_RESET	0x40
    153 #define	CMD_SCSI_RESET	0x42
    154 #define	CMD_SET_UP_REG	0x43
    155 
    156 #define	SCR	(sc->sc_pc[11])
    157 
    158 #define	TMR	(sc->sc_pc[12])
    159 #define	TM_SYNC		0x80
    160 #define	TM_ASYNC	0x00
    161 
    162 #define	WAR	(sc->sc_pc[15])
    163 #define	WA_MCSBUFWIN	0x00
    164 #define	WA_UPMWIN	0x80
    165 #define	WA_INITWIN	0xc0
    166 
    167 #define	MBR	(sc->sc_pc[15])
    168 
    169 #define ISCSR	(sc->sc_ps[2])
    170 
    171 #define	CCR	(sc->sc_pcx[0])
    172 #define	OIR	(sc->sc_pcx[1])
    173 #define	AMR	(sc->sc_pcx[2])
    174 #define	SMR	(sc->sc_pcx[3])
    175 #define	SRR	(sc->sc_pcx[4])
    176 #define	STR	(sc->sc_pcx[5])
    177 #define	RTR	(sc->sc_pcx[6])
    178 #define	ATR	(sc->sc_pcx[7])
    179 #define	PER	(sc->sc_pcx[8])
    180 #define	IER	(sc->sc_pcx[9])
    181 #define	IE_ALL	0xBF
    182 
    183 #define	GLR	(sc->sc_pcx[10])
    184 #define	DMR	(sc->sc_pcx[11])
    185 #define	IMR	(sc->sc_pcx[12])
    186 
    187 
    188 #ifndef DDB
    190 #define	Debugger() panic("should call debugger here (mha.c)")
    191 #endif /* ! DDB */
    192 
    193 
    194 #if SPC_DEBUG
    195 #define SPC_SHOWACBS	0x01
    196 #define SPC_SHOWINTS	0x02
    197 #define SPC_SHOWCMDS	0x04
    198 #define SPC_SHOWMISC	0x08
    199 #define SPC_SHOWTRAC	0x10
    200 #define SPC_SHOWSTART	0x20
    201 #define SPC_SHOWPHASE	0x40
    202 #define SPC_SHOWDMA	0x80
    203 #define SPC_SHOWCCMDS	0x100
    204 #define SPC_SHOWMSGS	0x200
    205 #define SPC_DOBREAK	0x400
    206 
    207 int mha_debug =
    208 #if 0
    209 0x7FF;
    210 #else
    211 SPC_SHOWSTART|SPC_SHOWTRAC;
    212 #endif
    213 
    214 
    215 #define SPC_ACBS(str)  do {if (mha_debug & SPC_SHOWACBS) printf str;} while (0)
    216 #define SPC_MISC(str)  do {if (mha_debug & SPC_SHOWMISC) printf str;} while (0)
    217 #define SPC_INTS(str)  do {if (mha_debug & SPC_SHOWINTS) printf str;} while (0)
    218 #define SPC_TRACE(str) do {if (mha_debug & SPC_SHOWTRAC) printf str;} while (0)
    219 #define SPC_CMDS(str)  do {if (mha_debug & SPC_SHOWCMDS) printf str;} while (0)
    220 #define SPC_START(str) do {if (mha_debug & SPC_SHOWSTART) printf str;}while (0)
    221 #define SPC_PHASE(str) do {if (mha_debug & SPC_SHOWPHASE) printf str;}while (0)
    222 #define SPC_DMA(str)   do {if (mha_debug & SPC_SHOWDMA) printf str;}while (0)
    223 #define SPC_MSGS(str)  do {if (mha_debug & SPC_SHOWMSGS) printf str;}while (0)
    224 #define	SPC_BREAK()    do {if ((mha_debug & SPC_DOBREAK) != 0) Debugger();} while (0)
    225 #define	SPC_ASSERT(x)  do {if (x) {} else {printf("%s at line %d: assertion failed\n", sc->sc_dev.dv_xname, __LINE__); Debugger();}} while (0)
    226 #else
    227 #define SPC_ACBS(str)
    228 #define SPC_MISC(str)
    229 #define SPC_INTS(str)
    230 #define SPC_TRACE(str)
    231 #define SPC_CMDS(str)
    232 #define SPC_START(str)
    233 #define SPC_PHASE(str)
    234 #define SPC_DMA(str)
    235 #define SPC_MSGS(str)
    236 #define	SPC_BREAK()
    237 #define	SPC_ASSERT(x)
    238 #endif
    239 
    240 int	mhamatch	__P((struct device *, struct cfdata *, void *));
    241 void	mhaattach	__P((struct device *, struct device *, void *));
    242 void	mhaselect	__P((struct mha_softc *,
    243 				     u_char, u_char, u_char *, u_char));
    244 void	mha_scsi_reset	__P((struct mha_softc *));
    245 void	mha_reset	__P((struct mha_softc *));
    246 void	mha_free_acb	__P((struct mha_softc *, struct acb *, int));
    247 void	mha_sense	__P((struct mha_softc *, struct acb *));
    248 void	mha_msgin	__P((struct mha_softc *));
    249 void	mha_msgout	__P((struct mha_softc *));
    250 int	mha_dataout_pio	__P((struct mha_softc *, u_char *, int));
    251 int	mha_datain_pio	__P((struct mha_softc *, u_char *, int));
    252 int	mha_dataout	__P((struct mha_softc *, u_char *, int));
    253 int	mha_datain	__P((struct mha_softc *, u_char *, int));
    254 void	mha_abort	__P((struct mha_softc *, struct acb *));
    255 void 	mha_init	__P((struct mha_softc *));
    256 void	mha_scsi_request __P((struct scsipi_channel *,
    257 				scsipi_adapter_req_t, void *));
    258 void	mha_poll	__P((struct mha_softc *, struct acb *));
    259 void	mha_sched	__P((struct mha_softc *));
    260 void	mha_done	__P((struct mha_softc *, struct acb *));
    261 int	mhaintr		__P((void*));
    262 void	mha_timeout	__P((void *));
    263 void	mha_minphys	__P((struct buf *));
    264 void	mha_dequeue	__P((struct mha_softc *, struct acb *));
    265 inline void	mha_setsync	__P((struct mha_softc *, struct spc_tinfo *));
    266 #if SPC_DEBUG
    267 void	mha_print_acb __P((struct acb *));
    268 void	mha_show_scsi_cmd __P((struct acb *));
    269 void	mha_print_active_acb __P((void));
    270 void	mha_dump_driver __P((struct mha_softc *));
    271 #endif
    272 
    273 static int mha_dataio_dma __P((int, int, struct mha_softc *, u_char *, int));
    274 
    275 struct cfattach mha_ca = {
    276 	sizeof(struct mha_softc), mhamatch, mhaattach
    277 };
    278 
    279 extern struct cfdriver mha_cd;
    280 
    281 
    282 /*
    284  * returns non-zero value if a controller is found.
    285  */
    286 int
    287 mhamatch(parent, cf, aux)
    288 	struct device *parent;
    289 	struct cfdata *cf;
    290 	void *aux;
    291 {
    292 	struct intio_attach_args *ia = aux;
    293 	bus_space_tag_t iot = ia->ia_bst;
    294 	bus_space_handle_t ioh;
    295 
    296 	ia->ia_size=0x20;
    297 	if (ia->ia_addr != 0xea0000)
    298 		return 0;
    299 
    300 	if (intio_map_allocate_region(parent->dv_parent, ia,
    301 				      INTIO_MAP_TESTONLY) < 0) /* FAKE */
    302 		return 0;
    303 
    304 	if (bus_space_map(iot, ia->ia_addr, 0x20, BUS_SPACE_MAP_SHIFTED,
    305 			  &ioh) < 0)
    306 		return 0;
    307 	if (!badaddr ((caddr_t)INTIO_ADDR(ia->ia_addr + 0)))
    308 		return 0;
    309 	bus_space_unmap(iot, ioh, 0x20);
    310 
    311 	return 1;
    312 }
    313 
    314 /*
    315  */
    316 
    317 struct mha_softc *tmpsc;
    318 
    319 void
    320 mhaattach(parent, self, aux)
    321 	struct device *parent, *self;
    322 	void *aux;
    323 {
    324 	struct mha_softc *sc = (void *)self;
    325 	struct intio_attach_args *ia = aux;
    326 
    327 	tmpsc = sc;	/* XXX */
    328 
    329 	printf (": Mankai Mach-2 Fast SCSI Host Adaptor\n");
    330 
    331 	SPC_TRACE(("mhaattach  "));
    332 	sc->sc_state = SPC_INIT;
    333 	sc->sc_iobase = INTIO_ADDR(ia->ia_addr + 0x80); /* XXX */
    334 	intio_map_allocate_region (parent->dv_parent, ia, INTIO_MAP_ALLOCATE);
    335 				/* XXX: FAKE  */
    336 	sc->sc_dmat = ia->ia_dmat;
    337 
    338 	sc->sc_pc = (volatile u_char *)sc->sc_iobase;
    339 	sc->sc_ps = (volatile u_short *)sc->sc_iobase;
    340 	sc->sc_pcx = &sc->sc_pc[0x10];
    341 
    342 	sc->sc_id = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
    343 
    344 	intio_intr_establish (ia->ia_intr, "mha", mhaintr, sc);
    345 
    346 	mha_init(sc);	/* Init chip and driver */
    347 
    348 	mha_scsi_reset(sc);	/* XXX: some devices need this. */
    349 
    350 	sc->sc_phase  = BUSFREE_PHASE;
    351 
    352 	/*
    353 	 * Fill in the adapter.
    354 	 */
    355 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    356 	sc->sc_adapter.adapt_nchannels = 1;
    357 	sc->sc_adapter.adapt_openings = 7;
    358 	sc->sc_adapter.adapt_max_periph = 1;
    359 	sc->sc_adapter.adapt_ioctl = NULL;
    360 	sc->sc_adapter.adapt_minphys = mha_minphys;
    361 	sc->sc_adapter.adapt_request = mha_scsi_request;
    362 
    363 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    364 	sc->sc_channel.chan_bustype = &scsi_bustype;
    365 	sc->sc_channel.chan_channel = 0;
    366 	sc->sc_channel.chan_ntargets = 8;
    367 	sc->sc_channel.chan_nluns = 8;
    368 	sc->sc_channel.chan_id = sc->sc_id;
    369 
    370 	sc->sc_spcinitialized = 0;
    371 	WAR = WA_INITWIN;
    372 #if 1
    373 	CCR = 0x14;
    374 	OIR = sc->sc_id;
    375 	AMR = 0x00;
    376 	SMR = 0x00;
    377 	SRR = 0x00;
    378 	STR = 0x20;
    379 	RTR = 0x40;
    380 	ATR = 0x01;
    381 	PER = 0xc9;
    382 #endif
    383 	IER = IE_ALL;	/* $B$9$Y$F$N3d$j9~$_$r5v2D(B */
    384 #if 1
    385 	GLR = 0x00;
    386 	DMR = 0x30;
    387 	IMR = 0x00;
    388 #endif
    389 	WAR = WA_MCSBUFWIN;
    390 
    391 	/* drop off */
    392 	while (SSR & SS_IREQUEST)
    393 	  {
    394 	    unsigned a = ISCSR;
    395 	  }
    396 
    397 	CMR = CMD_SET_UP_REG;	/* setup reg cmd. */
    398 
    399 	SPC_TRACE(("waiting for intr..."));
    400 	while (!(SSR & SS_IREQUEST))
    401 	  delay(10);
    402 	mhaintr	(sc);
    403 
    404 	tmpsc = NULL;
    405 
    406 	config_found(self, &sc->sc_channel, scsiprint);
    407 }
    408 
    409 #if 0
    410 void
    411 mha_reset(sc)
    412 	struct mha_softc *sc;
    413 {
    414 	u_short	dummy;
    415 printf("reset...");
    416 	CMR = CMD_SOFT_RESET;
    417 	asm volatile ("nop");	/* XXX wait (4clk in 20mhz) ??? */
    418 	dummy = sc->sc_ps[-1];
    419 	dummy = sc->sc_ps[-1];
    420 	dummy = sc->sc_ps[-1];
    421 	dummy = sc->sc_ps[-1];
    422 	asm volatile ("nop");
    423 	CMR = CMD_SOFT_RESET;
    424 	sc->sc_spcinitialized = 0;
    425 	CMR = CMD_SET_UP_REG;	/* setup reg cmd. */
    426 	while(!sc->sc_spcinitialized);
    427 
    428 	sc->sc_id = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
    429 printf("done.\n");
    430 }
    431 #endif
    432 
    433 /*
    434  * Pull the SCSI RST line for 500us.
    435  */
    436 void
    437 mha_scsi_reset(sc)	/* FINISH? */
    438 	struct mha_softc *sc;
    439 {
    440 
    441 	CMR = CMD_SCSI_RESET;	/* SCSI RESET */
    442 	while (!(SSR&SS_IREQUEST))
    443 	  delay(10);
    444 }
    445 
    446 /*
    447  * Initialize mha SCSI driver.
    448  */
    449 void
    450 mha_init(sc)
    451 	struct mha_softc *sc;
    452 {
    453 	struct acb *acb;
    454 	int r;
    455 
    456 	if (sc->sc_state == SPC_INIT) {
    457 		/* First time through; initialize. */
    458 		TAILQ_INIT(&sc->ready_list);
    459 		TAILQ_INIT(&sc->nexus_list);
    460 		TAILQ_INIT(&sc->free_list);
    461 		sc->sc_nexus = NULL;
    462 		acb = sc->sc_acb;
    463 		bzero(acb, sizeof(sc->sc_acb));
    464 		for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
    465 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    466 			acb++;
    467 		}
    468 		bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
    469 
    470 		r = bus_dmamem_alloc(sc->sc_dmat, MAXBSIZE, 0, 0,
    471 				     sc->sc_dmaseg, 1, &sc->sc_ndmasegs,
    472 				     BUS_DMA_NOWAIT);
    473 		if (r)
    474 			panic("mha_init: cannot allocate dma memory");
    475 		if (sc->sc_ndmasegs != 1)
    476 			panic("mha_init: number of segment > 1??");
    477 		r = bus_dmamem_map(sc->sc_dmat, sc->sc_dmaseg, sc->sc_ndmasegs,
    478 				   MAXBSIZE, &sc->sc_dmabuf, BUS_DMA_NOWAIT);
    479 		if (r)
    480 			panic("mha_init: cannot map dma memory");
    481 		r = bus_dmamap_create(sc->sc_dmat, MAXBSIZE, 1,
    482 				      MAXBSIZE, 0, BUS_DMA_NOWAIT,
    483 				      &sc->sc_dmamap);
    484 		if (r)
    485 			panic("mha_init: cannot create dmamap structure");
    486 		r = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    487 				    sc->sc_dmabuf, MAXBSIZE, NULL,
    488 				    BUS_DMA_NOWAIT);
    489 		if (r)
    490 			panic("mha_init: cannot load dma buffer into dmamap");
    491 		sc->sc_p = 0;
    492 	} else {
    493 		/* Cancel any active commands. */
    494 		sc->sc_flags |= SPC_ABORTING;
    495 		sc->sc_state = SPC_IDLE;
    496 		if ((acb = sc->sc_nexus) != NULL) {
    497 			acb->xs->error = XS_DRIVER_STUFFUP;
    498 			mha_done(sc, acb);
    499 		}
    500 		while ((acb = sc->nexus_list.tqh_first) != NULL) {
    501 			acb->xs->error = XS_DRIVER_STUFFUP;
    502 			mha_done(sc, acb);
    503 		}
    504 	}
    505 
    506 	sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
    507 	for (r = 0; r < 8; r++) {
    508 		struct spc_tinfo *ti = &sc->sc_tinfo[r];
    509 
    510 		ti->flags = 0;
    511 #if SPC_USE_SYNCHRONOUS
    512 		ti->flags |= T_SYNCMODE;
    513 		ti->period = sc->sc_minsync;
    514 		ti->offset = SPC_SYNC_REQ_ACK_OFS;
    515 #else
    516 		ti->period = ti->offset = 0;
    517 #endif
    518 		ti->width = 0;
    519 	}
    520 
    521 	sc->sc_state = SPC_IDLE;
    522 }
    523 
    524 void
    525 mha_free_acb(sc, acb, flags)
    526 	struct mha_softc *sc;
    527 	struct acb *acb;
    528 	int flags;
    529 {
    530 	int s;
    531 
    532 	s = splbio();
    533 
    534 	acb->flags = 0;
    535 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    536 
    537 	/*
    538 	 * If there were none, wake anybody waiting for one to come free,
    539 	 * starting with queued entries.
    540 	 */
    541 	if (acb->chain.tqe_next == 0)
    542 		wakeup(&sc->free_list);
    543 
    544 	splx(s);
    545 }
    546 
    547 
    548 /*
    550  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    551  */
    552 
    553 /*
    554  * Expected sequence:
    555  * 1) Command inserted into ready list
    556  * 2) Command selected for execution
    557  * 3) Command won arbitration and has selected target device
    558  * 4) Send message out (identify message, eventually also sync.negotiations)
    559  * 5) Send command
    560  * 5a) Receive disconnect message, disconnect.
    561  * 5b) Reselected by target
    562  * 5c) Receive identify message from target.
    563  * 6) Send or receive data
    564  * 7) Receive status
    565  * 8) Receive message (command complete etc.)
    566  * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
    567  *    Repeat 2-8 (no disconnects please...)
    568  */
    569 
    570 /*
    571  * Start a selection.  This is used by mha_sched() to select an idle target,
    572  * and by mha_done() to immediately reselect a target to get sense information.
    573  */
    574 void
    575 mhaselect(sc, target, lun, cmd, clen)
    576 	struct mha_softc *sc;
    577 	u_char target, lun;
    578 	u_char *cmd;
    579 	u_char clen;
    580 {
    581 	struct spc_tinfo *ti = &sc->sc_tinfo[target];
    582 	int i;
    583 	int s;
    584 
    585 	s = splbio();	/* XXX */
    586 
    587 	SPC_TRACE(("[mhaselect(t%d,l%d,cmd:%x)] ", target, lun, *(u_char *)cmd));
    588 
    589 	/* CDB $B$r(B SPC $B$N(B MCS REG $B$K%;%C%H$9$k(B */
    590 	/* Now the command into the FIFO */
    591 	WAIT;
    592 #if 1
    593 	SPC_MISC(("[cmd:"));
    594 	for (i = 0; i < clen; i++)
    595 	  {
    596 	    unsigned c = cmd[i];
    597 	    if (i == 1)
    598 	      c |= lun << 5;
    599 	    SPC_MISC((" %02x", c));
    600 	    sc->sc_pcx[i] = c;
    601 	  }
    602 	SPC_MISC(("], target=%d\n", target));
    603 #else
    604 	bcopy(cmd, sc->sc_pcx, clen);
    605 #endif
    606 	if (NSR & 0x80)
    607 		panic("scsistart: already selected...");
    608 	sc->sc_phase  = COMMAND_PHASE;
    609 
    610 	/* new state ASP_SELECTING */
    611 	sc->sc_state = SPC_SELECTING;
    612 
    613 	SIR = target;
    614 #if 0
    615 	CMR = CMD_SELECT;
    616 #else
    617 	CMR = CMD_SEL_AND_CMD;	/* select & cmd */
    618 #endif
    619 	splx(s);
    620 }
    621 
    622 #if 0
    623 int
    624 mha_reselect(sc, message)
    625 	struct mha_softc *sc;
    626 	u_char message;
    627 {
    628 	u_char selid, target, lun;
    629 	struct acb *acb;
    630 	struct scsipi_periph *periph;
    631 	struct spc_tinfo *ti;
    632 
    633 	/*
    634 	 * The SCSI chip made a snapshot of the data bus while the reselection
    635 	 * was being negotiated.  This enables us to determine which target did
    636 	 * the reselect.
    637 	 */
    638 	selid = sc->sc_selid & ~(1 << sc->sc_id);
    639 	if (selid & (selid - 1)) {
    640 		printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
    641 		    sc->sc_dev.dv_xname, selid);
    642 		SPC_BREAK();
    643 		goto reset;
    644 	}
    645 
    646 	/*
    647 	 * Search wait queue for disconnected cmd
    648 	 * The list should be short, so I haven't bothered with
    649 	 * any more sophisticated structures than a simple
    650 	 * singly linked list.
    651 	 */
    652 	target = ffs(selid) - 1;
    653 	lun = message & 0x07;
    654 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
    655 	     acb = acb->chain.tqe_next) {
    656 		periph = acb->xs->xs_periph;
    657 		if (periph->periph_target == target &&
    658 		    periph->periph_lun == lun)
    659 			break;
    660 	}
    661 	if (acb == NULL) {
    662 		printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
    663 		    sc->sc_dev.dv_xname, target, lun);
    664 		SPC_BREAK();
    665 		goto abort;
    666 	}
    667 
    668 	/* Make this nexus active again. */
    669 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    670 	sc->sc_state = SPC_HASNEXUS;
    671 	sc->sc_nexus = acb;
    672 	ti = &sc->sc_tinfo[target];
    673 	ti->lubusy |= (1 << lun);
    674 	mha_setsync(sc, ti);
    675 
    676 	if (acb->flags & ACB_RESET)
    677 		mha_sched_msgout(sc, SEND_DEV_RESET);
    678 	else if (acb->flags & ACB_ABORTED)
    679 		mha_sched_msgout(sc, SEND_ABORT);
    680 
    681 	/* Do an implicit RESTORE POINTERS. */
    682 	sc->sc_dp = acb->daddr;
    683 	sc->sc_dleft = acb->dleft;
    684 	sc->sc_cp = (u_char *)&acb->cmd;
    685 	sc->sc_cleft = acb->clen;
    686 
    687 	return (0);
    688 
    689 reset:
    690 	mha_sched_msgout(sc, SEND_DEV_RESET);
    691 	return (1);
    692 
    693 abort:
    694 	mha_sched_msgout(sc, SEND_ABORT);
    695 	return (1);
    696 }
    697 #endif
    698 /*
    699  * Start a SCSI-command
    700  * This function is called by the higher level SCSI-driver to queue/run
    701  * SCSI-commands.
    702  */
    703 void
    704 mha_scsi_request(chan, req, arg)
    705 	struct scsipi_channel *chan;
    706 	scsipi_adapter_req_t req;
    707 	void *arg;
    708 {
    709 	struct scsipi_xfer *xs;
    710 	struct scsipi_periph *periph;
    711 	struct mha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    712 	struct acb *acb;
    713 	int s, flags;
    714 
    715 	switch (req) {
    716 	case ADAPTER_REQ_RUN_XFER:
    717 		xs = arg;
    718 		periph = xs->xs_periph;
    719 
    720 		SPC_TRACE(("[mha_scsi_cmd] "));
    721 		SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    722 		    periph->periph_target));
    723 
    724 		flags = xs->xs_control;
    725 
    726 		/* Get a mha command block */
    727 		s = splbio();
    728 		acb = sc->free_list.tqh_first;
    729 		if (acb) {
    730 			TAILQ_REMOVE(&sc->free_list, acb, chain);
    731 			ACB_SETQ(acb, ACB_QNONE);
    732 		}
    733 
    734 		if (acb == NULL) {
    735 			xs->error = XS_RESOURCE_SHORTAGE;
    736 			scsipi_done(xs);
    737 			splx(s);
    738 			return;
    739 		}
    740 		splx(s);
    741 
    742 		/* Initialize acb */
    743 		acb->xs = xs;
    744 		bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
    745 		acb->clen = xs->cmdlen;
    746 		acb->daddr = xs->data;
    747 		acb->dleft = xs->datalen;
    748 		acb->stat = 0;
    749 
    750 		s = splbio();
    751 		ACB_SETQ(acb, ACB_QREADY);
    752 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    753 #if 1
    754 		callout_reset(&acb->xs->xs_callout,
    755 		    ((u_int64_t)xs->timeout * (u_int64_t)hz) / 1000,
    756 		    mha_timeout, acb);
    757 #endif
    758 
    759 		/*
    760 		 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
    761 		 */
    762 		if (sc->sc_state == SPC_IDLE)
    763 			mha_sched(sc);
    764 
    765 		splx(s);
    766 
    767 		if (flags & XS_CTL_POLL) {
    768 			/* Not allowed to use interrupts, use polling instead */
    769 			mha_poll(sc, acb);
    770 		}
    771 
    772 		SPC_MISC(("SUCCESSFULLY_QUEUED"));
    773 		return;
    774 
    775 	case ADAPTER_REQ_GROW_RESOURCES:
    776 		/* XXX Not supported. */
    777 		return;
    778 
    779 	case ADAPTER_REQ_SET_XFER_MODE:
    780 		/* XXX Not supported. */
    781 		return;
    782 }
    783 
    784 /*
    785  * Adjust transfer size in buffer structure
    786  */
    787 void
    788 mha_minphys(bp)
    789 	struct buf *bp;
    790 {
    791 
    792 	SPC_TRACE(("mha_minphys  "));
    793 	minphys(bp);
    794 }
    795 
    796 /*
    797  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    798  */
    799 void
    800 mha_poll(sc, acb)
    801 	struct mha_softc *sc;
    802 	struct acb *acb;
    803 {
    804 	struct scsipi_xfer *xs = acb->xs;
    805 	int count = xs->timeout * 100;
    806 	int s = splbio();
    807 
    808 	SPC_TRACE(("[mha_poll] "));
    809 
    810 	while (count) {
    811 		/*
    812 		 * If we had interrupts enabled, would we
    813 		 * have got an interrupt?
    814 		 */
    815 		if (SSR & SS_IREQUEST)
    816 			mhaintr(sc);
    817 		if ((xs->xs_status & XS_STS_DONE) != 0)
    818 			break;
    819 		DELAY(10);
    820 #if 1
    821 		if (sc->sc_state == SPC_IDLE) {
    822 			SPC_TRACE(("[mha_poll: rescheduling] "));
    823 			mha_sched(sc);
    824 		}
    825 #endif
    826 		count--;
    827 	}
    828 
    829 	if (count == 0) {
    830 		SPC_MISC(("mha_poll: timeout"));
    831 		mha_timeout((caddr_t)acb);
    832 	}
    833 	splx(s);
    834 	scsipi_done(xs);
    835 }
    836 
    837 /*
    839  * LOW LEVEL SCSI UTILITIES
    840  */
    841 
    842 /*
    843  * Set synchronous transfer offset and period.
    844  */
    845 inline void
    846 mha_setsync(sc, ti)
    847 	struct mha_softc *sc;
    848 	struct spc_tinfo *ti;
    849 {
    850 }
    851 
    852 
    853 /*
    855  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
    856  * handler so that we may call it from mha_scsi_cmd and mha_done.  This may
    857  * save us an unecessary interrupt just to get things going.  Should only be
    858  * called when state == SPC_IDLE and at bio pl.
    859  */
    860 void
    861 mha_sched(sc)
    862 	register struct mha_softc *sc;
    863 {
    864 	struct scsipi_periph *periph;
    865 	struct acb *acb;
    866 	int t;
    867 
    868 	SPC_TRACE(("[mha_sched] "));
    869 	if (sc->sc_state != SPC_IDLE)
    870 		panic("mha_sched: not IDLE (state=%d)", sc->sc_state);
    871 
    872 	if (sc->sc_flags & SPC_ABORTING)
    873 		return;
    874 
    875 	/*
    876 	 * Find first acb in ready queue that is for a target/lunit
    877 	 * combinations that is not busy.
    878 	 */
    879 	for (acb = sc->ready_list.tqh_first; acb ; acb = acb->chain.tqe_next) {
    880 		struct spc_tinfo *ti;
    881 		periph = acb->xs->xs_periph;
    882 		t = periph->periph_target;
    883 		ti = &sc->sc_tinfo[t];
    884 		if (!(ti->lubusy & (1 << periph->periph_lun))) {
    885 			if ((acb->flags & ACB_QBITS) != ACB_QREADY)
    886 				panic("mha: busy entry on ready list");
    887 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    888 			ACB_SETQ(acb, ACB_QNONE);
    889 			sc->sc_nexus = acb;
    890 			sc->sc_flags = 0;
    891 			sc->sc_prevphase = INVALID_PHASE;
    892 			sc->sc_dp = acb->daddr;
    893 			sc->sc_dleft = acb->dleft;
    894 			ti->lubusy |= (1<<periph->periph_lun);
    895 			mhaselect(sc, t, periph->periph_lun,
    896 				     (u_char *)&acb->cmd, acb->clen);
    897 			break;
    898 		} else {
    899 			SPC_MISC(("%d:%d busy\n",
    900 			    periph->periph_target,
    901 			    periph->periph_lun));
    902 		}
    903 	}
    904 }
    905 
    906 /*
    908  * POST PROCESSING OF SCSI_CMD (usually current)
    909  */
    910 void
    911 mha_done(sc, acb)
    912 	struct mha_softc *sc;
    913 	struct acb *acb;
    914 {
    915 	struct scsipi_xfer *xs = acb->xs;
    916 	struct scsipi_periph *periph = xs->xs_periph;
    917 	struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
    918 
    919 	SPC_TRACE(("[mha_done(error:%x)] ", xs->error));
    920 
    921 #if 1
    922 	callout_stop(&acb->xs->xs_callout);
    923 #endif
    924 
    925 	/*
    926 	 * Now, if we've come here with no error code, i.e. we've kept the
    927 	 * initial XS_NOERROR, and the status code signals that we should
    928 	 * check sense, we'll need to set up a request sense cmd block and
    929 	 * push the command back into the ready queue *before* any other
    930 	 * commands for this target/lunit, else we lose the sense info.
    931 	 * We don't support chk sense conditions for the request sense cmd.
    932 	 */
    933 	if (xs->error == XS_NOERROR) {
    934 		if ((acb->flags & ACB_ABORTED) != 0) {
    935 			xs->error = XS_TIMEOUT;
    936 		} else if (acb->flags & ACB_CHKSENSE) {
    937 			xs->error = XS_SENSE;
    938 		} else {
    939 			xs->status = acb->stat & ST_MASK;
    940 			switch (xs->status) {
    941 			case SCSI_CHECK:
    942 				xs->resid = acb->dleft;
    943 				/* FALLTHOUGH */
    944 			case SCSI_BUSY:
    945 				xs->error = XS_BUSY;
    946 				break;
    947 			case SCSI_OK:
    948 				xs->resid = acb->dleft;
    949 				break;
    950 			default:
    951 				xs->error = XS_DRIVER_STUFFUP;
    952 #if SPC_DEBUG
    953 				printf("%s: mha_done: bad stat 0x%x\n",
    954 					sc->sc_dev.dv_xname, acb->stat);
    955 #endif
    956 				break;
    957 			}
    958 		}
    959 	}
    960 
    961 #if SPC_DEBUG
    962 	if ((mha_debug & SPC_SHOWMISC) != 0) {
    963 		if (xs->resid != 0)
    964 			printf("resid=%d ", xs->resid);
    965 		if (xs->error == XS_SENSE)
    966 			printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
    967 		else
    968 			printf("error=%d\n", xs->error);
    969 	}
    970 #endif
    971 
    972 	/*
    973 	 * Remove the ACB from whatever queue it's on.
    974 	 */
    975 	switch (acb->flags & ACB_QBITS) {
    976 	case ACB_QNONE:
    977 		if (acb != sc->sc_nexus) {
    978 			panic("%s: floating acb", sc->sc_dev.dv_xname);
    979 		}
    980 		sc->sc_nexus = NULL;
    981 		sc->sc_state = SPC_IDLE;
    982 		ti->lubusy &= ~(1<<periph->periph_lun);
    983 		mha_sched(sc);
    984 		break;
    985 	case ACB_QREADY:
    986 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    987 		break;
    988 	case ACB_QNEXUS:
    989 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    990 		ti->lubusy &= ~(1<<periph->periph_lun);
    991 		break;
    992 	case ACB_QFREE:
    993 		panic("%s: dequeue: busy acb on free list",
    994 			sc->sc_dev.dv_xname);
    995 		break;
    996 	default:
    997 		panic("%s: dequeue: unknown queue %d",
    998 			sc->sc_dev.dv_xname, acb->flags & ACB_QBITS);
    999 	}
   1000 
   1001 	/* Put it on the free list, and clear flags. */
   1002 #if 0
   1003 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
   1004 	acb->flags = ACB_QFREE;
   1005 #else
   1006 	mha_free_acb(sc, acb, xs->xs_control);
   1007 #endif
   1008 
   1009 	ti->cmds++;
   1010 	scsipi_done(xs);
   1011 }
   1012 
   1013 void
   1014 mha_dequeue(sc, acb)
   1015 	struct mha_softc *sc;
   1016 	struct acb *acb;
   1017 {
   1018 
   1019 	if (acb->flags & ACB_QNEXUS) {
   1020 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1021 	} else {
   1022 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1023 	}
   1024 }
   1025 
   1026 /*
   1028  * INTERRUPT/PROTOCOL ENGINE
   1029  */
   1030 
   1031 /*
   1032  * Schedule an outgoing message by prioritizing it, and asserting
   1033  * attention on the bus. We can only do this when we are the initiator
   1034  * else there will be an illegal command interrupt.
   1035  */
   1036 #define mha_sched_msgout(m) \
   1037 	do {				\
   1038 		SPC_MISC(("mha_sched_msgout %d ", m)); \
   1039 		CMR = CMD_SET_ATN;	\
   1040 		sc->sc_msgpriq |= (m);	\
   1041 	} while (0)
   1042 
   1043 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
   1044 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1045 #define ISEXTMSG(m) ((m) == 0x01)
   1046 
   1047 /*
   1048  * Precondition:
   1049  * The SCSI bus is already in the MSGI phase and there is a message byte
   1050  * on the bus, along with an asserted REQ signal.
   1051  */
   1052 void
   1053 mha_msgin(sc)
   1054 	register struct mha_softc *sc;
   1055 {
   1056 	register int v;
   1057 	int n;
   1058 
   1059 	SPC_TRACE(("[mha_msgin(curmsglen:%d)] ", sc->sc_imlen));
   1060 
   1061 	/*
   1062 	 * Prepare for a new message.  A message should (according
   1063 	 * to the SCSI standard) be transmitted in one single
   1064 	 * MESSAGE_IN_PHASE. If we have been in some other phase,
   1065 	 * then this is a new message.
   1066 	 */
   1067 	if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
   1068 		sc->sc_flags &= ~SPC_DROP_MSGI;
   1069 		sc->sc_imlen = 0;
   1070 	}
   1071 
   1072 	WAIT;
   1073 
   1074 	v = MBR;	/* modified byte */
   1075 	v = sc->sc_pcx[0];
   1076 
   1077 	sc->sc_imess[sc->sc_imlen] = v;
   1078 
   1079 	/*
   1080 	 * If we're going to reject the message, don't bother storing
   1081 	 * the incoming bytes.  But still, we need to ACK them.
   1082 	 */
   1083 
   1084 	if ((sc->sc_flags & SPC_DROP_MSGI)) {
   1085 		CMR = CMD_SET_ATN;
   1086 /*		ESPCMD(sc, ESPCMD_MSGOK);*/
   1087 		printf("<dropping msg byte %x>",
   1088 			sc->sc_imess[sc->sc_imlen]);
   1089 		return;
   1090 	}
   1091 
   1092 	if (sc->sc_imlen >= SPC_MAX_MSG_LEN) {
   1093 		mha_sched_msgout(SEND_REJECT);
   1094 		sc->sc_flags |= SPC_DROP_MSGI;
   1095 	} else {
   1096 		sc->sc_imlen++;
   1097 		/*
   1098 		 * This testing is suboptimal, but most
   1099 		 * messages will be of the one byte variety, so
   1100 		 * it should not effect performance
   1101 		 * significantly.
   1102 		 */
   1103 		if (sc->sc_imlen == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1104 			goto gotit;
   1105 		if (sc->sc_imlen == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1106 			goto gotit;
   1107 		if (sc->sc_imlen >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1108 		    sc->sc_imlen == sc->sc_imess[1] + 2)
   1109 			goto gotit;
   1110 	}
   1111 #if 0
   1112 	/* Ack what we have so far */
   1113 	ESPCMD(sc, ESPCMD_MSGOK);
   1114 #endif
   1115 	return;
   1116 
   1117 gotit:
   1118 	SPC_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
   1119 	/*
   1120 	 * Now we should have a complete message (1 byte, 2 byte
   1121 	 * and moderately long extended messages).  We only handle
   1122 	 * extended messages which total length is shorter than
   1123 	 * SPC_MAX_MSG_LEN.  Longer messages will be amputated.
   1124 	 */
   1125 	if (sc->sc_state == SPC_HASNEXUS) {
   1126 		struct acb *acb = sc->sc_nexus;
   1127 		struct spc_tinfo *ti =
   1128 			&sc->sc_tinfo[acb->xs->xs_periph->periph_target];
   1129 
   1130 		switch (sc->sc_imess[0]) {
   1131 		case MSG_CMDCOMPLETE:
   1132 			SPC_MSGS(("cmdcomplete "));
   1133 			if (sc->sc_dleft < 0) {
   1134 				struct scsipi_periph *periph = acb->xs->xs_periph;
   1135 				printf("mha: %d extra bytes from %d:%d\n",
   1136 					-sc->sc_dleft,
   1137 					periph->periph_target,
   1138 				        periph->periph_lun);
   1139 				sc->sc_dleft = 0;
   1140 			}
   1141 			acb->xs->resid = acb->dleft = sc->sc_dleft;
   1142 			sc->sc_flags |= SPC_BUSFREE_OK;
   1143 			break;
   1144 
   1145 		case MSG_MESSAGE_REJECT:
   1146 #if SPC_DEBUG
   1147 			if (mha_debug & SPC_SHOWMSGS)
   1148 				printf("%s: our msg rejected by target\n",
   1149 					sc->sc_dev.dv_xname);
   1150 #endif
   1151 #if 1 /* XXX - must remember last message */
   1152 			scsipi_printaddr(acb->xs->xs_periph);
   1153 			printf("MSG_MESSAGE_REJECT>>");
   1154 #endif
   1155 			if (sc->sc_flags & SPC_SYNCHNEGO) {
   1156 				ti->period = ti->offset = 0;
   1157 				sc->sc_flags &= ~SPC_SYNCHNEGO;
   1158 				ti->flags &= ~T_NEGOTIATE;
   1159 			}
   1160 			/* Not all targets understand INITIATOR_DETECTED_ERR */
   1161 			if (sc->sc_msgout == SEND_INIT_DET_ERR)
   1162 				mha_sched_msgout(SEND_ABORT);
   1163 			break;
   1164 		case MSG_NOOP:
   1165 			SPC_MSGS(("noop "));
   1166 			break;
   1167 		case MSG_DISCONNECT:
   1168 			SPC_MSGS(("disconnect "));
   1169 			ti->dconns++;
   1170 			sc->sc_flags |= SPC_DISCON;
   1171 			sc->sc_flags |= SPC_BUSFREE_OK;
   1172 			if ((acb->xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
   1173 				break;
   1174 			/*FALLTHROUGH*/
   1175 		case MSG_SAVEDATAPOINTER:
   1176 			SPC_MSGS(("save datapointer "));
   1177 			acb->dleft = sc->sc_dleft;
   1178 			acb->daddr = sc->sc_dp;
   1179 			break;
   1180 		case MSG_RESTOREPOINTERS:
   1181 			SPC_MSGS(("restore datapointer "));
   1182 			if (!acb) {
   1183 				mha_sched_msgout(SEND_ABORT);
   1184 				printf("%s: no DATAPOINTERs to restore\n",
   1185 				    sc->sc_dev.dv_xname);
   1186 				break;
   1187 			}
   1188 			sc->sc_dp = acb->daddr;
   1189 			sc->sc_dleft = acb->dleft;
   1190 			break;
   1191 		case MSG_PARITY_ERROR:
   1192 			printf("%s:target%d: MSG_PARITY_ERROR\n",
   1193 				sc->sc_dev.dv_xname,
   1194 				acb->xs->xs_periph->periph_target);
   1195 			break;
   1196 		case MSG_EXTENDED:
   1197 			SPC_MSGS(("extended(%x) ", sc->sc_imess[2]));
   1198 			switch (sc->sc_imess[2]) {
   1199 			case MSG_EXT_SDTR:
   1200 				SPC_MSGS(("SDTR period %d, offset %d ",
   1201 					sc->sc_imess[3], sc->sc_imess[4]));
   1202 				ti->period = sc->sc_imess[3];
   1203 				ti->offset = sc->sc_imess[4];
   1204 				if (sc->sc_minsync == 0) {
   1205 					/* We won't do synch */
   1206 					ti->offset = 0;
   1207 					mha_sched_msgout(SEND_SDTR);
   1208 				} else if (ti->offset == 0) {
   1209 					printf("%s:%d: async\n", "mha",
   1210 						acb->xs->xs_periph->periph_target);
   1211 					ti->offset = 0;
   1212 					sc->sc_flags &= ~SPC_SYNCHNEGO;
   1213 				} else if (ti->period > 124) {
   1214 					printf("%s:%d: async\n", "mha",
   1215 						acb->xs->xs_periph->periph_target);
   1216 					ti->offset = 0;
   1217 					mha_sched_msgout(SEND_SDTR);
   1218 				} else {
   1219 					int r = 250/ti->period;
   1220 					int s = (100*250)/ti->period - 100*r;
   1221 					int p;
   1222 #if 0
   1223 					p =  mha_stp2cpb(sc, ti->period);
   1224 					ti->period = mha_cpb2stp(sc, p);
   1225 #endif
   1226 
   1227 #if SPC_DEBUG
   1228 					scsipi_printaddr(acb->xs->xs_periph);
   1229 #endif
   1230 					if ((sc->sc_flags&SPC_SYNCHNEGO) == 0) {
   1231 						/* Target initiated negotiation */
   1232 						if (ti->flags & T_SYNCMODE) {
   1233 						    ti->flags &= ~T_SYNCMODE;
   1234 #if SPC_DEBUG
   1235 						    printf("renegotiated ");
   1236 #endif
   1237 						}
   1238 						TMR=TM_ASYNC;
   1239 						/* Clamp to our maxima */
   1240 						if (ti->period < sc->sc_minsync)
   1241 							ti->period = sc->sc_minsync;
   1242 						if (ti->offset > 15)
   1243 							ti->offset = 15;
   1244 						mha_sched_msgout(SEND_SDTR);
   1245 					} else {
   1246 						/* we are sync */
   1247 						sc->sc_flags &= ~SPC_SYNCHNEGO;
   1248 						TMR = TM_SYNC;
   1249 						ti->flags |= T_SYNCMODE;
   1250 					}
   1251 #if SPC_DEBUG
   1252 					printf("max sync rate %d.%02dMb/s\n",
   1253 						r, s);
   1254 #endif
   1255 				}
   1256 				ti->flags &= ~T_NEGOTIATE;
   1257 				break;
   1258 			default: /* Extended messages we don't handle */
   1259 				CMR = CMD_SET_ATN; /* XXX? */
   1260 				break;
   1261 			}
   1262 			break;
   1263 		default:
   1264 			SPC_MSGS(("ident "));
   1265 			/* thanks for that ident... */
   1266 			if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1267 				SPC_MISC(("unknown "));
   1268 printf("%s: unimplemented message: %d\n", sc->sc_dev.dv_xname, sc->sc_imess[0]);
   1269 				CMR = CMD_SET_ATN; /* XXX? */
   1270 			}
   1271 			break;
   1272 		}
   1273 	} else if (sc->sc_state == SPC_RESELECTED) {
   1274 		struct scsipi_periph *periph = NULL;
   1275 		struct acb *acb;
   1276 		struct spc_tinfo *ti;
   1277 		u_char lunit;
   1278 
   1279 		if (MSG_ISIDENTIFY(sc->sc_imess[0])) { 	/* Identify? */
   1280 			SPC_MISC(("searching "));
   1281 			/*
   1282 			 * Search wait queue for disconnected cmd
   1283 			 * The list should be short, so I haven't bothered with
   1284 			 * any more sophisticated structures than a simple
   1285 			 * singly linked list.
   1286 			 */
   1287 			lunit = sc->sc_imess[0] & 0x07;
   1288 			for (acb = sc->nexus_list.tqh_first; acb;
   1289 			     acb = acb->chain.tqe_next) {
   1290 				periph = acb->xs->xs_periph;
   1291 				if (periph->periph_lun == lunit &&
   1292 				    sc->sc_selid == (1<<periph->periph_target)) {
   1293 					TAILQ_REMOVE(&sc->nexus_list, acb,
   1294 					    chain);
   1295 					ACB_SETQ(acb, ACB_QNONE);
   1296 					break;
   1297 				}
   1298 			}
   1299 
   1300 			if (!acb) {		/* Invalid reselection! */
   1301 				mha_sched_msgout(SEND_ABORT);
   1302 				printf("mha: invalid reselect (idbit=0x%2x)\n",
   1303 				    sc->sc_selid);
   1304 			} else {		/* Reestablish nexus */
   1305 				/*
   1306 				 * Setup driver data structures and
   1307 				 * do an implicit RESTORE POINTERS
   1308 				 */
   1309 				ti = &sc->sc_tinfo[periph->periph_target];
   1310 				sc->sc_nexus = acb;
   1311 				sc->sc_dp = acb->daddr;
   1312 				sc->sc_dleft = acb->dleft;
   1313 				sc->sc_tinfo[periph->periph_target].lubusy
   1314 					|= (1<<periph->periph_lun);
   1315 				if (ti->flags & T_SYNCMODE) {
   1316 					TMR = TM_SYNC;	/* XXX */
   1317 				} else {
   1318 					TMR = TM_ASYNC;
   1319 				}
   1320 				SPC_MISC(("... found acb"));
   1321 				sc->sc_state = SPC_HASNEXUS;
   1322 			}
   1323 		} else {
   1324 			printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
   1325 			    sc->sc_dev.dv_xname, sc->sc_selid);
   1326 			mha_sched_msgout(SEND_DEV_RESET);
   1327 		}
   1328 	} else { /* Neither SPC_HASNEXUS nor SPC_RESELECTED! */
   1329 		printf("%s: unexpected message in; will send DEV_RESET\n",
   1330 		    sc->sc_dev.dv_xname);
   1331 		mha_sched_msgout(SEND_DEV_RESET);
   1332 	}
   1333 
   1334 	/* Ack last message byte */
   1335 #if 0
   1336 	ESPCMD(sc, ESPCMD_MSGOK);
   1337 #endif
   1338 
   1339 	/* Done, reset message pointer. */
   1340 	sc->sc_flags &= ~SPC_DROP_MSGI;
   1341 	sc->sc_imlen = 0;
   1342 }
   1343 
   1344 /*
   1345  * Send the highest priority, scheduled message.
   1346  */
   1347 void
   1348 mha_msgout(sc)
   1349 	register struct mha_softc *sc;
   1350 {
   1351 	struct spc_tinfo *ti;
   1352 	int n;
   1353 
   1354 	SPC_TRACE(("mha_msgout  "));
   1355 
   1356 	if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   1357 		if (sc->sc_omp == sc->sc_omess) {
   1358 			/*
   1359 			 * This is a retransmission.
   1360 			 *
   1361 			 * We get here if the target stayed in MESSAGE OUT
   1362 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1363 			 * that all of the previously transmitted messages must
   1364 			 * be sent again, in the same order.  Therefore, we
   1365 			 * requeue all the previously transmitted messages, and
   1366 			 * start again from the top.  Our simple priority
   1367 			 * scheme keeps the messages in the right order.
   1368 			 */
   1369 			SPC_MISC(("retransmitting  "));
   1370 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1371 			/*
   1372 			 * Set ATN.  If we're just sending a trivial 1-byte
   1373 			 * message, we'll clear ATN later on anyway.
   1374 			 */
   1375 			CMR = CMD_SET_ATN; /* XXX? */
   1376 		} else {
   1377 			/* This is a continuation of the previous message. */
   1378 			n = sc->sc_omp - sc->sc_omess;
   1379 			goto nextbyte;
   1380 		}
   1381 	}
   1382 
   1383 	/* No messages transmitted so far. */
   1384 	sc->sc_msgoutq = 0;
   1385 	sc->sc_lastmsg = 0;
   1386 
   1387 nextmsg:
   1388 	/* Pick up highest priority message. */
   1389 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1390 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1391 	sc->sc_msgoutq |= sc->sc_currmsg;
   1392 
   1393 	/* Build the outgoing message data. */
   1394 	switch (sc->sc_currmsg) {
   1395 	case SEND_IDENTIFY:
   1396 		SPC_ASSERT(sc->sc_nexus != NULL);
   1397 		sc->sc_omess[0] =
   1398 		    MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1);
   1399 		n = 1;
   1400 		break;
   1401 
   1402 #if SPC_USE_SYNCHRONOUS
   1403 	case SEND_SDTR:
   1404 		SPC_ASSERT(sc->sc_nexus != NULL);
   1405 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
   1406 		sc->sc_omess[4] = MSG_EXTENDED;
   1407 		sc->sc_omess[3] = 3;
   1408 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1409 		sc->sc_omess[1] = ti->period >> 2;
   1410 		sc->sc_omess[0] = ti->offset;
   1411 		n = 5;
   1412 		break;
   1413 #endif
   1414 
   1415 #if SPC_USE_WIDE
   1416 	case SEND_WDTR:
   1417 		SPC_ASSERT(sc->sc_nexus != NULL);
   1418 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
   1419 		sc->sc_omess[3] = MSG_EXTENDED;
   1420 		sc->sc_omess[2] = 2;
   1421 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1422 		sc->sc_omess[0] = ti->width;
   1423 		n = 4;
   1424 		break;
   1425 #endif
   1426 
   1427 	case SEND_DEV_RESET:
   1428 		sc->sc_flags |= SPC_ABORTING;
   1429 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1430 		n = 1;
   1431 		break;
   1432 
   1433 	case SEND_REJECT:
   1434 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1435 		n = 1;
   1436 		break;
   1437 
   1438 	case SEND_PARITY_ERROR:
   1439 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1440 		n = 1;
   1441 		break;
   1442 
   1443 	case SEND_INIT_DET_ERR:
   1444 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1445 		n = 1;
   1446 		break;
   1447 
   1448 	case SEND_ABORT:
   1449 		sc->sc_flags |= SPC_ABORTING;
   1450 		sc->sc_omess[0] = MSG_ABORT;
   1451 		n = 1;
   1452 		break;
   1453 
   1454 	default:
   1455 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1456 		    sc->sc_dev.dv_xname);
   1457 		SPC_BREAK();
   1458 		sc->sc_omess[0] = MSG_NOOP;
   1459 		n = 1;
   1460 		break;
   1461 	}
   1462 	sc->sc_omp = &sc->sc_omess[n];
   1463 
   1464 nextbyte:
   1465 	/* Send message bytes. */
   1466 	/* send TRANSFER command. */
   1467 	sc->sc_ps[3] = 1;
   1468 	sc->sc_ps[4] = n >> 8;
   1469 	sc->sc_pc[10] = n;
   1470 	sc->sc_ps[-1] = 0x000F;	/* burst */
   1471 	asm volatile ("nop");
   1472 	CMR = CMD_SEND_FROM_DMA;	/* send from DMA */
   1473 	for (;;) {
   1474 		if ((SSR & SS_BUSY) != 0)
   1475 			break;
   1476 		if (SSR & SS_IREQUEST)
   1477 			goto out;
   1478 	}
   1479 	for (;;) {
   1480 #if 0
   1481 		for (;;) {
   1482 			if ((PSNS & PSNS_REQ) != 0)
   1483 				break;
   1484 			/* Wait for REQINIT.  XXX Need timeout. */
   1485 		}
   1486 #endif
   1487 		if (SSR & SS_IREQUEST) {
   1488 			/*
   1489 			 * Target left MESSAGE OUT, possibly to reject
   1490 			 * our message.
   1491 			 *
   1492 			 * If this is the last message being sent, then we
   1493 			 * deassert ATN, since either the target is going to
   1494 			 * ignore this message, or it's going to ask for a
   1495 			 * retransmission via MESSAGE PARITY ERROR (in which
   1496 			 * case we reassert ATN anyway).
   1497 			 */
   1498 #if 0
   1499 			if (sc->sc_msgpriq == 0)
   1500 				CMR = CMD_RESET_ATN;
   1501 #endif
   1502 			goto out;
   1503 		}
   1504 
   1505 #if 0
   1506 		/* Clear ATN before last byte if this is the last message. */
   1507 		if (n == 1 && sc->sc_msgpriq == 0)
   1508 			CMR = CMD_RESET_ATN;
   1509 #endif
   1510 
   1511 		while ((SSR & SS_DREG_FULL) != 0)
   1512 			;
   1513 		/* Send message byte. */
   1514 		sc->sc_pc[0] = *--sc->sc_omp;
   1515 		--n;
   1516 		/* Keep track of the last message we've sent any bytes of. */
   1517 		sc->sc_lastmsg = sc->sc_currmsg;
   1518 
   1519 		if (n == 0)
   1520 			break;
   1521 	}
   1522 
   1523 	/* We get here only if the entire message has been transmitted. */
   1524 	if (sc->sc_msgpriq != 0) {
   1525 		/* There are more outgoing messages. */
   1526 		goto nextmsg;
   1527 	}
   1528 
   1529 	/*
   1530 	 * The last message has been transmitted.  We need to remember the last
   1531 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1532 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1533 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1534 	 * request a retransmit).
   1535 	 */
   1536 
   1537 out:
   1538 	/* Disable REQ/ACK protocol. */
   1539 }
   1540 
   1541 
   1542 /***************************************************************
   1544  *
   1545  *	datain/dataout
   1546  *
   1547  */
   1548 
   1549 int
   1550 mha_datain_pio(sc, p, n)
   1551 	register struct mha_softc *sc;
   1552 	u_char *p;
   1553 	int n;
   1554 {
   1555 	u_short d;
   1556 	int a;
   1557 	int total_n = n;
   1558 
   1559 	SPC_TRACE(("[mha_datain_pio(%x,%d)", p, n));
   1560 
   1561 	WAIT;
   1562 	sc->sc_ps[3] = 1;
   1563 	sc->sc_ps[4] = n >> 8;
   1564 	sc->sc_pc[10] = n;
   1565 	/* $BHa$7$-%=%U%HE>Aw(B */
   1566 	CMR = CMD_RECEIVE_TO_MPU;
   1567 	for (;;) {
   1568 		a = SSR;
   1569 		if (a & 0x04) {
   1570 			d = sc->sc_ps[0];
   1571 			*p++ = d >> 8;
   1572 			if (--n > 0) {
   1573 				*p++ = d;
   1574 				--n;
   1575 			}
   1576 			a = SSR;
   1577 		}
   1578 		if (a & 0x40)
   1579 			continue;
   1580 		if (a & 0x80)
   1581 			break;
   1582 	}
   1583 	SPC_TRACE(("...%d resd]", n));
   1584 	return total_n - n;
   1585 }
   1586 
   1587 int
   1588 mha_dataout_pio(sc, p, n)
   1589 	register struct mha_softc *sc;
   1590 	u_char *p;
   1591 	int n;
   1592 {
   1593 	u_short d;
   1594 	int a;
   1595 	int total_n = n;
   1596 
   1597 	SPC_TRACE(("[mha_dataout_pio(%x,%d)", p, n));
   1598 
   1599 	WAIT;
   1600 	sc->sc_ps[3] = 1;
   1601 	sc->sc_ps[4] = n >> 8;
   1602 	sc->sc_pc[10] = n;
   1603 	/* $BHa$7$-%=%U%HE>Aw(B */
   1604 	CMR = CMD_SEND_FROM_MPU;
   1605 	for (;;) {
   1606 		a = SSR;
   1607 		if (a & 0x04) {
   1608 			d = *p++ << 8;
   1609 			if (--n > 0) {
   1610 				d |= *p++;
   1611 				--n;
   1612 			}
   1613 			sc->sc_ps[0] = d;
   1614 			a = SSR;
   1615 		}
   1616 		if (a & 0x40)
   1617 			continue;
   1618 		if (a & 0x80)
   1619 			break;
   1620 	}
   1621 	SPC_TRACE(("...%d resd]", n));
   1622 	return total_n - n;
   1623 }
   1624 
   1625 static int
   1626 mha_dataio_dma(dw, cw, sc, p, n)
   1627 	int dw;		/* DMA word */
   1628 	int cw;		/* CMR word */
   1629 	register struct mha_softc *sc;
   1630 	u_char *p;
   1631 	int n;
   1632 {
   1633   char *paddr, *vaddr;
   1634 
   1635   if (n > MAXBSIZE)
   1636     panic("transfer size exceeds MAXBSIZE");
   1637   if (sc->sc_dmasize > 0)
   1638     panic("DMA request while another DMA transfer is in pregress");
   1639 
   1640   if (cw == CMD_SEND_FROM_DMA) {
   1641     memcpy(sc->sc_dmabuf, p, n);
   1642     bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREWRITE);
   1643   } else {
   1644     bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREREAD);
   1645   }
   1646   sc->sc_p = p;
   1647   sc->sc_dmasize = n;
   1648 
   1649   paddr = (char *)sc->sc_dmaseg[0].ds_addr;
   1650 #if MHA_DMA_SHORT_BUS_CYCLE == 1
   1651   if ((*(int *)&IODEVbase->io_sram[0xac]) & (1 << ((paddr_t)paddr >> 19)))
   1652     dw &= ~(1 << 3);
   1653 #endif
   1654   dma_cachectl((caddr_t) sc->sc_dmabuf, n);
   1655 #if 0
   1656   printf("(%x,%x)->(%x,%x)\n", p, n, paddr, n);
   1657   PCIA();	/* XXX */
   1658 #endif
   1659   sc->sc_pc[0x80 + (((long)paddr >> 16) & 0xFF)] = 0;
   1660   sc->sc_pc[0x180 + (((long)paddr >> 8) & 0xFF)] = 0;
   1661   sc->sc_pc[0x280 + (((long)paddr >> 0) & 0xFF)] = 0;
   1662   WAIT;
   1663   sc->sc_ps[3] = 1;
   1664   sc->sc_ps[4] = n >> 8;
   1665   sc->sc_pc[10] = n;
   1666   /* DMA $BE>Aw@)8f$O0J2<$NDL$j!#(B
   1667      3 ... short bus cycle
   1668      2 ... MAXIMUM XFER.
   1669      1 ... BURST XFER.
   1670      0 ... R/W */
   1671   sc->sc_ps[-1] = dw;	/* burst */
   1672   asm volatile ("nop");
   1673   CMR = cw;	/* receive to DMA */
   1674   return n;
   1675 }
   1676 int
   1677 mha_dataout(sc, p, n)
   1678 	register struct mha_softc *sc;
   1679 	u_char *p;
   1680 	int n;
   1681 {
   1682   register struct acb *acb = sc->sc_nexus;
   1683 
   1684   if (n == 0)
   1685     return n;
   1686 
   1687   if (n & 1)
   1688     return mha_dataout_pio(sc, p, n);
   1689   return mha_dataio_dma(MHA_DMA_DATAOUT, CMD_SEND_FROM_DMA, sc, p, n);
   1690 }
   1691 
   1692 int
   1694 mha_datain(sc, p, n)
   1695 	register struct mha_softc *sc;
   1696 	u_char *p;
   1697 	int n;
   1698 {
   1699   int ts;
   1700   register struct acb *acb = sc->sc_nexus;
   1701   char *paddr, *vaddr;
   1702 
   1703   if (n == 0)
   1704     return n;
   1705   if (acb->cmd.opcode == REQUEST_SENSE || (n & 1))
   1706     return mha_datain_pio(sc, p, n);
   1707   return mha_dataio_dma(MHA_DMA_DATAIN, CMD_RECEIVE_TO_DMA, sc, p, n);
   1708 }
   1709 
   1710 
   1712 /*
   1713  * Catch an interrupt from the adaptor
   1714  */
   1715 /*
   1716  * This is the workhorse routine of the driver.
   1717  * Deficiencies (for now):
   1718  * 1) always uses programmed I/O
   1719  */
   1720 int
   1721 mhaintr(arg)
   1722 	void *arg;
   1723 {
   1724 	struct mha_softc *sc = arg;
   1725 #if 0
   1726 	u_char ints;
   1727 #endif
   1728 	struct acb *acb;
   1729 	struct scsipi_periph *periph;
   1730 	struct spc_tinfo *ti;
   1731 	u_char ph;
   1732 	u_short r;
   1733 	int n;
   1734 
   1735 #if 1	/* XXX called during attach? */
   1736 	if (tmpsc != NULL) {
   1737 		SPC_MISC(("[%x %x]\n", mha_cd.cd_devs, sc));
   1738 		sc = tmpsc;
   1739 	} else {
   1740 #endif
   1741 
   1742 #if 1	/* XXX */
   1743 	}
   1744 #endif
   1745 
   1746 #if 0
   1747 	/*
   1748 	 * $B3d$j9~$_6X;_$K$9$k(B
   1749 	 */
   1750 	SCTL &= ~SCTL_INTR_ENAB;
   1751 #endif
   1752 
   1753 	SPC_TRACE(("[mhaintr]"));
   1754 
   1755  loop:
   1756 	/*
   1757 	 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
   1758 	 */
   1759 	/*
   1760 	 * First check for abnormal conditions, such as reset.
   1761 	 */
   1762 #if 0
   1763 #if 1 /* XXX? */
   1764 	while (((ints = SSR) & SS_IREQUEST) == 0)
   1765 		delay(1);
   1766 	SPC_MISC(("ints = 0x%x  ", ints));
   1767 #else /* usually? */
   1768 	ints = SSR;
   1769 #endif
   1770 #endif
   1771 	while (SSR & SS_IREQUEST) {
   1772 		acb = sc->sc_nexus;
   1773 		r = ISCSR;
   1774 		SPC_MISC(("[r=0x%x]", r));
   1775 		switch (r >> 8) {
   1776 		default:
   1777 			printf("[addr=%x\n"
   1778 			       "result=0x%x\n"
   1779 			       "cmd=0x%x\n"
   1780 			       "ph=0x%x(ought to be %d)]\n",
   1781 			       &ISCSR,
   1782 			       r,
   1783 			       acb->xs->cmd->opcode,
   1784 			       SCR, sc->sc_phase);
   1785 			panic("unexpected result.");
   1786 		case 0x82:	/* selection timeout */
   1787 			SPC_MISC(("selection timeout  "));
   1788 			sc->sc_phase = BUSFREE_PHASE;
   1789 			SPC_ASSERT(sc->sc_nexus != NULL);
   1790 			acb = sc->sc_nexus;
   1791 			delay(250);
   1792 			acb->xs->error = XS_SELTIMEOUT;
   1793 			mha_done(sc, acb);
   1794 			continue;	/* XXX ??? msaitoh */
   1795 		case 0x60:	/* command completed */
   1796 			sc->sc_spcinitialized++;
   1797 			if (sc->sc_phase == BUSFREE_PHASE)
   1798 				continue;
   1799 			ph = SCR;
   1800 			if (ph & PSNS_ACK) {
   1801 				int s;
   1802 				/* $B$U$D!<$N%3%^%s%I$,=*N;$7$?$i$7$$(B */
   1803 				SPC_MISC(("0x60)phase = %x(ought to be %x)\n",
   1804 					  ph & PHASE_MASK, sc->sc_phase));
   1805 #if 0
   1806 /*				switch (sc->sc_phase) {*/
   1807 #else
   1808 				switch (ph & PHASE_MASK) {
   1809 #endif
   1810 				case STATUS_PHASE:
   1811 					if (sc->sc_state != SPC_HASNEXUS)
   1812 						printf("stsin: !SPC_HASNEXUS->(%d)\n",
   1813 						       sc->sc_state);
   1814 					SPC_ASSERT(sc->sc_nexus != NULL);
   1815 					acb = sc->sc_nexus;
   1816 					WAIT;
   1817 					s = MBR;
   1818 					SPC_ASSERT(s == 1);
   1819 					acb->stat = sc->sc_pcx[0]; /* XXX */
   1820 					SPC_MISC(("stat=0x%02x  ", acb->stat));
   1821 					sc->sc_prevphase = STATUS_PHASE;
   1822 					break;
   1823 				case MESSAGE_IN_PHASE:
   1824 					mha_msgin(sc);
   1825 					sc->sc_prevphase = MESSAGE_IN_PHASE;
   1826 					/* thru */
   1827 				case DATA_IN_PHASE:
   1828 					if (sc->sc_dmasize == 0)
   1829 						break;
   1830 					bus_dmamap_sync(sc->sc_dmat,
   1831 							sc->sc_dmamap,
   1832 							0, sc->sc_dmasize,
   1833 							BUS_DMASYNC_POSTREAD);
   1834 					memcpy(sc->sc_p, sc->sc_dmabuf,
   1835 					       sc->sc_dmasize);
   1836 					sc->sc_dmasize = 0;
   1837 					break;
   1838 				case DATA_OUT_PHASE:
   1839 					if (sc->sc_dmasize == 0)
   1840 						break;
   1841 					bus_dmamap_sync(sc->sc_dmat,
   1842 							sc->sc_dmamap,
   1843 							0, sc->sc_dmasize,
   1844 							BUS_DMASYNC_POSTWRITE);
   1845 					sc->sc_dmasize = 0;
   1846 					break;
   1847 				}
   1848 				WAIT;
   1849 				CMR = CMD_RESET_ACK;	/* reset ack */
   1850 				/*mha_done(sc, acb);	XXX */
   1851 				continue;
   1852 			} else if (NSR & 0x80) { /* nexus */
   1853 #if 1
   1854 				if (sc->sc_state == SPC_SELECTING)	/* XXX msaitoh */
   1855 					sc->sc_state = SPC_HASNEXUS;
   1856 				/* $B%U%'!<%:$N7h$aBG$A$r$9$k(B
   1857 				   $B30$l$?$i!"(Binitial-phase error(0x54) $B$,(B
   1858 				   $BJV$C$F$/$k$s$GCm0U$7$?$^$(!#(B
   1859 				   $B$G$b$J$<$+(B 0x65 $B$,JV$C$F$-$?$j$7$F$M!<$+(B? */
   1860 				WAIT;
   1861 				if (SSR & SS_IREQUEST)
   1862 					continue;
   1863 				switch (sc->sc_phase) {
   1864 				default:
   1865 					panic("$B8+CN$i$L(B phase $B$,Mh$A$^$C$?$@$h(B");
   1866 				case MESSAGE_IN_PHASE:
   1867 					/* $B2?$b$7$J$$(B */
   1868 					continue;
   1869 				case STATUS_PHASE:
   1870 					sc->sc_phase = MESSAGE_IN_PHASE;
   1871 					CMR = CMD_RECEIVE_MSG;	/* receive msg */
   1872 					continue;
   1873 				case DATA_IN_PHASE:
   1874 					sc->sc_prevphase = DATA_IN_PHASE;
   1875 					if (sc->sc_dleft == 0) {
   1876 						/* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
   1877 						   $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
   1878 						sc->sc_phase = STATUS_PHASE;
   1879 						CMR = CMD_RECEIVE_STS;	/* receive sts */
   1880 						continue;
   1881 					}
   1882 					n = mha_datain(sc, sc->sc_dp,
   1883 						       sc->sc_dleft);
   1884 					sc->sc_dp += n;
   1885 					sc->sc_dleft -= n;
   1886 					continue;
   1887 				case DATA_OUT_PHASE:
   1888 					sc->sc_prevphase = DATA_OUT_PHASE;
   1889 					if (sc->sc_dleft == 0) {
   1890 						/* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
   1891 						   $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
   1892 						sc->sc_phase = STATUS_PHASE;
   1893 						CMR = CMD_RECEIVE_STS;	/* receive sts */
   1894 						continue;
   1895 					}
   1896 					/* data phase $B$NB3$-$r$d$m$&(B */
   1897 					n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
   1898 					sc->sc_dp += n;
   1899 					sc->sc_dleft -= n;
   1900 					continue;
   1901 				case COMMAND_PHASE:
   1902 					/* $B:G=i$O(B CMD PHASE $B$H$$$&$3$H$i$7$$(B */
   1903 					if (acb->dleft) {
   1904 						/* $B%G!<%?E>Aw$,$"$j$&$k>l9g(B */
   1905 						if (acb->xs->xs_control & XS_CTL_DATA_IN) {
   1906 							sc->sc_phase = DATA_IN_PHASE;
   1907 							n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
   1908 							sc->sc_dp += n;
   1909 							sc->sc_dleft -= n;
   1910 						}
   1911 						else if (acb->xs->xs_control & XS_CTL_DATA_OUT) {
   1912 							sc->sc_phase = DATA_OUT_PHASE;
   1913 							n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
   1914 							sc->sc_dp += n;
   1915 							sc->sc_dleft -= n;
   1916 						}
   1917 						continue;
   1918 					}
   1919 					else {
   1920 						/* $B%G!<%?E>Aw$O$J$$$i$7$$(B?! */
   1921 						WAIT;
   1922 						sc->sc_phase = STATUS_PHASE;
   1923 						CMR = CMD_RECEIVE_STS;	/* receive sts */
   1924 						continue;
   1925 					}
   1926 				}
   1927 #endif
   1928 			}
   1929 			continue;
   1930 		case 0x31:	/* disconnected in xfer progress. */
   1931 			SPC_MISC(("[0x31]"));
   1932 		case 0x70:	/* disconnected. */
   1933 			SPC_ASSERT(sc->sc_flags & SPC_BUSFREE_OK);
   1934 			sc->sc_phase = BUSFREE_PHASE;
   1935 			sc->sc_state = SPC_IDLE;
   1936 #if 1
   1937 			acb = sc->sc_nexus;
   1938 			SPC_ASSERT(sc->sc_nexus != NULL);
   1939 			acb->xs->error = XS_NOERROR;
   1940 			mha_done(sc, acb);
   1941 #else
   1942 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1943 			mha_sched(sc);
   1944 #endif
   1945 			continue;
   1946 		case 0x32:	/* phase error in xfer progress. */
   1947 			SPC_MISC(("[0x32]"));
   1948 #if 0
   1949 		case 0x65:	/* invalid command.
   1950 				   $B$J$<$3$s$J$b$N$,=P$k$N$+(B
   1951 				   $B26$K$OA4$/M}2r$G$-$J$$(B */
   1952 #if 1
   1953 			SPC_MISC(("[0x%04x]", r));
   1954 #endif
   1955 #endif
   1956 		case 0x54:	/* initial-phase error. */
   1957 			SPC_MISC(("[0x54, ns=%x, ph=%x(ought to be %x)]",
   1958 				  NSR,
   1959 				  SCR, sc->sc_phase));
   1960 			/* thru */
   1961 		case 0x71:	/* assert req */
   1962 			WAIT;
   1963 			if (SSR & 0x40) {
   1964 				printf("SPC sts=%2x, r=%04x, ns=%x, ph=%x\n",
   1965 				       SSR, r, NSR, SCR);
   1966 				WAIT;
   1967 			}
   1968 			ph = SCR;
   1969 			if (sc->sc_state == SPC_SELECTING) {	/* XXX msaitoh */
   1970 				sc->sc_state = SPC_HASNEXUS;
   1971 			}
   1972 			if (ph & 0x80) {
   1973 				switch (ph & PHASE_MASK) {
   1974 				default:
   1975 					printf("phase = %x\n", ph);
   1976 					panic("assert req: the phase I don't know!");
   1977 				case DATA_IN_PHASE:
   1978 					sc->sc_prevphase = DATA_IN_PHASE;
   1979 					SPC_MISC(("DATAIN(%d)...", sc->sc_dleft));
   1980 					n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
   1981 					sc->sc_dp += n;
   1982 					sc->sc_dleft -= n;
   1983 					SPC_MISC(("done\n"));
   1984 					continue;
   1985 				case DATA_OUT_PHASE:
   1986 					sc->sc_prevphase = DATA_OUT_PHASE;
   1987 					SPC_MISC(("DATAOUT\n"));
   1988 					n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
   1989 					sc->sc_dp += n;
   1990 					sc->sc_dleft -= n;
   1991 					continue;
   1992 				case STATUS_PHASE:
   1993 					sc->sc_phase = STATUS_PHASE;
   1994 					SPC_MISC(("[RECV_STS]"));
   1995 					WAIT;
   1996 					CMR = CMD_RECEIVE_STS;	/* receive sts */
   1997 					continue;
   1998 				case MESSAGE_IN_PHASE:
   1999 					sc->sc_phase = MESSAGE_IN_PHASE;
   2000 					WAIT;
   2001 					CMR = CMD_RECEIVE_MSG;
   2002 					continue;
   2003 				}
   2004 			}
   2005 			continue;
   2006 		}
   2007 	}
   2008 }
   2009 
   2010 void
   2011 mha_abort(sc, acb)
   2012 	struct mha_softc *sc;
   2013 	struct acb *acb;
   2014 {
   2015 	acb->flags |= ACB_ABORTED;
   2016 
   2017 	if (acb == sc->sc_nexus) {
   2018 		/*
   2019 		 * If we're still selecting, the message will be scheduled
   2020 		 * after selection is complete.
   2021 		 */
   2022 		if (sc->sc_state == SPC_HASNEXUS) {
   2023 			sc->sc_flags |= SPC_ABORTING;
   2024 			mha_sched_msgout(SEND_ABORT);
   2025 		}
   2026 	} else {
   2027 		if (sc->sc_state == SPC_IDLE)
   2028 			mha_sched(sc);
   2029 	}
   2030 }
   2031 
   2032 void
   2033 mha_timeout(arg)
   2034 	void *arg;
   2035 {
   2036 	int s = splbio();
   2037 	struct acb *acb = (struct acb *)arg;
   2038 	struct scsipi_xfer *xs = acb->xs;
   2039 	struct scsipi_periph *periph = xs->xs_periph;
   2040 	struct mha_softc *sc =
   2041 	    (void*)periph->periph_chan->chan_adapter->adapt_dev;
   2042 
   2043 	scsipi_printaddr(periph);
   2044 again:
   2045 	printf("%s: timed out [acb %p (flags 0x%x, dleft %x, stat %x)], "
   2046 	       "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) >",
   2047 		sc->sc_dev.dv_xname,
   2048 		acb, acb->flags, acb->dleft, acb->stat,
   2049 		sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
   2050 		sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout
   2051 		);
   2052 	printf("[%04x %02x]\n", sc->sc_ps[1], SCR);
   2053 	panic("timeout, ouch!");
   2054 
   2055 	if (acb->flags & ACB_ABORTED) {
   2056 		/* abort timed out */
   2057 		printf(" AGAIN\n");
   2058 #if 0
   2059 		mha_init(sc, 1); /* XXX 1?*/
   2060 #endif
   2061 	} else {
   2062 		/* abort the operation that has timed out */
   2063 		printf("\n");
   2064 		xs->error = XS_TIMEOUT;
   2065 		mha_abort(sc, acb);
   2066 	}
   2067 
   2068 	splx(s);
   2069 }
   2070 
   2071 #if SPC_DEBUG
   2073 /*
   2074  * The following functions are mostly used for debugging purposes, either
   2075  * directly called from the driver or from the kernel debugger.
   2076  */
   2077 
   2078 void
   2079 mha_show_scsi_cmd(acb)
   2080 	struct acb *acb;
   2081 {
   2082 	u_char  *b = (u_char *)&acb->cmd;
   2083 	struct scsipi_periph *periph = acb->xs->xs_periph;
   2084 	int i;
   2085 
   2086 	scsipi_printaddr(periph);
   2087 	if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
   2088 		for (i = 0; i < acb->clen; i++) {
   2089 			if (i)
   2090 				printf(",");
   2091 			printf("%x", b[i]);
   2092 		}
   2093 		printf("\n");
   2094 	} else
   2095 		printf("RESET\n");
   2096 }
   2097 
   2098 void
   2099 mha_print_acb(acb)
   2100 	struct acb *acb;
   2101 {
   2102 
   2103 	printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
   2104 	printf(" dp=%x dleft=%d stat=%x\n",
   2105 	    (long)acb->daddr, acb->dleft, acb->stat);
   2106 	mha_show_scsi_cmd(acb);
   2107 }
   2108 
   2109 void
   2110 mha_print_active_acb()
   2111 {
   2112 	struct acb *acb;
   2113 	struct mha_softc *sc = mha_cd.cd_devs[0]; /* XXX */
   2114 
   2115 	printf("ready list:\n");
   2116 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   2117 	    acb = acb->chain.tqe_next)
   2118 		mha_print_acb(acb);
   2119 	printf("nexus:\n");
   2120 	if (sc->sc_nexus != NULL)
   2121 		mha_print_acb(sc->sc_nexus);
   2122 	printf("nexus list:\n");
   2123 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   2124 	    acb = acb->chain.tqe_next)
   2125 		mha_print_acb(acb);
   2126 }
   2127 
   2128 void
   2129 mha_dump_driver(sc)
   2130 	struct mha_softc *sc;
   2131 {
   2132 	struct spc_tinfo *ti;
   2133 	int i;
   2134 
   2135 	printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2136 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
   2137 	    sc->sc_state, sc->sc_imess[0],
   2138 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2139 	for (i = 0; i < 7; i++) {
   2140 		ti = &sc->sc_tinfo[i];
   2141 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2142 		    i, ti->cmds, ti->dconns, ti->touts);
   2143 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2144 	}
   2145 }
   2146 #endif
   2147