1*7150Swnj /* if_dmc.c 4.15 82/06/12 */ 25725Sroot 35725Sroot #include "dmc.h" 45725Sroot #if NDMC > 0 55725Sroot #define printd if(dmcdebug)printf 65725Sroot int dmcdebug = 1; 75725Sroot /* 85725Sroot * DMC11 device driver, internet version 95725Sroot * 105725Sroot * TODO 115725Sroot * allow more than one outstanding read or write. 125725Sroot */ 135725Sroot 145725Sroot #include "../h/param.h" 155725Sroot #include "../h/systm.h" 165725Sroot #include "../h/mbuf.h" 175725Sroot #include "../h/pte.h" 185725Sroot #include "../h/buf.h" 195725Sroot #include "../h/tty.h" 205725Sroot #include "../h/protosw.h" 215725Sroot #include "../h/socket.h" 225725Sroot #include "../h/ubareg.h" 235725Sroot #include "../h/ubavar.h" 245725Sroot #include "../h/cpu.h" 255725Sroot #include "../h/mtpr.h" 265725Sroot #include "../h/vmmac.h" 275725Sroot #include "../net/in.h" 285725Sroot #include "../net/in_systm.h" 295725Sroot #include "../net/if.h" 305725Sroot #include "../net/if_uba.h" 315853Sroot #include "../net/if_dmc.h" 325725Sroot #include "../net/ip.h" 335725Sroot #include "../net/ip_var.h" 346363Ssam #include "../net/route.h" 356502Ssam #include <errno.h> 365725Sroot 375725Sroot /* 385725Sroot * Driver information for auto-configuration stuff. 395725Sroot */ 405725Sroot int dmcprobe(), dmcattach(), dmcinit(), dmcoutput(), dmcreset(); 415725Sroot struct uba_device *dmcinfo[NDMC]; 425725Sroot u_short dmcstd[] = { 0 }; 435725Sroot struct uba_driver dmcdriver = 445725Sroot { dmcprobe, 0, dmcattach, 0, dmcstd, "dmc", dmcinfo }; 455725Sroot 466334Ssam #define DMC_AF 0xff /* 8 bits of address type in ui_flags */ 475725Sroot #define DMC_NET 0xff00 /* 8 bits of net number in ui_flags */ 485725Sroot 495725Sroot /* 505725Sroot * DMC software status per interface. 515725Sroot * 525725Sroot * Each interface is referenced by a network interface structure, 535725Sroot * sc_if, which the routing code uses to locate the interface. 545725Sroot * This structure contains the output queue for the interface, its address, ... 555725Sroot * We also have, for each interface, a UBA interface structure, which 565725Sroot * contains information about the UNIBUS resources held by the interface: 575725Sroot * map registers, buffered data paths, etc. Information is cached in this 585725Sroot * structure for use by the if_uba.c routines in running the interface 595725Sroot * efficiently. 605725Sroot */ 615725Sroot struct dmc_softc { 625725Sroot struct ifnet sc_if; /* network-visible interface */ 635725Sroot struct ifuba sc_ifuba; /* UNIBUS resources */ 645725Sroot short sc_flag; /* flags */ 655725Sroot short sc_oactive; /* output active */ 665725Sroot int sc_ubinfo; /* UBA mapping info for base table */ 675725Sroot struct clist sc_que; /* command queue */ 685725Sroot } dmc_softc[NDMC]; 695725Sroot 705725Sroot /* flags */ 715725Sroot #define DMCRUN 01 725725Sroot #define DMCBMAPPED 02 /* base table mapped */ 735725Sroot 745725Sroot struct dmc_base { 755725Sroot short d_base[128]; /* DMC base table */ 765725Sroot } dmc_base[NDMC]; 775725Sroot 785725Sroot #define loword(x) ((short *)&x)[0] 795725Sroot #define hiword(x) ((short *)&x)[1] 805725Sroot 815725Sroot dmcprobe(reg) 825725Sroot caddr_t reg; 835725Sroot { 845725Sroot register int br, cvec; 855725Sroot register struct dmcdevice *addr = (struct dmcdevice *)reg; 865725Sroot register int i; 875725Sroot 886363Ssam COUNT(DMCPROBE); 895725Sroot #ifdef lint 905725Sroot br = 0; cvec = br; br = cvec; 915725Sroot dmcrint(0); dmcxint(0); 925725Sroot #endif 935725Sroot addr->bsel1 = DMC_MCLR; 945725Sroot for (i = 100000; i && (addr->bsel1 & DMC_RUN) == 0; i--) 955725Sroot ; 965725Sroot if ((addr->bsel1 & DMC_RUN) == 0) 976334Ssam return (0); 985725Sroot addr->bsel1 &= ~DMC_MCLR; 995725Sroot addr->bsel0 = DMC_RQI|DMC_IEI; 1005725Sroot DELAY(100000); 1015725Sroot addr->bsel1 = DMC_MCLR; 1025725Sroot for (i = 100000; i && (addr->bsel1 & DMC_RUN) == 0; i--) 1035725Sroot ; 1046575Ssam #ifdef ECHACK 1056575Ssam br = 0x16; 1066575Ssam #endif 1076334Ssam return (1); 1085725Sroot } 1095725Sroot 1105725Sroot /* 1115725Sroot * Interface exists: make available by filling in network interface 1125725Sroot * record. System will initialize the interface when it is ready 1135725Sroot * to accept packets. 1145725Sroot */ 1155725Sroot dmcattach(ui) 1165725Sroot register struct uba_device *ui; 1175725Sroot { 1185725Sroot register struct dmc_softc *sc = &dmc_softc[ui->ui_unit]; 1196334Ssam register struct sockaddr_in *sin; 1205725Sroot 1216363Ssam COUNT(DMCATTACH); 1225725Sroot sc->sc_if.if_unit = ui->ui_unit; 1235725Sroot sc->sc_if.if_name = "dmc"; 1245725Sroot sc->sc_if.if_mtu = DMCMTU; 1255725Sroot sc->sc_if.if_net = (ui->ui_flags & DMC_NET) >> 8; 1265725Sroot sc->sc_if.if_host[0] = 17; /* random number */ 1276334Ssam sin = (struct sockaddr_in *)&sc->sc_if.if_addr; 1286587Ssam sin->sin_family = AF_INET; 1296334Ssam sin->sin_addr = if_makeaddr(sc->sc_if.if_net, sc->sc_if.if_host[0]); 1305725Sroot sc->sc_if.if_init = dmcinit; 1315725Sroot sc->sc_if.if_output = dmcoutput; 1325725Sroot sc->sc_if.if_ubareset = dmcreset; 1336587Ssam /* DON'T KNOW IF THIS WILL WORK WITH A BDP AT HIGH SPEEDS */ 1346587Ssam sc->sc_ifuba.ifu_flags = UBA_NEEDBDP | UBA_CANTWAIT; 1355725Sroot if_attach(&sc->sc_if); 1365725Sroot } 1375725Sroot 1385725Sroot /* 1395725Sroot * Reset of interface after UNIBUS reset. 1405725Sroot * If interface is on specified UBA, reset it's state. 1415725Sroot */ 1425725Sroot dmcreset(unit, uban) 1435725Sroot int unit, uban; 1445725Sroot { 1455725Sroot register struct uba_device *ui; 1465725Sroot 1476363Ssam COUNT(DMCRESET); 1485725Sroot if (unit >= NDMC || (ui = dmcinfo[unit]) == 0 || ui->ui_alive == 0 || 1495725Sroot ui->ui_ubanum != uban) 1505725Sroot return; 1515725Sroot printf(" dmc%d", unit); 1525725Sroot dmcinit(unit); 1535725Sroot } 1545725Sroot 1555725Sroot /* 1565725Sroot * Initialization of interface; reinitialize UNIBUS usage. 1575725Sroot */ 1585725Sroot dmcinit(unit) 1595725Sroot int unit; 1605725Sroot { 1615725Sroot register struct dmc_softc *sc = &dmc_softc[unit]; 1625725Sroot register struct uba_device *ui = dmcinfo[unit]; 1635725Sroot register struct dmcdevice *addr; 1645725Sroot int base; 1655725Sroot 1666363Ssam COUNT(DMCINIT); 1675725Sroot printd("dmcinit\n"); 1685725Sroot if ((sc->sc_flag&DMCBMAPPED) == 0) { 1695725Sroot sc->sc_ubinfo = uballoc(ui->ui_ubanum, 1705725Sroot (caddr_t)&dmc_base[unit], sizeof (struct dmc_base), 0); 1715725Sroot sc->sc_flag |= DMCBMAPPED; 1725725Sroot } 1735725Sroot if (if_ubainit(&sc->sc_ifuba, ui->ui_ubanum, 0, 1745768Sroot (int)btoc(DMCMTU)) == 0) { 1755725Sroot printf("dmc%d: can't initialize\n", unit); 1766334Ssam sc->sc_if.if_flags &= ~IFF_UP; 1775725Sroot return; 1785725Sroot } 1795725Sroot addr = (struct dmcdevice *)ui->ui_addr; 1805725Sroot addr->bsel2 |= DMC_IEO; 1815725Sroot base = sc->sc_ubinfo & 0x3ffff; 1825725Sroot printd(" base 0x%x\n", base); 1835725Sroot dmcload(sc, DMC_BASEI, base, (base>>2)&DMC_XMEM); 1845725Sroot dmcload(sc, DMC_CNTLI, 0, 0); 1855725Sroot base = sc->sc_ifuba.ifu_r.ifrw_info & 0x3ffff; 1865725Sroot dmcload(sc, DMC_READ, base, ((base>>2)&DMC_XMEM)|DMCMTU); 1875725Sroot printd(" first read queued, addr 0x%x\n", base); 1886334Ssam sc->sc_if.if_flags |= IFF_UP; 1896363Ssam /* set up routing table entry */ 1906363Ssam if ((sc->sc_if.if_flags & IFF_ROUTE) == 0) { 191*7150Swnj rtinit(&sc->sc_if.if_addr, &sc->sc_if.if_addr, RTF_HOST|RTF_UP); 1926363Ssam sc->sc_if.if_flags |= IFF_ROUTE; 1936363Ssam } 1945725Sroot } 1955725Sroot 1965725Sroot /* 1975725Sroot * Start output on interface. Get another datagram 1985725Sroot * to send from the interface queue and map it to 1995725Sroot * the interface before starting output. 2005725Sroot */ 2015725Sroot dmcstart(dev) 2025725Sroot dev_t dev; 2035725Sroot { 2045725Sroot int unit = minor(dev); 2055725Sroot struct uba_device *ui = dmcinfo[unit]; 2065725Sroot register struct dmc_softc *sc = &dmc_softc[unit]; 2075725Sroot int addr, len; 2085725Sroot struct mbuf *m; 2095725Sroot 2106363Ssam COUNT(DMCSTART); 2115725Sroot printd("dmcstart\n"); 2125725Sroot /* 2135725Sroot * Dequeue a request and map it to the UNIBUS. 2145725Sroot * If no more requests, just return. 2155725Sroot */ 2165725Sroot IF_DEQUEUE(&sc->sc_if.if_snd, m); 2175725Sroot if (m == 0) 2185725Sroot return; 2195725Sroot len = if_wubaput(&sc->sc_ifuba, m); 2205725Sroot 2215725Sroot /* 2225725Sroot * Have request mapped to UNIBUS for transmission. 2235725Sroot * Purge any stale data from this BDP and start the output. 2245725Sroot */ 2256587Ssam if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP) 2265853Sroot UBAPURGE(sc->sc_ifuba.ifu_uba, sc->sc_ifuba.ifu_w.ifrw_bdp); 2275725Sroot addr = sc->sc_ifuba.ifu_w.ifrw_info & 0x3ffff; 2285725Sroot printd(" len %d, addr 0x%x, ", len, addr); 2295725Sroot printd("mr 0x%x\n", sc->sc_ifuba.ifu_w.ifrw_mr[0]); 2305725Sroot dmcload(sc, DMC_WRITE, addr, (len&DMC_CCOUNT)|((addr>>2)&DMC_XMEM)); 2315725Sroot sc->sc_oactive = 1; 2325725Sroot } 2335725Sroot 2345725Sroot /* 2355725Sroot * Utility routine to load the DMC device registers. 2365725Sroot */ 2375725Sroot dmcload(sc, type, w0, w1) 2385725Sroot register struct dmc_softc *sc; 2395725Sroot int type, w0, w1; 2405725Sroot { 2415725Sroot register struct dmcdevice *addr; 2425725Sroot register int unit, sps, n; 2435725Sroot 2446363Ssam COUNT(DMCLOAD); 2455725Sroot printd("dmcload: 0x%x 0x%x 0x%x\n", type, w0, w1); 2465725Sroot unit = sc - dmc_softc; 2475725Sroot addr = (struct dmcdevice *)dmcinfo[unit]->ui_addr; 2485725Sroot sps = spl5(); 2495725Sroot if ((n = sc->sc_que.c_cc) == 0) 2505725Sroot addr->bsel0 = type | DMC_RQI; 2515725Sroot else 2526159Ssam (void) putc(type | DMC_RQI, &sc->sc_que); 2536159Ssam (void) putw(w0, &sc->sc_que); 2546159Ssam (void) putw(w1, &sc->sc_que); 2555725Sroot if (n == 0) 2565725Sroot dmcrint(unit); 2575725Sroot splx(sps); 2585725Sroot } 2595725Sroot 2605725Sroot /* 2615725Sroot * DMC interface receiver interrupt. 2625725Sroot * Ready to accept another command, 2635725Sroot * pull one off the command queue. 2645725Sroot */ 2655725Sroot dmcrint(unit) 2665725Sroot int unit; 2675725Sroot { 2685725Sroot register struct dmc_softc *sc; 2695725Sroot register struct dmcdevice *addr; 2705725Sroot register int n; 2715725Sroot 2726363Ssam COUNT(DMCRINT); 2735725Sroot addr = (struct dmcdevice *)dmcinfo[unit]->ui_addr; 2745725Sroot sc = &dmc_softc[unit]; 2755725Sroot while (addr->bsel0&DMC_RDYI) { 2765725Sroot addr->sel4 = getw(&sc->sc_que); 2775725Sroot addr->sel6 = getw(&sc->sc_que); 2785725Sroot addr->bsel0 &= ~(DMC_IEI|DMC_RQI); 2795725Sroot while (addr->bsel0&DMC_RDYI) 2805725Sroot ; 2815725Sroot if (sc->sc_que.c_cc == 0) 2825725Sroot return; 2835725Sroot addr->bsel0 = getc(&sc->sc_que); 2845725Sroot n = RDYSCAN; 2855725Sroot while (n-- && (addr->bsel0&DMC_RDYI) == 0) 2865725Sroot ; 2875725Sroot } 2885725Sroot if (sc->sc_que.c_cc) 2895725Sroot addr->bsel0 |= DMC_IEI; 2905725Sroot } 2915725Sroot 2925725Sroot /* 2935725Sroot * DMC interface transmitter interrupt. 2945725Sroot * A transfer has completed, check for errors. 2955725Sroot * If it was a read, notify appropriate protocol. 2965725Sroot * If it was a write, pull the next one off the queue. 2975725Sroot */ 2985725Sroot dmcxint(unit) 2995725Sroot int unit; 3005725Sroot { 3015725Sroot register struct dmc_softc *sc; 3025725Sroot struct uba_device *ui = dmcinfo[unit]; 3035725Sroot struct dmcdevice *addr; 3045725Sroot struct mbuf *m; 3055725Sroot register struct ifqueue *inq; 3065725Sroot int arg, cmd, len; 3075725Sroot 3086363Ssam COUNT(DMCXINT); 3095725Sroot addr = (struct dmcdevice *)ui->ui_addr; 3105725Sroot arg = addr->sel6; 3115725Sroot cmd = addr->bsel2&7; 3125725Sroot addr->bsel2 &= ~DMC_RDYO; 3135725Sroot sc = &dmc_softc[unit]; 3145725Sroot printd("dmcxint\n"); 3155725Sroot switch (cmd) { 3165725Sroot 3175725Sroot case DMC_OUR: 3185725Sroot /* 3195725Sroot * A read has completed. Purge input buffered 3205725Sroot * data path. Pass packet to type specific 3215725Sroot * higher-level input routine. 3225725Sroot */ 3235725Sroot sc->sc_if.if_ipackets++; 3246587Ssam if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP) 3255853Sroot UBAPURGE(sc->sc_ifuba.ifu_uba, 3265853Sroot sc->sc_ifuba.ifu_r.ifrw_bdp); 3275725Sroot len = arg & DMC_CCOUNT; 3285725Sroot printd(" read done, len %d\n", len); 3296334Ssam switch (ui->ui_flags & DMC_AF) { 3305725Sroot #ifdef INET 3316334Ssam case AF_INET: 3326260Swnj schednetisr(NETISR_IP); 3335725Sroot inq = &ipintrq; 3345725Sroot break; 3355725Sroot #endif 3365725Sroot 3375725Sroot default: 3386334Ssam printf("dmc%d: unknown address type %d\n", unit, 3396334Ssam ui->ui_flags & DMC_AF); 3405725Sroot goto setup; 3415725Sroot } 3425725Sroot m = if_rubaget(&sc->sc_ifuba, len, 0); 3435725Sroot if (m == 0) 3445725Sroot goto setup; 3456207Swnj if (IF_QFULL(inq)) { 3466207Swnj IF_DROP(inq); 3476207Swnj (void) m_freem(m); 3486207Swnj } else 3496207Swnj IF_ENQUEUE(inq, m); 3505725Sroot 3515725Sroot setup: 3525725Sroot arg = sc->sc_ifuba.ifu_r.ifrw_info & 0x3ffff; 3535725Sroot dmcload(sc, DMC_READ, arg, ((arg >> 2) & DMC_XMEM) | DMCMTU); 3545725Sroot return; 3555725Sroot 3565725Sroot case DMC_OUX: 3575725Sroot /* 3585725Sroot * A write has completed, start another 3595725Sroot * transfer if there is more data to send. 3605725Sroot */ 3615725Sroot if (sc->sc_oactive == 0) 3625725Sroot return; /* SHOULD IT BE A FATAL ERROR? */ 3635725Sroot printd(" write done\n"); 3645725Sroot sc->sc_if.if_opackets++; 3655725Sroot sc->sc_oactive = 0; 3665725Sroot if (sc->sc_ifuba.ifu_xtofree) { 3676207Swnj (void) m_freem(sc->sc_ifuba.ifu_xtofree); 3685725Sroot sc->sc_ifuba.ifu_xtofree = 0; 3695725Sroot } 3705725Sroot if (sc->sc_if.if_snd.ifq_head == 0) 3715725Sroot return; 3725725Sroot dmcstart(unit); 3735725Sroot return; 3745725Sroot 3755725Sroot case DMC_CNTLO: 3765725Sroot arg &= DMC_CNTMASK; 3775725Sroot if (arg&DMC_FATAL) { 3785725Sroot addr->bsel1 = DMC_MCLR; 3795725Sroot sc->sc_flag &= ~DMCRUN; 3805725Sroot /*** DO SOMETHING TO RESTART DEVICE ***/ 3815725Sroot printf("DMC FATAL ERROR 0%o\n", arg); 3825725Sroot } else { 3835725Sroot /* ACCUMULATE STATISTICS */ 3845725Sroot printf("DMC SOFT ERROR 0%o\n", arg); 3855725Sroot } 3865725Sroot return; 3875725Sroot 3885725Sroot default: 3895725Sroot printf("dmc%d: bad control %o\n", unit, cmd); 3905725Sroot } 3915725Sroot } 3925725Sroot 3935725Sroot /* 3945725Sroot * DMC output routine. 3955725Sroot * Just send the data, header was supplied by 3965725Sroot * upper level protocol routines. 3975725Sroot */ 3986334Ssam dmcoutput(ifp, m, dst) 3995725Sroot register struct ifnet *ifp; 4005725Sroot register struct mbuf *m; 4016334Ssam struct sockaddr *dst; 4025725Sroot { 4035725Sroot struct uba_device *ui = dmcinfo[ifp->if_unit]; 4045725Sroot int s; 4055725Sroot 4066363Ssam COUNT(DMCOUTPUT); 4075725Sroot printd("dmcoutput\n"); 4086334Ssam if (dst->sa_family != (ui->ui_flags & DMC_AF)) { 4096334Ssam printf("dmc%d: af%d not supported\n", ifp->if_unit, pf); 4106334Ssam m_freem(m); 4116502Ssam return (EAFNOSUPPORT); 4125725Sroot } 4135725Sroot s = splimp(); 4146207Swnj if (IF_QFULL(&ifp->if_snd)) { 4156207Swnj IF_DROP(&ifp->if_snd); 4166334Ssam m_freem(m); 4176207Swnj splx(s); 4186502Ssam return (ENOBUFS); 4196207Swnj } 4205725Sroot IF_ENQUEUE(&ifp->if_snd, m); 4215725Sroot if (dmc_softc[ifp->if_unit].sc_oactive == 0) 4225725Sroot dmcstart(ifp->if_unit); 4235725Sroot splx(s); 4246502Ssam return (0); 4255725Sroot } 426