123290Smckusick /* 229362Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 3*35319Sbostic * All rights reserved. 423290Smckusick * 5*35319Sbostic * Redistribution and use in source and binary forms are permitted 6*35319Sbostic * provided that the above copyright notice and this paragraph are 7*35319Sbostic * duplicated in all such forms and that any documentation, 8*35319Sbostic * advertising materials, and other materials related to such 9*35319Sbostic * distribution and use acknowledge that the software was developed 10*35319Sbostic * by the University of California, Berkeley. The name of the 11*35319Sbostic * University may not be used to endorse or promote products derived 12*35319Sbostic * from this software without specific prior written permission. 13*35319Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*35319Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*35319Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16*35319Sbostic * 17*35319Sbostic * @(#)if_en.c 7.2 (Berkeley) 08/04/88 1823290Smckusick */ 194686Swnj 204686Swnj #include "en.h" 2125272Sbloom #if NEN > 0 225105Swnj 234686Swnj /* 245105Swnj * Xerox prototype (3 Mb) Ethernet interface driver. 254686Swnj */ 269796Ssam #include "../machine/pte.h" 274686Swnj 2817113Sbloom #include "param.h" 2917113Sbloom #include "systm.h" 3017113Sbloom #include "mbuf.h" 3117113Sbloom #include "buf.h" 3217113Sbloom #include "protosw.h" 3317113Sbloom #include "socket.h" 3417113Sbloom #include "vmmac.h" 3517113Sbloom #include "errno.h" 3617113Sbloom #include "ioctl.h" 378462Sroot 388462Sroot #include "../net/if.h" 398462Sroot #include "../net/netisr.h" 408462Sroot #include "../net/route.h" 4124790Skarels 4224790Skarels #ifdef INET 438418Swnj #include "../netinet/in.h" 448418Swnj #include "../netinet/in_systm.h" 4519864Skarels #include "../netinet/in_var.h" 468418Swnj #include "../netinet/ip.h" 4724790Skarels #endif 4824790Skarels 4919950Sbloom #ifdef PUP 508418Swnj #include "../netpup/pup.h" 5113458Ssam #include "../netpup/ether.h" 5219950Sbloom #endif 534686Swnj 5425610Ssklower #ifdef NS 5525610Ssklower #include "../netns/ns.h" 5625610Ssklower #include "../netns/ns_if.h" 5725610Ssklower #endif 5825610Ssklower 598462Sroot #include "../vax/cpu.h" 608462Sroot #include "../vax/mtpr.h" 6117113Sbloom #include "if_en.h" 6217113Sbloom #include "if_enreg.h" 6317113Sbloom #include "if_uba.h" 648462Sroot #include "../vaxuba/ubareg.h" 658462Sroot #include "../vaxuba/ubavar.h" 668462Sroot 675213Swnj #define ENMTU (1024+512) 686925Swnj #define ENMRU (1024+512+16) /* 16 is enough to receive trailer */ 695083Swnj 704686Swnj int enprobe(), enattach(), enrint(), enxint(), encollide(); 714686Swnj struct uba_device *eninfo[NEN]; 724686Swnj u_short enstd[] = { 0 }; 734686Swnj struct uba_driver endriver = 745171Swnj { enprobe, 0, enattach, 0, enstd, "en", eninfo }; 754686Swnj #define ENUNIT(x) minor(x) 764686Swnj 7713054Ssam int eninit(),enoutput(),enreset(),enioctl(); 785105Swnj 7913293Ssam #ifdef notdef 805105Swnj /* 8113293Ssam * If you need to byte swap IP's in the system, define 8213293Ssam * this and do a SIOCSIFFLAGS at boot time. 8313293Ssam */ 8419864Skarels #define ENF_SWABIPS 0x1000 8513293Ssam #endif 8613293Ssam 8713293Ssam /* 885105Swnj * Ethernet software status per interface. 895105Swnj * 905105Swnj * Each interface is referenced by a network interface structure, 915105Swnj * es_if, which the routing code uses to locate the interface. 925105Swnj * This structure contains the output queue for the interface, its address, ... 935105Swnj * We also have, for each interface, a UBA interface structure, which 945105Swnj * contains information about the UNIBUS resources held by the interface: 955105Swnj * map registers, buffered data paths, etc. Information is cached in this 965105Swnj * structure for use by the if_uba.c routines in running the interface 975105Swnj * efficiently. 985105Swnj */ 995083Swnj struct en_softc { 1005105Swnj struct ifnet es_if; /* network-visible interface */ 1015105Swnj struct ifuba es_ifuba; /* UNIBUS resources */ 10216380Skarels short es_host; /* hardware host number */ 1035105Swnj short es_delay; /* current output delay */ 1045105Swnj short es_mask; /* mask for current output delay */ 1056471Sroot short es_lastx; /* host last transmitted to */ 1065105Swnj short es_oactive; /* is output active? */ 1075105Swnj short es_olen; /* length of last output */ 10825610Ssklower short es_nsactive; /* is interface enabled for ns? */ 1095083Swnj } en_softc[NEN]; 1104686Swnj 1115105Swnj /* 1125105Swnj * Do output DMA to determine interface presence and 1135105Swnj * interrupt vector. DMA is too short to disturb other hosts. 1145105Swnj */ 1154686Swnj enprobe(reg) 1164686Swnj caddr_t reg; 1174686Swnj { 1185171Swnj register int br, cvec; /* r11, r10 value-result */ 1194686Swnj register struct endevice *addr = (struct endevice *)reg; 1204686Swnj 1214686Swnj #ifdef lint 1224686Swnj br = 0; cvec = br; br = cvec; 1234922Swnj enrint(0); enxint(0); encollide(0); 1244686Swnj #endif 1254686Swnj addr->en_istat = 0; 1264686Swnj addr->en_owc = -1; 1274686Swnj addr->en_oba = 0; 1284771Swnj addr->en_ostat = EN_IEN|EN_GO; 1294686Swnj DELAY(100000); 1304686Swnj addr->en_ostat = 0; 1314686Swnj return (1); 1324686Swnj } 1334686Swnj 1345105Swnj /* 1355105Swnj * Interface exists: make available by filling in network interface 1365105Swnj * record. System will initialize the interface when it is ready 1375105Swnj * to accept packets. 1385105Swnj */ 1394686Swnj enattach(ui) 1404686Swnj struct uba_device *ui; 1414686Swnj { 1425105Swnj register struct en_softc *es = &en_softc[ui->ui_unit]; 1434688Swnj 1445105Swnj es->es_if.if_unit = ui->ui_unit; 1455171Swnj es->es_if.if_name = "en"; 1465105Swnj es->es_if.if_mtu = ENMTU; 14719864Skarels es->es_if.if_flags = IFF_BROADCAST; 1485171Swnj es->es_if.if_init = eninit; 1495105Swnj es->es_if.if_output = enoutput; 15013054Ssam es->es_if.if_ioctl = enioctl; 1518978Sroot es->es_if.if_reset = enreset; 1526554Ssam es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT; 1539271Ssam #if defined(VAX750) 1549271Ssam /* don't chew up 750 bdp's */ 1559271Ssam if (cpu == VAX_750 && ui->ui_unit > 0) 1569271Ssam es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP; 1579271Ssam #endif 1585160Swnj if_attach(&es->es_if); 1594686Swnj } 1604686Swnj 1615105Swnj /* 1625105Swnj * Reset of interface after UNIBUS reset. 1635105Swnj * If interface is on specified uba, reset its state. 1645105Swnj */ 1655105Swnj enreset(unit, uban) 1665105Swnj int unit, uban; 1675105Swnj { 1685105Swnj register struct uba_device *ui; 1695105Swnj 1705171Swnj if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || 1715171Swnj ui->ui_ubanum != uban) 1725105Swnj return; 1735171Swnj printf(" en%d", unit); 1745105Swnj eninit(unit); 1755105Swnj } 1765105Swnj 1775105Swnj /* 1785105Swnj * Initialization of interface; clear recorded pending 1795105Swnj * operations, and reinitialize UNIBUS usage. 1805105Swnj */ 1814688Swnj eninit(unit) 1824686Swnj int unit; 1835083Swnj { 1845171Swnj register struct en_softc *es = &en_softc[unit]; 1855171Swnj register struct uba_device *ui = eninfo[unit]; 1864686Swnj register struct endevice *addr; 18713054Ssam int s; 1884686Swnj 18919864Skarels if (es->es_if.if_addrlist == (struct ifaddr *)0) 19012349Ssam return; 1915105Swnj if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, 1926925Swnj sizeof (struct en_header), (int)btoc(ENMRU)) == 0) { 1935171Swnj printf("en%d: can't initialize\n", unit); 1946335Ssam es->es_if.if_flags &= ~IFF_UP; 1955083Swnj return; 1964686Swnj } 1974686Swnj addr = (struct endevice *)ui->ui_addr; 1985083Swnj addr->en_istat = addr->en_ostat = 0; 1994686Swnj 2005105Swnj /* 2015171Swnj * Hang a receive and start any 2025171Swnj * pending writes by faking a transmit complete. 2035105Swnj */ 2045105Swnj s = splimp(); 2055171Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 2066925Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 2075171Swnj addr->en_istat = EN_IEN|EN_GO; 2085171Swnj es->es_oactive = 1; 20919864Skarels es->es_if.if_flags |= IFF_RUNNING; 2105105Swnj enxint(unit); 2115105Swnj splx(s); 2124686Swnj } 2134686Swnj 2146471Sroot int enalldelay = 0; 2156951Swnj int enlastdel = 50; 2166471Sroot int enlastmask = (~0) << 5; 2175083Swnj 2185105Swnj /* 2195105Swnj * Start or restart output on interface. 2205105Swnj * If interface is already active, then this is a retransmit 2215105Swnj * after a collision, and just restuff registers and delay. 2225105Swnj * If interface is not already active, get another datagram 2235105Swnj * to send off of the interface queue, and map it to the interface 2245105Swnj * before starting the output. 2255105Swnj */ 2264688Swnj enstart(dev) 2274686Swnj dev_t dev; 2284686Swnj { 2295171Swnj int unit = ENUNIT(dev); 2305171Swnj struct uba_device *ui = eninfo[unit]; 2315171Swnj register struct en_softc *es = &en_softc[unit]; 2325083Swnj register struct endevice *addr; 23316380Skarels register struct en_header *en; 2345083Swnj struct mbuf *m; 2355083Swnj int dest; 2364686Swnj 2375083Swnj if (es->es_oactive) 2385083Swnj goto restart; 2395105Swnj 2405105Swnj /* 2415105Swnj * Not already active: dequeue another request 2425105Swnj * and map it to the UNIBUS. If no more requests, 2435105Swnj * just return. 2445105Swnj */ 2455105Swnj IF_DEQUEUE(&es->es_if.if_snd, m); 2465083Swnj if (m == 0) { 2475083Swnj es->es_oactive = 0; 2484686Swnj return; 2494686Swnj } 25016380Skarels en = mtod(m, struct en_header *); 25116380Skarels dest = en->en_dhost; 25216380Skarels en->en_shost = es->es_host; 2535105Swnj es->es_olen = if_wubaput(&es->es_ifuba, m); 25413293Ssam #ifdef ENF_SWABIPS 25513293Ssam /* 25613293Ssam * The Xerox interface does word at a time DMA, so 25713293Ssam * someone must do byte swapping of user data if high 25813293Ssam * and low ender machines are to communicate. It doesn't 25913293Ssam * belong here, but certain people depend on it, so... 26013293Ssam * 26113293Ssam * Should swab everybody, but this is a kludge anyway. 26213293Ssam */ 26313293Ssam if (es->es_if.if_flags & ENF_SWABIPS) { 26413293Ssam en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr; 26513293Ssam if (en->en_type == ENTYPE_IP) 26613293Ssam enswab((caddr_t)(en + 1), (caddr_t)(en + 1), 26713293Ssam es->es_olen - sizeof (struct en_header) + 1); 26813293Ssam } 26913293Ssam #endif 27013293Ssam 2715105Swnj /* 2725105Swnj * Ethernet cannot take back-to-back packets (no 2736471Sroot * buffering in interface. To help avoid overrunning 2746471Sroot * receivers, enforce a small delay (about 1ms) in interface: 2756471Sroot * * between all packets when enalldelay 2766471Sroot * * whenever last packet was broadcast 2776471Sroot * * whenever this packet is to same host as last packet 2785105Swnj */ 2796471Sroot if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) { 2805083Swnj es->es_delay = enlastdel; 2816471Sroot es->es_mask = enlastmask; 2826471Sroot } 2836471Sroot es->es_lastx = dest; 2845105Swnj 2855083Swnj restart: 2865105Swnj /* 2875105Swnj * Have request mapped to UNIBUS for transmission. 2885105Swnj * Purge any stale data from this BDP, and start the otput. 2895105Swnj */ 2906335Ssam if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 2916335Ssam UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); 2924686Swnj addr = (struct endevice *)ui->ui_addr; 2935160Swnj addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; 2945083Swnj addr->en_odelay = es->es_delay; 2955083Swnj addr->en_owc = -((es->es_olen + 1) >> 1); 2964771Swnj addr->en_ostat = EN_IEN|EN_GO; 2975083Swnj es->es_oactive = 1; 2984686Swnj } 2994686Swnj 3005105Swnj /* 3015105Swnj * Ethernet interface transmitter interrupt. 3025105Swnj * Start another output if more data to send. 3035105Swnj */ 3044686Swnj enxint(unit) 3054686Swnj int unit; 3064686Swnj { 3075171Swnj register struct uba_device *ui = eninfo[unit]; 3085171Swnj register struct en_softc *es = &en_softc[unit]; 3096242Sroot register struct endevice *addr = (struct endevice *)ui->ui_addr; 3104686Swnj 3115083Swnj if (es->es_oactive == 0) 3125083Swnj return; 3136242Sroot if (es->es_mask && (addr->en_ostat&EN_OERROR)) { 3146242Sroot es->es_if.if_oerrors++; 3156242Sroot endocoll(unit); 3166242Sroot return; 3176242Sroot } 3185171Swnj es->es_if.if_opackets++; 3195083Swnj es->es_oactive = 0; 3205083Swnj es->es_delay = 0; 3215083Swnj es->es_mask = ~0; 3225696Sroot if (es->es_ifuba.ifu_xtofree) { 3235696Sroot m_freem(es->es_ifuba.ifu_xtofree); 3245696Sroot es->es_ifuba.ifu_xtofree = 0; 3255696Sroot } 3265105Swnj if (es->es_if.if_snd.ifq_head == 0) { 3276471Sroot es->es_lastx = 256; /* putatively illegal */ 3284686Swnj return; 3294686Swnj } 3305083Swnj enstart(unit); 3314686Swnj } 3324686Swnj 3335105Swnj /* 3345105Swnj * Collision on ethernet interface. Do exponential 3355105Swnj * backoff, and retransmit. If have backed off all 3366335Ssam * the way print warning diagnostic, and drop packet. 3375105Swnj */ 3384686Swnj encollide(unit) 3394686Swnj int unit; 3404686Swnj { 3416242Sroot struct en_softc *es = &en_softc[unit]; 3424686Swnj 3435105Swnj es->es_if.if_collisions++; 3445083Swnj if (es->es_oactive == 0) 3454686Swnj return; 3466242Sroot endocoll(unit); 3476242Sroot } 3486242Sroot 3496242Sroot endocoll(unit) 3506242Sroot int unit; 3516242Sroot { 3526242Sroot register struct en_softc *es = &en_softc[unit]; 3536242Sroot 3545171Swnj /* 3555171Swnj * Es_mask is a 16 bit number with n low zero bits, with 3565171Swnj * n the number of backoffs. When es_mask is 0 we have 3575171Swnj * backed off 16 times, and give up. 3585171Swnj */ 3595083Swnj if (es->es_mask == 0) { 3605213Swnj printf("en%d: send error\n", unit); 3615083Swnj enxint(unit); 3625171Swnj return; 3634686Swnj } 3645171Swnj /* 3655171Swnj * Another backoff. Restart with delay based on n low bits 3665171Swnj * of the interval timer. 3675171Swnj */ 3685171Swnj es->es_mask <<= 1; 3695171Swnj es->es_delay = mfpr(ICR) &~ es->es_mask; 3705171Swnj enstart(unit); 3714686Swnj } 3724686Swnj 37313458Ssam #ifdef notdef 37413458Ssam struct sockproto enproto = { AF_ETHERLINK }; 37513458Ssam struct sockaddr_en endst = { AF_ETHERLINK }; 37613458Ssam struct sockaddr_en ensrc = { AF_ETHERLINK }; 37713458Ssam #endif 3785105Swnj /* 3795105Swnj * Ethernet interface receiver interrupt. 3805105Swnj * If input error just drop packet. 3815105Swnj * Otherwise purge input buffered data path and examine 3825105Swnj * packet to determine type. If can't determine length 3835105Swnj * from type, then have to drop packet. Othewise decapsulate 3845105Swnj * packet based on type and pass to type specific higher-level 3855105Swnj * input routine. 3865105Swnj */ 3874686Swnj enrint(unit) 3884686Swnj int unit; 3894686Swnj { 3905171Swnj register struct en_softc *es = &en_softc[unit]; 3915171Swnj struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 3925171Swnj register struct en_header *en; 3935083Swnj struct mbuf *m; 3948603Sroot int len; short resid; 3955171Swnj register struct ifqueue *inq; 39616511Skarels int off, s; 3974686Swnj 3985171Swnj es->es_if.if_ipackets++; 3995105Swnj 4005105Swnj /* 4015171Swnj * Purge BDP; drop if input error indicated. 4025105Swnj */ 4036335Ssam if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 4046335Ssam UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 4054771Swnj if (addr->en_istat&EN_IERROR) { 4065105Swnj es->es_if.if_ierrors++; 4075083Swnj goto setup; 4084686Swnj } 4095105Swnj 4105105Swnj /* 4116471Sroot * Calculate input data length. 4125105Swnj * Get pointer to ethernet header (in input buffer). 4135105Swnj * Deal with trailer protocol: if type is PUP trailer 4145105Swnj * get true type from first 16-bit word past data. 4155105Swnj * Remember that type was trailer by setting off. 4165105Swnj */ 4176471Sroot resid = addr->en_iwc; 4186471Sroot if (resid) 4196471Sroot resid |= 0176000; 4206925Swnj len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1; 4216471Sroot len -= sizeof (struct en_header); 4226925Swnj if (len > ENMRU) 4236471Sroot goto setup; /* sanity */ 4245105Swnj en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 42512352Ssam en->en_type = ntohs(en->en_type); 4265083Swnj #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 42712352Ssam if (en->en_type >= ENTYPE_TRAIL && 42812352Ssam en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) { 42912352Ssam off = (en->en_type - ENTYPE_TRAIL) * 512; 4306925Swnj if (off > ENMTU) 4315171Swnj goto setup; /* sanity */ 43212352Ssam en->en_type = ntohs(*endataaddr(en, off, u_short *)); 43312352Ssam resid = ntohs(*(endataaddr(en, off+2, u_short *))); 4346471Sroot if (off + resid > len) 4356471Sroot goto setup; /* sanity */ 4366471Sroot len = off + resid; 4375083Swnj } else 4385083Swnj off = 0; 4395083Swnj if (len == 0) 4405083Swnj goto setup; 44113293Ssam #ifdef ENF_SWABIPS 44213293Ssam if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP) 44313293Ssam enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len); 44413293Ssam #endif 4455105Swnj /* 4465105Swnj * Pull packet off interface. Off is nonzero if packet 4475105Swnj * has trailing header; if_rubaget will then force this header 4485105Swnj * information to be at the front, but we still have to drop 4496471Sroot * the type and length which are at the front of any trailer data. 4505105Swnj */ 45124790Skarels m = if_rubaget(&es->es_ifuba, len, off, &es->es_if); 4525213Swnj if (m == 0) 4535213Swnj goto setup; 4545105Swnj if (off) { 45524790Skarels struct ifnet *ifp; 45624790Skarels 45724790Skarels ifp = *(mtod(m, struct ifnet **)); 4586471Sroot m->m_off += 2 * sizeof (u_short); 4596471Sroot m->m_len -= 2 * sizeof (u_short); 46024790Skarels *(mtod(m, struct ifnet **)) = ifp; 4615105Swnj } 4626027Ssam switch (en->en_type) { 4636027Ssam 4646027Ssam #ifdef INET 46512352Ssam case ENTYPE_IP: 4666260Swnj schednetisr(NETISR_IP); 4676027Ssam inq = &ipintrq; 4686027Ssam break; 4696027Ssam #endif 4706335Ssam #ifdef PUP 47113458Ssam case ENTYPE_PUP: 47213458Ssam rpup_input(m); 4736027Ssam goto setup; 4746335Ssam #endif 47525610Ssklower #ifdef NS 47625610Ssklower case ETHERTYPE_NS: 47725610Ssklower if (es->es_nsactive) { 47825610Ssklower schednetisr(NETISR_NS); 47925610Ssklower inq = &nsintrq; 48025610Ssklower } else { 48125610Ssklower m_freem(m); 48225610Ssklower goto setup; 48325610Ssklower } 48425610Ssklower break; 48525610Ssklower #endif 48625610Ssklower 4876476Swnj default: 48813458Ssam #ifdef notdef 48913458Ssam enproto.sp_protocol = en->en_type; 49013458Ssam endst.sen_host = en->en_dhost; 49113458Ssam endst.sen_net = ensrc.sen_net = es->es_if.if_net; 49213458Ssam ensrc.sen_host = en->en_shost; 49313458Ssam raw_input(m, &enproto, 49413458Ssam (struct sockaddr *)&ensrc, (struct sockaddr *)&endst); 49513458Ssam #else 4966476Swnj m_freem(m); 49713458Ssam #endif 4986476Swnj goto setup; 4996335Ssam } 5006335Ssam 50116511Skarels s = splimp(); 5026207Swnj if (IF_QFULL(inq)) { 5036207Swnj IF_DROP(inq); 5046335Ssam m_freem(m); 5056207Swnj } else 5066207Swnj IF_ENQUEUE(inq, m); 50716511Skarels splx(s); 5085105Swnj 5094688Swnj setup: 5105105Swnj /* 5115105Swnj * Reset for next packet. 5125105Swnj */ 5135105Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 5146925Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 5154771Swnj addr->en_istat = EN_IEN|EN_GO; 5164688Swnj } 5174686Swnj 5185083Swnj /* 5195083Swnj * Ethernet output routine. 5205083Swnj * Encapsulate a packet of type family for the local net. 5215105Swnj * Use trailer local net encapsulation if enough data in first 5225105Swnj * packet leaves a multiple of 512 bytes of data in remainder. 5235083Swnj */ 5246335Ssam enoutput(ifp, m0, dst) 5255083Swnj struct ifnet *ifp; 5265083Swnj struct mbuf *m0; 5276335Ssam struct sockaddr *dst; 5284686Swnj { 5296503Ssam int type, dest, s, error; 5305105Swnj register struct mbuf *m = m0; 5315083Swnj register struct en_header *en; 5326335Ssam register int off; 5334686Swnj 53425446Skarels if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 53525446Skarels error = ENETDOWN; 53625446Skarels goto bad; 53725446Skarels } 5386335Ssam switch (dst->sa_family) { 5395083Swnj 5405083Swnj #ifdef INET 5416335Ssam case AF_INET: 54219864Skarels { 54319864Skarels struct in_addr in; 54419864Skarels 54519864Skarels in = ((struct sockaddr_in *)dst)->sin_addr; 54619864Skarels if (in_broadcast(in)) 54719864Skarels dest = EN_BROADCAST; 54819864Skarels else 54919864Skarels dest = in_lnaof(in); 55019864Skarels } 55119864Skarels if (dest >= 0x100) { 5526503Ssam error = EPERM; /* ??? */ 5536486Swnj goto bad; 5546503Ssam } 5556335Ssam off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 55613054Ssam /* need per host negotiation */ 55713054Ssam if ((ifp->if_flags & IFF_NOTRAILERS) == 0) 5586335Ssam if (off > 0 && (off & 0x1ff) == 0 && 5596471Sroot m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 56012352Ssam type = ENTYPE_TRAIL + (off>>9); 5616471Sroot m->m_off -= 2 * sizeof (u_short); 5626471Sroot m->m_len += 2 * sizeof (u_short); 56312352Ssam *mtod(m, u_short *) = htons((u_short)ENTYPE_IP); 56412352Ssam *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len); 5655105Swnj goto gottrailertype; 5665083Swnj } 56712352Ssam type = ENTYPE_IP; 5685105Swnj off = 0; 5695105Swnj goto gottype; 5705083Swnj #endif 57125610Ssklower #ifdef NS 57225610Ssklower case AF_NS: 57325610Ssklower { 57425610Ssklower u_char *up; 57525610Ssklower 57625610Ssklower type = ETHERTYPE_NS; 57725610Ssklower up = ((struct sockaddr_ns *)dst)->sns_addr.x_host.c_host; 57825610Ssklower if (*up & 1) 57925610Ssklower dest = EN_BROADCAST; 58025610Ssklower else 58125610Ssklower dest = up[5]; 58225610Ssklower 58325610Ssklower off = 0; 58425610Ssklower goto gottype; 58525610Ssklower } 58625610Ssklower #endif 5876027Ssam #ifdef PUP 5886335Ssam case AF_PUP: 58912833Ssam dest = ((struct sockaddr_pup *)dst)->spup_host; 59012352Ssam type = ENTYPE_PUP; 5916027Ssam off = 0; 5926027Ssam goto gottype; 5936027Ssam #endif 5946027Ssam 59513458Ssam #ifdef notdef 59613458Ssam case AF_ETHERLINK: 59713458Ssam goto gotheader; 59813458Ssam #endif 59913458Ssam 6005083Swnj default: 6016335Ssam printf("en%d: can't handle af%d\n", ifp->if_unit, 6026335Ssam dst->sa_family); 6036503Ssam error = EAFNOSUPPORT; 6046503Ssam goto bad; 6054686Swnj } 6065105Swnj 6075171Swnj gottrailertype: 6085105Swnj /* 6095105Swnj * Packet to be sent as trailer: move first packet 6105105Swnj * (control information) to end of chain. 6115105Swnj */ 6125105Swnj while (m->m_next) 6135105Swnj m = m->m_next; 6145105Swnj m->m_next = m0; 6155105Swnj m = m0->m_next; 6165105Swnj m0->m_next = 0; 6175171Swnj m0 = m; 6185105Swnj 6195171Swnj gottype: 6205105Swnj /* 6215105Swnj * Add local net header. If no space in first mbuf, 6225105Swnj * allocate another. 6235105Swnj */ 6245213Swnj if (m->m_off > MMAXOFF || 6255213Swnj MMINOFF + sizeof (struct en_header) > m->m_off) { 62617559Skarels MGET(m, M_DONTWAIT, MT_HEADER); 6275083Swnj if (m == 0) { 6286503Ssam error = ENOBUFS; 6296503Ssam goto bad; 6305083Swnj } 6315083Swnj m->m_next = m0; 6325083Swnj m->m_off = MMINOFF; 6335083Swnj m->m_len = sizeof (struct en_header); 6345083Swnj } else { 6355083Swnj m->m_off -= sizeof (struct en_header); 6365083Swnj m->m_len += sizeof (struct en_header); 6375083Swnj } 6385083Swnj en = mtod(m, struct en_header *); 63916380Skarels /* add en_shost later */ 6405083Swnj en->en_dhost = dest; 64112352Ssam en->en_type = htons((u_short)type); 6425105Swnj 64324790Skarels #ifdef notdef 64413458Ssam gotheader: 64524790Skarels #endif 6465105Swnj /* 6475105Swnj * Queue message on interface, and start output if interface 6485105Swnj * not yet active. 6495105Swnj */ 6505083Swnj s = splimp(); 6516207Swnj if (IF_QFULL(&ifp->if_snd)) { 6526207Swnj IF_DROP(&ifp->if_snd); 6536503Ssam error = ENOBUFS; 6546503Ssam goto qfull; 6556207Swnj } 6565083Swnj IF_ENQUEUE(&ifp->if_snd, m); 6575083Swnj if (en_softc[ifp->if_unit].es_oactive == 0) 6585083Swnj enstart(ifp->if_unit); 6595275Swnj splx(s); 6606503Ssam return (0); 6616503Ssam qfull: 6626503Ssam m0 = m; 6636503Ssam splx(s); 6646484Swnj bad: 6656503Ssam m_freem(m0); 6666503Ssam return (error); 6674686Swnj } 66813054Ssam 66913054Ssam /* 67013054Ssam * Process an ioctl request. 67113054Ssam */ 67213054Ssam enioctl(ifp, cmd, data) 67313054Ssam register struct ifnet *ifp; 67413054Ssam int cmd; 67513054Ssam caddr_t data; 67613054Ssam { 67719864Skarels register struct en_softc *es = ((struct en_softc *)ifp); 67819864Skarels struct ifaddr *ifa = (struct ifaddr *) data; 67913054Ssam int s = splimp(), error = 0; 68019864Skarels struct endevice *enaddr; 68113054Ssam 68213054Ssam switch (cmd) { 68313054Ssam 68413054Ssam case SIOCSIFADDR: 68519864Skarels enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr; 68619864Skarels es->es_host = (~enaddr->en_addr) & 0xff; 68719864Skarels /* 68819864Skarels * Attempt to check agreement of protocol address 68919864Skarels * and board address. 69019864Skarels */ 69119864Skarels switch (ifa->ifa_addr.sa_family) { 69219864Skarels case AF_INET: 69319864Skarels if (in_lnaof(IA_SIN(ifa)->sin_addr) != es->es_host) 69419864Skarels return (EADDRNOTAVAIL); 69519864Skarels break; 69625610Ssklower #ifdef NS 69725610Ssklower case AF_NS: 69825610Ssklower if (IA_SNS(ifa)->sns_addr.x_host.c_host[5] 69925610Ssklower != es->es_host) 70025610Ssklower return (EADDRNOTAVAIL); 70125610Ssklower es->es_nsactive = 1; 70225610Ssklower break; 70325610Ssklower #endif 70419864Skarels } 70519864Skarels ifp->if_flags |= IFF_UP; 70619864Skarels if ((ifp->if_flags & IFF_RUNNING) == 0) 70713054Ssam eninit(ifp->if_unit); 70813054Ssam break; 70913054Ssam 71013054Ssam default: 71113054Ssam error = EINVAL; 71224790Skarels break; 71313054Ssam } 71413054Ssam splx(s); 71513054Ssam return (error); 71613054Ssam } 71713054Ssam 71813293Ssam #ifdef ENF_SWABIPS 71913293Ssam /* 72013293Ssam * Swab bytes 72113293Ssam * Jeffrey Mogul, Stanford 72213293Ssam */ 72313293Ssam enswab(from, to, n) 72419864Skarels register unsigned char *from, *to; 72513293Ssam register int n; 72613293Ssam { 72713293Ssam register unsigned long temp; 72819864Skarels 72919864Skarels if ((n <= 0) || (n > 0xFFFF)) { 73019864Skarels printf("enswab: bad len %d\n", n); 73119864Skarels return; 73219864Skarels } 73313293Ssam 73413293Ssam n >>= 1; n++; 73519864Skarels #define STEP {temp = *from++;*to++ = *from++;*to++ = temp;} 73613293Ssam /* round to multiple of 8 */ 73713293Ssam while ((--n) & 07) 73813293Ssam STEP; 73913293Ssam n >>= 3; 74013293Ssam while (--n >= 0) { 74113293Ssam STEP; STEP; STEP; STEP; 74213293Ssam STEP; STEP; STEP; STEP; 74313293Ssam } 74413293Ssam } 74513293Ssam #endif 74625272Sbloom #endif 747