1*8416Swnj /* if_dmc.c 4.18 82/10/09 */ 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" 27*8416Swnj #include "../netinet/in.h" 28*8416Swnj #include "../netinet/in_systm.h" 295725Sroot #include "../net/if.h" 30*8416Swnj #include "../vaxif/if_uba.h" 31*8416Swnj #include "../vaxif/if_dmc.h" 326363Ssam #include "../net/route.h" 336502Ssam #include <errno.h> 345725Sroot 355725Sroot /* 365725Sroot * Driver information for auto-configuration stuff. 375725Sroot */ 385725Sroot int dmcprobe(), dmcattach(), dmcinit(), dmcoutput(), dmcreset(); 395725Sroot struct uba_device *dmcinfo[NDMC]; 405725Sroot u_short dmcstd[] = { 0 }; 415725Sroot struct uba_driver dmcdriver = 425725Sroot { dmcprobe, 0, dmcattach, 0, dmcstd, "dmc", dmcinfo }; 435725Sroot 446334Ssam #define DMC_AF 0xff /* 8 bits of address type in ui_flags */ 457161Ssam #define DMC_NET 0xffffff00 /* 24 bits of net number in ui_flags */ 465725Sroot 475725Sroot /* 485725Sroot * DMC software status per interface. 495725Sroot * 505725Sroot * Each interface is referenced by a network interface structure, 515725Sroot * sc_if, which the routing code uses to locate the interface. 525725Sroot * This structure contains the output queue for the interface, its address, ... 535725Sroot * We also have, for each interface, a UBA interface structure, which 545725Sroot * contains information about the UNIBUS resources held by the interface: 555725Sroot * map registers, buffered data paths, etc. Information is cached in this 565725Sroot * structure for use by the if_uba.c routines in running the interface 575725Sroot * efficiently. 585725Sroot */ 595725Sroot struct dmc_softc { 605725Sroot struct ifnet sc_if; /* network-visible interface */ 615725Sroot struct ifuba sc_ifuba; /* UNIBUS resources */ 625725Sroot short sc_flag; /* flags */ 635725Sroot short sc_oactive; /* output active */ 645725Sroot int sc_ubinfo; /* UBA mapping info for base table */ 655725Sroot struct clist sc_que; /* command queue */ 665725Sroot } dmc_softc[NDMC]; 675725Sroot 685725Sroot /* flags */ 695725Sroot #define DMCRUN 01 705725Sroot #define DMCBMAPPED 02 /* base table mapped */ 715725Sroot 725725Sroot struct dmc_base { 735725Sroot short d_base[128]; /* DMC base table */ 745725Sroot } dmc_base[NDMC]; 755725Sroot 765725Sroot #define loword(x) ((short *)&x)[0] 775725Sroot #define hiword(x) ((short *)&x)[1] 785725Sroot 795725Sroot dmcprobe(reg) 805725Sroot caddr_t reg; 815725Sroot { 825725Sroot register int br, cvec; 835725Sroot register struct dmcdevice *addr = (struct dmcdevice *)reg; 845725Sroot register int i; 855725Sroot 865725Sroot #ifdef lint 875725Sroot br = 0; cvec = br; br = cvec; 885725Sroot dmcrint(0); dmcxint(0); 895725Sroot #endif 905725Sroot addr->bsel1 = DMC_MCLR; 915725Sroot for (i = 100000; i && (addr->bsel1 & DMC_RUN) == 0; i--) 925725Sroot ; 935725Sroot if ((addr->bsel1 & DMC_RUN) == 0) 946334Ssam return (0); 955725Sroot addr->bsel1 &= ~DMC_MCLR; 965725Sroot addr->bsel0 = DMC_RQI|DMC_IEI; 975725Sroot DELAY(100000); 985725Sroot addr->bsel1 = DMC_MCLR; 995725Sroot for (i = 100000; i && (addr->bsel1 & DMC_RUN) == 0; i--) 1005725Sroot ; 1016575Ssam #ifdef ECHACK 1026575Ssam br = 0x16; 1036575Ssam #endif 1046334Ssam return (1); 1055725Sroot } 1065725Sroot 1075725Sroot /* 1085725Sroot * Interface exists: make available by filling in network interface 1095725Sroot * record. System will initialize the interface when it is ready 1105725Sroot * to accept packets. 1115725Sroot */ 1125725Sroot dmcattach(ui) 1135725Sroot register struct uba_device *ui; 1145725Sroot { 1155725Sroot register struct dmc_softc *sc = &dmc_softc[ui->ui_unit]; 1166334Ssam register struct sockaddr_in *sin; 1175725Sroot 1185725Sroot sc->sc_if.if_unit = ui->ui_unit; 1195725Sroot sc->sc_if.if_name = "dmc"; 1205725Sroot sc->sc_if.if_mtu = DMCMTU; 1215725Sroot sc->sc_if.if_net = (ui->ui_flags & DMC_NET) >> 8; 1225725Sroot sc->sc_if.if_host[0] = 17; /* random number */ 1236334Ssam sin = (struct sockaddr_in *)&sc->sc_if.if_addr; 1246587Ssam sin->sin_family = AF_INET; 1256334Ssam sin->sin_addr = if_makeaddr(sc->sc_if.if_net, sc->sc_if.if_host[0]); 1265725Sroot sc->sc_if.if_init = dmcinit; 1275725Sroot sc->sc_if.if_output = dmcoutput; 1285725Sroot sc->sc_if.if_ubareset = dmcreset; 1296587Ssam /* DON'T KNOW IF THIS WILL WORK WITH A BDP AT HIGH SPEEDS */ 1306587Ssam sc->sc_ifuba.ifu_flags = UBA_NEEDBDP | UBA_CANTWAIT; 1315725Sroot if_attach(&sc->sc_if); 1325725Sroot } 1335725Sroot 1345725Sroot /* 1355725Sroot * Reset of interface after UNIBUS reset. 1365725Sroot * If interface is on specified UBA, reset it's state. 1375725Sroot */ 1385725Sroot dmcreset(unit, uban) 1395725Sroot int unit, uban; 1405725Sroot { 1415725Sroot register struct uba_device *ui; 1425725Sroot 1435725Sroot if (unit >= NDMC || (ui = dmcinfo[unit]) == 0 || ui->ui_alive == 0 || 1445725Sroot ui->ui_ubanum != uban) 1455725Sroot return; 1465725Sroot printf(" dmc%d", unit); 1475725Sroot dmcinit(unit); 1485725Sroot } 1495725Sroot 1505725Sroot /* 1515725Sroot * Initialization of interface; reinitialize UNIBUS usage. 1525725Sroot */ 1535725Sroot dmcinit(unit) 1545725Sroot int unit; 1555725Sroot { 1565725Sroot register struct dmc_softc *sc = &dmc_softc[unit]; 1575725Sroot register struct uba_device *ui = dmcinfo[unit]; 1585725Sroot register struct dmcdevice *addr; 1595725Sroot int base; 1605725Sroot 1615725Sroot printd("dmcinit\n"); 1625725Sroot if ((sc->sc_flag&DMCBMAPPED) == 0) { 1635725Sroot sc->sc_ubinfo = uballoc(ui->ui_ubanum, 1645725Sroot (caddr_t)&dmc_base[unit], sizeof (struct dmc_base), 0); 1655725Sroot sc->sc_flag |= DMCBMAPPED; 1665725Sroot } 1675725Sroot if (if_ubainit(&sc->sc_ifuba, ui->ui_ubanum, 0, 1685768Sroot (int)btoc(DMCMTU)) == 0) { 1695725Sroot printf("dmc%d: can't initialize\n", unit); 1706334Ssam sc->sc_if.if_flags &= ~IFF_UP; 1715725Sroot return; 1725725Sroot } 1735725Sroot addr = (struct dmcdevice *)ui->ui_addr; 1745725Sroot addr->bsel2 |= DMC_IEO; 1755725Sroot base = sc->sc_ubinfo & 0x3ffff; 1765725Sroot printd(" base 0x%x\n", base); 1775725Sroot dmcload(sc, DMC_BASEI, base, (base>>2)&DMC_XMEM); 1785725Sroot dmcload(sc, DMC_CNTLI, 0, 0); 1795725Sroot base = sc->sc_ifuba.ifu_r.ifrw_info & 0x3ffff; 1805725Sroot dmcload(sc, DMC_READ, base, ((base>>2)&DMC_XMEM)|DMCMTU); 1815725Sroot printd(" first read queued, addr 0x%x\n", base); 1826334Ssam sc->sc_if.if_flags |= IFF_UP; 1836363Ssam /* set up routing table entry */ 1846363Ssam if ((sc->sc_if.if_flags & IFF_ROUTE) == 0) { 1857150Swnj rtinit(&sc->sc_if.if_addr, &sc->sc_if.if_addr, RTF_HOST|RTF_UP); 1866363Ssam sc->sc_if.if_flags |= IFF_ROUTE; 1876363Ssam } 1885725Sroot } 1895725Sroot 1905725Sroot /* 1915725Sroot * Start output on interface. Get another datagram 1925725Sroot * to send from the interface queue and map it to 1935725Sroot * the interface before starting output. 1945725Sroot */ 1955725Sroot dmcstart(dev) 1965725Sroot dev_t dev; 1975725Sroot { 1985725Sroot int unit = minor(dev); 1995725Sroot struct uba_device *ui = dmcinfo[unit]; 2005725Sroot register struct dmc_softc *sc = &dmc_softc[unit]; 2015725Sroot int addr, len; 2025725Sroot struct mbuf *m; 2035725Sroot 2045725Sroot printd("dmcstart\n"); 2055725Sroot /* 2065725Sroot * Dequeue a request and map it to the UNIBUS. 2075725Sroot * If no more requests, just return. 2085725Sroot */ 2095725Sroot IF_DEQUEUE(&sc->sc_if.if_snd, m); 2105725Sroot if (m == 0) 2115725Sroot return; 2125725Sroot len = if_wubaput(&sc->sc_ifuba, m); 2135725Sroot 2145725Sroot /* 2155725Sroot * Have request mapped to UNIBUS for transmission. 2165725Sroot * Purge any stale data from this BDP and start the output. 2175725Sroot */ 2186587Ssam if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP) 2195853Sroot UBAPURGE(sc->sc_ifuba.ifu_uba, sc->sc_ifuba.ifu_w.ifrw_bdp); 2205725Sroot addr = sc->sc_ifuba.ifu_w.ifrw_info & 0x3ffff; 2215725Sroot printd(" len %d, addr 0x%x, ", len, addr); 2225725Sroot printd("mr 0x%x\n", sc->sc_ifuba.ifu_w.ifrw_mr[0]); 2235725Sroot dmcload(sc, DMC_WRITE, addr, (len&DMC_CCOUNT)|((addr>>2)&DMC_XMEM)); 2245725Sroot sc->sc_oactive = 1; 2255725Sroot } 2265725Sroot 2275725Sroot /* 2285725Sroot * Utility routine to load the DMC device registers. 2295725Sroot */ 2305725Sroot dmcload(sc, type, w0, w1) 2315725Sroot register struct dmc_softc *sc; 2325725Sroot int type, w0, w1; 2335725Sroot { 2345725Sroot register struct dmcdevice *addr; 2355725Sroot register int unit, sps, n; 2365725Sroot 2375725Sroot printd("dmcload: 0x%x 0x%x 0x%x\n", type, w0, w1); 2385725Sroot unit = sc - dmc_softc; 2395725Sroot addr = (struct dmcdevice *)dmcinfo[unit]->ui_addr; 2405725Sroot sps = spl5(); 2415725Sroot if ((n = sc->sc_que.c_cc) == 0) 2425725Sroot addr->bsel0 = type | DMC_RQI; 2435725Sroot else 2446159Ssam (void) putc(type | DMC_RQI, &sc->sc_que); 2456159Ssam (void) putw(w0, &sc->sc_que); 2466159Ssam (void) putw(w1, &sc->sc_que); 2475725Sroot if (n == 0) 2485725Sroot dmcrint(unit); 2495725Sroot splx(sps); 2505725Sroot } 2515725Sroot 2525725Sroot /* 2535725Sroot * DMC interface receiver interrupt. 2545725Sroot * Ready to accept another command, 2555725Sroot * pull one off the command queue. 2565725Sroot */ 2575725Sroot dmcrint(unit) 2585725Sroot int unit; 2595725Sroot { 2605725Sroot register struct dmc_softc *sc; 2615725Sroot register struct dmcdevice *addr; 2625725Sroot register int n; 2635725Sroot 2645725Sroot addr = (struct dmcdevice *)dmcinfo[unit]->ui_addr; 2655725Sroot sc = &dmc_softc[unit]; 2665725Sroot while (addr->bsel0&DMC_RDYI) { 2675725Sroot addr->sel4 = getw(&sc->sc_que); 2685725Sroot addr->sel6 = getw(&sc->sc_que); 2695725Sroot addr->bsel0 &= ~(DMC_IEI|DMC_RQI); 2705725Sroot while (addr->bsel0&DMC_RDYI) 2715725Sroot ; 2725725Sroot if (sc->sc_que.c_cc == 0) 2735725Sroot return; 2745725Sroot addr->bsel0 = getc(&sc->sc_que); 2755725Sroot n = RDYSCAN; 2765725Sroot while (n-- && (addr->bsel0&DMC_RDYI) == 0) 2775725Sroot ; 2785725Sroot } 2795725Sroot if (sc->sc_que.c_cc) 2805725Sroot addr->bsel0 |= DMC_IEI; 2815725Sroot } 2825725Sroot 2835725Sroot /* 2845725Sroot * DMC interface transmitter interrupt. 2855725Sroot * A transfer has completed, check for errors. 2865725Sroot * If it was a read, notify appropriate protocol. 2875725Sroot * If it was a write, pull the next one off the queue. 2885725Sroot */ 2895725Sroot dmcxint(unit) 2905725Sroot int unit; 2915725Sroot { 2925725Sroot register struct dmc_softc *sc; 2935725Sroot struct uba_device *ui = dmcinfo[unit]; 2945725Sroot struct dmcdevice *addr; 2955725Sroot struct mbuf *m; 2965725Sroot register struct ifqueue *inq; 2975725Sroot int arg, cmd, len; 2985725Sroot 2995725Sroot addr = (struct dmcdevice *)ui->ui_addr; 3005725Sroot arg = addr->sel6; 3015725Sroot cmd = addr->bsel2&7; 3025725Sroot addr->bsel2 &= ~DMC_RDYO; 3035725Sroot sc = &dmc_softc[unit]; 3045725Sroot printd("dmcxint\n"); 3055725Sroot switch (cmd) { 3065725Sroot 3075725Sroot case DMC_OUR: 3085725Sroot /* 3095725Sroot * A read has completed. Purge input buffered 3105725Sroot * data path. Pass packet to type specific 3115725Sroot * higher-level input routine. 3125725Sroot */ 3135725Sroot sc->sc_if.if_ipackets++; 3146587Ssam if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP) 3155853Sroot UBAPURGE(sc->sc_ifuba.ifu_uba, 3165853Sroot sc->sc_ifuba.ifu_r.ifrw_bdp); 3175725Sroot len = arg & DMC_CCOUNT; 3185725Sroot printd(" read done, len %d\n", len); 3196334Ssam switch (ui->ui_flags & DMC_AF) { 3205725Sroot #ifdef INET 3216334Ssam case AF_INET: 3226260Swnj schednetisr(NETISR_IP); 3235725Sroot inq = &ipintrq; 3245725Sroot break; 3255725Sroot #endif 3265725Sroot 3275725Sroot default: 3286334Ssam printf("dmc%d: unknown address type %d\n", unit, 3296334Ssam ui->ui_flags & DMC_AF); 3305725Sroot goto setup; 3315725Sroot } 3325725Sroot m = if_rubaget(&sc->sc_ifuba, len, 0); 3335725Sroot if (m == 0) 3345725Sroot goto setup; 3356207Swnj if (IF_QFULL(inq)) { 3366207Swnj IF_DROP(inq); 3376207Swnj (void) m_freem(m); 3386207Swnj } else 3396207Swnj IF_ENQUEUE(inq, m); 3405725Sroot 3415725Sroot setup: 3425725Sroot arg = sc->sc_ifuba.ifu_r.ifrw_info & 0x3ffff; 3435725Sroot dmcload(sc, DMC_READ, arg, ((arg >> 2) & DMC_XMEM) | DMCMTU); 3445725Sroot return; 3455725Sroot 3465725Sroot case DMC_OUX: 3475725Sroot /* 3485725Sroot * A write has completed, start another 3495725Sroot * transfer if there is more data to send. 3505725Sroot */ 3515725Sroot if (sc->sc_oactive == 0) 3525725Sroot return; /* SHOULD IT BE A FATAL ERROR? */ 3535725Sroot printd(" write done\n"); 3545725Sroot sc->sc_if.if_opackets++; 3555725Sroot sc->sc_oactive = 0; 3565725Sroot if (sc->sc_ifuba.ifu_xtofree) { 3576207Swnj (void) m_freem(sc->sc_ifuba.ifu_xtofree); 3585725Sroot sc->sc_ifuba.ifu_xtofree = 0; 3595725Sroot } 3605725Sroot if (sc->sc_if.if_snd.ifq_head == 0) 3615725Sroot return; 3625725Sroot dmcstart(unit); 3635725Sroot return; 3645725Sroot 3655725Sroot case DMC_CNTLO: 3665725Sroot arg &= DMC_CNTMASK; 3675725Sroot if (arg&DMC_FATAL) { 3685725Sroot addr->bsel1 = DMC_MCLR; 3695725Sroot sc->sc_flag &= ~DMCRUN; 3705725Sroot /*** DO SOMETHING TO RESTART DEVICE ***/ 3715725Sroot printf("DMC FATAL ERROR 0%o\n", arg); 3725725Sroot } else { 3735725Sroot /* ACCUMULATE STATISTICS */ 3745725Sroot printf("DMC SOFT ERROR 0%o\n", arg); 3755725Sroot } 3765725Sroot return; 3775725Sroot 3785725Sroot default: 3795725Sroot printf("dmc%d: bad control %o\n", unit, cmd); 3805725Sroot } 3815725Sroot } 3825725Sroot 3835725Sroot /* 3845725Sroot * DMC output routine. 3855725Sroot * Just send the data, header was supplied by 3865725Sroot * upper level protocol routines. 3875725Sroot */ 3886334Ssam dmcoutput(ifp, m, dst) 3895725Sroot register struct ifnet *ifp; 3905725Sroot register struct mbuf *m; 3916334Ssam struct sockaddr *dst; 3925725Sroot { 3935725Sroot struct uba_device *ui = dmcinfo[ifp->if_unit]; 3945725Sroot int s; 3955725Sroot 3965725Sroot printd("dmcoutput\n"); 3976334Ssam if (dst->sa_family != (ui->ui_flags & DMC_AF)) { 3986334Ssam printf("dmc%d: af%d not supported\n", ifp->if_unit, pf); 3996334Ssam m_freem(m); 4006502Ssam return (EAFNOSUPPORT); 4015725Sroot } 4025725Sroot s = splimp(); 4036207Swnj if (IF_QFULL(&ifp->if_snd)) { 4046207Swnj IF_DROP(&ifp->if_snd); 4056334Ssam m_freem(m); 4066207Swnj splx(s); 4076502Ssam return (ENOBUFS); 4086207Swnj } 4095725Sroot IF_ENQUEUE(&ifp->if_snd, m); 4105725Sroot if (dmc_softc[ifp->if_unit].sc_oactive == 0) 4115725Sroot dmcstart(ifp->if_unit); 4125725Sroot splx(s); 4136502Ssam return (0); 4145725Sroot } 415