1*16511Skarels /* if_en.c 6.3 84/05/18 */ 24686Swnj 34686Swnj #include "en.h" 45105Swnj 54686Swnj /* 65105Swnj * Xerox prototype (3 Mb) Ethernet interface driver. 74686Swnj */ 89796Ssam #include "../machine/pte.h" 94686Swnj 104686Swnj #include "../h/param.h" 114686Swnj #include "../h/systm.h" 124686Swnj #include "../h/mbuf.h" 134686Swnj #include "../h/buf.h" 145083Swnj #include "../h/protosw.h" 155083Swnj #include "../h/socket.h" 165083Swnj #include "../h/vmmac.h" 1713054Ssam #include "../h/errno.h" 1813054Ssam #include "../h/ioctl.h" 198462Sroot 208462Sroot #include "../net/if.h" 218462Sroot #include "../net/netisr.h" 228462Sroot #include "../net/route.h" 238418Swnj #include "../netinet/in.h" 248418Swnj #include "../netinet/in_systm.h" 258418Swnj #include "../netinet/ip.h" 268418Swnj #include "../netinet/ip_var.h" 278418Swnj #include "../netpup/pup.h" 2813458Ssam #include "../netpup/ether.h" 294686Swnj 308462Sroot #include "../vax/cpu.h" 318462Sroot #include "../vax/mtpr.h" 328462Sroot #include "../vaxif/if_en.h" 338462Sroot #include "../vaxif/if_enreg.h" 348462Sroot #include "../vaxif/if_uba.h" 358462Sroot #include "../vaxuba/ubareg.h" 368462Sroot #include "../vaxuba/ubavar.h" 378462Sroot 385213Swnj #define ENMTU (1024+512) 396925Swnj #define ENMRU (1024+512+16) /* 16 is enough to receive trailer */ 405083Swnj 414686Swnj int enprobe(), enattach(), enrint(), enxint(), encollide(); 424686Swnj struct uba_device *eninfo[NEN]; 434686Swnj u_short enstd[] = { 0 }; 444686Swnj struct uba_driver endriver = 455171Swnj { enprobe, 0, enattach, 0, enstd, "en", eninfo }; 464686Swnj #define ENUNIT(x) minor(x) 474686Swnj 4813054Ssam int eninit(),enoutput(),enreset(),enioctl(); 495105Swnj 5013293Ssam #ifdef notdef 515105Swnj /* 5213293Ssam * If you need to byte swap IP's in the system, define 5313293Ssam * this and do a SIOCSIFFLAGS at boot time. 5413293Ssam */ 5513293Ssam #define ENF_SWABIPS 0x100 5613293Ssam #endif 5713293Ssam 5813293Ssam /* 595105Swnj * Ethernet software status per interface. 605105Swnj * 615105Swnj * Each interface is referenced by a network interface structure, 625105Swnj * es_if, which the routing code uses to locate the interface. 635105Swnj * This structure contains the output queue for the interface, its address, ... 645105Swnj * We also have, for each interface, a UBA interface structure, which 655105Swnj * contains information about the UNIBUS resources held by the interface: 665105Swnj * map registers, buffered data paths, etc. Information is cached in this 675105Swnj * structure for use by the if_uba.c routines in running the interface 685105Swnj * efficiently. 695105Swnj */ 705083Swnj struct en_softc { 715105Swnj struct ifnet es_if; /* network-visible interface */ 725105Swnj struct ifuba es_ifuba; /* UNIBUS resources */ 7316380Skarels short es_host; /* hardware host number */ 745105Swnj short es_delay; /* current output delay */ 755105Swnj short es_mask; /* mask for current output delay */ 766471Sroot short es_lastx; /* host last transmitted to */ 775105Swnj short es_oactive; /* is output active? */ 785105Swnj short es_olen; /* length of last output */ 795083Swnj } en_softc[NEN]; 804686Swnj 815105Swnj /* 825105Swnj * Do output DMA to determine interface presence and 835105Swnj * interrupt vector. DMA is too short to disturb other hosts. 845105Swnj */ 854686Swnj enprobe(reg) 864686Swnj caddr_t reg; 874686Swnj { 885171Swnj register int br, cvec; /* r11, r10 value-result */ 894686Swnj register struct endevice *addr = (struct endevice *)reg; 904686Swnj 914686Swnj #ifdef lint 924686Swnj br = 0; cvec = br; br = cvec; 934922Swnj enrint(0); enxint(0); encollide(0); 944686Swnj #endif 954686Swnj addr->en_istat = 0; 964686Swnj addr->en_owc = -1; 974686Swnj addr->en_oba = 0; 984771Swnj addr->en_ostat = EN_IEN|EN_GO; 994686Swnj DELAY(100000); 1004686Swnj addr->en_ostat = 0; 1014686Swnj return (1); 1024686Swnj } 1034686Swnj 1045105Swnj /* 1055105Swnj * Interface exists: make available by filling in network interface 1065105Swnj * record. System will initialize the interface when it is ready 1075105Swnj * to accept packets. 1085105Swnj */ 1094686Swnj enattach(ui) 1104686Swnj struct uba_device *ui; 1114686Swnj { 1125105Swnj register struct en_softc *es = &en_softc[ui->ui_unit]; 1134688Swnj 1145105Swnj es->es_if.if_unit = ui->ui_unit; 1155171Swnj es->es_if.if_name = "en"; 1165105Swnj es->es_if.if_mtu = ENMTU; 1175171Swnj es->es_if.if_init = eninit; 1185105Swnj es->es_if.if_output = enoutput; 11913054Ssam es->es_if.if_ioctl = enioctl; 1208978Sroot es->es_if.if_reset = enreset; 1216554Ssam es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT; 1229271Ssam #if defined(VAX750) 1239271Ssam /* don't chew up 750 bdp's */ 1249271Ssam if (cpu == VAX_750 && ui->ui_unit > 0) 1259271Ssam es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP; 1269271Ssam #endif 1275160Swnj if_attach(&es->es_if); 1284686Swnj } 1294686Swnj 1305105Swnj /* 1315105Swnj * Reset of interface after UNIBUS reset. 1325105Swnj * If interface is on specified uba, reset its state. 1335105Swnj */ 1345105Swnj enreset(unit, uban) 1355105Swnj int unit, uban; 1365105Swnj { 1375105Swnj register struct uba_device *ui; 1385105Swnj 1395171Swnj if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || 1405171Swnj ui->ui_ubanum != uban) 1415105Swnj return; 1425171Swnj printf(" en%d", unit); 1435105Swnj eninit(unit); 1445105Swnj } 1455105Swnj 1465105Swnj /* 1475105Swnj * Initialization of interface; clear recorded pending 1485105Swnj * operations, and reinitialize UNIBUS usage. 1495105Swnj */ 1504688Swnj eninit(unit) 1514686Swnj int unit; 1525083Swnj { 1535171Swnj register struct en_softc *es = &en_softc[unit]; 1545171Swnj register struct uba_device *ui = eninfo[unit]; 1554686Swnj register struct endevice *addr; 15612349Ssam struct sockaddr_in *sin = (struct sockaddr_in *)&es->es_if.if_addr; 15713054Ssam int s; 1584686Swnj 15913054Ssam if (in_netof(sin->sin_addr) == 0) 16012349Ssam return; 1615105Swnj if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, 1626925Swnj sizeof (struct en_header), (int)btoc(ENMRU)) == 0) { 1635171Swnj printf("en%d: can't initialize\n", unit); 1646335Ssam es->es_if.if_flags &= ~IFF_UP; 1655083Swnj return; 1664686Swnj } 1674686Swnj addr = (struct endevice *)ui->ui_addr; 1685083Swnj addr->en_istat = addr->en_ostat = 0; 1694686Swnj 1705105Swnj /* 1715171Swnj * Hang a receive and start any 1725171Swnj * pending writes by faking a transmit complete. 1735105Swnj */ 1745105Swnj s = splimp(); 1755171Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 1766925Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 1775171Swnj addr->en_istat = EN_IEN|EN_GO; 1785171Swnj es->es_oactive = 1; 17913054Ssam es->es_if.if_flags |= IFF_UP|IFF_RUNNING; 1805105Swnj enxint(unit); 1815105Swnj splx(s); 1827151Swnj if_rtinit(&es->es_if, RTF_UP); 1834686Swnj } 1844686Swnj 1856471Sroot int enalldelay = 0; 1866951Swnj int enlastdel = 50; 1876471Sroot int enlastmask = (~0) << 5; 1885083Swnj 1895105Swnj /* 1905105Swnj * Start or restart output on interface. 1915105Swnj * If interface is already active, then this is a retransmit 1925105Swnj * after a collision, and just restuff registers and delay. 1935105Swnj * If interface is not already active, get another datagram 1945105Swnj * to send off of the interface queue, and map it to the interface 1955105Swnj * before starting the output. 1965105Swnj */ 1974688Swnj enstart(dev) 1984686Swnj dev_t dev; 1994686Swnj { 2005171Swnj int unit = ENUNIT(dev); 2015171Swnj struct uba_device *ui = eninfo[unit]; 2025171Swnj register struct en_softc *es = &en_softc[unit]; 2035083Swnj register struct endevice *addr; 20416380Skarels register struct en_header *en; 2055083Swnj struct mbuf *m; 2065083Swnj int dest; 2074686Swnj 2085083Swnj if (es->es_oactive) 2095083Swnj goto restart; 2105105Swnj 2115105Swnj /* 2125105Swnj * Not already active: dequeue another request 2135105Swnj * and map it to the UNIBUS. If no more requests, 2145105Swnj * just return. 2155105Swnj */ 2165105Swnj IF_DEQUEUE(&es->es_if.if_snd, m); 2175083Swnj if (m == 0) { 2185083Swnj es->es_oactive = 0; 2194686Swnj return; 2204686Swnj } 22116380Skarels en = mtod(m, struct en_header *); 22216380Skarels dest = en->en_dhost; 22316380Skarels en->en_shost = es->es_host; 2245105Swnj es->es_olen = if_wubaput(&es->es_ifuba, m); 22513293Ssam #ifdef ENF_SWABIPS 22613293Ssam /* 22713293Ssam * The Xerox interface does word at a time DMA, so 22813293Ssam * someone must do byte swapping of user data if high 22913293Ssam * and low ender machines are to communicate. It doesn't 23013293Ssam * belong here, but certain people depend on it, so... 23113293Ssam * 23213293Ssam * Should swab everybody, but this is a kludge anyway. 23313293Ssam */ 23413293Ssam if (es->es_if.if_flags & ENF_SWABIPS) { 23513293Ssam en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr; 23613293Ssam if (en->en_type == ENTYPE_IP) 23713293Ssam enswab((caddr_t)(en + 1), (caddr_t)(en + 1), 23813293Ssam es->es_olen - sizeof (struct en_header) + 1); 23913293Ssam } 24013293Ssam #endif 24113293Ssam 2425105Swnj /* 2435105Swnj * Ethernet cannot take back-to-back packets (no 2446471Sroot * buffering in interface. To help avoid overrunning 2456471Sroot * receivers, enforce a small delay (about 1ms) in interface: 2466471Sroot * * between all packets when enalldelay 2476471Sroot * * whenever last packet was broadcast 2486471Sroot * * whenever this packet is to same host as last packet 2495105Swnj */ 2506471Sroot if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) { 2515083Swnj es->es_delay = enlastdel; 2526471Sroot es->es_mask = enlastmask; 2536471Sroot } 2546471Sroot es->es_lastx = dest; 2555105Swnj 2565083Swnj restart: 2575105Swnj /* 2585105Swnj * Have request mapped to UNIBUS for transmission. 2595105Swnj * Purge any stale data from this BDP, and start the otput. 2605105Swnj */ 2616335Ssam if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 2626335Ssam UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); 2634686Swnj addr = (struct endevice *)ui->ui_addr; 2645160Swnj addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; 2655083Swnj addr->en_odelay = es->es_delay; 2665083Swnj addr->en_owc = -((es->es_olen + 1) >> 1); 2674771Swnj addr->en_ostat = EN_IEN|EN_GO; 2685083Swnj es->es_oactive = 1; 2694686Swnj } 2704686Swnj 2715105Swnj /* 2725105Swnj * Ethernet interface transmitter interrupt. 2735105Swnj * Start another output if more data to send. 2745105Swnj */ 2754686Swnj enxint(unit) 2764686Swnj int unit; 2774686Swnj { 2785171Swnj register struct uba_device *ui = eninfo[unit]; 2795171Swnj register struct en_softc *es = &en_softc[unit]; 2806242Sroot register struct endevice *addr = (struct endevice *)ui->ui_addr; 2814686Swnj 2825083Swnj if (es->es_oactive == 0) 2835083Swnj return; 2846242Sroot if (es->es_mask && (addr->en_ostat&EN_OERROR)) { 2856242Sroot es->es_if.if_oerrors++; 2866242Sroot endocoll(unit); 2876242Sroot return; 2886242Sroot } 2895171Swnj es->es_if.if_opackets++; 2905083Swnj es->es_oactive = 0; 2915083Swnj es->es_delay = 0; 2925083Swnj es->es_mask = ~0; 2935696Sroot if (es->es_ifuba.ifu_xtofree) { 2945696Sroot m_freem(es->es_ifuba.ifu_xtofree); 2955696Sroot es->es_ifuba.ifu_xtofree = 0; 2965696Sroot } 2975105Swnj if (es->es_if.if_snd.ifq_head == 0) { 2986471Sroot es->es_lastx = 256; /* putatively illegal */ 2994686Swnj return; 3004686Swnj } 3015083Swnj enstart(unit); 3024686Swnj } 3034686Swnj 3045105Swnj /* 3055105Swnj * Collision on ethernet interface. Do exponential 3065105Swnj * backoff, and retransmit. If have backed off all 3076335Ssam * the way print warning diagnostic, and drop packet. 3085105Swnj */ 3094686Swnj encollide(unit) 3104686Swnj int unit; 3114686Swnj { 3126242Sroot struct en_softc *es = &en_softc[unit]; 3134686Swnj 3145105Swnj es->es_if.if_collisions++; 3155083Swnj if (es->es_oactive == 0) 3164686Swnj return; 3176242Sroot endocoll(unit); 3186242Sroot } 3196242Sroot 3206242Sroot endocoll(unit) 3216242Sroot int unit; 3226242Sroot { 3236242Sroot register struct en_softc *es = &en_softc[unit]; 3246242Sroot 3255171Swnj /* 3265171Swnj * Es_mask is a 16 bit number with n low zero bits, with 3275171Swnj * n the number of backoffs. When es_mask is 0 we have 3285171Swnj * backed off 16 times, and give up. 3295171Swnj */ 3305083Swnj if (es->es_mask == 0) { 3315213Swnj printf("en%d: send error\n", unit); 3325083Swnj enxint(unit); 3335171Swnj return; 3344686Swnj } 3355171Swnj /* 3365171Swnj * Another backoff. Restart with delay based on n low bits 3375171Swnj * of the interval timer. 3385171Swnj */ 3395171Swnj es->es_mask <<= 1; 3405171Swnj es->es_delay = mfpr(ICR) &~ es->es_mask; 3415171Swnj enstart(unit); 3424686Swnj } 3434686Swnj 34413458Ssam #ifdef notdef 34513458Ssam struct sockproto enproto = { AF_ETHERLINK }; 34613458Ssam struct sockaddr_en endst = { AF_ETHERLINK }; 34713458Ssam struct sockaddr_en ensrc = { AF_ETHERLINK }; 34813458Ssam #endif 3495105Swnj /* 3505105Swnj * Ethernet interface receiver interrupt. 3515105Swnj * If input error just drop packet. 3525105Swnj * Otherwise purge input buffered data path and examine 3535105Swnj * packet to determine type. If can't determine length 3545105Swnj * from type, then have to drop packet. Othewise decapsulate 3555105Swnj * packet based on type and pass to type specific higher-level 3565105Swnj * input routine. 3575105Swnj */ 3584686Swnj enrint(unit) 3594686Swnj int unit; 3604686Swnj { 3615171Swnj register struct en_softc *es = &en_softc[unit]; 3625171Swnj struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; 3635171Swnj register struct en_header *en; 3645083Swnj struct mbuf *m; 3658603Sroot int len; short resid; 3665171Swnj register struct ifqueue *inq; 367*16511Skarels int off, s; 3684686Swnj 3695171Swnj es->es_if.if_ipackets++; 3705105Swnj 3715105Swnj /* 3725171Swnj * Purge BDP; drop if input error indicated. 3735105Swnj */ 3746335Ssam if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) 3756335Ssam UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); 3764771Swnj if (addr->en_istat&EN_IERROR) { 3775105Swnj es->es_if.if_ierrors++; 3785083Swnj goto setup; 3794686Swnj } 3805105Swnj 3815105Swnj /* 3826471Sroot * Calculate input data length. 3835105Swnj * Get pointer to ethernet header (in input buffer). 3845105Swnj * Deal with trailer protocol: if type is PUP trailer 3855105Swnj * get true type from first 16-bit word past data. 3865105Swnj * Remember that type was trailer by setting off. 3875105Swnj */ 3886471Sroot resid = addr->en_iwc; 3896471Sroot if (resid) 3906471Sroot resid |= 0176000; 3916925Swnj len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1; 3926471Sroot len -= sizeof (struct en_header); 3936925Swnj if (len > ENMRU) 3946471Sroot goto setup; /* sanity */ 3955105Swnj en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); 39612352Ssam en->en_type = ntohs(en->en_type); 3975083Swnj #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) 39812352Ssam if (en->en_type >= ENTYPE_TRAIL && 39912352Ssam en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) { 40012352Ssam off = (en->en_type - ENTYPE_TRAIL) * 512; 4016925Swnj if (off > ENMTU) 4025171Swnj goto setup; /* sanity */ 40312352Ssam en->en_type = ntohs(*endataaddr(en, off, u_short *)); 40412352Ssam resid = ntohs(*(endataaddr(en, off+2, u_short *))); 4056471Sroot if (off + resid > len) 4066471Sroot goto setup; /* sanity */ 4076471Sroot len = off + resid; 4085083Swnj } else 4095083Swnj off = 0; 4105083Swnj if (len == 0) 4115083Swnj goto setup; 41213293Ssam #ifdef ENF_SWABIPS 41313293Ssam if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP) 41413293Ssam enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len); 41513293Ssam #endif 4165105Swnj /* 4175105Swnj * Pull packet off interface. Off is nonzero if packet 4185105Swnj * has trailing header; if_rubaget will then force this header 4195105Swnj * information to be at the front, but we still have to drop 4206471Sroot * the type and length which are at the front of any trailer data. 4215105Swnj */ 4225105Swnj m = if_rubaget(&es->es_ifuba, len, off); 4235213Swnj if (m == 0) 4245213Swnj goto setup; 4255105Swnj if (off) { 4266471Sroot m->m_off += 2 * sizeof (u_short); 4276471Sroot m->m_len -= 2 * sizeof (u_short); 4285105Swnj } 4296027Ssam switch (en->en_type) { 4306027Ssam 4316027Ssam #ifdef INET 43212352Ssam case ENTYPE_IP: 4336260Swnj schednetisr(NETISR_IP); 4346027Ssam inq = &ipintrq; 4356027Ssam break; 4366027Ssam #endif 4376335Ssam #ifdef PUP 43813458Ssam case ENTYPE_PUP: 43913458Ssam rpup_input(m); 4406027Ssam goto setup; 4416335Ssam #endif 4426476Swnj default: 44313458Ssam #ifdef notdef 44413458Ssam enproto.sp_protocol = en->en_type; 44513458Ssam endst.sen_host = en->en_dhost; 44613458Ssam endst.sen_net = ensrc.sen_net = es->es_if.if_net; 44713458Ssam ensrc.sen_host = en->en_shost; 44813458Ssam raw_input(m, &enproto, 44913458Ssam (struct sockaddr *)&ensrc, (struct sockaddr *)&endst); 45013458Ssam #else 4516476Swnj m_freem(m); 45213458Ssam #endif 4536476Swnj goto setup; 4546335Ssam } 4556335Ssam 456*16511Skarels s = splimp(); 4576207Swnj if (IF_QFULL(inq)) { 4586207Swnj IF_DROP(inq); 4596335Ssam m_freem(m); 4606207Swnj } else 4616207Swnj IF_ENQUEUE(inq, m); 462*16511Skarels splx(s); 4635105Swnj 4644688Swnj setup: 4655105Swnj /* 4665105Swnj * Reset for next packet. 4675105Swnj */ 4685105Swnj addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; 4696925Swnj addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; 4704771Swnj addr->en_istat = EN_IEN|EN_GO; 4714688Swnj } 4724686Swnj 4735083Swnj /* 4745083Swnj * Ethernet output routine. 4755083Swnj * Encapsulate a packet of type family for the local net. 4765105Swnj * Use trailer local net encapsulation if enough data in first 4775105Swnj * packet leaves a multiple of 512 bytes of data in remainder. 4785083Swnj */ 4796335Ssam enoutput(ifp, m0, dst) 4805083Swnj struct ifnet *ifp; 4815083Swnj struct mbuf *m0; 4826335Ssam struct sockaddr *dst; 4834686Swnj { 4846503Ssam int type, dest, s, error; 4855105Swnj register struct mbuf *m = m0; 4865083Swnj register struct en_header *en; 4876335Ssam register int off; 4884686Swnj 4896335Ssam switch (dst->sa_family) { 4905083Swnj 4915083Swnj #ifdef INET 4926335Ssam case AF_INET: 4936484Swnj dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 4949178Ssam if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) { 4956503Ssam error = EPERM; /* ??? */ 4966486Swnj goto bad; 4976503Ssam } 4986484Swnj dest = (dest >> 24) & 0xff; 4996335Ssam off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 50013054Ssam /* need per host negotiation */ 50113054Ssam if ((ifp->if_flags & IFF_NOTRAILERS) == 0) 5026335Ssam if (off > 0 && (off & 0x1ff) == 0 && 5036471Sroot m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 50412352Ssam type = ENTYPE_TRAIL + (off>>9); 5056471Sroot m->m_off -= 2 * sizeof (u_short); 5066471Sroot m->m_len += 2 * sizeof (u_short); 50712352Ssam *mtod(m, u_short *) = htons((u_short)ENTYPE_IP); 50812352Ssam *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len); 5095105Swnj goto gottrailertype; 5105083Swnj } 51112352Ssam type = ENTYPE_IP; 5125105Swnj off = 0; 5135105Swnj goto gottype; 5145083Swnj #endif 5156027Ssam #ifdef PUP 5166335Ssam case AF_PUP: 51712833Ssam dest = ((struct sockaddr_pup *)dst)->spup_host; 51812352Ssam type = ENTYPE_PUP; 5196027Ssam off = 0; 5206027Ssam goto gottype; 5216027Ssam #endif 5226027Ssam 52313458Ssam #ifdef notdef 52413458Ssam case AF_ETHERLINK: 52513458Ssam goto gotheader; 52613458Ssam #endif 52713458Ssam 5285083Swnj default: 5296335Ssam printf("en%d: can't handle af%d\n", ifp->if_unit, 5306335Ssam dst->sa_family); 5316503Ssam error = EAFNOSUPPORT; 5326503Ssam goto bad; 5334686Swnj } 5345105Swnj 5355171Swnj gottrailertype: 5365105Swnj /* 5375105Swnj * Packet to be sent as trailer: move first packet 5385105Swnj * (control information) to end of chain. 5395105Swnj */ 5405105Swnj while (m->m_next) 5415105Swnj m = m->m_next; 5425105Swnj m->m_next = m0; 5435105Swnj m = m0->m_next; 5445105Swnj m0->m_next = 0; 5455171Swnj m0 = m; 5465105Swnj 5475171Swnj gottype: 5485105Swnj /* 5495105Swnj * Add local net header. If no space in first mbuf, 5505105Swnj * allocate another. 5515105Swnj */ 5525213Swnj if (m->m_off > MMAXOFF || 5535213Swnj MMINOFF + sizeof (struct en_header) > m->m_off) { 5549649Ssam m = m_get(M_DONTWAIT, MT_HEADER); 5555083Swnj if (m == 0) { 5566503Ssam error = ENOBUFS; 5576503Ssam goto bad; 5585083Swnj } 5595083Swnj m->m_next = m0; 5605083Swnj m->m_off = MMINOFF; 5615083Swnj m->m_len = sizeof (struct en_header); 5625083Swnj } else { 5635083Swnj m->m_off -= sizeof (struct en_header); 5645083Swnj m->m_len += sizeof (struct en_header); 5655083Swnj } 5665083Swnj en = mtod(m, struct en_header *); 56716380Skarels /* add en_shost later */ 5685083Swnj en->en_dhost = dest; 56912352Ssam en->en_type = htons((u_short)type); 5705105Swnj 57113458Ssam gotheader: 5725105Swnj /* 5735105Swnj * Queue message on interface, and start output if interface 5745105Swnj * not yet active. 5755105Swnj */ 5765083Swnj s = splimp(); 5776207Swnj if (IF_QFULL(&ifp->if_snd)) { 5786207Swnj IF_DROP(&ifp->if_snd); 5796503Ssam error = ENOBUFS; 5806503Ssam goto qfull; 5816207Swnj } 5825083Swnj IF_ENQUEUE(&ifp->if_snd, m); 5835083Swnj if (en_softc[ifp->if_unit].es_oactive == 0) 5845083Swnj enstart(ifp->if_unit); 5855275Swnj splx(s); 5866503Ssam return (0); 5876503Ssam qfull: 5886503Ssam m0 = m; 5896503Ssam splx(s); 5906484Swnj bad: 5916503Ssam m_freem(m0); 5926503Ssam return (error); 5934686Swnj } 59413054Ssam 59513054Ssam /* 59613054Ssam * Process an ioctl request. 59713054Ssam */ 59813054Ssam enioctl(ifp, cmd, data) 59913054Ssam register struct ifnet *ifp; 60013054Ssam int cmd; 60113054Ssam caddr_t data; 60213054Ssam { 60313054Ssam struct ifreq *ifr = (struct ifreq *)data; 60413054Ssam int s = splimp(), error = 0; 60513054Ssam 60613054Ssam switch (cmd) { 60713054Ssam 60813054Ssam case SIOCSIFADDR: 60913054Ssam if (ifp->if_flags & IFF_RUNNING) 61013054Ssam if_rtinit(ifp, -1); /* delete previous route */ 61113054Ssam ensetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); 61213054Ssam if (ifp->if_flags & IFF_RUNNING) 61313054Ssam if_rtinit(ifp, RTF_UP); 61413054Ssam else 61513054Ssam eninit(ifp->if_unit); 61613054Ssam break; 61713054Ssam 61813054Ssam default: 61913054Ssam error = EINVAL; 62013054Ssam } 62113054Ssam splx(s); 62213054Ssam return (error); 62313054Ssam } 62413054Ssam 62513054Ssam ensetaddr(ifp, sin) 62613054Ssam register struct ifnet *ifp; 62713054Ssam register struct sockaddr_in *sin; 62813054Ssam { 62913054Ssam struct endevice *enaddr; 63013054Ssam 63116380Skarels /* set address once for in_netof, so subnets will be recognized */ 63216380Skarels ifp->if_addr = *(struct sockaddr *)sin; 63313054Ssam ifp->if_net = in_netof(sin->sin_addr); 63413054Ssam enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr; 63516380Skarels ((struct en_softc *) ifp)->es_host = (~enaddr->en_addr) & 0xff; 63613054Ssam sin = (struct sockaddr_in *)&ifp->if_addr; 63713054Ssam sin->sin_family = AF_INET; 63816380Skarels sin->sin_addr = if_makeaddr(ifp->if_net, 63916380Skarels ((struct en_softc *)ifp)->es_host); 64013054Ssam sin = (struct sockaddr_in *)&ifp->if_broadaddr; 64113054Ssam sin->sin_family = AF_INET; 64213054Ssam sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); 64313054Ssam ifp->if_flags |= IFF_BROADCAST; 64413054Ssam } 64513293Ssam 64613293Ssam #ifdef ENF_SWABIPS 64713293Ssam /* 64813293Ssam * Swab bytes 64913293Ssam * Jeffrey Mogul, Stanford 65013293Ssam */ 65113293Ssam enswab(from, to, n) 65213293Ssam register caddr_t *from, *to; 65313293Ssam register int n; 65413293Ssam { 65513293Ssam register unsigned long temp; 65613293Ssam 65713293Ssam n >>= 1; n++; 65813293Ssam #define STEP temp = *from++,*to++ = *from++,*to++ = temp 65913293Ssam /* round to multiple of 8 */ 66013293Ssam while ((--n) & 07) 66113293Ssam STEP; 66213293Ssam n >>= 3; 66313293Ssam while (--n >= 0) { 66413293Ssam STEP; STEP; STEP; STEP; 66513293Ssam STEP; STEP; STEP; STEP; 66613293Ssam } 66713293Ssam } 66813293Ssam #endif 669