Home | History | Annotate | Line # | Download | only in dev
mha.c revision 1.28.2.1
      1 /*	$NetBSD: mha.c,v 1.28.2.1 2002/07/15 01:41:06 gehenna 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 	    (void) 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 		memset(acb, 0, 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 		memset(&sc->sc_tinfo, 0, 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 	int i;
    582 	int s;
    583 
    584 	s = splbio();	/* XXX */
    585 
    586 	SPC_TRACE(("[mhaselect(t%d,l%d,cmd:%x)] ", target, lun, *(u_char *)cmd));
    587 
    588 	/* CDB $B$r(B SPC $B$N(B MCS REG $B$K%;%C%H$9$k(B */
    589 	/* Now the command into the FIFO */
    590 	WAIT;
    591 #if 1
    592 	SPC_MISC(("[cmd:"));
    593 	for (i = 0; i < clen; i++)
    594 	  {
    595 	    unsigned c = cmd[i];
    596 	    if (i == 1)
    597 	      c |= lun << 5;
    598 	    SPC_MISC((" %02x", c));
    599 	    sc->sc_pcx[i] = c;
    600 	  }
    601 	SPC_MISC(("], target=%d\n", target));
    602 #else
    603 	memcpy(sc->sc_pcx, cmd, clen);
    604 #endif
    605 	if (NSR & 0x80)
    606 		panic("scsistart: already selected...");
    607 	sc->sc_phase  = COMMAND_PHASE;
    608 
    609 	/* new state ASP_SELECTING */
    610 	sc->sc_state = SPC_SELECTING;
    611 
    612 	SIR = target;
    613 #if 0
    614 	CMR = CMD_SELECT;
    615 #else
    616 	CMR = CMD_SEL_AND_CMD;	/* select & cmd */
    617 #endif
    618 	splx(s);
    619 }
    620 
    621 #if 0
    622 int
    623 mha_reselect(sc, message)
    624 	struct mha_softc *sc;
    625 	u_char message;
    626 {
    627 	u_char selid, target, lun;
    628 	struct acb *acb;
    629 	struct scsipi_periph *periph;
    630 	struct spc_tinfo *ti;
    631 
    632 	/*
    633 	 * The SCSI chip made a snapshot of the data bus while the reselection
    634 	 * was being negotiated.  This enables us to determine which target did
    635 	 * the reselect.
    636 	 */
    637 	selid = sc->sc_selid & ~(1 << sc->sc_id);
    638 	if (selid & (selid - 1)) {
    639 		printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
    640 		    sc->sc_dev.dv_xname, selid);
    641 		SPC_BREAK();
    642 		goto reset;
    643 	}
    644 
    645 	/*
    646 	 * Search wait queue for disconnected cmd
    647 	 * The list should be short, so I haven't bothered with
    648 	 * any more sophisticated structures than a simple
    649 	 * singly linked list.
    650 	 */
    651 	target = ffs(selid) - 1;
    652 	lun = message & 0x07;
    653 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
    654 	     acb = acb->chain.tqe_next) {
    655 		periph = acb->xs->xs_periph;
    656 		if (periph->periph_target == target &&
    657 		    periph->periph_lun == lun)
    658 			break;
    659 	}
    660 	if (acb == NULL) {
    661 		printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
    662 		    sc->sc_dev.dv_xname, target, lun);
    663 		SPC_BREAK();
    664 		goto abort;
    665 	}
    666 
    667 	/* Make this nexus active again. */
    668 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    669 	sc->sc_state = SPC_HASNEXUS;
    670 	sc->sc_nexus = acb;
    671 	ti = &sc->sc_tinfo[target];
    672 	ti->lubusy |= (1 << lun);
    673 	mha_setsync(sc, ti);
    674 
    675 	if (acb->flags & ACB_RESET)
    676 		mha_sched_msgout(sc, SEND_DEV_RESET);
    677 	else if (acb->flags & ACB_ABORTED)
    678 		mha_sched_msgout(sc, SEND_ABORT);
    679 
    680 	/* Do an implicit RESTORE POINTERS. */
    681 	sc->sc_dp = acb->daddr;
    682 	sc->sc_dleft = acb->dleft;
    683 	sc->sc_cp = (u_char *)&acb->cmd;
    684 	sc->sc_cleft = acb->clen;
    685 
    686 	return (0);
    687 
    688 reset:
    689 	mha_sched_msgout(sc, SEND_DEV_RESET);
    690 	return (1);
    691 
    692 abort:
    693 	mha_sched_msgout(sc, SEND_ABORT);
    694 	return (1);
    695 }
    696 #endif
    697 /*
    698  * Start a SCSI-command
    699  * This function is called by the higher level SCSI-driver to queue/run
    700  * SCSI-commands.
    701  */
    702 void
    703 mha_scsi_request(chan, req, arg)
    704 	struct scsipi_channel *chan;
    705 	scsipi_adapter_req_t req;
    706 	void *arg;
    707 {
    708 	struct scsipi_xfer *xs;
    709 	struct scsipi_periph *periph;
    710 	struct mha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    711 	struct acb *acb;
    712 	int s, flags;
    713 
    714 	switch (req) {
    715 	case ADAPTER_REQ_RUN_XFER:
    716 		xs = arg;
    717 		periph = xs->xs_periph;
    718 
    719 		SPC_TRACE(("[mha_scsi_cmd] "));
    720 		SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    721 		    periph->periph_target));
    722 
    723 		flags = xs->xs_control;
    724 
    725 		/* Get a mha command block */
    726 		s = splbio();
    727 		acb = sc->free_list.tqh_first;
    728 		if (acb) {
    729 			TAILQ_REMOVE(&sc->free_list, acb, chain);
    730 			ACB_SETQ(acb, ACB_QNONE);
    731 		}
    732 
    733 		if (acb == NULL) {
    734 			xs->error = XS_RESOURCE_SHORTAGE;
    735 			scsipi_done(xs);
    736 			splx(s);
    737 			return;
    738 		}
    739 		splx(s);
    740 
    741 		/* Initialize acb */
    742 		acb->xs = xs;
    743 		memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
    744 		acb->clen = xs->cmdlen;
    745 		acb->daddr = xs->data;
    746 		acb->dleft = xs->datalen;
    747 		acb->stat = 0;
    748 
    749 		s = splbio();
    750 		ACB_SETQ(acb, ACB_QREADY);
    751 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    752 #if 1
    753 		callout_reset(&acb->xs->xs_callout,
    754 		    mstohz(xs->timeout), mha_timeout, acb);
    755 #endif
    756 
    757 		/*
    758 		 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
    759 		 */
    760 		if (sc->sc_state == SPC_IDLE)
    761 			mha_sched(sc);
    762 
    763 		splx(s);
    764 
    765 		if (flags & XS_CTL_POLL) {
    766 			/* Not allowed to use interrupts, use polling instead */
    767 			mha_poll(sc, acb);
    768 		}
    769 
    770 		SPC_MISC(("SUCCESSFULLY_QUEUED"));
    771 		return;
    772 
    773 	case ADAPTER_REQ_GROW_RESOURCES:
    774 		/* XXX Not supported. */
    775 		return;
    776 
    777 	case ADAPTER_REQ_SET_XFER_MODE:
    778 		/* XXX Not supported. */
    779 		return;
    780 	}
    781 }
    782 
    783 /*
    784  * Adjust transfer size in buffer structure
    785  */
    786 void
    787 mha_minphys(bp)
    788 	struct buf *bp;
    789 {
    790 
    791 	SPC_TRACE(("mha_minphys  "));
    792 	minphys(bp);
    793 }
    794 
    795 /*
    796  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    797  */
    798 void
    799 mha_poll(sc, acb)
    800 	struct mha_softc *sc;
    801 	struct acb *acb;
    802 {
    803 	struct scsipi_xfer *xs = acb->xs;
    804 	int count = xs->timeout * 100;
    805 	int s = splbio();
    806 
    807 	SPC_TRACE(("[mha_poll] "));
    808 
    809 	while (count) {
    810 		/*
    811 		 * If we had interrupts enabled, would we
    812 		 * have got an interrupt?
    813 		 */
    814 		if (SSR & SS_IREQUEST)
    815 			mhaintr(sc);
    816 		if ((xs->xs_status & XS_STS_DONE) != 0)
    817 			break;
    818 		DELAY(10);
    819 #if 1
    820 		if (sc->sc_state == SPC_IDLE) {
    821 			SPC_TRACE(("[mha_poll: rescheduling] "));
    822 			mha_sched(sc);
    823 		}
    824 #endif
    825 		count--;
    826 	}
    827 
    828 	if (count == 0) {
    829 		SPC_MISC(("mha_poll: timeout"));
    830 		mha_timeout((caddr_t)acb);
    831 	}
    832 	splx(s);
    833 	scsipi_done(xs);
    834 }
    835 
    836 /*
    838  * LOW LEVEL SCSI UTILITIES
    839  */
    840 
    841 /*
    842  * Set synchronous transfer offset and period.
    843  */
    844 inline void
    845 mha_setsync(sc, ti)
    846 	struct mha_softc *sc;
    847 	struct spc_tinfo *ti;
    848 {
    849 }
    850 
    851 
    852 /*
    854  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
    855  * handler so that we may call it from mha_scsi_cmd and mha_done.  This may
    856  * save us an unecessary interrupt just to get things going.  Should only be
    857  * called when state == SPC_IDLE and at bio pl.
    858  */
    859 void
    860 mha_sched(sc)
    861 	register struct mha_softc *sc;
    862 {
    863 	struct scsipi_periph *periph;
    864 	struct acb *acb;
    865 	int t;
    866 
    867 	SPC_TRACE(("[mha_sched] "));
    868 	if (sc->sc_state != SPC_IDLE)
    869 		panic("mha_sched: not IDLE (state=%d)", sc->sc_state);
    870 
    871 	if (sc->sc_flags & SPC_ABORTING)
    872 		return;
    873 
    874 	/*
    875 	 * Find first acb in ready queue that is for a target/lunit
    876 	 * combinations that is not busy.
    877 	 */
    878 	for (acb = sc->ready_list.tqh_first; acb ; acb = acb->chain.tqe_next) {
    879 		struct spc_tinfo *ti;
    880 		periph = acb->xs->xs_periph;
    881 		t = periph->periph_target;
    882 		ti = &sc->sc_tinfo[t];
    883 		if (!(ti->lubusy & (1 << periph->periph_lun))) {
    884 			if ((acb->flags & ACB_QBITS) != ACB_QREADY)
    885 				panic("mha: busy entry on ready list");
    886 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    887 			ACB_SETQ(acb, ACB_QNONE);
    888 			sc->sc_nexus = acb;
    889 			sc->sc_flags = 0;
    890 			sc->sc_prevphase = INVALID_PHASE;
    891 			sc->sc_dp = acb->daddr;
    892 			sc->sc_dleft = acb->dleft;
    893 			ti->lubusy |= (1<<periph->periph_lun);
    894 			mhaselect(sc, t, periph->periph_lun,
    895 				     (u_char *)&acb->cmd, acb->clen);
    896 			break;
    897 		} else {
    898 			SPC_MISC(("%d:%d busy\n",
    899 			    periph->periph_target,
    900 			    periph->periph_lun));
    901 		}
    902 	}
    903 }
    904 
    905 /*
    907  * POST PROCESSING OF SCSI_CMD (usually current)
    908  */
    909 void
    910 mha_done(sc, acb)
    911 	struct mha_softc *sc;
    912 	struct acb *acb;
    913 {
    914 	struct scsipi_xfer *xs = acb->xs;
    915 	struct scsipi_periph *periph = xs->xs_periph;
    916 	struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
    917 
    918 	SPC_TRACE(("[mha_done(error:%x)] ", xs->error));
    919 
    920 #if 1
    921 	callout_stop(&acb->xs->xs_callout);
    922 #endif
    923 
    924 	/*
    925 	 * Now, if we've come here with no error code, i.e. we've kept the
    926 	 * initial XS_NOERROR, and the status code signals that we should
    927 	 * check sense, we'll need to set up a request sense cmd block and
    928 	 * push the command back into the ready queue *before* any other
    929 	 * commands for this target/lunit, else we lose the sense info.
    930 	 * We don't support chk sense conditions for the request sense cmd.
    931 	 */
    932 	if (xs->error == XS_NOERROR) {
    933 		if ((acb->flags & ACB_ABORTED) != 0) {
    934 			xs->error = XS_TIMEOUT;
    935 		} else if (acb->flags & ACB_CHKSENSE) {
    936 			xs->error = XS_SENSE;
    937 		} else {
    938 			xs->status = acb->stat & ST_MASK;
    939 			switch (xs->status) {
    940 			case SCSI_CHECK:
    941 				xs->resid = acb->dleft;
    942 				/* FALLTHOUGH */
    943 			case SCSI_BUSY:
    944 				xs->error = XS_BUSY;
    945 				break;
    946 			case SCSI_OK:
    947 				xs->resid = acb->dleft;
    948 				break;
    949 			default:
    950 				xs->error = XS_DRIVER_STUFFUP;
    951 #if SPC_DEBUG
    952 				printf("%s: mha_done: bad stat 0x%x\n",
    953 					sc->sc_dev.dv_xname, acb->stat);
    954 #endif
    955 				break;
    956 			}
    957 		}
    958 	}
    959 
    960 #if SPC_DEBUG
    961 	if ((mha_debug & SPC_SHOWMISC) != 0) {
    962 		if (xs->resid != 0)
    963 			printf("resid=%d ", xs->resid);
    964 		if (xs->error == XS_SENSE)
    965 			printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
    966 		else
    967 			printf("error=%d\n", xs->error);
    968 	}
    969 #endif
    970 
    971 	/*
    972 	 * Remove the ACB from whatever queue it's on.
    973 	 */
    974 	switch (acb->flags & ACB_QBITS) {
    975 	case ACB_QNONE:
    976 		if (acb != sc->sc_nexus) {
    977 			panic("%s: floating acb", sc->sc_dev.dv_xname);
    978 		}
    979 		sc->sc_nexus = NULL;
    980 		sc->sc_state = SPC_IDLE;
    981 		ti->lubusy &= ~(1<<periph->periph_lun);
    982 		mha_sched(sc);
    983 		break;
    984 	case ACB_QREADY:
    985 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    986 		break;
    987 	case ACB_QNEXUS:
    988 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    989 		ti->lubusy &= ~(1<<periph->periph_lun);
    990 		break;
    991 	case ACB_QFREE:
    992 		panic("%s: dequeue: busy acb on free list",
    993 			sc->sc_dev.dv_xname);
    994 		break;
    995 	default:
    996 		panic("%s: dequeue: unknown queue %d",
    997 			sc->sc_dev.dv_xname, acb->flags & ACB_QBITS);
    998 	}
    999 
   1000 	/* Put it on the free list, and clear flags. */
   1001 #if 0
   1002 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
   1003 	acb->flags = ACB_QFREE;
   1004 #else
   1005 	mha_free_acb(sc, acb, xs->xs_control);
   1006 #endif
   1007 
   1008 	ti->cmds++;
   1009 	scsipi_done(xs);
   1010 }
   1011 
   1012 void
   1013 mha_dequeue(sc, acb)
   1014 	struct mha_softc *sc;
   1015 	struct acb *acb;
   1016 {
   1017 
   1018 	if (acb->flags & ACB_QNEXUS) {
   1019 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1020 	} else {
   1021 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
   1022 	}
   1023 }
   1024 
   1025 /*
   1027  * INTERRUPT/PROTOCOL ENGINE
   1028  */
   1029 
   1030 /*
   1031  * Schedule an outgoing message by prioritizing it, and asserting
   1032  * attention on the bus. We can only do this when we are the initiator
   1033  * else there will be an illegal command interrupt.
   1034  */
   1035 #define mha_sched_msgout(m) \
   1036 	do {				\
   1037 		SPC_MISC(("mha_sched_msgout %d ", m)); \
   1038 		CMR = CMD_SET_ATN;	\
   1039 		sc->sc_msgpriq |= (m);	\
   1040 	} while (0)
   1041 
   1042 /*
   1043  * Precondition:
   1044  * The SCSI bus is already in the MSGI phase and there is a message byte
   1045  * on the bus, along with an asserted REQ signal.
   1046  */
   1047 void
   1048 mha_msgin(sc)
   1049 	register struct mha_softc *sc;
   1050 {
   1051 	register int v;
   1052 
   1053 	SPC_TRACE(("[mha_msgin(curmsglen:%d)] ", sc->sc_imlen));
   1054 
   1055 	/*
   1056 	 * Prepare for a new message.  A message should (according
   1057 	 * to the SCSI standard) be transmitted in one single
   1058 	 * MESSAGE_IN_PHASE. If we have been in some other phase,
   1059 	 * then this is a new message.
   1060 	 */
   1061 	if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
   1062 		sc->sc_flags &= ~SPC_DROP_MSGI;
   1063 		sc->sc_imlen = 0;
   1064 	}
   1065 
   1066 	WAIT;
   1067 
   1068 	v = MBR;	/* modified byte */
   1069 	v = sc->sc_pcx[0];
   1070 
   1071 	sc->sc_imess[sc->sc_imlen] = v;
   1072 
   1073 	/*
   1074 	 * If we're going to reject the message, don't bother storing
   1075 	 * the incoming bytes.  But still, we need to ACK them.
   1076 	 */
   1077 
   1078 	if ((sc->sc_flags & SPC_DROP_MSGI)) {
   1079 		CMR = CMD_SET_ATN;
   1080 /*		ESPCMD(sc, ESPCMD_MSGOK);*/
   1081 		printf("<dropping msg byte %x>",
   1082 			sc->sc_imess[sc->sc_imlen]);
   1083 		return;
   1084 	}
   1085 
   1086 	if (sc->sc_imlen >= SPC_MAX_MSG_LEN) {
   1087 		mha_sched_msgout(SEND_REJECT);
   1088 		sc->sc_flags |= SPC_DROP_MSGI;
   1089 	} else {
   1090 		sc->sc_imlen++;
   1091 		/*
   1092 		 * This testing is suboptimal, but most
   1093 		 * messages will be of the one byte variety, so
   1094 		 * it should not effect performance
   1095 		 * significantly.
   1096 		 */
   1097 		if (sc->sc_imlen == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
   1098 			goto gotit;
   1099 		if (sc->sc_imlen == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
   1100 			goto gotit;
   1101 		if (sc->sc_imlen >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
   1102 		    sc->sc_imlen == sc->sc_imess[1] + 2)
   1103 			goto gotit;
   1104 	}
   1105 #if 0
   1106 	/* Ack what we have so far */
   1107 	ESPCMD(sc, ESPCMD_MSGOK);
   1108 #endif
   1109 	return;
   1110 
   1111 gotit:
   1112 	SPC_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
   1113 	/*
   1114 	 * Now we should have a complete message (1 byte, 2 byte
   1115 	 * and moderately long extended messages).  We only handle
   1116 	 * extended messages which total length is shorter than
   1117 	 * SPC_MAX_MSG_LEN.  Longer messages will be amputated.
   1118 	 */
   1119 	if (sc->sc_state == SPC_HASNEXUS) {
   1120 		struct acb *acb = sc->sc_nexus;
   1121 		struct spc_tinfo *ti =
   1122 			&sc->sc_tinfo[acb->xs->xs_periph->periph_target];
   1123 
   1124 		switch (sc->sc_imess[0]) {
   1125 		case MSG_CMDCOMPLETE:
   1126 			SPC_MSGS(("cmdcomplete "));
   1127 			if (sc->sc_dleft < 0) {
   1128 				struct scsipi_periph *periph = acb->xs->xs_periph;
   1129 				printf("mha: %d extra bytes from %d:%d\n",
   1130 					-sc->sc_dleft,
   1131 					periph->periph_target,
   1132 				        periph->periph_lun);
   1133 				sc->sc_dleft = 0;
   1134 			}
   1135 			acb->xs->resid = acb->dleft = sc->sc_dleft;
   1136 			sc->sc_flags |= SPC_BUSFREE_OK;
   1137 			break;
   1138 
   1139 		case MSG_MESSAGE_REJECT:
   1140 #if SPC_DEBUG
   1141 			if (mha_debug & SPC_SHOWMSGS)
   1142 				printf("%s: our msg rejected by target\n",
   1143 					sc->sc_dev.dv_xname);
   1144 #endif
   1145 #if 1 /* XXX - must remember last message */
   1146 			scsipi_printaddr(acb->xs->xs_periph);
   1147 			printf("MSG_MESSAGE_REJECT>>");
   1148 #endif
   1149 			if (sc->sc_flags & SPC_SYNCHNEGO) {
   1150 				ti->period = ti->offset = 0;
   1151 				sc->sc_flags &= ~SPC_SYNCHNEGO;
   1152 				ti->flags &= ~T_NEGOTIATE;
   1153 			}
   1154 			/* Not all targets understand INITIATOR_DETECTED_ERR */
   1155 			if (sc->sc_msgout == SEND_INIT_DET_ERR)
   1156 				mha_sched_msgout(SEND_ABORT);
   1157 			break;
   1158 		case MSG_NOOP:
   1159 			SPC_MSGS(("noop "));
   1160 			break;
   1161 		case MSG_DISCONNECT:
   1162 			SPC_MSGS(("disconnect "));
   1163 			ti->dconns++;
   1164 			sc->sc_flags |= SPC_DISCON;
   1165 			sc->sc_flags |= SPC_BUSFREE_OK;
   1166 			if ((acb->xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
   1167 				break;
   1168 			/*FALLTHROUGH*/
   1169 		case MSG_SAVEDATAPOINTER:
   1170 			SPC_MSGS(("save datapointer "));
   1171 			acb->dleft = sc->sc_dleft;
   1172 			acb->daddr = sc->sc_dp;
   1173 			break;
   1174 		case MSG_RESTOREPOINTERS:
   1175 			SPC_MSGS(("restore datapointer "));
   1176 			if (!acb) {
   1177 				mha_sched_msgout(SEND_ABORT);
   1178 				printf("%s: no DATAPOINTERs to restore\n",
   1179 				    sc->sc_dev.dv_xname);
   1180 				break;
   1181 			}
   1182 			sc->sc_dp = acb->daddr;
   1183 			sc->sc_dleft = acb->dleft;
   1184 			break;
   1185 		case MSG_PARITY_ERROR:
   1186 			printf("%s:target%d: MSG_PARITY_ERROR\n",
   1187 				sc->sc_dev.dv_xname,
   1188 				acb->xs->xs_periph->periph_target);
   1189 			break;
   1190 		case MSG_EXTENDED:
   1191 			SPC_MSGS(("extended(%x) ", sc->sc_imess[2]));
   1192 			switch (sc->sc_imess[2]) {
   1193 			case MSG_EXT_SDTR:
   1194 				SPC_MSGS(("SDTR period %d, offset %d ",
   1195 					sc->sc_imess[3], sc->sc_imess[4]));
   1196 				ti->period = sc->sc_imess[3];
   1197 				ti->offset = sc->sc_imess[4];
   1198 				if (sc->sc_minsync == 0) {
   1199 					/* We won't do synch */
   1200 					ti->offset = 0;
   1201 					mha_sched_msgout(SEND_SDTR);
   1202 				} else if (ti->offset == 0) {
   1203 					printf("%s:%d: async\n", "mha",
   1204 						acb->xs->xs_periph->periph_target);
   1205 					ti->offset = 0;
   1206 					sc->sc_flags &= ~SPC_SYNCHNEGO;
   1207 				} else if (ti->period > 124) {
   1208 					printf("%s:%d: async\n", "mha",
   1209 						acb->xs->xs_periph->periph_target);
   1210 					ti->offset = 0;
   1211 					mha_sched_msgout(SEND_SDTR);
   1212 				} else {
   1213 #if 0
   1214 					int p;
   1215 					p =  mha_stp2cpb(sc, ti->period);
   1216 					ti->period = mha_cpb2stp(sc, p);
   1217 #endif
   1218 
   1219 #if SPC_DEBUG
   1220 					scsipi_printaddr(acb->xs->xs_periph);
   1221 #endif
   1222 					if ((sc->sc_flags&SPC_SYNCHNEGO) == 0) {
   1223 						/* Target initiated negotiation */
   1224 						if (ti->flags & T_SYNCMODE) {
   1225 						    ti->flags &= ~T_SYNCMODE;
   1226 #if SPC_DEBUG
   1227 						    printf("renegotiated ");
   1228 #endif
   1229 						}
   1230 						TMR=TM_ASYNC;
   1231 						/* Clamp to our maxima */
   1232 						if (ti->period < sc->sc_minsync)
   1233 							ti->period = sc->sc_minsync;
   1234 						if (ti->offset > 15)
   1235 							ti->offset = 15;
   1236 						mha_sched_msgout(SEND_SDTR);
   1237 					} else {
   1238 						/* we are sync */
   1239 						sc->sc_flags &= ~SPC_SYNCHNEGO;
   1240 						TMR = TM_SYNC;
   1241 						ti->flags |= T_SYNCMODE;
   1242 					}
   1243 				}
   1244 				ti->flags &= ~T_NEGOTIATE;
   1245 				break;
   1246 			default: /* Extended messages we don't handle */
   1247 				CMR = CMD_SET_ATN; /* XXX? */
   1248 				break;
   1249 			}
   1250 			break;
   1251 		default:
   1252 			SPC_MSGS(("ident "));
   1253 			/* thanks for that ident... */
   1254 			if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1255 				SPC_MISC(("unknown "));
   1256 printf("%s: unimplemented message: %d\n", sc->sc_dev.dv_xname, sc->sc_imess[0]);
   1257 				CMR = CMD_SET_ATN; /* XXX? */
   1258 			}
   1259 			break;
   1260 		}
   1261 	} else if (sc->sc_state == SPC_RESELECTED) {
   1262 		struct scsipi_periph *periph = NULL;
   1263 		struct acb *acb;
   1264 		struct spc_tinfo *ti;
   1265 		u_char lunit;
   1266 
   1267 		if (MSG_ISIDENTIFY(sc->sc_imess[0])) { 	/* Identify? */
   1268 			SPC_MISC(("searching "));
   1269 			/*
   1270 			 * Search wait queue for disconnected cmd
   1271 			 * The list should be short, so I haven't bothered with
   1272 			 * any more sophisticated structures than a simple
   1273 			 * singly linked list.
   1274 			 */
   1275 			lunit = sc->sc_imess[0] & 0x07;
   1276 			for (acb = sc->nexus_list.tqh_first; acb;
   1277 			     acb = acb->chain.tqe_next) {
   1278 				periph = acb->xs->xs_periph;
   1279 				if (periph->periph_lun == lunit &&
   1280 				    sc->sc_selid == (1<<periph->periph_target)) {
   1281 					TAILQ_REMOVE(&sc->nexus_list, acb,
   1282 					    chain);
   1283 					ACB_SETQ(acb, ACB_QNONE);
   1284 					break;
   1285 				}
   1286 			}
   1287 
   1288 			if (!acb) {		/* Invalid reselection! */
   1289 				mha_sched_msgout(SEND_ABORT);
   1290 				printf("mha: invalid reselect (idbit=0x%2x)\n",
   1291 				    sc->sc_selid);
   1292 			} else {		/* Reestablish nexus */
   1293 				/*
   1294 				 * Setup driver data structures and
   1295 				 * do an implicit RESTORE POINTERS
   1296 				 */
   1297 				ti = &sc->sc_tinfo[periph->periph_target];
   1298 				sc->sc_nexus = acb;
   1299 				sc->sc_dp = acb->daddr;
   1300 				sc->sc_dleft = acb->dleft;
   1301 				sc->sc_tinfo[periph->periph_target].lubusy
   1302 					|= (1<<periph->periph_lun);
   1303 				if (ti->flags & T_SYNCMODE) {
   1304 					TMR = TM_SYNC;	/* XXX */
   1305 				} else {
   1306 					TMR = TM_ASYNC;
   1307 				}
   1308 				SPC_MISC(("... found acb"));
   1309 				sc->sc_state = SPC_HASNEXUS;
   1310 			}
   1311 		} else {
   1312 			printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
   1313 			    sc->sc_dev.dv_xname, sc->sc_selid);
   1314 			mha_sched_msgout(SEND_DEV_RESET);
   1315 		}
   1316 	} else { /* Neither SPC_HASNEXUS nor SPC_RESELECTED! */
   1317 		printf("%s: unexpected message in; will send DEV_RESET\n",
   1318 		    sc->sc_dev.dv_xname);
   1319 		mha_sched_msgout(SEND_DEV_RESET);
   1320 	}
   1321 
   1322 	/* Ack last message byte */
   1323 #if 0
   1324 	ESPCMD(sc, ESPCMD_MSGOK);
   1325 #endif
   1326 
   1327 	/* Done, reset message pointer. */
   1328 	sc->sc_flags &= ~SPC_DROP_MSGI;
   1329 	sc->sc_imlen = 0;
   1330 }
   1331 
   1332 /*
   1333  * Send the highest priority, scheduled message.
   1334  */
   1335 void
   1336 mha_msgout(sc)
   1337 	register struct mha_softc *sc;
   1338 {
   1339 #if (SPC_USE_SYNCHRONOUS || SPC_USE_WIDE)
   1340 	struct spc_tinfo *ti;
   1341 #endif
   1342 	int n;
   1343 
   1344 	SPC_TRACE(("mha_msgout  "));
   1345 
   1346 	if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   1347 		if (sc->sc_omp == sc->sc_omess) {
   1348 			/*
   1349 			 * This is a retransmission.
   1350 			 *
   1351 			 * We get here if the target stayed in MESSAGE OUT
   1352 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1353 			 * that all of the previously transmitted messages must
   1354 			 * be sent again, in the same order.  Therefore, we
   1355 			 * requeue all the previously transmitted messages, and
   1356 			 * start again from the top.  Our simple priority
   1357 			 * scheme keeps the messages in the right order.
   1358 			 */
   1359 			SPC_MISC(("retransmitting  "));
   1360 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1361 			/*
   1362 			 * Set ATN.  If we're just sending a trivial 1-byte
   1363 			 * message, we'll clear ATN later on anyway.
   1364 			 */
   1365 			CMR = CMD_SET_ATN; /* XXX? */
   1366 		} else {
   1367 			/* This is a continuation of the previous message. */
   1368 			n = sc->sc_omp - sc->sc_omess;
   1369 			goto nextbyte;
   1370 		}
   1371 	}
   1372 
   1373 	/* No messages transmitted so far. */
   1374 	sc->sc_msgoutq = 0;
   1375 	sc->sc_lastmsg = 0;
   1376 
   1377 nextmsg:
   1378 	/* Pick up highest priority message. */
   1379 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1380 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1381 	sc->sc_msgoutq |= sc->sc_currmsg;
   1382 
   1383 	/* Build the outgoing message data. */
   1384 	switch (sc->sc_currmsg) {
   1385 	case SEND_IDENTIFY:
   1386 		SPC_ASSERT(sc->sc_nexus != NULL);
   1387 		sc->sc_omess[0] =
   1388 		    MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1);
   1389 		n = 1;
   1390 		break;
   1391 
   1392 #if SPC_USE_SYNCHRONOUS
   1393 	case SEND_SDTR:
   1394 		SPC_ASSERT(sc->sc_nexus != NULL);
   1395 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
   1396 		sc->sc_omess[4] = MSG_EXTENDED;
   1397 		sc->sc_omess[3] = 3;
   1398 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1399 		sc->sc_omess[1] = ti->period >> 2;
   1400 		sc->sc_omess[0] = ti->offset;
   1401 		n = 5;
   1402 		break;
   1403 #endif
   1404 
   1405 #if SPC_USE_WIDE
   1406 	case SEND_WDTR:
   1407 		SPC_ASSERT(sc->sc_nexus != NULL);
   1408 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
   1409 		sc->sc_omess[3] = MSG_EXTENDED;
   1410 		sc->sc_omess[2] = 2;
   1411 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1412 		sc->sc_omess[0] = ti->width;
   1413 		n = 4;
   1414 		break;
   1415 #endif
   1416 
   1417 	case SEND_DEV_RESET:
   1418 		sc->sc_flags |= SPC_ABORTING;
   1419 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1420 		n = 1;
   1421 		break;
   1422 
   1423 	case SEND_REJECT:
   1424 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1425 		n = 1;
   1426 		break;
   1427 
   1428 	case SEND_PARITY_ERROR:
   1429 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1430 		n = 1;
   1431 		break;
   1432 
   1433 	case SEND_INIT_DET_ERR:
   1434 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1435 		n = 1;
   1436 		break;
   1437 
   1438 	case SEND_ABORT:
   1439 		sc->sc_flags |= SPC_ABORTING;
   1440 		sc->sc_omess[0] = MSG_ABORT;
   1441 		n = 1;
   1442 		break;
   1443 
   1444 	default:
   1445 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1446 		    sc->sc_dev.dv_xname);
   1447 		SPC_BREAK();
   1448 		sc->sc_omess[0] = MSG_NOOP;
   1449 		n = 1;
   1450 		break;
   1451 	}
   1452 	sc->sc_omp = &sc->sc_omess[n];
   1453 
   1454 nextbyte:
   1455 	/* Send message bytes. */
   1456 	/* send TRANSFER command. */
   1457 	sc->sc_ps[3] = 1;
   1458 	sc->sc_ps[4] = n >> 8;
   1459 	sc->sc_pc[10] = n;
   1460 	sc->sc_ps[-1] = 0x000F;	/* burst */
   1461 	asm volatile ("nop");
   1462 	CMR = CMD_SEND_FROM_DMA;	/* send from DMA */
   1463 	for (;;) {
   1464 		if ((SSR & SS_BUSY) != 0)
   1465 			break;
   1466 		if (SSR & SS_IREQUEST)
   1467 			goto out;
   1468 	}
   1469 	for (;;) {
   1470 #if 0
   1471 		for (;;) {
   1472 			if ((PSNS & PSNS_REQ) != 0)
   1473 				break;
   1474 			/* Wait for REQINIT.  XXX Need timeout. */
   1475 		}
   1476 #endif
   1477 		if (SSR & SS_IREQUEST) {
   1478 			/*
   1479 			 * Target left MESSAGE OUT, possibly to reject
   1480 			 * our message.
   1481 			 *
   1482 			 * If this is the last message being sent, then we
   1483 			 * deassert ATN, since either the target is going to
   1484 			 * ignore this message, or it's going to ask for a
   1485 			 * retransmission via MESSAGE PARITY ERROR (in which
   1486 			 * case we reassert ATN anyway).
   1487 			 */
   1488 #if 0
   1489 			if (sc->sc_msgpriq == 0)
   1490 				CMR = CMD_RESET_ATN;
   1491 #endif
   1492 			goto out;
   1493 		}
   1494 
   1495 #if 0
   1496 		/* Clear ATN before last byte if this is the last message. */
   1497 		if (n == 1 && sc->sc_msgpriq == 0)
   1498 			CMR = CMD_RESET_ATN;
   1499 #endif
   1500 
   1501 		while ((SSR & SS_DREG_FULL) != 0)
   1502 			;
   1503 		/* Send message byte. */
   1504 		sc->sc_pc[0] = *--sc->sc_omp;
   1505 		--n;
   1506 		/* Keep track of the last message we've sent any bytes of. */
   1507 		sc->sc_lastmsg = sc->sc_currmsg;
   1508 
   1509 		if (n == 0)
   1510 			break;
   1511 	}
   1512 
   1513 	/* We get here only if the entire message has been transmitted. */
   1514 	if (sc->sc_msgpriq != 0) {
   1515 		/* There are more outgoing messages. */
   1516 		goto nextmsg;
   1517 	}
   1518 
   1519 	/*
   1520 	 * The last message has been transmitted.  We need to remember the last
   1521 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1522 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1523 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1524 	 * request a retransmit).
   1525 	 */
   1526 
   1527 out:
   1528 	/* Disable REQ/ACK protocol. */
   1529 	return;
   1530 }
   1531 
   1532 
   1533 /***************************************************************
   1535  *
   1536  *	datain/dataout
   1537  *
   1538  */
   1539 
   1540 int
   1541 mha_datain_pio(sc, p, n)
   1542 	register struct mha_softc *sc;
   1543 	u_char *p;
   1544 	int n;
   1545 {
   1546 	u_short d;
   1547 	int a;
   1548 	int total_n = n;
   1549 
   1550 	SPC_TRACE(("[mha_datain_pio(%p,%d)", p, n));
   1551 
   1552 	WAIT;
   1553 	sc->sc_ps[3] = 1;
   1554 	sc->sc_ps[4] = n >> 8;
   1555 	sc->sc_pc[10] = n;
   1556 	/* $BHa$7$-%=%U%HE>Aw(B */
   1557 	CMR = CMD_RECEIVE_TO_MPU;
   1558 	for (;;) {
   1559 		a = SSR;
   1560 		if (a & 0x04) {
   1561 			d = sc->sc_ps[0];
   1562 			*p++ = d >> 8;
   1563 			if (--n > 0) {
   1564 				*p++ = d;
   1565 				--n;
   1566 			}
   1567 			a = SSR;
   1568 		}
   1569 		if (a & 0x40)
   1570 			continue;
   1571 		if (a & 0x80)
   1572 			break;
   1573 	}
   1574 	SPC_TRACE(("...%d resd]", n));
   1575 	return total_n - n;
   1576 }
   1577 
   1578 int
   1579 mha_dataout_pio(sc, p, n)
   1580 	register struct mha_softc *sc;
   1581 	u_char *p;
   1582 	int n;
   1583 {
   1584 	u_short d;
   1585 	int a;
   1586 	int total_n = n;
   1587 
   1588 	SPC_TRACE(("[mha_dataout_pio(%p,%d)", p, n));
   1589 
   1590 	WAIT;
   1591 	sc->sc_ps[3] = 1;
   1592 	sc->sc_ps[4] = n >> 8;
   1593 	sc->sc_pc[10] = n;
   1594 	/* $BHa$7$-%=%U%HE>Aw(B */
   1595 	CMR = CMD_SEND_FROM_MPU;
   1596 	for (;;) {
   1597 		a = SSR;
   1598 		if (a & 0x04) {
   1599 			d = *p++ << 8;
   1600 			if (--n > 0) {
   1601 				d |= *p++;
   1602 				--n;
   1603 			}
   1604 			sc->sc_ps[0] = d;
   1605 			a = SSR;
   1606 		}
   1607 		if (a & 0x40)
   1608 			continue;
   1609 		if (a & 0x80)
   1610 			break;
   1611 	}
   1612 	SPC_TRACE(("...%d resd]", n));
   1613 	return total_n - n;
   1614 }
   1615 
   1616 static int
   1617 mha_dataio_dma(dw, cw, sc, p, n)
   1618 	int dw;		/* DMA word */
   1619 	int cw;		/* CMR word */
   1620 	register struct mha_softc *sc;
   1621 	u_char *p;
   1622 	int n;
   1623 {
   1624   char *paddr;
   1625 
   1626   if (n > MAXBSIZE)
   1627     panic("transfer size exceeds MAXBSIZE");
   1628   if (sc->sc_dmasize > 0)
   1629     panic("DMA request while another DMA transfer is in pregress");
   1630 
   1631   if (cw == CMD_SEND_FROM_DMA) {
   1632     memcpy(sc->sc_dmabuf, p, n);
   1633     bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREWRITE);
   1634   } else {
   1635     bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREREAD);
   1636   }
   1637   sc->sc_p = p;
   1638   sc->sc_dmasize = n;
   1639 
   1640   paddr = (char *)sc->sc_dmaseg[0].ds_addr;
   1641 #if MHA_DMA_SHORT_BUS_CYCLE == 1
   1642   if ((*(int *)&IODEVbase->io_sram[0xac]) & (1 << ((paddr_t)paddr >> 19)))
   1643     dw &= ~(1 << 3);
   1644 #endif
   1645   sc->sc_pc[0x80 + (((long)paddr >> 16) & 0xFF)] = 0;
   1646   sc->sc_pc[0x180 + (((long)paddr >> 8) & 0xFF)] = 0;
   1647   sc->sc_pc[0x280 + (((long)paddr >> 0) & 0xFF)] = 0;
   1648   WAIT;
   1649   sc->sc_ps[3] = 1;
   1650   sc->sc_ps[4] = n >> 8;
   1651   sc->sc_pc[10] = n;
   1652   /* DMA $BE>Aw@)8f$O0J2<$NDL$j!#(B
   1653      3 ... short bus cycle
   1654      2 ... MAXIMUM XFER.
   1655      1 ... BURST XFER.
   1656      0 ... R/W */
   1657   sc->sc_ps[-1] = dw;	/* burst */
   1658   asm volatile ("nop");
   1659   CMR = cw;	/* receive to DMA */
   1660   return n;
   1661 }
   1662 int
   1663 mha_dataout(sc, p, n)
   1664 	register struct mha_softc *sc;
   1665 	u_char *p;
   1666 	int n;
   1667 {
   1668   if (n == 0)
   1669     return n;
   1670 
   1671   if (n & 1)
   1672     return mha_dataout_pio(sc, p, n);
   1673   return mha_dataio_dma(MHA_DMA_DATAOUT, CMD_SEND_FROM_DMA, sc, p, n);
   1674 }
   1675 
   1676 int
   1678 mha_datain(sc, p, n)
   1679 	register struct mha_softc *sc;
   1680 	u_char *p;
   1681 	int n;
   1682 {
   1683   register struct acb *acb = sc->sc_nexus;
   1684 
   1685   if (n == 0)
   1686     return n;
   1687   if (acb->cmd.opcode == REQUEST_SENSE || (n & 1))
   1688     return mha_datain_pio(sc, p, n);
   1689   return mha_dataio_dma(MHA_DMA_DATAIN, CMD_RECEIVE_TO_DMA, sc, p, n);
   1690 }
   1691 
   1692 
   1694 /*
   1695  * Catch an interrupt from the adaptor
   1696  */
   1697 /*
   1698  * This is the workhorse routine of the driver.
   1699  * Deficiencies (for now):
   1700  * 1) always uses programmed I/O
   1701  */
   1702 int
   1703 mhaintr(arg)
   1704 	void *arg;
   1705 {
   1706 	struct mha_softc *sc = arg;
   1707 #if 0
   1708 	u_char ints;
   1709 #endif
   1710 	struct acb *acb;
   1711 	u_char ph;
   1712 	u_short r;
   1713 	int n;
   1714 
   1715 #if 1	/* XXX called during attach? */
   1716 	if (tmpsc != NULL) {
   1717 		SPC_MISC(("[%p %p]\n", mha_cd.cd_devs, sc));
   1718 		sc = tmpsc;
   1719 	} else {
   1720 #endif
   1721 
   1722 #if 1	/* XXX */
   1723 	}
   1724 #endif
   1725 
   1726 #if 0
   1727 	/*
   1728 	 * $B3d$j9~$_6X;_$K$9$k(B
   1729 	 */
   1730 	SCTL &= ~SCTL_INTR_ENAB;
   1731 #endif
   1732 
   1733 	SPC_TRACE(("[mhaintr]"));
   1734 
   1735 	/*
   1736 	 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
   1737 	 */
   1738 	/*
   1739 	 * First check for abnormal conditions, such as reset.
   1740 	 */
   1741 #if 0
   1742 #if 1 /* XXX? */
   1743 	while (((ints = SSR) & SS_IREQUEST) == 0)
   1744 		delay(1);
   1745 	SPC_MISC(("ints = 0x%x  ", ints));
   1746 #else /* usually? */
   1747 	ints = SSR;
   1748 #endif
   1749 #endif
   1750 	while (SSR & SS_IREQUEST) {
   1751 		acb = sc->sc_nexus;
   1752 		r = ISCSR;
   1753 		SPC_MISC(("[r=0x%x]", r));
   1754 		switch (r >> 8) {
   1755 		default:
   1756 			printf("[addr=%p\n"
   1757 			       "result=0x%x\n"
   1758 			       "cmd=0x%x\n"
   1759 			       "ph=0x%x(ought to be %d)]\n",
   1760 			       &ISCSR,
   1761 			       r,
   1762 			       acb->xs->cmd->opcode,
   1763 			       SCR, sc->sc_phase);
   1764 			panic("unexpected result.");
   1765 		case 0x82:	/* selection timeout */
   1766 			SPC_MISC(("selection timeout  "));
   1767 			sc->sc_phase = BUSFREE_PHASE;
   1768 			SPC_ASSERT(sc->sc_nexus != NULL);
   1769 			acb = sc->sc_nexus;
   1770 			delay(250);
   1771 			acb->xs->error = XS_SELTIMEOUT;
   1772 			mha_done(sc, acb);
   1773 			continue;	/* XXX ??? msaitoh */
   1774 		case 0x60:	/* command completed */
   1775 			sc->sc_spcinitialized++;
   1776 			if (sc->sc_phase == BUSFREE_PHASE)
   1777 				continue;
   1778 			ph = SCR;
   1779 			if (ph & PSNS_ACK) {
   1780 				int s;
   1781 				/* $B$U$D!<$N%3%^%s%I$,=*N;$7$?$i$7$$(B */
   1782 				SPC_MISC(("0x60)phase = %x(ought to be %x)\n",
   1783 					  ph & PHASE_MASK, sc->sc_phase));
   1784 #if 0
   1785 /*				switch (sc->sc_phase) {*/
   1786 #else
   1787 				switch (ph & PHASE_MASK) {
   1788 #endif
   1789 				case STATUS_PHASE:
   1790 					if (sc->sc_state != SPC_HASNEXUS)
   1791 						printf("stsin: !SPC_HASNEXUS->(%d)\n",
   1792 						       sc->sc_state);
   1793 					SPC_ASSERT(sc->sc_nexus != NULL);
   1794 					acb = sc->sc_nexus;
   1795 					WAIT;
   1796 					s = MBR;
   1797 					SPC_ASSERT(s == 1);
   1798 					acb->stat = sc->sc_pcx[0]; /* XXX */
   1799 					SPC_MISC(("stat=0x%02x  ", acb->stat));
   1800 					sc->sc_prevphase = STATUS_PHASE;
   1801 					break;
   1802 				case MESSAGE_IN_PHASE:
   1803 					mha_msgin(sc);
   1804 					sc->sc_prevphase = MESSAGE_IN_PHASE;
   1805 					/* thru */
   1806 				case DATA_IN_PHASE:
   1807 					if (sc->sc_dmasize == 0)
   1808 						break;
   1809 					bus_dmamap_sync(sc->sc_dmat,
   1810 							sc->sc_dmamap,
   1811 							0, sc->sc_dmasize,
   1812 							BUS_DMASYNC_POSTREAD);
   1813 					memcpy(sc->sc_p, sc->sc_dmabuf,
   1814 					       sc->sc_dmasize);
   1815 					sc->sc_dmasize = 0;
   1816 					break;
   1817 				case DATA_OUT_PHASE:
   1818 					if (sc->sc_dmasize == 0)
   1819 						break;
   1820 					bus_dmamap_sync(sc->sc_dmat,
   1821 							sc->sc_dmamap,
   1822 							0, sc->sc_dmasize,
   1823 							BUS_DMASYNC_POSTWRITE);
   1824 					sc->sc_dmasize = 0;
   1825 					break;
   1826 				}
   1827 				WAIT;
   1828 				CMR = CMD_RESET_ACK;	/* reset ack */
   1829 				/*mha_done(sc, acb);	XXX */
   1830 				continue;
   1831 			} else if (NSR & 0x80) { /* nexus */
   1832 #if 1
   1833 				if (sc->sc_state == SPC_SELECTING)	/* XXX msaitoh */
   1834 					sc->sc_state = SPC_HASNEXUS;
   1835 				/* $B%U%'!<%:$N7h$aBG$A$r$9$k(B
   1836 				   $B30$l$?$i!"(Binitial-phase error(0x54) $B$,(B
   1837 				   $BJV$C$F$/$k$s$GCm0U$7$?$^$(!#(B
   1838 				   $B$G$b$J$<$+(B 0x65 $B$,JV$C$F$-$?$j$7$F$M!<$+(B? */
   1839 				WAIT;
   1840 				if (SSR & SS_IREQUEST)
   1841 					continue;
   1842 				switch (sc->sc_phase) {
   1843 				default:
   1844 					panic("$B8+CN$i$L(B phase $B$,Mh$A$^$C$?$@$h(B");
   1845 				case MESSAGE_IN_PHASE:
   1846 					/* $B2?$b$7$J$$(B */
   1847 					continue;
   1848 				case STATUS_PHASE:
   1849 					sc->sc_phase = MESSAGE_IN_PHASE;
   1850 					CMR = CMD_RECEIVE_MSG;	/* receive msg */
   1851 					continue;
   1852 				case DATA_IN_PHASE:
   1853 					sc->sc_prevphase = DATA_IN_PHASE;
   1854 					if (sc->sc_dleft == 0) {
   1855 						/* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
   1856 						   $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
   1857 						sc->sc_phase = STATUS_PHASE;
   1858 						CMR = CMD_RECEIVE_STS;	/* receive sts */
   1859 						continue;
   1860 					}
   1861 					n = mha_datain(sc, sc->sc_dp,
   1862 						       sc->sc_dleft);
   1863 					sc->sc_dp += n;
   1864 					sc->sc_dleft -= n;
   1865 					continue;
   1866 				case DATA_OUT_PHASE:
   1867 					sc->sc_prevphase = DATA_OUT_PHASE;
   1868 					if (sc->sc_dleft == 0) {
   1869 						/* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
   1870 						   $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
   1871 						sc->sc_phase = STATUS_PHASE;
   1872 						CMR = CMD_RECEIVE_STS;	/* receive sts */
   1873 						continue;
   1874 					}
   1875 					/* data phase $B$NB3$-$r$d$m$&(B */
   1876 					n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
   1877 					sc->sc_dp += n;
   1878 					sc->sc_dleft -= n;
   1879 					continue;
   1880 				case COMMAND_PHASE:
   1881 					/* $B:G=i$O(B CMD PHASE $B$H$$$&$3$H$i$7$$(B */
   1882 					if (acb->dleft) {
   1883 						/* $B%G!<%?E>Aw$,$"$j$&$k>l9g(B */
   1884 						if (acb->xs->xs_control & XS_CTL_DATA_IN) {
   1885 							sc->sc_phase = DATA_IN_PHASE;
   1886 							n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
   1887 							sc->sc_dp += n;
   1888 							sc->sc_dleft -= n;
   1889 						}
   1890 						else if (acb->xs->xs_control & XS_CTL_DATA_OUT) {
   1891 							sc->sc_phase = DATA_OUT_PHASE;
   1892 							n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
   1893 							sc->sc_dp += n;
   1894 							sc->sc_dleft -= n;
   1895 						}
   1896 						continue;
   1897 					}
   1898 					else {
   1899 						/* $B%G!<%?E>Aw$O$J$$$i$7$$(B?! */
   1900 						WAIT;
   1901 						sc->sc_phase = STATUS_PHASE;
   1902 						CMR = CMD_RECEIVE_STS;	/* receive sts */
   1903 						continue;
   1904 					}
   1905 				}
   1906 #endif
   1907 			}
   1908 			continue;
   1909 		case 0x31:	/* disconnected in xfer progress. */
   1910 			SPC_MISC(("[0x31]"));
   1911 		case 0x70:	/* disconnected. */
   1912 			SPC_ASSERT(sc->sc_flags & SPC_BUSFREE_OK);
   1913 			sc->sc_phase = BUSFREE_PHASE;
   1914 			sc->sc_state = SPC_IDLE;
   1915 #if 1
   1916 			acb = sc->sc_nexus;
   1917 			SPC_ASSERT(sc->sc_nexus != NULL);
   1918 			acb->xs->error = XS_NOERROR;
   1919 			mha_done(sc, acb);
   1920 #else
   1921 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1922 			mha_sched(sc);
   1923 #endif
   1924 			continue;
   1925 		case 0x32:	/* phase error in xfer progress. */
   1926 			SPC_MISC(("[0x32]"));
   1927 #if 0
   1928 		case 0x65:	/* invalid command.
   1929 				   $B$J$<$3$s$J$b$N$,=P$k$N$+(B
   1930 				   $B26$K$OA4$/M}2r$G$-$J$$(B */
   1931 #if 1
   1932 			SPC_MISC(("[0x%04x]", r));
   1933 #endif
   1934 #endif
   1935 		case 0x54:	/* initial-phase error. */
   1936 			SPC_MISC(("[0x54, ns=%x, ph=%x(ought to be %x)]",
   1937 				  NSR,
   1938 				  SCR, sc->sc_phase));
   1939 			/* thru */
   1940 		case 0x71:	/* assert req */
   1941 			WAIT;
   1942 			if (SSR & 0x40) {
   1943 				printf("SPC sts=%2x, r=%04x, ns=%x, ph=%x\n",
   1944 				       SSR, r, NSR, SCR);
   1945 				WAIT;
   1946 			}
   1947 			ph = SCR;
   1948 			if (sc->sc_state == SPC_SELECTING) {	/* XXX msaitoh */
   1949 				sc->sc_state = SPC_HASNEXUS;
   1950 			}
   1951 			if (ph & 0x80) {
   1952 				switch (ph & PHASE_MASK) {
   1953 				default:
   1954 					printf("phase = %x\n", ph);
   1955 					panic("assert req: the phase I don't know!");
   1956 				case DATA_IN_PHASE:
   1957 					sc->sc_prevphase = DATA_IN_PHASE;
   1958 					SPC_MISC(("DATAIN(%d)...", sc->sc_dleft));
   1959 					n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
   1960 					sc->sc_dp += n;
   1961 					sc->sc_dleft -= n;
   1962 					SPC_MISC(("done\n"));
   1963 					continue;
   1964 				case DATA_OUT_PHASE:
   1965 					sc->sc_prevphase = DATA_OUT_PHASE;
   1966 					SPC_MISC(("DATAOUT\n"));
   1967 					n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
   1968 					sc->sc_dp += n;
   1969 					sc->sc_dleft -= n;
   1970 					continue;
   1971 				case STATUS_PHASE:
   1972 					sc->sc_phase = STATUS_PHASE;
   1973 					SPC_MISC(("[RECV_STS]"));
   1974 					WAIT;
   1975 					CMR = CMD_RECEIVE_STS;	/* receive sts */
   1976 					continue;
   1977 				case MESSAGE_IN_PHASE:
   1978 					sc->sc_phase = MESSAGE_IN_PHASE;
   1979 					WAIT;
   1980 					CMR = CMD_RECEIVE_MSG;
   1981 					continue;
   1982 				}
   1983 			}
   1984 			continue;
   1985 		}
   1986 	}
   1987 
   1988 	return 1;
   1989 }
   1990 
   1991 void
   1992 mha_abort(sc, acb)
   1993 	struct mha_softc *sc;
   1994 	struct acb *acb;
   1995 {
   1996 	acb->flags |= ACB_ABORTED;
   1997 
   1998 	if (acb == sc->sc_nexus) {
   1999 		/*
   2000 		 * If we're still selecting, the message will be scheduled
   2001 		 * after selection is complete.
   2002 		 */
   2003 		if (sc->sc_state == SPC_HASNEXUS) {
   2004 			sc->sc_flags |= SPC_ABORTING;
   2005 			mha_sched_msgout(SEND_ABORT);
   2006 		}
   2007 	} else {
   2008 		if (sc->sc_state == SPC_IDLE)
   2009 			mha_sched(sc);
   2010 	}
   2011 }
   2012 
   2013 void
   2014 mha_timeout(arg)
   2015 	void *arg;
   2016 {
   2017 	int s = splbio();
   2018 	struct acb *acb = (struct acb *)arg;
   2019 	struct scsipi_xfer *xs = acb->xs;
   2020 	struct scsipi_periph *periph = xs->xs_periph;
   2021 	struct mha_softc *sc =
   2022 	    (void*)periph->periph_channel->chan_adapter->adapt_dev;
   2023 
   2024 	scsipi_printaddr(periph);
   2025 	printf("%s: timed out [acb %p (flags 0x%x, dleft %x, stat %x)], "
   2026 	       "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) >",
   2027 		sc->sc_dev.dv_xname,
   2028 		acb, acb->flags, acb->dleft, acb->stat,
   2029 		sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
   2030 		sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout
   2031 		);
   2032 	printf("[%04x %02x]\n", sc->sc_ps[1], SCR);
   2033 	panic("timeout, ouch!");
   2034 
   2035 	if (acb->flags & ACB_ABORTED) {
   2036 		/* abort timed out */
   2037 		printf(" AGAIN\n");
   2038 #if 0
   2039 		mha_init(sc, 1); /* XXX 1?*/
   2040 #endif
   2041 	} else {
   2042 		/* abort the operation that has timed out */
   2043 		printf("\n");
   2044 		xs->error = XS_TIMEOUT;
   2045 		mha_abort(sc, acb);
   2046 	}
   2047 
   2048 	splx(s);
   2049 }
   2050 
   2051 #if SPC_DEBUG
   2053 /*
   2054  * The following functions are mostly used for debugging purposes, either
   2055  * directly called from the driver or from the kernel debugger.
   2056  */
   2057 
   2058 void
   2059 mha_show_scsi_cmd(acb)
   2060 	struct acb *acb;
   2061 {
   2062 	u_char  *b = (u_char *)&acb->cmd;
   2063 	struct scsipi_periph *periph = acb->xs->xs_periph;
   2064 	int i;
   2065 
   2066 	scsipi_printaddr(periph);
   2067 	if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
   2068 		for (i = 0; i < acb->clen; i++) {
   2069 			if (i)
   2070 				printf(",");
   2071 			printf("%x", b[i]);
   2072 		}
   2073 		printf("\n");
   2074 	} else
   2075 		printf("RESET\n");
   2076 }
   2077 
   2078 void
   2079 mha_print_acb(acb)
   2080 	struct acb *acb;
   2081 {
   2082 
   2083 	printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags);
   2084 	printf(" dp=%p dleft=%d stat=%x\n",
   2085 	    acb->daddr, acb->dleft, acb->stat);
   2086 	mha_show_scsi_cmd(acb);
   2087 }
   2088 
   2089 void
   2090 mha_print_active_acb()
   2091 {
   2092 	struct acb *acb;
   2093 	struct mha_softc *sc = mha_cd.cd_devs[0]; /* XXX */
   2094 
   2095 	printf("ready list:\n");
   2096 	for (acb = sc->ready_list.tqh_first; acb != NULL;
   2097 	    acb = acb->chain.tqe_next)
   2098 		mha_print_acb(acb);
   2099 	printf("nexus:\n");
   2100 	if (sc->sc_nexus != NULL)
   2101 		mha_print_acb(sc->sc_nexus);
   2102 	printf("nexus list:\n");
   2103 	for (acb = sc->nexus_list.tqh_first; acb != NULL;
   2104 	    acb = acb->chain.tqe_next)
   2105 		mha_print_acb(acb);
   2106 }
   2107 
   2108 void
   2109 mha_dump_driver(sc)
   2110 	struct mha_softc *sc;
   2111 {
   2112 	struct spc_tinfo *ti;
   2113 	int i;
   2114 
   2115 	printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2116 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
   2117 	    sc->sc_state, sc->sc_imess[0],
   2118 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2119 	for (i = 0; i < 7; i++) {
   2120 		ti = &sc->sc_tinfo[i];
   2121 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2122 		    i, ti->cmds, ti->dconns, ti->touts);
   2123 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2124 	}
   2125 }
   2126 #endif
   2127