1*17115Sbloom /* if_il.c 6.5 84/08/29 */ 26893Sfeldman 36893Sfeldman #include "il.h" 46893Sfeldman 56893Sfeldman /* 66893Sfeldman * Interlan Ethernet Communications Controller interface 76893Sfeldman */ 89797Ssam #include "../machine/pte.h" 99797Ssam 10*17115Sbloom #include "param.h" 11*17115Sbloom #include "systm.h" 12*17115Sbloom #include "mbuf.h" 13*17115Sbloom #include "buf.h" 14*17115Sbloom #include "protosw.h" 15*17115Sbloom #include "socket.h" 16*17115Sbloom #include "vmmac.h" 17*17115Sbloom #include "ioctl.h" 18*17115Sbloom #include "errno.h" 198463Sroot 208463Sroot #include "../net/if.h" 218463Sroot #include "../net/netisr.h" 228463Sroot #include "../net/route.h" 238419Swnj #include "../netinet/in.h" 248419Swnj #include "../netinet/in_systm.h" 258419Swnj #include "../netinet/ip.h" 268419Swnj #include "../netinet/ip_var.h" 2711575Ssam #include "../netinet/if_ether.h" 288419Swnj #include "../netpup/pup.h" 296893Sfeldman 308463Sroot #include "../vax/cpu.h" 318463Sroot #include "../vax/mtpr.h" 32*17115Sbloom #include "if_il.h" 33*17115Sbloom #include "if_ilreg.h" 34*17115Sbloom #include "if_uba.h" 358463Sroot #include "../vaxuba/ubareg.h" 368463Sroot #include "../vaxuba/ubavar.h" 378463Sroot 386893Sfeldman int ilprobe(), ilattach(), ilrint(), ilcint(); 396893Sfeldman struct uba_device *ilinfo[NIL]; 406893Sfeldman u_short ilstd[] = { 0 }; 416893Sfeldman struct uba_driver ildriver = 426893Sfeldman { ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo }; 436893Sfeldman #define ILUNIT(x) minor(x) 4413056Ssam int ilinit(),iloutput(),ilioctl(),ilreset(),ilwatch(); 456893Sfeldman 466893Sfeldman /* 476893Sfeldman * Ethernet software status per interface. 486893Sfeldman * 496893Sfeldman * Each interface is referenced by a network interface structure, 506893Sfeldman * is_if, which the routing code uses to locate the interface. 516893Sfeldman * This structure contains the output queue for the interface, its address, ... 526893Sfeldman * We also have, for each interface, a UBA interface structure, which 536893Sfeldman * contains information about the UNIBUS resources held by the interface: 546893Sfeldman * map registers, buffered data paths, etc. Information is cached in this 556893Sfeldman * structure for use by the if_uba.c routines in running the interface 566893Sfeldman * efficiently. 576893Sfeldman */ 586893Sfeldman struct il_softc { 5911575Ssam struct arpcom is_ac; /* Ethernet common part */ 6011575Ssam #define is_if is_ac.ac_if /* network-visible interface */ 6111575Ssam #define is_addr is_ac.ac_enaddr /* hardware Ethernet address */ 626893Sfeldman struct ifuba is_ifuba; /* UNIBUS resources */ 637261Ssam int is_flags; 647261Ssam #define ILF_OACTIVE 0x1 /* output is active */ 657261Ssam #define ILF_RCVPENDING 0x2 /* start rcv in ilcint */ 667261Ssam #define ILF_STATPENDING 0x4 /* stat cmd pending */ 677261Ssam short is_lastcmd; /* can't read csr, so must save it */ 687261Ssam short is_scaninterval; /* interval of stat collection */ 697261Ssam #define ILWATCHINTERVAL 60 /* once every 60 seconds */ 707261Ssam struct il_stats is_stats; /* holds on-board statistics */ 717261Ssam struct il_stats is_sum; /* summation over time */ 727261Ssam int is_ubaddr; /* mapping registers of is_stats */ 736893Sfeldman } il_softc[NIL]; 746893Sfeldman 756893Sfeldman ilprobe(reg) 766893Sfeldman caddr_t reg; 776893Sfeldman { 786893Sfeldman register int br, cvec; /* r11, r10 value-result */ 796893Sfeldman register struct ildevice *addr = (struct ildevice *)reg; 806893Sfeldman register i; 816893Sfeldman 826893Sfeldman #ifdef lint 836893Sfeldman br = 0; cvec = br; br = cvec; 849179Ssam i = 0; ilrint(i); ilcint(i); ilwatch(i); 856893Sfeldman #endif 866893Sfeldman 876893Sfeldman addr->il_csr = ILC_OFFLINE|IL_CIE; 886893Sfeldman DELAY(100000); 897261Ssam i = addr->il_csr; /* clear CDONE */ 906893Sfeldman if (cvec > 0 && cvec != 0x200) 916893Sfeldman cvec -= 4; 926893Sfeldman return (1); 936893Sfeldman } 946893Sfeldman 956893Sfeldman /* 966893Sfeldman * Interface exists: make available by filling in network interface 976893Sfeldman * record. System will initialize the interface when it is ready 986893Sfeldman * to accept packets. A STATUS command is done to get the ethernet 996893Sfeldman * address and other interesting data. 1006893Sfeldman */ 1016893Sfeldman ilattach(ui) 1026893Sfeldman struct uba_device *ui; 1036893Sfeldman { 1046893Sfeldman register struct il_softc *is = &il_softc[ui->ui_unit]; 1057220Ssam register struct ifnet *ifp = &is->is_if; 1066893Sfeldman register struct ildevice *addr = (struct ildevice *)ui->ui_addr; 1077220Ssam struct sockaddr_in *sin; 1086893Sfeldman 1097220Ssam ifp->if_unit = ui->ui_unit; 1107220Ssam ifp->if_name = "il"; 1119746Ssam ifp->if_mtu = ETHERMTU; 1126893Sfeldman 1136893Sfeldman /* 1147261Ssam * Reset the board and map the statistics 1157261Ssam * buffer onto the Unibus. 1166893Sfeldman */ 1177261Ssam addr->il_csr = ILC_RESET; 1187261Ssam while ((addr->il_csr&IL_CDONE) == 0) 1197261Ssam ; 1207261Ssam if (addr->il_csr&IL_STATUS) 1217261Ssam printf("il%d: reset failed, csr=%b\n", ui->ui_unit, 1227261Ssam addr->il_csr, IL_BITS); 1236893Sfeldman 1249179Ssam is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats, 12513056Ssam sizeof (struct il_stats), 0); 1267261Ssam addr->il_bar = is->is_ubaddr & 0xffff; 1277261Ssam addr->il_bcr = sizeof (struct il_stats); 1287261Ssam addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT; 1297261Ssam while ((addr->il_csr&IL_CDONE) == 0) 1307261Ssam ; 1317261Ssam if (addr->il_csr&IL_STATUS) 1327261Ssam printf("il%d: status failed, csr=%b\n", ui->ui_unit, 1337261Ssam addr->il_csr, IL_BITS); 1347261Ssam ubarelse(ui->ui_ubanum, &is->is_ubaddr); 13511575Ssam #ifdef notdef 1366893Sfeldman printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n", 1376893Sfeldman ui->ui_unit, 1387261Ssam is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff, 1397261Ssam is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff, 1407261Ssam is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff, 1417261Ssam is->is_stats.ils_module, is->is_stats.ils_firmware); 14211575Ssam #endif 14316218Skarels is->is_addr = is->is_stats.ils_addr; 1447220Ssam sin = (struct sockaddr_in *)&ifp->if_addr; 1456893Sfeldman sin->sin_family = AF_INET; 1467220Ssam ifp->if_init = ilinit; 1477220Ssam ifp->if_output = iloutput; 14813056Ssam ifp->if_ioctl = ilioctl; 1498979Sroot ifp->if_reset = ilreset; 1506893Sfeldman is->is_ifuba.ifu_flags = UBA_CANTWAIT; 1517220Ssam #ifdef notdef 1527220Ssam is->is_ifuba.ifu_flags |= UBA_NEEDBDP; 1537220Ssam #endif 1547220Ssam if_attach(ifp); 1556893Sfeldman } 1566893Sfeldman 1576893Sfeldman /* 1586893Sfeldman * Reset of interface after UNIBUS reset. 1596893Sfeldman * If interface is on specified uba, reset its state. 1606893Sfeldman */ 1616893Sfeldman ilreset(unit, uban) 1626893Sfeldman int unit, uban; 1636893Sfeldman { 1646893Sfeldman register struct uba_device *ui; 1656893Sfeldman 1666893Sfeldman if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 || 1676893Sfeldman ui->ui_ubanum != uban) 1686893Sfeldman return; 1696893Sfeldman printf(" il%d", unit); 1706893Sfeldman ilinit(unit); 1716893Sfeldman } 1726893Sfeldman 1736893Sfeldman /* 1746893Sfeldman * Initialization of interface; clear recorded pending 1756893Sfeldman * operations, and reinitialize UNIBUS usage. 1766893Sfeldman */ 1776893Sfeldman ilinit(unit) 1786893Sfeldman int unit; 1796893Sfeldman { 1806893Sfeldman register struct il_softc *is = &il_softc[unit]; 1816893Sfeldman register struct uba_device *ui = ilinfo[unit]; 1826893Sfeldman register struct ildevice *addr; 18313056Ssam register struct ifnet *ifp = &is->is_if; 18413056Ssam register struct sockaddr_in *sin; 1857261Ssam int s; 1866893Sfeldman 18711575Ssam sin = (struct sockaddr_in *)&ifp->if_addr; 18813056Ssam if (sin->sin_addr.s_addr == 0) /* address still unknown */ 18911575Ssam return; 19011575Ssam 19113056Ssam if (ifp->if_flags & IFF_RUNNING) 19213056Ssam goto justarp; 1936893Sfeldman if (if_ubainit(&is->is_ifuba, ui->ui_ubanum, 1949746Ssam sizeof (struct il_rheader), (int)btoc(ETHERMTU)) == 0) { 1956893Sfeldman printf("il%d: can't initialize\n", unit); 1966893Sfeldman is->is_if.if_flags &= ~IFF_UP; 1976893Sfeldman return; 1986893Sfeldman } 1999179Ssam is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats, 20013056Ssam sizeof (struct il_stats), 0); 20115071Skarels ifp->if_watchdog = ilwatch; 20215071Skarels is->is_scaninterval = ILWATCHINTERVAL; 20315071Skarels ifp->if_timer = is->is_scaninterval; 2046893Sfeldman addr = (struct ildevice *)ui->ui_addr; 2056893Sfeldman 2066893Sfeldman /* 2079179Ssam * Turn off source address insertion (it's faster this way), 20812488Ssam * and set board online. Former doesn't work if board is 20912488Ssam * already online (happens on ubareset), so we put it offline 21012488Ssam * first. 2119179Ssam */ 2129179Ssam s = splimp(); 21312488Ssam addr->il_csr = ILC_OFFLINE; 2149179Ssam while ((addr->il_csr & IL_CDONE) == 0) 2159179Ssam ; 21612488Ssam addr->il_csr = ILC_CISA; 2179179Ssam while ((addr->il_csr & IL_CDONE) == 0) 2189179Ssam ; 2199179Ssam /* 2206893Sfeldman * Set board online. 2216893Sfeldman * Hang receive buffer and start any pending 2226893Sfeldman * writes by faking a transmit complete. 2236893Sfeldman * Receive bcr is not a muliple of 4 so buffer 2246893Sfeldman * chaining can't happen. 2256893Sfeldman */ 2266893Sfeldman addr->il_csr = ILC_ONLINE; 2277220Ssam while ((addr->il_csr & IL_CDONE) == 0) 2287220Ssam ; 2296893Sfeldman addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff; 2309746Ssam addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6; 2317261Ssam addr->il_csr = 23213056Ssam ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE; 2337220Ssam while ((addr->il_csr & IL_CDONE) == 0) 2347220Ssam ; 2357261Ssam is->is_flags = ILF_OACTIVE; 23613060Ssam is->is_if.if_flags |= IFF_UP|IFF_RUNNING; 2377261Ssam is->is_lastcmd = 0; 2386893Sfeldman ilcint(unit); 2396893Sfeldman splx(s); 24013056Ssam justarp: 2417152Swnj if_rtinit(&is->is_if, RTF_UP); 24211575Ssam arpwhohas(&is->is_ac, &sin->sin_addr); 2436893Sfeldman } 2446893Sfeldman 2456893Sfeldman /* 2466893Sfeldman * Start output on interface. 2476893Sfeldman * Get another datagram to send off of the interface queue, 2486893Sfeldman * and map it to the interface before starting the output. 2496893Sfeldman */ 2506893Sfeldman ilstart(dev) 2516893Sfeldman dev_t dev; 2526893Sfeldman { 2539179Ssam int unit = ILUNIT(dev), len; 2546893Sfeldman struct uba_device *ui = ilinfo[unit]; 2556893Sfeldman register struct il_softc *is = &il_softc[unit]; 2566893Sfeldman register struct ildevice *addr; 2576893Sfeldman struct mbuf *m; 2587220Ssam short csr; 2596893Sfeldman 2606893Sfeldman IF_DEQUEUE(&is->is_if.if_snd, m); 2617261Ssam addr = (struct ildevice *)ui->ui_addr; 2627261Ssam if (m == 0) { 2637261Ssam if ((is->is_flags & ILF_STATPENDING) == 0) 2647261Ssam return; 2659179Ssam addr->il_bar = is->is_ubaddr & 0xffff; 2667261Ssam addr->il_bcr = sizeof (struct il_stats); 2677261Ssam csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE; 2687261Ssam is->is_flags &= ~ILF_STATPENDING; 2697261Ssam goto startcmd; 2707261Ssam } 2716893Sfeldman len = if_wubaput(&is->is_ifuba, m); 2729179Ssam /* 2739179Ssam * Ensure minimum packet length. 2749179Ssam * This makes the safe assumtion that there are no virtual holes 2759179Ssam * after the data. 2769179Ssam * For security, it might be wise to zero out the added bytes, 2779179Ssam * but we're mainly interested in speed at the moment. 2789179Ssam */ 2799746Ssam if (len - sizeof(struct ether_header) < ETHERMIN) 2809746Ssam len = ETHERMIN + sizeof(struct ether_header); 2816893Sfeldman if (is->is_ifuba.ifu_flags & UBA_NEEDBDP) 2826893Sfeldman UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp); 2836893Sfeldman addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff; 2846893Sfeldman addr->il_bcr = len; 2857261Ssam csr = 2867261Ssam ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE; 2877261Ssam 2887261Ssam startcmd: 2897261Ssam is->is_lastcmd = csr & IL_CMD; 2907220Ssam addr->il_csr = csr; 2917261Ssam is->is_flags |= ILF_OACTIVE; 2926893Sfeldman } 2936893Sfeldman 2946893Sfeldman /* 2956893Sfeldman * Command done interrupt. 2966893Sfeldman */ 2976893Sfeldman ilcint(unit) 2986893Sfeldman int unit; 2996893Sfeldman { 3006893Sfeldman register struct il_softc *is = &il_softc[unit]; 3017220Ssam struct uba_device *ui = ilinfo[unit]; 3026893Sfeldman register struct ildevice *addr = (struct ildevice *)ui->ui_addr; 3037266Ssam short csr; 3046893Sfeldman 3057261Ssam if ((is->is_flags & ILF_OACTIVE) == 0) { 3067220Ssam printf("il%d: stray xmit interrupt, csr=%b\n", unit, 3077261Ssam addr->il_csr, IL_BITS); 3086893Sfeldman return; 3096893Sfeldman } 3107220Ssam 3117266Ssam csr = addr->il_csr; 3126893Sfeldman /* 3137261Ssam * Hang receive buffer if it couldn't 3147261Ssam * be done earlier (in ilrint). 3156893Sfeldman */ 3167261Ssam if (is->is_flags & ILF_RCVPENDING) { 3176893Sfeldman addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff; 3189746Ssam addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6; 3197261Ssam addr->il_csr = 3207261Ssam ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE; 3217220Ssam while ((addr->il_csr & IL_CDONE) == 0) 3227220Ssam ; 3237261Ssam is->is_flags &= ~ILF_RCVPENDING; 3246893Sfeldman } 3257261Ssam is->is_flags &= ~ILF_OACTIVE; 3267266Ssam csr &= IL_STATUS; 3277261Ssam switch (is->is_lastcmd) { 3287261Ssam 3297261Ssam case ILC_XMIT: 3307261Ssam is->is_if.if_opackets++; 3317266Ssam if (csr > ILERR_RETRIES) 3327261Ssam is->is_if.if_oerrors++; 3337261Ssam break; 3347261Ssam 3357261Ssam case ILC_STAT: 3367266Ssam if (csr == ILERR_SUCCESS) 3377261Ssam iltotal(is); 3387261Ssam break; 3397261Ssam } 3406893Sfeldman if (is->is_ifuba.ifu_xtofree) { 3416893Sfeldman m_freem(is->is_ifuba.ifu_xtofree); 3426893Sfeldman is->is_ifuba.ifu_xtofree = 0; 3436893Sfeldman } 3447261Ssam ilstart(unit); 3456893Sfeldman } 3466893Sfeldman 3476893Sfeldman /* 3486893Sfeldman * Ethernet interface receiver interrupt. 3496893Sfeldman * If input error just drop packet. 3506893Sfeldman * Otherwise purge input buffered data path and examine 3516893Sfeldman * packet to determine type. If can't determine length 3526893Sfeldman * from type, then have to drop packet. Othewise decapsulate 3536893Sfeldman * packet based on type and pass to type specific higher-level 3546893Sfeldman * input routine. 3556893Sfeldman */ 3566893Sfeldman ilrint(unit) 3576893Sfeldman int unit; 3586893Sfeldman { 3596893Sfeldman register struct il_softc *is = &il_softc[unit]; 3606893Sfeldman struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr; 3616893Sfeldman register struct il_rheader *il; 3626893Sfeldman struct mbuf *m; 36315787Sleres int len, off, resid, s; 3646893Sfeldman register struct ifqueue *inq; 3656893Sfeldman 3666893Sfeldman is->is_if.if_ipackets++; 3676893Sfeldman if (is->is_ifuba.ifu_flags & UBA_NEEDBDP) 3686893Sfeldman UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp); 3696893Sfeldman il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr); 3706893Sfeldman len = il->ilr_length - sizeof(struct il_rheader); 3719746Ssam if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 || 3729746Ssam len > ETHERMTU) { 3736893Sfeldman is->is_if.if_ierrors++; 3746893Sfeldman #ifdef notdef 3756893Sfeldman if (is->is_if.if_ierrors % 100 == 0) 3766893Sfeldman printf("il%d: += 100 input errors\n", unit); 3776893Sfeldman #endif 3786893Sfeldman goto setup; 3796893Sfeldman } 3806893Sfeldman 3816893Sfeldman /* 3826893Sfeldman * Deal with trailer protocol: if type is PUP trailer 3836893Sfeldman * get true type from first 16-bit word past data. 3846893Sfeldman * Remember that type was trailer by setting off. 3856893Sfeldman */ 3869746Ssam il->ilr_type = ntohs((u_short)il->ilr_type); 3876893Sfeldman #define ildataaddr(il, off, type) ((type)(((caddr_t)((il)+1)+(off)))) 3889746Ssam if (il->ilr_type >= ETHERPUP_TRAIL && 3899746Ssam il->ilr_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) { 3909746Ssam off = (il->ilr_type - ETHERPUP_TRAIL) * 512; 3919746Ssam if (off >= ETHERMTU) 3926893Sfeldman goto setup; /* sanity */ 3939746Ssam il->ilr_type = ntohs(*ildataaddr(il, off, u_short *)); 3949746Ssam resid = ntohs(*(ildataaddr(il, off+2, u_short *))); 3956893Sfeldman if (off + resid > len) 3966893Sfeldman goto setup; /* sanity */ 3976893Sfeldman len = off + resid; 3986893Sfeldman } else 3996893Sfeldman off = 0; 4006893Sfeldman if (len == 0) 4016893Sfeldman goto setup; 4026893Sfeldman 4036893Sfeldman /* 4046893Sfeldman * Pull packet off interface. Off is nonzero if packet 4056893Sfeldman * has trailing header; ilget will then force this header 4066893Sfeldman * information to be at the front, but we still have to drop 4076893Sfeldman * the type and length which are at the front of any trailer data. 4086893Sfeldman */ 4096893Sfeldman m = if_rubaget(&is->is_ifuba, len, off); 4106893Sfeldman if (m == 0) 4116893Sfeldman goto setup; 4126893Sfeldman if (off) { 4136893Sfeldman m->m_off += 2 * sizeof (u_short); 4146893Sfeldman m->m_len -= 2 * sizeof (u_short); 4156893Sfeldman } 4166893Sfeldman switch (il->ilr_type) { 4176893Sfeldman 4186893Sfeldman #ifdef INET 4199746Ssam case ETHERPUP_IPTYPE: 4206893Sfeldman schednetisr(NETISR_IP); 4216893Sfeldman inq = &ipintrq; 4226893Sfeldman break; 42311575Ssam 42411575Ssam case ETHERPUP_ARPTYPE: 42511575Ssam arpinput(&is->is_ac, m); 42613988Ssam goto setup; 4276893Sfeldman #endif 4286893Sfeldman default: 4296893Sfeldman m_freem(m); 4306893Sfeldman goto setup; 4316893Sfeldman } 4326893Sfeldman 43315787Sleres s = splimp(); 4346893Sfeldman if (IF_QFULL(inq)) { 4356893Sfeldman IF_DROP(inq); 4366893Sfeldman m_freem(m); 43715787Sleres } else 43815787Sleres IF_ENQUEUE(inq, m); 43915787Sleres splx(s); 4406893Sfeldman 4416893Sfeldman setup: 4426893Sfeldman /* 4436893Sfeldman * Reset for next packet if possible. 4446893Sfeldman * If waiting for transmit command completion, set flag 4456893Sfeldman * and wait until command completes. 4466893Sfeldman */ 4477261Ssam if (is->is_flags & ILF_OACTIVE) { 4487261Ssam is->is_flags |= ILF_RCVPENDING; 4497220Ssam return; 4507220Ssam } 4517220Ssam addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff; 4529746Ssam addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6; 4537261Ssam addr->il_csr = 4547261Ssam ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE; 4557220Ssam while ((addr->il_csr & IL_CDONE) == 0) 4567220Ssam ; 4576893Sfeldman } 4586893Sfeldman 4596893Sfeldman /* 4606893Sfeldman * Ethernet output routine. 4616893Sfeldman * Encapsulate a packet of type family for the local net. 4626893Sfeldman * Use trailer local net encapsulation if enough data in first 4636893Sfeldman * packet leaves a multiple of 512 bytes of data in remainder. 4646893Sfeldman */ 4656893Sfeldman iloutput(ifp, m0, dst) 4666893Sfeldman struct ifnet *ifp; 4676893Sfeldman struct mbuf *m0; 4686893Sfeldman struct sockaddr *dst; 4696893Sfeldman { 47011575Ssam int type, s, error; 47116218Skarels struct ether_addr edst; 47211575Ssam struct in_addr idst; 4736893Sfeldman register struct il_softc *is = &il_softc[ifp->if_unit]; 4746893Sfeldman register struct mbuf *m = m0; 4759746Ssam register struct ether_header *il; 4769179Ssam register int off; 4776893Sfeldman 4786893Sfeldman switch (dst->sa_family) { 4796893Sfeldman 4806893Sfeldman #ifdef INET 4816893Sfeldman case AF_INET: 48211575Ssam idst = ((struct sockaddr_in *)dst)->sin_addr; 48316218Skarels if (!arpresolve(&is->is_ac, m, &idst, &edst)) 48411575Ssam return (0); /* if not yet resolved */ 4856893Sfeldman off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 48613056Ssam /* need per host negotiation */ 48713056Ssam if ((ifp->if_flags & IFF_NOTRAILERS) == 0) 4886893Sfeldman if (off > 0 && (off & 0x1ff) == 0 && 4896893Sfeldman m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 4909746Ssam type = ETHERPUP_TRAIL + (off>>9); 4916893Sfeldman m->m_off -= 2 * sizeof (u_short); 4926893Sfeldman m->m_len += 2 * sizeof (u_short); 4939746Ssam *mtod(m, u_short *) = htons((u_short)ETHERPUP_IPTYPE); 4949746Ssam *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 4956893Sfeldman goto gottrailertype; 4966893Sfeldman } 4979746Ssam type = ETHERPUP_IPTYPE; 4986893Sfeldman off = 0; 4996893Sfeldman goto gottype; 5006893Sfeldman #endif 5016893Sfeldman 50211575Ssam case AF_UNSPEC: 50311575Ssam il = (struct ether_header *)dst->sa_data; 50416218Skarels edst = il->ether_dhost; 50511575Ssam type = il->ether_type; 50611575Ssam goto gottype; 50711575Ssam 5086893Sfeldman default: 5096893Sfeldman printf("il%d: can't handle af%d\n", ifp->if_unit, 5106893Sfeldman dst->sa_family); 5116893Sfeldman error = EAFNOSUPPORT; 5126893Sfeldman goto bad; 5136893Sfeldman } 5146893Sfeldman 5156893Sfeldman gottrailertype: 5166893Sfeldman /* 5176893Sfeldman * Packet to be sent as trailer: move first packet 5186893Sfeldman * (control information) to end of chain. 5196893Sfeldman */ 5206893Sfeldman while (m->m_next) 5216893Sfeldman m = m->m_next; 5226893Sfeldman m->m_next = m0; 5236893Sfeldman m = m0->m_next; 5246893Sfeldman m0->m_next = 0; 5256893Sfeldman m0 = m; 5266893Sfeldman 5276893Sfeldman gottype: 5286893Sfeldman /* 5296893Sfeldman * Add local net header. If no space in first mbuf, 5306893Sfeldman * allocate another. 5316893Sfeldman */ 5326893Sfeldman if (m->m_off > MMAXOFF || 5339746Ssam MMINOFF + sizeof (struct ether_header) > m->m_off) { 5349650Ssam m = m_get(M_DONTWAIT, MT_HEADER); 5356893Sfeldman if (m == 0) { 5366893Sfeldman error = ENOBUFS; 5376893Sfeldman goto bad; 5386893Sfeldman } 5396893Sfeldman m->m_next = m0; 5406893Sfeldman m->m_off = MMINOFF; 5419746Ssam m->m_len = sizeof (struct ether_header); 5426893Sfeldman } else { 5439746Ssam m->m_off -= sizeof (struct ether_header); 5449746Ssam m->m_len += sizeof (struct ether_header); 5456893Sfeldman } 5469746Ssam il = mtod(m, struct ether_header *); 5479746Ssam il->ether_type = htons((u_short)type); 54816218Skarels il->ether_dhost = edst; 54916218Skarels il->ether_shost = is->is_addr; 5506893Sfeldman 5516893Sfeldman /* 5526893Sfeldman * Queue message on interface, and start output if interface 5536893Sfeldman * not yet active. 5546893Sfeldman */ 5556893Sfeldman s = splimp(); 5566893Sfeldman if (IF_QFULL(&ifp->if_snd)) { 5576893Sfeldman IF_DROP(&ifp->if_snd); 5587220Ssam splx(s); 5597220Ssam m_freem(m); 5607220Ssam return (ENOBUFS); 5616893Sfeldman } 5626893Sfeldman IF_ENQUEUE(&ifp->if_snd, m); 5637261Ssam if ((is->is_flags & ILF_OACTIVE) == 0) 5646893Sfeldman ilstart(ifp->if_unit); 5656893Sfeldman splx(s); 5666893Sfeldman return (0); 5677220Ssam 5686893Sfeldman bad: 5696893Sfeldman m_freem(m0); 5707220Ssam return (error); 5716893Sfeldman } 5727261Ssam 5737261Ssam /* 5747261Ssam * Watchdog routine, request statistics from board. 5757261Ssam */ 5767261Ssam ilwatch(unit) 5777261Ssam int unit; 5787261Ssam { 5797261Ssam register struct il_softc *is = &il_softc[unit]; 5807261Ssam register struct ifnet *ifp = &is->is_if; 5817261Ssam int s; 5827261Ssam 5837261Ssam if (is->is_flags & ILF_STATPENDING) { 5847261Ssam ifp->if_timer = is->is_scaninterval; 5857261Ssam return; 5867261Ssam } 5877261Ssam s = splimp(); 5887261Ssam is->is_flags |= ILF_STATPENDING; 5897261Ssam if ((is->is_flags & ILF_OACTIVE) == 0) 5907261Ssam ilstart(ifp->if_unit); 5917261Ssam splx(s); 5927261Ssam ifp->if_timer = is->is_scaninterval; 5937261Ssam } 5947261Ssam 5957261Ssam /* 5967261Ssam * Total up the on-board statistics. 5977261Ssam */ 5987261Ssam iltotal(is) 5997261Ssam register struct il_softc *is; 6007261Ssam { 6017261Ssam register u_short *interval, *sum, *end; 6027261Ssam 6037261Ssam interval = &is->is_stats.ils_frames; 6047261Ssam sum = &is->is_sum.ils_frames; 6057261Ssam end = is->is_sum.ils_fill2; 6067261Ssam while (sum < end) 6077261Ssam *sum++ += *interval++; 6087261Ssam is->is_if.if_collisions = is->is_sum.ils_collis; 6097261Ssam } 61013056Ssam 61113056Ssam /* 61213056Ssam * Process an ioctl request. 61313056Ssam */ 61413056Ssam ilioctl(ifp, cmd, data) 61513056Ssam register struct ifnet *ifp; 61613056Ssam int cmd; 61713056Ssam caddr_t data; 61813056Ssam { 61913056Ssam register struct ifreq *ifr = (struct ifreq *)data; 62013056Ssam int s = splimp(), error = 0; 62113056Ssam 62213056Ssam switch (cmd) { 62313056Ssam 62413056Ssam case SIOCSIFADDR: 62513056Ssam if (ifp->if_flags & IFF_RUNNING) 62613056Ssam if_rtinit(ifp, -1); /* delete previous route */ 62713063Ssam ilsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); 62813056Ssam ilinit(ifp->if_unit); 62913056Ssam break; 63013056Ssam 63113056Ssam default: 63213056Ssam error = EINVAL; 63313056Ssam } 63413056Ssam splx(s); 63513056Ssam return (error); 63613056Ssam } 63713063Ssam 63813089Ssam ilsetaddr(ifp, sin) 63913063Ssam register struct ifnet *ifp; 64013063Ssam register struct sockaddr_in *sin; 64113063Ssam { 64213063Ssam 64313063Ssam ifp->if_addr = *(struct sockaddr *)sin; 64413063Ssam ifp->if_net = in_netof(sin->sin_addr); 64513063Ssam ifp->if_host[0] = in_lnaof(sin->sin_addr); 64613063Ssam sin = (struct sockaddr_in *)&ifp->if_broadaddr; 64713063Ssam sin->sin_family = AF_INET; 64813063Ssam sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); 64913063Ssam ifp->if_flags |= IFF_BROADCAST; 65013063Ssam } 651