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