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