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