siop.c revision 1.44 1 /* $NetBSD: siop.c,v 1.44 2001/04/25 17:53:09 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1994 Michael L. Hitch
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Van Jacobson of Lawrence Berkeley Laboratory.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)siop.c 7.5 (Berkeley) 5/4/91
40 */
41
42 /*
43 * AMIGA 53C710 scsi adaptor driver
44 */
45
46 #include "opt_ddb.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/disklabel.h>
52 #include <sys/dkstat.h>
53 #include <sys/buf.h>
54 #include <sys/malloc.h>
55 #include <dev/scsipi/scsi_all.h>
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsiconf.h>
58 #include <machine/cpu.h>
59 #ifdef __m68k__
60 #include <m68k/cacheops.h>
61 #endif
62 #include <amiga/amiga/custom.h>
63 #include <amiga/amiga/isr.h>
64 #include <amiga/dev/siopreg.h>
65 #include <amiga/dev/siopvar.h>
66
67 /*
68 * SCSI delays
69 * In u-seconds, primarily for state changes on the SPC.
70 */
71 #define SCSI_CMD_WAIT 500000 /* wait per step of 'immediate' cmds */
72 #define SCSI_DATA_WAIT 500000 /* wait per data in/out step */
73 #define SCSI_INIT_WAIT 500000 /* wait per step (both) during init */
74
75 void siop_select __P((struct siop_softc *));
76 void siopabort __P((struct siop_softc *, siop_regmap_p, char *));
77 void sioperror __P((struct siop_softc *, siop_regmap_p, u_char));
78 void siopstart __P((struct siop_softc *));
79 int siop_checkintr __P((struct siop_softc *, u_char, u_char, u_char, int *));
80 void siopreset __P((struct siop_softc *));
81 void siopsetdelay __P((int));
82 void siop_scsidone __P((struct siop_acb *, int));
83 void siop_sched __P((struct siop_softc *));
84 void siop_poll __P((struct siop_softc *, struct siop_acb *));
85 void siopintr __P((struct siop_softc *));
86 void scsi_period_to_siop __P((struct siop_softc *, int));
87 void siop_start __P((struct siop_softc *, int, int, u_char *, int, u_char *, int));
88 void siop_dump_acb __P((struct siop_acb *));
89
90 /* 53C710 script */
91 const
92 #include <amiga/dev/siop_script.out>
93
94 /* default to not inhibit sync negotiation on any drive */
95 u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
96 u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3};
97 int siop_no_dma = 0;
98
99 int siop_reset_delay = 250; /* delay after reset, in milleseconds */
100
101 int siop_cmd_wait = SCSI_CMD_WAIT;
102 int siop_data_wait = SCSI_DATA_WAIT;
103 int siop_init_wait = SCSI_INIT_WAIT;
104
105 #ifdef DEBUG_SYNC
106 /*
107 * sync period transfer lookup - only valid for 66Mhz clock
108 */
109 static struct {
110 unsigned char p; /* period from sync request message */
111 unsigned char r; /* siop_period << 4 | sbcl */
112 } sync_tab[] = {
113 { 60/4, 0<<4 | 1},
114 { 76/4, 1<<4 | 1},
115 { 92/4, 2<<4 | 1},
116 { 92/4, 0<<4 | 2},
117 {108/4, 3<<4 | 1},
118 {116/4, 1<<4 | 2},
119 {120/4, 4<<4 | 1},
120 {120/4, 0<<4 | 3},
121 {136/4, 5<<4 | 1},
122 {140/4, 2<<4 | 2},
123 {152/4, 6<<4 | 1},
124 {152/4, 1<<4 | 3},
125 {164/4, 3<<4 | 2},
126 {168/4, 7<<4 | 1},
127 {180/4, 2<<4 | 3},
128 {184/4, 4<<4 | 2},
129 {208/4, 5<<4 | 2},
130 {212/4, 3<<4 | 3},
131 {232/4, 6<<4 | 2},
132 {240/4, 4<<4 | 3},
133 {256/4, 7<<4 | 2},
134 {272/4, 5<<4 | 3},
135 {300/4, 6<<4 | 3},
136 {332/4, 7<<4 | 3}
137 };
138 #endif
139
140 #ifdef DEBUG
141 /*
142 * 0x01 - full debug
143 * 0x02 - DMA chaining
144 * 0x04 - siopintr
145 * 0x08 - phase mismatch
146 * 0x10 - <not used>
147 * 0x20 - panic on unhandled exceptions
148 * 0x100 - disconnect/reselect
149 */
150 int siop_debug = 0;
151 int siopsync_debug = 0;
152 int siopdma_hits = 0;
153 int siopdma_misses = 0;
154 int siopchain_ints = 0;
155 int siopstarts = 0;
156 int siopints = 0;
157 int siopphmm = 0;
158 #define SIOP_TRACE_SIZE 128
159 #define SIOP_TRACE(a,b,c,d) \
160 siop_trbuf[siop_trix] = (a); \
161 siop_trbuf[siop_trix+1] = (b); \
162 siop_trbuf[siop_trix+2] = (c); \
163 siop_trbuf[siop_trix+3] = (d); \
164 siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1);
165 u_char siop_trbuf[SIOP_TRACE_SIZE];
166 int siop_trix;
167 void siop_dump __P((struct siop_softc *));
168 void siop_dump_trace __P((void));
169 #else
170 #define SIOP_TRACE(a,b,c,d)
171 #endif
172
173
174 /*
175 * default minphys routine for siop based controllers
176 */
177 void
178 siop_minphys(bp)
179 struct buf *bp;
180 {
181
182 /*
183 * No max transfer at this level.
184 */
185 minphys(bp);
186 }
187
188 /*
189 * used by specific siop controller
190 *
191 */
192 void
193 siop_scsipi_request(chan, req, arg)
194 struct scsipi_channel *chan;
195 scsipi_adapter_req_t req;
196 void *arg;
197 {
198 struct scsipi_xfer *xs;
199 struct scsipi_periph *periph;
200 struct siop_acb *acb;
201 struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev;
202 int flags, s;
203
204 switch (req) {
205 case ADAPTER_REQ_RUN_XFER:
206 xs = arg;
207 periph = xs->xs_periph;
208 flags = xs->xs_control;
209
210 /* XXXX ?? */
211 if (flags & XS_CTL_DATA_UIO)
212 panic("siop: scsi data uio requested");
213
214 /* XXXX ?? */
215 if (sc->sc_nexus && flags & XS_CTL_POLL)
216 /* panic("siop_scsicmd: busy");*/
217 printf("siop_scsicmd: busy\n");
218
219 s = splbio();
220 acb = sc->free_list.tqh_first;
221 if (acb) {
222 TAILQ_REMOVE(&sc->free_list, acb, chain);
223 }
224 splx(s);
225
226 #ifdef DIAGNOSTIC
227 /*
228 * This should never happen as we track the resources
229 * in the mid-layer.
230 */
231 if (acb == NULL) {
232 scsipi_printaddr(periph);
233 printf("unable to allocate acb\n");
234 panic("siop_scsipi_request");
235 }
236 #endif
237
238 acb->flags = ACB_ACTIVE;
239 acb->xs = xs;
240 bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
241 acb->clen = xs->cmdlen;
242 acb->daddr = xs->data;
243 acb->dleft = xs->datalen;
244
245 s = splbio();
246 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
247
248 if (sc->sc_nexus == NULL)
249 siop_sched(sc);
250
251 splx(s);
252
253 if (flags & XS_CTL_POLL || siop_no_dma)
254 siop_poll(sc, acb);
255 return;
256
257 case ADAPTER_REQ_GROW_RESOURCES:
258 return;
259
260 case ADAPTER_REQ_SET_XFER_MODE:
261 return;
262 }
263 }
264
265 void
266 siop_poll(sc, acb)
267 struct siop_softc *sc;
268 struct siop_acb *acb;
269 {
270 siop_regmap_p rp = sc->sc_siopp;
271 struct scsipi_xfer *xs = acb->xs;
272 int i;
273 int status;
274 u_char istat;
275 u_char dstat;
276 u_char sstat0;
277 int s;
278 int to;
279
280 s = splbio();
281 to = xs->timeout / 1000;
282 if (sc->nexus_list.tqh_first)
283 printf("%s: siop_poll called with disconnected device\n",
284 sc->sc_dev.dv_xname);
285 for (;;) {
286 /* use cmd_wait values? */
287 i = 50000;
288 /* XXX spl0(); */
289 while (((istat = rp->siop_istat) &
290 (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
291 if (--i <= 0) {
292 #ifdef DEBUG
293 printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
294 xs->xs_periph->periph_target, acb->cmd.opcode,
295 rp->siop_sbcl, rp->siop_dsp,
296 rp->siop_dsp - sc->sc_scriptspa,
297 *((long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
298 #endif
299 i = 50000;
300 --to;
301 if (to <= 0) {
302 siopreset(sc);
303 return;
304 }
305 }
306 delay(20);
307 }
308 sstat0 = rp->siop_sstat0;
309 dstat = rp->siop_dstat;
310 if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
311 if (acb != sc->sc_nexus)
312 printf("%s: siop_poll disconnected device completed\n",
313 sc->sc_dev.dv_xname);
314 else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
315 sc->sc_flags &= ~SIOP_INTSOFF;
316 rp->siop_sien = sc->sc_sien;
317 rp->siop_dien = sc->sc_dien;
318 }
319 siop_scsidone(sc->sc_nexus, status);
320 }
321
322 if (xs->xs_status & XS_STS_DONE)
323 break;
324 }
325 splx(s);
326 }
327
328 /*
329 * start next command that's ready
330 */
331 void
332 siop_sched(sc)
333 struct siop_softc *sc;
334 {
335 struct scsipi_periph *periph;
336 struct siop_acb *acb;
337 int i;
338
339 #ifdef DEBUG
340 if (sc->sc_nexus) {
341 printf("%s: siop_sched- nexus %p/%d ready %p/%d\n",
342 sc->sc_dev.dv_xname, sc->sc_nexus,
343 sc->sc_nexus->xs->xs_periph->periph_target,
344 sc->ready_list.tqh_first,
345 sc->ready_list.tqh_first->xs->xs_periph->periph_target);
346 return;
347 }
348 #endif
349 for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
350 periph = acb->xs->xs_periph;
351 i = periph->periph_target;
352 if(!(sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
353 struct siop_tinfo *ti = &sc->sc_tinfo[i];
354
355 TAILQ_REMOVE(&sc->ready_list, acb, chain);
356 sc->sc_nexus = acb;
357 periph = acb->xs->xs_periph;
358 ti = &sc->sc_tinfo[periph->periph_target];
359 ti->lubusy |= (1 << periph->periph_lun);
360 break;
361 }
362 }
363
364 if (acb == NULL) {
365 #ifdef DEBUGXXX
366 printf("%s: siop_sched didn't find ready command\n",
367 sc->sc_dev.dv_xname);
368 #endif
369 return;
370 }
371
372 if (acb->xs->xs_control & XS_CTL_RESET)
373 siopreset(sc);
374
375 #if 0
376 acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5; /* XXXX */
377 #endif
378 ++sc->sc_active;
379 siop_select(sc);
380 }
381
382 void
383 siop_scsidone(acb, stat)
384 struct siop_acb *acb;
385 int stat;
386 {
387 struct scsipi_xfer *xs;
388 struct scsipi_periph *periph;
389 struct siop_softc *sc;
390 int dosched = 0;
391
392 if (acb == NULL || (xs = acb->xs) == NULL) {
393 #ifdef DIAGNOSTIC
394 printf("siop_scsidone: NULL acb or scsipi_xfer\n");
395 #if defined(DEBUG) && defined(DDB)
396 Debugger();
397 #endif
398 #endif
399 return;
400 }
401 periph = xs->xs_periph;
402 sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
403
404 xs->status = stat;
405 xs->resid = 0; /* XXXX */
406
407 if (xs->error == XS_NOERROR) {
408 if (stat == SCSI_CHECK || stat == SCSI_BUSY)
409 xs->error = XS_BUSY;
410 }
411
412 /*
413 * Remove the ACB from whatever queue it's on. We have to do a bit of
414 * a hack to figure out which queue it's on. Note that it is *not*
415 * necessary to cdr down the ready queue, but we must cdr down the
416 * nexus queue and see if it's there, so we can mark the unit as no
417 * longer busy. This code is sickening, but it works.
418 */
419 if (acb == sc->sc_nexus) {
420 sc->sc_nexus = NULL;
421 sc->sc_tinfo[periph->periph_target].lubusy &=
422 ~(1<<periph->periph_lun);
423 if (sc->ready_list.tqh_first)
424 dosched = 1; /* start next command */
425 --sc->sc_active;
426 SIOP_TRACE('d','a',stat,0)
427 } else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
428 TAILQ_REMOVE(&sc->ready_list, acb, chain);
429 SIOP_TRACE('d','r',stat,0)
430 } else {
431 register struct siop_acb *acb2;
432 for (acb2 = sc->nexus_list.tqh_first; acb2;
433 acb2 = acb2->chain.tqe_next)
434 if (acb2 == acb) {
435 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
436 sc->sc_tinfo[periph->periph_target].lubusy
437 &= ~(1<<periph->periph_lun);
438 --sc->sc_active;
439 break;
440 }
441 if (acb2)
442 ;
443 else if (acb->chain.tqe_next) {
444 TAILQ_REMOVE(&sc->ready_list, acb, chain);
445 --sc->sc_active;
446 } else {
447 printf("%s: can't find matching acb\n",
448 sc->sc_dev.dv_xname);
449 #ifdef DDB
450 /* Debugger(); */
451 #endif
452 }
453 SIOP_TRACE('d','n',stat,0);
454 }
455 /* Put it on the free list. */
456 acb->flags = ACB_FREE;
457 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
458
459 sc->sc_tinfo[periph->periph_target].cmds++;
460
461 scsipi_done(xs);
462
463 if (dosched && sc->sc_nexus == NULL)
464 siop_sched(sc);
465 }
466
467 void
468 siopabort(sc, rp, where)
469 register struct siop_softc *sc;
470 siop_regmap_p rp;
471 char *where;
472 {
473 #ifdef fix_this
474 int i;
475 #endif
476
477 printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
478 sc->sc_dev.dv_xname,
479 where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
480
481 if (sc->sc_active > 0) {
482 #ifdef TODO
483 SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
484 WAIT_CIP (rp);
485
486 GET_SBIC_asr (rp, asr);
487 if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
488 {
489 /* ok, get more drastic.. */
490
491 SET_SBIC_cmd (rp, SBIC_CMD_RESET);
492 delay(25);
493 SBIC_WAIT(rp, SBIC_ASR_INT, 0);
494 GET_SBIC_csr (rp, csr); /* clears interrupt also */
495
496 return;
497 }
498
499 do
500 {
501 SBIC_WAIT (rp, SBIC_ASR_INT, 0);
502 GET_SBIC_csr (rp, csr);
503 }
504 while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
505 && (csr != SBIC_CSR_CMD_INVALID));
506 #endif
507
508 /* lets just hope it worked.. */
509 #ifdef fix_this
510 for (i = 0; i < 2; ++i) {
511 if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
512 sc->sc_cur) {
513 printf ("siopabort: cleanup!\n");
514 sc->sc_iob[i].sc_xs = NULL;
515 }
516 }
517 #endif /* fix_this */
518 /* sc->sc_active = 0; */
519 }
520 }
521
522 void
523 siopinitialize(sc)
524 struct siop_softc *sc;
525 {
526 int i;
527 u_int inhibit_sync;
528 extern u_long scsi_nosync;
529 extern int shift_nosync;
530
531 /*
532 * Need to check that scripts is on a long word boundary
533 * Also should verify that dev doesn't span non-contiguous
534 * physical pages.
535 */
536 sc->sc_scriptspa = kvtop((caddr_t)scripts);
537
538 /*
539 * malloc sc_acb to ensure that DS is on a long word boundary.
540 */
541
542 MALLOC(sc->sc_acb, struct siop_acb *,
543 sizeof(struct siop_acb) * SIOP_NACB, M_DEVBUF, M_NOWAIT);
544 if (sc->sc_acb == NULL)
545 panic("siopinitialize: ACB malloc failed!");
546
547 sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
548 sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
549 sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
550 sc->sc_minsync = sc->sc_tcp[1]; /* in 4ns units */
551 if (sc->sc_minsync < 25)
552 sc->sc_minsync = 25;
553 if (sc->sc_clock_freq <= 25) {
554 sc->sc_dcntl |= 0x80; /* SCLK/1 */
555 sc->sc_tcp[0] = sc->sc_tcp[1];
556 } else if (sc->sc_clock_freq <= 37) {
557 sc->sc_dcntl |= 0x40; /* SCLK/1.5 */
558 sc->sc_tcp[0] = sc->sc_tcp[2];
559 } else if (sc->sc_clock_freq <= 50) {
560 sc->sc_dcntl |= 0x00; /* SCLK/2 */
561 sc->sc_tcp[0] = sc->sc_tcp[3];
562 } else {
563 sc->sc_dcntl |= 0xc0; /* SCLK/3 */
564 sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
565 }
566
567 if (scsi_nosync) {
568 inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
569 shift_nosync += 8;
570 #ifdef DEBUG
571 if (inhibit_sync)
572 printf("%s: Inhibiting synchronous transfer %02x\n",
573 sc->sc_dev.dv_xname, inhibit_sync);
574 #endif
575 for (i = 0; i < 8; ++i)
576 if (inhibit_sync & (1 << i))
577 siop_inhibit_sync[i] = 1;
578 }
579
580 siopreset (sc);
581 }
582
583 void
584 siopreset(sc)
585 struct siop_softc *sc;
586 {
587 siop_regmap_p rp;
588 u_int i, s;
589 u_char dummy;
590 struct siop_acb *acb;
591
592 rp = sc->sc_siopp;
593
594 if (sc->sc_flags & SIOP_ALIVE)
595 siopabort(sc, rp, "reset");
596
597 printf("%s: ", sc->sc_dev.dv_xname); /* XXXX */
598
599 s = splbio();
600
601 /*
602 * Reset the chip
603 * XXX - is this really needed?
604 */
605 rp->siop_istat |= SIOP_ISTAT_ABRT; /* abort current script */
606 rp->siop_istat |= SIOP_ISTAT_RST; /* reset chip */
607 rp->siop_istat &= ~SIOP_ISTAT_RST;
608 /*
609 * Reset SCSI bus (do we really want this?)
610 */
611 rp->siop_sien = 0;
612 rp->siop_scntl1 |= SIOP_SCNTL1_RST;
613 delay(1);
614 rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
615
616 /*
617 * Set up various chip parameters
618 */
619 rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
620 rp->siop_scntl1 = SIOP_SCNTL1_ESR;
621 rp->siop_dcntl = sc->sc_dcntl;
622 rp->siop_dmode = 0x80; /* burst length = 4 */
623 rp->siop_sien = 0x00; /* don't enable interrupts yet */
624 rp->siop_dien = 0x00; /* don't enable interrupts yet */
625 rp->siop_scid = 1 << sc->sc_channel.chan_id;
626 rp->siop_dwt = 0x00;
627 rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
628 rp->siop_ctest7 |= sc->sc_ctest7;
629
630 /* will need to re-negotiate sync xfers */
631 bzero(&sc->sc_sync, sizeof (sc->sc_sync));
632
633 i = rp->siop_istat;
634 if (i & SIOP_ISTAT_SIP)
635 dummy = rp->siop_sstat0;
636 if (i & SIOP_ISTAT_DIP)
637 dummy = rp->siop_dstat;
638
639 splx (s);
640
641 delay (siop_reset_delay * 1000);
642 printf("siop id %d reset V%d\n", sc->sc_channel.chan_id,
643 rp->siop_ctest8 >> 4);
644
645 if ((sc->sc_flags & SIOP_ALIVE) == 0) {
646 TAILQ_INIT(&sc->ready_list);
647 TAILQ_INIT(&sc->nexus_list);
648 TAILQ_INIT(&sc->free_list);
649 sc->sc_nexus = NULL;
650 acb = sc->sc_acb;
651 bzero(acb, sizeof(struct siop_acb) * SIOP_NACB);
652 for (i = 0; i < SIOP_NACB; i++) {
653 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
654 acb++;
655 }
656 bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
657 } else {
658 if (sc->sc_nexus != NULL) {
659 sc->sc_nexus->xs->error = XS_RESET;
660 siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
661 }
662 while ((acb = sc->nexus_list.tqh_first) > 0) {
663 acb->xs->error = XS_RESET;
664 siop_scsidone(acb, acb->stat[0]);
665 }
666 }
667
668 sc->sc_flags |= SIOP_ALIVE;
669 sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
670 /* enable SCSI and DMA interrupts */
671 sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
672 SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
673 sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
674 /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
675 rp->siop_sien = sc->sc_sien;
676 rp->siop_dien = sc->sc_dien;
677 }
678
679 /*
680 * Setup Data Storage for 53C710 and start SCRIPTS processing
681 */
682
683 void
684 siop_start (sc, target, lun, cbuf, clen, buf, len)
685 struct siop_softc *sc;
686 int target;
687 int lun;
688 u_char *cbuf;
689 int clen;
690 u_char *buf;
691 int len;
692 {
693 siop_regmap_p rp = sc->sc_siopp;
694 int nchain;
695 int count, tcount;
696 char *addr, *dmaend;
697 struct siop_acb *acb = sc->sc_nexus;
698 #ifdef DEBUG
699 int i;
700 #endif
701
702 #ifdef DEBUG
703 if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
704 printf ("ACK! siop was busy: rp %p script %p dsa %p active %ld\n",
705 rp, &scripts, &acb->ds, sc->sc_active);
706 printf ("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
707 rp->siop_istat, rp->siop_sfbr, rp->siop_lcrc,
708 rp->siop_sien, rp->siop_dien);
709 #ifdef DDB
710 /*Debugger();*/
711 #endif
712 }
713 #endif
714 acb->msgout[0] = MSG_IDENTIFY | lun;
715 if (siop_allow_disc[target] & 2 ||
716 (siop_allow_disc[target] && len == 0))
717 acb->msgout[0] = MSG_IDENTIFY_DR | lun;
718 acb->status = 0;
719 acb->stat[0] = -1;
720 acb->msg[0] = -1;
721 acb->ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].sxfer << 8);
722 acb->ds.idlen = 1;
723 acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]);
724 acb->ds.cmdlen = clen;
725 acb->ds.cmdbuf = (char *) kvtop(cbuf);
726 acb->ds.stslen = 1;
727 acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]);
728 acb->ds.msglen = 1;
729 acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]);
730 acb->msg[1] = -1;
731 acb->ds.msginlen = 1;
732 acb->ds.extmsglen = 1;
733 acb->ds.synmsglen = 3;
734 acb->ds.msginbuf = (char *) kvtop(&acb->msg[1]);
735 acb->ds.extmsgbuf = (char *) kvtop(&acb->msg[2]);
736 acb->ds.synmsgbuf = (char *) kvtop(&acb->msg[3]);
737 bzero(&acb->ds.chain, sizeof (acb->ds.chain));
738
739 /*
740 * Negotiate wide is the initial negotiation state; since the 53c710
741 * doesn't do wide transfers, just begin the synchronous transfer
742 * negotation here.
743 */
744 if (sc->sc_sync[target].state == NEG_WIDE) {
745 if (siop_inhibit_sync[target]) {
746 sc->sc_sync[target].state = NEG_DONE;
747 sc->sc_sync[target].sbcl = 0;
748 sc->sc_sync[target].sxfer = 0;
749 #ifdef DEBUG
750 if (siopsync_debug)
751 printf ("Forcing target %d asynchronous\n", target);
752 #endif
753 }
754 else {
755 acb->msg[2] = -1;
756 acb->msgout[1] = MSG_EXT_MESSAGE;
757 acb->msgout[2] = 3;
758 acb->msgout[3] = MSG_SYNC_REQ;
759 #ifdef MAXTOR_SYNC_KLUDGE
760 acb->msgout[4] = 50 / 4; /* ask for ridiculous period */
761 #else
762 acb->msgout[4] = sc->sc_minsync;
763 #endif
764 acb->msgout[5] = SIOP_MAX_OFFSET;
765 acb->ds.idlen = 6;
766 sc->sc_sync[target].state = NEG_WAITS;
767 #ifdef DEBUG
768 if (siopsync_debug)
769 printf ("Sending sync request to target %d\n", target);
770 #endif
771 }
772 }
773
774 /*
775 * Build physical DMA addresses for scatter/gather I/O
776 */
777 acb->iob_buf = buf;
778 acb->iob_len = len;
779 acb->iob_curbuf = acb->iob_curlen = 0;
780 nchain = 0;
781 count = len;
782 addr = buf;
783 dmaend = NULL;
784 while (count > 0) {
785 acb->ds.chain[nchain].databuf = (char *) kvtop (addr);
786 if (count < (tcount = NBPG - ((int) addr & PGOFSET)))
787 tcount = count;
788 acb->ds.chain[nchain].datalen = tcount;
789 addr += tcount;
790 count -= tcount;
791 if (acb->ds.chain[nchain].databuf == dmaend) {
792 dmaend += acb->ds.chain[nchain].datalen;
793 acb->ds.chain[nchain].datalen = 0;
794 acb->ds.chain[--nchain].datalen += tcount;
795 #ifdef DEBUG
796 ++siopdma_hits;
797 #endif
798 }
799 else {
800 dmaend = acb->ds.chain[nchain].databuf +
801 acb->ds.chain[nchain].datalen;
802 acb->ds.chain[nchain].datalen = tcount;
803 #ifdef DEBUG
804 if (nchain) /* Don't count miss on first one */
805 ++siopdma_misses;
806 #endif
807 }
808 ++nchain;
809 }
810 #ifdef DEBUG
811 if (nchain != 1 && len != 0 && siop_debug & 3) {
812 printf ("DMA chaining set: %d\n", nchain);
813 for (i = 0; i < nchain; ++i) {
814 printf (" [%d] %8p %lx\n", i, acb->ds.chain[i].databuf,
815 acb->ds.chain[i].datalen);
816 }
817 }
818 #endif
819
820 /* push data cache for all data the 53c710 needs to access */
821 dma_cachectl ((caddr_t)acb, sizeof (struct siop_acb));
822 dma_cachectl (cbuf, clen);
823 if (buf != NULL && len != 0)
824 dma_cachectl (buf, len);
825 #ifdef DEBUG
826 if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
827 printf ("ACK! siop was busy at start: rp %p script %p dsa %p active %ld\n",
828 rp, &scripts, &acb->ds, sc->sc_active);
829 #ifdef DDB
830 /*Debugger();*/
831 #endif
832 }
833 #endif
834 if (sc->nexus_list.tqh_first == NULL) {
835 if (rp->siop_istat & SIOP_ISTAT_CON)
836 printf("%s: siop_select while connected?\n",
837 sc->sc_dev.dv_xname);
838 rp->siop_temp = 0;
839 rp->siop_sbcl = sc->sc_sync[target].sbcl;
840 rp->siop_dsa = kvtop((caddr_t)&acb->ds);
841 rp->siop_dsp = sc->sc_scriptspa;
842 SIOP_TRACE('s',1,0,0)
843 } else {
844 if ((rp->siop_istat & SIOP_ISTAT_CON) == 0) {
845 rp->siop_istat = SIOP_ISTAT_SIGP;
846 SIOP_TRACE('s',2,0,0);
847 }
848 else {
849 SIOP_TRACE('s',3,rp->siop_istat,0);
850 }
851 }
852 #ifdef DEBUG
853 ++siopstarts;
854 #endif
855 }
856
857 /*
858 * Process a DMA or SCSI interrupt from the 53C710 SIOP
859 */
860
861 int
862 siop_checkintr(sc, istat, dstat, sstat0, status)
863 struct siop_softc *sc;
864 u_char istat;
865 u_char dstat;
866 u_char sstat0;
867 int *status;
868 {
869 siop_regmap_p rp = sc->sc_siopp;
870 struct siop_acb *acb = sc->sc_nexus;
871 int target = 0;
872 int dfifo, dbc, sstat1;
873
874 dfifo = rp->siop_dfifo;
875 dbc = rp->siop_dbc0;
876 sstat1 = rp->siop_sstat1;
877 rp->siop_ctest8 |= SIOP_CTEST8_CLF;
878 while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT)
879 ;
880 rp->siop_ctest8 &= ~SIOP_CTEST8_CLF;
881 #ifdef DEBUG
882 ++siopints;
883 #if 0
884 if (siop_debug & 0x100) {
885 DCIAS(&acb->stat[0]); /* XXX */
886 printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
887 istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]);
888 printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
889 acb->msg[0], acb->msg[1], acb->msg[2],
890 acb->msg[3], acb->msg[4], acb->msg[5]);
891 }
892 #endif
893 if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa ||
894 rp->siop_dsp >= sc->sc_scriptspa + sizeof(scripts))) {
895 printf ("%s: dsp not within script dsp %lx scripts %lx:%lx",
896 sc->sc_dev.dv_xname, rp->siop_dsp, sc->sc_scriptspa,
897 sc->sc_scriptspa + sizeof(scripts));
898 printf(" istat %x dstat %x sstat0 %x\n",
899 istat, dstat, sstat0);
900 #ifdef DDB
901 Debugger();
902 #endif
903 }
904 #endif
905 SIOP_TRACE('i',dstat,istat,(istat&SIOP_ISTAT_DIP)?rp->siop_dsps&0xff:sstat0);
906 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) {
907 /* Normal completion status, or check condition */
908 #ifdef DEBUG
909 if (rp->siop_dsa != kvtop((caddr_t)&acb->ds)) {
910 printf ("siop: invalid dsa: %lx %x\n", rp->siop_dsa,
911 kvtop((caddr_t)&acb->ds));
912 panic("*** siop DSA invalid ***");
913 }
914 #endif
915 target = acb->xs->xs_periph->periph_target;
916 if (sc->sc_sync[target].state == NEG_WAITS) {
917 if (acb->msg[1] == 0xff)
918 printf ("%s: target %d ignored sync request\n",
919 sc->sc_dev.dv_xname, target);
920 else if (acb->msg[1] == MSG_REJECT)
921 printf ("%s: target %d rejected sync request\n",
922 sc->sc_dev.dv_xname, target);
923 else
924 /* XXX - need to set sync transfer parameters */
925 printf("%s: target %d (sync) %02x %02x %02x\n",
926 sc->sc_dev.dv_xname, target, acb->msg[1],
927 acb->msg[2], acb->msg[3]);
928 sc->sc_sync[target].state = NEG_DONE;
929 }
930 dma_cachectl(&acb->stat[0], 1);
931 *status = acb->stat[0];
932 #ifdef DEBUG
933 if (rp->siop_sbcl & SIOP_BSY) {
934 /*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n",
935 rp, &scripts, &acb->ds);*/
936 #ifdef DDB
937 /*Debugger();*/
938 #endif
939 }
940 if (acb->msg[0] != 0x00)
941 printf("%s: message was not COMMAND COMPLETE: %x\n",
942 sc->sc_dev.dv_xname, acb->msg[0]);
943 #endif
944 if (sc->nexus_list.tqh_first)
945 rp->siop_dcntl |= SIOP_DCNTL_STD;
946 return 1;
947 }
948 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0b) {
949 target = acb->xs->xs_periph->periph_target;
950 if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 3 &&
951 acb->msg[3] == MSG_SYNC_REQ) {
952 #ifdef DEBUG
953 if (siopsync_debug)
954 printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
955 acb->msg[0], acb->msg[1], acb->msg[2],
956 acb->msg[3], acb->msg[4], acb->msg[5]);
957 #endif
958 sc->sc_sync[target].sxfer = 0;
959 sc->sc_sync[target].sbcl = 0;
960 if (acb->msg[2] == 3 &&
961 acb->msg[3] == MSG_SYNC_REQ &&
962 acb->msg[5] != 0) {
963 #ifdef MAXTOR_KLUDGE
964 /*
965 * Kludge for my Maxtor XT8580S
966 * It accepts whatever we request, even
967 * though it won't work. So we ask for
968 * a short period than we can handle. If
969 * the device says it can do it, use 208ns.
970 * If the device says it can do less than
971 * 100ns, then we limit it to 100ns.
972 */
973 if (acb->msg[4] && acb->msg[4] < 100 / 4) {
974 #ifdef DEBUG
975 printf ("%d: target %d wanted %dns period\n",
976 sc->sc_dev.dv_xname, target,
977 acb->msg[4] * 4);
978 #endif
979 if (acb->msg[4] == 50 / 4)
980 acb->msg[4] = 208 / 4;
981 else
982 acb->msg[4] = 100 / 4;
983 }
984 #endif /* MAXTOR_KLUDGE */
985 printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
986 sc->sc_dev.dv_xname, target,
987 acb->msg[4] * 4, acb->msg[5]);
988 scsi_period_to_siop (sc, target);
989 }
990 rp->siop_sxfer = sc->sc_sync[target].sxfer;
991 rp->siop_sbcl = sc->sc_sync[target].sbcl;
992 if (sc->sc_sync[target].state == NEG_WAITS) {
993 sc->sc_sync[target].state = NEG_DONE;
994 rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
995 return(0);
996 }
997 rp->siop_dcntl |= SIOP_DCNTL_STD;
998 sc->sc_sync[target].state = NEG_DONE;
999 return (0);
1000 }
1001 /* XXX - not SDTR message */
1002 }
1003 if (sstat0 & SIOP_SSTAT0_M_A) { /* Phase mismatch */
1004 #ifdef DEBUG
1005 ++siopphmm;
1006 if (acb == NULL)
1007 printf("%s: Phase mismatch with no active command?\n",
1008 sc->sc_dev.dv_xname);
1009 #endif
1010 if (acb->iob_len) {
1011 int adjust;
1012 adjust = ((dfifo - (dbc & 0x7f)) & 0x7f);
1013 if (sstat1 & SIOP_SSTAT1_ORF)
1014 ++adjust;
1015 if (sstat1 & SIOP_SSTAT1_OLF)
1016 ++adjust;
1017 acb->iob_curlen = *((long *)&rp->siop_dcmd) & 0xffffff;
1018 acb->iob_curlen += adjust;
1019 acb->iob_curbuf = *((long *)&rp->siop_dnad) - adjust;
1020 #ifdef DEBUG
1021 if (siop_debug & 0x100) {
1022 int i;
1023 printf ("Phase mismatch: curbuf %lx curlen %lx dfifo %x dbc %x sstat1 %x adjust %x sbcl %x starts %d acb %p\n",
1024 acb->iob_curbuf, acb->iob_curlen, dfifo,
1025 dbc, sstat1, adjust, rp->siop_sbcl, siopstarts, acb);
1026 if (acb->ds.chain[1].datalen) {
1027 for (i = 0; acb->ds.chain[i].datalen; ++i)
1028 printf("chain[%d] addr %p len %lx\n",
1029 i, acb->ds.chain[i].databuf,
1030 acb->ds.chain[i].datalen);
1031 }
1032 }
1033 #endif
1034 dma_cachectl ((caddr_t)acb, sizeof(*acb));
1035 }
1036 #ifdef DEBUG
1037 SIOP_TRACE('m',rp->siop_sbcl,(rp->siop_dsp>>8),rp->siop_dsp);
1038 if (siop_debug & 9)
1039 printf ("Phase mismatch: %x dsp +%lx dcmd %lx\n",
1040 rp->siop_sbcl,
1041 rp->siop_dsp - sc->sc_scriptspa,
1042 *((long *)&rp->siop_dcmd));
1043 #endif
1044 if ((rp->siop_sbcl & SIOP_REQ) == 0) {
1045 printf ("Phase mismatch: REQ not asserted! %02x dsp %lx\n",
1046 rp->siop_sbcl, rp->siop_dsp);
1047 #if defined(DEBUG) && defined(DDB)
1048 /*Debugger(); XXX is*/
1049 #endif
1050 }
1051 switch (rp->siop_sbcl & 7) {
1052 case 0: /* data out */
1053 case 1: /* data in */
1054 case 2: /* status */
1055 case 3: /* command */
1056 case 6: /* message in */
1057 case 7: /* message out */
1058 rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
1059 break;
1060 default:
1061 goto bad_phase;
1062 }
1063 return 0;
1064 }
1065 if (sstat0 & SIOP_SSTAT0_STO) { /* Select timed out */
1066 #ifdef DEBUG
1067 if (acb == NULL)
1068 printf("%s: Select timeout with no active command?\n",
1069 sc->sc_dev.dv_xname);
1070 if (rp->siop_sbcl & SIOP_BSY) {
1071 printf ("ACK! siop was busy at timeout: rp %p script %p dsa %p\n",
1072 rp, &scripts, &acb->ds);
1073 printf(" sbcl %x sdid %x istat %x dstat %x sstat0 %x\n",
1074 rp->siop_sbcl, rp->siop_sdid, istat, dstat, sstat0);
1075 if (!(rp->siop_sbcl & SIOP_BSY)) {
1076 printf ("Yikes, it's not busy now!\n");
1077 #if 0
1078 *status = -1;
1079 if (sc->nexus_list.tqh_first)
1080 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1081 return 1;
1082 #endif
1083 }
1084 /* rp->siop_dcntl |= SIOP_DCNTL_STD;*/
1085 return (0);
1086 #ifdef DDB
1087 Debugger();
1088 #endif
1089 }
1090 #endif
1091 *status = -1;
1092 acb->xs->error = XS_SELTIMEOUT;
1093 if (sc->nexus_list.tqh_first)
1094 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1095 return 1;
1096 }
1097 if (acb)
1098 target = acb->xs->xs_periph->periph_target;
1099 else
1100 target = 7;
1101 if (sstat0 & SIOP_SSTAT0_UDC) {
1102 #ifdef DEBUG
1103 if (acb == NULL)
1104 printf("%s: Unexpected disconnect with no active command?\n",
1105 sc->sc_dev.dv_xname);
1106 printf ("%s: target %d disconnected unexpectedly\n",
1107 sc->sc_dev.dv_xname, target);
1108 #endif
1109 #if 0
1110 siopabort (sc, rp, "siopchkintr");
1111 #endif
1112 *status = STS_BUSY;
1113 if (sc->nexus_list.tqh_first)
1114 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1115 return (acb != NULL);
1116 }
1117 if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff01 ||
1118 rp->siop_dsps == 0xff02)) {
1119 #ifdef DEBUG
1120 if (siop_debug & 0x100)
1121 printf ("%s: ID %02x disconnected TEMP %lx (+%lx) curbuf %lx curlen %lx buf %p len %lx dfifo %x dbc %x sstat1 %x starts %d acb %p\n",
1122 sc->sc_dev.dv_xname, 1 << target, rp->siop_temp,
1123 rp->siop_temp ? rp->siop_temp - sc->sc_scriptspa : 0,
1124 acb->iob_curbuf, acb->iob_curlen,
1125 acb->ds.chain[0].databuf, acb->ds.chain[0].datalen, dfifo, dbc, sstat1, siopstarts, acb);
1126 #endif
1127 if (acb == NULL) {
1128 printf("%s: Disconnect with no active command?\n",
1129 sc->sc_dev.dv_xname);
1130 return (0);
1131 }
1132 /*
1133 * XXXX need to update iob_curbuf/iob_curlen to reflect
1134 * current data transferred. If device disconnected in
1135 * the middle of a DMA block, they should already be set
1136 * by the phase change interrupt. If the disconnect
1137 * occurs on a DMA block boundary, we have to figure out
1138 * which DMA block it was.
1139 */
1140 if (acb->iob_len && rp->siop_temp) {
1141 int n = rp->siop_temp - sc->sc_scriptspa;
1142
1143 if (acb->iob_curlen && acb->iob_curlen != acb->ds.chain[0].datalen)
1144 printf("%s: iob_curbuf/len already set? n %x iob %lx/%lx chain[0] %p/%lx\n",
1145 sc->sc_dev.dv_xname, n, acb->iob_curbuf, acb->iob_curlen,
1146 acb->ds.chain[0].databuf, acb->ds.chain[0].datalen);
1147 if (n < Ent_datain)
1148 n = (n - Ent_dataout) / 16;
1149 else
1150 n = (n - Ent_datain) / 16;
1151 if (n <= 0 && n > DMAMAXIO)
1152 printf("TEMP invalid %d\n", n);
1153 else {
1154 acb->iob_curbuf = (u_long)acb->ds.chain[n].databuf;
1155 acb->iob_curlen = acb->ds.chain[n].datalen;
1156 }
1157 #ifdef DEBUG
1158 if (siop_debug & 0x100) {
1159 printf("%s: TEMP offset %d", sc->sc_dev.dv_xname, n);
1160 printf(" curbuf %lx curlen %lx\n", acb->iob_curbuf,
1161 acb->iob_curlen);
1162 }
1163 #endif
1164 }
1165 /*
1166 * If data transfer was interrupted by disconnect, iob_curbuf
1167 * and iob_curlen should reflect the point of interruption.
1168 * Adjust the DMA chain so that the data transfer begins
1169 * at the appropriate place upon reselection.
1170 * XXX This should only be done on save data pointer message?
1171 */
1172 if (acb->iob_curlen) {
1173 int i, j;
1174
1175 #ifdef DEBUG
1176 if (siop_debug & 0x100)
1177 printf ("%s: adjusting DMA chain\n",
1178 sc->sc_dev.dv_xname);
1179 if (rp->siop_dsps == 0xff02)
1180 printf ("%s: ID %02x disconnected without Save Data Pointers\n",
1181 sc->sc_dev.dv_xname, 1 << target);
1182 #endif
1183 for (i = 0; i < DMAMAXIO; ++i) {
1184 if (acb->ds.chain[i].datalen == 0)
1185 break;
1186 if (acb->iob_curbuf >= (long)acb->ds.chain[i].databuf &&
1187 acb->iob_curbuf < (long)(acb->ds.chain[i].databuf +
1188 acb->ds.chain[i].datalen))
1189 break;
1190 }
1191 if (i >= DMAMAXIO || acb->ds.chain[i].datalen == 0) {
1192 printf("couldn't find saved data pointer: ");
1193 printf("curbuf %lx curlen %lx i %d\n",
1194 acb->iob_curbuf, acb->iob_curlen, i);
1195 #ifdef DDB
1196 Debugger();
1197 #endif
1198 }
1199 #ifdef DEBUG
1200 if (siop_debug & 0x100)
1201 printf(" chain[0]: %p/%lx -> %lx/%lx\n",
1202 acb->ds.chain[0].databuf,
1203 acb->ds.chain[0].datalen,
1204 acb->iob_curbuf,
1205 acb->iob_curlen);
1206 #endif
1207 acb->ds.chain[0].databuf = (char *)acb->iob_curbuf;
1208 acb->ds.chain[0].datalen = acb->iob_curlen;
1209 for (j = 1, ++i; i < DMAMAXIO && acb->ds.chain[i].datalen; ++i, ++j) {
1210 #ifdef DEBUG
1211 if (siop_debug & 0x100)
1212 printf(" chain[%d]: %p/%lx -> %p/%lx\n", j,
1213 acb->ds.chain[j].databuf,
1214 acb->ds.chain[j].datalen,
1215 acb->ds.chain[i].databuf,
1216 acb->ds.chain[i].datalen);
1217 #endif
1218 acb->ds.chain[j].databuf = acb->ds.chain[i].databuf;
1219 acb->ds.chain[j].datalen = acb->ds.chain[i].datalen;
1220 }
1221 if (j < DMAMAXIO)
1222 acb->ds.chain[j].datalen = 0;
1223 DCIAS(kvtop((caddr_t)&acb->ds.chain));
1224 }
1225 ++sc->sc_tinfo[target].dconns;
1226 /*
1227 * add nexus to waiting list
1228 * clear nexus
1229 * try to start another command for another target/lun
1230 */
1231 acb->status = sc->sc_flags & SIOP_INTSOFF;
1232 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1233 sc->sc_nexus = NULL; /* no current device */
1234 /* start script to wait for reselect */
1235 if (sc->sc_nexus == NULL)
1236 rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
1237 /* XXXX start another command ? */
1238 if (sc->ready_list.tqh_first)
1239 siop_sched(sc);
1240 return (0);
1241 }
1242 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff03) {
1243 int reselid = rp->siop_scratch & 0x7f;
1244 int reselun = rp->siop_sfbr & 0x07;
1245
1246 sc->sc_sstat1 = rp->siop_sbcl; /* XXXX save current SBCL */
1247 #ifdef DEBUG
1248 if (siop_debug & 0x100)
1249 printf ("%s: target ID %02x reselected dsps %lx\n",
1250 sc->sc_dev.dv_xname, reselid,
1251 rp->siop_dsps);
1252 if ((rp->siop_sfbr & 0x80) == 0)
1253 printf("%s: Reselect message in was not identify: %x\n",
1254 sc->sc_dev.dv_xname, rp->siop_sfbr);
1255 #endif
1256 if (sc->sc_nexus) {
1257 #ifdef DEBUG
1258 if (siop_debug & 0x100)
1259 printf ("%s: reselect ID %02x w/active\n",
1260 sc->sc_dev.dv_xname, reselid);
1261 #endif
1262 TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain);
1263 sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target].lubusy
1264 &= ~(1 << sc->sc_nexus->xs->xs_periph->periph_lun);
1265 --sc->sc_active;
1266 }
1267 /*
1268 * locate acb of reselecting device
1269 * set sc->sc_nexus to acb
1270 */
1271 for (acb = sc->nexus_list.tqh_first; acb;
1272 acb = acb->chain.tqe_next) {
1273 if (reselid != (acb->ds.scsi_addr >> 16) ||
1274 reselun != (acb->msgout[0] & 0x07))
1275 continue;
1276 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1277 sc->sc_nexus = acb;
1278 sc->sc_flags |= acb->status;
1279 acb->status = 0;
1280 DCIAS(kvtop(&acb->stat[0]));
1281 rp->siop_dsa = kvtop((caddr_t)&acb->ds);
1282 rp->siop_sxfer =
1283 sc->sc_sync[acb->xs->xs_periph->periph_target].sxfer;
1284 rp->siop_sbcl =
1285 sc->sc_sync[acb->xs->xs_periph->periph_target].sbcl;
1286 break;
1287 }
1288 if (acb == NULL) {
1289 printf("%s: target ID %02x reselect nexus_list %p\n",
1290 sc->sc_dev.dv_xname, reselid,
1291 sc->nexus_list.tqh_first);
1292 panic("unable to find reselecting device");
1293 }
1294 dma_cachectl ((caddr_t)acb, sizeof(*acb));
1295 rp->siop_temp = 0;
1296 rp->siop_dcntl |= SIOP_DCNTL_STD;
1297 return (0);
1298 }
1299 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) {
1300 #ifdef DEBUG
1301 u_short ctest2 = rp->siop_ctest2;
1302
1303 /* reselect was interrupted (by Sig_P or select) */
1304 if (siop_debug & 0x100 ||
1305 (ctest2 & SIOP_CTEST2_SIGP) == 0)
1306 printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
1307 sc->sc_dev.dv_xname, rp->siop_scntl1,
1308 ctest2, rp->siop_sfbr, istat, rp->siop_istat);
1309 #endif
1310 /* XXX assumes it was not select */
1311 if (sc->sc_nexus == NULL) {
1312 #ifdef DEBUG
1313 printf("%s: reselect interrupted, sc_nexus == NULL\n",
1314 sc->sc_dev.dv_xname);
1315 #if 0
1316 siop_dump(sc);
1317 #ifdef DDB
1318 Debugger();
1319 #endif
1320 #endif
1321 #endif
1322 rp->siop_dcntl |= SIOP_DCNTL_STD;
1323 return(0);
1324 }
1325 target = sc->sc_nexus->xs->xs_periph->periph_target;
1326 rp->siop_temp = 0;
1327 rp->siop_dsa = kvtop((caddr_t)&sc->sc_nexus->ds);
1328 rp->siop_sxfer = sc->sc_sync[target].sxfer;
1329 rp->siop_sbcl = sc->sc_sync[target].sbcl;
1330 rp->siop_dsp = sc->sc_scriptspa;
1331 return (0);
1332 }
1333 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) {
1334 if (acb == NULL)
1335 printf("%s: Bad message-in with no active command?\n",
1336 sc->sc_dev.dv_xname);
1337 /* Unrecognized message in byte */
1338 dma_cachectl (&acb->msg[1],1);
1339 printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n",
1340 sc->sc_dev.dv_xname, rp->siop_sfbr, acb->msg[1], rp->siop_sbcl);
1341 /* what should be done here? */
1342 DCIAS(kvtop(&acb->msg[1]));
1343 rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
1344 return (0);
1345 }
1346 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
1347 /* Status phase wasn't followed by message in phase? */
1348 printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n",
1349 sc->sc_dev.dv_xname, rp->siop_sbcl, rp->siop_sbdl);
1350 if (rp->siop_sbcl == 0xa7) {
1351 /* It is now, just continue the script? */
1352 rp->siop_dcntl |= SIOP_DCNTL_STD;
1353 return (0);
1354 }
1355 }
1356 if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
1357 dma_cachectl (&acb->stat[0], 1);
1358 dma_cachectl (&acb->msg[0], 1);
1359 printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n",
1360 rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1],
1361 rp->siop_sbcl);
1362 siopreset (sc);
1363 *status = -1;
1364 return 0; /* siopreset has cleaned up */
1365 }
1366 if (sstat0 & SIOP_SSTAT0_SGE)
1367 printf ("SIOP: SCSI Gross Error\n");
1368 if (sstat0 & SIOP_SSTAT0_PAR)
1369 printf ("SIOP: Parity Error\n");
1370 if (dstat & SIOP_DSTAT_IID)
1371 printf ("SIOP: Invalid instruction detected\n");
1372 bad_phase:
1373 /*
1374 * temporary panic for unhandled conditions
1375 * displays various things about the 53C710 status and registers
1376 * then panics.
1377 * XXXX need to clean this up to print out the info, reset, and continue
1378 */
1379 printf ("siopchkintr: target %x ds %p\n", target, &acb->ds);
1380 printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n", sc->sc_scriptspa,
1381 kvtop((caddr_t)&acb->ds), kvtop((caddr_t)rp), rp->siop_dsp,
1382 *((long *)&rp->siop_dcmd));
1383 printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n",
1384 istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa,
1385 rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr);
1386 #ifdef DEBUG
1387 if (siop_debug & 0x20)
1388 panic("siopchkintr: **** temp ****");
1389 #endif
1390 #ifdef DDB
1391 Debugger ();
1392 #endif
1393 siopreset (sc); /* hard reset */
1394 *status = -1;
1395 return 0; /* siopreset cleaned up */
1396 }
1397
1398 void
1399 siop_select(sc)
1400 struct siop_softc *sc;
1401 {
1402 siop_regmap_p rp;
1403 struct siop_acb *acb = sc->sc_nexus;
1404
1405 #ifdef DEBUG
1406 if (siop_debug & 1)
1407 printf ("%s: select ", sc->sc_dev.dv_xname);
1408 #endif
1409
1410 rp = sc->sc_siopp;
1411 if (acb->xs->xs_control & XS_CTL_POLL || siop_no_dma) {
1412 sc->sc_flags |= SIOP_INTSOFF;
1413 sc->sc_flags &= ~SIOP_INTDEFER;
1414 if ((rp->siop_istat & 0x08) == 0) {
1415 rp->siop_sien = 0;
1416 rp->siop_dien = 0;
1417 }
1418 #if 0
1419 } else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
1420 sc->sc_flags &= ~SIOP_INTSOFF;
1421 if ((rp->siop_istat & 0x08) == 0) {
1422 rp->siop_sien = sc->sc_sien;
1423 rp->siop_dien = sc->sc_dien;
1424 }
1425 #endif
1426 }
1427 #ifdef DEBUG
1428 if (siop_debug & 1)
1429 printf ("siop_select: target %x cmd %02x ds %p\n",
1430 acb->xs->xs_periph->periph_target, acb->cmd.opcode,
1431 &sc->sc_nexus->ds);
1432 #endif
1433
1434 siop_start(sc, acb->xs->xs_periph->periph_target,
1435 acb->xs->xs_periph->periph_lun,
1436 (u_char *)&acb->cmd, acb->clen, acb->daddr, acb->dleft);
1437
1438 return;
1439 }
1440
1441 /*
1442 * 53C710 interrupt handler
1443 */
1444
1445 void
1446 siopintr (sc)
1447 register struct siop_softc *sc;
1448 {
1449 siop_regmap_p rp;
1450 register u_char istat, dstat, sstat0;
1451 int status;
1452 int s = splbio();
1453
1454 istat = sc->sc_istat;
1455 if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
1456 splx(s);
1457 return;
1458 }
1459
1460 /* Got a valid interrupt on this device */
1461 rp = sc->sc_siopp;
1462 dstat = sc->sc_dstat;
1463 sstat0 = sc->sc_sstat0;
1464 if (dstat & SIOP_DSTAT_SIR)
1465 sc->sc_intcode = rp->siop_dsps;
1466 sc->sc_istat = 0;
1467 #ifdef DEBUG
1468 if (siop_debug & 1)
1469 printf ("%s: intr istat %x dstat %x sstat0 %x\n",
1470 sc->sc_dev.dv_xname, istat, dstat, sstat0);
1471 if (!sc->sc_active) {
1472 printf ("%s: spurious interrupt? istat %x dstat %x sstat0 %x nexus %p status %x\n",
1473 sc->sc_dev.dv_xname, istat, dstat, sstat0,
1474 sc->sc_nexus, sc->sc_nexus ? sc->sc_nexus->stat[0] : 0);
1475 }
1476 #endif
1477
1478 #ifdef DEBUG
1479 if (siop_debug & 5) {
1480 DCIAS(kvtop(&sc->sc_nexus->stat[0]));
1481 printf ("%s: intr istat %x dstat %x sstat0 %x dsps %lx sbcl %x sts %x msg %x\n",
1482 sc->sc_dev.dv_xname, istat, dstat, sstat0,
1483 rp->siop_dsps, rp->siop_sbcl,
1484 sc->sc_nexus->stat[0], sc->sc_nexus->msg[0]);
1485 }
1486 #endif
1487 if (sc->sc_flags & SIOP_INTDEFER) {
1488 sc->sc_flags &= ~(SIOP_INTDEFER | SIOP_INTSOFF);
1489 rp->siop_sien = sc->sc_sien;
1490 rp->siop_dien = sc->sc_dien;
1491 }
1492 if (siop_checkintr (sc, istat, dstat, sstat0, &status)) {
1493 #if 1
1494 if (status == 0xff)
1495 printf ("siopintr: status == 0xff\n");
1496 #endif
1497 if ((sc->sc_flags & (SIOP_INTSOFF | SIOP_INTDEFER)) != SIOP_INTSOFF) {
1498 #if 0
1499 if (rp->siop_sbcl & SIOP_BSY) {
1500 printf ("%s: SCSI bus busy at completion",
1501 sc->sc_dev.dv_xname);
1502 printf(" targ %d sbcl %02x sfbr %x lcrc %02x dsp +%x\n",
1503 sc->sc_nexus->xs->xs_periph->periph_target,
1504 rp->siop_sbcl, rp->siop_sfbr, rp->siop_lcrc,
1505 rp->siop_dsp - sc->sc_scriptspa);
1506 }
1507 #endif
1508 siop_scsidone(sc->sc_nexus, sc->sc_nexus ?
1509 sc->sc_nexus->stat[0] : -1);
1510 }
1511 }
1512 splx(s);
1513 }
1514
1515 /*
1516 * This is based on the Progressive Peripherals 33Mhz Zeus driver and will
1517 * not be correct for other 53c710 boards.
1518 *
1519 */
1520 void
1521 scsi_period_to_siop (sc, target)
1522 struct siop_softc *sc;
1523 int target;
1524 {
1525 int period, offset, sxfer, sbcl = 0;
1526 #ifdef DEBUG_SYNC
1527 int i;
1528 #endif
1529
1530 period = sc->sc_nexus->msg[4];
1531 offset = sc->sc_nexus->msg[5];
1532 #ifdef DEBUG_SYNC
1533 sxfer = 0;
1534 if (offset <= SIOP_MAX_OFFSET)
1535 sxfer = offset;
1536 for (i = 0; i < sizeof (sync_tab) / 2; ++i) {
1537 if (period <= sync_tab[i].p) {
1538 sxfer |= sync_tab[i].r & 0x70;
1539 sbcl = sync_tab[i].r & 0x03;
1540 break;
1541 }
1542 }
1543 printf ("siop sync old: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
1544 #endif
1545 for (sbcl = 1; sbcl < 4; ++sbcl) {
1546 sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
1547 if (sxfer >= 0 && sxfer <= 7)
1548 break;
1549 }
1550 if (sbcl > 3) {
1551 printf("siop sync: unable to compute sync params for period %dns\n",
1552 period * 4);
1553 /*
1554 * XXX need to pick a value we can do and renegotiate
1555 */
1556 sxfer = sbcl = 0;
1557 } else {
1558 sxfer = (sxfer << 4) | ((offset <= SIOP_MAX_OFFSET) ?
1559 offset : SIOP_MAX_OFFSET);
1560 #ifdef DEBUG_SYNC
1561 printf("siop sync: params for period %dns: sxfer %x sbcl %x",
1562 period * 4, sxfer, sbcl);
1563 printf(" actual period %dns\n",
1564 sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
1565 #endif
1566 }
1567 sc->sc_sync[target].sxfer = sxfer;
1568 sc->sc_sync[target].sbcl = sbcl;
1569 #ifdef DEBUG_SYNC
1570 printf ("siop sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
1571 #endif
1572 }
1573
1574 #ifdef DEBUG
1575
1576 #if SIOP_TRACE_SIZE
1577 void
1578 siop_dump_trace()
1579 {
1580 int i;
1581
1582 printf("siop trace: next index %d\n", siop_trix);
1583 i = siop_trix;
1584 do {
1585 printf("%3d: '%c' %02x %02x %02x\n", i, siop_trbuf[i],
1586 siop_trbuf[i + 1], siop_trbuf[i + 2], siop_trbuf[i + 3]);
1587 i = (i + 4) & (SIOP_TRACE_SIZE - 1);
1588 } while (i != siop_trix);
1589 }
1590 #endif
1591
1592 void
1593 siop_dump_acb(acb)
1594 struct siop_acb *acb;
1595 {
1596 u_char *b = (u_char *) &acb->cmd;
1597 int i;
1598
1599 printf("acb@%p ", acb);
1600 if (acb->xs == NULL) {
1601 printf("<unused>\n");
1602 return;
1603 }
1604 printf("(%d:%d) flags %2x clen %2d cmd ",
1605 acb->xs->xs_periph->periph_target,
1606 acb->xs->xs_periph->periph_lun, acb->flags, acb->clen);
1607 for (i = acb->clen; i; --i)
1608 printf(" %02x", *b++);
1609 printf("\n");
1610 printf(" xs: %p data %p:%04x ", acb->xs, acb->xs->data,
1611 acb->xs->datalen);
1612 printf("va %p:%lx ", acb->iob_buf, acb->iob_len);
1613 printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen);
1614 }
1615
1616 void
1617 siop_dump(sc)
1618 struct siop_softc *sc;
1619 {
1620 struct siop_acb *acb;
1621 siop_regmap_p rp = sc->sc_siopp;
1622 int s;
1623 int i;
1624
1625 s = splbio();
1626 #if SIOP_TRACE_SIZE
1627 siop_dump_trace();
1628 #endif
1629 printf("%s@%p regs %p istat %x\n",
1630 sc->sc_dev.dv_xname, sc, rp, rp->siop_istat);
1631 if ((acb = sc->free_list.tqh_first) > 0) {
1632 printf("Free list:\n");
1633 while (acb) {
1634 siop_dump_acb(acb);
1635 acb = acb->chain.tqe_next;
1636 }
1637 }
1638 if ((acb = sc->ready_list.tqh_first) > 0) {
1639 printf("Ready list:\n");
1640 while (acb) {
1641 siop_dump_acb(acb);
1642 acb = acb->chain.tqe_next;
1643 }
1644 }
1645 if ((acb = sc->nexus_list.tqh_first) > 0) {
1646 printf("Nexus list:\n");
1647 while (acb) {
1648 siop_dump_acb(acb);
1649 acb = acb->chain.tqe_next;
1650 }
1651 }
1652 if (sc->sc_nexus) {
1653 printf("Nexus:\n");
1654 siop_dump_acb(sc->sc_nexus);
1655 }
1656 for (i = 0; i < 8; ++i) {
1657 if (sc->sc_tinfo[i].cmds > 2) {
1658 printf("tgt %d: cmds %d disc %d lubusy %x\n",
1659 i, sc->sc_tinfo[i].cmds,
1660 sc->sc_tinfo[i].dconns,
1661 sc->sc_tinfo[i].lubusy);
1662 }
1663 }
1664 splx(s);
1665 }
1666 #endif
1667