siop.c revision 1.39 1 /* $NetBSD: siop.c,v 1.39 1998/07/04 22:18:17 jonathan Exp $ */
2
3 /*
4 * Copyright (c) 1994 Michael L. Hitch
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Van Jacobson of Lawrence Berkeley Laboratory.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)siop.c 7.5 (Berkeley) 5/4/91
40 */
41
42 /*
43 * AMIGA 53C710 scsi adaptor driver
44 */
45
46 #include "opt_ddb.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/disklabel.h>
52 #include <sys/dkstat.h>
53 #include <sys/buf.h>
54 #include <sys/malloc.h>
55 #include <dev/scsipi/scsi_all.h>
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsiconf.h>
58 #include <machine/cpu.h>
59 #include <amiga/amiga/custom.h>
60 #include <amiga/amiga/isr.h>
61 #include <amiga/dev/siopreg.h>
62 #include <amiga/dev/siopvar.h>
63
64 /*
65 * SCSI delays
66 * In u-seconds, primarily for state changes on the SPC.
67 */
68 #define SCSI_CMD_WAIT 500000 /* wait per step of 'immediate' cmds */
69 #define SCSI_DATA_WAIT 500000 /* wait per data in/out step */
70 #define SCSI_INIT_WAIT 500000 /* wait per step (both) during init */
71
72 void siop_select __P((struct siop_softc *));
73 void siopabort __P((struct siop_softc *, siop_regmap_p, char *));
74 void sioperror __P((struct siop_softc *, siop_regmap_p, u_char));
75 void siopstart __P((struct siop_softc *));
76 int siop_checkintr __P((struct siop_softc *, u_char, u_char, u_char, int *));
77 void siopreset __P((struct siop_softc *));
78 void siopsetdelay __P((int));
79 void siop_scsidone __P((struct siop_acb *, int));
80 void siop_sched __P((struct siop_softc *));
81 int siop_poll __P((struct siop_softc *, struct siop_acb *));
82 void siopintr __P((struct siop_softc *));
83 void scsi_period_to_siop __P((struct siop_softc *, int));
84 void siop_start __P((struct siop_softc *, int, int, u_char *, int, u_char *, int));
85 void siop_dump_acb __P((struct siop_acb *));
86
87 /* 53C710 script */
88 const
89 #include <amiga/dev/siop_script.out>
90
91 /* default to not inhibit sync negotiation on any drive */
92 u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
93 u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3};
94 int siop_no_dma = 0;
95
96 int siop_reset_delay = 250; /* delay after reset, in milleseconds */
97
98 int siop_cmd_wait = SCSI_CMD_WAIT;
99 int siop_data_wait = SCSI_DATA_WAIT;
100 int siop_init_wait = SCSI_INIT_WAIT;
101
102 #ifdef DEBUG_SYNC
103 /*
104 * sync period transfer lookup - only valid for 66Mhz clock
105 */
106 static struct {
107 unsigned char p; /* period from sync request message */
108 unsigned char r; /* siop_period << 4 | sbcl */
109 } sync_tab[] = {
110 { 60/4, 0<<4 | 1},
111 { 76/4, 1<<4 | 1},
112 { 92/4, 2<<4 | 1},
113 { 92/4, 0<<4 | 2},
114 {108/4, 3<<4 | 1},
115 {116/4, 1<<4 | 2},
116 {120/4, 4<<4 | 1},
117 {120/4, 0<<4 | 3},
118 {136/4, 5<<4 | 1},
119 {140/4, 2<<4 | 2},
120 {152/4, 6<<4 | 1},
121 {152/4, 1<<4 | 3},
122 {164/4, 3<<4 | 2},
123 {168/4, 7<<4 | 1},
124 {180/4, 2<<4 | 3},
125 {184/4, 4<<4 | 2},
126 {208/4, 5<<4 | 2},
127 {212/4, 3<<4 | 3},
128 {232/4, 6<<4 | 2},
129 {240/4, 4<<4 | 3},
130 {256/4, 7<<4 | 2},
131 {272/4, 5<<4 | 3},
132 {300/4, 6<<4 | 3},
133 {332/4, 7<<4 | 3}
134 };
135 #endif
136
137 #ifdef DEBUG
138 /*
139 * 0x01 - full debug
140 * 0x02 - DMA chaining
141 * 0x04 - siopintr
142 * 0x08 - phase mismatch
143 * 0x10 - <not used>
144 * 0x20 - panic on unhandled exceptions
145 * 0x100 - disconnect/reselect
146 */
147 int siop_debug = 0;
148 int siopsync_debug = 0;
149 int siopdma_hits = 0;
150 int siopdma_misses = 0;
151 int siopchain_ints = 0;
152 int siopstarts = 0;
153 int siopints = 0;
154 int siopphmm = 0;
155 #define SIOP_TRACE_SIZE 128
156 #define SIOP_TRACE(a,b,c,d) \
157 siop_trbuf[siop_trix] = (a); \
158 siop_trbuf[siop_trix+1] = (b); \
159 siop_trbuf[siop_trix+2] = (c); \
160 siop_trbuf[siop_trix+3] = (d); \
161 siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1);
162 u_char siop_trbuf[SIOP_TRACE_SIZE];
163 int siop_trix;
164 void siop_dump __P((struct siop_softc *));
165 void siop_dump_trace __P((void));
166 #else
167 #define SIOP_TRACE(a,b,c,d)
168 #endif
169
170
171 /*
172 * default minphys routine for siop based controllers
173 */
174 void
175 siop_minphys(bp)
176 struct buf *bp;
177 {
178
179 /*
180 * No max transfer at this level.
181 */
182 minphys(bp);
183 }
184
185 /*
186 * used by specific siop controller
187 *
188 */
189 int
190 siop_scsicmd(xs)
191 struct scsipi_xfer *xs;
192 {
193 struct siop_acb *acb;
194 struct siop_softc *sc;
195 struct scsipi_link *slp;
196 int flags, s;
197
198 slp = xs->sc_link;
199 sc = slp->adapter_softc;
200 flags = xs->flags;
201
202 /* XXXX ?? */
203 if (flags & SCSI_DATA_UIO)
204 panic("siop: scsi data uio requested");
205
206 /* XXXX ?? */
207 if (sc->sc_nexus && flags & SCSI_POLL)
208 /* panic("siop_scsicmd: busy");*/
209 printf("siop_scsicmd: busy\n");
210
211 s = splbio();
212 acb = sc->free_list.tqh_first;
213 if (acb) {
214 TAILQ_REMOVE(&sc->free_list, acb, chain);
215 }
216 splx(s);
217
218 if (acb == NULL) {
219 xs->error = XS_DRIVER_STUFFUP;
220 return(TRY_AGAIN_LATER);
221 }
222
223 acb->flags = ACB_ACTIVE;
224 acb->xs = xs;
225 bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
226 acb->clen = xs->cmdlen;
227 acb->daddr = xs->data;
228 acb->dleft = xs->datalen;
229
230 s = splbio();
231 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
232
233 if (sc->sc_nexus == NULL)
234 siop_sched(sc);
235
236 splx(s);
237
238 if (flags & SCSI_POLL || siop_no_dma)
239 return(siop_poll(sc, acb));
240 return(SUCCESSFULLY_QUEUED);
241 }
242
243 int
244 siop_poll(sc, acb)
245 struct siop_softc *sc;
246 struct siop_acb *acb;
247 {
248 siop_regmap_p rp = sc->sc_siopp;
249 struct scsipi_xfer *xs = acb->xs;
250 int i;
251 int status;
252 u_char istat;
253 u_char dstat;
254 u_char sstat0;
255 int s;
256 int to;
257
258 s = splbio();
259 to = xs->timeout / 1000;
260 if (sc->nexus_list.tqh_first)
261 printf("%s: siop_poll called with disconnected device\n",
262 sc->sc_dev.dv_xname);
263 for (;;) {
264 /* use cmd_wait values? */
265 i = 50000;
266 /* XXX spl0(); */
267 while (((istat = rp->siop_istat) &
268 (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
269 if (--i <= 0) {
270 #ifdef DEBUG
271 printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
272 xs->sc_link->scsipi_scsi.target, acb->cmd.opcode,
273 rp->siop_sbcl, rp->siop_dsp,
274 rp->siop_dsp - sc->sc_scriptspa,
275 *((long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
276 #endif
277 i = 50000;
278 --to;
279 if (to <= 0) {
280 siopreset(sc);
281 return(COMPLETE);
282 }
283 }
284 delay(20);
285 }
286 sstat0 = rp->siop_sstat0;
287 dstat = rp->siop_dstat;
288 if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
289 if (acb != sc->sc_nexus)
290 printf("%s: siop_poll disconnected device completed\n",
291 sc->sc_dev.dv_xname);
292 else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
293 sc->sc_flags &= ~SIOP_INTSOFF;
294 rp->siop_sien = sc->sc_sien;
295 rp->siop_dien = sc->sc_dien;
296 }
297 siop_scsidone(sc->sc_nexus, status);
298 }
299 if (xs->flags & ITSDONE)
300 break;
301 }
302 splx(s);
303 return (COMPLETE);
304 }
305
306 /*
307 * start next command that's ready
308 */
309 void
310 siop_sched(sc)
311 struct siop_softc *sc;
312 {
313 struct scsipi_link *slp;
314 struct siop_acb *acb;
315 int i;
316
317 #ifdef DEBUG
318 if (sc->sc_nexus) {
319 printf("%s: siop_sched- nexus %p/%d ready %p/%d\n",
320 sc->sc_dev.dv_xname, sc->sc_nexus,
321 sc->sc_nexus->xs->sc_link->scsipi_scsi.target,
322 sc->ready_list.tqh_first,
323 sc->ready_list.tqh_first->xs->sc_link->scsipi_scsi.target);
324 return;
325 }
326 #endif
327 for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
328 slp = acb->xs->sc_link;
329 i = slp->scsipi_scsi.target;
330 if(!(sc->sc_tinfo[i].lubusy & (1 << slp->scsipi_scsi.lun))) {
331 struct siop_tinfo *ti = &sc->sc_tinfo[i];
332
333 TAILQ_REMOVE(&sc->ready_list, acb, chain);
334 sc->sc_nexus = acb;
335 slp = acb->xs->sc_link;
336 ti = &sc->sc_tinfo[slp->scsipi_scsi.target];
337 ti->lubusy |= (1 << slp->scsipi_scsi.lun);
338 break;
339 }
340 }
341
342 if (acb == NULL) {
343 #ifdef DEBUGXXX
344 printf("%s: siop_sched didn't find ready command\n",
345 sc->sc_dev.dv_xname);
346 #endif
347 return;
348 }
349
350 if (acb->xs->flags & SCSI_RESET)
351 siopreset(sc);
352
353 #if 0
354 acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5; /* XXXX */
355 #endif
356 ++sc->sc_active;
357 siop_select(sc);
358 }
359
360 void
361 siop_scsidone(acb, stat)
362 struct siop_acb *acb;
363 int stat;
364 {
365 struct scsipi_xfer *xs;
366 struct scsipi_link *slp;
367 struct siop_softc *sc;
368 int dosched = 0;
369
370 if (acb == NULL || (xs = acb->xs) == NULL) {
371 #ifdef DIAGNOSTIC
372 printf("siop_scsidone: NULL acb or scsipi_xfer\n");
373 #if defined(DEBUG) && defined(DDB)
374 Debugger();
375 #endif
376 #endif
377 return;
378 }
379 slp = xs->sc_link;
380 sc = slp->adapter_softc;
381 /*
382 * is this right?
383 */
384 xs->status = stat;
385
386 if (xs->error == XS_NOERROR && !(acb->flags & ACB_CHKSENSE)) {
387 if (stat == SCSI_CHECK) {
388 struct scsipi_sense *ss = (void *)&acb->cmd;
389 bzero(ss, sizeof(*ss));
390 ss->opcode = REQUEST_SENSE;
391 ss->byte2 = slp->scsipi_scsi.lun << 5;
392 ss->length = sizeof(struct scsipi_sense_data);
393 acb->clen = sizeof(*ss);
394 acb->daddr = (char *)&xs->sense.scsi_sense;
395 acb->dleft = sizeof(struct scsipi_sense_data);
396 acb->flags = ACB_ACTIVE | ACB_CHKSENSE;
397 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
398 --sc->sc_active;
399 sc->sc_tinfo[slp->scsipi_scsi.target].lubusy &=
400 ~(1 << slp->scsipi_scsi.lun);
401 sc->sc_tinfo[slp->scsipi_scsi.target].senses++;
402 if (sc->sc_nexus == acb) {
403 sc->sc_nexus = NULL;
404 siop_sched(sc);
405 }
406 SIOP_TRACE('d','s',0,0)
407 return;
408 }
409 }
410 if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE)) {
411 xs->error = XS_SENSE;
412 } else {
413 xs->resid = 0; /* XXXX */
414 }
415 #if whataboutthisone
416 case SCSI_BUSY:
417 xs->error = XS_BUSY;
418 break;
419 #endif
420 xs->flags |= ITSDONE;
421
422 /*
423 * Remove the ACB from whatever queue it's on. We have to do a bit of
424 * a hack to figure out which queue it's on. Note that it is *not*
425 * necessary to cdr down the ready queue, but we must cdr down the
426 * nexus queue and see if it's there, so we can mark the unit as no
427 * longer busy. This code is sickening, but it works.
428 */
429 if (acb == sc->sc_nexus) {
430 sc->sc_nexus = NULL;
431 sc->sc_tinfo[slp->scsipi_scsi.target].lubusy &=
432 ~(1<<slp->scsipi_scsi.lun);
433 if (sc->ready_list.tqh_first)
434 dosched = 1; /* start next command */
435 --sc->sc_active;
436 SIOP_TRACE('d','a',stat,0)
437 } else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
438 TAILQ_REMOVE(&sc->ready_list, acb, chain);
439 SIOP_TRACE('d','r',stat,0)
440 } else {
441 register struct siop_acb *acb2;
442 for (acb2 = sc->nexus_list.tqh_first; acb2;
443 acb2 = acb2->chain.tqe_next)
444 if (acb2 == acb) {
445 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
446 sc->sc_tinfo[slp->scsipi_scsi.target].lubusy
447 &= ~(1<<slp->scsipi_scsi.lun);
448 --sc->sc_active;
449 break;
450 }
451 if (acb2)
452 ;
453 else if (acb->chain.tqe_next) {
454 TAILQ_REMOVE(&sc->ready_list, acb, chain);
455 --sc->sc_active;
456 } else {
457 printf("%s: can't find matching acb\n",
458 sc->sc_dev.dv_xname);
459 #ifdef DDB
460 /* Debugger(); */
461 #endif
462 }
463 SIOP_TRACE('d','n',stat,0);
464 }
465 /* Put it on the free list. */
466 acb->flags = ACB_FREE;
467 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
468
469 sc->sc_tinfo[slp->scsipi_scsi.target].cmds++;
470
471 scsipi_done(xs);
472
473 if (dosched && sc->sc_nexus == NULL)
474 siop_sched(sc);
475 }
476
477 void
478 siopabort(sc, rp, where)
479 register struct siop_softc *sc;
480 siop_regmap_p rp;
481 char *where;
482 {
483 #ifdef fix_this
484 int i;
485 #endif
486
487 printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
488 sc->sc_dev.dv_xname,
489 where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
490
491 if (sc->sc_active > 0) {
492 #ifdef TODO
493 SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
494 WAIT_CIP (rp);
495
496 GET_SBIC_asr (rp, asr);
497 if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
498 {
499 /* ok, get more drastic.. */
500
501 SET_SBIC_cmd (rp, SBIC_CMD_RESET);
502 delay(25);
503 SBIC_WAIT(rp, SBIC_ASR_INT, 0);
504 GET_SBIC_csr (rp, csr); /* clears interrupt also */
505
506 return;
507 }
508
509 do
510 {
511 SBIC_WAIT (rp, SBIC_ASR_INT, 0);
512 GET_SBIC_csr (rp, csr);
513 }
514 while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
515 && (csr != SBIC_CSR_CMD_INVALID));
516 #endif
517
518 /* lets just hope it worked.. */
519 #ifdef fix_this
520 for (i = 0; i < 2; ++i) {
521 if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
522 sc->sc_cur) {
523 printf ("siopabort: cleanup!\n");
524 sc->sc_iob[i].sc_xs = NULL;
525 }
526 }
527 #endif /* fix_this */
528 /* sc->sc_active = 0; */
529 }
530 }
531
532 void
533 siopinitialize(sc)
534 struct siop_softc *sc;
535 {
536 int i;
537 u_int inhibit_sync;
538 extern u_long scsi_nosync;
539 extern int shift_nosync;
540
541 /*
542 * Need to check that scripts is on a long word boundary
543 * Also should verify that dev doesn't span non-contiguous
544 * physical pages.
545 */
546 sc->sc_scriptspa = kvtop((caddr_t)scripts);
547
548 /*
549 * malloc sc_acb to ensure that DS is on a long word boundary.
550 */
551
552 MALLOC(sc->sc_acb, struct siop_acb *,
553 sizeof(struct siop_acb) * SIOP_NACB, M_DEVBUF, M_NOWAIT);
554 if (sc->sc_acb == NULL)
555 panic("siopinitialize: ACB malloc failed!");
556
557 sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
558 sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
559 sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
560 sc->sc_minsync = sc->sc_tcp[1]; /* in 4ns units */
561 if (sc->sc_minsync < 25)
562 sc->sc_minsync = 25;
563 if (sc->sc_clock_freq <= 25) {
564 sc->sc_dcntl |= 0x80; /* SCLK/1 */
565 sc->sc_tcp[0] = sc->sc_tcp[1];
566 } else if (sc->sc_clock_freq <= 37) {
567 sc->sc_dcntl |= 0x40; /* SCLK/1.5 */
568 sc->sc_tcp[0] = sc->sc_tcp[2];
569 } else if (sc->sc_clock_freq <= 50) {
570 sc->sc_dcntl |= 0x00; /* SCLK/2 */
571 sc->sc_tcp[0] = sc->sc_tcp[3];
572 } else {
573 sc->sc_dcntl |= 0xc0; /* SCLK/3 */
574 sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
575 }
576
577 if (scsi_nosync) {
578 inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
579 shift_nosync += 8;
580 #ifdef DEBUG
581 if (inhibit_sync)
582 printf("%s: Inhibiting synchronous transfer %02x\n",
583 sc->sc_dev.dv_xname, inhibit_sync);
584 #endif
585 for (i = 0; i < 8; ++i)
586 if (inhibit_sync & (1 << i))
587 siop_inhibit_sync[i] = 1;
588 }
589
590 siopreset (sc);
591 }
592
593 void
594 siopreset(sc)
595 struct siop_softc *sc;
596 {
597 siop_regmap_p rp;
598 u_int i, s;
599 u_char dummy;
600 struct siop_acb *acb;
601
602 rp = sc->sc_siopp;
603
604 if (sc->sc_flags & SIOP_ALIVE)
605 siopabort(sc, rp, "reset");
606
607 printf("%s: ", sc->sc_dev.dv_xname); /* XXXX */
608
609 s = splbio();
610
611 /*
612 * Reset the chip
613 * XXX - is this really needed?
614 */
615 rp->siop_istat |= SIOP_ISTAT_ABRT; /* abort current script */
616 rp->siop_istat |= SIOP_ISTAT_RST; /* reset chip */
617 rp->siop_istat &= ~SIOP_ISTAT_RST;
618 /*
619 * Reset SCSI bus (do we really want this?)
620 */
621 rp->siop_sien = 0;
622 rp->siop_scntl1 |= SIOP_SCNTL1_RST;
623 delay(1);
624 rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
625
626 /*
627 * Set up various chip parameters
628 */
629 rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
630 rp->siop_scntl1 = SIOP_SCNTL1_ESR;
631 rp->siop_dcntl = sc->sc_dcntl;
632 rp->siop_dmode = 0x80; /* burst length = 4 */
633 rp->siop_sien = 0x00; /* don't enable interrupts yet */
634 rp->siop_dien = 0x00; /* don't enable interrupts yet */
635 rp->siop_scid = 1 << sc->sc_link.scsipi_scsi.adapter_target;
636 rp->siop_dwt = 0x00;
637 rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
638 rp->siop_ctest7 |= sc->sc_ctest7;
639
640 /* will need to re-negotiate sync xfers */
641 bzero(&sc->sc_sync, sizeof (sc->sc_sync));
642
643 i = rp->siop_istat;
644 if (i & SIOP_ISTAT_SIP)
645 dummy = rp->siop_sstat0;
646 if (i & SIOP_ISTAT_DIP)
647 dummy = rp->siop_dstat;
648
649 splx (s);
650
651 delay (siop_reset_delay * 1000);
652 printf("siop id %d reset V%d\n", sc->sc_link.scsipi_scsi.adapter_target,
653 rp->siop_ctest8 >> 4);
654
655 if ((sc->sc_flags & SIOP_ALIVE) == 0) {
656 TAILQ_INIT(&sc->ready_list);
657 TAILQ_INIT(&sc->nexus_list);
658 TAILQ_INIT(&sc->free_list);
659 sc->sc_nexus = NULL;
660 acb = sc->sc_acb;
661 bzero(acb, sizeof(struct siop_acb) * SIOP_NACB);
662 for (i = 0; i < SIOP_NACB; i++) {
663 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
664 acb++;
665 }
666 bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
667 } else {
668 if (sc->sc_nexus != NULL) {
669 sc->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
670 siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
671 }
672 while ((acb = sc->nexus_list.tqh_first) > 0) {
673 acb->xs->error = XS_DRIVER_STUFFUP;
674 siop_scsidone(acb, acb->stat[0]);
675 }
676 }
677
678 sc->sc_flags |= SIOP_ALIVE;
679 sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
680 /* enable SCSI and DMA interrupts */
681 sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
682 SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
683 sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
684 /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
685 rp->siop_sien = sc->sc_sien;
686 rp->siop_dien = sc->sc_dien;
687 }
688
689 /*
690 * Setup Data Storage for 53C710 and start SCRIPTS processing
691 */
692
693 void
694 siop_start (sc, target, lun, cbuf, clen, buf, len)
695 struct siop_softc *sc;
696 int target;
697 int lun;
698 u_char *cbuf;
699 int clen;
700 u_char *buf;
701 int len;
702 {
703 siop_regmap_p rp = sc->sc_siopp;
704 int nchain;
705 int count, tcount;
706 char *addr, *dmaend;
707 struct siop_acb *acb = sc->sc_nexus;
708 #ifdef DEBUG
709 int i;
710 #endif
711
712 #ifdef DEBUG
713 if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
714 printf ("ACK! siop was busy: rp %p script %p dsa %p active %ld\n",
715 rp, &scripts, &acb->ds, sc->sc_active);
716 printf ("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
717 rp->siop_istat, rp->siop_sfbr, rp->siop_lcrc,
718 rp->siop_sien, rp->siop_dien);
719 #ifdef DDB
720 /*Debugger();*/
721 #endif
722 }
723 #endif
724 acb->msgout[0] = MSG_IDENTIFY | lun;
725 if (siop_allow_disc[target] & 2 ||
726 (siop_allow_disc[target] && len == 0))
727 acb->msgout[0] = MSG_IDENTIFY_DR | lun;
728 acb->status = 0;
729 acb->stat[0] = -1;
730 acb->msg[0] = -1;
731 acb->ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].sxfer << 8);
732 acb->ds.idlen = 1;
733 acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]);
734 acb->ds.cmdlen = clen;
735 acb->ds.cmdbuf = (char *) kvtop(cbuf);
736 acb->ds.stslen = 1;
737 acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]);
738 acb->ds.msglen = 1;
739 acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]);
740 acb->msg[1] = -1;
741 acb->ds.msginlen = 1;
742 acb->ds.extmsglen = 1;
743 acb->ds.synmsglen = 3;
744 acb->ds.msginbuf = (char *) kvtop(&acb->msg[1]);
745 acb->ds.extmsgbuf = (char *) kvtop(&acb->msg[2]);
746 acb->ds.synmsgbuf = (char *) kvtop(&acb->msg[3]);
747 bzero(&acb->ds.chain, sizeof (acb->ds.chain));
748
749 if (sc->sc_sync[target].state == SYNC_START) {
750 if (siop_inhibit_sync[target]) {
751 sc->sc_sync[target].state = SYNC_DONE;
752 sc->sc_sync[target].sbcl = 0;
753 sc->sc_sync[target].sxfer = 0;
754 #ifdef DEBUG
755 if (siopsync_debug)
756 printf ("Forcing target %d asynchronous\n", target);
757 #endif
758 }
759 else {
760 acb->msg[2] = -1;
761 acb->msgout[1] = MSG_EXT_MESSAGE;
762 acb->msgout[2] = 3;
763 acb->msgout[3] = MSG_SYNC_REQ;
764 #ifdef MAXTOR_SYNC_KLUDGE
765 acb->msgout[4] = 50 / 4; /* ask for ridiculous period */
766 #else
767 acb->msgout[4] = sc->sc_minsync;
768 #endif
769 acb->msgout[5] = SIOP_MAX_OFFSET;
770 acb->ds.idlen = 6;
771 sc->sc_sync[target].state = SYNC_SENT;
772 #ifdef DEBUG
773 if (siopsync_debug)
774 printf ("Sending sync request to target %d\n", target);
775 #endif
776 }
777 }
778
779 /*
780 * Build physical DMA addresses for scatter/gather I/O
781 */
782 acb->iob_buf = buf;
783 acb->iob_len = len;
784 acb->iob_curbuf = acb->iob_curlen = 0;
785 nchain = 0;
786 count = len;
787 addr = buf;
788 dmaend = NULL;
789 while (count > 0) {
790 acb->ds.chain[nchain].databuf = (char *) kvtop (addr);
791 if (count < (tcount = NBPG - ((int) addr & PGOFSET)))
792 tcount = count;
793 acb->ds.chain[nchain].datalen = tcount;
794 addr += tcount;
795 count -= tcount;
796 if (acb->ds.chain[nchain].databuf == dmaend) {
797 dmaend += acb->ds.chain[nchain].datalen;
798 acb->ds.chain[nchain].datalen = 0;
799 acb->ds.chain[--nchain].datalen += tcount;
800 #ifdef DEBUG
801 ++siopdma_hits;
802 #endif
803 }
804 else {
805 dmaend = acb->ds.chain[nchain].databuf +
806 acb->ds.chain[nchain].datalen;
807 acb->ds.chain[nchain].datalen = tcount;
808 #ifdef DEBUG
809 if (nchain) /* Don't count miss on first one */
810 ++siopdma_misses;
811 #endif
812 }
813 ++nchain;
814 }
815 #ifdef DEBUG
816 if (nchain != 1 && len != 0 && siop_debug & 3) {
817 printf ("DMA chaining set: %d\n", nchain);
818 for (i = 0; i < nchain; ++i) {
819 printf (" [%d] %8p %lx\n", i, acb->ds.chain[i].databuf,
820 acb->ds.chain[i].datalen);
821 }
822 }
823 #endif
824
825 /* push data cache for all data the 53c710 needs to access */
826 dma_cachectl ((caddr_t)acb, sizeof (struct siop_acb));
827 dma_cachectl (cbuf, clen);
828 if (buf != NULL && len != 0)
829 dma_cachectl (buf, len);
830 #ifdef DEBUG
831 if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
832 printf ("ACK! siop was busy at start: rp %p script %p dsa %p active %ld\n",
833 rp, &scripts, &acb->ds, sc->sc_active);
834 #ifdef DDB
835 /*Debugger();*/
836 #endif
837 }
838 #endif
839 if (sc->nexus_list.tqh_first == NULL) {
840 if (rp->siop_istat & SIOP_ISTAT_CON)
841 printf("%s: siop_select while connected?\n",
842 sc->sc_dev.dv_xname);
843 rp->siop_temp = 0;
844 rp->siop_sbcl = sc->sc_sync[target].sbcl;
845 rp->siop_dsa = kvtop((caddr_t)&acb->ds);
846 rp->siop_dsp = sc->sc_scriptspa;
847 SIOP_TRACE('s',1,0,0)
848 } else {
849 if ((rp->siop_istat & SIOP_ISTAT_CON) == 0) {
850 rp->siop_istat = SIOP_ISTAT_SIGP;
851 SIOP_TRACE('s',2,0,0);
852 }
853 else {
854 SIOP_TRACE('s',3,rp->siop_istat,0);
855 }
856 }
857 #ifdef DEBUG
858 ++siopstarts;
859 #endif
860 }
861
862 /*
863 * Process a DMA or SCSI interrupt from the 53C710 SIOP
864 */
865
866 int
867 siop_checkintr(sc, istat, dstat, sstat0, status)
868 struct siop_softc *sc;
869 u_char istat;
870 u_char dstat;
871 u_char sstat0;
872 int *status;
873 {
874 siop_regmap_p rp = sc->sc_siopp;
875 struct siop_acb *acb = sc->sc_nexus;
876 int target = 0;
877 int dfifo, dbc, sstat1;
878
879 dfifo = rp->siop_dfifo;
880 dbc = rp->siop_dbc0;
881 sstat1 = rp->siop_sstat1;
882 rp->siop_ctest8 |= SIOP_CTEST8_CLF;
883 while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT)
884 ;
885 rp->siop_ctest8 &= ~SIOP_CTEST8_CLF;
886 #ifdef DEBUG
887 ++siopints;
888 #if 0
889 if (siop_debug & 0x100) {
890 DCIAS(&acb->stat[0]); /* XXX */
891 printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
892 istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]);
893 printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
894 acb->msg[0], acb->msg[1], acb->msg[2],
895 acb->msg[3], acb->msg[4], acb->msg[5]);
896 }
897 #endif
898 if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa ||
899 rp->siop_dsp >= sc->sc_scriptspa + sizeof(scripts))) {
900 printf ("%s: dsp not within script dsp %lx scripts %lx:%lx",
901 sc->sc_dev.dv_xname, rp->siop_dsp, sc->sc_scriptspa,
902 sc->sc_scriptspa + sizeof(scripts));
903 printf(" istat %x dstat %x sstat0 %x\n",
904 istat, dstat, sstat0);
905 #ifdef DDB
906 Debugger();
907 #endif
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->scsipi_scsi.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 #if defined(DEBUG) && defined(DDB)
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->scsipi_scsi.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->scsipi_scsi.target].lubusy
1247 &= ~(1 << sc->sc_nexus->xs->sc_link->scsipi_scsi.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 =
1266 sc->sc_sync[acb->xs->sc_link->scsipi_scsi.target].sxfer;
1267 rp->siop_sbcl =
1268 sc->sc_sync[acb->xs->sc_link->scsipi_scsi.target].sbcl;
1269 break;
1270 }
1271 if (acb == NULL) {
1272 printf("%s: target ID %02x reselect nexus_list %p\n",
1273 sc->sc_dev.dv_xname, reselid,
1274 sc->nexus_list.tqh_first);
1275 panic("unable to find reselecting device");
1276 }
1277 dma_cachectl ((caddr_t)acb, sizeof(*acb));
1278 rp->siop_temp = 0;
1279 rp->siop_dcntl |= SIOP_DCNTL_STD;
1280 return (0);
1281 }
1282 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) {
1283 #ifdef DEBUG
1284 u_short ctest2 = rp->siop_ctest2;
1285
1286 /* reselect was interrupted (by Sig_P or select) */
1287 if (siop_debug & 0x100 ||
1288 (ctest2 & SIOP_CTEST2_SIGP) == 0)
1289 printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
1290 sc->sc_dev.dv_xname, rp->siop_scntl1,
1291 ctest2, rp->siop_sfbr, istat, rp->siop_istat);
1292 #endif
1293 /* XXX assumes it was not select */
1294 if (sc->sc_nexus == NULL) {
1295 #ifdef DEBUG
1296 printf("%s: reselect interrupted, sc_nexus == NULL\n",
1297 sc->sc_dev.dv_xname);
1298 #if 0
1299 siop_dump(sc);
1300 #ifdef DDB
1301 Debugger();
1302 #endif
1303 #endif
1304 #endif
1305 rp->siop_dcntl |= SIOP_DCNTL_STD;
1306 return(0);
1307 }
1308 target = sc->sc_nexus->xs->sc_link->scsipi_scsi.target;
1309 rp->siop_temp = 0;
1310 rp->siop_dsa = kvtop((caddr_t)&sc->sc_nexus->ds);
1311 rp->siop_sxfer = sc->sc_sync[target].sxfer;
1312 rp->siop_sbcl = sc->sc_sync[target].sbcl;
1313 rp->siop_dsp = sc->sc_scriptspa;
1314 return (0);
1315 }
1316 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) {
1317 if (acb == NULL)
1318 printf("%s: Bad message-in with no active command?\n",
1319 sc->sc_dev.dv_xname);
1320 /* Unrecognized message in byte */
1321 dma_cachectl (&acb->msg[1],1);
1322 printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n",
1323 sc->sc_dev.dv_xname, rp->siop_sfbr, acb->msg[1], rp->siop_sbcl);
1324 /* what should be done here? */
1325 DCIAS(kvtop(&acb->msg[1]));
1326 rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
1327 return (0);
1328 }
1329 if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
1330 /* Status phase wasn't followed by message in phase? */
1331 printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n",
1332 sc->sc_dev.dv_xname, rp->siop_sbcl, rp->siop_sbdl);
1333 if (rp->siop_sbcl == 0xa7) {
1334 /* It is now, just continue the script? */
1335 rp->siop_dcntl |= SIOP_DCNTL_STD;
1336 return (0);
1337 }
1338 }
1339 if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
1340 dma_cachectl (&acb->stat[0], 1);
1341 dma_cachectl (&acb->msg[0], 1);
1342 printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n",
1343 rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1],
1344 rp->siop_sbcl);
1345 siopreset (sc);
1346 *status = -1;
1347 return 0; /* siopreset has cleaned up */
1348 }
1349 if (sstat0 & SIOP_SSTAT0_SGE)
1350 printf ("SIOP: SCSI Gross Error\n");
1351 if (sstat0 & SIOP_SSTAT0_PAR)
1352 printf ("SIOP: Parity Error\n");
1353 if (dstat & SIOP_DSTAT_IID)
1354 printf ("SIOP: Invalid instruction detected\n");
1355 bad_phase:
1356 /*
1357 * temporary panic for unhandled conditions
1358 * displays various things about the 53C710 status and registers
1359 * then panics.
1360 * XXXX need to clean this up to print out the info, reset, and continue
1361 */
1362 printf ("siopchkintr: target %x ds %p\n", target, &acb->ds);
1363 printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n", sc->sc_scriptspa,
1364 kvtop((caddr_t)&acb->ds), kvtop((caddr_t)rp), rp->siop_dsp,
1365 *((long *)&rp->siop_dcmd));
1366 printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n",
1367 istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa,
1368 rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr);
1369 #ifdef DEBUG
1370 if (siop_debug & 0x20)
1371 panic("siopchkintr: **** temp ****");
1372 #endif
1373 #ifdef DDB
1374 Debugger ();
1375 #endif
1376 siopreset (sc); /* hard reset */
1377 *status = -1;
1378 return 0; /* siopreset cleaned up */
1379 }
1380
1381 void
1382 siop_select(sc)
1383 struct siop_softc *sc;
1384 {
1385 siop_regmap_p rp;
1386 struct siop_acb *acb = sc->sc_nexus;
1387
1388 #ifdef DEBUG
1389 if (siop_debug & 1)
1390 printf ("%s: select ", sc->sc_dev.dv_xname);
1391 #endif
1392
1393 rp = sc->sc_siopp;
1394 if (acb->xs->flags & SCSI_POLL || siop_no_dma) {
1395 sc->sc_flags |= SIOP_INTSOFF;
1396 sc->sc_flags &= ~SIOP_INTDEFER;
1397 if ((rp->siop_istat & 0x08) == 0) {
1398 rp->siop_sien = 0;
1399 rp->siop_dien = 0;
1400 }
1401 #if 0
1402 } else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
1403 sc->sc_flags &= ~SIOP_INTSOFF;
1404 if ((rp->siop_istat & 0x08) == 0) {
1405 rp->siop_sien = sc->sc_sien;
1406 rp->siop_dien = sc->sc_dien;
1407 }
1408 #endif
1409 }
1410 #ifdef DEBUG
1411 if (siop_debug & 1)
1412 printf ("siop_select: target %x cmd %02x ds %p\n",
1413 acb->xs->sc_link->scsipi_scsi.target, acb->cmd.opcode,
1414 &sc->sc_nexus->ds);
1415 #endif
1416
1417 siop_start(sc, acb->xs->sc_link->scsipi_scsi.target,
1418 acb->xs->sc_link->scsipi_scsi.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->scsipi_scsi.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 ",
1588 acb->xs->sc_link->scsipi_scsi.target,
1589 acb->xs->sc_link->scsipi_scsi.lun, acb->flags, acb->clen);
1590 for (i = acb->clen; i; --i)
1591 printf(" %02x", *b++);
1592 printf("\n");
1593 printf(" xs: %p data %p:%04x ", acb->xs, acb->xs->data,
1594 acb->xs->datalen);
1595 printf("va %p:%lx ", acb->iob_buf, acb->iob_len);
1596 printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen);
1597 }
1598
1599 void
1600 siop_dump(sc)
1601 struct siop_softc *sc;
1602 {
1603 struct siop_acb *acb;
1604 siop_regmap_p rp = sc->sc_siopp;
1605 int s;
1606 int i;
1607
1608 s = splbio();
1609 #if SIOP_TRACE_SIZE
1610 siop_dump_trace();
1611 #endif
1612 printf("%s@%p regs %p istat %x\n",
1613 sc->sc_dev.dv_xname, sc, rp, rp->siop_istat);
1614 if ((acb = sc->free_list.tqh_first) > 0) {
1615 printf("Free list:\n");
1616 while (acb) {
1617 siop_dump_acb(acb);
1618 acb = acb->chain.tqe_next;
1619 }
1620 }
1621 if ((acb = sc->ready_list.tqh_first) > 0) {
1622 printf("Ready list:\n");
1623 while (acb) {
1624 siop_dump_acb(acb);
1625 acb = acb->chain.tqe_next;
1626 }
1627 }
1628 if ((acb = sc->nexus_list.tqh_first) > 0) {
1629 printf("Nexus list:\n");
1630 while (acb) {
1631 siop_dump_acb(acb);
1632 acb = acb->chain.tqe_next;
1633 }
1634 }
1635 if (sc->sc_nexus) {
1636 printf("Nexus:\n");
1637 siop_dump_acb(sc->sc_nexus);
1638 }
1639 for (i = 0; i < 8; ++i) {
1640 if (sc->sc_tinfo[i].cmds > 2) {
1641 printf("tgt %d: cmds %d disc %d senses %d lubusy %x\n",
1642 i, sc->sc_tinfo[i].cmds,
1643 sc->sc_tinfo[i].dconns,
1644 sc->sc_tinfo[i].senses,
1645 sc->sc_tinfo[i].lubusy);
1646 }
1647 }
1648 splx(s);
1649 }
1650 #endif
1651