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