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