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