1*16217Skarels /* if_ec.c 6.3 84/03/22 */ 26520Sfeldman 36520Sfeldman #include "ec.h" 46520Sfeldman 56520Sfeldman /* 66520Sfeldman * 3Com Ethernet Controller interface 76520Sfeldman */ 89795Ssam #include "../machine/pte.h" 96520Sfeldman 106520Sfeldman #include "../h/param.h" 116520Sfeldman #include "../h/systm.h" 126520Sfeldman #include "../h/mbuf.h" 136520Sfeldman #include "../h/buf.h" 146520Sfeldman #include "../h/protosw.h" 156520Sfeldman #include "../h/socket.h" 166520Sfeldman #include "../h/vmmac.h" 1713055Ssam #include "../h/ioctl.h" 1813055Ssam #include "../h/errno.h" 198461Sroot 208461Sroot #include "../net/if.h" 218461Sroot #include "../net/netisr.h" 228461Sroot #include "../net/route.h" 238417Swnj #include "../netinet/in.h" 248417Swnj #include "../netinet/in_systm.h" 258417Swnj #include "../netinet/ip.h" 268417Swnj #include "../netinet/ip_var.h" 2711574Ssam #include "../netinet/if_ether.h" 288417Swnj #include "../netpup/pup.h" 296520Sfeldman 308461Sroot #include "../vax/cpu.h" 318461Sroot #include "../vax/mtpr.h" 328461Sroot #include "../vaxif/if_ecreg.h" 338461Sroot #include "../vaxif/if_uba.h" 348461Sroot #include "../vaxuba/ubareg.h" 358461Sroot #include "../vaxuba/ubavar.h" 368461Sroot 377470Sfeldman #define ECMEM 0000000 386520Sfeldman 396520Sfeldman int ecprobe(), ecattach(), ecrint(), ecxint(), eccollide(); 406520Sfeldman struct uba_device *ecinfo[NEC]; 416520Sfeldman u_short ecstd[] = { 0 }; 426520Sfeldman struct uba_driver ecdriver = 436520Sfeldman { ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo }; 446520Sfeldman #define ECUNIT(x) minor(x) 456520Sfeldman 4613055Ssam int ecinit(),ecioctl(),ecoutput(),ecreset(); 478773Sroot struct mbuf *ecget(); 486520Sfeldman 496545Sfeldman extern struct ifnet loif; 506545Sfeldman 516520Sfeldman /* 526520Sfeldman * Ethernet software status per interface. 536520Sfeldman * 546520Sfeldman * Each interface is referenced by a network interface structure, 556520Sfeldman * es_if, which the routing code uses to locate the interface. 566520Sfeldman * This structure contains the output queue for the interface, its address, ... 576520Sfeldman * We also have, for each interface, a UBA interface structure, which 586520Sfeldman * contains information about the UNIBUS resources held by the interface: 596520Sfeldman * map registers, buffered data paths, etc. Information is cached in this 606520Sfeldman * structure for use by the if_uba.c routines in running the interface 616520Sfeldman * efficiently. 626520Sfeldman */ 636520Sfeldman struct ec_softc { 6411574Ssam struct arpcom es_ac; /* common Ethernet structures */ 6511574Ssam #define es_if es_ac.ac_if /* network-visible interface */ 6611574Ssam #define es_addr es_ac.ac_enaddr /* hardware Ethernet address */ 676520Sfeldman struct ifuba es_ifuba; /* UNIBUS resources */ 686520Sfeldman short es_mask; /* mask for current output delay */ 696520Sfeldman short es_oactive; /* is output active? */ 708773Sroot u_char *es_buf[16]; /* virtual addresses of buffers */ 716520Sfeldman } ec_softc[NEC]; 726520Sfeldman 736520Sfeldman /* 746520Sfeldman * Do output DMA to determine interface presence and 756520Sfeldman * interrupt vector. DMA is too short to disturb other hosts. 766520Sfeldman */ 776520Sfeldman ecprobe(reg) 786520Sfeldman caddr_t reg; 796520Sfeldman { 806520Sfeldman register int br, cvec; /* r11, r10 value-result */ 816520Sfeldman register struct ecdevice *addr = (struct ecdevice *)reg; 827470Sfeldman register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM]; 836520Sfeldman 846520Sfeldman #ifdef lint 856520Sfeldman br = 0; cvec = br; br = cvec; 866520Sfeldman ecrint(0); ecxint(0); eccollide(0); 876520Sfeldman #endif 886520Sfeldman /* 896637Sfeldman * Make sure memory is turned on 906637Sfeldman */ 916637Sfeldman addr->ec_rcr = EC_AROM; 926637Sfeldman /* 937470Sfeldman * Disable map registers for ec unibus space, 947470Sfeldman * but don't allocate yet. 957470Sfeldman */ 968773Sroot (void) ubamem(numuba, ECMEM, 32*2, 0); 977470Sfeldman /* 986520Sfeldman * Check for existence of buffers on Unibus. 996520Sfeldman */ 1008773Sroot if (badaddr((caddr_t)ecbuf, 2)) { 1017470Sfeldman bad1: 1027470Sfeldman printf("ec: buffer mem not found\n"); 1037470Sfeldman bad2: 1048773Sroot (void) ubamem(numuba, 0, 0, 0); /* reenable map (780 only) */ 1057470Sfeldman addr->ec_rcr = EC_MDISAB; /* disable memory */ 1066520Sfeldman return (0); 1076520Sfeldman } 1087470Sfeldman #if VAX780 1097470Sfeldman if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) { 1107470Sfeldman uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr; 1117470Sfeldman goto bad1; 1127470Sfeldman } 1137470Sfeldman #endif 1146520Sfeldman 1156520Sfeldman /* 1166520Sfeldman * Tell the system that the board has memory here, so it won't 1176520Sfeldman * attempt to allocate the addresses later. 1186520Sfeldman */ 1197470Sfeldman if (ubamem(numuba, ECMEM, 32*2, 1) == 0) { 1207470Sfeldman printf("ecprobe: cannot reserve uba addresses\n"); 1217470Sfeldman goto bad2; 1227470Sfeldman } 1236520Sfeldman 1246520Sfeldman /* 1256520Sfeldman * Make a one byte packet in what should be buffer #0. 1266520Sfeldman * Submit it for sending. This whould cause an xmit interrupt. 1276520Sfeldman * The xmit interrupt vector is 8 bytes after the receive vector, 1286520Sfeldman * so adjust for this before returning. 1296520Sfeldman */ 1306520Sfeldman *(u_short *)ecbuf = (u_short) 03777; 1316520Sfeldman ecbuf[03777] = '\0'; 1326520Sfeldman addr->ec_xcr = EC_XINTEN|EC_XWBN; 1336520Sfeldman DELAY(100000); 1346520Sfeldman addr->ec_xcr = EC_XCLR; 1357216Ssam if (cvec > 0 && cvec != 0x200) { 1367470Sfeldman if (cvec & 04) { /* collision interrupt */ 1377470Sfeldman cvec -= 04; 1387470Sfeldman br += 1; /* rcv is collision + 1 */ 1397470Sfeldman } else { /* xmit interrupt */ 1407470Sfeldman cvec -= 010; 1417470Sfeldman br += 2; /* rcv is xmit + 2 */ 1427470Sfeldman } 1437216Ssam } 1446520Sfeldman return (1); 1456520Sfeldman } 1466520Sfeldman 1476520Sfeldman /* 1486520Sfeldman * Interface exists: make available by filling in network interface 1496520Sfeldman * record. System will initialize the interface when it is ready 1506520Sfeldman * to accept packets. 1516520Sfeldman */ 1526520Sfeldman ecattach(ui) 1536520Sfeldman struct uba_device *ui; 1546520Sfeldman { 1557216Ssam struct ec_softc *es = &ec_softc[ui->ui_unit]; 1567216Ssam register struct ifnet *ifp = &es->es_if; 1576520Sfeldman register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr; 1587216Ssam struct sockaddr_in *sin; 1597216Ssam int i, j; 1607216Ssam u_char *cp; 1616520Sfeldman 1627216Ssam ifp->if_unit = ui->ui_unit; 1637216Ssam ifp->if_name = "ec"; 1649745Ssam ifp->if_mtu = ETHERMTU; 1656520Sfeldman 1666520Sfeldman /* 1677216Ssam * Read the ethernet address off the board, one nibble at a time. 1686520Sfeldman */ 1696520Sfeldman addr->ec_xcr = EC_UECLR; 1706520Sfeldman addr->ec_rcr = EC_AROM; 171*16217Skarels cp = (u_char *) &es->es_addr; 1727216Ssam #define NEXTBIT addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM 173*16217Skarels for (i=0; i < sizeof (es->es_addr); i++) { 1746520Sfeldman *cp = 0; 1756520Sfeldman for (j=0; j<=4; j+=4) { 1766520Sfeldman *cp |= ((addr->ec_rcr >> 8) & 0xf) << j; 1777216Ssam NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT; 1786520Sfeldman } 1796520Sfeldman cp++; 1806520Sfeldman } 1816520Sfeldman sin = (struct sockaddr_in *)&es->es_if.if_addr; 1826520Sfeldman sin->sin_family = AF_INET; 1837216Ssam ifp->if_init = ecinit; 18413055Ssam ifp->if_ioctl = ecioctl; 1857216Ssam ifp->if_output = ecoutput; 1868977Sroot ifp->if_reset = ecreset; 1876520Sfeldman for (i=0; i<16; i++) 1888773Sroot es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i]; 1897216Ssam if_attach(ifp); 1906520Sfeldman } 1916520Sfeldman 1926520Sfeldman /* 1936520Sfeldman * Reset of interface after UNIBUS reset. 1946520Sfeldman * If interface is on specified uba, reset its state. 1956520Sfeldman */ 1966520Sfeldman ecreset(unit, uban) 1976520Sfeldman int unit, uban; 1986520Sfeldman { 1996520Sfeldman register struct uba_device *ui; 2006520Sfeldman 2016520Sfeldman if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 || 2026520Sfeldman ui->ui_ubanum != uban) 2036520Sfeldman return; 2046520Sfeldman printf(" ec%d", unit); 2058773Sroot (void) ubamem(uban, ECMEM, 32*2, 0); /* mr disable (no alloc) */ 20616207Skarels ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING; 2076520Sfeldman ecinit(unit); 2086520Sfeldman } 2096520Sfeldman 2106520Sfeldman /* 2116520Sfeldman * Initialization of interface; clear recorded pending 2126520Sfeldman * operations, and reinitialize UNIBUS usage. 2136520Sfeldman */ 2146520Sfeldman ecinit(unit) 2156520Sfeldman int unit; 2166520Sfeldman { 2177216Ssam struct ec_softc *es = &ec_softc[unit]; 2187216Ssam struct ecdevice *addr; 21911574Ssam register struct ifnet *ifp = &es->es_if; 22013086Ssam register struct sockaddr_in *sin; 22113055Ssam int i, s; 2226520Sfeldman 22311574Ssam sin = (struct sockaddr_in *)&ifp->if_addr; 22413055Ssam if (sin->sin_addr.s_addr == 0) /* address still unknown */ 22511574Ssam return; 22611574Ssam 2276520Sfeldman /* 2287217Sfeldman * Hang receive buffers and start any pending writes. 2296637Sfeldman * Writing into the rcr also makes sure the memory 2306637Sfeldman * is turned on. 2316520Sfeldman */ 23213055Ssam if ((es->es_if.if_flags & IFF_RUNNING) == 0) { 23313055Ssam addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 23413055Ssam s = splimp(); 23513055Ssam for (i = ECRHBF; i >= ECRLBF; i--) 23613055Ssam addr->ec_rcr = EC_READ | i; 23713055Ssam es->es_oactive = 0; 23813055Ssam es->es_mask = ~0; 23913055Ssam es->es_if.if_flags |= IFF_UP|IFF_RUNNING; 24013055Ssam if (es->es_if.if_snd.ifq_head) 24113055Ssam ecstart(unit); 24213055Ssam splx(s); 24313055Ssam } 2447150Swnj if_rtinit(&es->es_if, RTF_UP); 24511574Ssam arpwhohas(&es->es_ac, &sin->sin_addr); 2466520Sfeldman } 2476520Sfeldman 2486520Sfeldman /* 2496520Sfeldman * Start or restart output on interface. 2506520Sfeldman * If interface is already active, then this is a retransmit 2516545Sfeldman * after a collision, and just restuff registers. 2526520Sfeldman * If interface is not already active, get another datagram 2536520Sfeldman * to send off of the interface queue, and map it to the interface 2546520Sfeldman * before starting the output. 2556520Sfeldman */ 2566520Sfeldman ecstart(dev) 2576520Sfeldman dev_t dev; 2586520Sfeldman { 2598773Sroot int unit = ECUNIT(dev); 2607216Ssam struct ec_softc *es = &ec_softc[unit]; 2617216Ssam struct ecdevice *addr; 2626520Sfeldman struct mbuf *m; 2636520Sfeldman 2646520Sfeldman if (es->es_oactive) 2656520Sfeldman goto restart; 2666520Sfeldman 2676520Sfeldman IF_DEQUEUE(&es->es_if.if_snd, m); 2686520Sfeldman if (m == 0) { 2696520Sfeldman es->es_oactive = 0; 2706520Sfeldman return; 2716520Sfeldman } 2726520Sfeldman ecput(es->es_buf[ECTBF], m); 2736520Sfeldman 2746520Sfeldman restart: 2757216Ssam addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 2766520Sfeldman addr->ec_xcr = EC_WRITE|ECTBF; 2776520Sfeldman es->es_oactive = 1; 2786520Sfeldman } 2796520Sfeldman 2806520Sfeldman /* 2816520Sfeldman * Ethernet interface transmitter interrupt. 2826520Sfeldman * Start another output if more data to send. 2836520Sfeldman */ 2846520Sfeldman ecxint(unit) 2856520Sfeldman int unit; 2866520Sfeldman { 2876520Sfeldman register struct ec_softc *es = &ec_softc[unit]; 2887216Ssam register struct ecdevice *addr = 2897216Ssam (struct ecdevice *)ecinfo[unit]->ui_addr; 2906520Sfeldman 2916520Sfeldman if (es->es_oactive == 0) 2926520Sfeldman return; 2937216Ssam if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) { 2947216Ssam printf("ec%d: stray xmit interrupt, xcr=%b\n", unit, 2957216Ssam addr->ec_xcr, EC_XBITS); 2967216Ssam es->es_oactive = 0; 2977216Ssam addr->ec_xcr = EC_XCLR; 2987216Ssam return; 2997216Ssam } 3006520Sfeldman es->es_if.if_opackets++; 3016520Sfeldman es->es_oactive = 0; 3026520Sfeldman es->es_mask = ~0; 3036520Sfeldman addr->ec_xcr = EC_XCLR; 3047216Ssam if (es->es_if.if_snd.ifq_head) 3057216Ssam ecstart(unit); 3066520Sfeldman } 3076520Sfeldman 3086520Sfeldman /* 3096520Sfeldman * Collision on ethernet interface. Do exponential 3106520Sfeldman * backoff, and retransmit. If have backed off all 3116520Sfeldman * the way print warning diagnostic, and drop packet. 3126520Sfeldman */ 3136520Sfeldman eccollide(unit) 3146520Sfeldman int unit; 3156520Sfeldman { 3166520Sfeldman struct ec_softc *es = &ec_softc[unit]; 3176520Sfeldman 3186520Sfeldman es->es_if.if_collisions++; 3197216Ssam if (es->es_oactive) 3207216Ssam ecdocoll(unit); 3216520Sfeldman } 3226520Sfeldman 3236520Sfeldman ecdocoll(unit) 3246520Sfeldman int unit; 3256520Sfeldman { 3266520Sfeldman register struct ec_softc *es = &ec_softc[unit]; 3276545Sfeldman register struct ecdevice *addr = 3286545Sfeldman (struct ecdevice *)ecinfo[unit]->ui_addr; 3296545Sfeldman register i; 3306545Sfeldman int delay; 3316520Sfeldman 3326520Sfeldman /* 3336520Sfeldman * Es_mask is a 16 bit number with n low zero bits, with 3346520Sfeldman * n the number of backoffs. When es_mask is 0 we have 3356520Sfeldman * backed off 16 times, and give up. 3366520Sfeldman */ 3376520Sfeldman if (es->es_mask == 0) { 3386545Sfeldman es->es_if.if_oerrors++; 3396520Sfeldman printf("ec%d: send error\n", unit); 3406520Sfeldman /* 3416545Sfeldman * Reset interface, then requeue rcv buffers. 3426545Sfeldman * Some incoming packets may be lost, but that 3436545Sfeldman * can't be helped. 3446520Sfeldman */ 3456545Sfeldman addr->ec_xcr = EC_UECLR; 3466545Sfeldman for (i=ECRHBF; i>=ECRLBF; i--) 3476545Sfeldman addr->ec_rcr = EC_READ|i; 3486545Sfeldman /* 3496545Sfeldman * Reset and transmit next packet (if any). 3506545Sfeldman */ 3516545Sfeldman es->es_oactive = 0; 3526545Sfeldman es->es_mask = ~0; 3536545Sfeldman if (es->es_if.if_snd.ifq_head) 3546545Sfeldman ecstart(unit); 3556520Sfeldman return; 3566520Sfeldman } 3576520Sfeldman /* 3586545Sfeldman * Do exponential backoff. Compute delay based on low bits 3596545Sfeldman * of the interval timer. Then delay for that number of 3606545Sfeldman * slot times. A slot time is 51.2 microseconds (rounded to 51). 3616545Sfeldman * This does not take into account the time already used to 3626545Sfeldman * process the interrupt. 3636520Sfeldman */ 3646520Sfeldman es->es_mask <<= 1; 3656545Sfeldman delay = mfpr(ICR) &~ es->es_mask; 3666545Sfeldman DELAY(delay * 51); 3676520Sfeldman /* 3686545Sfeldman * Clear the controller's collision flag, thus enabling retransmit. 3696520Sfeldman */ 3707470Sfeldman addr->ec_xcr = EC_CLEAR; 3716520Sfeldman } 3726520Sfeldman 3736520Sfeldman /* 3746520Sfeldman * Ethernet interface receiver interrupt. 3756520Sfeldman * If input error just drop packet. 3766520Sfeldman * Otherwise purge input buffered data path and examine 3776520Sfeldman * packet to determine type. If can't determine length 3786520Sfeldman * from type, then have to drop packet. Othewise decapsulate 3796520Sfeldman * packet based on type and pass to type specific higher-level 3806520Sfeldman * input routine. 3816520Sfeldman */ 3826520Sfeldman ecrint(unit) 3836520Sfeldman int unit; 3846520Sfeldman { 3856520Sfeldman struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 3866520Sfeldman 3876520Sfeldman while (addr->ec_rcr & EC_RDONE) 3886520Sfeldman ecread(unit); 3896520Sfeldman } 3906520Sfeldman 3916520Sfeldman ecread(unit) 3926520Sfeldman int unit; 3936520Sfeldman { 3946520Sfeldman register struct ec_softc *es = &ec_softc[unit]; 3956520Sfeldman struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 3969745Ssam register struct ether_header *ec; 3976520Sfeldman struct mbuf *m; 3988773Sroot int len, off, resid, ecoff, rbuf; 3996520Sfeldman register struct ifqueue *inq; 4008773Sroot u_char *ecbuf; 4016520Sfeldman 4026520Sfeldman es->es_if.if_ipackets++; 4038773Sroot rbuf = addr->ec_rcr & EC_RBN; 4048773Sroot if (rbuf < ECRLBF || rbuf > ECRHBF) 4056520Sfeldman panic("ecrint"); 4068773Sroot ecbuf = es->es_buf[rbuf]; 4076520Sfeldman ecoff = *(short *)ecbuf; 4086545Sfeldman if (ecoff <= ECRDOFF || ecoff > 2046) { 4096520Sfeldman es->es_if.if_ierrors++; 4106520Sfeldman #ifdef notdef 4116520Sfeldman if (es->es_if.if_ierrors % 100 == 0) 4126520Sfeldman printf("ec%d: += 100 input errors\n", unit); 4136520Sfeldman #endif 4146520Sfeldman goto setup; 4156520Sfeldman } 4166520Sfeldman 4176520Sfeldman /* 4186520Sfeldman * Get input data length. 4196520Sfeldman * Get pointer to ethernet header (in input buffer). 4206520Sfeldman * Deal with trailer protocol: if type is PUP trailer 4216520Sfeldman * get true type from first 16-bit word past data. 4226520Sfeldman * Remember that type was trailer by setting off. 4236520Sfeldman */ 4249745Ssam len = ecoff - ECRDOFF - sizeof (struct ether_header); 4259745Ssam ec = (struct ether_header *)(ecbuf + ECRDOFF); 4269745Ssam ec->ether_type = ntohs((u_short)ec->ether_type); 4276520Sfeldman #define ecdataaddr(ec, off, type) ((type)(((caddr_t)((ec)+1)+(off)))) 4289745Ssam if (ec->ether_type >= ETHERPUP_TRAIL && 4299745Ssam ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) { 4309745Ssam off = (ec->ether_type - ETHERPUP_TRAIL) * 512; 4319745Ssam if (off >= ETHERMTU) 4326520Sfeldman goto setup; /* sanity */ 4339745Ssam ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *)); 4349745Ssam resid = ntohs(*(ecdataaddr(ec, off+2, u_short *))); 4356520Sfeldman if (off + resid > len) 4366520Sfeldman goto setup; /* sanity */ 4376520Sfeldman len = off + resid; 4386520Sfeldman } else 4396520Sfeldman off = 0; 4406520Sfeldman if (len == 0) 4416520Sfeldman goto setup; 4426520Sfeldman 4436520Sfeldman /* 4446520Sfeldman * Pull packet off interface. Off is nonzero if packet 4456520Sfeldman * has trailing header; ecget will then force this header 4466520Sfeldman * information to be at the front, but we still have to drop 4476520Sfeldman * the type and length which are at the front of any trailer data. 4486520Sfeldman */ 4496520Sfeldman m = ecget(ecbuf, len, off); 4506520Sfeldman if (m == 0) 4516520Sfeldman goto setup; 4526520Sfeldman if (off) { 4536520Sfeldman m->m_off += 2 * sizeof (u_short); 4546520Sfeldman m->m_len -= 2 * sizeof (u_short); 4556520Sfeldman } 4569745Ssam switch (ec->ether_type) { 4576520Sfeldman 4586520Sfeldman #ifdef INET 4599745Ssam case ETHERPUP_IPTYPE: 4606520Sfeldman schednetisr(NETISR_IP); 4616520Sfeldman inq = &ipintrq; 4626520Sfeldman break; 46311574Ssam 46411574Ssam case ETHERPUP_ARPTYPE: 46511574Ssam arpinput(&es->es_ac, m); 46613987Ssam goto setup; 4676520Sfeldman #endif 4686520Sfeldman default: 4696520Sfeldman m_freem(m); 4706520Sfeldman goto setup; 4716520Sfeldman } 4726520Sfeldman 4736520Sfeldman if (IF_QFULL(inq)) { 4746520Sfeldman IF_DROP(inq); 4756520Sfeldman m_freem(m); 4767216Ssam goto setup; 4777216Ssam } 4787216Ssam IF_ENQUEUE(inq, m); 4796520Sfeldman 4806520Sfeldman setup: 4816520Sfeldman /* 4826520Sfeldman * Reset for next packet. 4836520Sfeldman */ 4848773Sroot addr->ec_rcr = EC_READ|EC_RCLR|rbuf; 4856520Sfeldman } 4866520Sfeldman 4876520Sfeldman /* 4886520Sfeldman * Ethernet output routine. 4896520Sfeldman * Encapsulate a packet of type family for the local net. 4906520Sfeldman * Use trailer local net encapsulation if enough data in first 4916520Sfeldman * packet leaves a multiple of 512 bytes of data in remainder. 4926545Sfeldman * If destination is this address or broadcast, send packet to 4936545Sfeldman * loop device to kludge around the fact that 3com interfaces can't 4946545Sfeldman * talk to themselves. 4956520Sfeldman */ 4966520Sfeldman ecoutput(ifp, m0, dst) 4976520Sfeldman struct ifnet *ifp; 4986520Sfeldman struct mbuf *m0; 4996520Sfeldman struct sockaddr *dst; 5006520Sfeldman { 50111574Ssam int type, s, error; 502*16217Skarels struct ether_addr edst; 50311574Ssam struct in_addr idst; 5046520Sfeldman register struct ec_softc *es = &ec_softc[ifp->if_unit]; 5056520Sfeldman register struct mbuf *m = m0; 5069745Ssam register struct ether_header *ec; 50712771Ssam register int off; 50811574Ssam struct mbuf *mcopy = (struct mbuf *)0; 5096520Sfeldman 5106520Sfeldman switch (dst->sa_family) { 5116520Sfeldman 5126520Sfeldman #ifdef INET 5136520Sfeldman case AF_INET: 51411574Ssam idst = ((struct sockaddr_in *)dst)->sin_addr; 515*16217Skarels if (!arpresolve(&es->es_ac, m, &idst, &edst)) 51611574Ssam return (0); /* if not yet resolved */ 51711574Ssam if (in_lnaof(idst) == INADDR_ANY) 5188838Sroot mcopy = m_copy(m, 0, (int)M_COPYALL); 5196520Sfeldman off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 52013055Ssam /* need per host negotiation */ 52113055Ssam if ((ifp->if_flags & IFF_NOTRAILERS) == 0) 5226520Sfeldman if (off > 0 && (off & 0x1ff) == 0 && 5236520Sfeldman m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 5249745Ssam type = ETHERPUP_TRAIL + (off>>9); 5256520Sfeldman m->m_off -= 2 * sizeof (u_short); 5266520Sfeldman m->m_len += 2 * sizeof (u_short); 5279745Ssam *mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE); 5289745Ssam *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len); 5296520Sfeldman goto gottrailertype; 5306520Sfeldman } 5319745Ssam type = ETHERPUP_IPTYPE; 5326520Sfeldman off = 0; 5336520Sfeldman goto gottype; 5346520Sfeldman #endif 5356520Sfeldman 53611574Ssam case AF_UNSPEC: 53711574Ssam ec = (struct ether_header *)dst->sa_data; 538*16217Skarels edst = ec->ether_dhost; 53911574Ssam type = ec->ether_type; 54011574Ssam goto gottype; 54111574Ssam 5426520Sfeldman default: 5436520Sfeldman printf("ec%d: can't handle af%d\n", ifp->if_unit, 5446520Sfeldman dst->sa_family); 5456520Sfeldman error = EAFNOSUPPORT; 5466520Sfeldman goto bad; 5476520Sfeldman } 5486520Sfeldman 5496520Sfeldman gottrailertype: 5506520Sfeldman /* 5516520Sfeldman * Packet to be sent as trailer: move first packet 5526520Sfeldman * (control information) to end of chain. 5536520Sfeldman */ 5546520Sfeldman while (m->m_next) 5556520Sfeldman m = m->m_next; 5566520Sfeldman m->m_next = m0; 5576520Sfeldman m = m0->m_next; 5586520Sfeldman m0->m_next = 0; 5596520Sfeldman m0 = m; 5606520Sfeldman 5616520Sfeldman gottype: 5626520Sfeldman /* 5636520Sfeldman * Add local net header. If no space in first mbuf, 5646520Sfeldman * allocate another. 5656520Sfeldman */ 5666520Sfeldman if (m->m_off > MMAXOFF || 5679745Ssam MMINOFF + sizeof (struct ether_header) > m->m_off) { 5689648Ssam m = m_get(M_DONTWAIT, MT_HEADER); 5696520Sfeldman if (m == 0) { 5706520Sfeldman error = ENOBUFS; 5716520Sfeldman goto bad; 5726520Sfeldman } 5736520Sfeldman m->m_next = m0; 5746520Sfeldman m->m_off = MMINOFF; 5759745Ssam m->m_len = sizeof (struct ether_header); 5766520Sfeldman } else { 5779745Ssam m->m_off -= sizeof (struct ether_header); 5789745Ssam m->m_len += sizeof (struct ether_header); 5796520Sfeldman } 5809745Ssam ec = mtod(m, struct ether_header *); 581*16217Skarels ec->ether_dhost = edst; 582*16217Skarels ec->ether_shost = es->es_addr; 5839745Ssam ec->ether_type = htons((u_short)type); 5846520Sfeldman 5856520Sfeldman /* 5866520Sfeldman * Queue message on interface, and start output if interface 5876520Sfeldman * not yet active. 5886520Sfeldman */ 5896520Sfeldman s = splimp(); 5906520Sfeldman if (IF_QFULL(&ifp->if_snd)) { 5916520Sfeldman IF_DROP(&ifp->if_snd); 5926520Sfeldman error = ENOBUFS; 5936520Sfeldman goto qfull; 5946520Sfeldman } 5956520Sfeldman IF_ENQUEUE(&ifp->if_snd, m); 5966520Sfeldman if (es->es_oactive == 0) 5976520Sfeldman ecstart(ifp->if_unit); 5986520Sfeldman splx(s); 59912771Ssam return (mcopy ? looutput(&loif, mcopy, dst) : 0); 6007216Ssam 6016520Sfeldman qfull: 6026520Sfeldman m0 = m; 6036520Sfeldman splx(s); 6046520Sfeldman bad: 6056520Sfeldman m_freem(m0); 60616207Skarels if (mcopy) 60716207Skarels m_freem(mcopy); 60812771Ssam return (error); 6096520Sfeldman } 6106520Sfeldman 6116520Sfeldman /* 6129177Ssam * Routine to copy from mbuf chain to transmit 6137216Ssam * buffer in UNIBUS memory. 6149177Ssam * If packet size is less than the minimum legal size, 6159177Ssam * the buffer is expanded. We probably should zero out the extra 6169177Ssam * bytes for security, but that would slow things down. 6176520Sfeldman */ 6186520Sfeldman ecput(ecbuf, m) 6197216Ssam u_char *ecbuf; 6206520Sfeldman struct mbuf *m; 6216520Sfeldman { 6226520Sfeldman register struct mbuf *mp; 6237216Ssam register int off; 6247263Ssam u_char *bp; 6256520Sfeldman 6267216Ssam for (off = 2048, mp = m; mp; mp = mp->m_next) 6277216Ssam off -= mp->m_len; 6289745Ssam if (2048 - off < ETHERMIN + sizeof (struct ether_header)) 6299745Ssam off = 2048 - ETHERMIN - sizeof (struct ether_header); 6307216Ssam *(u_short *)ecbuf = off; 6317216Ssam bp = (u_char *)(ecbuf + off); 6327263Ssam for (mp = m; mp; mp = mp->m_next) { 6337263Ssam register unsigned len = mp->m_len; 6347263Ssam u_char *mcp; 6357216Ssam 6367216Ssam if (len == 0) 6377216Ssam continue; 6387216Ssam mcp = mtod(mp, u_char *); 6397216Ssam if ((unsigned)bp & 01) { 6407032Swnj *bp++ = *mcp++; 6417216Ssam len--; 6427032Swnj } 6437263Ssam if (off = (len >> 1)) { 6447263Ssam register u_short *to, *from; 6457263Ssam 6467263Ssam to = (u_short *)bp; 6477263Ssam from = (u_short *)mcp; 6487263Ssam do 6497263Ssam *to++ = *from++; 6507263Ssam while (--off > 0); 6517263Ssam bp = (u_char *)to, 6527263Ssam mcp = (u_char *)from; 6537032Swnj } 6547263Ssam if (len & 01) 6556520Sfeldman *bp++ = *mcp++; 6566520Sfeldman } 6577263Ssam m_freem(m); 6586520Sfeldman } 6596520Sfeldman 6606520Sfeldman /* 6616520Sfeldman * Routine to copy from UNIBUS memory into mbufs. 6626520Sfeldman * Similar in spirit to if_rubaget. 6637032Swnj * 6647032Swnj * Warning: This makes the fairly safe assumption that 6657032Swnj * mbufs have even lengths. 6666520Sfeldman */ 6676520Sfeldman struct mbuf * 6686520Sfeldman ecget(ecbuf, totlen, off0) 6697263Ssam u_char *ecbuf; 6706520Sfeldman int totlen, off0; 6716520Sfeldman { 6727263Ssam register struct mbuf *m; 6737263Ssam struct mbuf *top = 0, **mp = ⊤ 6747263Ssam register int off = off0, len; 6757263Ssam u_char *cp; 6766520Sfeldman 6779745Ssam cp = ecbuf + ECRDOFF + sizeof (struct ether_header); 6786520Sfeldman while (totlen > 0) { 6797263Ssam register int words; 6807263Ssam u_char *mcp; 6817263Ssam 6829648Ssam MGET(m, M_DONTWAIT, MT_DATA); 6836520Sfeldman if (m == 0) 6846520Sfeldman goto bad; 6856520Sfeldman if (off) { 6866520Sfeldman len = totlen - off; 6879745Ssam cp = ecbuf + ECRDOFF + 6889745Ssam sizeof (struct ether_header) + off; 6896520Sfeldman } else 6906520Sfeldman len = totlen; 6916520Sfeldman if (len >= CLBYTES) { 6926520Sfeldman struct mbuf *p; 6936520Sfeldman 6946520Sfeldman MCLGET(p, 1); 6956520Sfeldman if (p != 0) { 6966520Sfeldman m->m_len = len = CLBYTES; 6976520Sfeldman m->m_off = (int)p - (int)m; 6986520Sfeldman } else { 6996520Sfeldman m->m_len = len = MIN(MLEN, len); 7006520Sfeldman m->m_off = MMINOFF; 7016520Sfeldman } 7026520Sfeldman } else { 7036520Sfeldman m->m_len = len = MIN(MLEN, len); 7046520Sfeldman m->m_off = MMINOFF; 7056520Sfeldman } 7067263Ssam mcp = mtod(m, u_char *); 7077263Ssam if (words = (len >> 1)) { 7087263Ssam register u_short *to, *from; 7097263Ssam 7107263Ssam to = (u_short *)mcp; 7117263Ssam from = (u_short *)cp; 7127263Ssam do 7137263Ssam *to++ = *from++; 7147263Ssam while (--words > 0); 7157263Ssam mcp = (u_char *)to; 7167263Ssam cp = (u_char *)from; 7177032Swnj } 7187216Ssam if (len & 01) 7196520Sfeldman *mcp++ = *cp++; 7206520Sfeldman *mp = m; 7216520Sfeldman mp = &m->m_next; 7227263Ssam if (off == 0) { 7236520Sfeldman totlen -= len; 7247263Ssam continue; 7257263Ssam } 7267263Ssam off += len; 7277263Ssam if (off == totlen) { 7289745Ssam cp = ecbuf + ECRDOFF + sizeof (struct ether_header); 7297263Ssam off = 0; 7307263Ssam totlen = off0; 7317263Ssam } 7326520Sfeldman } 7336520Sfeldman return (top); 7346520Sfeldman bad: 7356520Sfeldman m_freem(top); 7366520Sfeldman return (0); 7376520Sfeldman } 73813055Ssam 73913055Ssam /* 74013055Ssam * Process an ioctl request. 74113055Ssam */ 74213055Ssam ecioctl(ifp, cmd, data) 74313055Ssam register struct ifnet *ifp; 74413055Ssam int cmd; 74513055Ssam caddr_t data; 74613055Ssam { 74713055Ssam register struct ifreq *ifr = (struct ifreq *)data; 74813055Ssam int s = splimp(), error = 0; 74913055Ssam 75013055Ssam switch (cmd) { 75113055Ssam 75213055Ssam case SIOCSIFADDR: 75313055Ssam if (ifp->if_flags & IFF_RUNNING) 75413055Ssam if_rtinit(ifp, -1); /* delete previous route */ 75513062Ssam ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); 75613055Ssam ecinit(ifp->if_unit); 75713055Ssam break; 75813055Ssam 75913055Ssam default: 76013055Ssam error = EINVAL; 76113055Ssam } 76213055Ssam splx(s); 76313055Ssam return (error); 76413055Ssam } 76513062Ssam 76613062Ssam ecsetaddr(ifp, sin) 76713062Ssam register struct ifnet *ifp; 76813062Ssam register struct sockaddr_in *sin; 76913062Ssam { 77013062Ssam 77113062Ssam ifp->if_addr = *(struct sockaddr *)sin; 77213062Ssam ifp->if_net = in_netof(sin->sin_addr); 77313062Ssam ifp->if_host[0] = in_lnaof(sin->sin_addr); 77413062Ssam sin = (struct sockaddr_in *)&ifp->if_broadaddr; 77513062Ssam sin->sin_family = AF_INET; 77613062Ssam sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); 77713062Ssam ifp->if_flags |= IFF_BROADCAST; 77813062Ssam } 779