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