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