xref: /netbsd-src/sys/dev/ic/osiop.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: osiop.c,v 1.33 2007/10/19 11:59:58 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1990 The Regents of the University of California.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to Berkeley by
34  * Van Jacobson of Lawrence Berkeley Laboratory.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
61  */
62 
63 /*
64  * Copyright (c) 1994 Michael L. Hitch
65  *
66  * This code is derived from software contributed to Berkeley by
67  * Van Jacobson of Lawrence Berkeley Laboratory.
68  *
69  * Redistribution and use in source and binary forms, with or without
70  * modification, are permitted provided that the following conditions
71  * are met:
72  * 1. Redistributions of source code must retain the above copyright
73  *    notice, this list of conditions and the following disclaimer.
74  * 2. Redistributions in binary form must reproduce the above copyright
75  *    notice, this list of conditions and the following disclaimer in the
76  *    documentation and/or other materials provided with the distribution.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
79  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
80  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
81  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
82  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
83  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
87  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  *
89  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
90  */
91 
92 /*
93  * MI NCR53C710 scsi adaptor driver; based on arch/amiga/dev/siop.c:
94  *	NetBSD: siop.c,v 1.43 1999/09/30 22:59:53 thorpej Exp
95  *
96  * bus_space/bus_dma'fied by Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
97  *
98  * The 53c710 datasheet is available at:
99  * http://www.lsilogic.com/techlib/techdocs/storage_stand_prod/index.html
100  */
101 
102 #include <sys/cdefs.h>
103 __KERNEL_RCSID(0, "$NetBSD: osiop.c,v 1.33 2007/10/19 11:59:58 ad Exp $");
104 
105 /* #define OSIOP_DEBUG */
106 
107 #include "opt_ddb.h"
108 
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/device.h>
112 #include <sys/malloc.h>
113 #include <sys/buf.h>
114 #include <sys/kernel.h>
115 
116 #include <uvm/uvm_extern.h>
117 
118 #include <dev/scsipi/scsi_all.h>
119 #include <dev/scsipi/scsipi_all.h>
120 #include <dev/scsipi/scsiconf.h>
121 #include <dev/scsipi/scsi_message.h>
122 
123 #include <sys/cpu.h>
124 #include <sys/bus.h>
125 
126 #include <dev/ic/osiopreg.h>
127 #include <dev/ic/osiopvar.h>
128 
129 /* 53C710 script */
130 #include <dev/microcode/siop/osiop.out>
131 
132 void osiop_attach(struct osiop_softc *);
133 void osiop_minphys(struct buf *);
134 void osiop_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
135     void *);
136 void osiop_poll(struct osiop_softc *, struct osiop_acb *);
137 void osiop_sched(struct osiop_softc *);
138 void osiop_scsidone(struct osiop_acb *, int);
139 void osiop_abort(struct osiop_softc *, const char *);
140 void osiop_init(struct osiop_softc *);
141 void osiop_reset(struct osiop_softc *);
142 void osiop_resetbus(struct osiop_softc *);
143 void osiop_start(struct osiop_softc *);
144 int osiop_checkintr(struct osiop_softc *, uint8_t, uint8_t, uint8_t, int *);
145 void osiop_select(struct osiop_softc *);
146 void osiop_update_xfer_mode(struct osiop_softc *, int);
147 void scsi_period_to_osiop(struct osiop_softc *, int);
148 void osiop_timeout(void *);
149 
150 int osiop_reset_delay = 250;	/* delay after reset, in milliseconds */
151 
152 #ifdef OSIOP_DEBUG
153 #define DEBUG_DMA	0x01
154 #define DEBUG_INT	0x02
155 #define DEBUG_PHASE	0x04
156 #define DEBUG_UNEXCEPT	0x08
157 #define DEBUG_DISC	0x10
158 #define DEBUG_CMD	0x20
159 #define DEBUG_ALL	0xff
160 int osiop_debug = 0; /*DEBUG_ALL;*/
161 
162 int osiopsync_debug = 0;
163 int osiopdma_hits = 1;
164 int osiopstarts = 0;
165 int osiopints = 0;
166 int osiopphmm = 0;
167 int osiop_trix = 0;
168 #define OSIOP_TRACE_SIZE	128
169 #define OSIOP_TRACE(a,b,c,d)	do {				\
170 	osiop_trbuf[osiop_trix + 0] = (a);			\
171 	osiop_trbuf[osiop_trix + 1] = (b);			\
172 	osiop_trbuf[osiop_trix + 2] = (c);			\
173 	osiop_trbuf[osiop_trix + 3] = (d);			\
174 	osiop_trix = (osiop_trix + 4) & (OSIOP_TRACE_SIZE - 1);	\
175 } while (0)
176 uint8_t osiop_trbuf[OSIOP_TRACE_SIZE];
177 void osiop_dump_trace(void);
178 void osiop_dump_acb(struct osiop_acb *);
179 void osiop_dump(struct osiop_softc *);
180 #else
181 #define OSIOP_TRACE(a,b,c,d)
182 #endif
183 
184 void
185 osiop_attach(struct osiop_softc *sc)
186 {
187 	struct osiop_acb *acb;
188 	bus_dma_segment_t seg;
189 	int nseg;
190 	int i, err;
191 
192 	/*
193 	 * Allocate and map DMA-safe memory for the script.
194 	 */
195 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
196 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
197 	if (err) {
198 		printf(": failed to allocate script memory, err=%d\n", err);
199 		return;
200 	}
201 	err = bus_dmamem_map(sc->sc_dmat, &seg, nseg, PAGE_SIZE,
202 	    (void **)&sc->sc_script, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
203 	if (err) {
204 		printf(": failed to map script memory, err=%d\n", err);
205 		return;
206 	}
207 	err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
208 	    BUS_DMA_NOWAIT, &sc->sc_scrdma);
209 	if (err) {
210 		printf(": failed to create script map, err=%d\n", err);
211 		return;
212 	}
213 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_scrdma,
214 	    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
215 	if (err) {
216 		printf(": failed to load script map, err=%d\n", err);
217 		return;
218 	}
219 
220 	/*
221 	 * Copy and sync script
222 	 */
223 	memcpy(sc->sc_script, osiop_script, sizeof(osiop_script));
224 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scrdma, 0, sizeof(osiop_script),
225 	    BUS_DMASYNC_PREWRITE);
226 
227 	/*
228 	 * Allocate and map DMA-safe memory for the script data structure.
229 	 */
230 	err = bus_dmamem_alloc(sc->sc_dmat,
231 	    sizeof(struct osiop_ds) * OSIOP_NACB, PAGE_SIZE, 0,
232 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
233 	if (err) {
234 		printf(": failed to allocate ds memory, err=%d\n", err);
235 		return;
236 	}
237 	err = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
238 	    sizeof(struct osiop_ds) * OSIOP_NACB, (void **)&sc->sc_ds,
239 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
240 	if (err) {
241 		printf(": failed to map ds memory, err=%d\n", err);
242 		return;
243 	}
244 	err = bus_dmamap_create(sc->sc_dmat,
245 	    sizeof(struct osiop_ds) * OSIOP_NACB, 1,
246 	    sizeof(struct osiop_ds) * OSIOP_NACB, 0,
247 	    BUS_DMA_NOWAIT, &sc->sc_dsdma);
248 	if (err) {
249 		printf(": failed to create ds map, err=%d\n", err);
250 		return;
251 	}
252 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_dsdma, sc->sc_ds,
253 	    sizeof(struct osiop_ds) * OSIOP_NACB, NULL, BUS_DMA_NOWAIT);
254 	if (err) {
255 		printf(": failed to load ds map, err=%d\n", err);
256 		return;
257 	}
258 
259 	acb = malloc(sizeof(struct osiop_acb) * OSIOP_NACB,
260 	    M_DEVBUF, M_NOWAIT|M_ZERO);
261 	if (acb == NULL) {
262 		printf(": can't allocate memory for acb\n");
263 		return;
264 	}
265 	sc->sc_acb = acb;
266 	sc->sc_cfflags = device_cfdata(&sc->sc_dev)->cf_flags;
267 	sc->sc_nexus = NULL;
268 	sc->sc_active = 0;
269 	memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
270 
271 	/* Initialize command block queue */
272 	TAILQ_INIT(&sc->ready_list);
273 	TAILQ_INIT(&sc->nexus_list);
274 	TAILQ_INIT(&sc->free_list);
275 
276 	/* Initialize each command block */
277 	for (i = 0; i < OSIOP_NACB; i++) {
278 		bus_addr_t dsa;
279 
280 		err = bus_dmamap_create(sc->sc_dmat, OSIOP_MAX_XFER, OSIOP_NSG,
281 		    OSIOP_MAX_XFER, 0, BUS_DMA_NOWAIT, &acb->datadma);
282 		if (err) {
283 			printf(": failed to create datadma map, err=%d\n",
284 			    err);
285 			return;
286 		}
287 
288 		acb->sc = sc;
289 		acb->ds = &sc->sc_ds[i];
290 		acb->dsoffset = sizeof(struct osiop_ds) * i;
291 
292 		dsa = sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset;
293 		acb->ds->id.addr = dsa + OSIOP_DSIDOFF;
294 		acb->ds->cmd.addr = dsa + OSIOP_DSCMDOFF;
295 		acb->ds->status.count = 1;
296 		acb->ds->status.addr = dsa + OSIOP_DSSTATOFF;
297 		acb->ds->msg.count = 1;
298 		acb->ds->msg.addr = dsa + OSIOP_DSMSGOFF;
299 		acb->ds->msgin.count = 1;
300 		acb->ds->msgin.addr = dsa + OSIOP_DSMSGINOFF;
301 		acb->ds->extmsg.count = 1;
302 		acb->ds->extmsg.addr = dsa + OSIOP_DSEXTMSGOFF;
303 		acb->ds->synmsg.count = 3;
304 		acb->ds->synmsg.addr = dsa + OSIOP_DSSYNMSGOFF;
305 		TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
306 
307 		acb++;
308 	}
309 
310 	printf(": NCR53C710 rev %d, %dMHz, SCSI ID %d\n",
311 	    osiop_read_1(sc, OSIOP_CTEST8) >> 4, sc->sc_clock_freq, sc->sc_id);
312 
313 	/*
314 	 * Initialize all
315 	 */
316 	osiop_init(sc);
317 
318 	/*
319 	 * Fill in the adapter.
320 	 */
321 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
322 	sc->sc_adapter.adapt_nchannels = 1;
323 	sc->sc_adapter.adapt_openings = OSIOP_NACB;
324 	sc->sc_adapter.adapt_max_periph = 1;
325 	sc->sc_adapter.adapt_ioctl = NULL;
326 	sc->sc_adapter.adapt_minphys = osiop_minphys;
327 	sc->sc_adapter.adapt_request = osiop_scsipi_request;
328 
329 	/*
330 	 * Fill in the channel.
331 	 */
332 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
333 	sc->sc_channel.chan_bustype = &scsi_bustype;
334 	sc->sc_channel.chan_channel = 0;
335 	sc->sc_channel.chan_ntargets = OSIOP_NTGT;
336 	sc->sc_channel.chan_nluns = 8;
337 	sc->sc_channel.chan_id = sc->sc_id;
338 
339 	/*
340 	 * Now try to attach all the sub devices.
341 	 */
342 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
343 }
344 
345 /*
346  * default minphys routine for osiop based controllers
347  */
348 void
349 osiop_minphys(struct buf *bp)
350 {
351 
352 	if (bp->b_bcount > OSIOP_MAX_XFER)
353 		bp->b_bcount = OSIOP_MAX_XFER;
354 	minphys(bp);
355 }
356 
357 /*
358  * used by specific osiop controller
359  *
360  */
361 void
362 osiop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
363     void *arg)
364 {
365 	struct scsipi_xfer *xs;
366 	struct scsipi_periph *periph;
367 	struct osiop_acb *acb;
368 	struct osiop_softc *sc;
369 	int err, flags, s;
370 
371 	sc = (struct osiop_softc *)chan->chan_adapter->adapt_dev;
372 
373 	switch (req) {
374 	case ADAPTER_REQ_RUN_XFER:
375 		xs = arg;
376 		periph = xs->xs_periph;
377 		flags = xs->xs_control;
378 
379 		/* XXXX ?? */
380 		if (flags & XS_CTL_DATA_UIO)
381 			panic("osiop: scsi data uio requested");
382 
383 		/* XXXX ?? */
384 		if (sc->sc_nexus && flags & XS_CTL_POLL)
385 #if 0
386 			panic("osiop_scsicmd: busy");
387 #else
388 			printf("osiop_scsicmd: busy\n");
389 #endif
390 
391 		s = splbio();
392 		acb = TAILQ_FIRST(&sc->free_list);
393 		if (acb != NULL) {
394 			TAILQ_REMOVE(&sc->free_list, acb, chain);
395 		}
396 #ifdef DIAGNOSTIC
397 		else {
398 			scsipi_printaddr(periph);
399 			printf("unable to allocate acb\n");
400 			panic("osiop_scsipi_request");
401 		}
402 #endif
403 
404 		acb->status = ACB_S_READY;
405 		acb->xs = xs;
406 
407 		/* Setup DMA map for data buffer */
408 		if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
409 			err = bus_dmamap_load(sc->sc_dmat, acb->datadma,
410 			    xs->data, xs->datalen, NULL,
411 			    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
412 			    ((xs->xs_control & XS_CTL_DATA_IN) ?
413 			     BUS_DMA_READ : BUS_DMA_WRITE));
414 			if (err) {
415 				printf("%s: unable to load data DMA map: %d\n",
416 				    sc->sc_dev.dv_xname, err);
417 				xs->error = XS_DRIVER_STUFFUP;
418 				scsipi_done(xs);
419 				TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
420 				splx(s);
421 				return;
422 			}
423 		}
424 
425 		acb->cmdlen = xs->cmdlen;
426 		acb->datalen = xs->datalen;
427 #ifdef OSIOP_DEBUG
428 		acb->data = xs->data;
429 #endif
430 
431 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
432 
433 		if (sc->sc_nexus == NULL)
434 			osiop_sched(sc);
435 
436 		splx(s);
437 
438 		if (flags & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA)
439 			osiop_poll(sc, acb);
440 		return;
441 
442 	case ADAPTER_REQ_GROW_RESOURCES:
443 		return;
444 
445 	case ADAPTER_REQ_SET_XFER_MODE:
446 		{
447 			struct osiop_tinfo *ti;
448 			struct scsipi_xfer_mode *xm = arg;
449 
450 			ti = &sc->sc_tinfo[xm->xm_target];
451 
452 			if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
453 			    (ti->flags & TI_NOSYNC) == 0)
454 				ti->state = NEG_INIT;
455 
456 			/*
457 			 * If we're not going to negotiate, send the
458 			 * notification now, since it won't happen later.
459 			 */
460 			if (ti->state == NEG_DONE)
461 				osiop_update_xfer_mode(sc, xm->xm_target);
462 
463 			return;
464 		}
465 	}
466 }
467 
468 void
469 osiop_poll(struct osiop_softc *sc, struct osiop_acb *acb)
470 {
471 	struct scsipi_xfer *xs = acb->xs;
472 	int status, i, s, to;
473 	uint8_t istat, dstat, sstat0;
474 
475 	s = splbio();
476 	to = xs->timeout / 1000;
477 	if (!TAILQ_EMPTY(&sc->nexus_list))
478 		printf("%s: osiop_poll called with disconnected device\n",
479 		    sc->sc_dev.dv_xname);
480 	for (;;) {
481 		i = 1000;
482 		while (((istat = osiop_read_1(sc, OSIOP_ISTAT)) &
483 		    (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
484 			if (i <= 0) {
485 #ifdef OSIOP_DEBUG
486 				printf("waiting: tgt %d cmd %02x sbcl %02x"
487 				    " dsp %x (+%lx) dcmd %x"
488 				    " ds %p timeout %d\n",
489 				    xs->xs_periph->periph_target,
490 				    xs->cmd->opcode,
491 				    osiop_read_1(sc, OSIOP_SBCL),
492 				    osiop_read_4(sc, OSIOP_DSP),
493 				    osiop_read_4(sc, OSIOP_DSP) -
494 				        sc->sc_scrdma->dm_segs[0].ds_addr,
495 				    osiop_read_1(sc, OSIOP_DCMD),
496 				    acb->ds, acb->xs->timeout);
497 #endif
498 				i = 1000;
499 				to--;
500 				if (to <= 0) {
501 					osiop_reset(sc);
502 					splx(s);
503 					return;
504 				}
505 			}
506 			delay(1000);
507 			i--;
508 		}
509 		sstat0 = osiop_read_1(sc, OSIOP_SSTAT0);
510 		delay(25);
511 		dstat = osiop_read_1(sc, OSIOP_DSTAT);
512 		if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
513 			if (acb != sc->sc_nexus)
514 				printf("%s: osiop_poll disconnected device"
515 				    " completed\n", sc->sc_dev.dv_xname);
516 			else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
517 				sc->sc_flags &= ~OSIOP_INTSOFF;
518 				osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
519 				osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
520 			}
521 			osiop_scsidone(sc->sc_nexus, status);
522 		}
523 
524 		if (xs->xs_status & XS_STS_DONE)
525 			break;
526 	}
527 
528 	splx(s);
529 	return;
530 }
531 
532 /*
533  * start next command that's ready
534  */
535 void
536 osiop_sched(struct osiop_softc *sc)
537 {
538 	struct scsipi_periph *periph;
539 	struct osiop_acb *acb;
540 	int i;
541 
542 #ifdef OSIOP_DEBUG
543 	if (sc->sc_nexus != NULL) {
544 		printf("%s: osiop_sched- nexus %p/%d ready %p/%d\n",
545 		    sc->sc_dev.dv_xname, sc->sc_nexus,
546 		    sc->sc_nexus->xs->xs_periph->periph_target,
547 		    TAILQ_FIRST(&sc->ready_list),
548 		    TAILQ_FIRST(&sc->ready_list)->xs->xs_periph->periph_target);
549 		return;
550 	}
551 #endif
552 	TAILQ_FOREACH(acb, &sc->ready_list, chain) {
553 		periph = acb->xs->xs_periph;
554 		i = periph->periph_target;
555 		if ((sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun)) == 0) {
556 			struct osiop_tinfo *ti;
557 
558 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
559 			sc->sc_nexus = acb;
560 			ti = &sc->sc_tinfo[i];
561 			ti->lubusy |= (1 << periph->periph_lun);
562 			break;
563 		}
564 	}
565 
566 	if (acb == NULL) {
567 #ifdef OSIOP_DEBUG
568 		printf("%s: osiop_sched didn't find ready command\n",
569 		    sc->sc_dev.dv_xname);
570 #endif
571 		return;
572 	}
573 
574 	if (acb->xs->xs_control & XS_CTL_RESET)
575 		osiop_reset(sc);
576 
577 	sc->sc_active++;
578 	osiop_select(sc);
579 }
580 
581 void
582 osiop_scsidone(struct osiop_acb *acb, int status)
583 {
584 	struct scsipi_xfer *xs;
585 	struct scsipi_periph *periph;
586 	struct osiop_softc *sc;
587 	int dosched = 0;
588 
589 #ifdef DIAGNOSTIC
590 	if (acb == NULL || acb->xs == NULL) {
591 		printf("osiop_scsidone: NULL acb or scsipi_xfer\n");
592 #if defined(OSIOP_DEBUG) && defined(DDB)
593 		Debugger();
594 #endif
595 		return;
596 	}
597 #endif
598 	xs = acb->xs;
599 	sc = acb->sc;
600 	periph = xs->xs_periph;
601 
602 #ifdef OSIOP_DEBUG
603 	if (acb->status != ACB_S_DONE)
604 		printf("%s: acb not done (status %d)\n",
605 		    sc->sc_dev.dv_xname, acb->status);
606 #endif
607 
608 	xs->status = status;
609 
610 	switch (status) {
611 	case SCSI_OK:
612 		xs->error = XS_NOERROR;
613 		break;
614 	case SCSI_BUSY:
615 		xs->error = XS_BUSY;
616 		break;
617 	case SCSI_CHECK:
618 		xs->error = XS_BUSY;
619 		break;
620 	case SCSI_OSIOP_NOCHECK:
621 		/*
622 		 * don't check status, xs->error is already valid
623 		 */
624 		break;
625 	case SCSI_OSIOP_NOSTATUS:
626 		/*
627 		 * the status byte was not updated, cmd was
628 		 * aborted
629 		 */
630 		xs->error = XS_SELTIMEOUT;
631 		break;
632 	default:
633 #ifdef OSIOP_DEBUG
634 		printf("%s: osiop_scsidone: unknown status code (0x%02x)\n",
635 		    sc->sc_dev.dv_xname, status);
636 #endif
637 		xs->error = XS_DRIVER_STUFFUP;
638 		break;
639 	}
640 
641 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
642 		bus_dmamap_sync(sc->sc_dmat, acb->datadma, 0, acb->datalen,
643 		    (xs->xs_control & XS_CTL_DATA_IN) ?
644 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
645 		bus_dmamap_unload(sc->sc_dmat, acb->datadma);
646 	}
647 
648 	/*
649 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
650 	 * a hack to figure out which queue it's on.  Note that it is *not*
651 	 * necessary to cdr down the ready queue, but we must cdr down the
652 	 * nexus queue and see if it's there, so we can mark the unit as no
653 	 * longer busy.  This code is sickening, but it works.
654 	 */
655 	if (acb == sc->sc_nexus) {
656 		sc->sc_nexus = NULL;
657 		sc->sc_tinfo[periph->periph_target].lubusy &=
658 		    ~(1 << periph->periph_lun);
659 		if (!TAILQ_EMPTY(&sc->ready_list))
660 			dosched = 1;	/* start next command */
661 		sc->sc_active--;
662 		OSIOP_TRACE('d', 'a', status, 0);
663 	} else if (sc->ready_list.tqh_last == &TAILQ_NEXT(acb, chain)) {
664 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
665 		OSIOP_TRACE('d', 'r', status, 0);
666 	} else {
667 		struct osiop_acb *acb2;
668 		TAILQ_FOREACH(acb2, &sc->nexus_list, chain) {
669 			if (acb2 == acb) {
670 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
671 				sc->sc_tinfo[periph->periph_target].lubusy &=
672 				    ~(1 << periph->periph_lun);
673 				sc->sc_active--;
674 				break;
675 			}
676 		}
677 		if (acb2 == NULL) {
678 			if (TAILQ_NEXT(acb, chain) != NULL) {
679 				TAILQ_REMOVE(&sc->ready_list, acb, chain);
680 				sc->sc_active--;
681 			} else {
682 				printf("%s: can't find matching acb\n",
683 				    sc->sc_dev.dv_xname);
684 #ifdef DDB
685 #if 0
686 				Debugger();
687 #endif
688 #endif
689 			}
690 		}
691 		OSIOP_TRACE('d', 'n', status, 0);
692 	}
693 	/* Put it on the free list. */
694 	acb->status = ACB_S_FREE;
695 	TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
696 	sc->sc_tinfo[periph->periph_target].cmds++;
697 
698 	callout_stop(&xs->xs_callout);
699 	xs->resid = 0;
700 	scsipi_done(xs);
701 
702 	if (dosched && sc->sc_nexus == NULL)
703 		osiop_sched(sc);
704 }
705 
706 void
707 osiop_abort(struct osiop_softc *sc, const char *where)
708 {
709 
710 	printf("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
711 	    sc->sc_dev.dv_xname, where,
712 	    osiop_read_1(sc, OSIOP_DSTAT),
713 	    osiop_read_1(sc, OSIOP_SSTAT0),
714 	    osiop_read_1(sc, OSIOP_SBCL));
715 
716 	/* XXX XXX XXX */
717 	if (sc->sc_active > 0) {
718 		sc->sc_active = 0;
719 	}
720 }
721 
722 void
723 osiop_init(struct osiop_softc *sc)
724 {
725 	int i, inhibit_sync, inhibit_disc;
726 
727 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
728 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
729 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
730 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
731 
732 	if (sc->sc_minsync < 25)
733 		sc->sc_minsync = 25;
734 
735 	if (sc->sc_clock_freq <= 25) {
736 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1;	/* SCLK/1 */
737 		sc->sc_tcp[0] = sc->sc_tcp[1];
738 	} else if (sc->sc_clock_freq <= 37) {
739 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1_5;	/* SCLK/1.5 */
740 		sc->sc_tcp[0] = sc->sc_tcp[2];
741 	} else if (sc->sc_clock_freq <= 50) {
742 		sc->sc_dcntl |= OSIOP_DCNTL_CF_2;	/* SCLK/2 */
743 		sc->sc_tcp[0] = sc->sc_tcp[3];
744 	} else {
745 		sc->sc_dcntl |= OSIOP_DCNTL_CF_3;	/* SCLK/3 */
746 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
747 	}
748 
749 	if ((sc->sc_cfflags & 0x10000) != 0) {
750 		sc->sc_flags |= OSIOP_NODMA;
751 #ifdef OSIOP_DEBUG
752 		printf("%s: DMA disabled; use polling\n",
753 		    sc->sc_dev.dv_xname);
754 #endif
755 	}
756 
757 	inhibit_sync = (sc->sc_cfflags & 0xff00) >> 8;	/* XXX */
758 	inhibit_disc =  sc->sc_cfflags & 0x00ff;	/* XXX */
759 #ifdef OSIOP_DEBUG
760 	if (inhibit_sync != 0)
761 		printf("%s: Inhibiting synchronous transfer: 0x%02x\n",
762 		    sc->sc_dev.dv_xname, inhibit_sync);
763 	if (inhibit_disc != 0)
764 		printf("%s: Inhibiting disconnect: 0x%02x\n",
765 		    sc->sc_dev.dv_xname, inhibit_disc);
766 #endif
767 	for (i = 0; i < OSIOP_NTGT; i++) {
768 		if (inhibit_sync & (1 << i))
769 			sc->sc_tinfo[i].flags |= TI_NOSYNC;
770 		if (inhibit_disc & (1 << i))
771 			sc->sc_tinfo[i].flags |= TI_NODISC;
772 	}
773 
774 	osiop_resetbus(sc);
775 	osiop_reset(sc);
776 }
777 
778 void
779 osiop_reset(struct osiop_softc *sc)
780 {
781 	struct osiop_acb *acb;
782 	int i, s;
783 	uint8_t stat;
784 
785 #ifdef OSIOP_DEBUG
786 	printf("%s: resetting chip\n", sc->sc_dev.dv_xname);
787 #endif
788 	if (sc->sc_flags & OSIOP_ALIVE)
789 		osiop_abort(sc, "reset");
790 
791 	s = splbio();
792 
793 	/*
794 	 * Reset the chip
795 	 * XXX - is this really needed?
796 	 */
797 
798 	/* abort current script */
799 	osiop_write_1(sc, OSIOP_ISTAT,
800 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_ABRT);
801 	/* reset chip */
802 	osiop_write_1(sc, OSIOP_ISTAT,
803 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_RST);
804 	delay(100);
805 	osiop_write_1(sc, OSIOP_ISTAT,
806 	    osiop_read_1(sc, OSIOP_ISTAT) & ~OSIOP_ISTAT_RST);
807 	delay(100);
808 
809 	/*
810 	 * Set up various chip parameters
811 	 */
812 	osiop_write_1(sc, OSIOP_SCNTL0,
813 	    OSIOP_ARB_FULL | OSIOP_SCNTL0_EPC | OSIOP_SCNTL0_EPG);
814 	osiop_write_1(sc, OSIOP_SCNTL1, OSIOP_SCNTL1_ESR);
815 	osiop_write_1(sc, OSIOP_DCNTL, sc->sc_dcntl);
816 	osiop_write_1(sc, OSIOP_DMODE, OSIOP_DMODE_BL4);
817 	/* don't enable interrupts yet */
818 	osiop_write_1(sc, OSIOP_SIEN, 0x00);
819 	osiop_write_1(sc, OSIOP_DIEN, 0x00);
820 	osiop_write_1(sc, OSIOP_SCID, OSIOP_SCID_VALUE(sc->sc_id));
821 	osiop_write_1(sc, OSIOP_DWT, 0x00);
822 	osiop_write_1(sc, OSIOP_CTEST0, osiop_read_1(sc, OSIOP_CTEST0)
823 	    | OSIOP_CTEST0_BTD | OSIOP_CTEST0_EAN);
824 	osiop_write_1(sc, OSIOP_CTEST7,
825 	    osiop_read_1(sc, OSIOP_CTEST7) | sc->sc_ctest7);
826 	osiop_write_1(sc, OSIOP_CTEST4,
827 	    osiop_read_1(sc, OSIOP_CTEST4) | sc->sc_ctest4);
828 
829 	/* will need to re-negotiate sync xfers */
830 	for (i = 0; i < OSIOP_NTGT; i++) {
831 		sc->sc_tinfo[i].state = NEG_INIT;
832 		sc->sc_tinfo[i].period = 0;
833 		sc->sc_tinfo[i].offset = 0;
834 	}
835 
836 	stat = osiop_read_1(sc, OSIOP_ISTAT);
837 	if (stat & OSIOP_ISTAT_SIP)
838 		osiop_read_1(sc, OSIOP_SSTAT0);
839 	delay(25);
840 	if (stat & OSIOP_ISTAT_DIP)
841 		osiop_read_1(sc, OSIOP_DSTAT);
842 
843 	splx(s);
844 
845 	delay(osiop_reset_delay * 1000);
846 
847 	if (sc->sc_nexus != NULL) {
848 		sc->sc_nexus->xs->error =
849 		    (sc->sc_nexus->flags & ACB_F_TIMEOUT) ?
850 		    XS_TIMEOUT : XS_RESET;
851 		sc->sc_nexus->status = ACB_S_DONE;
852 		sc->sc_nexus->flags = 0;
853 		osiop_scsidone(sc->sc_nexus, SCSI_OSIOP_NOCHECK);
854 	}
855 	while ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
856 		acb->xs->error = (acb->flags & ACB_F_TIMEOUT) ?
857 		    XS_TIMEOUT : XS_RESET;
858 		acb->status = ACB_S_DONE;
859 		acb->flags = 0;
860 		osiop_scsidone(acb, SCSI_OSIOP_NOCHECK);
861 	}
862 
863 	sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
864 	/* enable SCSI and DMA interrupts */
865 	sc->sc_sien = OSIOP_SIEN_M_A | OSIOP_SIEN_STO | /*OSIOP_SIEN_SEL |*/
866 	    OSIOP_SIEN_SGE | OSIOP_SIEN_UDC | OSIOP_SIEN_RST | OSIOP_SIEN_PAR;
867 	sc->sc_dien = OSIOP_DIEN_BF | OSIOP_DIEN_ABRT | OSIOP_DIEN_SIR |
868 	    /*OSIOP_DIEN_WTD |*/ OSIOP_DIEN_IID;
869 	osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
870 	osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
871 }
872 
873 void
874 osiop_resetbus(struct osiop_softc *sc)
875 {
876 
877 	osiop_write_1(sc, OSIOP_SIEN, 0);
878 	osiop_write_1(sc, OSIOP_SCNTL1,
879 	    osiop_read_1(sc, OSIOP_SCNTL1) | OSIOP_SCNTL1_RST);
880 	delay(25);
881 	osiop_write_1(sc, OSIOP_SCNTL1,
882 	    osiop_read_1(sc, OSIOP_SCNTL1) & ~OSIOP_SCNTL1_RST);
883 }
884 
885 /*
886  * Setup Data Storage for 53C710 and start SCRIPTS processing
887  */
888 
889 void
890 osiop_start(struct osiop_softc *sc)
891 {
892 	struct osiop_acb *acb = sc->sc_nexus;
893 	struct osiop_ds *ds = acb->ds;
894 	struct scsipi_xfer *xs = acb->xs;
895 	bus_dmamap_t dsdma = sc->sc_dsdma, datadma = acb->datadma;
896 	struct osiop_tinfo *ti;
897 	int target = xs->xs_periph->periph_target;
898 	int lun = xs->xs_periph->periph_lun;
899 	int disconnect, i;
900 
901 #ifdef OSIOP_DEBUG
902 	if (osiop_debug & DEBUG_DISC &&
903 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
904 		printf("ACK! osiop was busy: script %p dsa %p active %d\n",
905 		    sc->sc_script, acb->ds, sc->sc_active);
906 		printf("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
907 		    osiop_read_1(sc, OSIOP_ISTAT),
908 		    osiop_read_1(sc, OSIOP_SFBR),
909 		    osiop_read_1(sc, OSIOP_LCRC),
910 		    osiop_read_1(sc, OSIOP_SIEN),
911 		    osiop_read_1(sc, OSIOP_DIEN));
912 #ifdef DDB
913 #if 0
914 		Debugger();
915 #endif
916 #endif
917 	}
918 #endif
919 
920 #ifdef OSIOP_DEBUG
921 	if (acb->status != ACB_S_READY)
922 		panic("osiop_start: non-ready cmd in acb");
923 #endif
924 
925 	acb->intstat = 0;
926 
927 	/* Copy SCSI command to DMA buffer */
928 	memcpy(ds->scsipi_cmd, xs->cmd, acb->cmdlen);
929 	ds->cmd.count = acb->cmdlen;
930 
931 	ti = &sc->sc_tinfo[target];
932 	ds->scsi_addr = ((1 << 16) << target) | (ti->sxfer << 8);
933 
934 	disconnect = (xs->xs_control & XS_CTL_REQSENSE) == 0 &&
935 	    (ti->flags & TI_NODISC) == 0;
936 
937 	ds->msgout[0] = MSG_IDENTIFY(lun, disconnect);
938 	ds->id.count = 1;
939 	ds->stat[0] = SCSI_OSIOP_NOSTATUS;	/* set invalid status */
940 	ds->msgbuf[0] = ds->msgbuf[1] = MSG_INVALID;
941 	memset(&ds->data, 0, sizeof(ds->data));
942 
943 	/*
944 	 * Negotiate wide is the initial negotiation state;  since the 53c710
945 	 * doesn't do wide transfers, just begin the synchronous transfer
946 	 * negotiation here.
947 	 */
948 	if (ti->state == NEG_INIT) {
949 		if ((ti->flags & TI_NOSYNC) != 0) {
950 			ti->state = NEG_DONE;
951 			ti->sbcl = 0;
952 			ti->sxfer = 0;
953 			ti->period = 0;
954 			ti->offset = 0;
955 			osiop_update_xfer_mode(sc, target);
956 #ifdef OSIOP_DEBUG
957 			if (osiopsync_debug)
958 				printf("Forcing target %d asynchronous\n",
959 				    target);
960 #endif
961 		} else {
962 			ds->msgbuf[2] = MSG_INVALID;
963 			ds->msgout[1] = MSG_EXTENDED;
964 			ds->msgout[2] = MSG_EXT_SDTR_LEN;
965 			ds->msgout[3] = MSG_EXT_SDTR;
966 			ds->msgout[4] = sc->sc_minsync;
967 			ds->msgout[5] = OSIOP_MAX_OFFSET;
968 			ds->id.count = MSG_EXT_SDTR_LEN + 3;
969 			ti->state = NEG_WAITS;
970 #ifdef OSIOP_DEBUG
971 			if (osiopsync_debug)
972 				printf("Sending sync request to target %d\n",
973 				    target);
974 #endif
975 		}
976 	}
977 
978 	acb->curaddr = 0;
979 	acb->curlen = 0;
980 
981 	/*
982 	 * Build physical DMA addresses for scatter/gather I/O
983 	 */
984 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
985 		for (i = 0; i < datadma->dm_nsegs; i++) {
986 			ds->data[i].count = datadma->dm_segs[i].ds_len;
987 			ds->data[i].addr  = datadma->dm_segs[i].ds_addr;
988 		}
989 		/* sync xfer data buffer */
990 		bus_dmamap_sync(sc->sc_dmat, acb->datadma,
991 		    0, acb->datalen, (xs->xs_control & XS_CTL_DATA_IN) ?
992 		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
993 	}
994 
995 	/* sync script data structure */
996 	bus_dmamap_sync(sc->sc_dmat, dsdma,
997 	    acb->dsoffset, sizeof(struct osiop_ds),
998 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
999 
1000 	acb->status = ACB_S_ACTIVE;
1001 
1002 	/* handle timeout */
1003 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
1004 		int timeout = mstohz(acb->xs->timeout);
1005 		/* start expire timer */
1006 		if (timeout == 0)
1007 			timeout = 1;
1008 		callout_reset(&xs->xs_callout, timeout,
1009 		    osiop_timeout, acb);
1010 	}
1011 #ifdef OSIOP_DEBUG
1012 	if (osiop_debug & DEBUG_DISC &&
1013 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1014 		printf("ACK! osiop was busy at start: "
1015 		    "script %p dsa %p active %d\n",
1016 		    sc->sc_script, acb->ds, sc->sc_active);
1017 #ifdef DDB
1018 #if 0
1019 		Debugger();
1020 #endif
1021 #endif
1022 	}
1023 #endif
1024 	if (TAILQ_EMPTY(&sc->nexus_list)) {
1025 		if (osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON)
1026 			printf("%s: osiop_select while connected?\n",
1027 			    sc->sc_dev.dv_xname);
1028 		osiop_write_4(sc, OSIOP_TEMP, 0);
1029 		osiop_write_1(sc, OSIOP_SBCL, ti->sbcl);
1030 		osiop_write_4(sc, OSIOP_DSA,
1031 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1032 		osiop_write_4(sc, OSIOP_DSP,
1033 		    sc->sc_scrdma->dm_segs[0].ds_addr + Ent_scripts);
1034 		OSIOP_TRACE('s', 1, 0, 0);
1035 	} else {
1036 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
1037 			osiop_write_1(sc, OSIOP_ISTAT, OSIOP_ISTAT_SIGP);
1038 			OSIOP_TRACE('s', 2, 0, 0);
1039 		} else {
1040 			OSIOP_TRACE('s', 3,
1041 			    osiop_read_1(sc, OSIOP_ISTAT), 0);
1042 		}
1043 	}
1044 #ifdef OSIOP_DEBUG
1045 	osiopstarts++;
1046 #endif
1047 }
1048 
1049 /*
1050  * Process a DMA or SCSI interrupt from the 53C710 SIOP
1051  */
1052 
1053 int
1054 osiop_checkintr(struct osiop_softc *sc, uint8_t istat, uint8_t dstat,
1055     uint8_t sstat0, int *status)
1056 {
1057 	struct osiop_acb *acb = sc->sc_nexus;
1058 	struct osiop_ds *ds = NULL;	/* XXX */
1059 	bus_dmamap_t dsdma = sc->sc_dsdma;
1060 	bus_addr_t scraddr = sc->sc_scrdma->dm_segs[0].ds_addr;
1061 	int target = 0;
1062 	int dfifo, dbc, intcode, sstat1;
1063 
1064 	dfifo = osiop_read_1(sc, OSIOP_DFIFO);
1065 	dbc = osiop_read_4(sc, OSIOP_DBC) & 0x00ffffff;
1066 	sstat1 = osiop_read_1(sc, OSIOP_SSTAT1);
1067 	osiop_write_1(sc, OSIOP_CTEST8,
1068 	    osiop_read_1(sc, OSIOP_CTEST8) | OSIOP_CTEST8_CLF);
1069 	while ((osiop_read_1(sc, OSIOP_CTEST1) & OSIOP_CTEST1_FMT) !=
1070 	    OSIOP_CTEST1_FMT)
1071 		;
1072 	osiop_write_1(sc, OSIOP_CTEST8,
1073 	    osiop_read_1(sc, OSIOP_CTEST8) & ~OSIOP_CTEST8_CLF);
1074 	intcode = osiop_read_4(sc, OSIOP_DSPS);
1075 #ifdef OSIOP_DEBUG
1076 	osiopints++;
1077 	if (osiop_read_4(sc, OSIOP_DSP) != 0 &&
1078 	    (osiop_read_4(sc, OSIOP_DSP) < scraddr ||
1079 	    osiop_read_4(sc, OSIOP_DSP) >= scraddr + sizeof(osiop_script))) {
1080 		printf("%s: dsp not within script dsp %x scripts %lx:%lx",
1081 		    sc->sc_dev.dv_xname,
1082 		    osiop_read_4(sc, OSIOP_DSP),
1083 		    scraddr, scraddr + sizeof(osiop_script));
1084 		printf(" istat %x dstat %x sstat0 %x\n", istat, dstat, sstat0);
1085 #ifdef DDB
1086 		Debugger();
1087 #endif
1088 	}
1089 #endif
1090 	OSIOP_TRACE('i', dstat, istat, (istat & OSIOP_ISTAT_DIP) ?
1091 	    intcode & 0xff : sstat0);
1092 
1093 	if (acb != NULL) { /* XXX */
1094 		ds = acb->ds;
1095 		bus_dmamap_sync(sc->sc_dmat, dsdma,
1096 		    acb->dsoffset, sizeof(struct osiop_ds),
1097 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1098 #ifdef OSIOP_DEBUG
1099 		if (acb->status != ACB_S_ACTIVE)
1100 			printf("osiop_checkintr: acb not active (status %d)\n",
1101 			    acb->status);
1102 #endif
1103 	}
1104 
1105 
1106 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_ok) {
1107 		/* Normal completion status, or check condition */
1108 		struct osiop_tinfo *ti;
1109 
1110 		if (acb == NULL) {
1111 			printf("%s: COMPLETE with no active command?\n",
1112 			    sc->sc_dev.dv_xname);
1113 			goto bad_phase;
1114 		}
1115 #ifdef OSIOP_DEBUG
1116 		if (osiop_read_4(sc, OSIOP_DSA) !=
1117 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset) {
1118 			printf("osiop: invalid dsa: %x %lx\n",
1119 			    osiop_read_4(sc, OSIOP_DSA),
1120 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1121 			panic("*** osiop DSA invalid ***");
1122 		}
1123 #endif
1124 		target = acb->xs->xs_periph->periph_target;
1125 		ti = &sc->sc_tinfo[target];
1126 		if (ti->state == NEG_WAITS) {
1127 			if (ds->msgbuf[1] == MSG_INVALID)
1128 				printf("%s: target %d ignored sync request\n",
1129 				    sc->sc_dev.dv_xname, target);
1130 			else if (ds->msgbuf[1] == MSG_MESSAGE_REJECT)
1131 				printf("%s: target %d rejected sync request\n",
1132 				    sc->sc_dev.dv_xname, target);
1133 			ti->period = 0;
1134 			ti->offset = 0;
1135 			osiop_update_xfer_mode(sc, target);
1136 			ti->state = NEG_DONE;
1137 		}
1138 #ifdef OSIOP_DEBUG
1139 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1140 #if 0
1141 			printf("ACK! osiop was busy at end: "
1142 			    "script %p dsa %p\n", &osiop_script, ds);
1143 #ifdef DDB
1144 			Debugger();
1145 #endif
1146 #endif
1147 		}
1148 		if (ds->msgbuf[0] != MSG_CMDCOMPLETE)
1149 			printf("%s: message was not COMMAND COMPLETE: %02x\n",
1150 			    sc->sc_dev.dv_xname, ds->msgbuf[0]);
1151 #endif
1152 		if (!TAILQ_EMPTY(&sc->nexus_list))
1153 			osiop_write_1(sc, OSIOP_DCNTL,
1154 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1155 		*status = ds->stat[0];
1156 		acb->status = ACB_S_DONE;
1157 		return (1);
1158 	}
1159 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_syncmsg) {
1160 		if (acb == NULL) {
1161 			printf("%s: sync message with no active command?\n",
1162 			    sc->sc_dev.dv_xname);
1163 			goto bad_phase;
1164 		}
1165 		target = acb->xs->xs_periph->periph_target;
1166 		if (ds->msgbuf[1] == MSG_EXTENDED &&
1167 		    ds->msgbuf[2] == MSG_EXT_SDTR_LEN &&
1168 		    ds->msgbuf[3] == MSG_EXT_SDTR) {
1169 			struct osiop_tinfo *ti = &sc->sc_tinfo[target];
1170 #ifdef OSIOP_DEBUG
1171 			if (osiopsync_debug)
1172 				printf("sync msg in: "
1173 				    "%02x %02x %02x %02x %02x %02x\n",
1174 				    ds->msgbuf[0], ds->msgbuf[1],
1175 				    ds->msgbuf[2], ds->msgbuf[3],
1176 				    ds->msgbuf[4], ds->msgbuf[5]);
1177 #endif
1178 			ti->period = ds->msgbuf[4];
1179 			ti->offset = ds->msgbuf[5];
1180 			ti->sxfer = 0;
1181 			ti->sbcl = 0;
1182 			if (ds->msgbuf[5] != 0)
1183 				scsi_period_to_osiop(sc, target);
1184 			osiop_update_xfer_mode(sc, target);
1185 
1186 			bus_dmamap_sync(sc->sc_dmat, dsdma,
1187 			    acb->dsoffset, sizeof(struct osiop_ds),
1188 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1189 			osiop_write_1(sc, OSIOP_SXFER, ti->sxfer);
1190 			osiop_write_1(sc, OSIOP_SBCL, ti->sbcl);
1191 			if (ti->state == NEG_WAITS) {
1192 				ti->state = NEG_DONE;
1193 				osiop_write_4(sc, OSIOP_DSP,
1194 				    scraddr + Ent_clear_ack);
1195 				return (0);
1196 			}
1197 			osiop_write_1(sc, OSIOP_DCNTL,
1198 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1199 			ti->state = NEG_DONE;
1200 			return (0);
1201 		}
1202 		/* XXX - not SDTR message */
1203 	}
1204 	if (sstat0 & OSIOP_SSTAT0_M_A) {
1205 		/* Phase mismatch */
1206 #ifdef OSIOP_DEBUG
1207 		osiopphmm++;
1208 #endif
1209 		if (acb == NULL) {
1210 			printf("%s: Phase mismatch with no active command?\n",
1211 			    sc->sc_dev.dv_xname);
1212 			goto bad_phase;
1213 		}
1214 		if (acb->datalen > 0) {
1215 			int adjust = (dfifo - (dbc & 0x7f)) & 0x7f;
1216 			if (sstat1 & OSIOP_SSTAT1_ORF)
1217 				adjust++;
1218 			if (sstat1 & OSIOP_SSTAT1_OLF)
1219 				adjust++;
1220 			acb->curaddr = osiop_read_4(sc, OSIOP_DNAD) - adjust;
1221 			acb->curlen = dbc + adjust;
1222 #ifdef OSIOP_DEBUG
1223 			if (osiop_debug & DEBUG_DISC) {
1224 				printf("Phase mismatch: curaddr %lx "
1225 				    "curlen %lx dfifo %x dbc %x sstat1 %x "
1226 				    "adjust %x sbcl %x starts %d acb %p\n",
1227 				    acb->curaddr, acb->curlen, dfifo,
1228 				    dbc, sstat1, adjust,
1229 				    osiop_read_1(sc, OSIOP_SBCL),
1230 				    osiopstarts, acb);
1231 				if (ds->data[1].count != 0) {
1232 					int i;
1233 					for (i = 0; ds->data[i].count != 0; i++)
1234 						printf("chain[%d] "
1235 						    "addr %x len %x\n", i,
1236 						    ds->data[i].addr,
1237 						    ds->data[i].count);
1238 				}
1239 				bus_dmamap_sync(sc->sc_dmat, dsdma,
1240 				    acb->dsoffset, sizeof(struct osiop_ds),
1241 				    BUS_DMASYNC_PREREAD |
1242 				    BUS_DMASYNC_PREWRITE);
1243 			}
1244 #endif
1245 		}
1246 #ifdef OSIOP_DEBUG
1247 		OSIOP_TRACE('m', osiop_read_1(sc, OSIOP_SBCL),
1248 		    osiop_read_4(sc, OSIOP_DSP) >> 8,
1249 		    osiop_read_4(sc, OSIOP_DSP));
1250 		if (osiop_debug & DEBUG_PHASE)
1251 			printf("Phase mismatch: %x dsp +%lx dcmd %x\n",
1252 			    osiop_read_1(sc, OSIOP_SBCL),
1253 			    osiop_read_4(sc, OSIOP_DSP) - scraddr,
1254 			    osiop_read_4(sc, OSIOP_DBC));
1255 #endif
1256 		if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_REQ) == 0) {
1257 			printf("Phase mismatch: "
1258 			    "REQ not asserted! %02x dsp %x\n",
1259 			    osiop_read_1(sc, OSIOP_SBCL),
1260 			    osiop_read_4(sc, OSIOP_DSP));
1261 #if defined(OSIOP_DEBUG) && defined(DDB)
1262 			/*Debugger(); XXX is*/
1263 #endif
1264 		}
1265 		switch (OSIOP_PHASE(osiop_read_1(sc, OSIOP_SBCL))) {
1266 		case DATA_OUT_PHASE:
1267 		case DATA_IN_PHASE:
1268 		case STATUS_PHASE:
1269 		case COMMAND_PHASE:
1270 		case MSG_IN_PHASE:
1271 		case MSG_OUT_PHASE:
1272 			osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
1273 			break;
1274 		default:
1275 			printf("%s: invalid phase\n", sc->sc_dev.dv_xname);
1276 			goto bad_phase;
1277 		}
1278 		return (0);
1279 	}
1280 	if (sstat0 & OSIOP_SSTAT0_STO) {
1281 		/* Select timed out */
1282 		if (acb == NULL) {
1283 			printf("%s: Select timeout with no active command?\n",
1284 			    sc->sc_dev.dv_xname);
1285 			goto bad_phase;
1286 		}
1287 #ifdef OSIOP_DEBUG
1288 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1289 			printf("ACK! osiop was busy at timeout: "
1290 			    "script %p dsa %lx\n", sc->sc_script,
1291 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1292 			printf(" sbcl %x sdid %x "
1293 			    "istat %x dstat %x sstat0 %x\n",
1294 			    osiop_read_1(sc, OSIOP_SBCL),
1295 			    osiop_read_1(sc, OSIOP_SDID),
1296 			    istat, dstat, sstat0);
1297 			if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) == 0) {
1298 				printf("Yikes, it's not busy now!\n");
1299 #if 0
1300 				*status = SCSI_OSIOP_NOSTATUS;
1301 				if (!TAILQ_EMPTY(&sc->nexus_list))
1302 					osiop_write_4(sc, OSIOP_DSP,
1303 					    scraddr + Ent_wait_reselect);
1304 				return (1);
1305 #endif
1306 			}
1307 #if 0
1308 			osiop_write_1(sc, OSIOP_DCNTL,
1309 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1310 #endif
1311 #ifdef DDB
1312 			Debugger();
1313 #endif
1314 			return (0);
1315 		}
1316 #endif
1317 		acb->status = ACB_S_DONE;
1318 		*status = SCSI_OSIOP_NOSTATUS;
1319 		acb->xs->error = XS_SELTIMEOUT;
1320 		if (!TAILQ_EMPTY(&sc->nexus_list))
1321 			osiop_write_4(sc, OSIOP_DSP,
1322 			    scraddr + Ent_wait_reselect);
1323 		return (1);
1324 	}
1325 	if (acb != NULL)
1326 		target = acb->xs->xs_periph->periph_target;
1327 	else
1328 		target = sc->sc_id;
1329 	if (sstat0 & OSIOP_SSTAT0_UDC) {
1330 		printf("%s: target %d disconnected unexpectedly",
1331 		    sc->sc_dev.dv_xname, target);
1332 		if (acb == NULL)
1333 			printf("with no active command?");
1334 		printf("\n");
1335 #if 0
1336 		osiop_abort(sc, "osiop_chkintr");
1337 #endif
1338 		*status = SCSI_CHECK;
1339 		if (!TAILQ_EMPTY(&sc->nexus_list))
1340 			osiop_write_4(sc, OSIOP_DSP,
1341 			    scraddr + Ent_wait_reselect);
1342 		return (acb != NULL);
1343 	}
1344 	if (dstat & OSIOP_DSTAT_SIR &&
1345 	    (intcode == A_int_disc || intcode == A_int_disc_wodp)) {
1346 		/* Disconnect */
1347 		if (acb == NULL) {
1348 			printf("%s: Disconnect with no active command?\n",
1349 			    sc->sc_dev.dv_xname);
1350 			return (0);
1351 		}
1352 #ifdef OSIOP_DEBUG
1353 		if (osiop_debug & DEBUG_DISC) {
1354 			printf("%s: ID %02x disconnected TEMP %x (+%lx) "
1355 			    "curaddr %lx curlen %lx buf %x len %x dfifo %x "
1356 			    "dbc %x sstat1 %x starts %d acb %p\n",
1357 			    sc->sc_dev.dv_xname, 1 << target,
1358 			    osiop_read_4(sc, OSIOP_TEMP),
1359 			    (osiop_read_4(sc, OSIOP_TEMP) != 0) ?
1360 			        osiop_read_4(sc, OSIOP_TEMP) - scraddr : 0,
1361 			    acb->curaddr, acb->curlen,
1362 			    ds->data[0].addr, ds->data[0].count,
1363 			    dfifo, dbc, sstat1, osiopstarts, acb);
1364 			bus_dmamap_sync(sc->sc_dmat, dsdma,
1365 			    acb->dsoffset, sizeof(struct osiop_ds),
1366 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1367 		}
1368 #endif
1369 		/*
1370 		 * XXXX need to update curaddr/curlen to reflect
1371 		 * current data transferred.  If device disconnected in
1372 		 * the middle of a DMA block, they should already be set
1373 		 * by the phase change interrupt.  If the disconnect
1374 		 * occurs on a DMA block boundary, we have to figure out
1375 		 * which DMA block it was.
1376 		 */
1377 		if (acb->datalen > 0 &&
1378 		    osiop_read_4(sc, OSIOP_TEMP) != 0) {
1379 			long n = osiop_read_4(sc, OSIOP_TEMP) - scraddr;
1380 
1381 			if (acb->curlen != 0 &&
1382 			    acb->curlen != ds->data[0].count)
1383 				printf("%s: curaddr/curlen already set? "
1384 				    "n %lx iob %lx/%lx chain[0] %x/%x\n",
1385 				    sc->sc_dev.dv_xname, n,
1386 				    acb->curaddr, acb->curlen,
1387 				    ds->data[0].addr, ds->data[0].count);
1388 			if (n < Ent_datain)
1389 				n = (n - Ent_dataout) / 16;
1390 			else
1391 				n = (n - Ent_datain) / 16;
1392 			if (n < 0 || n >= OSIOP_NSG)
1393 				printf("TEMP invalid %ld\n", n);
1394 			else {
1395 				acb->curaddr = ds->data[n].addr;
1396 				acb->curlen = ds->data[n].count;
1397 			}
1398 #ifdef OSIOP_DEBUG
1399 			if (osiop_debug & DEBUG_DISC) {
1400 				printf("%s: TEMP offset %ld",
1401 				    sc->sc_dev.dv_xname, n);
1402 				printf(" curaddr %lx curlen %lx\n",
1403 				    acb->curaddr, acb->curlen);
1404 			}
1405 #endif
1406 		}
1407 		/*
1408 		 * If data transfer was interrupted by disconnect, curaddr
1409 		 * and curlen should reflect the point of interruption.
1410 		 * Adjust the DMA chain so that the data transfer begins
1411 		 * at the appropriate place upon reselection.
1412 		 * XXX This should only be done on save data pointer message?
1413 		 */
1414 		if (acb->curlen > 0) {
1415 			int i, j;
1416 
1417 #ifdef OSIOP_DEBUG
1418 			if (osiop_debug & DEBUG_DISC)
1419 				printf("%s: adjusting DMA chain\n",
1420 				    sc->sc_dev.dv_xname);
1421 			if (intcode == A_int_disc_wodp)
1422 				printf("%s: ID %02x disconnected "
1423 				    "without Save Data Pointers\n",
1424 				    sc->sc_dev.dv_xname, 1 << target);
1425 #endif
1426 			for (i = 0; i < OSIOP_NSG; i++) {
1427 				if (ds->data[i].count == 0)
1428 					break;
1429 				if (acb->curaddr >= ds->data[i].addr &&
1430 				    acb->curaddr <
1431 				    (ds->data[i].addr + ds->data[i].count))
1432 					break;
1433 			}
1434 			if (i >= OSIOP_NSG || ds->data[i].count == 0) {
1435 				printf("couldn't find saved data pointer: "
1436 				    "curaddr %lx curlen %lx i %d\n",
1437 				    acb->curaddr, acb->curlen, i);
1438 #ifdef DDB
1439 				Debugger();
1440 #endif
1441 			}
1442 #ifdef OSIOP_DEBUG
1443 			if (osiop_debug & DEBUG_DISC)
1444 				printf(" chain[0]: %x/%x -> %lx/%lx\n",
1445 				    ds->data[0].addr, ds->data[0].count,
1446 				    acb->curaddr, acb->curlen);
1447 #endif
1448 			ds->data[0].addr = acb->curaddr;
1449 			ds->data[0].count = acb->curlen;
1450 			for (j = 1, i = i + 1;
1451 			    i < OSIOP_NSG && ds->data[i].count > 0;
1452 			    i++, j++) {
1453 #ifdef OSIOP_DEBUG
1454 				if (osiop_debug & DEBUG_DISC)
1455 					printf("  chain[%d]: %x/%x -> %x/%x\n",
1456 					    j, ds->data[j].addr,
1457 					    ds->data[j].count,
1458 					    ds->data[i].addr,
1459 					    ds->data[i].count);
1460 #endif
1461 				ds->data[j].addr  = ds->data[i].addr;
1462 				ds->data[j].count = ds->data[i].count;
1463 			}
1464 			if (j < OSIOP_NSG) {
1465 				ds->data[j].addr  = 0;
1466 				ds->data[j].count = 0;
1467 			}
1468 			bus_dmamap_sync(sc->sc_dmat, dsdma,
1469 			    acb->dsoffset, sizeof(struct osiop_ds),
1470 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1471 		}
1472 		sc->sc_tinfo[target].dconns++;
1473 		/*
1474 		 * add nexus to waiting list
1475 		 * clear nexus
1476 		 * try to start another command for another target/lun
1477 		 */
1478 		acb->intstat = sc->sc_flags & OSIOP_INTSOFF;
1479 		TAILQ_INSERT_TAIL(&sc->nexus_list, acb, chain);
1480 		sc->sc_nexus = NULL;		/* no current device */
1481 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_wait_reselect);
1482 		/* XXXX start another command ? */
1483 		if (!TAILQ_EMPTY(&sc->ready_list))
1484 			osiop_sched(sc);
1485 		return (0);
1486 	}
1487 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_reconnect) {
1488 		int reselid = ffs(osiop_read_4(sc, OSIOP_SCRATCH) & 0xff) - 1;
1489 		int reselun = osiop_read_1(sc, OSIOP_SFBR) & 0x07;
1490 #ifdef OSIOP_DEBUG
1491 		uint8_t resmsg;
1492 #endif
1493 
1494 		/* Reconnect */
1495 		/* XXXX save current SBCL */
1496 		sc->sc_sstat1 = osiop_read_1(sc, OSIOP_SBCL);
1497 #ifdef OSIOP_DEBUG
1498 		if (osiop_debug & DEBUG_DISC)
1499 			printf("%s: target ID %02x reselected dsps %x\n",
1500 			    sc->sc_dev.dv_xname, reselid, intcode);
1501 		resmsg = osiop_read_1(sc, OSIOP_SFBR);
1502 		if (!MSG_ISIDENTIFY(resmsg))
1503 			printf("%s: Reselect message in was not identify: "
1504 			    "%02x\n", sc->sc_dev.dv_xname, resmsg);
1505 #endif
1506 		if (sc->sc_nexus != NULL) {
1507 			struct scsipi_periph *periph =
1508 			    sc->sc_nexus->xs->xs_periph;
1509 #ifdef OSIOP_DEBUG
1510 			if (osiop_debug & DEBUG_DISC)
1511 				printf("%s: reselect ID %02x w/active\n",
1512 				    sc->sc_dev.dv_xname, reselid);
1513 #endif
1514 			TAILQ_INSERT_HEAD(&sc->ready_list,
1515 			    sc->sc_nexus, chain);
1516 			sc->sc_tinfo[periph->periph_target].lubusy
1517 			    &= ~(1 << periph->periph_lun);
1518 			sc->sc_active--;
1519 		}
1520 		/*
1521 		 * locate acb of reselecting device
1522 		 * set sc->sc_nexus to acb
1523 		 */
1524 		TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
1525 			struct scsipi_periph *periph = acb->xs->xs_periph;
1526 			if (reselid != periph->periph_target ||
1527 			    reselun != periph->periph_lun) {
1528 				continue;
1529 			}
1530 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1531 			sc->sc_nexus = acb;
1532 			sc->sc_flags |= acb->intstat;
1533 			acb->intstat = 0;
1534 			osiop_write_4(sc, OSIOP_DSA,
1535 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
1536 			osiop_write_1(sc, OSIOP_SXFER,
1537 			    sc->sc_tinfo[reselid].sxfer);
1538 			osiop_write_1(sc, OSIOP_SBCL,
1539 			    sc->sc_tinfo[reselid].sbcl);
1540 			break;
1541 		}
1542 		if (acb == NULL) {
1543 			printf("%s: target ID %02x reselect nexus_list %p\n",
1544 			    sc->sc_dev.dv_xname, reselid,
1545 			    TAILQ_FIRST(&sc->nexus_list));
1546 			panic("unable to find reselecting device");
1547 		}
1548 
1549 		osiop_write_4(sc, OSIOP_TEMP, 0);
1550 		osiop_write_1(sc, OSIOP_DCNTL,
1551 		    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1552 		return (0);
1553 	}
1554 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_connect) {
1555 #ifdef OSIOP_DEBUG
1556 		uint8_t ctest2 = osiop_read_1(sc, OSIOP_CTEST2);
1557 
1558 		/* reselect was interrupted (by Sig_P or select) */
1559 		if (osiop_debug & DEBUG_DISC ||
1560 		    (ctest2 & OSIOP_CTEST2_SIGP) == 0)
1561 			printf("%s: reselect interrupted (Sig_P?) "
1562 			    "scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
1563 			    sc->sc_dev.dv_xname,
1564 			    osiop_read_1(sc, OSIOP_SCNTL1), ctest2,
1565 			    osiop_read_1(sc, OSIOP_SFBR), istat,
1566 			    osiop_read_1(sc, OSIOP_ISTAT));
1567 #endif
1568 		/* XXX assumes it was not select */
1569 		if (sc->sc_nexus == NULL) {
1570 #ifdef OSIOP_DEBUG
1571 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
1572 			    sc->sc_dev.dv_xname);
1573 #if 0
1574 			osiop_dump(sc);
1575 #ifdef DDB
1576 			Debugger();
1577 #endif
1578 #endif
1579 #endif
1580 			osiop_write_1(sc, OSIOP_DCNTL,
1581 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1582 			return (0);
1583 		}
1584 		target = sc->sc_nexus->xs->xs_periph->periph_target;
1585 		osiop_write_4(sc, OSIOP_TEMP, 0);
1586 		osiop_write_4(sc, OSIOP_DSA,
1587 		    dsdma->dm_segs[0].ds_addr + sc->sc_nexus->dsoffset);
1588 		osiop_write_1(sc, OSIOP_SXFER, sc->sc_tinfo[target].sxfer);
1589 		osiop_write_1(sc, OSIOP_SBCL, sc->sc_tinfo[target].sbcl);
1590 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_scripts);
1591 		return (0);
1592 	}
1593 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_msgin) {
1594 		/* Unrecognized message in byte */
1595 		if (acb == NULL) {
1596 			printf("%s: Bad message-in with no active command?\n",
1597 			    sc->sc_dev.dv_xname);
1598 			goto bad_phase;
1599 		}
1600 		printf("%s: Unrecognized message in data "
1601 		    "sfbr %x msg %x sbcl %x\n", sc->sc_dev.dv_xname,
1602 		    osiop_read_1(sc, OSIOP_SFBR), ds->msgbuf[1],
1603 		    osiop_read_1(sc, OSIOP_SBCL));
1604 		/* what should be done here? */
1605 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
1606 		bus_dmamap_sync(sc->sc_dmat, dsdma,
1607 		    acb->dsoffset, sizeof(struct osiop_ds),
1608 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1609 		return (0);
1610 	}
1611 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_status) {
1612 		/* Status phase wasn't followed by message in phase? */
1613 		printf("%s: Status phase not followed by message in phase? "
1614 		    "sbcl %x sbdl %x\n", sc->sc_dev.dv_xname,
1615 		    osiop_read_1(sc, OSIOP_SBCL),
1616 		    osiop_read_1(sc, OSIOP_SBDL));
1617 		if (osiop_read_1(sc, OSIOP_SBCL) == 0xa7) {
1618 			/* It is now, just continue the script? */
1619 			osiop_write_1(sc, OSIOP_DCNTL,
1620 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
1621 			return (0);
1622 		}
1623 	}
1624 	if (dstat & OSIOP_DSTAT_SIR && sstat0 == 0) {
1625 		printf("OSIOP interrupt: %x sts %x msg %x %x sbcl %x\n",
1626 		    intcode, ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
1627 		    osiop_read_1(sc, OSIOP_SBCL));
1628 		osiop_reset(sc);
1629 		*status = SCSI_OSIOP_NOSTATUS;
1630 		return (0);	/* osiop_reset has cleaned up */
1631 	}
1632 	if (sstat0 & OSIOP_SSTAT0_SGE)
1633 		printf("%s: SCSI Gross Error\n", sc->sc_dev.dv_xname);
1634 	if (sstat0 & OSIOP_SSTAT0_PAR)
1635 		printf("%s: Parity Error\n", sc->sc_dev.dv_xname);
1636 	if (dstat & OSIOP_DSTAT_IID)
1637 		printf("%s: Invalid instruction detected\n",
1638 		    sc->sc_dev.dv_xname);
1639  bad_phase:
1640 	/*
1641 	 * temporary panic for unhandled conditions
1642 	 * displays various things about the 53C710 status and registers
1643 	 * then panics.
1644 	 * XXXX need to clean this up to print out the info, reset, and continue
1645 	 */
1646 	printf("osiop_chkintr: target %x ds %p\n", target, ds);
1647 	printf("scripts %lx ds %lx dsp %x dcmd %x\n", scraddr,
1648 	    acb ? sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset : 0,
1649 	    osiop_read_4(sc, OSIOP_DSP),
1650 	    osiop_read_4(sc, OSIOP_DBC));
1651 	printf("osiop_chkintr: istat %x dstat %x sstat0 %x "
1652 	    "dsps %x dsa %x sbcl %x sts %x msg %x %x sfbr %x\n",
1653 	    istat, dstat, sstat0, intcode,
1654 	    osiop_read_4(sc, OSIOP_DSA),
1655 	    osiop_read_1(sc, OSIOP_SBCL),
1656 	    ds ? ds->stat[0] : 0,
1657 	    ds ? ds->msgbuf[0] : 0,
1658 	    ds ? ds->msgbuf[1] : 0,
1659 	    osiop_read_1(sc, OSIOP_SFBR));
1660 #ifdef OSIOP_DEBUG
1661 	if (osiop_debug & DEBUG_DMA)
1662 		panic("osiop_chkintr: **** temp ****");
1663 #ifdef DDB
1664 	Debugger();
1665 #endif
1666 #endif
1667 	osiop_reset(sc);	/* hard reset */
1668 	*status = SCSI_OSIOP_NOSTATUS;
1669 	if (acb != NULL)
1670 		acb->status = ACB_S_DONE;
1671 	return (0);		/* osiop_reset cleaned up */
1672 }
1673 
1674 void
1675 osiop_select(struct osiop_softc *sc)
1676 {
1677 	struct osiop_acb *acb = sc->sc_nexus;
1678 
1679 #ifdef OSIOP_DEBUG
1680 	if (osiop_debug & DEBUG_CMD)
1681 		printf("%s: select ", sc->sc_dev.dv_xname);
1682 #endif
1683 
1684 	if (acb->xs->xs_control & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA) {
1685 		sc->sc_flags |= OSIOP_INTSOFF;
1686 		sc->sc_flags &= ~OSIOP_INTDEFER;
1687 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
1688 			osiop_write_1(sc, OSIOP_SIEN, 0);
1689 			osiop_write_1(sc, OSIOP_DIEN, 0);
1690 		}
1691 #if 0
1692 	} else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
1693 		sc->sc_flags &= ~OSIOP_INTSOFF;
1694 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
1695 			osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
1696 			osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
1697 		}
1698 #endif
1699 	}
1700 #ifdef OSIOP_DEBUG
1701 	if (osiop_debug & DEBUG_CMD)
1702 		printf("osiop_select: target %x cmd %02x ds %p\n",
1703 		    acb->xs->xs_periph->periph_target,
1704 		    acb->xs->cmd->opcode, sc->sc_nexus->ds);
1705 #endif
1706 
1707 	osiop_start(sc);
1708 
1709 	return;
1710 }
1711 
1712 /*
1713  * 53C710 interrupt handler
1714  */
1715 
1716 void
1717 osiop_intr(struct osiop_softc *sc)
1718 {
1719 	int status, s;
1720 	uint8_t istat, dstat, sstat0;
1721 
1722 	s = splbio();
1723 
1724 	istat = sc->sc_istat;
1725 	if ((istat & (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
1726 		splx(s);
1727 		return;
1728 	}
1729 
1730 	/* Got a valid interrupt on this device; set by MD handler */
1731 	dstat = sc->sc_dstat;
1732 	sstat0 = sc->sc_sstat0;
1733 	sc->sc_istat = 0;
1734 #ifdef OSIOP_DEBUG
1735 	if (!sc->sc_active) {
1736 		/* XXX needs sync */
1737 		printf("%s: spurious interrupt? "
1738 		    "istat %x dstat %x sstat0 %x nexus %p status %x\n",
1739 		    sc->sc_dev.dv_xname, istat, dstat, sstat0, sc->sc_nexus,
1740 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0);
1741 	}
1742 #endif
1743 
1744 #ifdef OSIOP_DEBUG
1745 	if (osiop_debug & (DEBUG_INT|DEBUG_CMD)) {
1746 		/* XXX needs sync */
1747 		printf("%s: intr istat %x dstat %x sstat0 %x dsps %x "
1748 		    "sbcl %x dsp %x dcmd %x sts %x msg %x\n",
1749 		    sc->sc_dev.dv_xname,
1750 		    istat, dstat, sstat0,
1751 		    osiop_read_4(sc, OSIOP_DSPS),
1752 		    osiop_read_1(sc, OSIOP_SBCL),
1753 		    osiop_read_4(sc, OSIOP_DSP),
1754 		    osiop_read_4(sc, OSIOP_DBC),
1755 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0,
1756 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->msgbuf[0] : 0);
1757 	}
1758 #endif
1759 	if (sc->sc_flags & OSIOP_INTDEFER) {
1760 		sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
1761 		osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
1762 		osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
1763 	}
1764 	if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
1765 #if 0
1766 		if (status == SCSI_OSIOP_NOSTATUS)
1767 			printf("osiop_intr: no valid status \n");
1768 #endif
1769 		if ((sc->sc_flags & (OSIOP_INTSOFF | OSIOP_INTDEFER)) !=
1770 		    OSIOP_INTSOFF) {
1771 #if 0
1772 			if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
1773 				struct scsipi_periph *periph;
1774 
1775 				periph = sc->sc_nexus->xs->xs_periph;
1776 				printf("%s: SCSI bus busy at completion"
1777 				    " targ %d sbcl %02x sfbr %x lcrc "
1778 				    "%02x dsp +%x\n", sc->sc_dev.dv_xname,
1779 				    periph->periphtarget,
1780 				    osiop_read_1(sc, OSIOP_SBCL),
1781 				    osiop_read_1(sc, OSIOP_SFBR),
1782 				    osiop_read_1(sc, OSIOP_LCRC),
1783 				    osiop_read_4(sc, OSIOP_DSP) -
1784 				        sc->sc_scrdma->dm_segs[0].ds_addr);
1785 			}
1786 #endif
1787 			osiop_scsidone(sc->sc_nexus, status);
1788 		}
1789 	}
1790 	splx(s);
1791 }
1792 
1793 void
1794 osiop_update_xfer_mode(struct osiop_softc *sc, int target)
1795 {
1796 	struct osiop_tinfo *tinfo = &sc->sc_tinfo[target];
1797 	struct scsipi_xfer_mode xm;
1798 
1799 	xm.xm_target = target;
1800 	xm.xm_mode = 0;
1801 	xm.xm_period = 0;
1802 	xm.xm_offset = 0;
1803 
1804 	if (tinfo->period) {
1805 		xm.xm_mode |= PERIPH_CAP_SYNC;
1806 		xm.xm_period = tinfo->period;
1807 		xm.xm_offset = tinfo->offset;
1808 	}
1809 
1810 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
1811 }
1812 
1813 void
1814 scsi_period_to_osiop(struct osiop_softc *sc, int target)
1815 {
1816 	int period, offset, sxfer, sbcl;
1817 
1818 	period = sc->sc_tinfo[target].period;
1819 	offset = sc->sc_tinfo[target].offset;
1820 	for (sbcl = 1; sbcl < 4; sbcl++) {
1821 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
1822 		if (sxfer >= 0 && sxfer <= 7)
1823 			break;
1824 	}
1825 	if (sbcl > 3) {
1826 		printf("osiop sync: unable to compute sync params "
1827 		    "for period %d ns\n", period * 4);
1828 		/*
1829 		 * XXX need to pick a value we can do and renegotiate
1830 		 */
1831 		sxfer = sbcl = 0;
1832 	} else {
1833 		sxfer = (sxfer << 4) | ((offset <= OSIOP_MAX_OFFSET) ?
1834 		    offset : OSIOP_MAX_OFFSET);
1835 #ifdef DEBUG_SYNC
1836 		printf("osiop sync: params for period %dns: sxfer %x sbcl %x",
1837 		    period * 4, sxfer, sbcl);
1838 		printf(" actual period %dns\n",
1839 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
1840 #endif
1841 	}
1842 	sc->sc_tinfo[target].sxfer = sxfer;
1843 	sc->sc_tinfo[target].sbcl = sbcl;
1844 #ifdef DEBUG_SYNC
1845 	printf("osiop sync: osiop_sxfr %02x, osiop_sbcl %02x\n", sxfer, sbcl);
1846 #endif
1847 }
1848 
1849 void
1850 osiop_timeout(void *arg)
1851 {
1852 	struct osiop_acb *acb = arg;
1853 	struct scsipi_xfer *xs = acb->xs;
1854 	struct osiop_softc *sc = acb->sc;
1855 	int s;
1856 
1857 	scsipi_printaddr(xs->xs_periph);
1858 	printf("command timeout\n");
1859 
1860 	s = splbio();
1861 	/* reset the scsi bus */
1862 	osiop_resetbus(sc);
1863 
1864 	/* deactivate callout */
1865 	callout_stop(&xs->xs_callout);
1866 	acb->flags |= ACB_F_TIMEOUT;
1867 	osiop_reset(sc);
1868 	splx(s);
1869 	return;
1870 }
1871 
1872 #ifdef OSIOP_DEBUG
1873 
1874 #if OSIOP_TRACE_SIZE
1875 void
1876 osiop_dump_trace(void)
1877 {
1878 	int i;
1879 
1880 	printf("osiop trace: next index %d\n", osiop_trix);
1881 	i = osiop_trix;
1882 	do {
1883 		printf("%3d: '%c' %02x %02x %02x\n", i,
1884 		    osiop_trbuf[i], osiop_trbuf[i + 1],
1885 		    osiop_trbuf[i + 2], osiop_trbuf[i + 3]);
1886 		i = (i + 4) & (OSIOP_TRACE_SIZE - 1);
1887 	} while (i != osiop_trix);
1888 }
1889 #endif
1890 
1891 void
1892 osiop_dump_acb(struct osiop_acb *acb)
1893 {
1894 	uint8_t *b;
1895 	int i;
1896 
1897 	printf("acb@%p ", acb);
1898 	if (acb->xs == NULL) {
1899 		printf("<unused>\n");
1900 		return;
1901 	}
1902 
1903 	b = (uint8_t *)&acb->xs->cmd;
1904 	printf("(%d:%d) status %2x cmdlen %2ld cmd ",
1905 	    acb->xs->xs_periph->periph_target,
1906 	    acb->xs->xs_periph->periph_lun, acb->status, acb->cmdlen);
1907 	for (i = acb->cmdlen; i > 0; i--)
1908 		printf(" %02x", *b++);
1909 	printf("\n");
1910 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
1911 	    acb->xs->datalen);
1912 	printf("va %p:%lx ", acb->data, acb->datalen);
1913 	printf("cur %lx:%lx\n", acb->curaddr, acb->curlen);
1914 }
1915 
1916 void
1917 osiop_dump(struct osiop_softc *sc)
1918 {
1919 	struct osiop_acb *acb;
1920 	int i, s;
1921 
1922 	s = splbio();
1923 #if OSIOP_TRACE_SIZE
1924 	osiop_dump_trace();
1925 #endif
1926 	printf("%s@%p istat %02x\n",
1927 	    sc->sc_dev.dv_xname, sc, osiop_read_1(sc, OSIOP_ISTAT));
1928 	if ((acb = TAILQ_FIRST(&sc->free_list)) != NULL) {
1929 		printf("Free list:\n");
1930 		while (acb) {
1931 			osiop_dump_acb(acb);
1932 			acb = TAILQ_NEXT(acb, chain);
1933 		}
1934 	}
1935 	if ((acb = TAILQ_FIRST(&sc->ready_list)) != NULL) {
1936 		printf("Ready list:\n");
1937 		while (acb) {
1938 			osiop_dump_acb(acb);
1939 			acb = TAILQ_NEXT(acb, chain);
1940 		}
1941 	}
1942 	if ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
1943 		printf("Nexus list:\n");
1944 		while (acb) {
1945 			osiop_dump_acb(acb);
1946 			acb = TAILQ_NEXT(acb, chain);
1947 		}
1948 	}
1949 	if (sc->sc_nexus) {
1950 		printf("Nexus:\n");
1951 		osiop_dump_acb(sc->sc_nexus);
1952 	}
1953 	for (i = 0; i < OSIOP_NTGT; i++) {
1954 		if (sc->sc_tinfo[i].cmds > 2) {
1955 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
1956 			    i, sc->sc_tinfo[i].cmds,
1957 			    sc->sc_tinfo[i].dconns,
1958 			    sc->sc_tinfo[i].lubusy);
1959 		}
1960 	}
1961 	splx(s);
1962 }
1963 #endif
1964