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