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