1*12351Smo /* if_vv.c 4.17 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; 144*12351Smo 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; 1857024Ssam if_attach(&vs->vs_if); 1867024Ssam } 1877024Ssam 1887024Ssam /* 1897024Ssam * Reset of interface after UNIBUS reset. 1907024Ssam * If interface is on specified uba, reset its state. 1917024Ssam */ 1927024Ssam vvreset(unit, uban) 1937024Ssam int unit, uban; 1947024Ssam { 1957024Ssam register struct uba_device *ui; 1967024Ssam 1977024Ssam if (unit >= NVV || (ui = vvinfo[unit]) == 0 || ui->ui_alive == 0 || 1987024Ssam ui->ui_ubanum != uban) 1997024Ssam return; 2007024Ssam printf(" vv%d", unit); 2017024Ssam vvinit(unit); 2027024Ssam } 2037024Ssam 2047024Ssam /* 2057024Ssam * Initialization of interface; clear recorded pending 2067024Ssam * operations, and reinitialize UNIBUS usage. 2077024Ssam */ 2087024Ssam vvinit(unit) 2097024Ssam int unit; 2107024Ssam { 2117024Ssam register struct vv_softc *vs = &vv_softc[unit]; 2127024Ssam register struct uba_device *ui = vvinfo[unit]; 2137024Ssam register struct vvreg *addr; 2147024Ssam struct sockaddr_in *sin; 2157640Ssam int ubainfo, s; 21611192Ssam int vvtimeout(); 2177024Ssam 2187640Ssam addr = (struct vvreg *)ui->ui_addr; 2197024Ssam if (if_ubainit(&vs->vs_ifuba, ui->ui_ubanum, 2207024Ssam sizeof (struct vv_header), (int)btoc(VVMTU)) == 0) { 2217024Ssam printf("vv%d: can't initialize\n", unit); 2227640Ssam vs->vs_if.if_flags &= ~IFF_UP; 2237024Ssam return; 2247024Ssam } 22511209Ssam if (vv_ticking++ == 0) 22611209Ssam timeout(vvtimeout, (caddr_t) 0, VV_FLYWHEEL); 2277024Ssam /* 22811209Ssam * Discover our host address and post it 2297640Ssam */ 2307640Ssam vs->vs_if.if_host[0] = vvidentify(unit); 2317640Ssam printf("vv%d: host %d\n", unit, vs->vs_if.if_host[0]); 2327640Ssam sin = (struct sockaddr_in *)&vs->vs_if.if_addr; 2337640Ssam sin->sin_family = AF_INET; 23411209Ssam sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]); 2357640Ssam 2367640Ssam /* 2377640Ssam * Reset the interface, and join the ring 2387640Ssam */ 2397640Ssam addr->vvocsr = VV_RST | VV_CPB; /* clear packet buffer */ 2407640Ssam addr->vvicsr = VV_RST | VV_CONF; /* close logical relay */ 241*12351Smo DELAY(500000); /* let contacts settle */ 2427640Ssam vs->vs_init = 0; 24311192Ssam vs->vs_dropped = 0; 2447640Ssam vs->vs_nottaken = 0; 2457640Ssam 2467640Ssam /* 2477640Ssam * Hang a receive and start any 2487640Ssam * pending writes by faking a transmit complete. 2497640Ssam */ 2507640Ssam s = splimp(); 2517640Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 2527640Ssam addr->vviba = (u_short) ubainfo; 2537640Ssam addr->vviea = (u_short) (ubainfo >> 16); 2547640Ssam addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1; 2557640Ssam addr->vvicsr = VV_IEN | VV_CONF | VV_DEN | VV_ENB; 25611192Ssam vs->vs_iactive = ACTIVE; 2577640Ssam vs->vs_oactive = 1; 2587640Ssam vs->vs_if.if_flags |= IFF_UP; 2597640Ssam vvxint(unit); 2607640Ssam splx(s); 2617640Ssam if_rtinit(&vs->vs_if, RTF_UP); 2627640Ssam } 2637640Ssam 2647640Ssam /* 2657640Ssam * vvidentify() - return our host address 2667640Ssam */ 26711209Ssam vvidentify(unit) 26811209Ssam { 2697640Ssam register struct vv_softc *vs = &vv_softc[unit]; 2707640Ssam register struct uba_device *ui = vvinfo[unit]; 2717640Ssam register struct vvreg *addr; 2727640Ssam struct mbuf *m; 2737640Ssam struct vv_header *v; 2747640Ssam int ubainfo, retrying, attempts, waitcount, s; 2757640Ssam 2767640Ssam /* 2777024Ssam * Build a multicast message to identify our address 2787024Ssam */ 2797640Ssam addr = (struct vvreg *)ui->ui_addr; 2807024Ssam attempts = 0; /* total attempts, including bad msg type */ 2817024Ssam retrying = 0; /* first time through */ 28211209Ssam m = m_get(M_DONTWAIT, MT_HEADER); 28311192Ssam if (m == 0) 28411192Ssam panic("vvinit: can't get mbuf"); 28511192Ssam m->m_next = 0; 2867024Ssam m->m_off = MMINOFF; 2877024Ssam m->m_len = sizeof(struct vv_header); 2887024Ssam v = mtod(m, struct vv_header *); 28911192Ssam v->vh_dhost = VV_BROADCAST; /* multicast destination address */ 2907024Ssam v->vh_shost = 0; /* will be overwritten with ours */ 2917024Ssam v->vh_version = RING_VERSION; 2927024Ssam v->vh_type = RING_WHOAMI; 2937024Ssam v->vh_info = 0; 29411192Ssam /* map xmit message into uba */ 2957640Ssam vs->vs_olen = if_wubaput(&vs->vs_ifuba, m); 2967640Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 2977640Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp); 2987024Ssam /* 2997024Ssam * Reset interface, establish Digital Loopback Mode, and 3007024Ssam * send the multicast (to myself) with Input Copy enabled. 3017024Ssam */ 3027024Ssam retry: 3037024Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 3047024Ssam addr->vvicsr = VV_RST; 3057024Ssam addr->vviba = (u_short) ubainfo; 3067024Ssam addr->vviea = (u_short) (ubainfo >> 16); 3077024Ssam addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1; 3087024Ssam addr->vvicsr = VV_STE | VV_DEN | VV_ENB | VV_LPB; 3097640Ssam 3107640Ssam /* let flag timers fire so ring will initialize */ 31112164Ssam DELAY(2000000); 3127640Ssam 3137024Ssam addr->vvocsr = VV_RST | VV_CPB; /* clear packet buffer */ 3147024Ssam ubainfo = vs->vs_ifuba.ifu_w.ifrw_info; 3157024Ssam addr->vvoba = (u_short) ubainfo; 3167024Ssam addr->vvoea = (u_short) (ubainfo >> 16); 3177024Ssam addr->vvowc = -((vs->vs_olen + 1) >> 1); 3187024Ssam addr->vvocsr = VV_CPB | VV_DEN | VV_INR | VV_ENB; 3197024Ssam /* 3207024Ssam * Wait for receive side to finish. 32111192Ssam * Extract source address (which will our own), 3227024Ssam * and post to interface structure. 3237024Ssam */ 3247024Ssam DELAY(1000); 32511192Ssam for (waitcount = 0; (addr->vvicsr & VV_RDY) == 0; waitcount++) { 3269876Ssam if (waitcount < 10) { 3277024Ssam DELAY(1000); 32811209Ssam continue; 32911192Ssam } 33011209Ssam if (attempts++ >= 10) { 33111209Ssam printf("vv%d: can't initialize\n", unit); 33211209Ssam printf("vvinit loopwait: icsr = %b\n", 33311209Ssam 0xffff&(addr->vvicsr), VV_IBITS); 33411209Ssam vs->vs_if.if_flags &= ~IFF_UP; 33511209Ssam return; 3367024Ssam } 33711209Ssam goto retry; 33811192Ssam } 3397024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 3407024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp); 3417024Ssam if (vs->vs_ifuba.ifu_xtofree) 3427024Ssam m_freem(vs->vs_ifuba.ifu_xtofree); 3437024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 3447024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp); 3457024Ssam m = if_rubaget(&vs->vs_ifuba, sizeof(struct vv_header), 0); 34611209Ssam if (m != NULL) 3477024Ssam m_freem(m); 3487024Ssam /* 34911209Ssam * Check message type before we believe the source host address 3507024Ssam */ 3517024Ssam v = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr); 35211209Ssam if (v->vh_type != RING_WHOAMI) 3537640Ssam goto retry; 35411209Ssam return(v->vh_shost); 3557024Ssam } 3567024Ssam 3577024Ssam /* 35811192Ssam * vvtimeout() - called by timer flywheel to monitor input packet 35911192Ssam * discard rate. Interfaces getting too many errors are shut 36011192Ssam * down for a while. If the condition persists, the interface 36111192Ssam * is marked down. 36211192Ssam */ 36311192Ssam vvtimeout(junk) 36411209Ssam int junk; 36511192Ssam { 36611192Ssam register struct vv_softc *vs; 36711192Ssam register int i; 36811192Ssam register struct vvreg *addr; 36911192Ssam int ubainfo; 37011192Ssam 37111192Ssam timeout(vvtimeout, (caddr_t) 0, VV_FLYWHEEL); 37211209Ssam for (i = 0; i < NVV; i++) { 37311192Ssam vs = &vv_softc[i]; 37411192Ssam addr = (struct vvreg *)vvinfo[i]->ui_addr; 37511192Ssam if (vs->vs_if.if_flags & IFF_UP == 0) continue; 37611192Ssam switch (vs->vs_major) { 37711192Ssam 37811192Ssam /* 37911192Ssam * MODE0: generally OK, just check error rate 38011192Ssam */ 38111192Ssam case MODE0: 38211192Ssam if (vs->vs_dropped < VV_ERRORTHRESHOLD) { 38311192Ssam vs->vs_dropped = 0; 38411192Ssam continue; 38511192Ssam } 38611209Ssam /* suspend reads for a while */ 38711209Ssam vvtrprintf("vv%d going MODE1 in vvtimeout\n",i); 38811209Ssam vs->vs_major = MODE1; 38911209Ssam vs->vs_iactive = PAUSE; /* no new reads */ 39011209Ssam vs->vs_retry = VV_MODE1ATTEMPTS; 39111209Ssam vs->vs_delayclock = VV_MODE1DELAY; 39211209Ssam vs->vs_minor = 0; 39311209Ssam continue; 39411192Ssam 39511192Ssam /* 39611192Ssam * MODE1: excessive error rate observed 39711192Ssam * Scheme: try simply suspending reads for a 39811192Ssam * short while a small number of times 39911192Ssam */ 40011192Ssam case MODE1: 40111192Ssam if (vs->vs_delayclock > 0) { 40211192Ssam vs->vs_delayclock--; 40311192Ssam continue; 40411192Ssam } 40511192Ssam switch (vs->vs_minor) { 40611209Ssam 40711192Ssam case 0: /* reenable reads */ 40811192Ssam vvtrprintf("vv%d M1m0\n",i); 40911192Ssam vs->vs_dropped = 0; 41011192Ssam vs->vs_iactive = ACTIVE; 41111192Ssam vs->vs_minor = 1; /* next state */ 41211192Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 41311192Ssam addr->vviba = (u_short) ubainfo; 41411192Ssam addr->vviea = (u_short) (ubainfo >> 16); 41511209Ssam addr->vviwc = 41611209Ssam -(sizeof (struct vv_header) + VVMTU) >> 1; 41711192Ssam addr->vvicsr = VV_RST | VV_CONF; 41811192Ssam addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB; 41911192Ssam continue; 42011209Ssam 42111192Ssam case 1: /* see if it worked */ 42211192Ssam vvtrprintf("vv%d M1m1\n",i); 42311192Ssam if (vs->vs_dropped < VV_ERRORTHRESHOLD) { 42411192Ssam vs->vs_dropped = 0; 42511192Ssam vs->vs_major = MODE0; /* yeah!! */ 42611192Ssam continue; 42711192Ssam } 42811209Ssam if (vs->vs_retry -- > 0) { 42911209Ssam vs->vs_dropped = 0; 43011209Ssam vs->vs_iactive = PAUSE; 43111209Ssam vs->vs_delayclock = VV_MODE1DELAY; 43211209Ssam vs->vs_minor = 0; /* recheck */ 43311209Ssam continue; 43411192Ssam } 43511209Ssam vs->vs_major = MODE2; 43611209Ssam vs->vs_minor = 0; 43711209Ssam vs->vs_dropped = 0; 43811209Ssam vs->vs_iactive = OPEN; 43911209Ssam vs->vs_delayrange = VV_MODE2DELAY; 44011209Ssam vs->vs_delayclock = VV_MODE2DELAY; 44111209Ssam /* fall thru ... */ 44211192Ssam } 44311192Ssam 44411192Ssam /* 44511192Ssam * MODE2: simply ignoring traffic didn't relieve condition 44611192Ssam * Scheme: open host relay for intervals linearly 44711192Ssam * increasing up to some maximum of a several minutes. 44811192Ssam * This allows broken networks to return to operation 44911192Ssam * without rebooting. 45011192Ssam */ 45111192Ssam case MODE2: 45211192Ssam if (vs->vs_delayclock > 0) { 45311192Ssam vs->vs_delayclock--; 45411192Ssam continue; 45511192Ssam } 45611192Ssam switch (vs->vs_minor) { 45711209Ssam 45811192Ssam case 0: /* close relay and reenable reads */ 45911192Ssam vvtrprintf("vv%d M2m0\n",i); 46011192Ssam vs->vs_dropped = 0; 46111192Ssam vs->vs_iactive = ACTIVE; 46211192Ssam vs->vs_minor = 1; /* next state */ 46311192Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 46411192Ssam addr->vviba = (u_short) ubainfo; 46511192Ssam addr->vviea = (u_short) (ubainfo >> 16); 46611209Ssam addr->vviwc = 46711209Ssam -(sizeof (struct vv_header) + VVMTU) >> 1; 46811192Ssam addr->vvicsr = VV_RST | VV_CONF; 46911192Ssam addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB; 47011192Ssam continue; 47111209Ssam 47211192Ssam case 1: /* see if it worked */ 47311192Ssam vvtrprintf("vv%d M2m1\n",i); 47411192Ssam if (vs->vs_dropped < VV_ERRORTHRESHOLD) { 47511192Ssam vs->vs_dropped = 0; 47611192Ssam vs->vs_major = MODE0; /* yeah!! */ 47711192Ssam continue; 47811192Ssam } 47911209Ssam vvtrprintf("vv%d M2m1 ++ delay\n",i); 48011209Ssam vs->vs_dropped = 0; 48111209Ssam vs->vs_iactive = OPEN; 48211209Ssam vs->vs_minor = 0; 48311209Ssam if (vs->vs_delayrange < VV_MAXDELAY) 48411209Ssam vs->vs_delayrange += 48511209Ssam (vs->vs_delayrange/2); 48611209Ssam vs->vs_delayclock = vs->vs_delayrange; 48711209Ssam continue; 48811192Ssam } 48911192Ssam 49011192Ssam default: 49111192Ssam printf("vv%d: major state screwed\n", i); 49211192Ssam vs->vs_if.if_flags &= ~IFF_UP; 49311192Ssam } 49411192Ssam } 49511192Ssam } 49611192Ssam 49711192Ssam /* 4987024Ssam * Start or restart output on interface. 49911192Ssam * If interface is active, this is a retransmit, so just 50011192Ssam * restuff registers and go. 5017024Ssam * If interface is not already active, get another datagram 5027024Ssam * to send off of the interface queue, and map it to the interface 5037024Ssam * before starting the output. 5047024Ssam */ 5057024Ssam vvstart(dev) 5067024Ssam dev_t dev; 5077024Ssam { 5087024Ssam int unit = VVUNIT(dev); 5097024Ssam struct uba_device *ui = vvinfo[unit]; 5107024Ssam register struct vv_softc *vs = &vv_softc[unit]; 5117024Ssam register struct vvreg *addr; 5127024Ssam struct mbuf *m; 51311192Ssam int ubainfo; 51411192Ssam int dest; 5157024Ssam 5167024Ssam if (vs->vs_oactive) 5177024Ssam goto restart; 5187024Ssam /* 5197024Ssam * Not already active: dequeue another request 5207024Ssam * and map it to the UNIBUS. If no more requests, 5217024Ssam * just return. 5227024Ssam */ 5237024Ssam IF_DEQUEUE(&vs->vs_if.if_snd, m); 52411209Ssam if (m == NULL) { 5257024Ssam vs->vs_oactive = 0; 5267024Ssam return; 5277024Ssam } 5287024Ssam dest = mtod(m, struct vv_header *)->vh_dhost; 5297024Ssam vs->vs_olen = if_wubaput(&vs->vs_ifuba, m); 5307024Ssam vs->vs_lastx = dest; 5317024Ssam restart: 5327024Ssam /* 5337024Ssam * Have request mapped to UNIBUS for transmission. 5347024Ssam * Purge any stale data from this BDP, and start the otput. 5357024Ssam */ 53612172Ssam if (vs->vs_olen > VVMTU + sizeof (struct vvheader)) { 53711192Ssam printf("vv%d vs_olen: %d > VVMTU\n", unit, vs->vs_olen); 53811192Ssam panic("vvdriver vs_olen botch"); 53911192Ssam } 5407024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 5417024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp); 5427024Ssam addr = (struct vvreg *)ui->ui_addr; 5437024Ssam ubainfo = vs->vs_ifuba.ifu_w.ifrw_info; 5447024Ssam addr->vvoba = (u_short) ubainfo; 5457024Ssam addr->vvoea = (u_short) (ubainfo >> 16); 5467024Ssam addr->vvowc = -((vs->vs_olen + 1) >> 1); 5477024Ssam addr->vvocsr = VV_IEN | VV_CPB | VV_DEN | VV_INR | VV_ENB; 5487024Ssam vs->vs_oactive = 1; 5497024Ssam } 5507024Ssam 5517024Ssam /* 5527024Ssam * VVLNI transmit interrupt 5537024Ssam * Start another output if more data to send. 5547024Ssam */ 5557024Ssam vvxint(unit) 5567024Ssam int unit; 5577024Ssam { 5587024Ssam register struct uba_device *ui = vvinfo[unit]; 5597024Ssam register struct vv_softc *vs = &vv_softc[unit]; 5607024Ssam register struct vvreg *addr; 5617024Ssam register int oc; 5627024Ssam 5637024Ssam addr = (struct vvreg *)ui->ui_addr; 5647024Ssam oc = 0xffff & (addr->vvocsr); 5657024Ssam if (vs->vs_oactive == 0) { 56611192Ssam printf("vv%d: stray interrupt vvocsr = %b\n", unit, 5677024Ssam oc, VV_OBITS); 5687024Ssam return; 5697024Ssam } 5707024Ssam if (oc & (VV_OPT | VV_RFS)) { 5717640Ssam vs->vs_if.if_collisions++; 57211192Ssam if (vs->vs_tries++ < VVRETRY) { 5737024Ssam if (oc & VV_OPT) 5747024Ssam vs->vs_init++; 5757024Ssam if (oc & VV_RFS) 5767024Ssam vs->vs_nottaken++; 57711192Ssam vvstart(unit); /* restart this message */ 5787024Ssam return; 5797024Ssam } 5807024Ssam if (oc & VV_OPT) 5817024Ssam printf("vv%d: output timeout\n"); 5827024Ssam } 5837024Ssam vs->vs_if.if_opackets++; 5847024Ssam vs->vs_oactive = 0; 5857024Ssam vs->vs_tries = 0; 5867024Ssam if (oc & VVXERR) { 5877024Ssam vs->vs_if.if_oerrors++; 58811192Ssam printf("vv%d: error vvocsr = %b\n", unit, 0xffff & oc, 5897024Ssam VV_OBITS); 5907024Ssam } 5917024Ssam if (vs->vs_ifuba.ifu_xtofree) { 5927024Ssam m_freem(vs->vs_ifuba.ifu_xtofree); 5937024Ssam vs->vs_ifuba.ifu_xtofree = 0; 5947024Ssam } 5957024Ssam if (vs->vs_if.if_snd.ifq_head == 0) { 59611192Ssam vs->vs_lastx = 256; /* an invalid address */ 5977024Ssam return; 5987024Ssam } 5997024Ssam vvstart(unit); 6007024Ssam } 6017024Ssam 6027024Ssam /* 6037024Ssam * V2lni interface receiver interrupt. 6047024Ssam * If input error just drop packet. 6057024Ssam * Otherwise purge input buffered data path and examine 6067024Ssam * packet to determine type. If can't determine length 6077024Ssam * from type, then have to drop packet. Othewise decapsulate 6087024Ssam * packet based on type and pass to type specific higher-level 6097024Ssam * input routine. 6107024Ssam */ 6117024Ssam vvrint(unit) 6127024Ssam int unit; 6137024Ssam { 6147024Ssam register struct vv_softc *vs = &vv_softc[unit]; 6157024Ssam struct vvreg *addr = (struct vvreg *)vvinfo[unit]->ui_addr; 6167024Ssam register struct vv_header *vv; 6177024Ssam register struct ifqueue *inq; 6187024Ssam struct mbuf *m; 6197024Ssam int ubainfo, len, off; 6207640Ssam short resid; 6217024Ssam 6227024Ssam vs->vs_if.if_ipackets++; 6237024Ssam /* 6247024Ssam * Purge BDP; drop if input error indicated. 6257024Ssam */ 6267024Ssam if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP) 6277024Ssam UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp); 6287024Ssam if (addr->vvicsr & VVRERR) { 62911192Ssam if (vv_logreaderrors) 63011192Ssam printf("vv%d: error vvicsr = %b\n", unit, 63111192Ssam 0xffff&(addr->vvicsr), VV_IBITS); 6327640Ssam goto dropit; 6337024Ssam } 6347640Ssam 6357024Ssam /* 6367640Ssam * Get packet length from word count residue 6377640Ssam * 6387640Ssam * Compute header offset if trailer protocol 6397640Ssam * 6407640Ssam * Pull packet off interface. Off is nonzero if packet 6417640Ssam * has trailing header; if_rubaget will then force this header 6427640Ssam * information to be at the front. The vh_info field 6437640Ssam * carries the offset to the trailer data in trailer 6447640Ssam * format packets. 6457024Ssam */ 6467640Ssam vv = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr); 64711192Ssam vvtracehdr("vi", vv); 6487640Ssam resid = addr->vviwc; 6497640Ssam if (resid) 6507640Ssam resid |= 0176000; /* ugly!!!! */ 6517640Ssam len = (((sizeof (struct vv_header) + VVMRU) >> 1) + resid) << 1; 6527640Ssam len -= sizeof(struct vv_header); 65311192Ssam if (len > VVMRU || len <= 0) 6547640Ssam goto dropit; 6557640Ssam #define vvdataaddr(vv, off, type) ((type)(((caddr_t)((vv)+1)+(off)))) 6567640Ssam if (vv_dotrailer && vv->vh_type >= RING_IPTrailer && 6577640Ssam vv->vh_type < RING_IPTrailer+RING_IPNTrailer){ 6587640Ssam off = (vv->vh_type - RING_IPTrailer) * 512; 6597640Ssam if (off > VVMTU) 6607640Ssam goto dropit; 6617640Ssam vv->vh_type = *vvdataaddr(vv, off, u_short *); 6627640Ssam resid = *(vvdataaddr(vv, off+2, u_short *)); 6637640Ssam if (off + resid > len) 6647640Ssam goto dropit; 6657640Ssam len = off + resid; 66611209Ssam } else 6677640Ssam off = 0; 6687640Ssam if (len == 0) 6697640Ssam goto dropit; 6707640Ssam m = if_rubaget(&vs->vs_ifuba, len, off); 67111209Ssam if (m == NULL) 6727640Ssam goto dropit; 6737640Ssam if (off) { 6747640Ssam m->m_off += 2 * sizeof(u_short); 6757640Ssam m->m_len -= 2 * sizeof(u_short); 6767640Ssam } 67711192Ssam 67811192Ssam /* 67911192Ssam * Demultiplex on packet type 68011192Ssam */ 6817024Ssam switch (vv->vh_type) { 68211192Ssam 6837024Ssam #ifdef INET 6847024Ssam case RING_IP: 6857024Ssam schednetisr(NETISR_IP); 6867024Ssam inq = &ipintrq; 6877024Ssam break; 6887024Ssam #endif 6897024Ssam default: 6907024Ssam printf("vv%d: unknown pkt type 0x%x\n", unit, vv->vh_type); 6917640Ssam m_freem(m); 6927024Ssam goto setup; 6937024Ssam } 6947640Ssam if (IF_QFULL(inq)) { 6957640Ssam IF_DROP(inq); 6967640Ssam m_freem(m); 69711209Ssam } else 6987640Ssam IF_ENQUEUE(inq, m); 6997024Ssam setup: 7007024Ssam /* 70111192Ssam * Check the error rate and start recovery if needed 70211192Ssam * this has to go here since the timer flywheel runs at 70311192Ssam * a lower ipl and never gets a chance to change the mode 7047024Ssam */ 70511192Ssam if (vs->vs_major == MODE0 && vs->vs_dropped > VV_ERRORTHRESHOLD) { 70611192Ssam vvtrprintf("vv%d going MODE1 in vvrint\n",unit); 70711192Ssam vs->vs_major = MODE1; 70811192Ssam vs->vs_iactive = PAUSE; /* no new reads */ 70911192Ssam vs->vs_retry = VV_MODE1ATTEMPTS; 71011192Ssam vs->vs_delayclock = VV_MODE1DELAY; 71111192Ssam vs->vs_minor = 0; 71211192Ssam vs->vs_dropped = 0; 71311192Ssam } 71411192Ssam switch (vs->vs_iactive) { 71511192Ssam 71611209Ssam case ACTIVE: /* Restart the read for next packet */ 71711192Ssam ubainfo = vs->vs_ifuba.ifu_r.ifrw_info; 71811192Ssam addr->vviba = (u_short) ubainfo; 71911192Ssam addr->vviea = (u_short) (ubainfo >> 16); 72011192Ssam addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1; 72111192Ssam addr->vvicsr = VV_RST | VV_CONF; 72211192Ssam addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB; 72311192Ssam return; 72411192Ssam 72511209Ssam case PAUSE: /* requested to not start any new reads */ 72611192Ssam vs->vs_dropped = 0; 72711192Ssam return; 72811192Ssam 72911209Ssam case OPEN: /* request to open host relay */ 73011192Ssam vs->vs_dropped = 0; 73111192Ssam addr->vvicsr = 0; 73211192Ssam return; 73311192Ssam 73411192Ssam default: 73511192Ssam printf("vv%d: vs_iactive = %d\n", unit, vs->vs_iactive); 73611192Ssam return; 73711192Ssam } 73811192Ssam /* 73911209Ssam * Drop packet on floor -- count them!! 74011192Ssam */ 7417640Ssam dropit: 7427640Ssam vs->vs_if.if_ierrors++; 74311192Ssam vs->vs_dropped++; 7447640Ssam /* 7457640Ssam printf("vv%d: error vvicsr = %b\n", unit, 7467640Ssam 0xffff&(addr->vvicsr), VV_IBITS); 7477640Ssam */ 7487640Ssam goto setup; 7497024Ssam } 7507024Ssam 7517024Ssam /* 7527024Ssam * V2lni output routine. 7537024Ssam * Encapsulate a packet of type family for the local net. 7547024Ssam * Use trailer local net encapsulation if enough data in first 7557024Ssam * packet leaves a multiple of 512 bytes of data in remainder. 7567024Ssam */ 7577024Ssam vvoutput(ifp, m0, dst) 7587024Ssam struct ifnet *ifp; 7597024Ssam struct mbuf *m0; 7607024Ssam struct sockaddr *dst; 7617024Ssam { 7627024Ssam register struct mbuf *m = m0; 7637024Ssam register struct vv_header *vv; 7647640Ssam register int off; 7657640Ssam int type, dest, s, error; 7667024Ssam 7677024Ssam switch (dst->sa_family) { 76811192Ssam 7697024Ssam #ifdef INET 7707024Ssam case AF_INET: { 7717640Ssam dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 77211192Ssam if ((dest = in_lnaof(*((struct in_addr *)&dest))) >= 0x100) { 7737640Ssam error = EPERM; 7747640Ssam goto bad; 7757640Ssam } 7767640Ssam off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 7777640Ssam if (vv_dotrailer && off > 0 && (off & 0x1ff) == 0 && 7787640Ssam m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 7797640Ssam type = RING_IPTrailer + (off>>9); 7807640Ssam m->m_off -= 2 * sizeof (u_short); 7817640Ssam m->m_len += 2 * sizeof (u_short); 7827640Ssam *mtod(m, u_short *) = RING_IP; 7837640Ssam *(mtod(m, u_short *) + 1) = m->m_len; 7847640Ssam goto gottrailertype; 7857640Ssam } 7867024Ssam type = RING_IP; 7877024Ssam off = 0; 7887024Ssam goto gottype; 7897024Ssam } 7907024Ssam #endif 7917024Ssam default: 7927024Ssam printf("vv%d: can't handle af%d\n", ifp->if_unit, 7937024Ssam dst->sa_family); 7947640Ssam error = EAFNOSUPPORT; 7957640Ssam goto bad; 7967024Ssam } 7977024Ssam 7987024Ssam gottrailertype: 7997024Ssam /* 8007024Ssam * Packet to be sent as trailer: move first packet 8017024Ssam * (control information) to end of chain. 8027024Ssam */ 8037024Ssam while (m->m_next) 8047024Ssam m = m->m_next; 8057024Ssam m->m_next = m0; 8067024Ssam m = m0->m_next; 8077024Ssam m0->m_next = 0; 8087024Ssam m0 = m; 8097024Ssam gottype: 8107024Ssam /* 8117024Ssam * Add local net header. If no space in first mbuf, 8127024Ssam * allocate another. 8137024Ssam */ 8147024Ssam if (m->m_off > MMAXOFF || 8157024Ssam MMINOFF + sizeof (struct vv_header) > m->m_off) { 81611209Ssam m = m_get(M_DONTWAIT, MT_HEADER); 81711209Ssam if (m == NULL) { 8187640Ssam error = ENOBUFS; 8197640Ssam goto bad; 8207024Ssam } 8217024Ssam m->m_next = m0; 8227024Ssam m->m_off = MMINOFF; 8237024Ssam m->m_len = sizeof (struct vv_header); 8247024Ssam } else { 8257024Ssam m->m_off -= sizeof (struct vv_header); 8267024Ssam m->m_len += sizeof (struct vv_header); 8277024Ssam } 8287024Ssam vv = mtod(m, struct vv_header *); 8297024Ssam vv->vh_shost = ifp->if_host[0]; 8307024Ssam vv->vh_dhost = dest; 8317024Ssam vv->vh_version = RING_VERSION; 8327024Ssam vv->vh_type = type; 8337640Ssam vv->vh_info = off; 83411192Ssam vvtracehdr("vo", vv); 8357024Ssam 8367024Ssam /* 8377024Ssam * Queue message on interface, and start output if interface 8387024Ssam * not yet active. 8397024Ssam */ 8407024Ssam s = splimp(); 8417640Ssam if (IF_QFULL(&ifp->if_snd)) { 8427640Ssam IF_DROP(&ifp->if_snd); 8437640Ssam error = ENOBUFS; 8447640Ssam goto qfull; 8457640Ssam } 8467024Ssam IF_ENQUEUE(&ifp->if_snd, m); 8477024Ssam if (vv_softc[ifp->if_unit].vs_oactive == 0) 8487024Ssam vvstart(ifp->if_unit); 8497024Ssam splx(s); 8507640Ssam return (0); 8517640Ssam qfull: 8527640Ssam m0 = m; 8537640Ssam splx(s); 8547640Ssam bad: 8557640Ssam m_freem(m0); 8567640Ssam return(error); 8577024Ssam } 8587024Ssam 8597024Ssam /* 8607024Ssam * vvprt_hdr(s, v) print the local net header in "v" 8617024Ssam * with title is "s" 8627024Ssam */ 8637024Ssam vvprt_hdr(s, v) 8647024Ssam char *s; 8657024Ssam register struct vv_header *v; 8667024Ssam { 8677024Ssam printf("%s: dsvti: 0x%x 0x%x 0x%x 0x%x 0x%x\n", 8687024Ssam s, 8697024Ssam 0xff & (int)(v->vh_dhost), 0xff & (int)(v->vh_shost), 8707024Ssam 0xff & (int)(v->vh_version), 0xff & (int)(v->vh_type), 8717024Ssam 0xffff & (int)(v->vh_info)); 8727024Ssam } 8737024Ssam 8747024Ssam /* 8757024Ssam * print "l" hex bytes starting at "s" 8767024Ssam */ 8777024Ssam vvprt_hex(s, l) 8787024Ssam char *s; 8797024Ssam int l; 8807024Ssam { 8817024Ssam register int i; 8827024Ssam register int z; 8837024Ssam 8847024Ssam for (i=0 ; i < l; i++) { 8857024Ssam z = 0xff & (int)(*(s + i)); 8867024Ssam printf("%c%c ", 8877024Ssam "0123456789abcdef"[(z >> 4) & 0x0f], 8887024Ssam "0123456789abcdef"[z & 0x0f] 8897024Ssam ); 8907024Ssam } 8917024Ssam } 892