1*5927Sroot /* if_en.c 4.34 82/02/21 */ 24686Swnj 34686Swnj #include "en.h" 45105Swnj 54686Swnj /* 65105Swnj * Xerox prototype (3 Mb) Ethernet interface driver. 74686Swnj */ 84686Swnj 94686Swnj #include "../h/param.h" 104686Swnj #include "../h/systm.h" 114686Swnj #include "../h/mbuf.h" 124686Swnj #include "../h/pte.h" 134686Swnj #include "../h/buf.h" 145083Swnj #include "../h/protosw.h" 155083Swnj #include "../h/socket.h" 164686Swnj #include "../h/ubareg.h" 174686Swnj #include "../h/ubavar.h" 184686Swnj #include "../h/enreg.h" 195083Swnj #include "../h/cpu.h" 204686Swnj #include "../h/mtpr.h" 215083Swnj #include "../h/vmmac.h" 225083Swnj #include "../net/in.h" 235083Swnj #include "../net/in_systm.h" 245083Swnj #include "../net/if.h" 255083Swnj #include "../net/if_en.h" 265083Swnj #include "../net/if_uba.h" 275083Swnj #include "../net/ip.h" 285083Swnj #include "../net/ip_var.h" 294686Swnj 305213Swnj #define ENMTU (1024+512) 315083Swnj 324686Swnj int enprobe(), enattach(), enrint(), enxint(), encollide(); 334686Swnj struct uba_device *eninfo[NEN]; 344686Swnj u_short enstd[] = { 0 }; 354686Swnj struct uba_driver endriver = 365171Swnj { enprobe, 0, enattach, 0, enstd, "en", eninfo }; 374686Swnj #define ENUNIT(x) minor(x) 384686Swnj 395105Swnj int eninit(),enoutput(),enreset(); 405105Swnj 415105Swnj /* 425105Swnj * Ethernet software status per interface. 435105Swnj * 445105Swnj * Each interface is referenced by a network interface structure, 455105Swnj * es_if, which the routing code uses to locate the interface. 465105Swnj * This structure contains the output queue for the interface, its address, ... 475105Swnj * We also have, for each interface, a UBA interface structure, which 485105Swnj * contains information about the UNIBUS resources held by the interface: 495105Swnj * map registers, buffered data paths, etc. Information is cached in this 505105Swnj * structure for use by the if_uba.c routines in running the interface 515105Swnj * efficiently. 525105Swnj */ 535083Swnj struct en_softc { 545105Swnj struct ifnet es_if; /* network-visible interface */ 555105Swnj struct ifuba es_ifuba; /* UNIBUS resources */ 565105Swnj short es_delay; /* current output delay */ 575105Swnj short es_mask; /* mask for current output delay */ 585105Swnj u_char es_lastx; /* host last transmitted to */ 595105Swnj short es_oactive; /* is output active? */ 605105Swnj short es_olen; /* length of last output */ 615083Swnj } en_softc[NEN]; 624686Swnj 635105Swnj /* 645105Swnj * Do output DMA to determine interface presence and 655105Swnj * interrupt vector. DMA is too short to disturb other hosts. 665105Swnj */ 674686Swnj enprobe(reg) 684686Swnj caddr_t reg; 694686Swnj { 705171Swnj register int br, cvec; /* r11, r10 value-result */ 714686Swnj register struct endevice *addr = (struct endevice *)reg; 724686Swnj 735083Swnj COUNT(ENPROBE); 744686Swnj #ifdef lint 754686Swnj br = 0; cvec = br; br = cvec; 764922Swnj enrint(0); enxint(0); encollide(0); 774686Swnj #endif 784686Swnj addr->en_istat = 0; 794686Swnj addr->en_owc = -1; 804686Swnj addr->en_oba = 0; 814771Swnj addr->en_ostat = EN_IEN|EN_GO; 824686Swnj DELAY(100000); 834686Swnj addr->en_ostat = 0; 844686Swnj return (1); 854686Swnj } 864686Swnj 875105Swnj /* 885105Swnj * Interface exists: make available by filling in network interface 895105Swnj * record. System will initialize the interface when it is ready 905105Swnj * to accept packets. 915105Swnj */ 924686Swnj enattach(ui) 934686Swnj struct uba_device *ui; 944686Swnj { 955105Swnj register struct en_softc *es = &en_softc[ui->ui_unit]; 965105Swnj COUNT(ENATTACH); 974688Swnj 985105Swnj es->es_if.if_unit = ui->ui_unit; 995171Swnj es->es_if.if_name = "en"; 1005105Swnj es->es_if.if_mtu = ENMTU; 1015105Swnj es->es_if.if_net = ui->ui_flags; 1025105Swnj es->es_if.if_host[0] = 1035213Swnj (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff; 1045294Sroot #ifdef ENKLUDGE 1055294Sroot if (es->es_if.if_net == 10) { 1065294Sroot es->es_if.if_host[0] <<= 16; 1075294Sroot es->es_if.if_host[0] |= 0x4e; 1085294Sroot } 1095294Sroot #endif 1105105Swnj es->es_if.if_addr = 1115105Swnj if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]); 1125171Swnj es->es_if.if_init = eninit; 1135105Swnj es->es_if.if_output = enoutput; 1145105Swnj es->es_if.if_ubareset = enreset; 1155696Sroot es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16; 1165160Swnj if_attach(&es->es_if); 1174686Swnj } 1184686Swnj 1195105Swnj /* 1205105Swnj * Reset of interface after UNIBUS reset. 1215105Swnj * If interface is on specified uba, reset its state. 1225105Swnj */ 1235105Swnj enreset(unit, uban) 1245105Swnj int unit, uban; 1255105Swnj { 1265105Swnj register struct uba_device *ui; 1275105Swnj COUNT(ENRESET); 1285105Swnj 1295171Swnj if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || 1305171Swnj ui->ui_ubanum != uban) 1315105Swnj return; 1325171Swnj printf(" en%d", unit); 1335105Swnj eninit(unit); 1345105Swnj } 1355105Swnj 1365105Swnj /* 1375105Swnj * Initialization of interface; clear recorded pending 1385105Swnj * operations, and reinitialize UNIBUS usage. 1395105Swnj */ 1404688Swnj eninit(unit) 1414686Swnj int unit; 1425083Swnj { 1435171Swnj register struct en_softc *es = &en_softc[unit]; 1445171Swnj register struct uba_device *ui = eninfo[unit]; 1454686Swnj register struct endevice *addr; 1465105Swnj int s; 1474686Swnj 1485105Swnj if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, 1495708Sroot sizeof (struct en_header), (int)btoc(ENMTU)) == 0) { 1505171Swnj printf("en%d: can't initialize\n", unit); 1515083Swnj return; 1524686Swnj } 1534686Swnj addr = (struct endevice *)ui->ui_addr; 1545083Swnj addr->en_istat = addr->en_ostat = 0; 1554686Swnj 1565105Swnj /* 1575171Swnj * Hang a receive and start any 1585171Swnj * pending writes by faking a transmit complete. 1595105Swnj */ 1605105Swnj s = splimp(); 1615171Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 1625171Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; 1635171Swnj addr->en_istat = EN_IEN|EN_GO; 1645171Swnj es->es_oactive = 1; 1655105Swnj enxint(unit); 1665105Swnj splx(s); 1674686Swnj } 1684686Swnj 1694717Swnj int enlastdel = 25; 1705083Swnj 1715105Swnj /* 1725105Swnj * Start or restart output on interface. 1735105Swnj * If interface is already active, then this is a retransmit 1745105Swnj * after a collision, and just restuff registers and delay. 1755105Swnj * If interface is not already active, get another datagram 1765105Swnj * to send off of the interface queue, and map it to the interface 1775105Swnj * before starting the output. 1785105Swnj */ 1794688Swnj enstart(dev) 1804686Swnj dev_t dev; 1814686Swnj { 1825171Swnj int unit = ENUNIT(dev); 1835171Swnj struct uba_device *ui = eninfo[unit]; 1845171Swnj register struct en_softc *es = &en_softc[unit]; 1855083Swnj register struct endevice *addr; 1865083Swnj struct mbuf *m; 1875083Swnj int dest; 1884688Swnj COUNT(ENSTART); 1894686Swnj 1905083Swnj if (es->es_oactive) 1915083Swnj goto restart; 1925105Swnj 1935105Swnj /* 1945105Swnj * Not already active: dequeue another request 1955105Swnj * and map it to the UNIBUS. If no more requests, 1965105Swnj * just return. 1975105Swnj */ 1985105Swnj IF_DEQUEUE(&es->es_if.if_snd, m); 1995083Swnj if (m == 0) { 2005083Swnj es->es_oactive = 0; 2014686Swnj return; 2024686Swnj } 2035213Swnj dest = mtod(m, struct en_header *)->en_dhost; 2045105Swnj es->es_olen = if_wubaput(&es->es_ifuba, m); 2055105Swnj 2065105Swnj /* 2075105Swnj * Ethernet cannot take back-to-back packets (no 2085105Swnj * buffering in interface. To avoid overrunning 2095105Swnj * receiver, enforce a small delay (about 1ms) in interface 2105105Swnj * on successive packets sent to same host. 2115105Swnj */ 2125083Swnj if (es->es_lastx && es->es_lastx == dest) 2135083Swnj es->es_delay = enlastdel; 2145083Swnj else 2155083Swnj es->es_lastx = dest; 2165105Swnj 2175083Swnj restart: 2185105Swnj /* 2195105Swnj * Have request mapped to UNIBUS for transmission. 2205105Swnj * Purge any stale data from this BDP, and start the otput. 2215105Swnj */ 2225105Swnj UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); 2234686Swnj addr = (struct endevice *)ui->ui_addr; 2245160Swnj addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; 2255083Swnj addr->en_odelay = es->es_delay; 2265083Swnj addr->en_owc = -((es->es_olen + 1) >> 1); 2274771Swnj addr->en_ostat = EN_IEN|EN_GO; 2285083Swnj es->es_oactive = 1; 2294686Swnj } 2304686Swnj 2315105Swnj /* 2325105Swnj * Ethernet interface transmitter interrupt. 2335105Swnj * Start another output if more data to send. 2345105Swnj */ 2354686Swnj enxint(unit) 2364686Swnj int unit; 2374686Swnj { 2385171Swnj register struct uba_device *ui = eninfo[unit]; 2395171Swnj register struct en_softc *es = &en_softc[unit]; 2404686Swnj register struct endevice *addr; 2414686Swnj COUNT(ENXINT); 2424686Swnj 2435083Swnj if (es->es_oactive == 0) 2445083Swnj return; 2454686Swnj addr = (struct endevice *)ui->ui_addr; 2465171Swnj es->es_if.if_opackets++; 2475083Swnj es->es_oactive = 0; 2485083Swnj es->es_delay = 0; 2495083Swnj es->es_mask = ~0; 2505275Swnj if (addr->en_ostat&EN_OERROR) { 2515275Swnj es->es_if.if_oerrors++; 2525213Swnj printf("en%d: output error\n", unit); 2535275Swnj } 2545696Sroot if (es->es_ifuba.ifu_xtofree) { 2555696Sroot m_freem(es->es_ifuba.ifu_xtofree); 2565696Sroot es->es_ifuba.ifu_xtofree = 0; 2575696Sroot } 2585105Swnj if (es->es_if.if_snd.ifq_head == 0) { 2595083Swnj es->es_lastx = 0; 2604686Swnj return; 2614686Swnj } 2625083Swnj enstart(unit); 2634686Swnj } 2644686Swnj 2655105Swnj /* 2665105Swnj * Collision on ethernet interface. Do exponential 2675105Swnj * backoff, and retransmit. If have backed off all 2685105Swnj * the way printing warning diagnostic, and drop packet. 2695105Swnj */ 2704686Swnj encollide(unit) 2714686Swnj int unit; 2724686Swnj { 2735171Swnj register struct en_softc *es = &en_softc[unit]; 2744686Swnj COUNT(ENCOLLIDE); 2754686Swnj 2765105Swnj es->es_if.if_collisions++; 2775083Swnj if (es->es_oactive == 0) 2784686Swnj return; 2795171Swnj /* 2805171Swnj * Es_mask is a 16 bit number with n low zero bits, with 2815171Swnj * n the number of backoffs. When es_mask is 0 we have 2825171Swnj * backed off 16 times, and give up. 2835171Swnj */ 2845083Swnj if (es->es_mask == 0) { 2855213Swnj printf("en%d: send error\n", unit); 2865083Swnj enxint(unit); 2875171Swnj return; 2884686Swnj } 2895171Swnj /* 2905171Swnj * Another backoff. Restart with delay based on n low bits 2915171Swnj * of the interval timer. 2925171Swnj */ 2935171Swnj es->es_mask <<= 1; 2945171Swnj es->es_delay = mfpr(ICR) &~ es->es_mask; 2955171Swnj enstart(unit); 2964686Swnj } 2974686Swnj 2985631Swnj int enprintierrors; 2995105Swnj /* 3005105Swnj * Ethernet interface receiver interrupt. 3015105Swnj * If input error just drop packet. 3025105Swnj * Otherwise purge input buffered data path and examine 3035105Swnj * packet to determine type. If can't determine length 3045105Swnj * from type, then have to drop packet. Othewise decapsulate 3055105Swnj * packet based on type and pass to type specific higher-level 3065105Swnj * input routine. 3075105Swnj */ 3084686Swnj enrint(unit) 3094686Swnj int unit; 3104686Swnj { 3115171Swnj register struct en_softc *es = &en_softc[unit]; 3125171Swnj struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 3135171Swnj register struct en_header *en; 3145083Swnj struct mbuf *m; 3155171Swnj int len; 3165171Swnj register struct ifqueue *inq; 3175083Swnj int off; 3184686Swnj COUNT(ENRINT); 3194686Swnj 3205171Swnj es->es_if.if_ipackets++; 3215105Swnj 3225105Swnj /* 3235171Swnj * Purge BDP; drop if input error indicated. 3245105Swnj */ 3255105Swnj UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 3264771Swnj if (addr->en_istat&EN_IERROR) { 3275105Swnj es->es_if.if_ierrors++; 3285631Swnj if (enprintierrors) 3295213Swnj printf("en%d: input error\n", unit); 3305083Swnj goto setup; 3314686Swnj } 3325105Swnj 3335105Swnj /* 3345105Swnj * Get pointer to ethernet header (in input buffer). 3355105Swnj * Deal with trailer protocol: if type is PUP trailer 3365105Swnj * get true type from first 16-bit word past data. 3375105Swnj * Remember that type was trailer by setting off. 3385105Swnj */ 3395105Swnj en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 3405083Swnj #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 3415083Swnj if (en->en_type >= ENPUP_TRAIL && 3425083Swnj en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { 3435083Swnj off = (en->en_type - ENPUP_TRAIL) * 512; 3445171Swnj if (off >= ENMTU) 3455171Swnj goto setup; /* sanity */ 3465083Swnj en->en_type = *endataaddr(en, off, u_short *); 3475083Swnj } else 3485083Swnj off = 0; 3495105Swnj 3505105Swnj /* 3515105Swnj * Attempt to infer packet length from type; 3525105Swnj * can't deal with packet if can't infer length. 3535105Swnj */ 3545083Swnj switch (en->en_type) { 3555083Swnj 3565083Swnj #ifdef INET 3575083Swnj case ENPUP_IPTYPE: 3585239Sroot len = htons((u_short)endataaddr(en, off ? off+2 : 0, struct ip *)->ip_len); 3595239Sroot if (off) 3605239Sroot len += 2; 3615083Swnj setipintr(); 3625083Swnj inq = &ipintrq; 3634686Swnj break; 3644686Swnj #endif 3654686Swnj 3665083Swnj default: 3675083Swnj printf("en%d: unknow pkt type 0x%x\n", en->en_type); 3685083Swnj goto setup; 3694686Swnj } 3705083Swnj if (len == 0) 3715083Swnj goto setup; 3725105Swnj 3735105Swnj /* 3745105Swnj * Pull packet off interface. Off is nonzero if packet 3755105Swnj * has trailing header; if_rubaget will then force this header 3765105Swnj * information to be at the front, but we still have to drop 3775171Swnj * the two-byte type which is at the front of any trailer data. 3785105Swnj */ 3795105Swnj m = if_rubaget(&es->es_ifuba, len, off); 3805213Swnj if (m == 0) 3815213Swnj goto setup; 3825105Swnj if (off) { 3835105Swnj m->m_off += 2; 3845105Swnj m->m_len -= 2; 3855105Swnj } 3865083Swnj IF_ENQUEUE(inq, m); 3875105Swnj 3884688Swnj setup: 3895105Swnj /* 3905105Swnj * Reset for next packet. 3915105Swnj */ 3925105Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 3935083Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; 3944771Swnj addr->en_istat = EN_IEN|EN_GO; 3954688Swnj } 3964686Swnj 3975083Swnj /* 3985083Swnj * Ethernet output routine. 3995083Swnj * Encapsulate a packet of type family for the local net. 4005105Swnj * Use trailer local net encapsulation if enough data in first 4015105Swnj * packet leaves a multiple of 512 bytes of data in remainder. 4025083Swnj */ 4035083Swnj enoutput(ifp, m0, pf) 4045083Swnj struct ifnet *ifp; 4055083Swnj struct mbuf *m0; 4065083Swnj int pf; 4074686Swnj { 4085083Swnj int type, dest; 4095105Swnj register struct mbuf *m = m0; 4105083Swnj register struct en_header *en; 4115083Swnj int s; 4124686Swnj 413*5927Sroot COUNT(ENOUTPUT); 4145083Swnj switch (pf) { 4155083Swnj 4165083Swnj #ifdef INET 4175083Swnj case PF_INET: { 4185083Swnj register struct ip *ip = mtod(m0, struct ip *); 4195083Swnj int off; 4205083Swnj 4215294Sroot #ifndef ENKLUDGE 4225105Swnj dest = ip->ip_dst.s_addr >> 24; 4235294Sroot #else 4245294Sroot dest = (ip->ip_dst.s_addr >> 8) & 0xff; 4255294Sroot #endif 4265239Sroot off = ntohs((u_short)ip->ip_len) - m->m_len; 4275356Sroot #ifndef ENKLUDGE 4285171Swnj if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) { 4295083Swnj type = ENPUP_TRAIL + (off>>9); 4305105Swnj m->m_off -= 2; 4315105Swnj m->m_len += 2; 4325105Swnj *mtod(m, u_short *) = ENPUP_IPTYPE; 4335105Swnj goto gottrailertype; 4345083Swnj } 4355356Sroot #endif 4365105Swnj type = ENPUP_IPTYPE; 4375105Swnj off = 0; 4385105Swnj goto gottype; 4395083Swnj } 4405083Swnj #endif 4415083Swnj 4425083Swnj default: 4435083Swnj printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf); 4445083Swnj m_freem(m0); 4455083Swnj return (0); 4464686Swnj } 4475105Swnj 4485171Swnj gottrailertype: 4495105Swnj /* 4505105Swnj * Packet to be sent as trailer: move first packet 4515105Swnj * (control information) to end of chain. 4525105Swnj */ 4535105Swnj while (m->m_next) 4545105Swnj m = m->m_next; 4555105Swnj m->m_next = m0; 4565105Swnj m = m0->m_next; 4575105Swnj m0->m_next = 0; 4585171Swnj m0 = m; 4595105Swnj 4605171Swnj gottype: 4615105Swnj /* 4625105Swnj * Add local net header. If no space in first mbuf, 4635105Swnj * allocate another. 4645105Swnj */ 4655213Swnj if (m->m_off > MMAXOFF || 4665213Swnj MMINOFF + sizeof (struct en_header) > m->m_off) { 4675584Sroot m = m_get(M_DONTWAIT); 4685083Swnj if (m == 0) { 4695083Swnj m_freem(m0); 4705083Swnj return (0); 4715083Swnj } 4725083Swnj m->m_next = m0; 4735083Swnj m->m_off = MMINOFF; 4745083Swnj m->m_len = sizeof (struct en_header); 4755083Swnj } else { 4765083Swnj m->m_off -= sizeof (struct en_header); 4775083Swnj m->m_len += sizeof (struct en_header); 4785083Swnj } 4795083Swnj en = mtod(m, struct en_header *); 4805083Swnj en->en_shost = ifp->if_host[0]; 4815083Swnj en->en_dhost = dest; 4825083Swnj en->en_type = type; 4835105Swnj 4845105Swnj /* 4855105Swnj * Queue message on interface, and start output if interface 4865105Swnj * not yet active. 4875105Swnj */ 4885083Swnj s = splimp(); 4895083Swnj IF_ENQUEUE(&ifp->if_snd, m); 4905083Swnj if (en_softc[ifp->if_unit].es_oactive == 0) 4915083Swnj enstart(ifp->if_unit); 4925275Swnj splx(s); 4935105Swnj return (1); 4944686Swnj } 495