1*9177Ssam /* if_ec.c 4.29 82/11/13 */ 26520Sfeldman 36520Sfeldman #include "ec.h" 46520Sfeldman 56520Sfeldman /* 66520Sfeldman * 3Com Ethernet Controller interface 76520Sfeldman */ 86520Sfeldman 96520Sfeldman #include "../h/param.h" 106520Sfeldman #include "../h/systm.h" 116520Sfeldman #include "../h/mbuf.h" 126520Sfeldman #include "../h/pte.h" 136520Sfeldman #include "../h/buf.h" 146520Sfeldman #include "../h/protosw.h" 156520Sfeldman #include "../h/socket.h" 166520Sfeldman #include "../h/vmmac.h" 178461Sroot #include <errno.h> 188461Sroot 198461Sroot #include "../net/if.h" 208461Sroot #include "../net/netisr.h" 218461Sroot #include "../net/route.h" 228417Swnj #include "../netinet/in.h" 238417Swnj #include "../netinet/in_systm.h" 248417Swnj #include "../netinet/ip.h" 258417Swnj #include "../netinet/ip_var.h" 268417Swnj #include "../netpup/pup.h" 276520Sfeldman 288461Sroot #include "../vax/cpu.h" 298461Sroot #include "../vax/mtpr.h" 308461Sroot #include "../vaxif/if_ec.h" 318461Sroot #include "../vaxif/if_ecreg.h" 328461Sroot #include "../vaxif/if_uba.h" 338461Sroot #include "../vaxuba/ubareg.h" 348461Sroot #include "../vaxuba/ubavar.h" 358461Sroot 366520Sfeldman #define ECMTU 1500 37*9177Ssam #define ECMIN (60-14) 387470Sfeldman #define ECMEM 0000000 396520Sfeldman 406520Sfeldman int ecprobe(), ecattach(), ecrint(), ecxint(), eccollide(); 416520Sfeldman struct uba_device *ecinfo[NEC]; 426520Sfeldman u_short ecstd[] = { 0 }; 436520Sfeldman struct uba_driver ecdriver = 446520Sfeldman { ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo }; 456892Sfeldman u_char ec_iltop[3] = { 0x02, 0x07, 0x01 }; 466520Sfeldman #define ECUNIT(x) minor(x) 476520Sfeldman 486520Sfeldman int ecinit(),ecoutput(),ecreset(); 498773Sroot struct mbuf *ecget(); 506520Sfeldman 516545Sfeldman extern struct ifnet loif; 526545Sfeldman 536520Sfeldman /* 546520Sfeldman * Ethernet software status per interface. 556520Sfeldman * 566520Sfeldman * Each interface is referenced by a network interface structure, 576520Sfeldman * es_if, which the routing code uses to locate the interface. 586520Sfeldman * This structure contains the output queue for the interface, its address, ... 596520Sfeldman * We also have, for each interface, a UBA interface structure, which 606520Sfeldman * contains information about the UNIBUS resources held by the interface: 616520Sfeldman * map registers, buffered data paths, etc. Information is cached in this 626520Sfeldman * structure for use by the if_uba.c routines in running the interface 636520Sfeldman * efficiently. 646520Sfeldman */ 656520Sfeldman struct ec_softc { 666520Sfeldman struct ifnet es_if; /* network-visible interface */ 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 u_char es_enaddr[6]; /* board's ethernet address */ 726520Sfeldman } ec_softc[NEC]; 736520Sfeldman 746520Sfeldman /* 756520Sfeldman * Do output DMA to determine interface presence and 766520Sfeldman * interrupt vector. DMA is too short to disturb other hosts. 776520Sfeldman */ 786520Sfeldman ecprobe(reg) 796520Sfeldman caddr_t reg; 806520Sfeldman { 816520Sfeldman register int br, cvec; /* r11, r10 value-result */ 826520Sfeldman register struct ecdevice *addr = (struct ecdevice *)reg; 837470Sfeldman register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM]; 846520Sfeldman 856520Sfeldman #ifdef lint 866520Sfeldman br = 0; cvec = br; br = cvec; 876520Sfeldman ecrint(0); ecxint(0); eccollide(0); 886520Sfeldman #endif 896520Sfeldman /* 906637Sfeldman * Make sure memory is turned on 916637Sfeldman */ 926637Sfeldman addr->ec_rcr = EC_AROM; 936637Sfeldman /* 947470Sfeldman * Disable map registers for ec unibus space, 957470Sfeldman * but don't allocate yet. 967470Sfeldman */ 978773Sroot (void) ubamem(numuba, ECMEM, 32*2, 0); 987470Sfeldman /* 996520Sfeldman * Check for existence of buffers on Unibus. 1006520Sfeldman */ 1018773Sroot if (badaddr((caddr_t)ecbuf, 2)) { 1027470Sfeldman bad1: 1037470Sfeldman printf("ec: buffer mem not found\n"); 1047470Sfeldman bad2: 1058773Sroot (void) ubamem(numuba, 0, 0, 0); /* reenable map (780 only) */ 1067470Sfeldman addr->ec_rcr = EC_MDISAB; /* disable memory */ 1076520Sfeldman return (0); 1086520Sfeldman } 1097470Sfeldman #if VAX780 1107470Sfeldman if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) { 1117470Sfeldman uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr; 1127470Sfeldman goto bad1; 1137470Sfeldman } 1147470Sfeldman #endif 1156520Sfeldman 1166520Sfeldman /* 1176520Sfeldman * Tell the system that the board has memory here, so it won't 1186520Sfeldman * attempt to allocate the addresses later. 1196520Sfeldman */ 1207470Sfeldman if (ubamem(numuba, ECMEM, 32*2, 1) == 0) { 1217470Sfeldman printf("ecprobe: cannot reserve uba addresses\n"); 1227470Sfeldman goto bad2; 1237470Sfeldman } 1246520Sfeldman 1256520Sfeldman /* 1266520Sfeldman * Make a one byte packet in what should be buffer #0. 1276520Sfeldman * Submit it for sending. This whould cause an xmit interrupt. 1286520Sfeldman * The xmit interrupt vector is 8 bytes after the receive vector, 1296520Sfeldman * so adjust for this before returning. 1306520Sfeldman */ 1316520Sfeldman *(u_short *)ecbuf = (u_short) 03777; 1326520Sfeldman ecbuf[03777] = '\0'; 1336520Sfeldman addr->ec_xcr = EC_XINTEN|EC_XWBN; 1346520Sfeldman DELAY(100000); 1356520Sfeldman addr->ec_xcr = EC_XCLR; 1367216Ssam if (cvec > 0 && cvec != 0x200) { 1377470Sfeldman if (cvec & 04) { /* collision interrupt */ 1387470Sfeldman cvec -= 04; 1397470Sfeldman br += 1; /* rcv is collision + 1 */ 1407470Sfeldman } else { /* xmit interrupt */ 1417470Sfeldman cvec -= 010; 1427470Sfeldman br += 2; /* rcv is xmit + 2 */ 1437470Sfeldman } 1447216Ssam } 1456520Sfeldman return (1); 1466520Sfeldman } 1476520Sfeldman 1486520Sfeldman /* 1496520Sfeldman * Interface exists: make available by filling in network interface 1506520Sfeldman * record. System will initialize the interface when it is ready 1516520Sfeldman * to accept packets. 1526520Sfeldman */ 1536520Sfeldman ecattach(ui) 1546520Sfeldman struct uba_device *ui; 1556520Sfeldman { 1567216Ssam struct ec_softc *es = &ec_softc[ui->ui_unit]; 1577216Ssam register struct ifnet *ifp = &es->es_if; 1586520Sfeldman register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr; 1597216Ssam struct sockaddr_in *sin; 1607216Ssam int i, j; 1617216Ssam u_char *cp; 1626520Sfeldman 1637216Ssam ifp->if_unit = ui->ui_unit; 1647216Ssam ifp->if_name = "ec"; 1657216Ssam ifp->if_mtu = ECMTU; 1667216Ssam ifp->if_net = ui->ui_flags; 1676520Sfeldman 1686520Sfeldman /* 1697216Ssam * Read the ethernet address off the board, one nibble at a time. 1706520Sfeldman */ 1716520Sfeldman addr->ec_xcr = EC_UECLR; 1726520Sfeldman addr->ec_rcr = EC_AROM; 1736520Sfeldman cp = es->es_enaddr; 1747216Ssam #define NEXTBIT addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM 1756520Sfeldman for (i=0; i<6; i++) { 1766520Sfeldman *cp = 0; 1776520Sfeldman for (j=0; j<=4; j+=4) { 1786520Sfeldman *cp |= ((addr->ec_rcr >> 8) & 0xf) << j; 1797216Ssam NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT; 1806520Sfeldman } 1816520Sfeldman cp++; 1826520Sfeldman } 1837216Ssam #ifdef notdef 1846520Sfeldman printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit, 1856520Sfeldman es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff, 1866520Sfeldman es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff, 1876520Sfeldman es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff); 1887216Ssam #endif 1897216Ssam ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) | 1906520Sfeldman ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff); 1916520Sfeldman sin = (struct sockaddr_in *)&es->es_if.if_addr; 1926520Sfeldman sin->sin_family = AF_INET; 1937216Ssam sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]); 1946520Sfeldman 1957216Ssam sin = (struct sockaddr_in *)&ifp->if_broadaddr; 1966520Sfeldman sin->sin_family = AF_INET; 1977216Ssam sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); 1987216Ssam ifp->if_flags = IFF_BROADCAST; 1996520Sfeldman 2007216Ssam ifp->if_init = ecinit; 2017216Ssam ifp->if_output = ecoutput; 2028977Sroot ifp->if_reset = ecreset; 2036520Sfeldman for (i=0; i<16; i++) 2048773Sroot es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i]; 2057216Ssam if_attach(ifp); 2066520Sfeldman } 2076520Sfeldman 2086520Sfeldman /* 2096520Sfeldman * Reset of interface after UNIBUS reset. 2106520Sfeldman * If interface is on specified uba, reset its state. 2116520Sfeldman */ 2126520Sfeldman ecreset(unit, uban) 2136520Sfeldman int unit, uban; 2146520Sfeldman { 2156520Sfeldman register struct uba_device *ui; 2166520Sfeldman 2176520Sfeldman if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 || 2186520Sfeldman ui->ui_ubanum != uban) 2196520Sfeldman return; 2206520Sfeldman printf(" ec%d", unit); 2218773Sroot (void) ubamem(uban, ECMEM, 32*2, 0); /* mr disable (no alloc) */ 2226520Sfeldman ecinit(unit); 2236520Sfeldman } 2246520Sfeldman 2256520Sfeldman /* 2266520Sfeldman * Initialization of interface; clear recorded pending 2276520Sfeldman * operations, and reinitialize UNIBUS usage. 2286520Sfeldman */ 2296520Sfeldman ecinit(unit) 2306520Sfeldman int unit; 2316520Sfeldman { 2327216Ssam struct ec_softc *es = &ec_softc[unit]; 2337216Ssam struct ecdevice *addr; 2347216Ssam int i, s; 2356520Sfeldman 2366520Sfeldman /* 2377217Sfeldman * Hang receive buffers and start any pending writes. 2386637Sfeldman * Writing into the rcr also makes sure the memory 2396637Sfeldman * is turned on. 2406520Sfeldman */ 2417216Ssam addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 2426520Sfeldman s = splimp(); 2436520Sfeldman for (i=ECRHBF; i>=ECRLBF; i--) 2446520Sfeldman addr->ec_rcr = EC_READ|i; 2457217Sfeldman es->es_oactive = 0; 2467217Sfeldman es->es_mask = ~0; 2476520Sfeldman es->es_if.if_flags |= IFF_UP; 2487217Sfeldman if (es->es_if.if_snd.ifq_head) 2497217Sfeldman ecstart(unit); 2506520Sfeldman splx(s); 2517150Swnj if_rtinit(&es->es_if, RTF_UP); 2526520Sfeldman } 2536520Sfeldman 2546520Sfeldman /* 2556520Sfeldman * Start or restart output on interface. 2566520Sfeldman * If interface is already active, then this is a retransmit 2576545Sfeldman * after a collision, and just restuff registers. 2586520Sfeldman * If interface is not already active, get another datagram 2596520Sfeldman * to send off of the interface queue, and map it to the interface 2606520Sfeldman * before starting the output. 2616520Sfeldman */ 2626520Sfeldman ecstart(dev) 2636520Sfeldman dev_t dev; 2646520Sfeldman { 2658773Sroot int unit = ECUNIT(dev); 2667216Ssam struct ec_softc *es = &ec_softc[unit]; 2677216Ssam struct ecdevice *addr; 2686520Sfeldman struct mbuf *m; 2696520Sfeldman 2706520Sfeldman if (es->es_oactive) 2716520Sfeldman goto restart; 2726520Sfeldman 2736520Sfeldman IF_DEQUEUE(&es->es_if.if_snd, m); 2746520Sfeldman if (m == 0) { 2756520Sfeldman es->es_oactive = 0; 2766520Sfeldman return; 2776520Sfeldman } 2786520Sfeldman ecput(es->es_buf[ECTBF], m); 2796520Sfeldman 2806520Sfeldman restart: 2817216Ssam addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 2826520Sfeldman addr->ec_xcr = EC_WRITE|ECTBF; 2836520Sfeldman es->es_oactive = 1; 2846520Sfeldman } 2856520Sfeldman 2866520Sfeldman /* 2876520Sfeldman * Ethernet interface transmitter interrupt. 2886520Sfeldman * Start another output if more data to send. 2896520Sfeldman */ 2906520Sfeldman ecxint(unit) 2916520Sfeldman int unit; 2926520Sfeldman { 2936520Sfeldman register struct ec_softc *es = &ec_softc[unit]; 2947216Ssam register struct ecdevice *addr = 2957216Ssam (struct ecdevice *)ecinfo[unit]->ui_addr; 2966520Sfeldman 2976520Sfeldman if (es->es_oactive == 0) 2986520Sfeldman return; 2997216Ssam if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) { 3007216Ssam printf("ec%d: stray xmit interrupt, xcr=%b\n", unit, 3017216Ssam addr->ec_xcr, EC_XBITS); 3027216Ssam es->es_oactive = 0; 3037216Ssam addr->ec_xcr = EC_XCLR; 3047216Ssam return; 3057216Ssam } 3066520Sfeldman es->es_if.if_opackets++; 3076520Sfeldman es->es_oactive = 0; 3086520Sfeldman es->es_mask = ~0; 3096520Sfeldman addr->ec_xcr = EC_XCLR; 3107216Ssam if (es->es_if.if_snd.ifq_head) 3117216Ssam ecstart(unit); 3126520Sfeldman } 3136520Sfeldman 3146520Sfeldman /* 3156520Sfeldman * Collision on ethernet interface. Do exponential 3166520Sfeldman * backoff, and retransmit. If have backed off all 3176520Sfeldman * the way print warning diagnostic, and drop packet. 3186520Sfeldman */ 3196520Sfeldman eccollide(unit) 3206520Sfeldman int unit; 3216520Sfeldman { 3226520Sfeldman struct ec_softc *es = &ec_softc[unit]; 3236520Sfeldman 3246520Sfeldman es->es_if.if_collisions++; 3257216Ssam if (es->es_oactive) 3267216Ssam ecdocoll(unit); 3276520Sfeldman } 3286520Sfeldman 3296520Sfeldman ecdocoll(unit) 3306520Sfeldman int unit; 3316520Sfeldman { 3326520Sfeldman register struct ec_softc *es = &ec_softc[unit]; 3336545Sfeldman register struct ecdevice *addr = 3346545Sfeldman (struct ecdevice *)ecinfo[unit]->ui_addr; 3356545Sfeldman register i; 3366545Sfeldman int delay; 3376520Sfeldman 3386520Sfeldman /* 3396520Sfeldman * Es_mask is a 16 bit number with n low zero bits, with 3406520Sfeldman * n the number of backoffs. When es_mask is 0 we have 3416520Sfeldman * backed off 16 times, and give up. 3426520Sfeldman */ 3436520Sfeldman if (es->es_mask == 0) { 3446545Sfeldman es->es_if.if_oerrors++; 3456520Sfeldman printf("ec%d: send error\n", unit); 3466520Sfeldman /* 3476545Sfeldman * Reset interface, then requeue rcv buffers. 3486545Sfeldman * Some incoming packets may be lost, but that 3496545Sfeldman * can't be helped. 3506520Sfeldman */ 3516545Sfeldman addr->ec_xcr = EC_UECLR; 3526545Sfeldman for (i=ECRHBF; i>=ECRLBF; i--) 3536545Sfeldman addr->ec_rcr = EC_READ|i; 3546545Sfeldman /* 3556545Sfeldman * Reset and transmit next packet (if any). 3566545Sfeldman */ 3576545Sfeldman es->es_oactive = 0; 3586545Sfeldman es->es_mask = ~0; 3596545Sfeldman if (es->es_if.if_snd.ifq_head) 3606545Sfeldman ecstart(unit); 3616520Sfeldman return; 3626520Sfeldman } 3636520Sfeldman /* 3646545Sfeldman * Do exponential backoff. Compute delay based on low bits 3656545Sfeldman * of the interval timer. Then delay for that number of 3666545Sfeldman * slot times. A slot time is 51.2 microseconds (rounded to 51). 3676545Sfeldman * This does not take into account the time already used to 3686545Sfeldman * process the interrupt. 3696520Sfeldman */ 3706520Sfeldman es->es_mask <<= 1; 3716545Sfeldman delay = mfpr(ICR) &~ es->es_mask; 3726545Sfeldman DELAY(delay * 51); 3736520Sfeldman /* 3746545Sfeldman * Clear the controller's collision flag, thus enabling retransmit. 3756520Sfeldman */ 3767470Sfeldman addr->ec_xcr = EC_CLEAR; 3776520Sfeldman } 3786520Sfeldman 3796520Sfeldman /* 3806520Sfeldman * Ethernet interface receiver interrupt. 3816520Sfeldman * If input error just drop packet. 3826520Sfeldman * Otherwise purge input buffered data path and examine 3836520Sfeldman * packet to determine type. If can't determine length 3846520Sfeldman * from type, then have to drop packet. Othewise decapsulate 3856520Sfeldman * packet based on type and pass to type specific higher-level 3866520Sfeldman * input routine. 3876520Sfeldman */ 3886520Sfeldman ecrint(unit) 3896520Sfeldman int unit; 3906520Sfeldman { 3916520Sfeldman struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 3926520Sfeldman 3936520Sfeldman while (addr->ec_rcr & EC_RDONE) 3946520Sfeldman ecread(unit); 3956520Sfeldman } 3966520Sfeldman 3976520Sfeldman ecread(unit) 3986520Sfeldman int unit; 3996520Sfeldman { 4006520Sfeldman register struct ec_softc *es = &ec_softc[unit]; 4016520Sfeldman struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; 4026520Sfeldman register struct ec_header *ec; 4036520Sfeldman struct mbuf *m; 4048773Sroot int len, off, resid, ecoff, rbuf; 4056520Sfeldman register struct ifqueue *inq; 4068773Sroot u_char *ecbuf; 4076520Sfeldman 4086520Sfeldman es->es_if.if_ipackets++; 4098773Sroot rbuf = addr->ec_rcr & EC_RBN; 4108773Sroot if (rbuf < ECRLBF || rbuf > ECRHBF) 4116520Sfeldman panic("ecrint"); 4128773Sroot ecbuf = es->es_buf[rbuf]; 4136520Sfeldman ecoff = *(short *)ecbuf; 4146545Sfeldman if (ecoff <= ECRDOFF || ecoff > 2046) { 4156520Sfeldman es->es_if.if_ierrors++; 4166520Sfeldman #ifdef notdef 4176520Sfeldman if (es->es_if.if_ierrors % 100 == 0) 4186520Sfeldman printf("ec%d: += 100 input errors\n", unit); 4196520Sfeldman #endif 4206520Sfeldman goto setup; 4216520Sfeldman } 4226520Sfeldman 4236520Sfeldman /* 4246520Sfeldman * Get input data length. 4256520Sfeldman * Get pointer to ethernet header (in input buffer). 4266520Sfeldman * Deal with trailer protocol: if type is PUP trailer 4276520Sfeldman * get true type from first 16-bit word past data. 4286520Sfeldman * Remember that type was trailer by setting off. 4296520Sfeldman */ 4306520Sfeldman len = ecoff - ECRDOFF - sizeof (struct ec_header); 4316520Sfeldman ec = (struct ec_header *)(ecbuf + ECRDOFF); 4326520Sfeldman #define ecdataaddr(ec, off, type) ((type)(((caddr_t)((ec)+1)+(off)))) 4336520Sfeldman if (ec->ec_type >= ECPUP_TRAIL && 4346520Sfeldman ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) { 4356520Sfeldman off = (ec->ec_type - ECPUP_TRAIL) * 512; 4366520Sfeldman if (off >= ECMTU) 4376520Sfeldman goto setup; /* sanity */ 4386520Sfeldman ec->ec_type = *ecdataaddr(ec, off, u_short *); 4396520Sfeldman resid = *(ecdataaddr(ec, off+2, u_short *)); 4406520Sfeldman if (off + resid > len) 4416520Sfeldman goto setup; /* sanity */ 4426520Sfeldman len = off + resid; 4436520Sfeldman } else 4446520Sfeldman off = 0; 4456520Sfeldman if (len == 0) 4466520Sfeldman goto setup; 4476520Sfeldman 4486520Sfeldman /* 4496520Sfeldman * Pull packet off interface. Off is nonzero if packet 4506520Sfeldman * has trailing header; ecget will then force this header 4516520Sfeldman * information to be at the front, but we still have to drop 4526520Sfeldman * the type and length which are at the front of any trailer data. 4536520Sfeldman */ 4546520Sfeldman m = ecget(ecbuf, len, off); 4556520Sfeldman if (m == 0) 4566520Sfeldman goto setup; 4576520Sfeldman if (off) { 4586520Sfeldman m->m_off += 2 * sizeof (u_short); 4596520Sfeldman m->m_len -= 2 * sizeof (u_short); 4606520Sfeldman } 4616520Sfeldman switch (ec->ec_type) { 4626520Sfeldman 4636520Sfeldman #ifdef INET 4646520Sfeldman case ECPUP_IPTYPE: 4656520Sfeldman schednetisr(NETISR_IP); 4666520Sfeldman inq = &ipintrq; 4676520Sfeldman break; 4686520Sfeldman #endif 4696520Sfeldman default: 4706520Sfeldman m_freem(m); 4716520Sfeldman goto setup; 4726520Sfeldman } 4736520Sfeldman 4746520Sfeldman if (IF_QFULL(inq)) { 4756520Sfeldman IF_DROP(inq); 4766520Sfeldman m_freem(m); 4777216Ssam goto setup; 4787216Ssam } 4797216Ssam IF_ENQUEUE(inq, m); 4806520Sfeldman 4816520Sfeldman setup: 4826520Sfeldman /* 4836520Sfeldman * Reset for next packet. 4846520Sfeldman */ 4858773Sroot addr->ec_rcr = EC_READ|EC_RCLR|rbuf; 4866520Sfeldman } 4876520Sfeldman 4886520Sfeldman /* 4896520Sfeldman * Ethernet output routine. 4906520Sfeldman * Encapsulate a packet of type family for the local net. 4916520Sfeldman * Use trailer local net encapsulation if enough data in first 4926520Sfeldman * packet leaves a multiple of 512 bytes of data in remainder. 4936545Sfeldman * If destination is this address or broadcast, send packet to 4946545Sfeldman * loop device to kludge around the fact that 3com interfaces can't 4956545Sfeldman * talk to themselves. 4966520Sfeldman */ 4976520Sfeldman ecoutput(ifp, m0, dst) 4986520Sfeldman struct ifnet *ifp; 4996520Sfeldman struct mbuf *m0; 5006520Sfeldman struct sockaddr *dst; 5016520Sfeldman { 5026520Sfeldman int type, dest, s, error; 5036520Sfeldman register struct ec_softc *es = &ec_softc[ifp->if_unit]; 5046520Sfeldman register struct mbuf *m = m0; 5056520Sfeldman register struct ec_header *ec; 5067216Ssam register int off, i; 5076545Sfeldman struct mbuf *mcopy = (struct mbuf *) 0; /* Null */ 5086520Sfeldman 5096520Sfeldman switch (dst->sa_family) { 5106520Sfeldman 5116520Sfeldman #ifdef INET 5126520Sfeldman case AF_INET: 5136520Sfeldman dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; 5146545Sfeldman if ((dest &~ 0xff) == 0) 5158838Sroot mcopy = m_copy(m, 0, (int)M_COPYALL); 5166545Sfeldman else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)-> 5176545Sfeldman sin_addr.s_addr) { 5186545Sfeldman mcopy = m; 5196545Sfeldman goto gotlocal; 5206545Sfeldman } 5216520Sfeldman off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 5226520Sfeldman if (off > 0 && (off & 0x1ff) == 0 && 5236520Sfeldman m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 5246520Sfeldman type = ECPUP_TRAIL + (off>>9); 5256520Sfeldman m->m_off -= 2 * sizeof (u_short); 5266520Sfeldman m->m_len += 2 * sizeof (u_short); 5276520Sfeldman *mtod(m, u_short *) = ECPUP_IPTYPE; 5286520Sfeldman *(mtod(m, u_short *) + 1) = m->m_len; 5296520Sfeldman goto gottrailertype; 5306520Sfeldman } 5316520Sfeldman type = ECPUP_IPTYPE; 5326520Sfeldman off = 0; 5336520Sfeldman goto gottype; 5346520Sfeldman #endif 5356520Sfeldman 5366520Sfeldman default: 5376520Sfeldman printf("ec%d: can't handle af%d\n", ifp->if_unit, 5386520Sfeldman dst->sa_family); 5396520Sfeldman error = EAFNOSUPPORT; 5406520Sfeldman goto bad; 5416520Sfeldman } 5426520Sfeldman 5436520Sfeldman gottrailertype: 5446520Sfeldman /* 5456520Sfeldman * Packet to be sent as trailer: move first packet 5466520Sfeldman * (control information) to end of chain. 5476520Sfeldman */ 5486520Sfeldman while (m->m_next) 5496520Sfeldman m = m->m_next; 5506520Sfeldman m->m_next = m0; 5516520Sfeldman m = m0->m_next; 5526520Sfeldman m0->m_next = 0; 5536520Sfeldman m0 = m; 5546520Sfeldman 5556520Sfeldman gottype: 5566520Sfeldman /* 5576520Sfeldman * Add local net header. If no space in first mbuf, 5586520Sfeldman * allocate another. 5596520Sfeldman */ 5606520Sfeldman if (m->m_off > MMAXOFF || 5616520Sfeldman MMINOFF + sizeof (struct ec_header) > m->m_off) { 5626520Sfeldman m = m_get(M_DONTWAIT); 5636520Sfeldman if (m == 0) { 5646520Sfeldman error = ENOBUFS; 5656520Sfeldman goto bad; 5666520Sfeldman } 5676520Sfeldman m->m_next = m0; 5686520Sfeldman m->m_off = MMINOFF; 5696520Sfeldman m->m_len = sizeof (struct ec_header); 5706520Sfeldman } else { 5716520Sfeldman m->m_off -= sizeof (struct ec_header); 5726520Sfeldman m->m_len += sizeof (struct ec_header); 5736520Sfeldman } 5746520Sfeldman ec = mtod(m, struct ec_header *); 5756520Sfeldman for (i=0; i<6; i++) 5766520Sfeldman ec->ec_shost[i] = es->es_enaddr[i]; 5776525Sfeldman if ((dest &~ 0xff) == 0) 5787216Ssam /* broadcast address */ 5796520Sfeldman for (i=0; i<6; i++) 5806520Sfeldman ec->ec_dhost[i] = 0xff; 5816520Sfeldman else { 5826892Sfeldman if (dest & 0x8000) { 5836892Sfeldman ec->ec_dhost[0] = ec_iltop[0]; 5846892Sfeldman ec->ec_dhost[1] = ec_iltop[1]; 5856892Sfeldman ec->ec_dhost[2] = ec_iltop[2]; 5866892Sfeldman } else { 5876892Sfeldman ec->ec_dhost[0] = es->es_enaddr[0]; 5886892Sfeldman ec->ec_dhost[1] = es->es_enaddr[1]; 5896892Sfeldman ec->ec_dhost[2] = es->es_enaddr[2]; 5906892Sfeldman } 5916892Sfeldman ec->ec_dhost[3] = (dest>>8) & 0x7f; 5926520Sfeldman ec->ec_dhost[4] = (dest>>16) & 0xff; 5936520Sfeldman ec->ec_dhost[5] = (dest>>24) & 0xff; 5946520Sfeldman } 5956520Sfeldman ec->ec_type = type; 5966520Sfeldman 5976520Sfeldman /* 5986520Sfeldman * Queue message on interface, and start output if interface 5996520Sfeldman * not yet active. 6006520Sfeldman */ 6016520Sfeldman s = splimp(); 6026520Sfeldman if (IF_QFULL(&ifp->if_snd)) { 6036520Sfeldman IF_DROP(&ifp->if_snd); 6046520Sfeldman error = ENOBUFS; 6056520Sfeldman goto qfull; 6066520Sfeldman } 6076520Sfeldman IF_ENQUEUE(&ifp->if_snd, m); 6086520Sfeldman if (es->es_oactive == 0) 6096520Sfeldman ecstart(ifp->if_unit); 6106520Sfeldman splx(s); 6117216Ssam 6126545Sfeldman gotlocal: 6137216Ssam return(mcopy ? looutput(&loif, mcopy, dst) : 0); 6147216Ssam 6156520Sfeldman qfull: 6166520Sfeldman m0 = m; 6176520Sfeldman splx(s); 6186520Sfeldman bad: 6196520Sfeldman m_freem(m0); 6206520Sfeldman return(error); 6216520Sfeldman } 6226520Sfeldman 6236520Sfeldman /* 624*9177Ssam * Routine to copy from mbuf chain to transmit 6257216Ssam * buffer in UNIBUS memory. 626*9177Ssam * If packet size is less than the minimum legal size, 627*9177Ssam * the buffer is expanded. We probably should zero out the extra 628*9177Ssam * bytes for security, but that would slow things down. 6296520Sfeldman */ 6306520Sfeldman ecput(ecbuf, m) 6317216Ssam u_char *ecbuf; 6326520Sfeldman struct mbuf *m; 6336520Sfeldman { 6346520Sfeldman register struct mbuf *mp; 6357216Ssam register int off; 6367263Ssam u_char *bp; 6376520Sfeldman 6387216Ssam for (off = 2048, mp = m; mp; mp = mp->m_next) 6397216Ssam off -= mp->m_len; 640*9177Ssam if (2048 - off < ECMIN + sizeof (struct ec_header)) 641*9177Ssam off = 2048 - ECMIN - sizeof (struct ec_header); 6427216Ssam *(u_short *)ecbuf = off; 6437216Ssam bp = (u_char *)(ecbuf + off); 6447263Ssam for (mp = m; mp; mp = mp->m_next) { 6457263Ssam register unsigned len = mp->m_len; 6467263Ssam u_char *mcp; 6477216Ssam 6487216Ssam if (len == 0) 6497216Ssam continue; 6507216Ssam mcp = mtod(mp, u_char *); 6517216Ssam if ((unsigned)bp & 01) { 6527032Swnj *bp++ = *mcp++; 6537216Ssam len--; 6547032Swnj } 6557263Ssam if (off = (len >> 1)) { 6567263Ssam register u_short *to, *from; 6577263Ssam 6587263Ssam to = (u_short *)bp; 6597263Ssam from = (u_short *)mcp; 6607263Ssam do 6617263Ssam *to++ = *from++; 6627263Ssam while (--off > 0); 6637263Ssam bp = (u_char *)to, 6647263Ssam mcp = (u_char *)from; 6657032Swnj } 6667263Ssam if (len & 01) 6676520Sfeldman *bp++ = *mcp++; 6686520Sfeldman } 6697216Ssam #ifdef notdef 6707216Ssam if (bp - ecbuf != 2048) 6717216Ssam printf("ec: bad ecput, diff=%d\n", bp-ecbuf); 6727216Ssam #endif 6737263Ssam m_freem(m); 6746520Sfeldman } 6756520Sfeldman 6766520Sfeldman /* 6776520Sfeldman * Routine to copy from UNIBUS memory into mbufs. 6786520Sfeldman * Similar in spirit to if_rubaget. 6797032Swnj * 6807032Swnj * Warning: This makes the fairly safe assumption that 6817032Swnj * mbufs have even lengths. 6826520Sfeldman */ 6836520Sfeldman struct mbuf * 6846520Sfeldman ecget(ecbuf, totlen, off0) 6857263Ssam u_char *ecbuf; 6866520Sfeldman int totlen, off0; 6876520Sfeldman { 6887263Ssam register struct mbuf *m; 6897263Ssam struct mbuf *top = 0, **mp = ⊤ 6907263Ssam register int off = off0, len; 6917263Ssam u_char *cp; 6926520Sfeldman 6937216Ssam cp = ecbuf + ECRDOFF + sizeof (struct ec_header); 6946520Sfeldman while (totlen > 0) { 6957263Ssam register int words; 6967263Ssam u_char *mcp; 6977263Ssam 6986520Sfeldman MGET(m, 0); 6996520Sfeldman if (m == 0) 7006520Sfeldman goto bad; 7016520Sfeldman if (off) { 7026520Sfeldman len = totlen - off; 7036520Sfeldman cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off; 7046520Sfeldman } else 7056520Sfeldman len = totlen; 7066520Sfeldman if (len >= CLBYTES) { 7076520Sfeldman struct mbuf *p; 7086520Sfeldman 7096520Sfeldman MCLGET(p, 1); 7106520Sfeldman if (p != 0) { 7116520Sfeldman m->m_len = len = CLBYTES; 7126520Sfeldman m->m_off = (int)p - (int)m; 7136520Sfeldman } else { 7146520Sfeldman m->m_len = len = MIN(MLEN, len); 7156520Sfeldman m->m_off = MMINOFF; 7166520Sfeldman } 7176520Sfeldman } else { 7186520Sfeldman m->m_len = len = MIN(MLEN, len); 7196520Sfeldman m->m_off = MMINOFF; 7206520Sfeldman } 7217263Ssam mcp = mtod(m, u_char *); 7227263Ssam if (words = (len >> 1)) { 7237263Ssam register u_short *to, *from; 7247263Ssam 7257263Ssam to = (u_short *)mcp; 7267263Ssam from = (u_short *)cp; 7277263Ssam do 7287263Ssam *to++ = *from++; 7297263Ssam while (--words > 0); 7307263Ssam mcp = (u_char *)to; 7317263Ssam cp = (u_char *)from; 7327032Swnj } 7337216Ssam if (len & 01) 7346520Sfeldman *mcp++ = *cp++; 7356520Sfeldman *mp = m; 7366520Sfeldman mp = &m->m_next; 7377263Ssam if (off == 0) { 7386520Sfeldman totlen -= len; 7397263Ssam continue; 7407263Ssam } 7417263Ssam off += len; 7427263Ssam if (off == totlen) { 7437263Ssam cp = ecbuf + ECRDOFF + sizeof (struct ec_header); 7447263Ssam off = 0; 7457263Ssam totlen = off0; 7467263Ssam } 7476520Sfeldman } 7486520Sfeldman return (top); 7496520Sfeldman bad: 7506520Sfeldman m_freem(top); 7516520Sfeldman return (0); 7526520Sfeldman } 753