1*12363Ssam /* if_vv.c 4.19 83/05/10 */ 27024Ssam 39799Ssam #include "vv.h" 411192Ssam 57024Ssam /* 67024Ssam * Proteon 10 Meg Ring Driver. 77024Ssam * This device is called "vv" because its "real name", 87024Ssam * V2LNI won't work if shortened to the obvious "v2". 97024Ssam * Hence the subterfuge. 1011192Ssam * 117024Ssam */ 1211209Ssam #include "../machine/pte.h" 139799Ssam 147024Ssam #include "../h/param.h" 157024Ssam #include "../h/systm.h" 167024Ssam #include "../h/mbuf.h" 177024Ssam #include "../h/buf.h" 187024Ssam #include "../h/protosw.h" 197024Ssam #include "../h/socket.h" 207024Ssam #include "../h/vmmac.h" 2111192Ssam #include "../h/errno.h" 2211209Ssam #include "../h/time.h" 2311209Ssam #include "../h/kernel.h" 248465Sroot 258465Sroot #include "../net/if.h" 2611209Ssam #include "../net/netisr.h" 278465Sroot #include "../net/route.h" 2811192Ssam 298421Swnj #include "../netinet/in.h" 308421Swnj #include "../netinet/in_systm.h" 318421Swnj #include "../netinet/ip.h" 328421Swnj #include "../netinet/ip_var.h" 338465Sroot 3411209Ssam #include "../vax/mtpr.h" 3511209Ssam #include "../vax/cpu.h" 3611192Ssam 378465Sroot #include "../vaxuba/ubareg.h" 388465Sroot #include "../vaxuba/ubavar.h" 397024Ssam 4011209Ssam #include "../vaxif/if_vv.h" 4111209Ssam #include "../vaxif/if_uba.h" 4211209Ssam 437024Ssam /* 447024Ssam * N.B. - if WIRECENTER is defined wrong, it can well break 457024Ssam * the hardware!! 467024Ssam */ 477024Ssam #define WIRECENTER 487024Ssam 497024Ssam #ifdef WIRECENTER 507024Ssam #define VV_CONF VV_HEN /* drive wire center relay */ 517024Ssam #else 527024Ssam #define VV_CONF VV_STE /* allow operation without wire center */ 537024Ssam #endif 547024Ssam 557024Ssam #define VVMTU (1024+512) 567640Ssam #define VVMRU (1024+512+16) /* space for trailer */ 577024Ssam 5811192Ssam int vv_dotrailer = 0, /* 1 => do trailer protocol */ 5911192Ssam vv_tracehdr = 0, /* 1 => trace headers (slowly!!) */ 6011192Ssam vv_tracetimeout = 1; /* 1 => trace input error-rate limiting */ 6111192Ssam vv_logreaderrors = 0; /* 1 => log all read errors */ 627640Ssam 6311192Ssam #define vvtracehdr if (vv_tracehdr) vvprt_hdr 6411192Ssam #define vvtrprintf if (vv_tracetimeout) printf 6511192Ssam 6611192Ssam int vv_ticking = 0; /* error flywheel is running */ 6711192Ssam 6811209Ssam /* 6911209Ssam * Interval in HZ - 50 msec. 7011209Ssam * N.B. all times below are in units of flywheel ticks 7111209Ssam */ 7211209Ssam #define VV_FLYWHEEL 3 7311192Ssam #define VV_ERRORTHRESHOLD 100 /* errors/flywheel-interval */ 7411192Ssam #define VV_MODE1ATTEMPTS 10 /* number mode 1 retries */ 7511192Ssam #define VV_MODE1DELAY 2 /* period interface is PAUSEd - 100ms */ 7611192Ssam #define VV_MODE2DELAY 4 /* base interval host relay is off - 200ms */ 7711192Ssam #define VV_MAXDELAY 6400 /* max interval host relay is off - 2 minutes */ 7811192Ssam 797024Ssam int vvprobe(), vvattach(), vvrint(), vvxint(); 807024Ssam struct uba_device *vvinfo[NVV]; 817024Ssam u_short vvstd[] = { 0 }; 827024Ssam struct uba_driver vvdriver = 837024Ssam { vvprobe, 0, vvattach, 0, vvstd, "vv", vvinfo }; 847024Ssam #define VVUNIT(x) minor(x) 857024Ssam int vvinit(),vvoutput(),vvreset(); 867024Ssam 877024Ssam /* 887024Ssam * Software status of each interface. 897024Ssam * 907024Ssam * Each interface is referenced by a network interface structure, 917024Ssam * vs_if, which the routing code uses to locate the interface. 927024Ssam * This structure contains the output queue for the interface, its address, ... 937024Ssam * We also have, for each interface, a UBA interface structure, which 947024Ssam * contains information about the UNIBUS resources held by the interface: 957024Ssam * map registers, buffered data paths, etc. Information is cached in this 967024Ssam * structure for use by the if_uba.c routines in running the interface 977024Ssam * efficiently. 987024Ssam */ 997024Ssam struct vv_softc { 1007024Ssam struct ifnet vs_if; /* network-visible interface */ 1017024Ssam struct ifuba vs_ifuba; /* UNIBUS resources */ 10211192Ssam short vs_oactive; /* is output active */ 10311192Ssam short vs_iactive; /* is input active */ 1047024Ssam short vs_olen; /* length of last output */ 1057024Ssam u_short vs_lastx; /* last destination address */ 10611192Ssam short vs_tries; /* transmit current retry count */ 1077024Ssam short vs_init; /* number of ring inits */ 1087024Ssam short vs_nottaken; /* number of packets refused */ 10911192Ssam /* input error rate limiting state */ 11011192Ssam short vs_major; /* recovery major state */ 11111192Ssam short vs_minor; /* recovery minor state */ 11211192Ssam short vs_retry; /* recovery retry count */ 11311192Ssam short vs_delayclock; /* recovery delay clock */ 11411192Ssam short vs_delayrange; /* increasing delay interval */ 11511192Ssam short vs_dropped; /* number of packes tossed in last dt */ 1167024Ssam } vv_softc[NVV]; 1177024Ssam 11811192Ssam /* 11911209Ssam * States of vs_iactive. 12011192Ssam */ 12111192Ssam #define ACTIVE 1 /* interface should post new receives */ 12211192Ssam #define PAUSE 0 /* interface should NOT post new receives */ 12311192Ssam #define OPEN -1 /* PAUSE and open host relay */ 12411192Ssam 12511192Ssam /* 12611209Ssam * Recovery major states. 12711192Ssam */ 12811192Ssam #define MODE0 0 /* everything is wonderful */ 12911192Ssam #define MODE1 1 /* hopefully whatever will go away */ 13011209Ssam #define MODE2 2 /* drastic measures - open host relay for increasing intervals */ 13111192Ssam 1327024Ssam vvprobe(reg) 1337024Ssam caddr_t reg; 1347024Ssam { 1357024Ssam register int br, cvec; 1367024Ssam register struct vvreg *addr = (struct vvreg *)reg; 1377024Ssam 1387024Ssam #ifdef lint 1397024Ssam br = 0; cvec = br; br = cvec; 1407024Ssam #endif 1417024Ssam /* reset interface, enable, and wait till dust settles */ 1427024Ssam addr->vvicsr = VV_RST; 1437024Ssam addr->vvocsr = VV_RST; 14412351Smo DELAY(10000); 1457024Ssam /* generate interrupt by doing 1 word DMA from 0 in uba space!! */ 1467024Ssam addr->vvocsr = VV_IEN; /* enable interrupt */ 1477024Ssam addr->vvoba = 0; /* low 16 bits */ 1487024Ssam addr->vvoea = 0; /* extended bits */ 1497024Ssam addr->vvowc = -1; /* for 1 word */ 1507024Ssam addr->vvocsr |= VV_DEN; /* start the DMA */ 1517024Ssam DELAY(100000); 1527024Ssam addr->vvocsr = 0; 1537024Ssam if (cvec && cvec != 0x200) 1547024Ssam cvec -= 4; /* backup so vector => recieve */ 1557024Ssam return(1); 1567024Ssam } 1577024Ssam 1587024Ssam /* 1597024Ssam * Interface exists: make available by filling in network interface 1607024Ssam * record. System will initialize the interface when it is ready 1617024Ssam * to accept packets. 1627024Ssam */ 1637024Ssam vvattach(ui) 1647024Ssam struct uba_device *ui; 1657024Ssam { 1667024Ssam register struct vv_softc *vs = &vv_softc[ui->ui_unit]; 1677024Ssam register struct sockaddr_in *sin; 1687024Ssam 1697024Ssam vs->vs_if.if_unit = ui->ui_unit; 1707024Ssam vs->vs_if.if_name = "vv"; 1717024Ssam vs->vs_if.if_mtu = VVMTU; 1727024Ssam vs->vs_if.if_net = ui->ui_flags; 1737024Ssam vs->vs_if.if_host[0] = 0; /* this will be reset in vvinit() */ 1747024Ssam sin = (struct sockaddr_in *)&vs->vs_if.if_addr; 1757024Ssam sin->sin_family = AF_INET; 1767024Ssam sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]); 1777024Ssam sin = (struct sockaddr_in *)&vs->vs_if.if_broadaddr; 1787024Ssam sin->sin_family = AF_INET; 1797024Ssam sin->sin_addr = if_makeaddr(vs->vs_if.if_net, VV_BROADCAST); 1807024Ssam vs->vs_if.if_flags = IFF_BROADCAST; 1817024Ssam vs->vs_if.if_init = vvinit; 1827024Ssam vs->vs_if.if_output = vvoutput; 18311209Ssam vs->vs_if.if_reset = vvreset; 1847640Ssam vs->vs_ifuba.ifu_flags = UBA_CANTWAIT | UBA_NEEDBDP | UBA_NEED16; 18512354Smo #if defined(VAX750) 18612354Smo /* don't chew up 750 bdp's */ 18712354Smo if (cpu == VAX_750 && ui->ui_unit > 0) 18812354Smo vs->vs_ifuba.ifu_flags &= ~UBA_NEEDBDP; 18912354Smo #endif 1907024Ssam if_attach(&vs->vs_if); 1917024Ssam } 1927024Ssam 1937024Ssam /* 1947024Ssam * Reset of interface after UNIBUS reset. 1957024Ssam * If interface is on specified uba, reset its state. 1967024Ssam */ 1977024Ssam vvreset(unit, uban) 1987024Ssam int unit, uban; 1997024Ssam { 2007024Ssam register struct uba_device *ui; 2017024Ssam 2027024Ssam if (unit >= NVV || (ui = vvinfo[unit]) == 0 || ui->ui_alive == 0 || 2037024Ssam ui->ui_ubanum != uban) 2047024Ssam return; 2057024Ssam printf(" vv%d", unit); 2067024Ssam vvinit(unit); 2077024Ssam } 2087024Ssam 2097024Ssam /* 2107024Ssam * Initialization of interface; clear recorded pending 2117024Ssam * operations, and reinitialize UNIBUS usage. 2127024Ssam */ 2137024Ssam vvinit(unit) 2147024Ssam int unit; 2157024Ssam { 2167024Ssam register struct vv_softc *vs = &vv_softc[unit]; 2177024Ssam register struct uba_device *ui = vvinfo[unit]; 2187024Ssam register struct vvreg *addr; 2197024Ssam struct sockaddr_in *sin; 2207640Ssam int ubainfo, s; 22111192Ssam int vvtimeout(); 2227024Ssam 2237640Ssam addr = (struct vvreg *)ui->ui_addr; 2247024Ssam if (if_ubainit(&vs->vs_ifuba, ui->ui_ubanum, 2257024Ssam sizeof (struct vv_header), (int)btoc(VVMTU)) == 0) { 2267024Ssam printf("vv%d: can't initialize\n", unit); 2277640Ssam vs->vs_if.if_flags &= ~IFF_UP; 2287024Ssam return; 2297024Ssam } 23011209Ssam if (vv_ticking++ == 0) 23111209Ssam timeout(vvtimeout, (caddr_t) 0, VV_FLYWHEEL); 2327024Ssam /* 23311209Ssam * Discover our host address and post it 2347640Ssam */ 2357640Ssam vs->vs_if.if_host[0] = vvidentify(unit); 2367640Ssam printf("vv%d: host %d\n", unit, vs->vs_if.if_host[0]); 2377640Ssam sin = (struct sockaddr_in *)&vs->vs_if.if_addr; 2387640Ssam sin->sin_family = AF_INET; 23911209Ssam sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]); 2407640Ssam 2417640Ssam /* 2427640Ssam * Reset the interface, and join the ring 2437640Ssam */ 2447640Ssam addr->vvocsr = VV_RST | VV_CPB; /* clear packet buffer */ 2457640Ssam addr->vvicsr = VV_RST | VV_CONF; /* close logical relay */ 24612351Smo DELAY(500000); /* let contacts settle */ 2477640Ssam vs->vs_init = 0; 24811192Ssam vs->vs_dropped = 0; 2497640Ssam vs->vs_nottaken = 0; 2507640Ssam 2517640Ssam /* 2527640Ssam * Hang a receive and start any 2537640Ssam * pending writes by faking a transmit complete. 2547640Ssam */ 2557640Ssam s = splimp(); 2567640Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 2577640Ssam addr->vviba = (u_short) ubainfo; 2587640Ssam addr->vviea = (u_short) (ubainfo >> 16); 2597640Ssam addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1; 2607640Ssam addr->vvicsr = VV_IEN | VV_CONF | VV_DEN | VV_ENB; 26111192Ssam vs->vs_iactive = ACTIVE; 2627640Ssam vs->vs_oactive = 1; 2637640Ssam vs->vs_if.if_flags |= IFF_UP; 2647640Ssam vvxint(unit); 2657640Ssam splx(s); 2667640Ssam if_rtinit(&vs->vs_if, RTF_UP); 2677640Ssam } 2687640Ssam 2697640Ssam /* 2707640Ssam * vvidentify() - return our host address 2717640Ssam */ 27211209Ssam vvidentify(unit) 27311209Ssam { 2747640Ssam register struct vv_softc *vs = &vv_softc[unit]; 2757640Ssam register struct uba_device *ui = vvinfo[unit]; 2767640Ssam register struct vvreg *addr; 2777640Ssam struct mbuf *m; 2787640Ssam struct vv_header *v; 2797640Ssam int ubainfo, retrying, attempts, waitcount, s; 2807640Ssam 2817640Ssam /* 2827024Ssam * Build a multicast message to identify our address 2837024Ssam */ 2847640Ssam addr = (struct vvreg *)ui->ui_addr; 2857024Ssam attempts = 0; /* total attempts, including bad msg type */ 2867024Ssam retrying = 0; /* first time through */ 28711209Ssam m = m_get(M_DONTWAIT, MT_HEADER); 28811192Ssam if (m == 0) 28911192Ssam panic("vvinit: can't get mbuf"); 29011192Ssam m->m_next = 0; 2917024Ssam m->m_off = MMINOFF; 2927024Ssam m->m_len = sizeof(struct vv_header); 2937024Ssam v = mtod(m, struct vv_header *); 29411192Ssam v->vh_dhost = VV_BROADCAST; /* multicast destination address */ 2957024Ssam v->vh_shost = 0; /* will be overwritten with ours */ 2967024Ssam v->vh_version = RING_VERSION; 2977024Ssam v->vh_type = RING_WHOAMI; 2987024Ssam v->vh_info = 0; 29911192Ssam /* map xmit message into uba */ 3007640Ssam vs->vs_olen = if_wubaput(&vs->vs_ifuba, m); 3017640Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 3027640Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp); 3037024Ssam /* 3047024Ssam * Reset interface, establish Digital Loopback Mode, and 3057024Ssam * send the multicast (to myself) with Input Copy enabled. 3067024Ssam */ 3077024Ssam retry: 3087024Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 3097024Ssam addr->vvicsr = VV_RST; 3107024Ssam addr->vviba = (u_short) ubainfo; 3117024Ssam addr->vviea = (u_short) (ubainfo >> 16); 3127024Ssam addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1; 3137024Ssam addr->vvicsr = VV_STE | VV_DEN | VV_ENB | VV_LPB; 3147640Ssam 3157640Ssam /* let flag timers fire so ring will initialize */ 31612164Ssam DELAY(2000000); 3177640Ssam 3187024Ssam addr->vvocsr = VV_RST | VV_CPB; /* clear packet buffer */ 3197024Ssam ubainfo = vs->vs_ifuba.ifu_w.ifrw_info; 3207024Ssam addr->vvoba = (u_short) ubainfo; 3217024Ssam addr->vvoea = (u_short) (ubainfo >> 16); 3227024Ssam addr->vvowc = -((vs->vs_olen + 1) >> 1); 3237024Ssam addr->vvocsr = VV_CPB | VV_DEN | VV_INR | VV_ENB; 3247024Ssam /* 3257024Ssam * Wait for receive side to finish. 32611192Ssam * Extract source address (which will our own), 3277024Ssam * and post to interface structure. 3287024Ssam */ 3297024Ssam DELAY(1000); 33011192Ssam for (waitcount = 0; (addr->vvicsr & VV_RDY) == 0; waitcount++) { 3319876Ssam if (waitcount < 10) { 3327024Ssam DELAY(1000); 33311209Ssam continue; 33411192Ssam } 33511209Ssam if (attempts++ >= 10) { 33611209Ssam printf("vv%d: can't initialize\n", unit); 33711209Ssam printf("vvinit loopwait: icsr = %b\n", 33811209Ssam 0xffff&(addr->vvicsr), VV_IBITS); 33911209Ssam vs->vs_if.if_flags &= ~IFF_UP; 34011209Ssam return; 3417024Ssam } 34211209Ssam goto retry; 34311192Ssam } 3447024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 3457024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp); 3467024Ssam if (vs->vs_ifuba.ifu_xtofree) 3477024Ssam m_freem(vs->vs_ifuba.ifu_xtofree); 3487024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 3497024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp); 3507024Ssam m = if_rubaget(&vs->vs_ifuba, sizeof(struct vv_header), 0); 35111209Ssam if (m != NULL) 3527024Ssam m_freem(m); 3537024Ssam /* 35411209Ssam * Check message type before we believe the source host address 3557024Ssam */ 3567024Ssam v = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr); 35711209Ssam if (v->vh_type != RING_WHOAMI) 3587640Ssam goto retry; 35911209Ssam return(v->vh_shost); 3607024Ssam } 3617024Ssam 3627024Ssam /* 36311192Ssam * vvtimeout() - called by timer flywheel to monitor input packet 36411192Ssam * discard rate. Interfaces getting too many errors are shut 36511192Ssam * down for a while. If the condition persists, the interface 36611192Ssam * is marked down. 36711192Ssam */ 36811192Ssam vvtimeout(junk) 36911209Ssam int junk; 37011192Ssam { 37111192Ssam register struct vv_softc *vs; 37211192Ssam register int i; 37311192Ssam register struct vvreg *addr; 37411192Ssam int ubainfo; 37511192Ssam 37611192Ssam timeout(vvtimeout, (caddr_t) 0, VV_FLYWHEEL); 37711209Ssam for (i = 0; i < NVV; i++) { 37811192Ssam vs = &vv_softc[i]; 37911192Ssam addr = (struct vvreg *)vvinfo[i]->ui_addr; 38011192Ssam if (vs->vs_if.if_flags & IFF_UP == 0) continue; 38111192Ssam switch (vs->vs_major) { 38211192Ssam 38311192Ssam /* 38411192Ssam * MODE0: generally OK, just check error rate 38511192Ssam */ 38611192Ssam case MODE0: 38711192Ssam if (vs->vs_dropped < VV_ERRORTHRESHOLD) { 38811192Ssam vs->vs_dropped = 0; 38911192Ssam continue; 39011192Ssam } 39111209Ssam /* suspend reads for a while */ 39211209Ssam vvtrprintf("vv%d going MODE1 in vvtimeout\n",i); 39311209Ssam vs->vs_major = MODE1; 39411209Ssam vs->vs_iactive = PAUSE; /* no new reads */ 39511209Ssam vs->vs_retry = VV_MODE1ATTEMPTS; 39611209Ssam vs->vs_delayclock = VV_MODE1DELAY; 39711209Ssam vs->vs_minor = 0; 39811209Ssam continue; 39911192Ssam 40011192Ssam /* 40111192Ssam * MODE1: excessive error rate observed 40211192Ssam * Scheme: try simply suspending reads for a 40311192Ssam * short while a small number of times 40411192Ssam */ 40511192Ssam case MODE1: 40611192Ssam if (vs->vs_delayclock > 0) { 40711192Ssam vs->vs_delayclock--; 40811192Ssam continue; 40911192Ssam } 41011192Ssam switch (vs->vs_minor) { 41111209Ssam 41211192Ssam case 0: /* reenable reads */ 41311192Ssam vvtrprintf("vv%d M1m0\n",i); 41411192Ssam vs->vs_dropped = 0; 41511192Ssam vs->vs_iactive = ACTIVE; 41611192Ssam vs->vs_minor = 1; /* next state */ 41711192Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 41811192Ssam addr->vviba = (u_short) ubainfo; 41911192Ssam addr->vviea = (u_short) (ubainfo >> 16); 42011209Ssam addr->vviwc = 42111209Ssam -(sizeof (struct vv_header) + VVMTU) >> 1; 42211192Ssam addr->vvicsr = VV_RST | VV_CONF; 42311192Ssam addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB; 42411192Ssam continue; 42511209Ssam 42611192Ssam case 1: /* see if it worked */ 42711192Ssam vvtrprintf("vv%d M1m1\n",i); 42811192Ssam if (vs->vs_dropped < VV_ERRORTHRESHOLD) { 42911192Ssam vs->vs_dropped = 0; 43011192Ssam vs->vs_major = MODE0; /* yeah!! */ 43111192Ssam continue; 43211192Ssam } 43311209Ssam if (vs->vs_retry -- > 0) { 43411209Ssam vs->vs_dropped = 0; 43511209Ssam vs->vs_iactive = PAUSE; 43611209Ssam vs->vs_delayclock = VV_MODE1DELAY; 43711209Ssam vs->vs_minor = 0; /* recheck */ 43811209Ssam continue; 43911192Ssam } 44011209Ssam vs->vs_major = MODE2; 44111209Ssam vs->vs_minor = 0; 44211209Ssam vs->vs_dropped = 0; 44311209Ssam vs->vs_iactive = OPEN; 44411209Ssam vs->vs_delayrange = VV_MODE2DELAY; 44511209Ssam vs->vs_delayclock = VV_MODE2DELAY; 44611209Ssam /* fall thru ... */ 44711192Ssam } 44811192Ssam 44911192Ssam /* 45011192Ssam * MODE2: simply ignoring traffic didn't relieve condition 45111192Ssam * Scheme: open host relay for intervals linearly 45211192Ssam * increasing up to some maximum of a several minutes. 45311192Ssam * This allows broken networks to return to operation 45411192Ssam * without rebooting. 45511192Ssam */ 45611192Ssam case MODE2: 45711192Ssam if (vs->vs_delayclock > 0) { 45811192Ssam vs->vs_delayclock--; 45911192Ssam continue; 46011192Ssam } 46111192Ssam switch (vs->vs_minor) { 46211209Ssam 46311192Ssam case 0: /* close relay and reenable reads */ 46411192Ssam vvtrprintf("vv%d M2m0\n",i); 46511192Ssam vs->vs_dropped = 0; 46611192Ssam vs->vs_iactive = ACTIVE; 46711192Ssam vs->vs_minor = 1; /* next state */ 46811192Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 46911192Ssam addr->vviba = (u_short) ubainfo; 47011192Ssam addr->vviea = (u_short) (ubainfo >> 16); 47111209Ssam addr->vviwc = 47211209Ssam -(sizeof (struct vv_header) + VVMTU) >> 1; 47311192Ssam addr->vvicsr = VV_RST | VV_CONF; 47411192Ssam addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB; 47511192Ssam continue; 47611209Ssam 47711192Ssam case 1: /* see if it worked */ 47811192Ssam vvtrprintf("vv%d M2m1\n",i); 47911192Ssam if (vs->vs_dropped < VV_ERRORTHRESHOLD) { 48011192Ssam vs->vs_dropped = 0; 48111192Ssam vs->vs_major = MODE0; /* yeah!! */ 48211192Ssam continue; 48311192Ssam } 48411209Ssam vvtrprintf("vv%d M2m1 ++ delay\n",i); 48511209Ssam vs->vs_dropped = 0; 48611209Ssam vs->vs_iactive = OPEN; 48711209Ssam vs->vs_minor = 0; 48811209Ssam if (vs->vs_delayrange < VV_MAXDELAY) 48911209Ssam vs->vs_delayrange += 49011209Ssam (vs->vs_delayrange/2); 49111209Ssam vs->vs_delayclock = vs->vs_delayrange; 49211209Ssam continue; 49311192Ssam } 49411192Ssam 49511192Ssam default: 49611192Ssam printf("vv%d: major state screwed\n", i); 49711192Ssam vs->vs_if.if_flags &= ~IFF_UP; 49811192Ssam } 49911192Ssam } 50011192Ssam } 50111192Ssam 50211192Ssam /* 5037024Ssam * Start or restart output on interface. 50411192Ssam * If interface is active, this is a retransmit, so just 50511192Ssam * restuff registers and go. 5067024Ssam * If interface is not already active, get another datagram 5077024Ssam * to send off of the interface queue, and map it to the interface 5087024Ssam * before starting the output. 5097024Ssam */ 5107024Ssam vvstart(dev) 5117024Ssam dev_t dev; 5127024Ssam { 5137024Ssam int unit = VVUNIT(dev); 5147024Ssam struct uba_device *ui = vvinfo[unit]; 5157024Ssam register struct vv_softc *vs = &vv_softc[unit]; 5167024Ssam register struct vvreg *addr; 5177024Ssam struct mbuf *m; 51811192Ssam int ubainfo; 51911192Ssam int dest; 5207024Ssam 5217024Ssam if (vs->vs_oactive) 5227024Ssam goto restart; 5237024Ssam /* 5247024Ssam * Not already active: dequeue another request 5257024Ssam * and map it to the UNIBUS. If no more requests, 5267024Ssam * just return. 5277024Ssam */ 5287024Ssam IF_DEQUEUE(&vs->vs_if.if_snd, m); 52911209Ssam if (m == NULL) { 5307024Ssam vs->vs_oactive = 0; 5317024Ssam return; 5327024Ssam } 5337024Ssam dest = mtod(m, struct vv_header *)->vh_dhost; 5347024Ssam vs->vs_olen = if_wubaput(&vs->vs_ifuba, m); 5357024Ssam vs->vs_lastx = dest; 5367024Ssam restart: 5377024Ssam /* 5387024Ssam * Have request mapped to UNIBUS for transmission. 5397024Ssam * Purge any stale data from this BDP, and start the otput. 5407024Ssam */ 541*12363Ssam if (vs->vs_olen > VVMTU + sizeof (struct vv_header)) { 54211192Ssam printf("vv%d vs_olen: %d > VVMTU\n", unit, vs->vs_olen); 54311192Ssam panic("vvdriver vs_olen botch"); 54411192Ssam } 5457024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 5467024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp); 5477024Ssam addr = (struct vvreg *)ui->ui_addr; 5487024Ssam ubainfo = vs->vs_ifuba.ifu_w.ifrw_info; 5497024Ssam addr->vvoba = (u_short) ubainfo; 5507024Ssam addr->vvoea = (u_short) (ubainfo >> 16); 5517024Ssam addr->vvowc = -((vs->vs_olen + 1) >> 1); 5527024Ssam addr->vvocsr = VV_IEN | VV_CPB | VV_DEN | VV_INR | VV_ENB; 5537024Ssam vs->vs_oactive = 1; 5547024Ssam } 5557024Ssam 5567024Ssam /* 5577024Ssam * VVLNI transmit interrupt 5587024Ssam * Start another output if more data to send. 5597024Ssam */ 5607024Ssam vvxint(unit) 5617024Ssam int unit; 5627024Ssam { 5637024Ssam register struct uba_device *ui = vvinfo[unit]; 5647024Ssam register struct vv_softc *vs = &vv_softc[unit]; 5657024Ssam register struct vvreg *addr; 5667024Ssam register int oc; 5677024Ssam 5687024Ssam addr = (struct vvreg *)ui->ui_addr; 5697024Ssam oc = 0xffff & (addr->vvocsr); 5707024Ssam if (vs->vs_oactive == 0) { 57111192Ssam printf("vv%d: stray interrupt vvocsr = %b\n", unit, 5727024Ssam oc, VV_OBITS); 5737024Ssam return; 5747024Ssam } 5757024Ssam if (oc & (VV_OPT | VV_RFS)) { 5767640Ssam vs->vs_if.if_collisions++; 57711192Ssam if (vs->vs_tries++ < VVRETRY) { 5787024Ssam if (oc & VV_OPT) 5797024Ssam vs->vs_init++; 5807024Ssam if (oc & VV_RFS) 5817024Ssam vs->vs_nottaken++; 58211192Ssam vvstart(unit); /* restart this message */ 5837024Ssam return; 5847024Ssam } 5857024Ssam if (oc & VV_OPT) 5867024Ssam printf("vv%d: output timeout\n"); 5877024Ssam } 5887024Ssam vs->vs_if.if_opackets++; 5897024Ssam vs->vs_oactive = 0; 5907024Ssam vs->vs_tries = 0; 5917024Ssam if (oc & VVXERR) { 5927024Ssam vs->vs_if.if_oerrors++; 59311192Ssam printf("vv%d: error vvocsr = %b\n", unit, 0xffff & oc, 5947024Ssam VV_OBITS); 5957024Ssam } 5967024Ssam if (vs->vs_ifuba.ifu_xtofree) { 5977024Ssam m_freem(vs->vs_ifuba.ifu_xtofree); 5987024Ssam vs->vs_ifuba.ifu_xtofree = 0; 5997024Ssam } 6007024Ssam if (vs->vs_if.if_snd.ifq_head == 0) { 60111192Ssam vs->vs_lastx = 256; /* an invalid address */ 6027024Ssam return; 6037024Ssam } 6047024Ssam vvstart(unit); 6057024Ssam } 6067024Ssam 6077024Ssam /* 6087024Ssam * V2lni interface receiver interrupt. 6097024Ssam * If input error just drop packet. 6107024Ssam * Otherwise purge input buffered data path and examine 6117024Ssam * packet to determine type. If can't determine length 6127024Ssam * from type, then have to drop packet. Othewise decapsulate 6137024Ssam * packet based on type and pass to type specific higher-level 6147024Ssam * input routine. 6157024Ssam */ 6167024Ssam vvrint(unit) 6177024Ssam int unit; 6187024Ssam { 6197024Ssam register struct vv_softc *vs = &vv_softc[unit]; 6207024Ssam struct vvreg *addr = (struct vvreg *)vvinfo[unit]->ui_addr; 6217024Ssam register struct vv_header *vv; 6227024Ssam register struct ifqueue *inq; 6237024Ssam struct mbuf *m; 6247024Ssam int ubainfo, len, off; 6257640Ssam short resid; 6267024Ssam 6277024Ssam vs->vs_if.if_ipackets++; 6287024Ssam /* 6297024Ssam * Purge BDP; drop if input error indicated. 6307024Ssam */ 6317024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 6327024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp); 6337024Ssam if (addr->vvicsr & VVRERR) { 63411192Ssam if (vv_logreaderrors) 63511192Ssam printf("vv%d: error vvicsr = %b\n", unit, 63611192Ssam 0xffff&(addr->vvicsr), VV_IBITS); 6377640Ssam goto dropit; 6387024Ssam } 6397640Ssam 6407024Ssam /* 6417640Ssam * Get packet length from word count residue 6427640Ssam * 6437640Ssam * Compute header offset if trailer protocol 6447640Ssam * 6457640Ssam * Pull packet off interface. Off is nonzero if packet 6467640Ssam * has trailing header; if_rubaget will then force this header 6477640Ssam * information to be at the front. The vh_info field 6487640Ssam * carries the offset to the trailer data in trailer 6497640Ssam * format packets. 6507024Ssam */ 6517640Ssam vv = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr); 65211192Ssam vvtracehdr("vi", vv); 6537640Ssam resid = addr->vviwc; 6547640Ssam if (resid) 6557640Ssam resid |= 0176000; /* ugly!!!! */ 6567640Ssam len = (((sizeof (struct vv_header) + VVMRU) >> 1) + resid) << 1; 6577640Ssam len -= sizeof(struct vv_header); 65811192Ssam if (len > VVMRU || len <= 0) 6597640Ssam goto dropit; 6607640Ssam #define vvdataaddr(vv, off, type) ((type)(((caddr_t)((vv)+1)+(off)))) 6617640Ssam if (vv_dotrailer && vv->vh_type >= RING_IPTrailer && 6627640Ssam vv->vh_type < RING_IPTrailer+RING_IPNTrailer){ 6637640Ssam off = (vv->vh_type - RING_IPTrailer) * 512; 6647640Ssam if (off > VVMTU) 6657640Ssam goto dropit; 6667640Ssam vv->vh_type = *vvdataaddr(vv, off, u_short *); 6677640Ssam resid = *(vvdataaddr(vv, off+2, u_short *)); 6687640Ssam if (off + resid > len) 6697640Ssam goto dropit; 6707640Ssam len = off + resid; 67111209Ssam } else 6727640Ssam off = 0; 6737640Ssam if (len == 0) 6747640Ssam goto dropit; 6757640Ssam m = if_rubaget(&vs->vs_ifuba, len, off); 67611209Ssam if (m == NULL) 6777640Ssam goto dropit; 6787640Ssam if (off) { 6797640Ssam m->m_off += 2 * sizeof(u_short); 6807640Ssam m->m_len -= 2 * sizeof(u_short); 6817640Ssam } 68211192Ssam 68311192Ssam /* 68411192Ssam * Demultiplex on packet type 68511192Ssam */ 6867024Ssam switch (vv->vh_type) { 68711192Ssam 6887024Ssam #ifdef INET 6897024Ssam case RING_IP: 6907024Ssam schednetisr(NETISR_IP); 6917024Ssam inq = &ipintrq; 6927024Ssam break; 6937024Ssam #endif 6947024Ssam default: 6957024Ssam printf("vv%d: unknown pkt type 0x%x\n", unit, vv->vh_type); 6967640Ssam m_freem(m); 6977024Ssam goto setup; 6987024Ssam } 6997640Ssam if (IF_QFULL(inq)) { 7007640Ssam IF_DROP(inq); 7017640Ssam m_freem(m); 70211209Ssam } else 7037640Ssam IF_ENQUEUE(inq, m); 7047024Ssam setup: 7057024Ssam /* 70611192Ssam * Check the error rate and start recovery if needed 70711192Ssam * this has to go here since the timer flywheel runs at 70811192Ssam * a lower ipl and never gets a chance to change the mode 7097024Ssam */ 71011192Ssam if (vs->vs_major == MODE0 && vs->vs_dropped > VV_ERRORTHRESHOLD) { 71111192Ssam vvtrprintf("vv%d going MODE1 in vvrint\n",unit); 71211192Ssam vs->vs_major = MODE1; 71311192Ssam vs->vs_iactive = PAUSE; /* no new reads */ 71411192Ssam vs->vs_retry = VV_MODE1ATTEMPTS; 71511192Ssam vs->vs_delayclock = VV_MODE1DELAY; 71611192Ssam vs->vs_minor = 0; 71711192Ssam vs->vs_dropped = 0; 71811192Ssam } 71911192Ssam switch (vs->vs_iactive) { 72011192Ssam 72111209Ssam case ACTIVE: /* Restart the read for next packet */ 72211192Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 72311192Ssam addr->vviba = (u_short) ubainfo; 72411192Ssam addr->vviea = (u_short) (ubainfo >> 16); 72511192Ssam addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1; 72611192Ssam addr->vvicsr = VV_RST | VV_CONF; 72711192Ssam addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB; 72811192Ssam return; 72911192Ssam 73011209Ssam case PAUSE: /* requested to not start any new reads */ 73111192Ssam vs->vs_dropped = 0; 73211192Ssam return; 73311192Ssam 73411209Ssam case OPEN: /* request to open host relay */ 73511192Ssam vs->vs_dropped = 0; 73611192Ssam addr->vvicsr = 0; 73711192Ssam return; 73811192Ssam 73911192Ssam default: 74011192Ssam printf("vv%d: vs_iactive = %d\n", unit, vs->vs_iactive); 74111192Ssam return; 74211192Ssam } 74311192Ssam /* 74411209Ssam * Drop packet on floor -- count them!! 74511192Ssam */ 7467640Ssam dropit: 7477640Ssam vs->vs_if.if_ierrors++; 74811192Ssam vs->vs_dropped++; 7497640Ssam /* 7507640Ssam printf("vv%d: error vvicsr = %b\n", unit, 7517640Ssam 0xffff&(addr->vvicsr), VV_IBITS); 7527640Ssam */ 7537640Ssam goto setup; 7547024Ssam } 7557024Ssam 7567024Ssam /* 7577024Ssam * V2lni output routine. 7587024Ssam * Encapsulate a packet of type family for the local net. 7597024Ssam * Use trailer local net encapsulation if enough data in first 7607024Ssam * packet leaves a multiple of 512 bytes of data in remainder. 7617024Ssam */ 7627024Ssam vvoutput(ifp, m0, dst) 7637024Ssam struct ifnet *ifp; 7647024Ssam struct mbuf *m0; 7657024Ssam struct sockaddr *dst; 7667024Ssam { 7677024Ssam register struct mbuf *m = m0; 7687024Ssam register struct vv_header *vv; 7697640Ssam register int off; 7707640Ssam int type, dest, s, error; 7717024Ssam 7727024Ssam switch (dst->sa_family) { 77311192Ssam 7747024Ssam #ifdef INET 7757024Ssam case AF_INET: { 7767640Ssam dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 77711192Ssam if ((dest = in_lnaof(*((struct in_addr *)&dest))) >= 0x100) { 7787640Ssam error = EPERM; 7797640Ssam goto bad; 7807640Ssam } 7817640Ssam off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 7827640Ssam if (vv_dotrailer && off > 0 && (off & 0x1ff) == 0 && 7837640Ssam m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 7847640Ssam type = RING_IPTrailer + (off>>9); 7857640Ssam m->m_off -= 2 * sizeof (u_short); 7867640Ssam m->m_len += 2 * sizeof (u_short); 7877640Ssam *mtod(m, u_short *) = RING_IP; 7887640Ssam *(mtod(m, u_short *) + 1) = m->m_len; 7897640Ssam goto gottrailertype; 7907640Ssam } 7917024Ssam type = RING_IP; 7927024Ssam off = 0; 7937024Ssam goto gottype; 7947024Ssam } 7957024Ssam #endif 7967024Ssam default: 7977024Ssam printf("vv%d: can't handle af%d\n", ifp->if_unit, 7987024Ssam dst->sa_family); 7997640Ssam error = EAFNOSUPPORT; 8007640Ssam goto bad; 8017024Ssam } 8027024Ssam 8037024Ssam gottrailertype: 8047024Ssam /* 8057024Ssam * Packet to be sent as trailer: move first packet 8067024Ssam * (control information) to end of chain. 8077024Ssam */ 8087024Ssam while (m->m_next) 8097024Ssam m = m->m_next; 8107024Ssam m->m_next = m0; 8117024Ssam m = m0->m_next; 8127024Ssam m0->m_next = 0; 8137024Ssam m0 = m; 8147024Ssam gottype: 8157024Ssam /* 8167024Ssam * Add local net header. If no space in first mbuf, 8177024Ssam * allocate another. 8187024Ssam */ 8197024Ssam if (m->m_off > MMAXOFF || 8207024Ssam MMINOFF + sizeof (struct vv_header) > m->m_off) { 82111209Ssam m = m_get(M_DONTWAIT, MT_HEADER); 82211209Ssam if (m == NULL) { 8237640Ssam error = ENOBUFS; 8247640Ssam goto bad; 8257024Ssam } 8267024Ssam m->m_next = m0; 8277024Ssam m->m_off = MMINOFF; 8287024Ssam m->m_len = sizeof (struct vv_header); 8297024Ssam } else { 8307024Ssam m->m_off -= sizeof (struct vv_header); 8317024Ssam m->m_len += sizeof (struct vv_header); 8327024Ssam } 8337024Ssam vv = mtod(m, struct vv_header *); 8347024Ssam vv->vh_shost = ifp->if_host[0]; 8357024Ssam vv->vh_dhost = dest; 8367024Ssam vv->vh_version = RING_VERSION; 8377024Ssam vv->vh_type = type; 8387640Ssam vv->vh_info = off; 83911192Ssam vvtracehdr("vo", vv); 8407024Ssam 8417024Ssam /* 8427024Ssam * Queue message on interface, and start output if interface 8437024Ssam * not yet active. 8447024Ssam */ 8457024Ssam s = splimp(); 8467640Ssam if (IF_QFULL(&ifp->if_snd)) { 8477640Ssam IF_DROP(&ifp->if_snd); 8487640Ssam error = ENOBUFS; 8497640Ssam goto qfull; 8507640Ssam } 8517024Ssam IF_ENQUEUE(&ifp->if_snd, m); 8527024Ssam if (vv_softc[ifp->if_unit].vs_oactive == 0) 8537024Ssam vvstart(ifp->if_unit); 8547024Ssam splx(s); 8557640Ssam return (0); 8567640Ssam qfull: 8577640Ssam m0 = m; 8587640Ssam splx(s); 8597640Ssam bad: 8607640Ssam m_freem(m0); 8617640Ssam return(error); 8627024Ssam } 8637024Ssam 8647024Ssam /* 8657024Ssam * vvprt_hdr(s, v) print the local net header in "v" 8667024Ssam * with title is "s" 8677024Ssam */ 8687024Ssam vvprt_hdr(s, v) 8697024Ssam char *s; 8707024Ssam register struct vv_header *v; 8717024Ssam { 8727024Ssam printf("%s: dsvti: 0x%x 0x%x 0x%x 0x%x 0x%x\n", 8737024Ssam s, 8747024Ssam 0xff & (int)(v->vh_dhost), 0xff & (int)(v->vh_shost), 8757024Ssam 0xff & (int)(v->vh_version), 0xff & (int)(v->vh_type), 8767024Ssam 0xffff & (int)(v->vh_info)); 8777024Ssam } 8787024Ssam 8797024Ssam /* 8807024Ssam * print "l" hex bytes starting at "s" 8817024Ssam */ 8827024Ssam vvprt_hex(s, l) 8837024Ssam char *s; 8847024Ssam int l; 8857024Ssam { 8867024Ssam register int i; 8877024Ssam register int z; 8887024Ssam 8897024Ssam for (i=0 ; i < l; i++) { 8907024Ssam z = 0xff & (int)(*(s + i)); 8917024Ssam printf("%c%c ", 8927024Ssam "0123456789abcdef"[(z >> 4) & 0x0f], 8937024Ssam "0123456789abcdef"[z & 0x0f] 8947024Ssam ); 8957024Ssam } 8967024Ssam } 897