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