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