2 * NCR/Symbios/LSI Logic 53c8xx driver for Plan 9
3 * Nigel Roles (nigel@9fs.org)
5 * 27/5/02 Fixed problems with transfers >= 256 * 512
7 * 13/3/01 Fixed microcode to support targets > 7
9 * 01/12/00 Removed previous comments. Fixed a small problem in
10 * mismatch recovery for targets with synchronous offsets of >=16
11 * connected to >=875s. Thanks, Jean.
15 * Read/write mismatch recovery may fail on 53c1010s. Really need to get a manual.
18 #define MAXTARGET 16 /* can be 8 or 16 */
21 #include "../port/lib.h"
26 #include "../port/pci.h"
28 #include "../port/sd.h"
29 extern SDifc sd53c8xxifc;
31 /**********************************/
32 /* Portable configuration macros */
33 /**********************************/
37 /**********************************/
38 /* CPU specific macros */
39 /**********************************/
41 #define PRINTPREFIX "sd53c8xx: "
43 static int idebug = 0;
44 #define KPRINT if(0) iprint
45 #define IPRINT if(idebug) iprint
49 /*******************************/
51 /*******************************/
53 #define DMASEG(x) PCIWADDR(x)
54 #define legetl(x) (*(ulong*)(x))
55 #define lesetl(x,v) (*(ulong*)(x) = (v))
57 #define DMASEG_TO_PADDR(x) ((uintptr)(x)-PCIWINDOW)
58 #define DMASEG_TO_KADDR(x) KADDR(DMASEG_TO_PADDR(x))
59 #define KPTR(x) (((x) == 0) ? nil : DMASEG_TO_KADDR(x))
63 #define SCLK (33 * MEGA)
65 #define SCLK (40 * MEGA)
66 #endif /* INTERNAL_SCLK */
67 #define ULTRA_NOCLOCKDOUBLE_SCLK (80 * MEGA)
69 #define MAXSYNCSCSIRATE (5 * MEGA)
70 #define MAXFASTSYNCSCSIRATE (10 * MEGA)
71 #define MAXULTRASYNCSCSIRATE (20 * MEGA)
72 #define MAXULTRA2SYNCSCSIRATE (40 * MEGA)
73 #define MAXASYNCCORERATE (25 * MEGA)
74 #define MAXSYNCCORERATE (25 * MEGA)
75 #define MAXFASTSYNCCORERATE (50 * MEGA)
76 #define MAXULTRASYNCCORERATE (80 * MEGA)
77 #define MAXULTRA2SYNCCORERATE (160 * MEGA)
90 uchar scntl0; /* 00 */
105 uchar dstat; /* 0c */
110 uchar dsa[4]; /* 10 */
112 uchar istat; /* 14 */
115 uchar ctest0; /* 18 */
120 uchar temp[4]; /* 1c */
122 uchar dfifo; /* 20 */
127 uchar dbc[3]; /* 24 */
130 uchar dnad[4]; /* 28 */
131 uchar dsp[4]; /* 2c */
132 uchar dsps[4]; /* 30 */
134 uchar scratcha[4]; /* 34 */
136 uchar dmode; /* 38 */
141 uchar adder[4]; /* 3c */
143 uchar sien0; /* 40 */
148 uchar slpar; /* 44 */
153 uchar stime0; /* 48 */
158 uchar stest0; /* 4c */
172 uchar scratchb[4]; /* 5c */
175 typedef struct Movedata {
180 typedef enum NegoState {
181 NeitherDone, WideInit, WideResponse, WideDone,
182 SyncInit, SyncResponse, BothDone
186 Allocated, Queued, Active, Done
189 typedef struct Dsa Dsa;
194 uchar flag; /* setbyte(state,3,...) */
196 uchar dmaaddr[4]; /* For block transfer: NCR order (little-endian) */
198 uchar target; /* Target */
201 uchar lun; /* Logical Unit Number */
204 uchar scntl3; /* Sync */
208 uchar next[4]; /* chaining for SCRIPT (NCR byte order) */
210 Dsa *freechain; /* chaining for freelist */
213 uchar scsi_id_buf[4];
214 Movedata msg_out_buf;
219 uchar msg_out[10]; /* enough to include SDTR */
225 typedef enum Feature {
226 BigFifo = 1, /* 536 byte fifo */
227 BurstOpCodeFetch = 2, /* burst fetch opcodes */
228 Prefetch = 4, /* prefetch 8 longwords */
229 LocalRAM = 8, /* 4K longwords of local RAM */
230 Differential = 16, /* Differential support */
231 Wide = 32, /* Wide capable */
232 Ultra = 64, /* Ultra capable */
233 ClockDouble = 128, /* Has clock doubler */
234 ClockQuad = 256, /* Has clock quadrupler (same as Ultra2) */
248 typedef struct Variant {
250 uchar maxrid; /* maximum allowed revision ID */
252 Burst burst; /* codings for max burst */
253 uchar maxsyncoff; /* max synchronous offset */
254 uchar registers; /* number of 32 bit registers */
258 static unsigned char cf2[] = { 6, 2, 3, 4, 6, 8, 12, 16 };
259 #define NULTRA2SCF (sizeof(cf2)/sizeof(cf2[0]))
260 #define NULTRASCF (NULTRA2SCF - 2)
261 #define NSCF (NULTRASCF - 1)
263 typedef struct Controller {
269 uchar synctab[NULTRA2SCF - 1][8];/* table of legal tpfs */
270 NegoState s[MAXTARGET];
271 uchar scntl3[MAXTARGET];
272 uchar sxfer[MAXTARGET];
273 uchar cap[MAXTARGET]; /* capabilities byte from Identify */
274 ushort capvalid; /* bit per target for validity of cap[] */
275 ushort wide; /* bit per target set if wide negotiated */
276 ulong sclk; /* clock speed of controller */
277 uchar clockmult; /* set by synctabinit */
278 uchar ccf; /* CCF bits */
279 uchar tpf; /* best tpf value for this controller */
280 uchar feature; /* requested features */
281 int running; /* is the script processor running? */
282 int ssm; /* single step mode */
283 Ncr *n; /* pointer to registers */
284 Variant *v; /* pointer to variant type */
285 ulong *script; /* where the real script is */
286 ulong scriptpa; /* where the real script is */
292 uchar head[4]; /* head of free list (NCR byte order) */
296 QLock q[MAXTARGET]; /* queues for each target */
299 #define SYNCOFFMASK(c) (((c)->v->maxsyncoff * 2) - 1)
300 #define SSIDMASK(c) (((c)->v->feature & Wide) ? 15 : 7)
303 enum { Abrt = 0x80, Srst = 0x40, Sigp = 0x20, Sem = 0x10, Con = 0x08, Intf = 0x04, Sip = 0x02, Dip = 0x01 };
306 enum { Dfe = 0x80, Mdpe = 0x40, Bf = 0x20, Abrted = 0x10, Ssi = 0x08, Sir = 0x04, Iid = 0x01 };
309 enum { DataOut, DataIn, Cmd, Status, ReservedOut, ReservedIn, MessageOut, MessageIn };
311 static void setmovedata(Movedata*, ulong, ulong);
312 static void advancedata(Movedata*, long);
313 static int bios_set_differential(Controller *c);
315 static char *phase[] = {
316 "data out", "data in", "command", "status",
317 "reserved out", "reserved in", "message out", "message in"
321 #define DEBUGSIZE 10240
322 char debugbuf[DEBUGSIZE];
326 intrprint(char *format, ...)
329 debuglast = debugbuf;
330 debuglast = vseprint(debuglast, debugbuf + (DEBUGSIZE - 1), format, (&format + 1));
340 debuglast = debugbuf;
341 if (debuglast == debugbuf) {
347 screenputs(debugbuf, endp - debugbuf);
349 memmove(debugbuf, endp, debuglast - endp);
350 debuglast -= endp - debugbuf;
355 oprint(char *format, ...)
362 debuglast = debugbuf;
363 debuglast = vseprint(debuglast, debugbuf + (DEBUGSIZE - 1), format, (&format + 1));
369 #include "../pc/sd53c8xx.i"
372 * We used to use a linked list of Dsas with nil as the terminator,
373 * but occasionally the 896 card seems not to notice that the 0
374 * is really a 0, and then it tries to reference the Dsa at address 0.
375 * To address this, we use a sentinel dsa that links back to itself
376 * and has state A_STATE_END. If the card takes an iteration or
377 * two to notice that the state says A_STATE_END, that's no big
378 * deal. Clearly this isn't the right approach, but I'm just
379 * stumped. Even with this, we occasionally get prints about
380 * "WSR set", usually with about the same frequency that the
381 * card used to walk past 0.
386 dsaalloc(Controller *c, int target, int lun)
391 if ((d = c->dsalist.freechain) != nil) {
392 c->dsalist.freechain = d->freechain;
395 IPRINT(PRINTPREFIX "%d/%d: reused dsa %#p\n", target, lun, d);
397 /* c->dsalist must be ilocked */
398 d = xalloc(sizeof *d);
400 panic("sd53c8xx dsaallocnew: no memory");
402 lesetl(d->next, legetl(c->dsalist.head));
403 lesetl(&d->stateb, A_STATE_FREE);
405 lesetl(c->dsalist.head, DMASEG(d));
408 IPRINT(PRINTPREFIX "%d/%d: allocated dsa %#p\n", target, lun, d);
410 lesetl(&d->stateb, A_STATE_ALLOCATED);
411 iunlock(&c->dsalist);
418 dsafree(Controller *c, Dsa *d)
421 d->freechain = c->dsalist.freechain;
422 c->dsalist.freechain = d;
423 lesetl(&d->stateb, A_STATE_FREE);
424 iunlock(&c->dsalist);
428 dsadump(Controller *c)
433 iprint("dsa controller list: c=%p head=%.8lux\n", c, legetl(c->dsalist.head));
434 for(d=KPTR(legetl(c->dsalist.head)); d != nil && d != dsaend; d=KPTR(legetl(d->next))){
436 iprint("\tdsa %p %.8ux %.8ux %.8ux %.8ux %.8ux %.8ux\n", a, a[0], a[1], a[2], a[3], a[4], a[5]);
440 a = KPTR(c->scriptpa+E_dsa_addr);
441 iprint("dsa_addr: %.8ux %.8ux %.8ux %.8ux %.8ux\n",
442 a[0], a[1], a[2], a[3], a[4]);
443 a = KPTR(c->scriptpa+E_issue_addr);
444 iprint("issue_addr: %.8ux %.8ux %.8ux %.8ux %.8ux\n",
445 a[0], a[1], a[2], a[3], a[4]);
447 a = KPTR(c->scriptpa+E_issue_test_begin);
448 e = KPTR(c->scriptpa+E_issue_test_end);
449 iprint("issue_test code (at offset %.8ux):\n", E_issue_test_begin);
453 iprint(" %.8ux", *a);
463 dsafind(Controller *c, uchar target, uchar lun, uchar state)
467 for (d = KPTR(legetl(c->dsalist.head)); d != nil && d != dsaend; d = KPTR(legetl(d->next))) {
468 if (d->target != 0xff && d->target != target)
470 if (lun != 0xff && d->lun != lun)
472 if (state != 0xff && d->stateb != state)
480 dumpncrregs(Controller *c, int intr)
484 int depth = c->v->registers / 4;
487 IPRINT("sa = %.8lux\n", c->scriptpa);
490 KPRINT("sa = %.8lux\n", c->scriptpa);
492 for (i = 0; i < depth; i++) {
494 for (j = 0; j < 4; j++) {
495 int k = j * depth + i;
498 /* display little-endian to make 32-bit values readable */
501 IPRINT(" %.2x%.2x%.2x%.2x %.2x %.2x", p[3], p[2], p[1], p[0], k * 4, (k * 4) + 0x80);
504 KPRINT(" %.2x%.2x%.2x%.2x %.2x %.2x", p[3], p[2], p[1], p[0], k * 4, (k * 4) + 0x80);
518 chooserate(Controller *c, int tpf, int *scfp, int *xferpp)
520 /* find lowest entry >= tpf */
527 if (c->v->feature & Ultra2)
529 else if (c->v->feature & Ultra)
535 * search large clock factors first since this should
536 * result in more reliable transfers
538 for (scf = maxscf; scf >= 1; scf--) {
539 for (xferp = 0; xferp < 8; xferp++) {
540 unsigned char v = c->synctab[scf - 1][xferp];
543 if (v >= tpf && v < besttpf) {
560 synctabinit(Controller *c)
563 unsigned long scsilimit;
565 unsigned long cr, sr;
570 if (c->v->feature & Ultra2)
572 else if (c->v->feature & Ultra)
578 * for chips with no clock doubler, but Ultra capable (e.g. 860, or interestingly the
579 * first spin of the 875), assume 80MHz
580 * otherwise use the internal (33 Mhz) or external (40MHz) default
583 if ((c->v->feature & Ultra) != 0 && (c->v->feature & (ClockDouble | ClockQuad)) == 0)
584 c->sclk = ULTRA_NOCLOCKDOUBLE_SCLK;
589 * otherwise, if the chip is Ultra capable, but has a slow(ish) clock,
593 if (SCLK <= 40000000) {
594 if (c->v->feature & ClockDouble) {
598 else if (c->v->feature & ClockQuad) {
608 /* derive CCF from sclk */
609 /* woebetide anyone with SCLK < 16.7 or > 80MHz */
610 if (c->sclk <= 25 * MEGA)
612 else if (c->sclk <= 3750000)
614 else if (c->sclk <= 50 * MEGA)
616 else if (c->sclk <= 75 * MEGA)
618 else if ((c->v->feature & ClockDouble) && c->sclk <= 80 * MEGA)
620 else if ((c->v->feature & ClockQuad) && c->sclk <= 120 * MEGA)
622 else if ((c->v->feature & ClockQuad) && c->sclk <= 160 * MEGA)
625 for (scf = 1; scf < maxscf; scf++) {
626 /* check for legal core rate */
627 /* round up so we run slower for safety */
628 cr = (c->sclk * 2 + cf2[scf] - 1) / cf2[scf];
629 if (cr <= MAXSYNCCORERATE) {
630 scsilimit = MAXSYNCSCSIRATE;
633 else if (cr <= MAXFASTSYNCCORERATE) {
634 scsilimit = MAXFASTSYNCSCSIRATE;
637 else if ((c->v->feature & Ultra) && cr <= MAXULTRASYNCCORERATE) {
638 scsilimit = MAXULTRASYNCSCSIRATE;
641 else if ((c->v->feature & Ultra2) && cr <= MAXULTRA2SYNCCORERATE) {
642 scsilimit = MAXULTRA2SYNCSCSIRATE;
647 for (xferp = 11; xferp >= 4; xferp--) {
650 /* calculate scsi rate - round up again */
651 /* start from sclk for accuracy */
652 int totaldivide = xferp * cf2[scf];
653 sr = (c->sclk * 2 + totaldivide - 1) / totaldivide;
657 * now work out transfer period
658 * round down now so that period is pessimistic
660 tp = (MEGA * 1000) / sr;
664 if (tp < 25 || tp > 255 * 4)
667 * spot stupid special case for Ultra or Ultra2
668 * while working out factor
679 * now check tpf looks sensible
684 /* scf must be ccf for SCSI 1 */
685 ok = tpf >= 50 && scf == c->ccf;
688 ok = tpf >= 25 && tpf < 50;
692 * must use xferp of 4, or 5 at a pinch
693 * for an Ultra transfer
695 ok = xferp <= 5 && tpf >= 12 && tpf < 25;
698 ok = xferp == 4 && (tpf == 10 || tpf == 11);
705 c->synctab[scf - 1][xferp - 4] = tpf;
710 if (c->v->feature & Ultra2)
714 if (c->v->feature & Ultra)
718 for (; tpf < 256; tpf++) {
719 if (chooserate(c, tpf, &scf, &xferp) == tpf) {
720 unsigned tp = tpf == 10 ? 25 : (tpf == 12 ? 50 : tpf * 4);
721 unsigned long khz = (MEGA + tp - 1) / (tp);
722 KPRINT(PRINTPREFIX "tpf=%d scf=%d.%.1d xferp=%d mhz=%ld.%.3ld\n",
723 tpf, cf2[scf] / 2, (cf2[scf] & 1) ? 5 : 0,
724 xferp + 4, khz / 1000, khz % 1000);
727 c->tpf = tpf; /* note lowest value for controller */
733 synctodsa(Dsa *dsa, Controller *c)
736 KPRINT("synctodsa(dsa=%lux, target=%d, scntl3=%.2lx sxfer=%.2x)\n",
737 dsa, dsa->target, c->scntl3[dsa->target], c->sxfer[dsa->target]);
739 dsa->scntl3 = c->scntl3[dsa->target];
740 dsa->sxfer = c->sxfer[dsa->target];
744 setsync(Dsa *dsa, Controller *c, int target, uchar ultra, uchar scf, uchar xferp, uchar reqack)
747 (c->scntl3[target] & 0x08) | (((scf << 4) | c->ccf | (ultra << 7)) & ~0x08);
748 c->sxfer[target] = (xferp << 5) | reqack;
749 c->s[target] = BothDone;
752 c->n->scntl3 = c->scntl3[target];
753 c->n->sxfer = c->sxfer[target];
758 setasync(Dsa *dsa, Controller *c, int target)
760 setsync(dsa, c, target, 0, c->ccf, 0, 0);
764 setwide(Dsa *dsa, Controller *c, int target, uchar wide)
766 c->scntl3[target] = wide ? (1 << 3) : 0;
767 setasync(dsa, c, target);
768 c->s[target] = WideDone;
772 buildsdtrmsg(uchar *buf, uchar tpf, uchar offset)
783 buildwdtrmsg(uchar *buf, uchar expo)
793 start(Controller *c, long entry)
798 panic(PRINTPREFIX "start called while running");
800 p = c->scriptpa + entry;
801 lesetl(c->n->dsp, p);
804 c->n->dcntl |= 0x4; /* start DMA in SSI mode */
808 ncrcontinue(Controller *c)
811 panic(PRINTPREFIX "ncrcontinue called while running");
812 /* set the start DMA bit to continue execution */
819 softreset(Controller *c)
823 n->istat = Srst; /* software reset */
825 /* general initialisation */
826 n->scid = (1 << 6) | 7; /* respond to reselect, ID 7 */
827 n->respid = 1 << 7; /* response ID = 7 */
830 n->stest1 = 0x80; /* disable external scsi clock */
835 n->stime0 = 0xdd; /* about 0.5 second timeout on each device */
836 n->scntl0 |= 0x8; /* Enable parity checking */
838 /* continued setup */
842 n->stest3 = 0x80; /* TolerANT enable */
845 if (c->v->feature & BigFifo)
846 n->ctest5 = (1 << 5);
847 n->dmode = c->v->burst << 6; /* set burst length bits */
849 n->ctest5 |= (1 << 2); /* including overflow into ctest5 bit 2 */
850 if (c->v->feature & Prefetch)
851 n->dcntl |= (1 << 5); /* prefetch enable */
852 else if (c->v->feature & BurstOpCodeFetch)
853 n->dmode |= (1 << 1); /* burst opcode fetch */
854 if (c->v->feature & Differential) {
856 if ((c->feature & Differential) || bios_set_differential(c)) {
857 /* user enabled, or some evidence bios set differential */
858 if (n->sstat2 & (1 << 2))
859 print(PRINTPREFIX "can't go differential; wrong cable\n");
861 n->stest2 = (1 << 5);
862 print(PRINTPREFIX "differential mode set\n");
867 n->stest1 |= (1 << 3); /* power up doubler */
869 n->stest3 |= (1 << 5); /* stop clock */
870 n->stest1 |= (1 << 2); /* enable doubler */
871 n->stest3 &= ~(1 << 5); /* start clock */
877 msgsm(Dsa *dsa, Controller *c, int msg, int *cont, int *wakeme)
879 uchar histpf, hisreqack;
886 switch (c->s[dsa->target]) {
890 /* reply to my SDTR */
891 histpf = n->scratcha[2];
892 hisreqack = n->scratcha[3];
893 KPRINT(PRINTPREFIX "%d: SDTN response %d %d\n",
894 dsa->target, histpf, hisreqack);
897 setasync(dsa, c, dsa->target);
899 /* hisreqack should be <= c->v->maxsyncoff */
900 tpf = chooserate(c, histpf, &scf, &xferp);
901 KPRINT(PRINTPREFIX "%d: SDTN: using %d %d\n",
902 dsa->target, tpf, hisreqack);
903 setsync(dsa, c, dsa->target, tpf < 25, scf, xferp, hisreqack);
907 case A_SIR_EV_PHASE_SWITCH_AFTER_ID:
908 /* target ignored ATN for message after IDENTIFY - not SCSI-II */
909 KPRINT(PRINTPREFIX "%d: illegal phase switch after ID message - SCSI-1 device?\n", dsa->target);
910 KPRINT(PRINTPREFIX "%d: SDTN: async\n", dsa->target);
911 setasync(dsa, c, dsa->target);
912 *cont = E_to_decisions;
914 case A_SIR_MSG_REJECT:
915 /* rejection of my SDTR */
916 KPRINT(PRINTPREFIX "%d: SDTN: rejected SDTR\n", dsa->target);
918 KPRINT(PRINTPREFIX "%d: SDTN: async\n", dsa->target);
919 setasync(dsa, c, dsa->target);
927 /* reply to my WDTR */
928 KPRINT(PRINTPREFIX "%d: WDTN: response %d\n",
929 dsa->target, n->scratcha[2]);
930 setwide(dsa, c, dsa->target, n->scratcha[2]);
933 case A_SIR_EV_PHASE_SWITCH_AFTER_ID:
934 /* target ignored ATN for message after IDENTIFY - not SCSI-II */
935 KPRINT(PRINTPREFIX "%d: illegal phase switch after ID message - SCSI-1 device?\n", dsa->target);
936 setwide(dsa, c, dsa->target, 0);
937 *cont = E_to_decisions;
939 case A_SIR_MSG_REJECT:
940 /* rejection of my SDTR */
941 KPRINT(PRINTPREFIX "%d: WDTN: rejected WDTR\n", dsa->target);
942 setwide(dsa, c, dsa->target, 0);
952 case A_SIR_MSG_WDTR: {
953 uchar hiswide, mywide;
954 hiswide = n->scratcha[2];
955 mywide = (c->v->feature & Wide) != 0;
956 KPRINT(PRINTPREFIX "%d: WDTN: target init %d\n",
957 dsa->target, hiswide);
958 if (hiswide < mywide)
960 KPRINT(PRINTPREFIX "%d: WDTN: responding %d\n",
961 dsa->target, mywide);
962 setwide(dsa, c, dsa->target, mywide);
963 len = buildwdtrmsg(dsa->msg_out, mywide);
964 setmovedata(&dsa->msg_out_buf, DMASEG(dsa->msg_out), len);
966 c->s[dsa->target] = WideResponse;
974 /* target decides to renegotiate */
975 histpf = n->scratcha[2];
976 hisreqack = n->scratcha[3];
977 KPRINT(PRINTPREFIX "%d: SDTN: target init %d %d\n",
978 dsa->target, histpf, hisreqack);
979 if (hisreqack == 0) {
980 /* he wants asynchronous */
981 setasync(dsa, c, dsa->target);
985 /* he wants synchronous */
986 tpf = chooserate(c, histpf, &scf, &xferp);
987 if (hisreqack > c->v->maxsyncoff)
988 hisreqack = c->v->maxsyncoff;
989 KPRINT(PRINTPREFIX "%d: using %d %d\n",
990 dsa->target, tpf, hisreqack);
991 setsync(dsa, c, dsa->target, tpf < 25, scf, xferp, hisreqack);
993 /* build my SDTR message */
994 len = buildsdtrmsg(dsa->msg_out, tpf, hisreqack);
995 setmovedata(&dsa->msg_out_buf, DMASEG(dsa->msg_out), len);
997 c->s[dsa->target] = SyncResponse;
1004 case A_SIR_EV_RESPONSE_OK:
1005 c->s[dsa->target] = WideDone;
1006 KPRINT(PRINTPREFIX "%d: WDTN: response accepted\n", dsa->target);
1009 case A_SIR_MSG_REJECT:
1010 setwide(dsa, c, dsa->target, 0);
1011 KPRINT(PRINTPREFIX "%d: WDTN: response REJECTed\n", dsa->target);
1018 case A_SIR_EV_RESPONSE_OK:
1019 c->s[dsa->target] = BothDone;
1020 KPRINT(PRINTPREFIX "%d: SDTN: response accepted (%s)\n",
1021 dsa->target, phase[n->sstat1 & 7]);
1024 case A_SIR_MSG_REJECT:
1025 setasync(dsa, c, dsa->target);
1026 KPRINT(PRINTPREFIX "%d: SDTN: response REJECTed\n", dsa->target);
1032 KPRINT(PRINTPREFIX "%d: msgsm: state %d msg %d\n",
1033 dsa->target, c->s[dsa->target], msg);
1039 calcblockdma(Dsa *d, ulong base, ulong count)
1045 blocks = count / A_BSIZE;
1049 d->dmablks = blocks;
1050 d->dmaaddr[0] = base;
1051 d->dmaaddr[1] = base >> 8;
1052 d->dmaaddr[2] = base >> 16;
1053 d->dmaaddr[3] = base >> 24;
1054 setmovedata(&d->data_buf, base + blocks * A_BSIZE, count - blocks * A_BSIZE);
1055 d->flag = legetl(d->data_buf.dbc) == 0;
1059 read_mismatch_recover(Controller *c, Ncr *n, Dsa *dsa)
1062 uchar dfifo = n->dfifo;
1065 dbc = (n->dbc[2]<<16)|(n->dbc[1]<<8)|n->dbc[0];
1066 if (n->ctest5 & (1 << 5))
1067 inchip = ((dfifo | ((n->ctest5 & 3) << 8)) - (dbc & 0x3ff)) & 0x3ff;
1069 inchip = ((dfifo & 0x7f) - (dbc & 0x7f)) & 0x7f;
1071 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: DMA FIFO = %d\n",
1072 dsa->target, dsa->lun, inchip);
1074 if (n->sxfer & SYNCOFFMASK(c)) {
1076 uchar fifo = n->sstat1 >> 4;
1077 if (c->v->maxsyncoff > 8)
1078 fifo |= (n->sstat2 & (1 << 4));
1081 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: SCSI FIFO = %d\n",
1082 dsa->target, dsa->lun, fifo);
1086 if (n->sstat0 & (1 << 7)) {
1088 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: SIDL full\n",
1089 dsa->target, dsa->lun);
1091 if (n->sstat2 & (1 << 7)) {
1093 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: SIDL msb full\n",
1094 dsa->target, dsa->lun);
1102 write_mismatch_recover(Controller *c, Ncr *n, Dsa *dsa)
1105 uchar dfifo = n->dfifo;
1108 dbc = (n->dbc[2]<<16)|(n->dbc[1]<<8)|n->dbc[0];
1110 if (n->ctest5 & (1 << 5))
1111 inchip = ((dfifo | ((n->ctest5 & 3) << 8)) - (dbc & 0x3ff)) & 0x3ff;
1113 inchip = ((dfifo & 0x7f) - (dbc & 0x7f)) & 0x7f;
1116 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: DMA FIFO = %d\n",
1117 dsa->target, dsa->lun, inchip);
1120 if (n->sstat0 & (1 << 5)) {
1123 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODL full\n", dsa->target, dsa->lun);
1126 if (n->sstat2 & (1 << 5)) {
1129 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODL msb full\n", dsa->target, dsa->lun);
1132 if (n->sxfer & SYNCOFFMASK(c)) {
1133 /* synchronous SODR */
1134 if (n->sstat0 & (1 << 6)) {
1137 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODR full\n",
1138 dsa->target, dsa->lun);
1141 if (n->sstat2 & (1 << 6)) {
1144 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODR msb full\n",
1145 dsa->target, dsa->lun);
1149 /* clear the dma fifo */
1150 n->ctest3 |= (1 << 2);
1151 /* wait till done */
1152 while ((n->dstat & Dfe) == 0)
1154 return dbc + inchip;
1158 sd53c8xxinterrupt(Ureg *ur, void *a)
1171 IPRINT(PRINTPREFIX "int\n");
1177 int wokesomething = 0;
1179 IPRINT(PRINTPREFIX "Intfly\n");
1182 /* search for structures in A_STATE_DONE */
1183 for (d = KPTR(legetl(c->dsalist.head)); d != nil && d != dsaend; d = KPTR(legetl(d->next))) {
1184 if (d->stateb == A_STATE_DONE) {
1185 d->p9status = d->status;
1187 IPRINT(PRINTPREFIX "waking up dsa %#p\n", d);
1193 if (!wokesomething) {
1194 IPRINT(PRINTPREFIX "nothing to wake up\n");
1198 if ((istat & (Sip | Dip)) == 0) {
1200 IPRINT(PRINTPREFIX "int end %x\n", istat);
1206 sist = (n->sist1<<8)|n->sist0; /* BUG? can two-byte read be inconsistent? */
1208 dsapa = legetl(n->dsa);
1211 * Can't compute dsa until we know that dsapa is valid.
1213 if(DMASEG_TO_PADDR(dsapa) < -KZERO)
1214 dsa = (Dsa*)DMASEG_TO_KADDR(dsapa);
1218 * happens at startup on some cards but we
1219 * don't actually deref dsa because none of the
1220 * flags we are about are set.
1221 * still, print in case that changes and we're
1222 * about to dereference nil.
1224 iprint("sd53c8xxinterrupt: dsa=%.8lux istat=%ux sist=%ux dstat=%ux\n", dsapa, istat, sist, dstat);
1230 IPRINT("sist = %.4x\n", sist);
1240 addr = legetl(n->dsp);
1241 sa = addr - c->scriptpa;
1242 if (DEBUG(1) || DEBUG(2)) {
1243 IPRINT(PRINTPREFIX "%d/%d: Phase Mismatch sa=%.8lux\n",
1244 dsa->target, dsa->lun, sa);
1249 if (sa == E_data_in_mismatch) {
1251 * though this is a failure in the residue, there may have been blocks
1252 * as well. if so, dmablks will not have been zeroed, since the state
1253 * was not saved by the microcode.
1255 dbc = read_mismatch_recover(c, n, dsa);
1256 tbc = legetl(dsa->data_buf.dbc) - dbc;
1259 advancedata(&dsa->data_buf, tbc);
1260 if (DEBUG(1) || DEBUG(2)) {
1261 IPRINT(PRINTPREFIX "%d/%d: transferred = %ld residue = %ld\n",
1262 dsa->target, dsa->lun, tbc, legetl(dsa->data_buf.dbc));
1264 cont = E_data_mismatch_recover;
1266 else if (sa == E_data_in_block_mismatch) {
1267 dbc = read_mismatch_recover(c, n, dsa);
1268 tbc = A_BSIZE - dbc;
1269 /* recover current state from registers */
1270 dmablks = n->scratcha[2];
1271 dmaaddr = legetl(n->scratchb);
1272 /* we have got to dmaaddr + tbc */
1273 /* we have dmablks * A_BSIZE - tbc + residue left to do */
1274 /* so remaining transfer is */
1275 IPRINT("in_block_mismatch: dmaaddr = 0x%lux tbc=%lud dmablks=%d\n",
1276 dmaaddr, tbc, dmablks);
1277 calcblockdma(dsa, dmaaddr + tbc,
1278 dmablks * A_BSIZE - tbc + legetl(dsa->data_buf.dbc));
1279 /* copy changes into scratch registers */
1280 IPRINT("recalc: dmablks %d dmaaddr 0x%lx pa 0x%lx dbc %ld\n",
1281 dsa->dmablks, legetl(dsa->dmaaddr),
1282 legetl(dsa->data_buf.pa), legetl(dsa->data_buf.dbc));
1283 n->scratcha[2] = dsa->dmablks;
1284 lesetl(n->scratchb, *((ulong*)dsa->dmaaddr));
1285 cont = E_data_block_mismatch_recover;
1287 else if (sa == E_data_out_mismatch) {
1288 dbc = write_mismatch_recover(c, n, dsa);
1289 tbc = legetl(dsa->data_buf.dbc) - dbc;
1292 advancedata(&dsa->data_buf, tbc);
1293 if (DEBUG(1) || DEBUG(2)) {
1294 IPRINT(PRINTPREFIX "%d/%d: transferred = %ld residue = %ld\n",
1295 dsa->target, dsa->lun, tbc, legetl(dsa->data_buf.dbc));
1297 cont = E_data_mismatch_recover;
1299 else if (sa == E_data_out_block_mismatch) {
1300 dbc = write_mismatch_recover(c, n, dsa);
1301 tbc = legetl(dsa->data_buf.dbc) - dbc;
1302 /* recover current state from registers */
1303 dmablks = n->scratcha[2];
1304 dmaaddr = legetl(n->scratchb);
1305 /* we have got to dmaaddr + tbc */
1306 /* we have dmablks blocks - tbc + residue left to do */
1307 /* so remaining transfer is */
1308 IPRINT("out_block_mismatch: dmaaddr = %lux tbc=%lud dmablks=%d\n",
1309 dmaaddr, tbc, dmablks);
1310 calcblockdma(dsa, dmaaddr + tbc,
1311 dmablks * A_BSIZE - tbc + legetl(dsa->data_buf.dbc));
1312 /* copy changes into scratch registers */
1313 n->scratcha[2] = dsa->dmablks;
1314 lesetl(n->scratchb, *((ulong*)dsa->dmaaddr));
1315 cont = E_data_block_mismatch_recover;
1317 else if (sa == E_id_out_mismatch) {
1319 * target switched phases while attention held during
1320 * message out. The possibilities are:
1321 * 1. It didn't like the last message. This is indicated
1322 * by the new phase being message_in. Use script to recover
1324 * 2. It's not SCSI-II compliant. The new phase will be other
1325 * than message_in. We should also indicate that the device
1326 * is asynchronous, if it's the SDTR that got ignored
1328 * For now, if the phase switch is not to message_in, and
1329 * and it happens after IDENTIFY and before SDTR, we
1330 * notify the negotiation state machine.
1332 ulong lim = legetl(dsa->msg_out_buf.dbc);
1333 uchar p = n->sstat1 & 7;
1334 dbc = write_mismatch_recover(c, n, dsa);
1336 IPRINT(PRINTPREFIX "%d/%d: msg_out_mismatch: %lud/%lud sent, phase %s\n",
1337 dsa->target, dsa->lun, tbc, lim, phase[p]);
1338 if (p != MessageIn && tbc == 1) {
1339 msgsm(dsa, c, A_SIR_EV_PHASE_SWITCH_AFTER_ID, &cont, &wakeme);
1342 cont = E_id_out_mismatch_recover;
1344 else if (sa == E_cmd_out_mismatch) {
1346 * probably the command count is longer than the device wants ...
1348 ulong lim = legetl(dsa->cmd_buf.dbc);
1349 uchar p = n->sstat1 & 7;
1350 dbc = write_mismatch_recover(c, n, dsa);
1352 IPRINT(PRINTPREFIX "%d/%d: cmd_out_mismatch: %lud/%lud sent, phase %s\n",
1353 dsa->target, dsa->lun, tbc, lim, phase[p]);
1355 cont = E_to_decisions;
1358 IPRINT(PRINTPREFIX "%d/%d: ma sa=%.8lux wanted=%s got=%s\n",
1359 dsa->target, dsa->lun, sa,
1361 phase[n->sstat1 & 7]);
1363 dsa->p9status = SDeio; /* chf */
1367 /*else*/ if (sist & 0x400) {
1369 IPRINT(PRINTPREFIX "%d/%d Sto\n", dsa->target, dsa->lun);
1371 dsa->p9status = SDtimeout;
1372 dsa->stateb = A_STATE_DONE;
1375 cont = E_issue_check;
1379 IPRINT(PRINTPREFIX "%d/%d: parity error\n", dsa->target, dsa->lun);
1380 dsa->parityerror = 1;
1383 IPRINT(PRINTPREFIX "%s%d lun %d: unexpected disconnect\n",
1384 c->sdev->name, dsa->target, dsa->lun);
1387 dsa->p9status = SDeio;
1392 IPRINT("dstat = %.2x\n", dstat);
1394 /*else*/ if (dstat & Ssi) {
1395 ulong w = legetl(n->dsp) - c->scriptpa;
1396 IPRINT("[%lux]", w);
1398 cont = -2; /* restart */
1401 switch (legetl(n->dsps)) {
1402 case A_SIR_MSG_IO_COMPLETE:
1403 dsa->p9status = dsa->status;
1406 case A_SIR_MSG_SDTR:
1407 case A_SIR_MSG_WDTR:
1408 case A_SIR_MSG_REJECT:
1409 case A_SIR_EV_RESPONSE_OK:
1410 msgsm(dsa, c, legetl(n->dsps), &cont, &wakeme);
1412 case A_SIR_MSG_IGNORE_WIDE_RESIDUE:
1413 /* back up one in the data transfer */
1414 IPRINT(PRINTPREFIX "%d/%d: ignore wide residue %d, WSR = %d\n",
1415 dsa->target, dsa->lun, n->scratcha[1], n->scntl2 & 1);
1416 if (dsa->flag == 2) {
1417 IPRINT(PRINTPREFIX "%d/%d: transfer over; residue ignored\n",
1418 dsa->target, dsa->lun);
1421 calcblockdma(dsa, legetl(dsa->dmaaddr) - 1,
1422 dsa->dmablks * A_BSIZE + legetl(dsa->data_buf.dbc) + 1);
1426 case A_SIR_ERROR_NOT_MSG_IN_AFTER_RESELECT:
1427 IPRINT(PRINTPREFIX "%d: not msg_in after reselect (%s)",
1428 n->ssid & SSIDMASK(c), phase[n->sstat1 & 7]);
1429 dsa = dsafind(c, n->ssid & SSIDMASK(c), -1, A_STATE_DISCONNECTED);
1433 case A_SIR_NOTIFY_LOAD_STATE:
1434 IPRINT(PRINTPREFIX ": load_state dsa=%p\n", dsa);
1438 panic("bad dsa in load_state");
1442 case A_SIR_NOTIFY_MSG_IN:
1443 IPRINT(PRINTPREFIX "%d/%d: msg_in %d\n",
1444 dsa->target, dsa->lun, n->sfbr);
1447 case A_SIR_NOTIFY_DISC:
1448 IPRINT(PRINTPREFIX "%d/%d: disconnect:", dsa->target, dsa->lun);
1450 case A_SIR_NOTIFY_STATUS:
1451 IPRINT(PRINTPREFIX "%d/%d: status\n", dsa->target, dsa->lun);
1454 case A_SIR_NOTIFY_COMMAND:
1455 IPRINT(PRINTPREFIX "%d/%d: commands\n", dsa->target, dsa->lun);
1458 case A_SIR_NOTIFY_DATA_IN:
1459 IPRINT(PRINTPREFIX "%d/%d: data in a %lx b %lx\n",
1460 dsa->target, dsa->lun, legetl(n->scratcha), legetl(n->scratchb));
1463 case A_SIR_NOTIFY_BLOCK_DATA_IN:
1464 IPRINT(PRINTPREFIX "%d/%d: block data in: a2 %x b %lx\n",
1465 dsa->target, dsa->lun, n->scratcha[2], legetl(n->scratchb));
1468 case A_SIR_NOTIFY_DATA_OUT:
1469 IPRINT(PRINTPREFIX "%d/%d: data out\n", dsa->target, dsa->lun);
1472 case A_SIR_NOTIFY_DUMP:
1473 IPRINT(PRINTPREFIX "%d/%d: dump\n", dsa->target, dsa->lun);
1477 case A_SIR_NOTIFY_DUMP2:
1478 IPRINT(PRINTPREFIX "%d/%d: dump2:", dsa->target, dsa->lun);
1479 IPRINT(" sa %lux", legetl(n->dsp) - c->scriptpa);
1480 IPRINT(" dsa %lux", legetl(n->dsa));
1481 IPRINT(" sfbr %ux", n->sfbr);
1482 IPRINT(" a %lux", legetl(n->scratcha));
1483 IPRINT(" b %lux", legetl(n->scratchb));
1484 IPRINT(" ssid %ux", n->ssid);
1488 case A_SIR_NOTIFY_WAIT_RESELECT:
1489 IPRINT(PRINTPREFIX "wait reselect\n");
1492 case A_SIR_NOTIFY_RESELECT:
1493 IPRINT(PRINTPREFIX "reselect: ssid %.2x sfbr %.2x at %ld\n",
1494 n->ssid, n->sfbr, TK2MS(m->ticks));
1497 case A_SIR_NOTIFY_ISSUE:
1498 IPRINT(PRINTPREFIX "%d/%d: issue dsa=%p end=%p:", dsa->target, dsa->lun, dsa, dsaend);
1500 IPRINT(" tgt=%d", dsa->target);
1501 IPRINT(" time=%ld", TK2MS(m->ticks));
1505 case A_SIR_NOTIFY_ISSUE_CHECK:
1506 IPRINT(PRINTPREFIX "issue check\n");
1509 case A_SIR_NOTIFY_SIGP:
1510 IPRINT(PRINTPREFIX "responded to SIGP\n");
1513 case A_SIR_NOTIFY_DUMP_NEXT_CODE: {
1514 ulong *dsp = &c->script[(legetl(n->dsp)-c->scriptpa)/4];
1516 IPRINT(PRINTPREFIX "code at %lux", (ulong)(dsp - c->script));
1517 for (x = 0; x < 6; x++) {
1518 IPRINT(" %.8lux", dsp[x]);
1525 case A_SIR_NOTIFY_WSR:
1526 IPRINT(PRINTPREFIX "%d/%d: WSR set\n", dsa->target, dsa->lun);
1529 case A_SIR_NOTIFY_LOAD_SYNC:
1530 IPRINT(PRINTPREFIX "%d/%d: scntl=%.2x sxfer=%.2x\n",
1531 dsa->target, dsa->lun, n->scntl3, n->sxfer);
1534 case A_SIR_NOTIFY_RESELECTED_ON_SELECT:
1536 IPRINT(PRINTPREFIX "%d/%d: reselected during select\n",
1537 dsa->target, dsa->lun);
1541 case A_error_reselected: /* dsa isn't valid here */
1542 iprint(PRINTPREFIX "reselection error\n");
1544 for (dsa = KPTR(legetl(c->dsalist.head)); dsa != nil && dsa != dsaend; dsa = KPTR(legetl(dsa->next))) {
1545 IPRINT(PRINTPREFIX "dsa target %d lun %d state %d\n", dsa->target, dsa->lun, dsa->stateb);
1549 IPRINT(PRINTPREFIX "%d/%d: script error %ld\n",
1550 dsa->target, dsa->lun, legetl(n->dsps));
1555 /*else*/ if (dstat & Iid) {
1557 ulong addr, dbc, *v;
1559 addr = legetl(n->dsp);
1561 target = dsa->target;
1567 dbc = (n->dbc[2]<<16)|(n->dbc[1]<<8)|n->dbc[0];
1571 IPRINT(PRINTPREFIX "%d/%d: Iid pa=%.8lux sa=%.8lux dbc=%lux\n",
1573 addr, addr - c->scriptpa, dbc);
1574 addr -= c->scriptpa;
1577 v = &c->script[addr/4];
1579 IPRINT("%.8lux: %.8lux %.8lux %.8lux %.8lux\n",
1580 addr, v[0], v[1], v[2], v[3]);
1590 dsa->p9status = SDeio;
1593 /*else*/ if (dstat & Bf) {
1594 IPRINT(PRINTPREFIX "%d/%d: Bus Fault\n", dsa->target, dsa->lun);
1596 dsa->p9status = SDeio;
1605 if(dsa->p9status == SDnostatus)
1606 dsa->p9status = SDeio;
1611 IPRINT(PRINTPREFIX "int end 1\n");
1618 return ((Dsa *)arg)->p9status != SDnostatus;
1622 setmovedata(Movedata *d, ulong pa, ulong bc)
1635 advancedata(Movedata *d, long v)
1637 lesetl(d->pa, legetl(d->pa) + v);
1638 lesetl(d->dbc, legetl(d->dbc) - v);
1642 dumpwritedata(uchar *data, int datalen)
1647 USED(data, datalen);
1652 KPRINT(PRINTPREFIX "write:");
1653 for (i = 0, bp = data; i < 50 && i < datalen; i++, bp++) {
1654 KPRINT("%.2ux", *bp);
1664 dumpreaddata(uchar *data, int datalen)
1669 USED(data, datalen);
1674 KPRINT(PRINTPREFIX "read:");
1675 for (i = 0, bp = data; i < 50 && i < datalen; i++, bp++) {
1676 KPRINT("%.2ux", *bp);
1686 busreset(Controller *c)
1691 c->n->scntl1 |= (1 << 3);
1693 c->n->scntl1 &= ~(1 << 3);
1694 if(!(c->v->feature & Wide))
1697 ntarget = MAXTARGET;
1698 for (x = 0; x < ntarget; x++) {
1699 setwide(0, c, x, 0);
1701 c->s[x] = NeitherDone;
1708 reset(Controller *c)
1710 /* should wakeup all pending tasks */
1716 sd53c8xxrio(SDreq* r)
1721 uchar target_expo, my_expo;
1722 int bc, check, i, status, target;
1724 if((target = r->unit->subno) == 0x07)
1725 return r->status = SDtimeout; /* assign */
1727 c = r->unit->dev->ctlr;
1730 d = dsaalloc(c, target, r->lun);
1732 qlock(&c->q[target]); /* obtain access to target */
1734 /* load the transfer control stuff */
1735 d->scsi_id_buf[0] = 0;
1736 d->scsi_id_buf[1] = c->sxfer[target];
1737 d->scsi_id_buf[2] = target;
1738 d->scsi_id_buf[3] = c->scntl3[target];
1743 d->msg_out[bc] = 0x80 | r->lun;
1745 #ifndef NO_DISCONNECT
1746 d->msg_out[bc] |= (1 << 6);
1750 /* work out what to do about negotiation */
1751 switch (c->s[target]) {
1753 KPRINT(PRINTPREFIX "%d: strange nego state %d\n", target, c->s[target]);
1754 c->s[target] = NeitherDone;
1757 if ((c->capvalid & (1 << target)) == 0)
1759 target_expo = (c->cap[target] >> 5) & 3;
1760 my_expo = (c->v->feature & Wide) != 0;
1761 if (target_expo < my_expo)
1762 my_expo = target_expo;
1763 #ifdef ALWAYS_DO_WDTR
1764 bc += buildwdtrmsg(d->msg_out + bc, my_expo);
1765 KPRINT(PRINTPREFIX "%d: WDTN: initiating expo %d\n", target, my_expo);
1766 c->s[target] = WideInit;
1770 bc += buildwdtrmsg(d->msg_out + bc, (c->v->feature & Wide) ? 1 : 0);
1771 KPRINT(PRINTPREFIX "%d: WDTN: initiating expo %d\n", target, my_expo);
1772 c->s[target] = WideInit;
1775 KPRINT(PRINTPREFIX "%d: WDTN: narrow\n", target);
1779 if (c->cap[target] & (1 << 4)) {
1780 KPRINT(PRINTPREFIX "%d: SDTN: initiating %d %d\n", target, c->tpf, c->v->maxsyncoff);
1781 bc += buildsdtrmsg(d->msg_out + bc, c->tpf, c->v->maxsyncoff);
1782 c->s[target] = SyncInit;
1785 KPRINT(PRINTPREFIX "%d: SDTN: async only\n", target);
1786 c->s[target] = BothDone;
1793 setmovedata(&d->msg_out_buf, DMASEG(d->msg_out), bc);
1794 setmovedata(&d->cmd_buf, DMASEG(r->cmd), r->clen);
1795 calcblockdma(d, r->data ? DMASEG(r->data) : 0, r->dlen);
1798 KPRINT(PRINTPREFIX "%d/%d: exec: ", target, r->lun);
1799 for (bp = r->cmd; bp < &r->cmd[r->clen]; bp++) {
1800 KPRINT("%.2ux", *bp);
1804 KPRINT(PRINTPREFIX "%d/%d: exec: limit=(%d)%ld\n",
1805 target, r->lun, d->dmablks, legetl(d->data_buf.dbc));
1808 dumpwritedata(r->data, r->dlen);
1811 setmovedata(&d->status_buf, DMASEG(&d->status), 1);
1813 d->p9status = SDnostatus;
1816 d->stateb = A_STATE_ISSUE; /* start operation */
1821 c->n->dcntl |= 0x10; /* single step */
1826 start(c, E_issue_check);
1832 tsleep(d, done, d, 600 * 1000);
1836 KPRINT(PRINTPREFIX "%d/%d: exec: Timed out\n", target, r->lun);
1840 qunlock(&c->q[target]);
1841 r->status = SDtimeout;
1842 return r->status = SDtimeout; /* assign */
1845 if((status = d->p9status) == SDeio)
1846 c->s[target] = NeitherDone;
1847 if (d->parityerror) {
1856 KPRINT(PRINTPREFIX "%d/%d: exec: before rlen adjust: dmablks %d flag %d dbc %lud\n",
1857 target, r->lun, d->dmablks, d->flag, legetl(d->data_buf.dbc));
1861 r->rlen -= d->dmablks * A_BSIZE;
1862 r->rlen -= legetl(d->data_buf.dbc);
1865 dumpreaddata(r->data, r->rlen);
1867 KPRINT(PRINTPREFIX "%d/%d: exec: p9status=%d status %d rlen %ld\n",
1868 target, r->lun, d->p9status, status, r->rlen);
1873 if ((c->capvalid & (1 << target)) == 0
1874 && (status == SDok || status == SDcheck)
1875 && r->cmd[0] == 0x12 && r->dlen >= 8) {
1876 c->capvalid |= 1 << target;
1878 c->cap[target] = bp[7];
1879 KPRINT(PRINTPREFIX "%d: capabilities %.2x\n", target, bp[7]);
1881 if(!check && status == SDcheck && !(r->flags & SDnosense)){
1884 memset(r->cmd, 0, sizeof(r->cmd));
1886 r->cmd[1] = r->lun<<5;
1887 r->cmd[4] = sizeof(r->sense)-1;
1890 r->dlen = sizeof(r->sense)-1;
1892 * Clear out the microcode state
1893 * so the Dsa can be re-used.
1895 lesetl(&d->stateb, A_STATE_ALLOCATED);
1899 qunlock(&c->q[target]);
1902 if(status == SDok && check){
1904 r->flags |= SDvalidsense;
1907 KPRINT(PRINTPREFIX "%d: r flags %8.8uX status %d rlen %ld\n",
1908 target, r->flags, status, r->rlen);
1909 if(r->flags & SDvalidsense){
1911 KPRINT(PRINTPREFIX "%d: r flags %8.8uX status %d rlen %ld\n",
1912 target, r->flags, status, r->rlen);
1913 for(i = 0; i < r->rlen; i++)
1914 KPRINT(" %2.2uX", r->sense[i]);
1917 return r->status = status;
1921 cribbios(Controller *c)
1923 c->bios.scntl3 = c->n->scntl3;
1924 c->bios.stest2 = c->n->stest2;
1925 print(PRINTPREFIX "%s: bios scntl3(%.2x) stest2(%.2x)\n",
1926 c->sdev->name, c->bios.scntl3, c->bios.stest2);
1930 bios_set_differential(Controller *c)
1932 /* Concept lifted from FreeBSD - thanks Gerard */
1933 /* basically, if clock conversion factors are set, then there is
1934 * evidence the bios had a go at the chip, and if so, it would
1935 * have set the differential enable bit in stest2
1937 return (c->bios.scntl3 & 7) != 0 && (c->bios.stest2 & 0x20) != 0;
1940 #define NCR_VID 0x1000
1941 #define NCR_810_DID 0x0001
1942 #define NCR_820_DID 0x0002 /* don't know enough about this one to support it */
1943 #define NCR_825_DID 0x0003
1944 #define NCR_815_DID 0x0004
1945 #define SYM_810AP_DID 0x0005
1946 #define SYM_860_DID 0x0006
1947 #define SYM_896_DID 0x000b
1948 #define SYM_895_DID 0x000c
1949 #define SYM_885_DID 0x000d /* ditto */
1950 #define SYM_875_DID 0x000f /* ditto */
1951 #define SYM_1010_DID 0x0020
1952 #define SYM_1011_DID 0x0021
1953 #define SYM_875J_DID 0x008f
1955 static Variant variant[] = {
1956 { NCR_810_DID, 0x0f, "NCR53C810", Burst16, 8, 24, 0 },
1957 { NCR_810_DID, 0x1f, "SYM53C810ALV", Burst16, 8, 24, Prefetch },
1958 { NCR_810_DID, 0xff, "SYM53C810A", Burst16, 8, 24, Prefetch },
1959 { SYM_810AP_DID, 0xff, "SYM53C810AP", Burst16, 8, 24, Prefetch },
1960 { NCR_815_DID, 0xff, "NCR53C815", Burst16, 8, 24, BurstOpCodeFetch },
1961 { NCR_825_DID, 0x0f, "NCR53C825", Burst16, 8, 24, Wide|BurstOpCodeFetch|Differential },
1962 { NCR_825_DID, 0xff, "SYM53C825A", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide },
1963 { SYM_860_DID, 0x0f, "SYM53C860", Burst16, 8, 24, Prefetch|Ultra },
1964 { SYM_860_DID, 0xff, "SYM53C860LV", Burst16, 8, 24, Prefetch|Ultra },
1965 { SYM_875_DID, 0x01, "SYM53C875r1", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide|Ultra },
1966 { SYM_875_DID, 0xff, "SYM53C875", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide|Ultra|ClockDouble },
1967 { SYM_875J_DID, 0xff, "SYM53C875j", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide|Ultra|ClockDouble },
1968 { SYM_885_DID, 0xff, "SYM53C885", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Wide|Ultra|ClockDouble },
1969 { SYM_895_DID, 0xff, "SYM53C895", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1970 { SYM_896_DID, 0xff, "SYM53C896", Burst128, 16, 64, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1971 { SYM_1010_DID, 0xff, "SYM53C1010", Burst128, 16, 64, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1972 { SYM_1011_DID, 0xff, "SYM53C1010", Burst128, 16, 64, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1976 xfunc(Controller *c, enum na_external x, unsigned long *v)
1980 print("xfunc: can't find external %d\n", x);
1983 *v = offsetof(Dsa, scsi_id_buf[0]);
1986 *v = offsetof(Dsa, msg_out_buf);
1989 *v = offsetof(Dsa, cmd_buf);
1992 *v = offsetof(Dsa, data_buf);
1995 *v = offsetof(Dsa, status_buf);
1998 *v = DMASEG(&c->dsalist.head[0]);
2008 na_fixup(Controller *c, ulong pa_reg,
2009 struct na_patch *patch, int patches,
2010 int (*externval)(Controller*, int, ulong*))
2014 ulong *script, pa_script;
2015 unsigned long lw, lv;
2018 pa_script = c->scriptpa;
2019 for (p = 0; p < patches; p++) {
2020 switch (patch[p].type) {
2022 /* script relative */
2023 script[patch[p].lwoff] += pa_script;
2026 /* register i/o relative */
2027 script[patch[p].lwoff] += pa_reg;
2031 lw = script[patch[p].lwoff];
2032 v = (lw >> 8) & 0xff;
2033 if (!(*externval)(c, v, &lv))
2036 script[patch[p].lwoff] = (lw & 0xffff00ffL) | (v << 8);
2039 /* 32 bit external */
2040 lw = script[patch[p].lwoff];
2041 if (!(*externval)(c, lw, &lv))
2043 script[patch[p].lwoff] = lv;
2046 /* 24 bit external */
2047 lw = script[patch[p].lwoff];
2048 if (!(*externval)(c, lw & 0xffffff, &lv))
2050 script[patch[p].lwoff] = (lw & 0xff000000L) | (lv & 0xffffffL);
2066 SDev *sdev, *head, *tail;
2067 uvlong regpa, scriptpa;
2069 void *regva, *scriptva;
2071 if(cp = getconf("*maxsd53c8xx"))
2072 nctlr = strtoul(cp, 0, 0);
2078 while((p = pcimatch(p, NCR_VID, 0)) != nil && nctlr > 0){
2079 for(v = variant; v < &variant[nelem(variant)]; v++){
2080 if(p->did == v->did && p->rid <= v->maxrid)
2083 if(v >= &variant[nelem(variant)]) {
2084 print("no match\n");
2087 print(PRINTPREFIX "%s rev. 0x%2.2x intr=%d command=%4.4uX\n",
2088 v->name, p->rid, p->intl, p->pcr);
2090 regpa = p->mem[1].bar;
2100 regva = vmap(regpa, p->mem[1].size);
2102 print("sd53c8xx: can't map %llux\n", regpa);
2110 if((v->feature & LocalRAM) && sizeof(na_script) <= 4096){
2111 scriptpa = p->mem[ba].bar;
2112 if((scriptpa & 0x04) && p->mem[ba+1].bar){
2113 vunmap(regva, p->mem[1].size);
2117 scriptva = vmap(scriptpa, p->mem[ba].size);
2123 * Either the map failed, or this chip does not have
2124 * local RAM. It will need a copy of the microcode.
2126 scriptma = malloc(sizeof(na_script));
2127 if(scriptma == nil){
2128 vunmap(regva, p->mem[1].size);
2131 scriptpa = DMASEG(scriptma);
2135 ctlr = malloc(sizeof(Controller));
2136 sdev = malloc(sizeof(SDev));
2137 if(ctlr == nil || sdev == nil){
2146 vunmap(scriptva, p->mem[ba].size);
2148 vunmap(regva, p->mem[1].size);
2152 lock(&ctlr->dsalist);
2153 ctlr->dsalist.freechain = nil;
2155 dsaend = xalloc(sizeof *dsaend);
2157 panic("sd53c8xxpnp: no memory");
2158 lesetl(&dsaend->stateb, A_STATE_END);
2159 lesetl(dsaend->next, DMASEG(dsaend));
2161 lesetl(ctlr->dsalist.head, DMASEG(dsaend));
2163 unlock(&ctlr->dsalist);
2167 ctlr->script = script;
2168 memmove(ctlr->script, na_script, sizeof(na_script));
2171 * Because we don't yet have an abstraction for the
2172 * addresses as seen from the controller side (and on
2173 * the 386 it doesn't matter), the following two lines
2174 * are different between the 386 and alpha copies of
2177 ctlr->scriptpa = scriptpa;
2178 if(!na_fixup(ctlr, regpa, na_patches, NA_PATCHES, xfunc)){
2179 print("script fixup failed\n");
2182 swabl(ctlr->script, ctlr->script, sizeof(na_script));
2186 sdev->ifc = &sd53c8xxifc;
2189 if(!(v->feature & Wide))
2192 sdev->nunit = MAXTARGET;
2208 sd53c8xxenable(SDev* sdev)
2215 pcidev = ctlr->pcidev;
2223 snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
2224 intrenable(pcidev->intl, sd53c8xxinterrupt, ctlr, pcidev->tbdf, name);
2230 SDifc sd53c8xxifc = {
2231 "53c8xx", /* name */
2233 sd53c8xxpnp, /* pnp */
2235 sd53c8xxenable, /* enable */
2238 scsiverify, /* verify */
2239 scsionline, /* online */
2240 sd53c8xxrio, /* rio */