1*5356Sroot /* if_en.c 4.28 82/01/07 */ 24686Swnj 35294Sroot #define ENKLUDGE 44686Swnj #include "en.h" 55105Swnj 64686Swnj /* 75105Swnj * Xerox prototype (3 Mb) Ethernet interface driver. 84686Swnj */ 94686Swnj 104686Swnj #include "../h/param.h" 114686Swnj #include "../h/systm.h" 124686Swnj #include "../h/mbuf.h" 134686Swnj #include "../h/pte.h" 144686Swnj #include "../h/buf.h" 155083Swnj #include "../h/protosw.h" 165083Swnj #include "../h/socket.h" 174686Swnj #include "../h/ubareg.h" 184686Swnj #include "../h/ubavar.h" 194686Swnj #include "../h/enreg.h" 205083Swnj #include "../h/cpu.h" 214686Swnj #include "../h/mtpr.h" 225083Swnj #include "../h/vmmac.h" 235083Swnj #include "../net/in.h" 245083Swnj #include "../net/in_systm.h" 255083Swnj #include "../net/if.h" 265083Swnj #include "../net/if_en.h" 275083Swnj #include "../net/if_uba.h" 285083Swnj #include "../net/ip.h" 295083Swnj #include "../net/ip_var.h" 304686Swnj 315213Swnj #define ENMTU (1024+512) 325083Swnj 334686Swnj int enprobe(), enattach(), enrint(), enxint(), encollide(); 344686Swnj struct uba_device *eninfo[NEN]; 354686Swnj u_short enstd[] = { 0 }; 364686Swnj struct uba_driver endriver = 375171Swnj { enprobe, 0, enattach, 0, enstd, "en", eninfo }; 384686Swnj #define ENUNIT(x) minor(x) 394686Swnj 405105Swnj int eninit(),enoutput(),enreset(); 415105Swnj 425105Swnj /* 435105Swnj * Ethernet software status per interface. 445105Swnj * 455105Swnj * Each interface is referenced by a network interface structure, 465105Swnj * es_if, which the routing code uses to locate the interface. 475105Swnj * This structure contains the output queue for the interface, its address, ... 485105Swnj * We also have, for each interface, a UBA interface structure, which 495105Swnj * contains information about the UNIBUS resources held by the interface: 505105Swnj * map registers, buffered data paths, etc. Information is cached in this 515105Swnj * structure for use by the if_uba.c routines in running the interface 525105Swnj * efficiently. 535105Swnj */ 545083Swnj struct en_softc { 555105Swnj struct ifnet es_if; /* network-visible interface */ 565105Swnj struct ifuba es_ifuba; /* UNIBUS resources */ 575105Swnj short es_delay; /* current output delay */ 585105Swnj short es_mask; /* mask for current output delay */ 595105Swnj u_char es_lastx; /* host last transmitted to */ 605105Swnj short es_oactive; /* is output active? */ 615105Swnj short es_olen; /* length of last output */ 625083Swnj } en_softc[NEN]; 634686Swnj 645105Swnj /* 655105Swnj * Do output DMA to determine interface presence and 665105Swnj * interrupt vector. DMA is too short to disturb other hosts. 675105Swnj */ 684686Swnj enprobe(reg) 694686Swnj caddr_t reg; 704686Swnj { 715171Swnj register int br, cvec; /* r11, r10 value-result */ 724686Swnj register struct endevice *addr = (struct endevice *)reg; 734686Swnj 745083Swnj COUNT(ENPROBE); 754686Swnj #ifdef lint 764686Swnj br = 0; cvec = br; br = cvec; 774922Swnj enrint(0); enxint(0); encollide(0); 784686Swnj #endif 794686Swnj addr->en_istat = 0; 804686Swnj addr->en_owc = -1; 814686Swnj addr->en_oba = 0; 824771Swnj addr->en_ostat = EN_IEN|EN_GO; 834686Swnj DELAY(100000); 844686Swnj addr->en_ostat = 0; 854686Swnj return (1); 864686Swnj } 874686Swnj 885105Swnj /* 895105Swnj * Interface exists: make available by filling in network interface 905105Swnj * record. System will initialize the interface when it is ready 915105Swnj * to accept packets. 925105Swnj */ 934686Swnj enattach(ui) 944686Swnj struct uba_device *ui; 954686Swnj { 965105Swnj register struct en_softc *es = &en_softc[ui->ui_unit]; 975105Swnj COUNT(ENATTACH); 984688Swnj 995105Swnj es->es_if.if_unit = ui->ui_unit; 1005171Swnj es->es_if.if_name = "en"; 1015105Swnj es->es_if.if_mtu = ENMTU; 1025105Swnj es->es_if.if_net = ui->ui_flags; 1035105Swnj es->es_if.if_host[0] = 1045213Swnj (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff; 1055294Sroot #ifdef ENKLUDGE 1065294Sroot if (es->es_if.if_net == 10) { 1075294Sroot es->es_if.if_host[0] <<= 16; 1085294Sroot es->es_if.if_host[0] |= 0x4e; 1095294Sroot } 1105294Sroot #endif 1115105Swnj es->es_if.if_addr = 1125105Swnj if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]); 1135171Swnj es->es_if.if_init = eninit; 1145105Swnj es->es_if.if_output = enoutput; 1155105Swnj es->es_if.if_ubareset = enreset; 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, 1495105Swnj sizeof (struct en_header), (int)btop(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 } 2545105Swnj if (es->es_if.if_snd.ifq_head == 0) { 2555171Swnj if (es->es_ifuba.ifu_xtofree) { 2565171Swnj m_freem(es->es_ifuba.ifu_xtofree); 2575171Swnj es->es_ifuba.ifu_xtofree = 0; 2585171Swnj } 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 2985105Swnj /* 2995105Swnj * Ethernet interface receiver interrupt. 3005105Swnj * If input error just drop packet. 3015105Swnj * Otherwise purge input buffered data path and examine 3025105Swnj * packet to determine type. If can't determine length 3035105Swnj * from type, then have to drop packet. Othewise decapsulate 3045105Swnj * packet based on type and pass to type specific higher-level 3055105Swnj * input routine. 3065105Swnj */ 3074686Swnj enrint(unit) 3084686Swnj int unit; 3094686Swnj { 3105171Swnj register struct en_softc *es = &en_softc[unit]; 3115171Swnj struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 3125171Swnj register struct en_header *en; 3135083Swnj struct mbuf *m; 3145171Swnj int len; 3155171Swnj register struct ifqueue *inq; 3165083Swnj int off; 3174686Swnj COUNT(ENRINT); 3184686Swnj 3195171Swnj es->es_if.if_ipackets++; 3205105Swnj 3215105Swnj /* 3225171Swnj * Purge BDP; drop if input error indicated. 3235105Swnj */ 3245105Swnj UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 3254771Swnj if (addr->en_istat&EN_IERROR) { 3265105Swnj es->es_if.if_ierrors++; 3275213Swnj printf("en%d: input error\n", unit); 3285083Swnj goto setup; 3294686Swnj } 3305105Swnj 3315105Swnj /* 3325105Swnj * Get pointer to ethernet header (in input buffer). 3335105Swnj * Deal with trailer protocol: if type is PUP trailer 3345105Swnj * get true type from first 16-bit word past data. 3355105Swnj * Remember that type was trailer by setting off. 3365105Swnj */ 3375105Swnj en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 3385083Swnj #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 3395083Swnj if (en->en_type >= ENPUP_TRAIL && 3405083Swnj en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { 3415083Swnj off = (en->en_type - ENPUP_TRAIL) * 512; 3425171Swnj if (off >= ENMTU) 3435171Swnj goto setup; /* sanity */ 3445083Swnj en->en_type = *endataaddr(en, off, u_short *); 3455083Swnj } else 3465083Swnj off = 0; 3475105Swnj 3485105Swnj /* 3495105Swnj * Attempt to infer packet length from type; 3505105Swnj * can't deal with packet if can't infer length. 3515105Swnj */ 3525083Swnj switch (en->en_type) { 3535083Swnj 3545083Swnj #ifdef INET 3555083Swnj case ENPUP_IPTYPE: 3565239Sroot len = htons((u_short)endataaddr(en, off ? off+2 : 0, struct ip *)->ip_len); 3575239Sroot if (off) 3585239Sroot len += 2; 3595083Swnj setipintr(); 3605083Swnj inq = &ipintrq; 3614686Swnj break; 3624686Swnj #endif 3634686Swnj 3645083Swnj default: 3655083Swnj printf("en%d: unknow pkt type 0x%x\n", en->en_type); 3665083Swnj goto setup; 3674686Swnj } 3685083Swnj if (len == 0) 3695083Swnj goto setup; 3705105Swnj 3715105Swnj /* 3725105Swnj * Pull packet off interface. Off is nonzero if packet 3735105Swnj * has trailing header; if_rubaget will then force this header 3745105Swnj * information to be at the front, but we still have to drop 3755171Swnj * the two-byte type which is at the front of any trailer data. 3765105Swnj */ 3775105Swnj m = if_rubaget(&es->es_ifuba, len, off); 3785213Swnj if (m == 0) 3795213Swnj goto setup; 3805105Swnj if (off) { 3815105Swnj m->m_off += 2; 3825105Swnj m->m_len -= 2; 3835105Swnj } 3845083Swnj IF_ENQUEUE(inq, m); 3855105Swnj 3864688Swnj setup: 3875105Swnj /* 3885105Swnj * Reset for next packet. 3895105Swnj */ 3905105Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 3915083Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; 3924771Swnj addr->en_istat = EN_IEN|EN_GO; 3934688Swnj } 3944686Swnj 3955083Swnj /* 3965083Swnj * Ethernet output routine. 3975083Swnj * Encapsulate a packet of type family for the local net. 3985105Swnj * Use trailer local net encapsulation if enough data in first 3995105Swnj * packet leaves a multiple of 512 bytes of data in remainder. 4005083Swnj */ 4015083Swnj enoutput(ifp, m0, pf) 4025083Swnj struct ifnet *ifp; 4035083Swnj struct mbuf *m0; 4045083Swnj int pf; 4054686Swnj { 4065083Swnj int type, dest; 4075105Swnj register struct mbuf *m = m0; 4085083Swnj register struct en_header *en; 4095083Swnj int s; 4104686Swnj 4115083Swnj switch (pf) { 4125083Swnj 4135083Swnj #ifdef INET 4145083Swnj case PF_INET: { 4155083Swnj register struct ip *ip = mtod(m0, struct ip *); 4165083Swnj int off; 4175083Swnj 4185294Sroot #ifndef ENKLUDGE 4195105Swnj dest = ip->ip_dst.s_addr >> 24; 4205294Sroot #else 4215294Sroot dest = (ip->ip_dst.s_addr >> 8) & 0xff; 4225294Sroot #endif 4235239Sroot off = ntohs((u_short)ip->ip_len) - m->m_len; 424*5356Sroot #ifndef ENKLUDGE 4255171Swnj if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) { 4265083Swnj type = ENPUP_TRAIL + (off>>9); 4275105Swnj m->m_off -= 2; 4285105Swnj m->m_len += 2; 4295105Swnj *mtod(m, u_short *) = ENPUP_IPTYPE; 4305105Swnj goto gottrailertype; 4315083Swnj } 432*5356Sroot #endif 4335105Swnj type = ENPUP_IPTYPE; 4345105Swnj off = 0; 4355105Swnj goto gottype; 4365083Swnj } 4375083Swnj #endif 4385083Swnj 4395083Swnj default: 4405083Swnj printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf); 4415083Swnj m_freem(m0); 4425083Swnj return (0); 4434686Swnj } 4445105Swnj 4455171Swnj gottrailertype: 4465105Swnj /* 4475105Swnj * Packet to be sent as trailer: move first packet 4485105Swnj * (control information) to end of chain. 4495105Swnj */ 4505105Swnj while (m->m_next) 4515105Swnj m = m->m_next; 4525105Swnj m->m_next = m0; 4535105Swnj m = m0->m_next; 4545105Swnj m0->m_next = 0; 4555171Swnj m0 = m; 4565105Swnj 4575171Swnj gottype: 4585105Swnj /* 4595105Swnj * Add local net header. If no space in first mbuf, 4605105Swnj * allocate another. 4615105Swnj */ 4625213Swnj if (m->m_off > MMAXOFF || 4635213Swnj MMINOFF + sizeof (struct en_header) > m->m_off) { 4645083Swnj m = m_get(0); 4655083Swnj if (m == 0) { 4665083Swnj m_freem(m0); 4675083Swnj return (0); 4685083Swnj } 4695083Swnj m->m_next = m0; 4705083Swnj m->m_off = MMINOFF; 4715083Swnj m->m_len = sizeof (struct en_header); 4725083Swnj } else { 4735083Swnj m->m_off -= sizeof (struct en_header); 4745083Swnj m->m_len += sizeof (struct en_header); 4755083Swnj } 4765083Swnj en = mtod(m, struct en_header *); 4775083Swnj en->en_shost = ifp->if_host[0]; 4785083Swnj en->en_dhost = dest; 4795083Swnj en->en_type = type; 4805105Swnj 4815105Swnj /* 4825105Swnj * Queue message on interface, and start output if interface 4835105Swnj * not yet active. 4845105Swnj */ 4855083Swnj s = splimp(); 4865083Swnj IF_ENQUEUE(&ifp->if_snd, m); 4875083Swnj if (en_softc[ifp->if_unit].es_oactive == 0) 4885083Swnj enstart(ifp->if_unit); 4895275Swnj splx(s); 4905105Swnj return (1); 4914686Swnj } 492