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"
27 #include "../port/sd.h"
28 extern SDifc sd53c8xxifc;
30 /**********************************/
31 /* Portable configuration macros */
32 /**********************************/
36 /**********************************/
37 /* CPU specific macros */
38 /**********************************/
40 #define PRINTPREFIX "sd53c8xx: "
42 static int idebug = 0;
43 #define KPRINT if(0) iprint
44 #define IPRINT if(idebug) iprint
48 /*******************************/
50 /*******************************/
52 #define DMASEG(x) PCIWADDR(x)
53 #define legetl(x) (*(ulong*)(x))
54 #define lesetl(x,v) (*(ulong*)(x) = (v))
56 #define DMASEG_TO_PADDR(x) ((uintptr)(x)-PCIWINDOW)
57 #define DMASEG_TO_KADDR(x) KADDR(DMASEG_TO_PADDR(x))
58 #define KPTR(x) (((x) == 0) ? nil : DMASEG_TO_KADDR(x))
62 #define SCLK (33 * MEGA)
64 #define SCLK (40 * MEGA)
65 #endif /* INTERNAL_SCLK */
66 #define ULTRA_NOCLOCKDOUBLE_SCLK (80 * MEGA)
68 #define MAXSYNCSCSIRATE (5 * MEGA)
69 #define MAXFASTSYNCSCSIRATE (10 * MEGA)
70 #define MAXULTRASYNCSCSIRATE (20 * MEGA)
71 #define MAXULTRA2SYNCSCSIRATE (40 * MEGA)
72 #define MAXASYNCCORERATE (25 * MEGA)
73 #define MAXSYNCCORERATE (25 * MEGA)
74 #define MAXFASTSYNCCORERATE (50 * MEGA)
75 #define MAXULTRASYNCCORERATE (80 * MEGA)
76 #define MAXULTRA2SYNCCORERATE (160 * MEGA)
89 uchar scntl0; /* 00 */
104 uchar dstat; /* 0c */
109 uchar dsa[4]; /* 10 */
111 uchar istat; /* 14 */
114 uchar ctest0; /* 18 */
119 uchar temp[4]; /* 1c */
121 uchar dfifo; /* 20 */
126 uchar dbc[3]; /* 24 */
129 uchar dnad[4]; /* 28 */
130 uchar dsp[4]; /* 2c */
131 uchar dsps[4]; /* 30 */
133 uchar scratcha[4]; /* 34 */
135 uchar dmode; /* 38 */
140 uchar adder[4]; /* 3c */
142 uchar sien0; /* 40 */
147 uchar slpar; /* 44 */
152 uchar stime0; /* 48 */
157 uchar stest0; /* 4c */
171 uchar scratchb[4]; /* 5c */
174 typedef struct Movedata {
179 typedef enum NegoState {
180 NeitherDone, WideInit, WideResponse, WideDone,
181 SyncInit, SyncResponse, BothDone
185 Allocated, Queued, Active, Done
188 typedef struct Dsa Dsa;
193 uchar flag; /* setbyte(state,3,...) */
195 uchar dmaaddr[4]; /* For block transfer: NCR order (little-endian) */
197 uchar target; /* Target */
200 uchar lun; /* Logical Unit Number */
203 uchar scntl3; /* Sync */
207 uchar next[4]; /* chaining for SCRIPT (NCR byte order) */
209 Dsa *freechain; /* chaining for freelist */
212 uchar scsi_id_buf[4];
213 Movedata msg_out_buf;
218 uchar msg_out[10]; /* enough to include SDTR */
224 typedef enum Feature {
225 BigFifo = 1, /* 536 byte fifo */
226 BurstOpCodeFetch = 2, /* burst fetch opcodes */
227 Prefetch = 4, /* prefetch 8 longwords */
228 LocalRAM = 8, /* 4K longwords of local RAM */
229 Differential = 16, /* Differential support */
230 Wide = 32, /* Wide capable */
231 Ultra = 64, /* Ultra capable */
232 ClockDouble = 128, /* Has clock doubler */
233 ClockQuad = 256, /* Has clock quadrupler (same as Ultra2) */
247 typedef struct Variant {
249 uchar maxrid; /* maximum allowed revision ID */
251 Burst burst; /* codings for max burst */
252 uchar maxsyncoff; /* max synchronous offset */
253 uchar registers; /* number of 32 bit registers */
257 static unsigned char cf2[] = { 6, 2, 3, 4, 6, 8, 12, 16 };
258 #define NULTRA2SCF (sizeof(cf2)/sizeof(cf2[0]))
259 #define NULTRASCF (NULTRA2SCF - 2)
260 #define NSCF (NULTRASCF - 1)
262 typedef struct Controller {
268 uchar synctab[NULTRA2SCF - 1][8];/* table of legal tpfs */
269 NegoState s[MAXTARGET];
270 uchar scntl3[MAXTARGET];
271 uchar sxfer[MAXTARGET];
272 uchar cap[MAXTARGET]; /* capabilities byte from Identify */
273 ushort capvalid; /* bit per target for validity of cap[] */
274 ushort wide; /* bit per target set if wide negotiated */
275 ulong sclk; /* clock speed of controller */
276 uchar clockmult; /* set by synctabinit */
277 uchar ccf; /* CCF bits */
278 uchar tpf; /* best tpf value for this controller */
279 uchar feature; /* requested features */
280 int running; /* is the script processor running? */
281 int ssm; /* single step mode */
282 Ncr *n; /* pointer to registers */
283 Variant *v; /* pointer to variant type */
284 ulong *script; /* where the real script is */
285 ulong scriptpa; /* where the real script is */
291 uchar head[4]; /* head of free list (NCR byte order) */
295 QLock q[MAXTARGET]; /* queues for each target */
298 #define SYNCOFFMASK(c) (((c)->v->maxsyncoff * 2) - 1)
299 #define SSIDMASK(c) (((c)->v->feature & Wide) ? 15 : 7)
302 enum { Abrt = 0x80, Srst = 0x40, Sigp = 0x20, Sem = 0x10, Con = 0x08, Intf = 0x04, Sip = 0x02, Dip = 0x01 };
305 enum { Dfe = 0x80, Mdpe = 0x40, Bf = 0x20, Abrted = 0x10, Ssi = 0x08, Sir = 0x04, Iid = 0x01 };
308 enum { DataOut, DataIn, Cmd, Status, ReservedOut, ReservedIn, MessageOut, MessageIn };
310 static void setmovedata(Movedata*, ulong, ulong);
311 static void advancedata(Movedata*, long);
312 static int bios_set_differential(Controller *c);
314 static char *phase[] = {
315 "data out", "data in", "command", "status",
316 "reserved out", "reserved in", "message out", "message in"
320 #define DEBUGSIZE 10240
321 char debugbuf[DEBUGSIZE];
325 intrprint(char *format, ...)
328 debuglast = debugbuf;
329 debuglast = vseprint(debuglast, debugbuf + (DEBUGSIZE - 1), format, (&format + 1));
339 debuglast = debugbuf;
340 if (debuglast == debugbuf) {
346 screenputs(debugbuf, endp - debugbuf);
348 memmove(debugbuf, endp, debuglast - endp);
349 debuglast -= endp - debugbuf;
354 oprint(char *format, ...)
361 debuglast = debugbuf;
362 debuglast = vseprint(debuglast, debugbuf + (DEBUGSIZE - 1), format, (&format + 1));
368 #include "../pc/sd53c8xx.i"
371 * We used to use a linked list of Dsas with nil as the terminator,
372 * but occasionally the 896 card seems not to notice that the 0
373 * is really a 0, and then it tries to reference the Dsa at address 0.
374 * To address this, we use a sentinel dsa that links back to itself
375 * and has state A_STATE_END. If the card takes an iteration or
376 * two to notice that the state says A_STATE_END, that's no big
377 * deal. Clearly this isn't the right approach, but I'm just
378 * stumped. Even with this, we occasionally get prints about
379 * "WSR set", usually with about the same frequency that the
380 * card used to walk past 0.
385 dsaalloc(Controller *c, int target, int lun)
390 if ((d = c->dsalist.freechain) != nil) {
391 c->dsalist.freechain = d->freechain;
394 IPRINT(PRINTPREFIX "%d/%d: reused dsa %#p\n", target, lun, d);
396 /* c->dsalist must be ilocked */
397 d = xalloc(sizeof *d);
399 panic("sd53c8xx dsaallocnew: no memory");
401 lesetl(d->next, legetl(c->dsalist.head));
402 lesetl(&d->stateb, A_STATE_FREE);
404 lesetl(c->dsalist.head, DMASEG(d));
407 IPRINT(PRINTPREFIX "%d/%d: allocated dsa %#p\n", target, lun, d);
409 lesetl(&d->stateb, A_STATE_ALLOCATED);
410 iunlock(&c->dsalist);
417 dsafree(Controller *c, Dsa *d)
420 d->freechain = c->dsalist.freechain;
421 c->dsalist.freechain = d;
422 lesetl(&d->stateb, A_STATE_FREE);
423 iunlock(&c->dsalist);
427 dsadump(Controller *c)
432 iprint("dsa controller list: c=%p head=%.8lux\n", c, legetl(c->dsalist.head));
433 for(d=KPTR(legetl(c->dsalist.head)); d != nil && d != dsaend; d=KPTR(legetl(d->next))){
435 iprint("\tdsa %p %.8ux %.8ux %.8ux %.8ux %.8ux %.8ux\n", a, a[0], a[1], a[2], a[3], a[4], a[5]);
439 a = KPTR(c->scriptpa+E_dsa_addr);
440 iprint("dsa_addr: %.8ux %.8ux %.8ux %.8ux %.8ux\n",
441 a[0], a[1], a[2], a[3], a[4]);
442 a = KPTR(c->scriptpa+E_issue_addr);
443 iprint("issue_addr: %.8ux %.8ux %.8ux %.8ux %.8ux\n",
444 a[0], a[1], a[2], a[3], a[4]);
446 a = KPTR(c->scriptpa+E_issue_test_begin);
447 e = KPTR(c->scriptpa+E_issue_test_end);
448 iprint("issue_test code (at offset %.8ux):\n", E_issue_test_begin);
452 iprint(" %.8ux", *a);
462 dsafind(Controller *c, uchar target, uchar lun, uchar state)
466 for (d = KPTR(legetl(c->dsalist.head)); d != nil && d != dsaend; d = KPTR(legetl(d->next))) {
467 if (d->target != 0xff && d->target != target)
469 if (lun != 0xff && d->lun != lun)
471 if (state != 0xff && d->stateb != state)
479 dumpncrregs(Controller *c, int intr)
483 int depth = c->v->registers / 4;
486 IPRINT("sa = %.8lux\n", c->scriptpa);
489 KPRINT("sa = %.8lux\n", c->scriptpa);
491 for (i = 0; i < depth; i++) {
493 for (j = 0; j < 4; j++) {
494 int k = j * depth + i;
497 /* display little-endian to make 32-bit values readable */
500 IPRINT(" %.2x%.2x%.2x%.2x %.2x %.2x", p[3], p[2], p[1], p[0], k * 4, (k * 4) + 0x80);
503 KPRINT(" %.2x%.2x%.2x%.2x %.2x %.2x", p[3], p[2], p[1], p[0], k * 4, (k * 4) + 0x80);
517 chooserate(Controller *c, int tpf, int *scfp, int *xferpp)
519 /* find lowest entry >= tpf */
526 if (c->v->feature & Ultra2)
528 else if (c->v->feature & Ultra)
534 * search large clock factors first since this should
535 * result in more reliable transfers
537 for (scf = maxscf; scf >= 1; scf--) {
538 for (xferp = 0; xferp < 8; xferp++) {
539 unsigned char v = c->synctab[scf - 1][xferp];
542 if (v >= tpf && v < besttpf) {
559 synctabinit(Controller *c)
562 unsigned long scsilimit;
564 unsigned long cr, sr;
569 if (c->v->feature & Ultra2)
571 else if (c->v->feature & Ultra)
577 * for chips with no clock doubler, but Ultra capable (e.g. 860, or interestingly the
578 * first spin of the 875), assume 80MHz
579 * otherwise use the internal (33 Mhz) or external (40MHz) default
582 if ((c->v->feature & Ultra) != 0 && (c->v->feature & (ClockDouble | ClockQuad)) == 0)
583 c->sclk = ULTRA_NOCLOCKDOUBLE_SCLK;
588 * otherwise, if the chip is Ultra capable, but has a slow(ish) clock,
592 if (SCLK <= 40000000) {
593 if (c->v->feature & ClockDouble) {
597 else if (c->v->feature & ClockQuad) {
607 /* derive CCF from sclk */
608 /* woebetide anyone with SCLK < 16.7 or > 80MHz */
609 if (c->sclk <= 25 * MEGA)
611 else if (c->sclk <= 3750000)
613 else if (c->sclk <= 50 * MEGA)
615 else if (c->sclk <= 75 * MEGA)
617 else if ((c->v->feature & ClockDouble) && c->sclk <= 80 * MEGA)
619 else if ((c->v->feature & ClockQuad) && c->sclk <= 120 * MEGA)
621 else if ((c->v->feature & ClockQuad) && c->sclk <= 160 * MEGA)
624 for (scf = 1; scf < maxscf; scf++) {
625 /* check for legal core rate */
626 /* round up so we run slower for safety */
627 cr = (c->sclk * 2 + cf2[scf] - 1) / cf2[scf];
628 if (cr <= MAXSYNCCORERATE) {
629 scsilimit = MAXSYNCSCSIRATE;
632 else if (cr <= MAXFASTSYNCCORERATE) {
633 scsilimit = MAXFASTSYNCSCSIRATE;
636 else if ((c->v->feature & Ultra) && cr <= MAXULTRASYNCCORERATE) {
637 scsilimit = MAXULTRASYNCSCSIRATE;
640 else if ((c->v->feature & Ultra2) && cr <= MAXULTRA2SYNCCORERATE) {
641 scsilimit = MAXULTRA2SYNCSCSIRATE;
646 for (xferp = 11; xferp >= 4; xferp--) {
649 /* calculate scsi rate - round up again */
650 /* start from sclk for accuracy */
651 int totaldivide = xferp * cf2[scf];
652 sr = (c->sclk * 2 + totaldivide - 1) / totaldivide;
656 * now work out transfer period
657 * round down now so that period is pessimistic
659 tp = (MEGA * 1000) / sr;
663 if (tp < 25 || tp > 255 * 4)
666 * spot stupid special case for Ultra or Ultra2
667 * while working out factor
678 * now check tpf looks sensible
683 /* scf must be ccf for SCSI 1 */
684 ok = tpf >= 50 && scf == c->ccf;
687 ok = tpf >= 25 && tpf < 50;
691 * must use xferp of 4, or 5 at a pinch
692 * for an Ultra transfer
694 ok = xferp <= 5 && tpf >= 12 && tpf < 25;
697 ok = xferp == 4 && (tpf == 10 || tpf == 11);
704 c->synctab[scf - 1][xferp - 4] = tpf;
709 if (c->v->feature & Ultra2)
713 if (c->v->feature & Ultra)
717 for (; tpf < 256; tpf++) {
718 if (chooserate(c, tpf, &scf, &xferp) == tpf) {
719 unsigned tp = tpf == 10 ? 25 : (tpf == 12 ? 50 : tpf * 4);
720 unsigned long khz = (MEGA + tp - 1) / (tp);
721 KPRINT(PRINTPREFIX "tpf=%d scf=%d.%.1d xferp=%d mhz=%ld.%.3ld\n",
722 tpf, cf2[scf] / 2, (cf2[scf] & 1) ? 5 : 0,
723 xferp + 4, khz / 1000, khz % 1000);
726 c->tpf = tpf; /* note lowest value for controller */
732 synctodsa(Dsa *dsa, Controller *c)
735 KPRINT("synctodsa(dsa=%lux, target=%d, scntl3=%.2lx sxfer=%.2x)\n",
736 dsa, dsa->target, c->scntl3[dsa->target], c->sxfer[dsa->target]);
738 dsa->scntl3 = c->scntl3[dsa->target];
739 dsa->sxfer = c->sxfer[dsa->target];
743 setsync(Dsa *dsa, Controller *c, int target, uchar ultra, uchar scf, uchar xferp, uchar reqack)
746 (c->scntl3[target] & 0x08) | (((scf << 4) | c->ccf | (ultra << 7)) & ~0x08);
747 c->sxfer[target] = (xferp << 5) | reqack;
748 c->s[target] = BothDone;
751 c->n->scntl3 = c->scntl3[target];
752 c->n->sxfer = c->sxfer[target];
757 setasync(Dsa *dsa, Controller *c, int target)
759 setsync(dsa, c, target, 0, c->ccf, 0, 0);
763 setwide(Dsa *dsa, Controller *c, int target, uchar wide)
765 c->scntl3[target] = wide ? (1 << 3) : 0;
766 setasync(dsa, c, target);
767 c->s[target] = WideDone;
771 buildsdtrmsg(uchar *buf, uchar tpf, uchar offset)
782 buildwdtrmsg(uchar *buf, uchar expo)
792 start(Controller *c, long entry)
797 panic(PRINTPREFIX "start called while running");
799 p = c->scriptpa + entry;
800 lesetl(c->n->dsp, p);
803 c->n->dcntl |= 0x4; /* start DMA in SSI mode */
807 ncrcontinue(Controller *c)
810 panic(PRINTPREFIX "ncrcontinue called while running");
811 /* set the start DMA bit to continue execution */
818 softreset(Controller *c)
822 n->istat = Srst; /* software reset */
824 /* general initialisation */
825 n->scid = (1 << 6) | 7; /* respond to reselect, ID 7 */
826 n->respid = 1 << 7; /* response ID = 7 */
829 n->stest1 = 0x80; /* disable external scsi clock */
834 n->stime0 = 0xdd; /* about 0.5 second timeout on each device */
835 n->scntl0 |= 0x8; /* Enable parity checking */
837 /* continued setup */
841 n->stest3 = 0x80; /* TolerANT enable */
844 if (c->v->feature & BigFifo)
845 n->ctest5 = (1 << 5);
846 n->dmode = c->v->burst << 6; /* set burst length bits */
848 n->ctest5 |= (1 << 2); /* including overflow into ctest5 bit 2 */
849 if (c->v->feature & Prefetch)
850 n->dcntl |= (1 << 5); /* prefetch enable */
851 else if (c->v->feature & BurstOpCodeFetch)
852 n->dmode |= (1 << 1); /* burst opcode fetch */
853 if (c->v->feature & Differential) {
855 if ((c->feature & Differential) || bios_set_differential(c)) {
856 /* user enabled, or some evidence bios set differential */
857 if (n->sstat2 & (1 << 2))
858 print(PRINTPREFIX "can't go differential; wrong cable\n");
860 n->stest2 = (1 << 5);
861 print(PRINTPREFIX "differential mode set\n");
866 n->stest1 |= (1 << 3); /* power up doubler */
868 n->stest3 |= (1 << 5); /* stop clock */
869 n->stest1 |= (1 << 2); /* enable doubler */
870 n->stest3 &= ~(1 << 5); /* start clock */
876 msgsm(Dsa *dsa, Controller *c, int msg, int *cont, int *wakeme)
878 uchar histpf, hisreqack;
885 switch (c->s[dsa->target]) {
889 /* reply to my SDTR */
890 histpf = n->scratcha[2];
891 hisreqack = n->scratcha[3];
892 KPRINT(PRINTPREFIX "%d: SDTN response %d %d\n",
893 dsa->target, histpf, hisreqack);
896 setasync(dsa, c, dsa->target);
898 /* hisreqack should be <= c->v->maxsyncoff */
899 tpf = chooserate(c, histpf, &scf, &xferp);
900 KPRINT(PRINTPREFIX "%d: SDTN: using %d %d\n",
901 dsa->target, tpf, hisreqack);
902 setsync(dsa, c, dsa->target, tpf < 25, scf, xferp, hisreqack);
906 case A_SIR_EV_PHASE_SWITCH_AFTER_ID:
907 /* target ignored ATN for message after IDENTIFY - not SCSI-II */
908 KPRINT(PRINTPREFIX "%d: illegal phase switch after ID message - SCSI-1 device?\n", dsa->target);
909 KPRINT(PRINTPREFIX "%d: SDTN: async\n", dsa->target);
910 setasync(dsa, c, dsa->target);
911 *cont = E_to_decisions;
913 case A_SIR_MSG_REJECT:
914 /* rejection of my SDTR */
915 KPRINT(PRINTPREFIX "%d: SDTN: rejected SDTR\n", dsa->target);
917 KPRINT(PRINTPREFIX "%d: SDTN: async\n", dsa->target);
918 setasync(dsa, c, dsa->target);
926 /* reply to my WDTR */
927 KPRINT(PRINTPREFIX "%d: WDTN: response %d\n",
928 dsa->target, n->scratcha[2]);
929 setwide(dsa, c, dsa->target, n->scratcha[2]);
932 case A_SIR_EV_PHASE_SWITCH_AFTER_ID:
933 /* target ignored ATN for message after IDENTIFY - not SCSI-II */
934 KPRINT(PRINTPREFIX "%d: illegal phase switch after ID message - SCSI-1 device?\n", dsa->target);
935 setwide(dsa, c, dsa->target, 0);
936 *cont = E_to_decisions;
938 case A_SIR_MSG_REJECT:
939 /* rejection of my SDTR */
940 KPRINT(PRINTPREFIX "%d: WDTN: rejected WDTR\n", dsa->target);
941 setwide(dsa, c, dsa->target, 0);
951 case A_SIR_MSG_WDTR: {
952 uchar hiswide, mywide;
953 hiswide = n->scratcha[2];
954 mywide = (c->v->feature & Wide) != 0;
955 KPRINT(PRINTPREFIX "%d: WDTN: target init %d\n",
956 dsa->target, hiswide);
957 if (hiswide < mywide)
959 KPRINT(PRINTPREFIX "%d: WDTN: responding %d\n",
960 dsa->target, mywide);
961 setwide(dsa, c, dsa->target, mywide);
962 len = buildwdtrmsg(dsa->msg_out, mywide);
963 setmovedata(&dsa->msg_out_buf, DMASEG(dsa->msg_out), len);
965 c->s[dsa->target] = WideResponse;
973 /* target decides to renegotiate */
974 histpf = n->scratcha[2];
975 hisreqack = n->scratcha[3];
976 KPRINT(PRINTPREFIX "%d: SDTN: target init %d %d\n",
977 dsa->target, histpf, hisreqack);
978 if (hisreqack == 0) {
979 /* he wants asynchronous */
980 setasync(dsa, c, dsa->target);
984 /* he wants synchronous */
985 tpf = chooserate(c, histpf, &scf, &xferp);
986 if (hisreqack > c->v->maxsyncoff)
987 hisreqack = c->v->maxsyncoff;
988 KPRINT(PRINTPREFIX "%d: using %d %d\n",
989 dsa->target, tpf, hisreqack);
990 setsync(dsa, c, dsa->target, tpf < 25, scf, xferp, hisreqack);
992 /* build my SDTR message */
993 len = buildsdtrmsg(dsa->msg_out, tpf, hisreqack);
994 setmovedata(&dsa->msg_out_buf, DMASEG(dsa->msg_out), len);
996 c->s[dsa->target] = SyncResponse;
1003 case A_SIR_EV_RESPONSE_OK:
1004 c->s[dsa->target] = WideDone;
1005 KPRINT(PRINTPREFIX "%d: WDTN: response accepted\n", dsa->target);
1008 case A_SIR_MSG_REJECT:
1009 setwide(dsa, c, dsa->target, 0);
1010 KPRINT(PRINTPREFIX "%d: WDTN: response REJECTed\n", dsa->target);
1017 case A_SIR_EV_RESPONSE_OK:
1018 c->s[dsa->target] = BothDone;
1019 KPRINT(PRINTPREFIX "%d: SDTN: response accepted (%s)\n",
1020 dsa->target, phase[n->sstat1 & 7]);
1023 case A_SIR_MSG_REJECT:
1024 setasync(dsa, c, dsa->target);
1025 KPRINT(PRINTPREFIX "%d: SDTN: response REJECTed\n", dsa->target);
1031 KPRINT(PRINTPREFIX "%d: msgsm: state %d msg %d\n",
1032 dsa->target, c->s[dsa->target], msg);
1038 calcblockdma(Dsa *d, ulong base, ulong count)
1044 blocks = count / A_BSIZE;
1048 d->dmablks = blocks;
1049 d->dmaaddr[0] = base;
1050 d->dmaaddr[1] = base >> 8;
1051 d->dmaaddr[2] = base >> 16;
1052 d->dmaaddr[3] = base >> 24;
1053 setmovedata(&d->data_buf, base + blocks * A_BSIZE, count - blocks * A_BSIZE);
1054 d->flag = legetl(d->data_buf.dbc) == 0;
1058 read_mismatch_recover(Controller *c, Ncr *n, Dsa *dsa)
1061 uchar dfifo = n->dfifo;
1064 dbc = (n->dbc[2]<<16)|(n->dbc[1]<<8)|n->dbc[0];
1065 if (n->ctest5 & (1 << 5))
1066 inchip = ((dfifo | ((n->ctest5 & 3) << 8)) - (dbc & 0x3ff)) & 0x3ff;
1068 inchip = ((dfifo & 0x7f) - (dbc & 0x7f)) & 0x7f;
1070 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: DMA FIFO = %d\n",
1071 dsa->target, dsa->lun, inchip);
1073 if (n->sxfer & SYNCOFFMASK(c)) {
1075 uchar fifo = n->sstat1 >> 4;
1076 if (c->v->maxsyncoff > 8)
1077 fifo |= (n->sstat2 & (1 << 4));
1080 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: SCSI FIFO = %d\n",
1081 dsa->target, dsa->lun, fifo);
1085 if (n->sstat0 & (1 << 7)) {
1087 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: SIDL full\n",
1088 dsa->target, dsa->lun);
1090 if (n->sstat2 & (1 << 7)) {
1092 IPRINT(PRINTPREFIX "%d/%d: read_mismatch_recover: SIDL msb full\n",
1093 dsa->target, dsa->lun);
1101 write_mismatch_recover(Controller *c, Ncr *n, Dsa *dsa)
1104 uchar dfifo = n->dfifo;
1107 dbc = (n->dbc[2]<<16)|(n->dbc[1]<<8)|n->dbc[0];
1109 if (n->ctest5 & (1 << 5))
1110 inchip = ((dfifo | ((n->ctest5 & 3) << 8)) - (dbc & 0x3ff)) & 0x3ff;
1112 inchip = ((dfifo & 0x7f) - (dbc & 0x7f)) & 0x7f;
1115 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: DMA FIFO = %d\n",
1116 dsa->target, dsa->lun, inchip);
1119 if (n->sstat0 & (1 << 5)) {
1122 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODL full\n", dsa->target, dsa->lun);
1125 if (n->sstat2 & (1 << 5)) {
1128 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODL msb full\n", dsa->target, dsa->lun);
1131 if (n->sxfer & SYNCOFFMASK(c)) {
1132 /* synchronous SODR */
1133 if (n->sstat0 & (1 << 6)) {
1136 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODR full\n",
1137 dsa->target, dsa->lun);
1140 if (n->sstat2 & (1 << 6)) {
1143 IPRINT(PRINTPREFIX "%d/%d: write_mismatch_recover: SODR msb full\n",
1144 dsa->target, dsa->lun);
1148 /* clear the dma fifo */
1149 n->ctest3 |= (1 << 2);
1150 /* wait till done */
1151 while ((n->dstat & Dfe) == 0)
1153 return dbc + inchip;
1157 sd53c8xxinterrupt(Ureg *ur, void *a)
1170 IPRINT(PRINTPREFIX "int\n");
1176 int wokesomething = 0;
1178 IPRINT(PRINTPREFIX "Intfly\n");
1181 /* search for structures in A_STATE_DONE */
1182 for (d = KPTR(legetl(c->dsalist.head)); d != nil && d != dsaend; d = KPTR(legetl(d->next))) {
1183 if (d->stateb == A_STATE_DONE) {
1184 d->p9status = d->status;
1186 IPRINT(PRINTPREFIX "waking up dsa %#p\n", d);
1192 if (!wokesomething) {
1193 IPRINT(PRINTPREFIX "nothing to wake up\n");
1197 if ((istat & (Sip | Dip)) == 0) {
1199 IPRINT(PRINTPREFIX "int end %x\n", istat);
1205 sist = (n->sist1<<8)|n->sist0; /* BUG? can two-byte read be inconsistent? */
1207 dsapa = legetl(n->dsa);
1210 * Can't compute dsa until we know that dsapa is valid.
1212 if(DMASEG_TO_PADDR(dsapa) < -KZERO)
1213 dsa = (Dsa*)DMASEG_TO_KADDR(dsapa);
1217 * happens at startup on some cards but we
1218 * don't actually deref dsa because none of the
1219 * flags we are about are set.
1220 * still, print in case that changes and we're
1221 * about to dereference nil.
1223 iprint("sd53c8xxinterrupt: dsa=%.8lux istat=%ux sist=%ux dstat=%ux\n", dsapa, istat, sist, dstat);
1229 IPRINT("sist = %.4x\n", sist);
1239 addr = legetl(n->dsp);
1240 sa = addr - c->scriptpa;
1241 if (DEBUG(1) || DEBUG(2)) {
1242 IPRINT(PRINTPREFIX "%d/%d: Phase Mismatch sa=%.8lux\n",
1243 dsa->target, dsa->lun, sa);
1248 if (sa == E_data_in_mismatch) {
1250 * though this is a failure in the residue, there may have been blocks
1251 * as well. if so, dmablks will not have been zeroed, since the state
1252 * was not saved by the microcode.
1254 dbc = read_mismatch_recover(c, n, dsa);
1255 tbc = legetl(dsa->data_buf.dbc) - dbc;
1258 advancedata(&dsa->data_buf, tbc);
1259 if (DEBUG(1) || DEBUG(2)) {
1260 IPRINT(PRINTPREFIX "%d/%d: transferred = %ld residue = %ld\n",
1261 dsa->target, dsa->lun, tbc, legetl(dsa->data_buf.dbc));
1263 cont = E_data_mismatch_recover;
1265 else if (sa == E_data_in_block_mismatch) {
1266 dbc = read_mismatch_recover(c, n, dsa);
1267 tbc = A_BSIZE - dbc;
1268 /* recover current state from registers */
1269 dmablks = n->scratcha[2];
1270 dmaaddr = legetl(n->scratchb);
1271 /* we have got to dmaaddr + tbc */
1272 /* we have dmablks * A_BSIZE - tbc + residue left to do */
1273 /* so remaining transfer is */
1274 IPRINT("in_block_mismatch: dmaaddr = 0x%lux tbc=%lud dmablks=%d\n",
1275 dmaaddr, tbc, dmablks);
1276 calcblockdma(dsa, dmaaddr + tbc,
1277 dmablks * A_BSIZE - tbc + legetl(dsa->data_buf.dbc));
1278 /* copy changes into scratch registers */
1279 IPRINT("recalc: dmablks %d dmaaddr 0x%lx pa 0x%lx dbc %ld\n",
1280 dsa->dmablks, legetl(dsa->dmaaddr),
1281 legetl(dsa->data_buf.pa), legetl(dsa->data_buf.dbc));
1282 n->scratcha[2] = dsa->dmablks;
1283 lesetl(n->scratchb, *((ulong*)dsa->dmaaddr));
1284 cont = E_data_block_mismatch_recover;
1286 else if (sa == E_data_out_mismatch) {
1287 dbc = write_mismatch_recover(c, n, dsa);
1288 tbc = legetl(dsa->data_buf.dbc) - dbc;
1291 advancedata(&dsa->data_buf, tbc);
1292 if (DEBUG(1) || DEBUG(2)) {
1293 IPRINT(PRINTPREFIX "%d/%d: transferred = %ld residue = %ld\n",
1294 dsa->target, dsa->lun, tbc, legetl(dsa->data_buf.dbc));
1296 cont = E_data_mismatch_recover;
1298 else if (sa == E_data_out_block_mismatch) {
1299 dbc = write_mismatch_recover(c, n, dsa);
1300 tbc = legetl(dsa->data_buf.dbc) - dbc;
1301 /* recover current state from registers */
1302 dmablks = n->scratcha[2];
1303 dmaaddr = legetl(n->scratchb);
1304 /* we have got to dmaaddr + tbc */
1305 /* we have dmablks blocks - tbc + residue left to do */
1306 /* so remaining transfer is */
1307 IPRINT("out_block_mismatch: dmaaddr = %lux tbc=%lud dmablks=%d\n",
1308 dmaaddr, tbc, dmablks);
1309 calcblockdma(dsa, dmaaddr + tbc,
1310 dmablks * A_BSIZE - tbc + legetl(dsa->data_buf.dbc));
1311 /* copy changes into scratch registers */
1312 n->scratcha[2] = dsa->dmablks;
1313 lesetl(n->scratchb, *((ulong*)dsa->dmaaddr));
1314 cont = E_data_block_mismatch_recover;
1316 else if (sa == E_id_out_mismatch) {
1318 * target switched phases while attention held during
1319 * message out. The possibilities are:
1320 * 1. It didn't like the last message. This is indicated
1321 * by the new phase being message_in. Use script to recover
1323 * 2. It's not SCSI-II compliant. The new phase will be other
1324 * than message_in. We should also indicate that the device
1325 * is asynchronous, if it's the SDTR that got ignored
1327 * For now, if the phase switch is not to message_in, and
1328 * and it happens after IDENTIFY and before SDTR, we
1329 * notify the negotiation state machine.
1331 ulong lim = legetl(dsa->msg_out_buf.dbc);
1332 uchar p = n->sstat1 & 7;
1333 dbc = write_mismatch_recover(c, n, dsa);
1335 IPRINT(PRINTPREFIX "%d/%d: msg_out_mismatch: %lud/%lud sent, phase %s\n",
1336 dsa->target, dsa->lun, tbc, lim, phase[p]);
1337 if (p != MessageIn && tbc == 1) {
1338 msgsm(dsa, c, A_SIR_EV_PHASE_SWITCH_AFTER_ID, &cont, &wakeme);
1341 cont = E_id_out_mismatch_recover;
1343 else if (sa == E_cmd_out_mismatch) {
1345 * probably the command count is longer than the device wants ...
1347 ulong lim = legetl(dsa->cmd_buf.dbc);
1348 uchar p = n->sstat1 & 7;
1349 dbc = write_mismatch_recover(c, n, dsa);
1351 IPRINT(PRINTPREFIX "%d/%d: cmd_out_mismatch: %lud/%lud sent, phase %s\n",
1352 dsa->target, dsa->lun, tbc, lim, phase[p]);
1354 cont = E_to_decisions;
1357 IPRINT(PRINTPREFIX "%d/%d: ma sa=%.8lux wanted=%s got=%s\n",
1358 dsa->target, dsa->lun, sa,
1360 phase[n->sstat1 & 7]);
1362 dsa->p9status = SDeio; /* chf */
1366 /*else*/ if (sist & 0x400) {
1368 IPRINT(PRINTPREFIX "%d/%d Sto\n", dsa->target, dsa->lun);
1370 dsa->p9status = SDtimeout;
1371 dsa->stateb = A_STATE_DONE;
1374 cont = E_issue_check;
1378 IPRINT(PRINTPREFIX "%d/%d: parity error\n", dsa->target, dsa->lun);
1379 dsa->parityerror = 1;
1382 IPRINT(PRINTPREFIX "%s%d lun %d: unexpected disconnect\n",
1383 c->sdev->name, dsa->target, dsa->lun);
1386 dsa->p9status = SDeio;
1391 IPRINT("dstat = %.2x\n", dstat);
1393 /*else*/ if (dstat & Ssi) {
1394 ulong w = legetl(n->dsp) - c->scriptpa;
1395 IPRINT("[%lux]", w);
1397 cont = -2; /* restart */
1400 switch (legetl(n->dsps)) {
1401 case A_SIR_MSG_IO_COMPLETE:
1402 dsa->p9status = dsa->status;
1405 case A_SIR_MSG_SDTR:
1406 case A_SIR_MSG_WDTR:
1407 case A_SIR_MSG_REJECT:
1408 case A_SIR_EV_RESPONSE_OK:
1409 msgsm(dsa, c, legetl(n->dsps), &cont, &wakeme);
1411 case A_SIR_MSG_IGNORE_WIDE_RESIDUE:
1412 /* back up one in the data transfer */
1413 IPRINT(PRINTPREFIX "%d/%d: ignore wide residue %d, WSR = %d\n",
1414 dsa->target, dsa->lun, n->scratcha[1], n->scntl2 & 1);
1415 if (dsa->flag == 2) {
1416 IPRINT(PRINTPREFIX "%d/%d: transfer over; residue ignored\n",
1417 dsa->target, dsa->lun);
1420 calcblockdma(dsa, legetl(dsa->dmaaddr) - 1,
1421 dsa->dmablks * A_BSIZE + legetl(dsa->data_buf.dbc) + 1);
1425 case A_SIR_ERROR_NOT_MSG_IN_AFTER_RESELECT:
1426 IPRINT(PRINTPREFIX "%d: not msg_in after reselect (%s)",
1427 n->ssid & SSIDMASK(c), phase[n->sstat1 & 7]);
1428 dsa = dsafind(c, n->ssid & SSIDMASK(c), -1, A_STATE_DISCONNECTED);
1432 case A_SIR_NOTIFY_LOAD_STATE:
1433 IPRINT(PRINTPREFIX ": load_state dsa=%p\n", dsa);
1437 panic("bad dsa in load_state");
1441 case A_SIR_NOTIFY_MSG_IN:
1442 IPRINT(PRINTPREFIX "%d/%d: msg_in %d\n",
1443 dsa->target, dsa->lun, n->sfbr);
1446 case A_SIR_NOTIFY_DISC:
1447 IPRINT(PRINTPREFIX "%d/%d: disconnect:", dsa->target, dsa->lun);
1449 case A_SIR_NOTIFY_STATUS:
1450 IPRINT(PRINTPREFIX "%d/%d: status\n", dsa->target, dsa->lun);
1453 case A_SIR_NOTIFY_COMMAND:
1454 IPRINT(PRINTPREFIX "%d/%d: commands\n", dsa->target, dsa->lun);
1457 case A_SIR_NOTIFY_DATA_IN:
1458 IPRINT(PRINTPREFIX "%d/%d: data in a %lx b %lx\n",
1459 dsa->target, dsa->lun, legetl(n->scratcha), legetl(n->scratchb));
1462 case A_SIR_NOTIFY_BLOCK_DATA_IN:
1463 IPRINT(PRINTPREFIX "%d/%d: block data in: a2 %x b %lx\n",
1464 dsa->target, dsa->lun, n->scratcha[2], legetl(n->scratchb));
1467 case A_SIR_NOTIFY_DATA_OUT:
1468 IPRINT(PRINTPREFIX "%d/%d: data out\n", dsa->target, dsa->lun);
1471 case A_SIR_NOTIFY_DUMP:
1472 IPRINT(PRINTPREFIX "%d/%d: dump\n", dsa->target, dsa->lun);
1476 case A_SIR_NOTIFY_DUMP2:
1477 IPRINT(PRINTPREFIX "%d/%d: dump2:", dsa->target, dsa->lun);
1478 IPRINT(" sa %lux", legetl(n->dsp) - c->scriptpa);
1479 IPRINT(" dsa %lux", legetl(n->dsa));
1480 IPRINT(" sfbr %ux", n->sfbr);
1481 IPRINT(" a %lux", legetl(n->scratcha));
1482 IPRINT(" b %lux", legetl(n->scratchb));
1483 IPRINT(" ssid %ux", n->ssid);
1487 case A_SIR_NOTIFY_WAIT_RESELECT:
1488 IPRINT(PRINTPREFIX "wait reselect\n");
1491 case A_SIR_NOTIFY_RESELECT:
1492 IPRINT(PRINTPREFIX "reselect: ssid %.2x sfbr %.2x at %ld\n",
1493 n->ssid, n->sfbr, TK2MS(m->ticks));
1496 case A_SIR_NOTIFY_ISSUE:
1497 IPRINT(PRINTPREFIX "%d/%d: issue dsa=%p end=%p:", dsa->target, dsa->lun, dsa, dsaend);
1499 IPRINT(" tgt=%d", dsa->target);
1500 IPRINT(" time=%ld", TK2MS(m->ticks));
1504 case A_SIR_NOTIFY_ISSUE_CHECK:
1505 IPRINT(PRINTPREFIX "issue check\n");
1508 case A_SIR_NOTIFY_SIGP:
1509 IPRINT(PRINTPREFIX "responded to SIGP\n");
1512 case A_SIR_NOTIFY_DUMP_NEXT_CODE: {
1513 ulong *dsp = &c->script[(legetl(n->dsp)-c->scriptpa)/4];
1515 IPRINT(PRINTPREFIX "code at %lux", (ulong)(dsp - c->script));
1516 for (x = 0; x < 6; x++) {
1517 IPRINT(" %.8lux", dsp[x]);
1524 case A_SIR_NOTIFY_WSR:
1525 IPRINT(PRINTPREFIX "%d/%d: WSR set\n", dsa->target, dsa->lun);
1528 case A_SIR_NOTIFY_LOAD_SYNC:
1529 IPRINT(PRINTPREFIX "%d/%d: scntl=%.2x sxfer=%.2x\n",
1530 dsa->target, dsa->lun, n->scntl3, n->sxfer);
1533 case A_SIR_NOTIFY_RESELECTED_ON_SELECT:
1535 IPRINT(PRINTPREFIX "%d/%d: reselected during select\n",
1536 dsa->target, dsa->lun);
1540 case A_error_reselected: /* dsa isn't valid here */
1541 iprint(PRINTPREFIX "reselection error\n");
1543 for (dsa = KPTR(legetl(c->dsalist.head)); dsa != nil && dsa != dsaend; dsa = KPTR(legetl(dsa->next))) {
1544 IPRINT(PRINTPREFIX "dsa target %d lun %d state %d\n", dsa->target, dsa->lun, dsa->stateb);
1548 IPRINT(PRINTPREFIX "%d/%d: script error %ld\n",
1549 dsa->target, dsa->lun, legetl(n->dsps));
1554 /*else*/ if (dstat & Iid) {
1556 ulong addr, dbc, *v;
1558 addr = legetl(n->dsp);
1560 target = dsa->target;
1566 dbc = (n->dbc[2]<<16)|(n->dbc[1]<<8)|n->dbc[0];
1570 IPRINT(PRINTPREFIX "%d/%d: Iid pa=%.8lux sa=%.8lux dbc=%lux\n",
1572 addr, addr - c->scriptpa, dbc);
1573 addr -= c->scriptpa;
1576 v = &c->script[addr/4];
1578 IPRINT("%.8lux: %.8lux %.8lux %.8lux %.8lux\n",
1579 addr, v[0], v[1], v[2], v[3]);
1589 dsa->p9status = SDeio;
1592 /*else*/ if (dstat & Bf) {
1593 IPRINT(PRINTPREFIX "%d/%d: Bus Fault\n", dsa->target, dsa->lun);
1595 dsa->p9status = SDeio;
1604 if(dsa->p9status == SDnostatus)
1605 dsa->p9status = SDeio;
1610 IPRINT(PRINTPREFIX "int end 1\n");
1617 return ((Dsa *)arg)->p9status != SDnostatus;
1621 setmovedata(Movedata *d, ulong pa, ulong bc)
1634 advancedata(Movedata *d, long v)
1636 lesetl(d->pa, legetl(d->pa) + v);
1637 lesetl(d->dbc, legetl(d->dbc) - v);
1641 dumpwritedata(uchar *data, int datalen)
1646 USED(data, datalen);
1651 KPRINT(PRINTPREFIX "write:");
1652 for (i = 0, bp = data; i < 50 && i < datalen; i++, bp++) {
1653 KPRINT("%.2ux", *bp);
1663 dumpreaddata(uchar *data, int datalen)
1668 USED(data, datalen);
1673 KPRINT(PRINTPREFIX "read:");
1674 for (i = 0, bp = data; i < 50 && i < datalen; i++, bp++) {
1675 KPRINT("%.2ux", *bp);
1685 busreset(Controller *c)
1690 c->n->scntl1 |= (1 << 3);
1692 c->n->scntl1 &= ~(1 << 3);
1693 if(!(c->v->feature & Wide))
1696 ntarget = MAXTARGET;
1697 for (x = 0; x < ntarget; x++) {
1698 setwide(0, c, x, 0);
1700 c->s[x] = NeitherDone;
1707 reset(Controller *c)
1709 /* should wakeup all pending tasks */
1715 sd53c8xxrio(SDreq* r)
1720 uchar target_expo, my_expo;
1721 int bc, check, i, status, target;
1723 if((target = r->unit->subno) == 0x07)
1724 return r->status = SDtimeout; /* assign */
1726 c = r->unit->dev->ctlr;
1729 d = dsaalloc(c, target, r->lun);
1731 qlock(&c->q[target]); /* obtain access to target */
1733 /* load the transfer control stuff */
1734 d->scsi_id_buf[0] = 0;
1735 d->scsi_id_buf[1] = c->sxfer[target];
1736 d->scsi_id_buf[2] = target;
1737 d->scsi_id_buf[3] = c->scntl3[target];
1742 d->msg_out[bc] = 0x80 | r->lun;
1744 #ifndef NO_DISCONNECT
1745 d->msg_out[bc] |= (1 << 6);
1749 /* work out what to do about negotiation */
1750 switch (c->s[target]) {
1752 KPRINT(PRINTPREFIX "%d: strange nego state %d\n", target, c->s[target]);
1753 c->s[target] = NeitherDone;
1756 if ((c->capvalid & (1 << target)) == 0)
1758 target_expo = (c->cap[target] >> 5) & 3;
1759 my_expo = (c->v->feature & Wide) != 0;
1760 if (target_expo < my_expo)
1761 my_expo = target_expo;
1762 #ifdef ALWAYS_DO_WDTR
1763 bc += buildwdtrmsg(d->msg_out + bc, my_expo);
1764 KPRINT(PRINTPREFIX "%d: WDTN: initiating expo %d\n", target, my_expo);
1765 c->s[target] = WideInit;
1769 bc += buildwdtrmsg(d->msg_out + bc, (c->v->feature & Wide) ? 1 : 0);
1770 KPRINT(PRINTPREFIX "%d: WDTN: initiating expo %d\n", target, my_expo);
1771 c->s[target] = WideInit;
1774 KPRINT(PRINTPREFIX "%d: WDTN: narrow\n", target);
1778 if (c->cap[target] & (1 << 4)) {
1779 KPRINT(PRINTPREFIX "%d: SDTN: initiating %d %d\n", target, c->tpf, c->v->maxsyncoff);
1780 bc += buildsdtrmsg(d->msg_out + bc, c->tpf, c->v->maxsyncoff);
1781 c->s[target] = SyncInit;
1784 KPRINT(PRINTPREFIX "%d: SDTN: async only\n", target);
1785 c->s[target] = BothDone;
1792 setmovedata(&d->msg_out_buf, DMASEG(d->msg_out), bc);
1793 setmovedata(&d->cmd_buf, DMASEG(r->cmd), r->clen);
1794 calcblockdma(d, r->data ? DMASEG(r->data) : 0, r->dlen);
1797 KPRINT(PRINTPREFIX "%d/%d: exec: ", target, r->lun);
1798 for (bp = r->cmd; bp < &r->cmd[r->clen]; bp++) {
1799 KPRINT("%.2ux", *bp);
1803 KPRINT(PRINTPREFIX "%d/%d: exec: limit=(%d)%ld\n",
1804 target, r->lun, d->dmablks, legetl(d->data_buf.dbc));
1807 dumpwritedata(r->data, r->dlen);
1810 setmovedata(&d->status_buf, DMASEG(&d->status), 1);
1812 d->p9status = SDnostatus;
1815 d->stateb = A_STATE_ISSUE; /* start operation */
1820 c->n->dcntl |= 0x10; /* single step */
1825 start(c, E_issue_check);
1831 tsleep(d, done, d, 600 * 1000);
1835 KPRINT(PRINTPREFIX "%d/%d: exec: Timed out\n", target, r->lun);
1839 qunlock(&c->q[target]);
1840 r->status = SDtimeout;
1841 return r->status = SDtimeout; /* assign */
1844 if((status = d->p9status) == SDeio)
1845 c->s[target] = NeitherDone;
1846 if (d->parityerror) {
1855 KPRINT(PRINTPREFIX "%d/%d: exec: before rlen adjust: dmablks %d flag %d dbc %lud\n",
1856 target, r->lun, d->dmablks, d->flag, legetl(d->data_buf.dbc));
1860 r->rlen -= d->dmablks * A_BSIZE;
1861 r->rlen -= legetl(d->data_buf.dbc);
1864 dumpreaddata(r->data, r->rlen);
1866 KPRINT(PRINTPREFIX "%d/%d: exec: p9status=%d status %d rlen %ld\n",
1867 target, r->lun, d->p9status, status, r->rlen);
1872 if ((c->capvalid & (1 << target)) == 0
1873 && (status == SDok || status == SDcheck)
1874 && r->cmd[0] == 0x12 && r->dlen >= 8) {
1875 c->capvalid |= 1 << target;
1877 c->cap[target] = bp[7];
1878 KPRINT(PRINTPREFIX "%d: capabilities %.2x\n", target, bp[7]);
1880 if(!check && status == SDcheck && !(r->flags & SDnosense)){
1883 memset(r->cmd, 0, sizeof(r->cmd));
1885 r->cmd[1] = r->lun<<5;
1886 r->cmd[4] = sizeof(r->sense)-1;
1889 r->dlen = sizeof(r->sense)-1;
1891 * Clear out the microcode state
1892 * so the Dsa can be re-used.
1894 lesetl(&d->stateb, A_STATE_ALLOCATED);
1898 qunlock(&c->q[target]);
1901 if(status == SDok && check){
1903 r->flags |= SDvalidsense;
1906 KPRINT(PRINTPREFIX "%d: r flags %8.8uX status %d rlen %ld\n",
1907 target, r->flags, status, r->rlen);
1908 if(r->flags & SDvalidsense){
1910 KPRINT(PRINTPREFIX "%d: r flags %8.8uX status %d rlen %ld\n",
1911 target, r->flags, status, r->rlen);
1912 for(i = 0; i < r->rlen; i++)
1913 KPRINT(" %2.2uX", r->sense[i]);
1916 return r->status = status;
1920 cribbios(Controller *c)
1922 c->bios.scntl3 = c->n->scntl3;
1923 c->bios.stest2 = c->n->stest2;
1924 print(PRINTPREFIX "%s: bios scntl3(%.2x) stest2(%.2x)\n",
1925 c->sdev->name, c->bios.scntl3, c->bios.stest2);
1929 bios_set_differential(Controller *c)
1931 /* Concept lifted from FreeBSD - thanks Gerard */
1932 /* basically, if clock conversion factors are set, then there is
1933 * evidence the bios had a go at the chip, and if so, it would
1934 * have set the differential enable bit in stest2
1936 return (c->bios.scntl3 & 7) != 0 && (c->bios.stest2 & 0x20) != 0;
1939 #define NCR_VID 0x1000
1940 #define NCR_810_DID 0x0001
1941 #define NCR_820_DID 0x0002 /* don't know enough about this one to support it */
1942 #define NCR_825_DID 0x0003
1943 #define NCR_815_DID 0x0004
1944 #define SYM_810AP_DID 0x0005
1945 #define SYM_860_DID 0x0006
1946 #define SYM_896_DID 0x000b
1947 #define SYM_895_DID 0x000c
1948 #define SYM_885_DID 0x000d /* ditto */
1949 #define SYM_875_DID 0x000f /* ditto */
1950 #define SYM_1010_DID 0x0020
1951 #define SYM_1011_DID 0x0021
1952 #define SYM_875J_DID 0x008f
1954 static Variant variant[] = {
1955 { NCR_810_DID, 0x0f, "NCR53C810", Burst16, 8, 24, 0 },
1956 { NCR_810_DID, 0x1f, "SYM53C810ALV", Burst16, 8, 24, Prefetch },
1957 { NCR_810_DID, 0xff, "SYM53C810A", Burst16, 8, 24, Prefetch },
1958 { SYM_810AP_DID, 0xff, "SYM53C810AP", Burst16, 8, 24, Prefetch },
1959 { NCR_815_DID, 0xff, "NCR53C815", Burst16, 8, 24, BurstOpCodeFetch },
1960 { NCR_825_DID, 0x0f, "NCR53C825", Burst16, 8, 24, Wide|BurstOpCodeFetch|Differential },
1961 { NCR_825_DID, 0xff, "SYM53C825A", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide },
1962 { SYM_860_DID, 0x0f, "SYM53C860", Burst16, 8, 24, Prefetch|Ultra },
1963 { SYM_860_DID, 0xff, "SYM53C860LV", Burst16, 8, 24, Prefetch|Ultra },
1964 { SYM_875_DID, 0x01, "SYM53C875r1", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide|Ultra },
1965 { SYM_875_DID, 0xff, "SYM53C875", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide|Ultra|ClockDouble },
1966 { SYM_875J_DID, 0xff, "SYM53C875j", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Differential|Wide|Ultra|ClockDouble },
1967 { SYM_885_DID, 0xff, "SYM53C885", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Wide|Ultra|ClockDouble },
1968 { SYM_895_DID, 0xff, "SYM53C895", Burst128, 16, 24, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1969 { SYM_896_DID, 0xff, "SYM53C896", Burst128, 16, 64, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1970 { SYM_1010_DID, 0xff, "SYM53C1010", Burst128, 16, 64, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1971 { SYM_1011_DID, 0xff, "SYM53C1010", Burst128, 16, 64, Prefetch|LocalRAM|BigFifo|Wide|Ultra|Ultra2 },
1975 xfunc(Controller *c, enum na_external x, unsigned long *v)
1979 print("xfunc: can't find external %d\n", x);
1982 *v = offsetof(Dsa, scsi_id_buf[0]);
1985 *v = offsetof(Dsa, msg_out_buf);
1988 *v = offsetof(Dsa, cmd_buf);
1991 *v = offsetof(Dsa, data_buf);
1994 *v = offsetof(Dsa, status_buf);
1997 *v = DMASEG(&c->dsalist.head[0]);
2007 na_fixup(Controller *c, ulong pa_reg,
2008 struct na_patch *patch, int patches,
2009 int (*externval)(Controller*, int, ulong*))
2013 ulong *script, pa_script;
2014 unsigned long lw, lv;
2017 pa_script = c->scriptpa;
2018 for (p = 0; p < patches; p++) {
2019 switch (patch[p].type) {
2021 /* script relative */
2022 script[patch[p].lwoff] += pa_script;
2025 /* register i/o relative */
2026 script[patch[p].lwoff] += pa_reg;
2030 lw = script[patch[p].lwoff];
2031 v = (lw >> 8) & 0xff;
2032 if (!(*externval)(c, v, &lv))
2035 script[patch[p].lwoff] = (lw & 0xffff00ffL) | (v << 8);
2038 /* 32 bit external */
2039 lw = script[patch[p].lwoff];
2040 if (!(*externval)(c, lw, &lv))
2042 script[patch[p].lwoff] = lv;
2045 /* 24 bit external */
2046 lw = script[patch[p].lwoff];
2047 if (!(*externval)(c, lw & 0xffffff, &lv))
2049 script[patch[p].lwoff] = (lw & 0xff000000L) | (lv & 0xffffffL);
2065 SDev *sdev, *head, *tail;
2066 ulong regpa, *script, scriptpa;
2067 void *regva, *scriptva;
2069 if(cp = getconf("*maxsd53c8xx"))
2070 nctlr = strtoul(cp, 0, 0);
2076 while((p = pcimatch(p, NCR_VID, 0)) != nil && nctlr > 0){
2077 for(v = variant; v < &variant[nelem(variant)]; v++){
2078 if(p->did == v->did && p->rid <= v->maxrid)
2081 if(v >= &variant[nelem(variant)]) {
2082 print("no match\n");
2085 print(PRINTPREFIX "%s rev. 0x%2.2x intr=%d command=%4.4uX\n",
2086 v->name, p->rid, p->intl, p->pcr);
2088 regpa = p->mem[1].bar;
2098 regva = vmap(regpa, p->mem[1].size);
2106 if((v->feature & LocalRAM) && sizeof(na_script) <= 4096){
2107 scriptpa = p->mem[ba].bar;
2108 if((scriptpa & 0x04) && p->mem[ba+1].bar){
2109 vunmap(regva, p->mem[1].size);
2113 scriptva = vmap(scriptpa, p->mem[ba].size);
2119 * Either the map failed, or this chip does not have
2120 * local RAM. It will need a copy of the microcode.
2122 scriptma = malloc(sizeof(na_script));
2123 if(scriptma == nil){
2124 vunmap(regva, p->mem[1].size);
2127 scriptpa = DMASEG(scriptma);
2131 ctlr = malloc(sizeof(Controller));
2132 sdev = malloc(sizeof(SDev));
2133 if(ctlr == nil || sdev == nil){
2142 vunmap(scriptva, p->mem[ba].size);
2144 vunmap(regva, p->mem[1].size);
2148 lock(&ctlr->dsalist);
2149 ctlr->dsalist.freechain = nil;
2151 dsaend = xalloc(sizeof *dsaend);
2153 panic("sd53c8xxpnp: no memory");
2154 lesetl(&dsaend->stateb, A_STATE_END);
2155 lesetl(dsaend->next, DMASEG(dsaend));
2157 lesetl(ctlr->dsalist.head, DMASEG(dsaend));
2159 unlock(&ctlr->dsalist);
2163 ctlr->script = script;
2164 memmove(ctlr->script, na_script, sizeof(na_script));
2167 * Because we don't yet have an abstraction for the
2168 * addresses as seen from the controller side (and on
2169 * the 386 it doesn't matter), the following two lines
2170 * are different between the 386 and alpha copies of
2173 ctlr->scriptpa = scriptpa;
2174 if(!na_fixup(ctlr, regpa, na_patches, NA_PATCHES, xfunc)){
2175 print("script fixup failed\n");
2178 swabl(ctlr->script, ctlr->script, sizeof(na_script));
2182 sdev->ifc = &sd53c8xxifc;
2185 if(!(v->feature & Wide))
2188 sdev->nunit = MAXTARGET;
2204 sd53c8xxenable(SDev* sdev)
2211 pcidev = ctlr->pcidev;
2219 snprint(name, sizeof(name), "%s (%s)", sdev->name, sdev->ifc->name);
2220 intrenable(pcidev->intl, sd53c8xxinterrupt, ctlr, pcidev->tbdf, name);
2226 SDifc sd53c8xxifc = {
2227 "53c8xx", /* name */
2229 sd53c8xxpnp, /* pnp */
2231 sd53c8xxenable, /* enable */
2234 scsiverify, /* verify */
2235 scsionline, /* online */
2236 sd53c8xxrio, /* rio */