1*6027Ssam /* if_en.c 4.36 82/03/03 */ 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" 29*6027Ssam #include "../net/pup.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; 1165696Sroot es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16; 1175160Swnj if_attach(&es->es_if); 1184686Swnj } 1194686Swnj 1205105Swnj /* 1215105Swnj * Reset of interface after UNIBUS reset. 1225105Swnj * If interface is on specified uba, reset its state. 1235105Swnj */ 1245105Swnj enreset(unit, uban) 1255105Swnj int unit, uban; 1265105Swnj { 1275105Swnj register struct uba_device *ui; 1285105Swnj COUNT(ENRESET); 1295105Swnj 1305171Swnj if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || 1315171Swnj ui->ui_ubanum != uban) 1325105Swnj return; 1335171Swnj printf(" en%d", unit); 1345105Swnj eninit(unit); 1355105Swnj } 1365105Swnj 1375105Swnj /* 1385105Swnj * Initialization of interface; clear recorded pending 1395105Swnj * operations, and reinitialize UNIBUS usage. 1405105Swnj */ 1414688Swnj eninit(unit) 1424686Swnj int unit; 1435083Swnj { 1445171Swnj register struct en_softc *es = &en_softc[unit]; 1455171Swnj register struct uba_device *ui = eninfo[unit]; 1464686Swnj register struct endevice *addr; 1475105Swnj int s; 1484686Swnj 1495105Swnj if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, 1505708Sroot sizeof (struct en_header), (int)btoc(ENMTU)) == 0) { 1515171Swnj printf("en%d: can't initialize\n", unit); 1525083Swnj return; 1534686Swnj } 1544686Swnj addr = (struct endevice *)ui->ui_addr; 1555083Swnj addr->en_istat = addr->en_ostat = 0; 1564686Swnj 1575105Swnj /* 1585171Swnj * Hang a receive and start any 1595171Swnj * pending writes by faking a transmit complete. 1605105Swnj */ 1615105Swnj s = splimp(); 1625171Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 1635171Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; 1645171Swnj addr->en_istat = EN_IEN|EN_GO; 1655171Swnj es->es_oactive = 1; 1665105Swnj enxint(unit); 1675105Swnj splx(s); 1684686Swnj } 1694686Swnj 1704717Swnj int enlastdel = 25; 1715083Swnj 1725105Swnj /* 1735105Swnj * Start or restart output on interface. 1745105Swnj * If interface is already active, then this is a retransmit 1755105Swnj * after a collision, and just restuff registers and delay. 1765105Swnj * If interface is not already active, get another datagram 1775105Swnj * to send off of the interface queue, and map it to the interface 1785105Swnj * before starting the output. 1795105Swnj */ 1804688Swnj enstart(dev) 1814686Swnj dev_t dev; 1824686Swnj { 1835171Swnj int unit = ENUNIT(dev); 1845171Swnj struct uba_device *ui = eninfo[unit]; 1855171Swnj register struct en_softc *es = &en_softc[unit]; 1865083Swnj register struct endevice *addr; 1875083Swnj struct mbuf *m; 1885083Swnj int dest; 1894688Swnj COUNT(ENSTART); 1904686Swnj 1915083Swnj if (es->es_oactive) 1925083Swnj goto restart; 1935105Swnj 1945105Swnj /* 1955105Swnj * Not already active: dequeue another request 1965105Swnj * and map it to the UNIBUS. If no more requests, 1975105Swnj * just return. 1985105Swnj */ 1995105Swnj IF_DEQUEUE(&es->es_if.if_snd, m); 2005083Swnj if (m == 0) { 2015083Swnj es->es_oactive = 0; 2024686Swnj return; 2034686Swnj } 2045213Swnj dest = mtod(m, struct en_header *)->en_dhost; 2055105Swnj es->es_olen = if_wubaput(&es->es_ifuba, m); 2065105Swnj 2075105Swnj /* 2085105Swnj * Ethernet cannot take back-to-back packets (no 2095105Swnj * buffering in interface. To avoid overrunning 2105105Swnj * receiver, enforce a small delay (about 1ms) in interface 2115105Swnj * on successive packets sent to same host. 2125105Swnj */ 2135083Swnj if (es->es_lastx && es->es_lastx == dest) 2145083Swnj es->es_delay = enlastdel; 2155083Swnj else 2165083Swnj es->es_lastx = dest; 2175105Swnj 2185083Swnj restart: 2195105Swnj /* 2205105Swnj * Have request mapped to UNIBUS for transmission. 2215105Swnj * Purge any stale data from this BDP, and start the otput. 2225105Swnj */ 2235105Swnj UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); 2244686Swnj addr = (struct endevice *)ui->ui_addr; 2255160Swnj addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; 2265083Swnj addr->en_odelay = es->es_delay; 2275083Swnj addr->en_owc = -((es->es_olen + 1) >> 1); 2284771Swnj addr->en_ostat = EN_IEN|EN_GO; 2295083Swnj es->es_oactive = 1; 2304686Swnj } 2314686Swnj 2325105Swnj /* 2335105Swnj * Ethernet interface transmitter interrupt. 2345105Swnj * Start another output if more data to send. 2355105Swnj */ 2364686Swnj enxint(unit) 2374686Swnj int unit; 2384686Swnj { 2395171Swnj register struct uba_device *ui = eninfo[unit]; 2405171Swnj register struct en_softc *es = &en_softc[unit]; 2414686Swnj register struct endevice *addr; 2424686Swnj COUNT(ENXINT); 2434686Swnj 2445083Swnj if (es->es_oactive == 0) 2455083Swnj return; 2464686Swnj addr = (struct endevice *)ui->ui_addr; 2475171Swnj es->es_if.if_opackets++; 2485083Swnj es->es_oactive = 0; 2495083Swnj es->es_delay = 0; 2505083Swnj es->es_mask = ~0; 2515275Swnj if (addr->en_ostat&EN_OERROR) { 2525275Swnj es->es_if.if_oerrors++; 2535213Swnj printf("en%d: output error\n", unit); 2545275Swnj } 2555696Sroot if (es->es_ifuba.ifu_xtofree) { 2565696Sroot m_freem(es->es_ifuba.ifu_xtofree); 2575696Sroot es->es_ifuba.ifu_xtofree = 0; 2585696Sroot } 2595105Swnj if (es->es_if.if_snd.ifq_head == 0) { 2605083Swnj es->es_lastx = 0; 2614686Swnj return; 2624686Swnj } 2635083Swnj enstart(unit); 2644686Swnj } 2654686Swnj 2665105Swnj /* 2675105Swnj * Collision on ethernet interface. Do exponential 2685105Swnj * backoff, and retransmit. If have backed off all 2695105Swnj * the way printing warning diagnostic, and drop packet. 2705105Swnj */ 2714686Swnj encollide(unit) 2724686Swnj int unit; 2734686Swnj { 2745171Swnj register struct en_softc *es = &en_softc[unit]; 2754686Swnj COUNT(ENCOLLIDE); 2764686Swnj 2775105Swnj es->es_if.if_collisions++; 2785083Swnj if (es->es_oactive == 0) 2794686Swnj return; 2805171Swnj /* 2815171Swnj * Es_mask is a 16 bit number with n low zero bits, with 2825171Swnj * n the number of backoffs. When es_mask is 0 we have 2835171Swnj * backed off 16 times, and give up. 2845171Swnj */ 2855083Swnj if (es->es_mask == 0) { 2865213Swnj printf("en%d: send error\n", unit); 2875083Swnj enxint(unit); 2885171Swnj return; 2894686Swnj } 2905171Swnj /* 2915171Swnj * Another backoff. Restart with delay based on n low bits 2925171Swnj * of the interval timer. 2935171Swnj */ 2945171Swnj es->es_mask <<= 1; 2955171Swnj es->es_delay = mfpr(ICR) &~ es->es_mask; 2965171Swnj enstart(unit); 2974686Swnj } 2984686Swnj 2995631Swnj int enprintierrors; 300*6027Ssam struct sockaddr_pup pupsrc = { AF_PUP }; 301*6027Ssam struct sockaddr_pup pupdst = { AF_PUP }; 302*6027Ssam struct sockproto pupproto = { PF_PUP }; 3035105Swnj /* 3045105Swnj * Ethernet interface receiver interrupt. 3055105Swnj * If input error just drop packet. 3065105Swnj * Otherwise purge input buffered data path and examine 3075105Swnj * packet to determine type. If can't determine length 3085105Swnj * from type, then have to drop packet. Othewise decapsulate 3095105Swnj * packet based on type and pass to type specific higher-level 3105105Swnj * input routine. 3115105Swnj */ 3124686Swnj enrint(unit) 3134686Swnj int unit; 3144686Swnj { 3155171Swnj register struct en_softc *es = &en_softc[unit]; 3165171Swnj struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 3175171Swnj register struct en_header *en; 3185083Swnj struct mbuf *m; 3195171Swnj int len; 3205171Swnj register struct ifqueue *inq; 3215083Swnj int off; 3224686Swnj COUNT(ENRINT); 3234686Swnj 3245171Swnj es->es_if.if_ipackets++; 3255105Swnj 3265105Swnj /* 3275171Swnj * Purge BDP; drop if input error indicated. 3285105Swnj */ 3295105Swnj UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 3304771Swnj if (addr->en_istat&EN_IERROR) { 3315105Swnj es->es_if.if_ierrors++; 3325631Swnj if (enprintierrors) 3335213Swnj printf("en%d: input error\n", unit); 3345083Swnj goto setup; 3354686Swnj } 3365105Swnj 3375105Swnj /* 3385105Swnj * Get pointer to ethernet header (in input buffer). 3395105Swnj * Deal with trailer protocol: if type is PUP trailer 3405105Swnj * get true type from first 16-bit word past data. 3415105Swnj * Remember that type was trailer by setting off. 3425105Swnj */ 3435105Swnj en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 3445083Swnj #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 3455083Swnj if (en->en_type >= ENPUP_TRAIL && 3465083Swnj en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { 3475083Swnj off = (en->en_type - ENPUP_TRAIL) * 512; 3485171Swnj if (off >= ENMTU) 3495171Swnj goto setup; /* sanity */ 3505083Swnj en->en_type = *endataaddr(en, off, u_short *); 3515083Swnj } else 3525083Swnj off = 0; 3535105Swnj 3545105Swnj /* 3555105Swnj * Attempt to infer packet length from type; 3565105Swnj * can't deal with packet if can't infer length. 3575105Swnj */ 3585083Swnj switch (en->en_type) { 3595083Swnj 3605083Swnj #ifdef INET 3615083Swnj case ENPUP_IPTYPE: 3625239Sroot len = htons((u_short)endataaddr(en, off ? off+2 : 0, struct ip *)->ip_len); 3635239Sroot if (off) 3645239Sroot len += 2; 3654686Swnj break; 3664686Swnj #endif 367*6027Ssam #ifdef PUP 368*6027Ssam case ENPUP_PUPTYPE: 369*6027Ssam len = endataaddr(en, off, struct pup_header *)->pup_length; 370*6027Ssam if (off) 371*6027Ssam len -= 2; 372*6027Ssam break; 373*6027Ssam #endif 374*6027Ssam 3755083Swnj default: 3766026Sroot printf("en%d: unknown pkt type 0x%x\n", unit, en->en_type); 3775083Swnj goto setup; 3784686Swnj } 3795083Swnj if (len == 0) 3805083Swnj goto setup; 3815105Swnj 3825105Swnj /* 3835105Swnj * Pull packet off interface. Off is nonzero if packet 3845105Swnj * has trailing header; if_rubaget will then force this header 3855105Swnj * information to be at the front, but we still have to drop 3865171Swnj * the two-byte type which is at the front of any trailer data. 3875105Swnj */ 3885105Swnj m = if_rubaget(&es->es_ifuba, len, off); 3895213Swnj if (m == 0) 3905213Swnj goto setup; 3915105Swnj if (off) { 3925105Swnj m->m_off += 2; 3935105Swnj m->m_len -= 2; 3945105Swnj } 395*6027Ssam switch (en->en_type) { 396*6027Ssam 397*6027Ssam #ifdef INET 398*6027Ssam case ENPUP_IPTYPE: 399*6027Ssam setipintr(); 400*6027Ssam inq = &ipintrq; 401*6027Ssam break; 402*6027Ssam #endif 403*6027Ssam case ENPUP_PUPTYPE: { 404*6027Ssam struct pup_header *pup = mtod(m, struct pup_header *); 405*6027Ssam 406*6027Ssam pupproto.sp_protocol = pup->pup_type; 407*6027Ssam pupdst.spup_addr = pup->pup_dport; 408*6027Ssam pupsrc.spup_addr = pup->pup_sport; 409*6027Ssam raw_input(m, &pupproto, &pupdst, &pupsrc); 410*6027Ssam goto setup; 411*6027Ssam } 412*6027Ssam } 4135083Swnj IF_ENQUEUE(inq, m); 4145105Swnj 4154688Swnj setup: 4165105Swnj /* 4175105Swnj * Reset for next packet. 4185105Swnj */ 4195105Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 4205083Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; 4214771Swnj addr->en_istat = EN_IEN|EN_GO; 4224688Swnj } 4234686Swnj 4245083Swnj /* 4255083Swnj * Ethernet output routine. 4265083Swnj * Encapsulate a packet of type family for the local net. 4275105Swnj * Use trailer local net encapsulation if enough data in first 4285105Swnj * packet leaves a multiple of 512 bytes of data in remainder. 4295083Swnj */ 4305083Swnj enoutput(ifp, m0, pf) 4315083Swnj struct ifnet *ifp; 4325083Swnj struct mbuf *m0; 4335083Swnj int pf; 4344686Swnj { 4355083Swnj int type, dest; 4365105Swnj register struct mbuf *m = m0; 4375083Swnj register struct en_header *en; 4385083Swnj int s; 4394686Swnj 4405927Sroot COUNT(ENOUTPUT); 4415083Swnj switch (pf) { 4425083Swnj 4435083Swnj #ifdef INET 4445083Swnj case PF_INET: { 4455083Swnj register struct ip *ip = mtod(m0, struct ip *); 4465083Swnj int off; 4475083Swnj 4485294Sroot #ifndef ENKLUDGE 4495105Swnj dest = ip->ip_dst.s_addr >> 24; 4505294Sroot #else 4515294Sroot dest = (ip->ip_dst.s_addr >> 8) & 0xff; 4525294Sroot #endif 4535239Sroot off = ntohs((u_short)ip->ip_len) - m->m_len; 4545356Sroot #ifndef ENKLUDGE 4555171Swnj if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) { 4565083Swnj type = ENPUP_TRAIL + (off>>9); 4575105Swnj m->m_off -= 2; 4585105Swnj m->m_len += 2; 4595105Swnj *mtod(m, u_short *) = ENPUP_IPTYPE; 4605105Swnj goto gottrailertype; 4615083Swnj } 4625356Sroot #endif 4635105Swnj type = ENPUP_IPTYPE; 4645105Swnj off = 0; 4655105Swnj goto gottype; 4665083Swnj } 4675083Swnj #endif 468*6027Ssam #ifdef PUP 469*6027Ssam case PF_PUP: { 470*6027Ssam register struct pup_header *pup = mtod(m, struct pup_header *); 4715083Swnj 472*6027Ssam dest = pup->pup_dhost; 473*6027Ssam off = pup->pup_length - m->m_len; 474*6027Ssam if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) { 475*6027Ssam type = ENPUP_TRAIL + (off>>9); 476*6027Ssam m->m_off -= 2; 477*6027Ssam m->m_len += 2; 478*6027Ssam *mtod(m, u_short *) = ENPUP_PUPTYPE; 479*6027Ssam goto gottrailertype; 480*6027Ssam } 481*6027Ssam type = ENPUP_PUPTYPE; 482*6027Ssam off = 0; 483*6027Ssam goto gottype; 484*6027Ssam } 485*6027Ssam #endif 486*6027Ssam 4875083Swnj default: 4885083Swnj printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf); 4895083Swnj m_freem(m0); 4905083Swnj return (0); 4914686Swnj } 4925105Swnj 4935171Swnj gottrailertype: 4945105Swnj /* 4955105Swnj * Packet to be sent as trailer: move first packet 4965105Swnj * (control information) to end of chain. 4975105Swnj */ 4985105Swnj while (m->m_next) 4995105Swnj m = m->m_next; 5005105Swnj m->m_next = m0; 5015105Swnj m = m0->m_next; 5025105Swnj m0->m_next = 0; 5035171Swnj m0 = m; 5045105Swnj 5055171Swnj gottype: 5065105Swnj /* 5075105Swnj * Add local net header. If no space in first mbuf, 5085105Swnj * allocate another. 5095105Swnj */ 5105213Swnj if (m->m_off > MMAXOFF || 5115213Swnj MMINOFF + sizeof (struct en_header) > m->m_off) { 5125584Sroot m = m_get(M_DONTWAIT); 5135083Swnj if (m == 0) { 5145083Swnj m_freem(m0); 5155083Swnj return (0); 5165083Swnj } 5175083Swnj m->m_next = m0; 5185083Swnj m->m_off = MMINOFF; 5195083Swnj m->m_len = sizeof (struct en_header); 5205083Swnj } else { 5215083Swnj m->m_off -= sizeof (struct en_header); 5225083Swnj m->m_len += sizeof (struct en_header); 5235083Swnj } 5245083Swnj en = mtod(m, struct en_header *); 5255083Swnj en->en_shost = ifp->if_host[0]; 5265083Swnj en->en_dhost = dest; 5275083Swnj en->en_type = type; 5285105Swnj 5295105Swnj /* 5305105Swnj * Queue message on interface, and start output if interface 5315105Swnj * not yet active. 5325105Swnj */ 5335083Swnj s = splimp(); 5345083Swnj IF_ENQUEUE(&ifp->if_snd, m); 5355083Swnj if (en_softc[ifp->if_unit].es_oactive == 0) 5365083Swnj enstart(ifp->if_unit); 5375275Swnj splx(s); 5385105Swnj return (1); 5394686Swnj } 540