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