Home | History | Annotate | Line # | Download | only in ic
mb89352.c revision 1.26
      1 /*	$NetBSD: mb89352.c,v 1.26 2004/01/06 18:07:17 tsutsui Exp $	*/
      2 /*	NecBSD: mb89352.c,v 1.4 1998/03/14 07:31:20 kmatsuda Exp	*/
      3 
      4 /*-
      5  * Copyright (c) 1996,97,98,99 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Charles M. Hannum, Masaru Oki and Kouichi Matsuda.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by Charles M. Hannum.
     22  * 4. The name of the author may not be used to endorse or promote products
     23  *    derived from this software without specific prior written permission.
     24  *
     25  * Copyright (c) 1994 Jarle Greipsland
     26  * All rights reserved.
     27  *
     28  * Redistribution and use in source and binary forms, with or without
     29  * modification, are permitted provided that the following conditions
     30  * are met:
     31  * 1. Redistributions of source code must retain the above copyright
     32  *    notice, this list of conditions and the following disclaimer.
     33  * 2. Redistributions in binary form must reproduce the above copyright
     34  *    notice, this list of conditions and the following disclaimer in the
     35  *    documentation and/or other materials provided with the distribution.
     36  * 3. The name of the author may not be used to endorse or promote products
     37  *    derived from this software without specific prior written permission.
     38  *
     39  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     40  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     41  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     42  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     43  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     44  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     45  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     49  * POSSIBILITY OF SUCH DAMAGE.
     50  */
     51 /*
     52  * [NetBSD for NEC PC-98 series]
     53  *  Copyright (c) 1996, 1997, 1998
     54  *	NetBSD/pc98 porting staff. All rights reserved.
     55  *  Copyright (c) 1996, 1997, 1998
     56  *	Kouichi Matsuda. All rights reserved.
     57  */
     58 
     59 /*
     60  * Acknowledgements: Many of the algorithms used in this driver are
     61  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     62  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     63  */
     64 
     65 /* TODO list:
     66  * 1) Get the DMA stuff working.
     67  * 2) Get the iov/uio stuff working. Is this a good thing ???
     68  * 3) Get the synch stuff working.
     69  * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: mb89352.c,v 1.26 2004/01/06 18:07:17 tsutsui Exp $");
     74 
     75 #ifdef DDB
     76 #define	integrate
     77 #else
     78 #define	integrate	__inline static
     79 #endif
     80 
     81 /*
     82  * A few customizable items:
     83  */
     84 
     85 /* Synchronous data transfers? */
     86 #define SPC_USE_SYNCHRONOUS	0
     87 #define SPC_SYNC_REQ_ACK_OFS 	8
     88 
     89 /* Wide data transfers? */
     90 #define	SPC_USE_WIDE		0
     91 #define	SPC_MAX_WIDTH		0
     92 
     93 /* Max attempts made to transmit a message */
     94 #define SPC_MSG_MAX_ATTEMPT	3 /* Not used now XXX */
     95 
     96 /*
     97  * Some spin loop parameters (essentially how long to wait some places)
     98  * The problem(?) is that sometimes we expect either to be able to transmit a
     99  * byte or to get a new one from the SCSI bus pretty soon.  In order to avoid
    100  * returning from the interrupt just to get yanked back for the next byte we
    101  * may spin in the interrupt routine waiting for this byte to come.  How long?
    102  * This is really (SCSI) device and processor dependent.  Tuneable, I guess.
    103  */
    104 #define SPC_MSGIN_SPIN	1 	/* Will spinwait upto ?ms for a new msg byte */
    105 #define SPC_MSGOUT_SPIN	1
    106 
    107 /*
    108  * Include debug functions?  At the end of this file there are a bunch of
    109  * functions that will print out various information regarding queued SCSI
    110  * commands, driver state and chip contents.  You can call them from the
    111  * kernel debugger.  If you set SPC_DEBUG to 0 they are not included (the
    112  * kernel uses less memory) but you lose the debugging facilities.
    113  */
    114 #if 0
    115 #define SPC_DEBUG		1
    116 #endif
    117 
    118 #define	SPC_ABORT_TIMEOUT	2000	/* time to wait for abort */
    119 
    120 /* threshold length for DMA transfer */
    121 #define SPC_MIN_DMA_LEN	32
    122 
    123 #ifdef x68k	/* XXX it seems x68k SPC SCSI hardware has some quirks */
    124 #define NEED_DREQ_ON_HARDWARE_XFER
    125 #define NO_MANUAL_XFER
    126 #endif
    127 
    128 /* End of customizable parameters */
    129 
    130 /*
    131  * MB89352 SCSI Protocol Controller (SPC) routines.
    132  */
    133 
    134 #include "opt_ddb.h"
    135 
    136 #include <sys/param.h>
    137 #include <sys/systm.h>
    138 #include <sys/kernel.h>
    139 #include <sys/errno.h>
    140 #include <sys/ioctl.h>
    141 #include <sys/device.h>
    142 #include <sys/buf.h>
    143 #include <sys/proc.h>
    144 #include <sys/user.h>
    145 #include <sys/queue.h>
    146 
    147 #include <machine/intr.h>
    148 #include <machine/bus.h>
    149 
    150 #include <dev/scsipi/scsi_all.h>
    151 #include <dev/scsipi/scsipi_all.h>
    152 #include <dev/scsipi/scsi_message.h>
    153 #include <dev/scsipi/scsiconf.h>
    154 
    155 #include <dev/ic/mb89352reg.h>
    156 #include <dev/ic/mb89352var.h>
    157 
    158 #ifndef DDB
    159 #define	Debugger() panic("should call debugger here (mb89352.c)")
    160 #endif /* ! DDB */
    161 
    162 #if SPC_DEBUG
    163 int spc_debug = 0x00; /* SPC_SHOWSTART|SPC_SHOWMISC|SPC_SHOWTRACE; */
    164 #endif
    165 
    166 void	spc_done	__P((struct spc_softc *, struct spc_acb *));
    167 void	spc_dequeue	__P((struct spc_softc *, struct spc_acb *));
    168 void	spc_scsipi_request __P((struct scsipi_channel *,
    169 				scsipi_adapter_req_t, void *));
    170 int	spc_poll	__P((struct spc_softc *, struct scsipi_xfer *, int));
    171 integrate void	spc_sched_msgout __P((struct spc_softc *, u_char));
    172 integrate void	spc_setsync	__P((struct spc_softc *, struct spc_tinfo *));
    173 void	spc_select	__P((struct spc_softc *, struct spc_acb *));
    174 void	spc_timeout	__P((void *));
    175 void	spc_scsi_reset	__P((struct spc_softc *));
    176 void	spc_reset	__P((struct spc_softc *));
    177 void	spc_free_acb	__P((struct spc_softc *, struct spc_acb *, int));
    178 struct spc_acb* spc_get_acb __P((struct spc_softc *));
    179 int	spc_reselect	__P((struct spc_softc *, int));
    180 void	spc_msgin	__P((struct spc_softc *));
    181 void	spc_abort	__P((struct spc_softc *, struct spc_acb *));
    182 void	spc_msgout	__P((struct spc_softc *));
    183 int	spc_dataout_pio	__P((struct spc_softc *, u_char *, int));
    184 int	spc_datain_pio	__P((struct spc_softc *, u_char *, int));
    185 #if SPC_DEBUG
    186 void	spc_print_acb	__P((struct spc_acb *));
    187 void	spc_dump_driver __P((struct spc_softc *));
    188 void	spc_dump89352	__P((struct spc_softc *));
    189 void	spc_show_scsi_cmd __P((struct spc_acb *));
    190 void	spc_print_active_acb __P((void));
    191 #endif
    192 
    193 extern struct cfdriver spc_cd;
    194 
    195 /*
    196  * INITIALIZATION ROUTINES (probe, attach ++)
    197  */
    198 
    199 /*
    200  * Do the real search-for-device.
    201  * Prerequisite: sc->sc_iobase should be set to the proper value
    202  */
    203 int
    204 spc_find(iot, ioh, bdid)
    205 	bus_space_tag_t iot;
    206 	bus_space_handle_t ioh;
    207 	int bdid;
    208 {
    209 	long timeout = SPC_ABORT_TIMEOUT;
    210 
    211 	SPC_TRACE(("spc: probing for spc-chip\n"));
    212 	/*
    213 	 * Disable interrupts then reset the FUJITSU chip.
    214 	 */
    215 	bus_space_write_1(iot, ioh, SCTL, SCTL_DISABLE | SCTL_CTRLRST);
    216 	bus_space_write_1(iot, ioh, SCMD, 0);
    217 	bus_space_write_1(iot, ioh, PCTL, 0);
    218 	bus_space_write_1(iot, ioh, TEMP, 0);
    219 	bus_space_write_1(iot, ioh, TCH, 0);
    220 	bus_space_write_1(iot, ioh, TCM, 0);
    221 	bus_space_write_1(iot, ioh, TCL, 0);
    222 	bus_space_write_1(iot, ioh, INTS, 0);
    223 	bus_space_write_1(iot, ioh, SCTL,
    224 	    SCTL_DISABLE | SCTL_ABRT_ENAB | SCTL_PARITY_ENAB | SCTL_RESEL_ENAB);
    225 	bus_space_write_1(iot, ioh, BDID, bdid);
    226 	delay(400);
    227 	bus_space_write_1(iot, ioh, SCTL,
    228 	    bus_space_read_1(iot, ioh, SCTL) & ~SCTL_DISABLE);
    229 
    230 	/* The following detection is derived from spc.c
    231 	 * (by Takahide Matsutsuka) in FreeBSD/pccard-test.
    232 	 */
    233 	while (bus_space_read_1(iot, ioh, PSNS) && timeout) {
    234 		timeout--;
    235 		DELAY(1);
    236 	}
    237 	if (timeout == 0) {
    238 		printf("spc: find failed\n");
    239 		return 0;
    240 	}
    241 
    242 	SPC_START(("SPC found"));
    243 	return 1;
    244 }
    245 
    246 void
    247 spc_attach(sc)
    248 	struct spc_softc *sc;
    249 {
    250 
    251 	SPC_TRACE(("spc_attach  "));
    252 	sc->sc_state = SPC_INIT;
    253 
    254 	sc->sc_freq = 20;	/* XXXX Assume 20 MHz. */
    255 
    256 #if SPC_USE_SYNCHRONOUS
    257 	/*
    258 	 * These are the bounds of the sync period, based on the frequency of
    259 	 * the chip's clock input and the size and offset of the sync period
    260 	 * register.
    261 	 *
    262 	 * For a 20MHz clock, this gives us 25, or 100nS, or 10MB/s, as a
    263 	 * maximum transfer rate, and 112.5, or 450nS, or 2.22MB/s, as a
    264 	 * minimum transfer rate.
    265 	 */
    266 	sc->sc_minsync = (2 * 250) / sc->sc_freq;
    267 	sc->sc_maxsync = (9 * 250) / sc->sc_freq;
    268 #endif
    269 
    270 	spc_init(sc);	/* Init chip and driver */
    271 
    272 	/*
    273 	 * Fill in the adapter.
    274 	 */
    275 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    276 	sc->sc_adapter.adapt_nchannels = 1;
    277 	sc->sc_adapter.adapt_openings = 7;
    278 	sc->sc_adapter.adapt_max_periph = 1;
    279 	sc->sc_adapter.adapt_minphys = minphys;
    280 	sc->sc_adapter.adapt_request = spc_scsipi_request;
    281 
    282 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    283 	sc->sc_channel.chan_bustype = &scsi_bustype;
    284 	sc->sc_channel.chan_channel = 0;
    285 	sc->sc_channel.chan_ntargets = 8;
    286 	sc->sc_channel.chan_nluns = 8;
    287 	sc->sc_channel.chan_id = sc->sc_initiator;
    288 
    289 	/*
    290 	 * ask the adapter what subunits are present
    291 	 */
    292 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    293 }
    294 
    295 /*
    296  * Initialize MB89352 chip itself
    297  * The following conditions should hold:
    298  * spc_isa_probe should have succeeded, i.e. the iobase address in spc_softc
    299  * must be valid.
    300  */
    301 void
    302 spc_reset(sc)
    303 	struct spc_softc *sc;
    304 {
    305 	bus_space_tag_t iot = sc->sc_iot;
    306 	bus_space_handle_t ioh = sc->sc_ioh;
    307 
    308 	SPC_TRACE(("spc_reset  "));
    309 	/*
    310 	 * Disable interrupts then reset the FUJITSU chip.
    311 	 */
    312 	bus_space_write_1(iot, ioh, SCTL, SCTL_DISABLE | SCTL_CTRLRST);
    313 	bus_space_write_1(iot, ioh, SCMD, 0);
    314 	bus_space_write_1(iot, ioh, TMOD, 0);
    315 	bus_space_write_1(iot, ioh, PCTL, 0);
    316 	bus_space_write_1(iot, ioh, TEMP, 0);
    317 	bus_space_write_1(iot, ioh, TCH, 0);
    318 	bus_space_write_1(iot, ioh, TCM, 0);
    319 	bus_space_write_1(iot, ioh, TCL, 0);
    320 	bus_space_write_1(iot, ioh, INTS, 0);
    321 	bus_space_write_1(iot, ioh, SCTL,
    322 	    SCTL_DISABLE | SCTL_ABRT_ENAB | SCTL_PARITY_ENAB | SCTL_RESEL_ENAB);
    323 	bus_space_write_1(iot, ioh, BDID, sc->sc_initiator);
    324 	delay(400);
    325 	bus_space_write_1(iot, ioh, SCTL,
    326 	    bus_space_read_1(iot, ioh, SCTL) & ~SCTL_DISABLE);
    327 }
    328 
    329 
    330 /*
    331  * Pull the SCSI RST line for 500us.
    332  */
    333 void
    334 spc_scsi_reset(sc)
    335 	struct spc_softc *sc;
    336 {
    337 	bus_space_tag_t iot = sc->sc_iot;
    338 	bus_space_handle_t ioh = sc->sc_ioh;
    339 
    340 	SPC_TRACE(("spc_scsi_reset  "));
    341 	bus_space_write_1(iot, ioh, SCMD,
    342 	    bus_space_read_1(iot, ioh, SCMD) | SCMD_RST);
    343 	delay(500);
    344 	bus_space_write_1(iot, ioh, SCMD,
    345 	    bus_space_read_1(iot, ioh, SCMD) & ~SCMD_RST);
    346 	delay(50);
    347 }
    348 
    349 /*
    350  * Initialize spc SCSI driver.
    351  */
    352 void
    353 spc_init(sc)
    354 	struct spc_softc *sc;
    355 {
    356 	struct spc_acb *acb;
    357 	int r;
    358 
    359 	SPC_TRACE(("spc_init  "));
    360 	spc_reset(sc);
    361 	spc_scsi_reset(sc);
    362 	spc_reset(sc);
    363 
    364 	if (sc->sc_state == SPC_INIT) {
    365 		/* First time through; initialize. */
    366 		TAILQ_INIT(&sc->ready_list);
    367 		TAILQ_INIT(&sc->nexus_list);
    368 		TAILQ_INIT(&sc->free_list);
    369 		sc->sc_nexus = NULL;
    370 		acb = sc->sc_acb;
    371 		memset(acb, 0, sizeof(sc->sc_acb));
    372 		for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
    373 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    374 			acb++;
    375 		}
    376 		memset(&sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
    377 	} else {
    378 		/* Cancel any active commands. */
    379 		sc->sc_state = SPC_CLEANING;
    380 		if ((acb = sc->sc_nexus) != NULL) {
    381 			acb->xs->error = XS_DRIVER_STUFFUP;
    382 			callout_stop(&acb->xs->xs_callout);
    383 			spc_done(sc, acb);
    384 		}
    385 		while ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
    386 			acb->xs->error = XS_DRIVER_STUFFUP;
    387 			callout_stop(&acb->xs->xs_callout);
    388 			spc_done(sc, acb);
    389 		}
    390 	}
    391 
    392 	sc->sc_prevphase = PH_INVALID;
    393 	for (r = 0; r < 8; r++) {
    394 		struct spc_tinfo *ti = &sc->sc_tinfo[r];
    395 
    396 		ti->flags = 0;
    397 #if SPC_USE_SYNCHRONOUS
    398 		ti->flags |= DO_SYNC;
    399 		ti->period = sc->sc_minsync;
    400 		ti->offset = SPC_SYNC_REQ_ACK_OFS;
    401 #else
    402 		ti->period = ti->offset = 0;
    403 #endif
    404 #if SPC_USE_WIDE
    405 		ti->flags |= DO_WIDE;
    406 		ti->width = SPC_MAX_WIDTH;
    407 #else
    408 		ti->width = 0;
    409 #endif
    410 	}
    411 
    412 	sc->sc_state = SPC_IDLE;
    413 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCTL,
    414 	    bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCTL) | SCTL_INTR_ENAB);
    415 }
    416 
    417 void
    418 spc_free_acb(sc, acb, flags)
    419 	struct spc_softc *sc;
    420 	struct spc_acb *acb;
    421 	int flags;
    422 {
    423 	int s;
    424 
    425 	SPC_TRACE(("spc_free_acb  "));
    426 	s = splbio();
    427 
    428 	acb->flags = 0;
    429 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    430 	splx(s);
    431 }
    432 
    433 struct spc_acb *
    434 spc_get_acb(sc)
    435 	struct spc_softc *sc;
    436 {
    437 	struct spc_acb *acb;
    438 	int s;
    439 
    440 	SPC_TRACE(("spc_get_acb  "));
    441 	s = splbio();
    442 	acb = TAILQ_FIRST(&sc->free_list);
    443 	if (acb != NULL) {
    444 		TAILQ_REMOVE(&sc->free_list, acb, chain);
    445 		acb->flags |= ACB_ALLOC;
    446 	}
    447 	splx(s);
    448 	return acb;
    449 }
    450 
    451 /*
    452  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    453  */
    454 
    455 /*
    456  * Expected sequence:
    457  * 1) Command inserted into ready list
    458  * 2) Command selected for execution
    459  * 3) Command won arbitration and has selected target device
    460  * 4) Send message out (identify message, eventually also sync.negotiations)
    461  * 5) Send command
    462  * 5a) Receive disconnect message, disconnect.
    463  * 5b) Reselected by target
    464  * 5c) Receive identify message from target.
    465  * 6) Send or receive data
    466  * 7) Receive status
    467  * 8) Receive message (command complete etc.)
    468  */
    469 
    470 /*
    471  * Start a SCSI-command
    472  * This function is called by the higher level SCSI-driver to queue/run
    473  * SCSI-commands.
    474  */
    475 void
    476 spc_scsipi_request(chan, req, arg)
    477 	struct scsipi_channel *chan;
    478 	scsipi_adapter_req_t req;
    479 	void *arg;
    480 {
    481 	struct scsipi_xfer *xs;
    482 	struct scsipi_periph *periph;
    483 	struct spc_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    484 	struct spc_acb *acb;
    485 	int s, flags;
    486 
    487 	switch (req) {
    488 	case ADAPTER_REQ_RUN_XFER:
    489 		xs = arg;
    490 		periph = xs->xs_periph;
    491 		SPC_TRACE(("spc_scsipi_request  "));
    492 		SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    493 		    periph->periph_target));
    494 
    495 		flags = xs->xs_control;
    496 		acb = spc_get_acb(sc);
    497 #ifdef DIAGNOSTIC
    498 		/*
    499 		 * This should nerver happen as we track the resources
    500 		 * in the mid-layer.
    501 		 */
    502 		if (acb == NULL) {
    503 			scsipi_printaddr(periph);
    504 			printf("unable to allocate acb\n");
    505 			panic("spc_scsipi_request");
    506 		}
    507 #endif
    508 
    509 		/* Initialize acb */
    510 		acb->xs = xs;
    511 		acb->timeout = xs->timeout;
    512 
    513 		if (xs->xs_control & XS_CTL_RESET) {
    514 			acb->flags |= ACB_RESET;
    515 			acb->scsipi_cmd_length = 0;
    516 			acb->data_length = 0;
    517 		} else {
    518 			memcpy(&acb->scsipi_cmd, xs->cmd, xs->cmdlen);
    519 			acb->scsipi_cmd_length = xs->cmdlen;
    520 			acb->data_addr = xs->data;
    521 			acb->data_length = xs->datalen;
    522 		}
    523 		acb->target_stat = 0;
    524 
    525 		s = splbio();
    526 
    527 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    528 		/*
    529 		 * Start scheduling unless a queue process is in progress.
    530 		 */
    531 		if (sc->sc_state == SPC_IDLE)
    532 			spc_sched(sc);
    533 		/*
    534 		 * After successful sending, check if we should return just now.
    535 		 * If so, return SUCCESSFULLY_QUEUED.
    536 		 */
    537 
    538 		splx(s);
    539 
    540 		if ((flags & XS_CTL_POLL) == 0)
    541 			return;
    542 
    543 		/* Not allowed to use interrupts, use polling instead */
    544 		s = splbio();
    545 		if (spc_poll(sc, xs, acb->timeout)) {
    546 			spc_timeout(acb);
    547 			if (spc_poll(sc, xs, acb->timeout))
    548 				spc_timeout(acb);
    549 		}
    550 		splx(s);
    551 		return;
    552 	case ADAPTER_REQ_GROW_RESOURCES:
    553 		/* XXX Not supported. */
    554 		return;
    555 	case ADAPTER_REQ_SET_XFER_MODE:
    556 	    {
    557 		/*
    558 		 * We don't support Sync, Wide, or Tagged Command Queuing.
    559 		 * Just callback now, to report this.
    560 		 */
    561 		struct scsipi_xfer_mode *xm = arg;
    562 
    563 		xm->xm_mode = 0;
    564 		xm->xm_period = 0;
    565 		xm->xm_offset = 0;
    566 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    567 		return;
    568 	    }
    569 	}
    570 }
    571 
    572 /*
    573  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    574  */
    575 int
    576 spc_poll(sc, xs, count)
    577 	struct spc_softc *sc;
    578 	struct scsipi_xfer *xs;
    579 	int count;
    580 {
    581 	bus_space_tag_t iot = sc->sc_iot;
    582 	bus_space_handle_t ioh = sc->sc_ioh;
    583 
    584 	SPC_TRACE(("spc_poll  "));
    585 	while (count) {
    586 		/*
    587 		 * If we had interrupts enabled, would we
    588 		 * have got an interrupt?
    589 		 */
    590 		if (bus_space_read_1(iot, ioh, INTS) != 0)
    591 			spc_intr(sc);
    592 		if ((xs->xs_status & XS_STS_DONE) != 0)
    593 			return 0;
    594 		delay(1000);
    595 		count--;
    596 	}
    597 	return 1;
    598 }
    599 
    600 /*
    601  * LOW LEVEL SCSI UTILITIES
    602  */
    603 
    604 integrate void
    605 spc_sched_msgout(sc, m)
    606 	struct spc_softc *sc;
    607 	u_char m;
    608 {
    609 	bus_space_tag_t iot = sc->sc_iot;
    610 	bus_space_handle_t ioh = sc->sc_ioh;
    611 
    612 	SPC_TRACE(("spc_sched_msgout  "));
    613 	if (sc->sc_msgpriq == 0)
    614 		bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ATN);
    615 	sc->sc_msgpriq |= m;
    616 }
    617 
    618 /*
    619  * Set synchronous transfer offset and period.
    620  */
    621 integrate void
    622 spc_setsync(sc, ti)
    623 	struct spc_softc *sc;
    624 	struct spc_tinfo *ti;
    625 {
    626 #if SPC_USE_SYNCHRONOUS
    627 	bus_space_tag_t iot = sc->sc_iot;
    628 	bus_space_handle_t ioh = sc->sc_ioh;
    629 
    630 	SPC_TRACE(("spc_setsync  "));
    631 	if (ti->offset != 0)
    632 		bus_space_write_1(iot, ioh, TMOD,
    633 		    ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
    634 	else
    635 		bus_space_write_1(iot, ioh, TMOD, 0);
    636 #endif
    637 }
    638 
    639 /*
    640  * Start a selection.  This is used by spc_sched() to select an idle target.
    641  */
    642 void
    643 spc_select(sc, acb)
    644 	struct spc_softc *sc;
    645 	struct spc_acb *acb;
    646 {
    647 	struct scsipi_periph *periph = acb->xs->xs_periph;
    648 	int target = periph->periph_target;
    649 	struct spc_tinfo *ti = &sc->sc_tinfo[target];
    650 	bus_space_tag_t iot = sc->sc_iot;
    651 	bus_space_handle_t ioh = sc->sc_ioh;
    652 
    653 	SPC_TRACE(("spc_select  "));
    654 	spc_setsync(sc, ti);
    655 
    656 #if 0
    657 	bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ATN);
    658 #endif
    659 
    660 	bus_space_write_1(iot, ioh, PCTL, 0);
    661 	bus_space_write_1(iot, ioh, TEMP,
    662 	    (1 << sc->sc_initiator) | (1 << target));
    663 	/*
    664 	 * Setup BSY timeout (selection timeout).
    665 	 * 250ms according to the SCSI specification.
    666 	 * T = (X * 256 + 15) * Tclf * 2  (Tclf = 200ns on x68k)
    667 	 * To setup 256ms timeout,
    668 	 * 128000ns/200ns = X * 256 + 15
    669 	 * 640 - 15 = X * 256
    670 	 * X = 625 / 256
    671 	 * X = 2 + 113 / 256
    672 	 *  ==> tch = 2, tcm = 113 (correct?)
    673 	 */
    674 	/* Time to the information transfer phase start. */
    675 	/* XXX These values should be calculated from sc_freq */
    676 	bus_space_write_1(iot, ioh, TCH, 2);
    677 	bus_space_write_1(iot, ioh, TCM, 113);
    678 	bus_space_write_1(iot, ioh, TCL, 3);
    679 	bus_space_write_1(iot, ioh, SCMD, SCMD_SELECT);
    680 
    681 	sc->sc_state = SPC_SELECTING;
    682 }
    683 
    684 int
    685 spc_reselect(sc, message)
    686 	struct spc_softc *sc;
    687 	int message;
    688 {
    689 	u_char selid, target, lun;
    690 	struct spc_acb *acb;
    691 	struct scsipi_periph *periph;
    692 	struct spc_tinfo *ti;
    693 
    694 	SPC_TRACE(("spc_reselect  "));
    695 	/*
    696 	 * The SCSI chip made a snapshot of the data bus while the reselection
    697 	 * was being negotiated.  This enables us to determine which target did
    698 	 * the reselect.
    699 	 */
    700 	selid = sc->sc_selid & ~(1 << sc->sc_initiator);
    701 	if (selid & (selid - 1)) {
    702 		printf("%s: reselect with invalid selid %02x; "
    703 		    "sending DEVICE RESET\n", sc->sc_dev.dv_xname, selid);
    704 		SPC_BREAK();
    705 		goto reset;
    706 	}
    707 
    708 	/*
    709 	 * Search wait queue for disconnected cmd
    710 	 * The list should be short, so I haven't bothered with
    711 	 * any more sophisticated structures than a simple
    712 	 * singly linked list.
    713 	 */
    714 	target = ffs(selid) - 1;
    715 	lun = message & 0x07;
    716 	TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
    717 		periph = acb->xs->xs_periph;
    718 		if (periph->periph_target == target &&
    719 		    periph->periph_lun == lun)
    720 			break;
    721 	}
    722 	if (acb == NULL) {
    723 		printf("%s: reselect from target %d lun %d with no nexus; "
    724 		    "sending ABORT\n", sc->sc_dev.dv_xname, target, lun);
    725 		SPC_BREAK();
    726 		goto abort;
    727 	}
    728 
    729 	/* Make this nexus active again. */
    730 	TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    731 	sc->sc_state = SPC_CONNECTED;
    732 	sc->sc_nexus = acb;
    733 	ti = &sc->sc_tinfo[target];
    734 	ti->lubusy |= (1 << lun);
    735 	spc_setsync(sc, ti);
    736 
    737 	if (acb->flags & ACB_RESET)
    738 		spc_sched_msgout(sc, SEND_DEV_RESET);
    739 	else if (acb->flags & ACB_ABORT)
    740 		spc_sched_msgout(sc, SEND_ABORT);
    741 
    742 	/* Do an implicit RESTORE POINTERS. */
    743 	sc->sc_dp = acb->data_addr;
    744 	sc->sc_dleft = acb->data_length;
    745 	sc->sc_cp = (u_char *)&acb->scsipi_cmd;
    746 	sc->sc_cleft = acb->scsipi_cmd_length;
    747 
    748 	return (0);
    749 
    750 reset:
    751 	spc_sched_msgout(sc, SEND_DEV_RESET);
    752 	return (1);
    753 
    754 abort:
    755 	spc_sched_msgout(sc, SEND_ABORT);
    756 	return (1);
    757 }
    758 
    759 /*
    760  * Schedule a SCSI operation.  This has now been pulled out of the interrupt
    761  * handler so that we may call it from spc_scsi_cmd and spc_done.  This may
    762  * save us an unnecessary interrupt just to get things going.  Should only be
    763  * called when state == SPC_IDLE and at bio pl.
    764  */
    765 void
    766 spc_sched(sc)
    767 	struct spc_softc *sc;
    768 {
    769 	struct spc_acb *acb;
    770 	struct scsipi_periph *periph;
    771 	struct spc_tinfo *ti;
    772 
    773 	/* missing the hw, just return and wait for our hw */
    774 	if (sc->sc_flags & SPC_INACTIVE)
    775 		return;
    776 	SPC_TRACE(("spc_sched  "));
    777 	/*
    778 	 * Find first acb in ready queue that is for a target/lunit pair that
    779 	 * is not busy.
    780 	 */
    781 	TAILQ_FOREACH(acb, &sc->ready_list, chain) {
    782 		periph = acb->xs->xs_periph;
    783 		ti = &sc->sc_tinfo[periph->periph_target];
    784 		if ((ti->lubusy & (1 << periph->periph_lun)) == 0) {
    785 			SPC_MISC(("selecting %d:%d  ",
    786 			    periph->periph_target, periph->periph_lun));
    787 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    788 			sc->sc_nexus = acb;
    789 			spc_select(sc, acb);
    790 			return;
    791 		} else
    792 			SPC_MISC(("%d:%d busy\n",
    793 			    periph->periph_target, periph->periph_lun));
    794 	}
    795 	SPC_MISC(("idle  "));
    796 	/* Nothing to start; just enable reselections and wait. */
    797 }
    798 
    799 /*
    800  * POST PROCESSING OF SCSI_CMD (usually current)
    801  */
    802 void
    803 spc_done(sc, acb)
    804 	struct spc_softc *sc;
    805 	struct spc_acb *acb;
    806 {
    807 	struct scsipi_xfer *xs = acb->xs;
    808 	struct scsipi_periph *periph = xs->xs_periph;
    809 	struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
    810 
    811 	SPC_TRACE(("spc_done  "));
    812 
    813 	if (xs->error == XS_NOERROR) {
    814 		if (acb->flags & ACB_ABORT) {
    815 			xs->error = XS_DRIVER_STUFFUP;
    816 		} else {
    817 			switch (acb->target_stat) {
    818 			case SCSI_CHECK:
    819 				/* First, save the return values */
    820 				xs->resid = acb->data_length;
    821 				/* FALLTHROUGH */
    822 			case SCSI_BUSY:
    823 				xs->status = acb->target_stat;
    824 				xs->error = XS_BUSY;
    825 				break;
    826 			case SCSI_OK:
    827 				xs->resid = acb->data_length;
    828 				break;
    829 			default:
    830 				xs->error = XS_DRIVER_STUFFUP;
    831 #if SPC_DEBUG
    832 				printf("%s: spc_done: bad stat 0x%x\n",
    833 				    sc->sc_dev.dv_xname, acb->target_stat);
    834 #endif
    835 				break;
    836 			}
    837 		}
    838 	}
    839 
    840 #if SPC_DEBUG
    841 	if ((spc_debug & SPC_SHOWMISC) != 0) {
    842 		if (xs->resid != 0)
    843 			printf("resid=%d ", xs->resid);
    844 		else
    845 			printf("error=%d\n", xs->error);
    846 	}
    847 #endif
    848 
    849 	/*
    850 	 * Remove the ACB from whatever queue it happens to be on.
    851 	 */
    852 	if (acb->flags & ACB_NEXUS)
    853 		ti->lubusy &= ~(1 << periph->periph_lun);
    854 	if (acb == sc->sc_nexus) {
    855 		sc->sc_nexus = NULL;
    856 		sc->sc_state = SPC_IDLE;
    857 		spc_sched(sc);
    858 	} else
    859 		spc_dequeue(sc, acb);
    860 
    861 	spc_free_acb(sc, acb, xs->xs_control);
    862 	ti->cmds++;
    863 	scsipi_done(xs);
    864 }
    865 
    866 void
    867 spc_dequeue(sc, acb)
    868 	struct spc_softc *sc;
    869 	struct spc_acb *acb;
    870 {
    871 
    872 	SPC_TRACE(("spc_dequeue  "));
    873 	if (acb->flags & ACB_NEXUS)
    874 		TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    875 	else
    876 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    877 }
    878 
    879 /*
    880  * INTERRUPT/PROTOCOL ENGINE
    881  */
    882 
    883 /*
    884  * Precondition:
    885  * The SCSI bus is already in the MSGI phase and there is a message byte
    886  * on the bus, along with an asserted REQ signal.
    887  */
    888 void
    889 spc_msgin(sc)
    890 	struct spc_softc *sc;
    891 {
    892 	bus_space_tag_t iot = sc->sc_iot;
    893 	bus_space_handle_t ioh = sc->sc_ioh;
    894 	int n;
    895 	u_int8_t msg;
    896 
    897 	SPC_TRACE(("spc_msgin  "));
    898 
    899 	if (sc->sc_prevphase == PH_MSGIN) {
    900 		/* This is a continuation of the previous message. */
    901 		n = sc->sc_imp - sc->sc_imess;
    902 		goto nextbyte;
    903 	}
    904 
    905 	/* This is a new MESSAGE IN phase.  Clean up our state. */
    906 	sc->sc_flags &= ~SPC_DROP_MSGIN;
    907 
    908 nextmsg:
    909 	n = 0;
    910 	sc->sc_imp = &sc->sc_imess[n];
    911 
    912 nextbyte:
    913 	/*
    914 	 * Read a whole message, but don't ack the last byte.  If we reject the
    915 	 * message, we have to assert ATN during the message transfer phase
    916 	 * itself.
    917 	 */
    918 	for (;;) {
    919 #ifdef NO_MANUAL_XFER /* XXX */
    920 		if (bus_space_read_1(iot, ioh, INTS) != 0) {
    921 			/*
    922 			 * Target left MESSAGE IN, probably because it
    923 			 * a) noticed our ATN signal, or
    924 			 * b) ran out of messages.
    925 			 */
    926 			goto out;
    927 		}
    928 #endif
    929 		/* If parity error, just dump everything on the floor. */
    930 		if ((bus_space_read_1(iot, ioh, SERR) &
    931 		     (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
    932 			sc->sc_flags |= SPC_DROP_MSGIN;
    933 			spc_sched_msgout(sc, SEND_PARITY_ERROR);
    934 		}
    935 
    936 #ifdef NO_MANUAL_XFER /* XXX */
    937 		/* send TRANSFER command. */
    938 		bus_space_write_1(iot, ioh, TCH, 0);
    939 		bus_space_write_1(iot, ioh, TCM, 0);
    940 		bus_space_write_1(iot, ioh, TCL, 1);
    941 		bus_space_write_1(iot, ioh, PCTL,
    942 		    sc->sc_phase | PCTL_BFINT_ENAB);
    943 #ifdef NEED_DREQ_ON_HARDWARE_XFER
    944 		bus_space_write_1(iot, ioh, SCMD, SCMD_XFR);
    945 #else
    946 		bus_space_write_1(iot, ioh, SCMD, SCMD_XFR | SCMD_PROG_XFR);
    947 #endif
    948 		for (;;) {
    949 			if ((bus_space_read_1(iot, ioh, SSTS) &
    950 			    SSTS_DREG_EMPTY) == 0)
    951 				break;
    952 			if (bus_space_read_1(iot, ioh, INTS) != 0)
    953 				goto out;
    954 		}
    955 		msg = bus_space_read_1(iot, ioh, DREG);
    956 #else
    957 		if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_ATN) != 0)
    958 			bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ATN);
    959 
    960 		while ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) == 0) {
    961 			/* XXX needs timeout */
    962 			if ((bus_space_read_1(iot, ioh, PSNS) & PH_MASK)
    963 			     != PH_MSGIN)
    964 				/*
    965 				 * Target left MESSAGE IN, probably because it
    966 				 * a) noticed our ATN signal, or
    967 				 * b) ran out of messages.
    968 				 */
    969 				goto out;
    970 		}
    971 
    972 		bus_space_write_1(iot, ioh, PCTL, PH_MSGIN);
    973 		bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ACK);
    974 		while ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) != 0)
    975 			continue;	/* XXX needs timeout */
    976 		msg = bus_space_read_1(iot, ioh, TEMP);
    977 #endif
    978 
    979 		/* Gather incoming message bytes if needed. */
    980 		if ((sc->sc_flags & SPC_DROP_MSGIN) == 0) {
    981 			if (n >= SPC_MAX_MSG_LEN) {
    982 				sc->sc_flags |= SPC_DROP_MSGIN;
    983 				spc_sched_msgout(sc, SEND_REJECT);
    984 			} else {
    985 				*sc->sc_imp++ = msg;
    986 				n++;
    987 				/*
    988 				 * This testing is suboptimal, but most
    989 				 * messages will be of the one byte variety, so
    990 				 * it should not affect performance
    991 				 * significantly.
    992 				 */
    993 				if (n == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
    994 					break;
    995 				if (n == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
    996 					break;
    997 				if (n >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
    998 				    n == sc->sc_imess[1] + 2)
    999 					break;
   1000 			}
   1001 		}
   1002 		/*
   1003 		 * If we reach this spot we're either:
   1004 		 * a) in the middle of a multi-byte message, or
   1005 		 * b) dropping bytes.
   1006 		 */
   1007 
   1008 #ifndef NO_MANUAL_XFER /* XXX */
   1009 		/* Ack the last byte read. */
   1010 		bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ACK);
   1011 #endif
   1012 	}
   1013 
   1014 	SPC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1015 
   1016 	/* We now have a complete message.  Parse it. */
   1017 	switch (sc->sc_state) {
   1018 		struct spc_acb *acb;
   1019 		struct scsipi_periph *periph;
   1020 		struct spc_tinfo *ti;
   1021 
   1022 	case SPC_CONNECTED:
   1023 		SPC_ASSERT(sc->sc_nexus != NULL);
   1024 		acb = sc->sc_nexus;
   1025 		ti = &sc->sc_tinfo[acb->xs->xs_periph->periph_target];
   1026 
   1027 		switch (sc->sc_imess[0]) {
   1028 		case MSG_CMDCOMPLETE:
   1029 			if (sc->sc_dleft < 0) {
   1030 				periph = acb->xs->xs_periph;
   1031 				printf("%s: %d extra bytes from %d:%d\n",
   1032 				    sc->sc_dev.dv_xname, -sc->sc_dleft,
   1033 				    periph->periph_target, periph->periph_lun);
   1034 				sc->sc_dleft = 0;
   1035 			}
   1036 			acb->xs->resid = acb->data_length = sc->sc_dleft;
   1037 			sc->sc_state = SPC_CMDCOMPLETE;
   1038 			break;
   1039 
   1040 		case MSG_PARITY_ERROR:
   1041 			/* Resend the last message. */
   1042 			spc_sched_msgout(sc, sc->sc_lastmsg);
   1043 			break;
   1044 
   1045 		case MSG_MESSAGE_REJECT:
   1046 			SPC_MISC(("message rejected %02x  ", sc->sc_lastmsg));
   1047 			switch (sc->sc_lastmsg) {
   1048 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
   1049 			case SEND_IDENTIFY:
   1050 				ti->flags &= ~(DO_SYNC | DO_WIDE);
   1051 				ti->period = ti->offset = 0;
   1052 				spc_setsync(sc, ti);
   1053 				ti->width = 0;
   1054 				break;
   1055 #endif
   1056 #if SPC_USE_SYNCHRONOUS
   1057 			case SEND_SDTR:
   1058 				ti->flags &= ~DO_SYNC;
   1059 				ti->period = ti->offset = 0;
   1060 				spc_setsync(sc, ti);
   1061 				break;
   1062 #endif
   1063 #if SPC_USE_WIDE
   1064 			case SEND_WDTR:
   1065 				ti->flags &= ~DO_WIDE;
   1066 				ti->width = 0;
   1067 				break;
   1068 #endif
   1069 			case SEND_INIT_DET_ERR:
   1070 				spc_sched_msgout(sc, SEND_ABORT);
   1071 				break;
   1072 			}
   1073 			break;
   1074 
   1075 		case MSG_NOOP:
   1076 			break;
   1077 
   1078 		case MSG_DISCONNECT:
   1079 			ti->dconns++;
   1080 			sc->sc_state = SPC_DISCONNECT;
   1081 			break;
   1082 
   1083 		case MSG_SAVEDATAPOINTER:
   1084 			acb->data_addr = sc->sc_dp;
   1085 			acb->data_length = sc->sc_dleft;
   1086 			break;
   1087 
   1088 		case MSG_RESTOREPOINTERS:
   1089 			sc->sc_dp = acb->data_addr;
   1090 			sc->sc_dleft = acb->data_length;
   1091 			sc->sc_cp = (u_char *)&acb->scsipi_cmd;
   1092 			sc->sc_cleft = acb->scsipi_cmd_length;
   1093 			break;
   1094 
   1095 		case MSG_EXTENDED:
   1096 			switch (sc->sc_imess[2]) {
   1097 #if SPC_USE_SYNCHRONOUS
   1098 			case MSG_EXT_SDTR:
   1099 				if (sc->sc_imess[1] != 3)
   1100 					goto reject;
   1101 				ti->period = sc->sc_imess[3];
   1102 				ti->offset = sc->sc_imess[4];
   1103 				ti->flags &= ~DO_SYNC;
   1104 				if (ti->offset == 0) {
   1105 				} else if (ti->period < sc->sc_minsync ||
   1106 				    ti->period > sc->sc_maxsync ||
   1107 				    ti->offset > 8) {
   1108 					ti->period = ti->offset = 0;
   1109 					spc_sched_msgout(sc, SEND_SDTR);
   1110 				} else {
   1111 					scsipi_printaddr(acb->xs->xs_periph);
   1112 					printf("sync, offset %d, "
   1113 					    "period %dnsec\n",
   1114 					    ti->offset, ti->period * 4);
   1115 				}
   1116 				spc_setsync(sc, ti);
   1117 				break;
   1118 #endif
   1119 
   1120 #if SPC_USE_WIDE
   1121 			case MSG_EXT_WDTR:
   1122 				if (sc->sc_imess[1] != 2)
   1123 					goto reject;
   1124 				ti->width = sc->sc_imess[3];
   1125 				ti->flags &= ~DO_WIDE;
   1126 				if (ti->width == 0) {
   1127 				} else if (ti->width > SPC_MAX_WIDTH) {
   1128 					ti->width = 0;
   1129 					spc_sched_msgout(sc, SEND_WDTR);
   1130 				} else {
   1131 					scsipi_printaddr(acb->xs->xs_periph);
   1132 					printf("wide, width %d\n",
   1133 					    1 << (3 + ti->width));
   1134 				}
   1135 				break;
   1136 #endif
   1137 
   1138 			default:
   1139 				printf("%s: unrecognized MESSAGE EXTENDED; "
   1140 				    "sending REJECT\n", sc->sc_dev.dv_xname);
   1141 				SPC_BREAK();
   1142 				goto reject;
   1143 			}
   1144 			break;
   1145 
   1146 		default:
   1147 			printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1148 			    sc->sc_dev.dv_xname);
   1149 			SPC_BREAK();
   1150 		reject:
   1151 			spc_sched_msgout(sc, SEND_REJECT);
   1152 			break;
   1153 		}
   1154 		break;
   1155 
   1156 	case SPC_RESELECTED:
   1157 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1158 			printf("%s: reselect without IDENTIFY; "
   1159 			    "sending DEVICE RESET\n", sc->sc_dev.dv_xname);
   1160 			SPC_BREAK();
   1161 			goto reset;
   1162 		}
   1163 
   1164 		(void) spc_reselect(sc, sc->sc_imess[0]);
   1165 		break;
   1166 
   1167 	default:
   1168 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1169 		    sc->sc_dev.dv_xname);
   1170 		SPC_BREAK();
   1171 	reset:
   1172 		spc_sched_msgout(sc, SEND_DEV_RESET);
   1173 		break;
   1174 
   1175 #ifdef notdef
   1176 	abort:
   1177 		spc_sched_msgout(sc, SEND_ABORT);
   1178 		break;
   1179 #endif
   1180 	}
   1181 
   1182 #ifndef NO_MANUAL_XFER /* XXX */
   1183 	/* Ack the last message byte. */
   1184 	bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ACK);
   1185 #endif
   1186 
   1187 	/* Go get the next message, if any. */
   1188 	goto nextmsg;
   1189 
   1190 out:
   1191 	/* Ack the last message byte. */
   1192 	bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ACK);
   1193 	SPC_MISC(("n=%d imess=0x%02x  ", n, sc->sc_imess[0]));
   1194 }
   1195 
   1196 /*
   1197  * Send the highest priority, scheduled message.
   1198  */
   1199 void
   1200 spc_msgout(sc)
   1201 	struct spc_softc *sc;
   1202 {
   1203 	bus_space_tag_t iot = sc->sc_iot;
   1204 	bus_space_handle_t ioh = sc->sc_ioh;
   1205 #if SPC_USE_SYNCHRONOUS
   1206 	struct spc_tinfo *ti;
   1207 #endif
   1208 	int n;
   1209 
   1210 	SPC_TRACE(("spc_msgout  "));
   1211 
   1212 	if (sc->sc_prevphase == PH_MSGOUT) {
   1213 		if (sc->sc_omp == sc->sc_omess) {
   1214 			/*
   1215 			 * This is a retransmission.
   1216 			 *
   1217 			 * We get here if the target stayed in MESSAGE OUT
   1218 			 * phase.  Section 5.1.9.2 of the SCSI 2 spec indicates
   1219 			 * that all of the previously transmitted messages must
   1220 			 * be sent again, in the same order.  Therefore, we
   1221 			 * requeue all the previously transmitted messages, and
   1222 			 * start again from the top.  Our simple priority
   1223 			 * scheme keeps the messages in the right order.
   1224 			 */
   1225 			SPC_MISC(("retransmitting  "));
   1226 			sc->sc_msgpriq |= sc->sc_msgoutq;
   1227 			/*
   1228 			 * Set ATN.  If we're just sending a trivial 1-byte
   1229 			 * message, we'll clear ATN later on anyway.
   1230 			 */
   1231 			bus_space_write_1(iot, ioh, SCMD,
   1232 			    SCMD_SET_ATN);	/* XXX? */
   1233 		} else {
   1234 			/* This is a continuation of the previous message. */
   1235 			n = sc->sc_omp - sc->sc_omess;
   1236 			goto nextbyte;
   1237 		}
   1238 	}
   1239 
   1240 	/* No messages transmitted so far. */
   1241 	sc->sc_msgoutq = 0;
   1242 	sc->sc_lastmsg = 0;
   1243 
   1244 nextmsg:
   1245 	/* Pick up highest priority message. */
   1246 	sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
   1247 	sc->sc_msgpriq &= ~sc->sc_currmsg;
   1248 	sc->sc_msgoutq |= sc->sc_currmsg;
   1249 
   1250 	/* Build the outgoing message data. */
   1251 	switch (sc->sc_currmsg) {
   1252 	case SEND_IDENTIFY:
   1253 		SPC_ASSERT(sc->sc_nexus != NULL);
   1254 		sc->sc_omess[0] =
   1255 		    MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1);
   1256 		n = 1;
   1257 		break;
   1258 
   1259 #if SPC_USE_SYNCHRONOUS
   1260 	case SEND_SDTR:
   1261 		SPC_ASSERT(sc->sc_nexus != NULL);
   1262 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
   1263 		sc->sc_omess[4] = MSG_EXTENDED;
   1264 		sc->sc_omess[3] = MSG_EXT_SDTR_LEN;
   1265 		sc->sc_omess[2] = MSG_EXT_SDTR;
   1266 		sc->sc_omess[1] = ti->period >> 2;
   1267 		sc->sc_omess[0] = ti->offset;
   1268 		n = 5;
   1269 		break;
   1270 #endif
   1271 
   1272 #if SPC_USE_WIDE
   1273 	case SEND_WDTR:
   1274 		SPC_ASSERT(sc->sc_nexus != NULL);
   1275 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
   1276 		sc->sc_omess[3] = MSG_EXTENDED;
   1277 		sc->sc_omess[2] = MSG_EXT_WDTR_LEN;
   1278 		sc->sc_omess[1] = MSG_EXT_WDTR;
   1279 		sc->sc_omess[0] = ti->width;
   1280 		n = 4;
   1281 		break;
   1282 #endif
   1283 
   1284 	case SEND_DEV_RESET:
   1285 		sc->sc_flags |= SPC_ABORTING;
   1286 		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1287 		n = 1;
   1288 		break;
   1289 
   1290 	case SEND_REJECT:
   1291 		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1292 		n = 1;
   1293 		break;
   1294 
   1295 	case SEND_PARITY_ERROR:
   1296 		sc->sc_omess[0] = MSG_PARITY_ERROR;
   1297 		n = 1;
   1298 		break;
   1299 
   1300 	case SEND_INIT_DET_ERR:
   1301 		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1302 		n = 1;
   1303 		break;
   1304 
   1305 	case SEND_ABORT:
   1306 		sc->sc_flags |= SPC_ABORTING;
   1307 		sc->sc_omess[0] = MSG_ABORT;
   1308 		n = 1;
   1309 		break;
   1310 
   1311 	default:
   1312 		printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
   1313 		    sc->sc_dev.dv_xname);
   1314 		SPC_BREAK();
   1315 		sc->sc_omess[0] = MSG_NOOP;
   1316 		n = 1;
   1317 		break;
   1318 	}
   1319 	sc->sc_omp = &sc->sc_omess[n];
   1320 
   1321 nextbyte:
   1322 	/* Send message bytes. */
   1323 	/* send TRANSFER command. */
   1324 	bus_space_write_1(iot, ioh, TCH, n >> 16);
   1325 	bus_space_write_1(iot, ioh, TCM, n >> 8);
   1326 	bus_space_write_1(iot, ioh, TCL, n);
   1327 	bus_space_write_1(iot, ioh, PCTL, sc->sc_phase | PCTL_BFINT_ENAB);
   1328 #ifdef NEED_DREQ_ON_HARDWARE_XFER
   1329 	bus_space_write_1(iot, ioh, SCMD, SCMD_XFR);	/* XXX */
   1330 #else
   1331 	bus_space_write_1(iot, ioh, SCMD,
   1332 	    SCMD_XFR | SCMD_PROG_XFR);
   1333 #endif
   1334 	for (;;) {
   1335 		if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0)
   1336 			break;
   1337 		if (bus_space_read_1(iot, ioh, INTS) != 0)
   1338 			goto out;
   1339 	}
   1340 	for (;;) {
   1341 #if 0
   1342 		for (;;) {
   1343 			if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) != 0)
   1344 				break;
   1345 			/* Wait for REQINIT.  XXX Need timeout. */
   1346 		}
   1347 #endif
   1348 		if (bus_space_read_1(iot, ioh, INTS) != 0) {
   1349 			/*
   1350 			 * Target left MESSAGE OUT, possibly to reject
   1351 			 * our message.
   1352 			 *
   1353 			 * If this is the last message being sent, then we
   1354 			 * deassert ATN, since either the target is going to
   1355 			 * ignore this message, or it's going to ask for a
   1356 			 * retransmission via MESSAGE PARITY ERROR (in which
   1357 			 * case we reassert ATN anyway).
   1358 			 */
   1359 #if 0
   1360 			if (sc->sc_msgpriq == 0)
   1361 				bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ATN);
   1362 #endif
   1363 			goto out;
   1364 		}
   1365 
   1366 #if 0
   1367 		/* Clear ATN before last byte if this is the last message. */
   1368 		if (n == 1 && sc->sc_msgpriq == 0)
   1369 			bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ATN);
   1370 #endif
   1371 
   1372 		while ((bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_FULL) != 0)
   1373 			;
   1374 		/* Send message byte. */
   1375 		bus_space_write_1(iot, ioh, DREG, *--sc->sc_omp);
   1376 		--n;
   1377 		/* Keep track of the last message we've sent any bytes of. */
   1378 		sc->sc_lastmsg = sc->sc_currmsg;
   1379 #if 0
   1380 		/* Wait for ACK to be negated.  XXX Need timeout. */
   1381 		while ((bus_space_read_1(iot, ioh, PSNS) & ACKI) != 0)
   1382 			;
   1383 #endif
   1384 
   1385 		if (n == 0)
   1386 			break;
   1387 	}
   1388 
   1389 	/* We get here only if the entire message has been transmitted. */
   1390 	if (sc->sc_msgpriq != 0) {
   1391 		/* There are more outgoing messages. */
   1392 		goto nextmsg;
   1393 	}
   1394 
   1395 	/*
   1396 	 * The last message has been transmitted.  We need to remember the last
   1397 	 * message transmitted (in case the target switches to MESSAGE IN phase
   1398 	 * and sends a MESSAGE REJECT), and the list of messages transmitted
   1399 	 * this time around (in case the target stays in MESSAGE OUT phase to
   1400 	 * request a retransmit).
   1401 	 */
   1402 
   1403 out:
   1404 	/* Disable REQ/ACK protocol. */
   1405 	return;
   1406 }
   1407 
   1408 /*
   1409  * spc_dataout_pio: perform a data transfer using the FIFO datapath in the spc
   1410  * Precondition: The SCSI bus should be in the DOUT phase, with REQ asserted
   1411  * and ACK deasserted (i.e. waiting for a data byte)
   1412  *
   1413  * This new revision has been optimized (I tried) to make the common case fast,
   1414  * and the rarer cases (as a result) somewhat more comlex
   1415  */
   1416 int
   1417 spc_dataout_pio(sc, p, n)
   1418 	struct spc_softc *sc;
   1419 	u_char *p;
   1420 	int n;
   1421 {
   1422 	bus_space_tag_t iot = sc->sc_iot;
   1423 	bus_space_handle_t ioh = sc->sc_ioh;
   1424 	u_char intstat = 0;
   1425 	int out = 0;
   1426 #define DOUTAMOUNT 8		/* Full FIFO */
   1427 
   1428 	SPC_TRACE(("spc_dataout_pio  "));
   1429 	/* send TRANSFER command. */
   1430 	bus_space_write_1(iot, ioh, TCH, n >> 16);
   1431 	bus_space_write_1(iot, ioh, TCM, n >> 8);
   1432 	bus_space_write_1(iot, ioh, TCL, n);
   1433 	bus_space_write_1(iot, ioh, PCTL, sc->sc_phase | PCTL_BFINT_ENAB);
   1434 #ifdef NEED_DREQ_ON_HARDWARE_XFER
   1435 	bus_space_write_1(iot, ioh, SCMD, SCMD_XFR);	/* XXX */
   1436 #else
   1437 	bus_space_write_1(iot, ioh, SCMD,
   1438 	    SCMD_XFR | SCMD_PROG_XFR);	/* XXX */
   1439 #endif
   1440 	for (;;) {
   1441 		if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0)
   1442 			break;
   1443 		if (bus_space_read_1(iot, ioh, INTS) != 0)
   1444 			break;
   1445 	}
   1446 
   1447 	/*
   1448 	 * I have tried to make the main loop as tight as possible.  This
   1449 	 * means that some of the code following the loop is a bit more
   1450 	 * complex than otherwise.
   1451 	 */
   1452 	while (n > 0) {
   1453 		int xfer;
   1454 
   1455 		for (;;) {
   1456 			intstat = bus_space_read_1(iot, ioh, INTS);
   1457 			/* Wait till buffer is empty. */
   1458 			if ((bus_space_read_1(iot, ioh, SSTS) &
   1459 			    SSTS_DREG_EMPTY) != 0)
   1460 				break;
   1461 			/* Break on interrupt. */
   1462 			if (intstat != 0)
   1463 				goto phasechange;
   1464 		}
   1465 
   1466 		xfer = min(DOUTAMOUNT, n);
   1467 
   1468 		SPC_MISC(("%d> ", xfer));
   1469 
   1470 		n -= xfer;
   1471 		out += xfer;
   1472 
   1473 		bus_space_write_multi_1(iot, ioh, DREG, p, xfer);
   1474 		p += xfer;
   1475 	}
   1476 
   1477 	if (out == 0) {
   1478 		for (;;) {
   1479 			if (bus_space_read_1(iot, ioh, INTS) != 0)
   1480 				break;
   1481 		}
   1482 		SPC_MISC(("extra data  "));
   1483 	} else {
   1484 		/* See the bytes off chip */
   1485 		for (;;) {
   1486 			/* Wait till buffer is empty. */
   1487 			if ((bus_space_read_1(iot, ioh, SSTS) &
   1488 			    SSTS_DREG_EMPTY) != 0)
   1489 				break;
   1490 			intstat = bus_space_read_1(iot, ioh, INTS);
   1491 			/* Break on interrupt. */
   1492 			if (intstat != 0)
   1493 				goto phasechange;
   1494 		}
   1495 	}
   1496 
   1497 phasechange:
   1498 	/* Stop the FIFO data path. */
   1499 
   1500 	if (intstat != 0) {
   1501 		/* Some sort of phase change. */
   1502 		int amount;
   1503 
   1504 		amount = (bus_space_read_1(iot, ioh, TCH) << 16) |
   1505 		    (bus_space_read_1(iot, ioh, TCM) << 8) |
   1506 		    bus_space_read_1(iot, ioh, TCL);
   1507 		if (amount > 0) {
   1508 			out -= amount;
   1509 			SPC_MISC(("+%d ", amount));
   1510 		}
   1511 	}
   1512 
   1513 	return out;
   1514 }
   1515 
   1516 /*
   1517  * spc_datain_pio: perform data transfers using the FIFO datapath in the spc
   1518  * Precondition: The SCSI bus should be in the DIN phase, with REQ asserted
   1519  * and ACK deasserted (i.e. at least one byte is ready).
   1520  *
   1521  * For now, uses a pretty dumb algorithm, hangs around until all data has been
   1522  * transferred.  This, is OK for fast targets, but not so smart for slow
   1523  * targets which don't disconnect or for huge transfers.
   1524  */
   1525 int
   1526 spc_datain_pio(sc, p, n)
   1527 	struct spc_softc *sc;
   1528 	u_char *p;
   1529 	int n;
   1530 {
   1531 	bus_space_tag_t iot = sc->sc_iot;
   1532 	bus_space_handle_t ioh = sc->sc_ioh;
   1533 	int in = 0;
   1534 	u_int8_t intstat, sstat;
   1535 #define DINAMOUNT 8		/* Full FIFO */
   1536 
   1537 	SPC_TRACE(("spc_datain_pio  "));
   1538 	/* send TRANSFER command. */
   1539 	bus_space_write_1(iot, ioh, TCH, n >> 16);
   1540 	bus_space_write_1(iot, ioh, TCM, n >> 8);
   1541 	bus_space_write_1(iot, ioh, TCL, n);
   1542 	bus_space_write_1(iot, ioh, PCTL, sc->sc_phase | PCTL_BFINT_ENAB);
   1543 #ifdef NEED_DREQ_ON_HARDWARE_XFER
   1544 	bus_space_write_1(iot, ioh, SCMD, SCMD_XFR);	/* XXX */
   1545 #else
   1546 	bus_space_write_1(iot, ioh, SCMD,
   1547 	    SCMD_XFR | SCMD_PROG_XFR);	/* XXX */
   1548 #endif
   1549 	for (;;) {
   1550 		if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0)
   1551 			break;
   1552 		if (bus_space_read_1(iot, ioh, INTS) != 0)
   1553 			goto phasechange;
   1554 	}
   1555 
   1556 	/*
   1557 	 * We leave this loop if one or more of the following is true:
   1558 	 * a) phase != PH_DATAIN && FIFOs are empty
   1559 	 * b) reset has occurred or busfree is detected.
   1560 	 */
   1561 	while (n > 0) {
   1562 		int xfer;
   1563 
   1564 		/* Wait for fifo half full or phase mismatch */
   1565 		for (;;) {
   1566 			/* XXX needs timeout */
   1567 			intstat = bus_space_read_1(iot, ioh, INTS);
   1568 			sstat = bus_space_read_1(iot, ioh, SSTS);
   1569 			if (intstat != 0 ||
   1570 			    (sstat & SSTS_DREG_EMPTY) == 0)
   1571 				break;
   1572 		}
   1573 
   1574 #ifdef NEED_DREQ_ON_HARDWARE_XFER
   1575 		if (intstat != 0)
   1576 			goto phasechange;
   1577 #endif
   1578 
   1579 		if (sstat & SSTS_DREG_FULL) {
   1580 			xfer = DINAMOUNT;
   1581 			n -= xfer;
   1582 			in += xfer;
   1583 			bus_space_read_multi_1(iot, ioh, DREG, p, xfer);
   1584 			p += xfer;
   1585 		}
   1586 		while (n > 0 &&
   1587 		    (bus_space_read_1(iot, ioh, SSTS) & SSTS_DREG_EMPTY) == 0) {
   1588 			n--;
   1589 			in++;
   1590 			*p++ = bus_space_read_1(iot, ioh, DREG);
   1591 		}
   1592 
   1593 		if (intstat != 0)
   1594 			goto phasechange;
   1595 	}
   1596 
   1597 	/*
   1598 	 * Some SCSI-devices are rude enough to transfer more data than what
   1599 	 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
   1600 	 * requested 512.  Test for progress, i.e. real transfers.  If no real
   1601 	 * transfers have been performed (n is probably already zero) and the
   1602 	 * FIFO is not empty, waste some bytes....
   1603 	 */
   1604 	if (in == 0) {
   1605 		for (;;) {
   1606 			/* XXX needs timeout */
   1607 			if (bus_space_read_1(iot, ioh, INTS) != 0)
   1608 				break;
   1609 		}
   1610 		SPC_MISC(("extra data  "));
   1611 	}
   1612 
   1613 phasechange:
   1614 	/* Stop the FIFO data path. */
   1615 
   1616 	return in;
   1617 }
   1618 
   1619 /*
   1620  * Catch an interrupt from the adaptor
   1621  */
   1622 /*
   1623  * This is the workhorse routine of the driver.
   1624  * Deficiencies (for now):
   1625  * 1) always uses programmed I/O
   1626  */
   1627 int
   1628 spc_intr(arg)
   1629 	void *arg;
   1630 {
   1631 	struct spc_softc *sc = arg;
   1632 	bus_space_tag_t iot = sc->sc_iot;
   1633 	bus_space_handle_t ioh = sc->sc_ioh;
   1634 	u_char ints;
   1635 	struct spc_acb *acb;
   1636 	struct scsipi_periph *periph;
   1637 	struct spc_tinfo *ti;
   1638 	int n;
   1639 
   1640 	/*
   1641 	 * Disable interrupt.
   1642 	 */
   1643 	bus_space_write_1(iot, ioh, SCTL,
   1644 	    bus_space_read_1(iot, ioh, SCTL) & ~SCTL_INTR_ENAB);
   1645 
   1646 	SPC_TRACE(("spc_intr  "));
   1647 
   1648 	ints = bus_space_read_1(iot, ioh, INTS);
   1649 	if (ints == 0)
   1650 		goto out;
   1651 
   1652 	if (sc->sc_dma_done != NULL &&
   1653 	    sc->sc_state == SPC_CONNECTED &&
   1654 	    (sc->sc_flags & SPC_DOINGDMA) != 0 &&
   1655 	    (sc->sc_phase == PH_DATAOUT || sc->sc_phase == PH_DATAIN)) {
   1656 		(*sc->sc_dma_done)(sc);
   1657 	}
   1658 
   1659 loop:
   1660 	/*
   1661 	 * Loop until transfer completion.
   1662 	 */
   1663 	/*
   1664 	 * First check for abnormal conditions, such as reset.
   1665 	 */
   1666 	ints = bus_space_read_1(iot, ioh, INTS);
   1667 	SPC_MISC(("ints = 0x%x  ", ints));
   1668 
   1669 	if ((ints & INTS_RST) != 0) {
   1670 		printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
   1671 		goto reset;
   1672 	}
   1673 
   1674 	/*
   1675 	 * Check for less serious errors.
   1676 	 */
   1677 	if ((bus_space_read_1(iot, ioh, SERR) & (SERR_SCSI_PAR|SERR_SPC_PAR))
   1678 	    != 0) {
   1679 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   1680 		if (sc->sc_prevphase == PH_MSGIN) {
   1681 			sc->sc_flags |= SPC_DROP_MSGIN;
   1682 			spc_sched_msgout(sc, SEND_PARITY_ERROR);
   1683 		} else
   1684 			spc_sched_msgout(sc, SEND_INIT_DET_ERR);
   1685 	}
   1686 
   1687 	/*
   1688 	 * If we're not already busy doing something test for the following
   1689 	 * conditions:
   1690 	 * 1) We have been reselected by something
   1691 	 * 2) We have selected something successfully
   1692 	 * 3) Our selection process has timed out
   1693 	 * 4) This is really a bus free interrupt just to get a new command
   1694 	 *    going?
   1695 	 * 5) Spurious interrupt?
   1696 	 */
   1697 	switch (sc->sc_state) {
   1698 	case SPC_IDLE:
   1699 	case SPC_SELECTING:
   1700 		SPC_MISC(("ints:0x%02x ", ints));
   1701 
   1702 		if ((ints & INTS_SEL) != 0) {
   1703 			/*
   1704 			 * We don't currently support target mode.
   1705 			 */
   1706 			printf("%s: target mode selected; going to BUS FREE\n",
   1707 			    sc->sc_dev.dv_xname);
   1708 
   1709 			goto sched;
   1710 		} else if ((ints & INTS_RESEL) != 0) {
   1711 			SPC_MISC(("reselected  "));
   1712 
   1713 			/*
   1714 			 * If we're trying to select a target ourselves,
   1715 			 * push our command back into the ready list.
   1716 			 */
   1717 			if (sc->sc_state == SPC_SELECTING) {
   1718 				SPC_MISC(("backoff selector  "));
   1719 				SPC_ASSERT(sc->sc_nexus != NULL);
   1720 				acb = sc->sc_nexus;
   1721 				sc->sc_nexus = NULL;
   1722 				TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   1723 			}
   1724 
   1725 			/* Save reselection ID. */
   1726 			sc->sc_selid = bus_space_read_1(iot, ioh, TEMP);
   1727 
   1728 			sc->sc_state = SPC_RESELECTED;
   1729 		} else if ((ints & INTS_CMD_DONE) != 0) {
   1730 			SPC_MISC(("selected  "));
   1731 
   1732 			/*
   1733 			 * We have selected a target. Things to do:
   1734 			 * a) Determine what message(s) to send.
   1735 			 * b) Verify that we're still selecting the target.
   1736 			 * c) Mark device as busy.
   1737 			 */
   1738 			if (sc->sc_state != SPC_SELECTING) {
   1739 				printf("%s: selection out while idle; "
   1740 				    "resetting\n", sc->sc_dev.dv_xname);
   1741 				SPC_BREAK();
   1742 				goto reset;
   1743 			}
   1744 			SPC_ASSERT(sc->sc_nexus != NULL);
   1745 			acb = sc->sc_nexus;
   1746 			periph = acb->xs->xs_periph;
   1747 			ti = &sc->sc_tinfo[periph->periph_target];
   1748 
   1749 			sc->sc_msgpriq = SEND_IDENTIFY;
   1750 			if (acb->flags & ACB_RESET)
   1751 				sc->sc_msgpriq |= SEND_DEV_RESET;
   1752 			else if (acb->flags & ACB_ABORT)
   1753 				sc->sc_msgpriq |= SEND_ABORT;
   1754 			else {
   1755 #if SPC_USE_SYNCHRONOUS
   1756 				if ((ti->flags & DO_SYNC) != 0)
   1757 					sc->sc_msgpriq |= SEND_SDTR;
   1758 #endif
   1759 #if SPC_USE_WIDE
   1760 				if ((ti->flags & DO_WIDE) != 0)
   1761 					sc->sc_msgpriq |= SEND_WDTR;
   1762 #endif
   1763 			}
   1764 
   1765 			acb->flags |= ACB_NEXUS;
   1766 			ti->lubusy |= (1 << periph->periph_lun);
   1767 
   1768 			/* Do an implicit RESTORE POINTERS. */
   1769 			sc->sc_dp = acb->data_addr;
   1770 			sc->sc_dleft = acb->data_length;
   1771 			sc->sc_cp = (u_char *)&acb->scsipi_cmd;
   1772 			sc->sc_cleft = acb->scsipi_cmd_length;
   1773 
   1774 			/* On our first connection, schedule a timeout. */
   1775 			if ((acb->xs->xs_control & XS_CTL_POLL) == 0)
   1776 				callout_reset(&acb->xs->xs_callout,
   1777 				    mstohz(acb->timeout), spc_timeout, acb);
   1778 
   1779 			sc->sc_state = SPC_CONNECTED;
   1780 		} else if ((ints & INTS_TIMEOUT) != 0) {
   1781 			SPC_MISC(("selection timeout  "));
   1782 
   1783 			if (sc->sc_state != SPC_SELECTING) {
   1784 				printf("%s: selection timeout while idle; "
   1785 				    "resetting\n", sc->sc_dev.dv_xname);
   1786 				SPC_BREAK();
   1787 				goto reset;
   1788 			}
   1789 			SPC_ASSERT(sc->sc_nexus != NULL);
   1790 			acb = sc->sc_nexus;
   1791 
   1792 			delay(250);
   1793 
   1794 			acb->xs->error = XS_SELTIMEOUT;
   1795 			goto finish;
   1796 		} else {
   1797 			if (sc->sc_state != SPC_IDLE) {
   1798 				printf("%s: BUS FREE while not idle; "
   1799 				    "state=%d\n",
   1800 				    sc->sc_dev.dv_xname, sc->sc_state);
   1801 				SPC_BREAK();
   1802 				goto out;
   1803 			}
   1804 
   1805 			goto sched;
   1806 		}
   1807 
   1808 		/*
   1809 		 * Turn off selection stuff, and prepare to catch bus free
   1810 		 * interrupts, parity errors, and phase changes.
   1811 		 */
   1812 
   1813 		sc->sc_flags = 0;
   1814 		sc->sc_prevphase = PH_INVALID;
   1815 		goto dophase;
   1816 	}
   1817 
   1818 	if ((ints & INTS_DISCON) != 0) {
   1819 		/* We've gone to BUS FREE phase. */
   1820 		/* disable disconnect interrupt */
   1821 		bus_space_write_1(iot, ioh, PCTL,
   1822 		    bus_space_read_1(iot, ioh, PCTL) & ~PCTL_BFINT_ENAB);
   1823 		/* XXX reset interrput */
   1824 		bus_space_write_1(iot, ioh, INTS, ints);
   1825 
   1826 		switch (sc->sc_state) {
   1827 		case SPC_RESELECTED:
   1828 			goto sched;
   1829 
   1830 		case SPC_CONNECTED:
   1831 			SPC_ASSERT(sc->sc_nexus != NULL);
   1832 			acb = sc->sc_nexus;
   1833 
   1834 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
   1835 			if (sc->sc_prevphase == PH_MSGOUT) {
   1836 				/*
   1837 				 * If the target went to BUS FREE phase during
   1838 				 * or immediately after sending a SDTR or WDTR
   1839 				 * message, disable negotiation.
   1840 				 */
   1841 				periph = acb->xs->xs_periph;
   1842 				ti = &sc->sc_tinfo[periph->periph_target];
   1843 				switch (sc->sc_lastmsg) {
   1844 #if SPC_USE_SYNCHRONOUS
   1845 				case SEND_SDTR:
   1846 					ti->flags &= ~DO_SYNC;
   1847 					ti->period = ti->offset = 0;
   1848 					break;
   1849 #endif
   1850 #if SPC_USE_WIDE
   1851 				case SEND_WDTR:
   1852 					ti->flags &= ~DO_WIDE;
   1853 					ti->width = 0;
   1854 					break;
   1855 #endif
   1856 				}
   1857 			}
   1858 #endif
   1859 
   1860 			if ((sc->sc_flags & SPC_ABORTING) == 0) {
   1861 				/*
   1862 				 * Section 5.1.1 of the SCSI 2 spec suggests
   1863 				 * issuing a REQUEST SENSE following an
   1864 				 * unexpected disconnect.  Some devices go into
   1865 				 * a contingent allegiance condition when
   1866 				 * disconnecting, and this is necessary to
   1867 				 * clean up their state.
   1868 				 */
   1869 				printf("%s: unexpected disconnect; "
   1870 				    "sending REQUEST SENSE\n",
   1871 				    sc->sc_dev.dv_xname);
   1872 				SPC_BREAK();
   1873 				acb->target_stat = SCSI_CHECK;
   1874 				acb->xs->error = XS_NOERROR;
   1875 				goto finish;
   1876 			}
   1877 
   1878 			acb->xs->error = XS_DRIVER_STUFFUP;
   1879 			goto finish;
   1880 
   1881 		case SPC_DISCONNECT:
   1882 			SPC_ASSERT(sc->sc_nexus != NULL);
   1883 			acb = sc->sc_nexus;
   1884 			TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1885 			sc->sc_nexus = NULL;
   1886 			goto sched;
   1887 
   1888 		case SPC_CMDCOMPLETE:
   1889 			SPC_ASSERT(sc->sc_nexus != NULL);
   1890 			acb = sc->sc_nexus;
   1891 			goto finish;
   1892 		}
   1893 	}
   1894 	else if ((ints & INTS_CMD_DONE) != 0 &&
   1895 	    sc->sc_prevphase == PH_MSGIN &&
   1896 	    sc->sc_state != SPC_CONNECTED)
   1897 		goto out;
   1898 
   1899 dophase:
   1900 #if 0
   1901 	if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) == 0) {
   1902 		/* Wait for REQINIT. */
   1903 		goto out;
   1904 	}
   1905 #else
   1906 	bus_space_write_1(iot, ioh, INTS, ints);
   1907 	ints = 0;
   1908 	while ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) == 0)
   1909 		delay(1);	/* need timeout XXX */
   1910 #endif
   1911 
   1912 	/*
   1913 	 * State transition.
   1914 	 */
   1915 	sc->sc_phase = bus_space_read_1(iot, ioh, PSNS) & PH_MASK;
   1916 #if 0
   1917 	bus_space_write_1(iot, ioh, PCTL, sc->sc_phase);
   1918 #endif
   1919 
   1920 	SPC_MISC(("phase=%d\n", sc->sc_phase));
   1921 	switch (sc->sc_phase) {
   1922 	case PH_MSGOUT:
   1923 		if (sc->sc_state != SPC_CONNECTED &&
   1924 		    sc->sc_state != SPC_RESELECTED)
   1925 			break;
   1926 		spc_msgout(sc);
   1927 		sc->sc_prevphase = PH_MSGOUT;
   1928 		goto loop;
   1929 
   1930 	case PH_MSGIN:
   1931 		if (sc->sc_state != SPC_CONNECTED &&
   1932 		    sc->sc_state != SPC_RESELECTED)
   1933 			break;
   1934 		spc_msgin(sc);
   1935 		sc->sc_prevphase = PH_MSGIN;
   1936 		goto loop;
   1937 
   1938 	case PH_CMD:
   1939 		if (sc->sc_state != SPC_CONNECTED)
   1940 			break;
   1941 #if SPC_DEBUG
   1942 		if ((spc_debug & SPC_SHOWMISC) != 0) {
   1943 			SPC_ASSERT(sc->sc_nexus != NULL);
   1944 			acb = sc->sc_nexus;
   1945 			printf("cmd=0x%02x+%d  ",
   1946 			    acb->scsipi_cmd.opcode, acb->scsipi_cmd_length - 1);
   1947 		}
   1948 #endif
   1949 		n = spc_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
   1950 		sc->sc_cp += n;
   1951 		sc->sc_cleft -= n;
   1952 		sc->sc_prevphase = PH_CMD;
   1953 		goto loop;
   1954 
   1955 	case PH_DATAOUT:
   1956 		if (sc->sc_state != SPC_CONNECTED)
   1957 			break;
   1958 		SPC_MISC(("dataout dleft=%d  ", sc->sc_dleft));
   1959 		if (sc->sc_dma_start != NULL &&
   1960 		    sc->sc_dleft > SPC_MIN_DMA_LEN) {
   1961 			(*sc->sc_dma_start)(sc, sc->sc_dp, sc->sc_dleft, 0);
   1962 			sc->sc_prevphase = PH_DATAOUT;
   1963 			goto out;
   1964 		}
   1965 		n = spc_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
   1966 		sc->sc_dp += n;
   1967 		sc->sc_dleft -= n;
   1968 		sc->sc_prevphase = PH_DATAOUT;
   1969 		goto loop;
   1970 
   1971 	case PH_DATAIN:
   1972 		if (sc->sc_state != SPC_CONNECTED)
   1973 			break;
   1974 		SPC_MISC(("datain  "));
   1975 		if (sc->sc_dma_start != NULL &&
   1976 		    sc->sc_dleft > SPC_MIN_DMA_LEN) {
   1977 			(*sc->sc_dma_start)(sc, sc->sc_dp, sc->sc_dleft, 1);
   1978 			sc->sc_prevphase = PH_DATAIN;
   1979 			goto out;
   1980 		}
   1981 		n = spc_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
   1982 		sc->sc_dp += n;
   1983 		sc->sc_dleft -= n;
   1984 		sc->sc_prevphase = PH_DATAIN;
   1985 		goto loop;
   1986 
   1987 	case PH_STAT:
   1988 		if (sc->sc_state != SPC_CONNECTED)
   1989 			break;
   1990 		SPC_ASSERT(sc->sc_nexus != NULL);
   1991 		acb = sc->sc_nexus;
   1992 
   1993 #ifdef NO_MANUAL_XFER
   1994 		spc_datain_pio(sc, &acb->target_stat, 1);
   1995 #else
   1996 		if ((bus_space_read_1(iot, ioh, PSNS) & PSNS_ATN) != 0)
   1997 			bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ATN);
   1998 		while ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) == 0)
   1999 			continue;	/* XXX needs timeout */
   2000 		bus_space_write_1(iot, ioh, PCTL, PH_STAT);
   2001 		bus_space_write_1(iot, ioh, SCMD, SCMD_SET_ACK);
   2002 		while ((bus_space_read_1(iot, ioh, PSNS) & PSNS_REQ) != 0)
   2003 			continue;	/* XXX needs timeout */
   2004 		acb->target_stat = bus_space_read_1(iot, ioh, TEMP);
   2005 		bus_space_write_1(iot, ioh, SCMD, SCMD_RST_ACK);
   2006 #endif
   2007 
   2008 		SPC_MISC(("target_stat=0x%02x  ", acb->target_stat));
   2009 		sc->sc_prevphase = PH_STAT;
   2010 		goto loop;
   2011 	}
   2012 
   2013 	printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
   2014 	SPC_BREAK();
   2015 reset:
   2016 	spc_init(sc);
   2017 	return 1;
   2018 
   2019 finish:
   2020 	callout_stop(&acb->xs->xs_callout);
   2021 	bus_space_write_1(iot, ioh, INTS, ints);
   2022 	ints = 0;
   2023 	spc_done(sc, acb);
   2024 	goto out;
   2025 
   2026 sched:
   2027 	sc->sc_state = SPC_IDLE;
   2028 	spc_sched(sc);
   2029 	goto out;
   2030 
   2031 out:
   2032 	if (ints)
   2033 		bus_space_write_1(iot, ioh, INTS, ints);
   2034 	bus_space_write_1(iot, ioh, SCTL,
   2035 	    bus_space_read_1(iot, ioh, SCTL) | SCTL_INTR_ENAB);
   2036 	return 1;
   2037 }
   2038 
   2039 void
   2040 spc_abort(sc, acb)
   2041 	struct spc_softc *sc;
   2042 	struct spc_acb *acb;
   2043 {
   2044 
   2045 	/* 2 secs for the abort */
   2046 	acb->timeout = SPC_ABORT_TIMEOUT;
   2047 	acb->flags |= ACB_ABORT;
   2048 
   2049 	if (acb == sc->sc_nexus) {
   2050 		/*
   2051 		 * If we're still selecting, the message will be scheduled
   2052 		 * after selection is complete.
   2053 		 */
   2054 		if (sc->sc_state == SPC_CONNECTED)
   2055 			spc_sched_msgout(sc, SEND_ABORT);
   2056 	} else {
   2057 		spc_dequeue(sc, acb);
   2058 		TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
   2059 		if (sc->sc_state == SPC_IDLE)
   2060 			spc_sched(sc);
   2061 	}
   2062 }
   2063 
   2064 void
   2065 spc_timeout(arg)
   2066 	void *arg;
   2067 {
   2068 	struct spc_acb *acb = arg;
   2069 	struct scsipi_xfer *xs = acb->xs;
   2070 	struct scsipi_periph *periph = xs->xs_periph;
   2071 	struct spc_softc *sc;
   2072 	int s;
   2073 
   2074 	sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
   2075 	scsipi_printaddr(periph);
   2076 	printf("timed out");
   2077 
   2078 	s = splbio();
   2079 
   2080 	if (acb->flags & ACB_ABORT) {
   2081 		/* abort timed out */
   2082 		printf(" AGAIN\n");
   2083 		/* XXX Must reset! */
   2084 	} else {
   2085 		/* abort the operation that has timed out */
   2086 		printf("\n");
   2087 		acb->xs->error = XS_TIMEOUT;
   2088 		spc_abort(sc, acb);
   2089 	}
   2090 
   2091 	splx(s);
   2092 }
   2093 
   2094 #ifdef SPC_DEBUG
   2095 /*
   2096  * The following functions are mostly used for debugging purposes, either
   2097  * directly called from the driver or from the kernel debugger.
   2098  */
   2099 
   2100 void
   2101 spc_show_scsi_cmd(acb)
   2102 	struct spc_acb *acb;
   2103 {
   2104 	u_char  *b = (u_char *)&acb->scsipi_cmd;
   2105 	int i;
   2106 
   2107 	scsipi_printaddr(acb->xs->xs_periph);
   2108 	if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
   2109 		for (i = 0; i < acb->scsipi_cmd_length; i++) {
   2110 			if (i)
   2111 				printf(",");
   2112 			printf("%x", b[i]);
   2113 		}
   2114 		printf("\n");
   2115 	} else
   2116 		printf("RESET\n");
   2117 }
   2118 
   2119 void
   2120 spc_print_acb(acb)
   2121 	struct spc_acb *acb;
   2122 {
   2123 
   2124 	printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags);
   2125 	printf(" dp=%p dleft=%d target_stat=%x\n",
   2126 	    acb->data_addr, acb->data_length, acb->target_stat);
   2127 	spc_show_scsi_cmd(acb);
   2128 }
   2129 
   2130 void
   2131 spc_print_active_acb()
   2132 {
   2133 	struct spc_acb *acb;
   2134 	struct spc_softc *sc = spc_cd.cd_devs[0]; /* XXX */
   2135 
   2136 	printf("ready list:\n");
   2137 	TAILQ_FOREACH(acb, &sc->ready_list, chain)
   2138 		spc_print_acb(acb);
   2139 	printf("nexus:\n");
   2140 	if (sc->sc_nexus != NULL)
   2141 		spc_print_acb(sc->sc_nexus);
   2142 	printf("nexus list:\n");
   2143 	TAILQ_FOREACH(acb, &sc->nexus_list, chain)
   2144 		spc_print_acb(acb);
   2145 }
   2146 
   2147 void
   2148 spc_dump89352(sc)
   2149 	struct spc_softc *sc;
   2150 {
   2151 	bus_space_tag_t iot = sc->sc_iot;
   2152 	bus_space_handle_t ioh = sc->sc_ioh;
   2153 
   2154 	printf("mb89352: BDID=%x SCTL=%x SCMD=%x TMOD=%x\n",
   2155 	    bus_space_read_1(iot, ioh, BDID),
   2156 	    bus_space_read_1(iot, ioh, SCTL),
   2157 	    bus_space_read_1(iot, ioh, SCMD),
   2158 	    bus_space_read_1(iot, ioh, TMOD));
   2159 	printf("         INTS=%x PSNS=%x SSTS=%x SERR=%x PCTL=%x\n",
   2160 	    bus_space_read_1(iot, ioh, INTS),
   2161 	    bus_space_read_1(iot, ioh, PSNS),
   2162 	    bus_space_read_1(iot, ioh, SSTS),
   2163 	    bus_space_read_1(iot, ioh, SERR),
   2164 	    bus_space_read_1(iot, ioh, PCTL));
   2165 	printf("         MBC=%x DREG=%x TEMP=%x TCH=%x TCM=%x\n",
   2166 	    bus_space_read_1(iot, ioh, MBC),
   2167 #if 0
   2168 	    bus_space_read_1(iot, ioh, DREG),
   2169 #else
   2170 	    0,
   2171 #endif
   2172 	    bus_space_read_1(iot, ioh, TEMP),
   2173 	    bus_space_read_1(iot, ioh, TCH),
   2174 	    bus_space_read_1(iot, ioh, TCM));
   2175 	printf("         TCL=%x EXBF=%x\n",
   2176 	    bus_space_read_1(iot, ioh, TCL),
   2177 	    bus_space_read_1(iot, ioh, EXBF));
   2178 }
   2179 
   2180 void
   2181 spc_dump_driver(sc)
   2182 	struct spc_softc *sc;
   2183 {
   2184 	struct spc_tinfo *ti;
   2185 	int i;
   2186 
   2187 	printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
   2188 	printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x "
   2189 	    "currmsg=%x\n", sc->sc_state, sc->sc_imess[0],
   2190 	    sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
   2191 	for (i = 0; i < 7; i++) {
   2192 		ti = &sc->sc_tinfo[i];
   2193 		printf("tinfo%d: %d cmds %d disconnects %d timeouts",
   2194 		    i, ti->cmds, ti->dconns, ti->touts);
   2195 		printf(" %d senses flags=%x\n", ti->senses, ti->flags);
   2196 	}
   2197 }
   2198 #endif
   2199