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