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