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