1*29563Ssam /* if_ace.c 1.10 86/07/16 */ 224007Ssam 324007Ssam /* 424007Ssam * ACC VERSAbus Ethernet controller 524007Ssam */ 624007Ssam #include "ace.h" 724007Ssam #if NACE > 0 824007Ssam 924007Ssam #include "../machine/pte.h" 1024007Ssam 1125694Ssam #include "param.h" 1225694Ssam #include "systm.h" 1325694Ssam #include "mbuf.h" 1425694Ssam #include "buf.h" 1525694Ssam #include "protosw.h" 1625694Ssam #include "socket.h" 1725694Ssam #include "vmmac.h" 1825694Ssam #include "ioctl.h" 1925694Ssam #include "errno.h" 2025694Ssam #include "vmparam.h" 2125855Ssam #include "syslog.h" 2224007Ssam 2324007Ssam #include "../net/if.h" 2424007Ssam #include "../net/netisr.h" 2524007Ssam #include "../net/route.h" 2625855Ssam #ifdef INET 2724007Ssam #include "../netinet/in.h" 2824007Ssam #include "../netinet/in_systm.h" 2925694Ssam #include "../netinet/in_var.h" 3024007Ssam #include "../netinet/ip.h" 3124007Ssam #include "../netinet/ip_var.h" 3224007Ssam #include "../netinet/if_ether.h" 3325855Ssam #endif 3425855Ssam #ifdef NS 3525855Ssam #include "../netns/ns.h" 3625855Ssam #include "../netns/ns_if.h" 3725855Ssam #endif 3824007Ssam 3924007Ssam #include "../tahoe/mtpr.h" 4024007Ssam #include "../tahoeif/if_acereg.h" 4125694Ssam #include "../tahoevba/vbavar.h" 4224007Ssam 4324007Ssam int aceprobe(), aceattach(), acerint(), acecint(); 4424007Ssam struct vba_device *aceinfo[NACE]; 4525983Ssam long acestd[] = { 0 }; 4624007Ssam struct vba_driver acedriver = 4725927Ssam { aceprobe, 0, aceattach, 0, acestd, "ace", aceinfo, "v/eiu", 0 }; 4824007Ssam 4924007Ssam int aceinit(), aceoutput(), aceioctl(), acereset(); 5024007Ssam struct mbuf *aceget(); 5124007Ssam 5224007Ssam /* 5324007Ssam * Ethernet software status per interface. 5424007Ssam * 5524007Ssam * Each interface is referenced by a network interface structure, 5624007Ssam * is_if, which the routing code uses to locate the interface. 5724007Ssam * This structure contains the output queue for the interface, its address, ... 5824007Ssam */ 5924007Ssam struct ace_softc { 6024007Ssam struct arpcom is_ac; /* Ethernet common part */ 6124007Ssam #define is_if is_ac.ac_if /* network-visible interface */ 6224007Ssam #define is_addr is_ac.ac_enaddr /* hardware Ethernet address */ 6324007Ssam short is_flags; 6424007Ssam #define ACEF_OACTIVE 0x1 /* output is active */ 6524007Ssam #define ACEF_RCVPENDING 0x2 /* start rcv in acecint */ 6624007Ssam short is_promiscuous; /* true is enabled */ 6724007Ssam short is_segboundry; /* first TX Seg in dpm */ 6824007Ssam short is_eictr; /* Rx segment tracking ctr */ 6924007Ssam short is_eoctr; /* Tx segment tracking ctr */ 7024007Ssam short is_txnext; /* Next available Tx segment */ 7124007Ssam short is_currnd; /* current random backoff */ 7224007Ssam struct ace_stats is_stats; /* holds board statistics */ 7324007Ssam short is_xcnt; /* count xmitted segments to be acked 7424007Ssam by the controller */ 7525855Ssam long is_ivec; /* autoconfig interrupt vector base */ 7625927Ssam struct pte *is_map; /* pte map for dual ported memory */ 7725927Ssam caddr_t is_dpm; /* address of mapped memory */ 7824007Ssam } ace_softc[NACE]; 7924007Ssam extern struct ifnet loif; 8024007Ssam 8125855Ssam aceprobe(reg, vi) 8224007Ssam caddr_t reg; 8325855Ssam struct vba_device *vi; 8424007Ssam { 8525855Ssam register br, cvec; /* must be r12, r11 */ 8625855Ssam struct acedevice *ap = (struct acedevice *)reg; 8725855Ssam struct ace_softc *is = &ace_softc[vi->ui_unit]; 8824007Ssam 8924007Ssam #ifdef lint 9024007Ssam acerint(0); acecint(0); 9124007Ssam #endif 9224007Ssam if (badaddr(reg, 2)) 9325855Ssam return (0); 9425855Ssam movow(&ap->csr, CSR_RESET); 9524007Ssam DELAY(10000); 9625855Ssam #ifdef notdef 9725855Ssam /* 9825855Ssam * Select two spaces for the interrupts aligned to an 9925855Ssam * eight vector boundary and fitting in 8 bits (as 10025855Ssam * required by the controller) -- YECH. The controller 10125855Ssam * will be notified later at initialization time. 10225855Ssam */ 10325855Ssam if ((vi->ui_hd->vh_lastiv -= 2) > 0xff) 10425855Ssam vi->ui_hd->vh_lastiv = 0x200; 10525855Ssam is->is_ivec = vi->ui_hd->vh_lastiv = vi->ui_hd->vh_lastiv &~ 0x7; 10625855Ssam #else 10725855Ssam is->is_ivec = 0x90+vi->ui_unit*8; 10825855Ssam #endif 10925855Ssam br = 0x14, cvec = is->is_ivec; /* XXX */ 11025855Ssam return (sizeof (*ap)); 11124007Ssam } 11224007Ssam 11324007Ssam /* 11424007Ssam * Interface exists: make available by filling in network interface 11524007Ssam * record. System will initialize the interface when it is ready 11624007Ssam * to accept packets. 11724007Ssam */ 11824007Ssam aceattach(ui) 11924007Ssam struct vba_device *ui; 12024007Ssam { 12124007Ssam register short unit = ui->ui_unit; 12224007Ssam register struct ace_softc *is = &ace_softc[unit]; 12324007Ssam register struct ifnet *ifp = &is->is_if; 12424007Ssam register struct acedevice *addr = (struct acedevice *)ui->ui_addr; 12524007Ssam register short *wp, i; 12629408Ssam char *cp; 12724007Ssam 12824007Ssam ifp->if_unit = unit; 12924007Ssam ifp->if_name = "ace"; 13024007Ssam ifp->if_mtu = ETHERMTU; 13124007Ssam /* 13229408Ssam * Get station's addresses and set multicast hash table. 13324007Ssam */ 13429408Ssam for (wp = (short *)addr->station, i = 0; i < 6; i++) 13529408Ssam is->is_addr[i] = ~*wp++; 13629408Ssam printf("ace%d: hardware address %s\n", unit, 13729408Ssam ether_sprintf(is->is_addr)); 13824007Ssam is->is_promiscuous = 0; 13929408Ssam for (wp = (short *)addr->hash, i = 0; i < 8; i++) 14029408Ssam movow(wp++, ~0xf); 14125694Ssam movow(&addr->bcastena[0], ~0xffff); 14225694Ssam movow(&addr->bcastena[1], ~0xffff); 14325927Ssam /* 14425927Ssam * Allocate and map dual ported VERSAbus memory. 14525927Ssam */ 14625927Ssam vbmemalloc(32, (caddr_t)ui->ui_flags, &is->is_map, &is->is_dpm); 14725927Ssam 14824007Ssam ifp->if_init = aceinit; 14924007Ssam ifp->if_output = aceoutput; 15024007Ssam ifp->if_ioctl = aceioctl; 15124007Ssam ifp->if_reset = acereset; 15225694Ssam ifp->if_flags = IFF_BROADCAST; 15324007Ssam if_attach(ifp); 15424007Ssam } 15524007Ssam 15624007Ssam /* 15724007Ssam * Reset of interface after "system" reset. 15824007Ssam */ 15924007Ssam acereset(unit, vban) 16024007Ssam int unit, vban; 16124007Ssam { 16224007Ssam register struct vba_device *ui; 16324007Ssam 16424007Ssam if (unit >= NACE || (ui = aceinfo[unit]) == 0 || ui->ui_alive == 0 || 16524007Ssam ui->ui_vbanum != vban) 16624007Ssam return; 16724007Ssam printf(" ace%d", unit); 16824007Ssam aceinit(unit); 16924007Ssam } 17024007Ssam 17124007Ssam /* 17224007Ssam * Initialization of interface; clear recorded pending operations 17324007Ssam */ 17424007Ssam aceinit(unit) 17524007Ssam int unit; 17624007Ssam { 17724007Ssam register struct ace_softc *is = &ace_softc[unit]; 17824007Ssam register struct vba_device *ui = aceinfo[unit]; 17924007Ssam register struct acedevice *addr; 18024007Ssam register struct ifnet *ifp = &is->is_if; 18124007Ssam register short Csr; 18225694Ssam register int s; 18324007Ssam 18425694Ssam if (ifp->if_addrlist == (struct ifaddr *)0) 18524007Ssam return; 18624007Ssam if ((ifp->if_flags & IFF_RUNNING) == 0) { 18724007Ssam /* 18824007Ssam * Reset the controller, initialize the recieve buffers, 18924007Ssam * and turn the controller on again and set board online. 19024007Ssam */ 19124007Ssam addr = (struct acedevice *)ui->ui_addr; 19224007Ssam s = splimp(); 19325694Ssam movow(&addr->csr, CSR_RESET); 19424007Ssam DELAY(10000); 19524007Ssam 19624007Ssam /* 19725927Ssam * Clean up dpm since the controller might 19825927Ssam * jumble dpm after reset. 19924007Ssam */ 20025927Ssam acesetup(unit); 20125694Ssam movow(&addr->csr, CSR_GO); 20224007Ssam Csr = addr->csr; 20324007Ssam if (Csr & CSR_ACTIVE) { 20425855Ssam movow(&addr->ivct, is->is_ivec); 20524007Ssam Csr |= CSR_IENA | is->is_promiscuous; 20625694Ssam movow(&addr->csr, Csr); 20724007Ssam is->is_flags = 0; 20824007Ssam is->is_xcnt = 0; 20925694Ssam is->is_if.if_flags |= IFF_RUNNING; 21024007Ssam } 21124007Ssam splx(s); 21224007Ssam } 21325694Ssam if (is->is_if.if_snd.ifq_head) 21425927Ssam acestart(unit); 21524007Ssam } 21624007Ssam 21724007Ssam /* 21824007Ssam * Start output on interface. 21924007Ssam * Get another datagram to send off of the interface queue, 22024007Ssam * and map it to the interface before starting the output. 22124007Ssam */ 22225927Ssam acestart(unit) 22325927Ssam int unit; 22424007Ssam { 22524007Ssam register struct tx_segment *txs; 22625694Ssam register long len; 22725694Ssam register int s; 22824007Ssam register struct ace_softc *is = &ace_softc[unit]; 22924007Ssam struct mbuf *m; 23025694Ssam short retries; 23124007Ssam 23225927Ssam if (is->is_flags & ACEF_OACTIVE) 23325927Ssam return; 23425927Ssam is->is_flags |= ACEF_OACTIVE; 23524007Ssam again: 23624007Ssam txs = (struct tx_segment*)(is->is_dpm + (is->is_txnext << 11)); 23724007Ssam if (txs->tx_csr & TCS_TBFULL) { 23824007Ssam is->is_stats.tx_busy++; 23925927Ssam is->is_flags &= ~ACEF_OACTIVE; 24024007Ssam return; 24124007Ssam } 24225694Ssam s = splimp(); 24324007Ssam IF_DEQUEUE(&is->is_if.if_snd, m); 24425694Ssam splx(s); 24525927Ssam if (m == 0) { 24625927Ssam is->is_flags &= ~ACEF_OACTIVE; 24724007Ssam return; 24825927Ssam } 24924007Ssam len = aceput(unit, txs->tx_data, m); 25024007Ssam retries = txs->tx_csr & TCS_RTC; 25124007Ssam if (retries > 0) 25224007Ssam acebakoff(is, txs, retries); 25324007Ssam 25424007Ssam /* 25524007Ssam * Ensure minimum packet length. 25624007Ssam * This makes the safe assumtion that there are no virtual holes 25724007Ssam * after the data. 25824007Ssam * For security, it might be wise to zero out the added bytes, 25924007Ssam * but we're mainly interested in speed at the moment. 26024007Ssam */ 26124007Ssam if (len - sizeof (struct ether_header) < ETHERMIN) 26224007Ssam len = ETHERMIN + sizeof (struct ether_header); 26324007Ssam if (++is->is_txnext > SEG_MAX) 26424007Ssam is->is_txnext = is->is_segboundry; 26524007Ssam is->is_if.if_opackets++; 26624007Ssam is->is_xcnt++; 26724007Ssam len = (len & 0x7fff) | TCS_TBFULL; 26825694Ssam movow(txs, len); 26924007Ssam goto again; 27024007Ssam } 27124007Ssam 27224007Ssam /* 27324007Ssam * Transmit done interrupt. 27424007Ssam */ 27524007Ssam acecint(unit) 27624007Ssam int unit; 27724007Ssam { 27824007Ssam register struct ace_softc *is = &ace_softc[unit]; 27924007Ssam register struct tx_segment *txseg; 28025694Ssam short eostat; 28124007Ssam 28224007Ssam if (is->is_xcnt <= 0) { 28325855Ssam log(LOG_ERR, "ace%d: stray xmit interrupt, xcnt %d\n", 28424007Ssam unit, is->is_xcnt); 28524007Ssam is->is_xcnt = 0; 28625694Ssam if (is->is_if.if_snd.ifq_head) 28725927Ssam acestart(unit); 28824007Ssam return; 28924007Ssam } 29024007Ssam is->is_xcnt--; 29124007Ssam txseg = (struct tx_segment *)((is->is_eoctr << 11) + is->is_dpm); 29224007Ssam eostat = txseg->tx_csr; 29324007Ssam if ((eostat & TCS_TBFULL) == 0) { 29424007Ssam is->is_stats.tx_retries += eostat & TCS_RTC; 29524007Ssam if (eostat & TCS_RTFAIL) { 29624007Ssam is->is_stats.tx_discarded++; 29724007Ssam is->is_if.if_oerrors++; 29824007Ssam } else 29924007Ssam is->is_stats.tx_datagrams++; 30024007Ssam if (++is->is_eoctr >= 16) 30124007Ssam is->is_eoctr = is->is_segboundry; 30224007Ssam } 30325694Ssam if (is->is_if.if_snd.ifq_head) 30425927Ssam acestart(unit); 30524007Ssam } 30624007Ssam 30724007Ssam /* 30824007Ssam * Ethernet interface receiver interrupt. 30924007Ssam * If input error just drop packet. 31024007Ssam * Otherwise purge input buffered data path and examine 31124007Ssam * packet to determine type. If can't determine length 31224007Ssam * from type, then have to drop packet. Othewise decapsulate 31324007Ssam * packet based on type and pass to type specific higher-level 31424007Ssam * input routine. 31524007Ssam */ 31624007Ssam acerint(unit) 31724007Ssam int unit; 31824007Ssam { 31924007Ssam register struct ace_softc *is = &ace_softc[unit]; 32024007Ssam register struct ifqueue *inq; 32124007Ssam register struct ether_header *ace; 32224007Ssam register struct rx_segment *rxseg; 32324007Ssam int len, s, off, resid; 32424007Ssam struct mbuf *m; 32524007Ssam short eistat; 32624007Ssam 32725694Ssam if ((is->is_if.if_flags&IFF_RUNNING) == 0) 32825694Ssam return; 32924007Ssam again: 33024007Ssam rxseg = (struct rx_segment *)((is->is_eictr << 11) + is->is_dpm); 33124007Ssam eistat = rxseg->rx_csr; 33224007Ssam if ((eistat & RCS_RBFULL) == 0) 33324007Ssam return; 33424007Ssam is->is_if.if_ipackets++; 33524007Ssam if (++is->is_eictr >= is->is_segboundry) 33624007Ssam is->is_eictr = 0; 33724007Ssam len = eistat & RCS_RBC; 33824007Ssam if ((eistat & (RCS_ROVRN | RCS_RCRC | RCS_RODD)) || 33924007Ssam len < ET_MINLEN || len > ET_MAXLEN+CRC_SIZE) { 34024007Ssam if (eistat & RCS_ROVRN) 34124007Ssam is->is_stats.rx_overruns++; 34224007Ssam if (eistat & RCS_RCRC) 34324007Ssam is->is_stats.rx_crc_errors++; 34424007Ssam if (eistat & RCS_RODD) 34524007Ssam is->is_stats.rx_align_errors++; 34624007Ssam if (len < ET_MINLEN) 34724007Ssam is->is_stats.rx_underruns++; 34824007Ssam if (len > ET_MAXLEN+CRC_SIZE) 34924007Ssam is->is_stats.rx_overruns++; 35024007Ssam is->is_if.if_ierrors++; 35124007Ssam rxseg->rx_csr = 0; 35224007Ssam return; 35324007Ssam } else 35424007Ssam is->is_stats.rx_datagrams++; 35524007Ssam ace = (struct ether_header *)rxseg->rx_data; 35624007Ssam len -= sizeof (struct ether_header); 35724007Ssam /* 35825694Ssam * Deal with trailer protocol: if type is trailer 35924007Ssam * get true type from first 16-bit word past data. 36024007Ssam * Remember that type was trailer by setting off. 36124007Ssam */ 36224007Ssam ace->ether_type = ntohs((u_short)ace->ether_type); 36324007Ssam #define acedataaddr(ace, off, type) \ 36424007Ssam ((type)(((caddr_t)(((char *)ace)+sizeof (struct ether_header))+(off)))) 36525694Ssam if (ace->ether_type >= ETHERTYPE_TRAIL && 36625694Ssam ace->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) { 36725694Ssam off = (ace->ether_type - ETHERTYPE_TRAIL) * 512; 36824007Ssam if (off >= ETHERMTU) 36924007Ssam goto setup; /* sanity */ 37024007Ssam ace->ether_type = ntohs(*acedataaddr(ace, off, u_short *)); 37124007Ssam resid = ntohs(*(acedataaddr(ace, off+2, u_short *))); 37224007Ssam if (off + resid > len) 37324007Ssam goto setup; /* sanity */ 37424007Ssam len = off + resid; 37524007Ssam } else 37624007Ssam off = 0; 37724007Ssam if (len == 0) 37824007Ssam goto setup; 37924007Ssam 38024007Ssam /* 38124007Ssam * Pull packet off interface. Off is nonzero if packet 38224007Ssam * has trailing header; aceget will then force this header 38324007Ssam * information to be at the front, but we still have to drop 38424007Ssam * the type and length which are at the front of any trailer data. 38524007Ssam */ 38625855Ssam m = aceget((u_char *)rxseg->rx_data, len, off, &is->is_if); 38724007Ssam if (m == 0) 38824007Ssam goto setup; 38924007Ssam if (off) { 39025855Ssam struct ifnet *ifp; 39125855Ssam 39225855Ssam ifp = *(mtod(m, struct ifnet **)); 39324007Ssam m->m_off += 2 * sizeof (u_short); 39424007Ssam m->m_len -= 2 * sizeof (u_short); 39525855Ssam *(mtod(m, struct ifnet **)) = ifp; 39624007Ssam } 39724007Ssam switch (ace->ether_type) { 39824007Ssam 39924007Ssam #ifdef INET 40025694Ssam case ETHERTYPE_IP: 40124007Ssam schednetisr(NETISR_IP); 40224007Ssam inq = &ipintrq; 40324007Ssam break; 40425855Ssam #endif 40524007Ssam 40625694Ssam case ETHERTYPE_ARP: 40724007Ssam arpinput(&is->is_ac, m); 40824007Ssam goto setup; 40925694Ssam #ifdef NS 41025694Ssam case ETHERTYPE_NS: 41125694Ssam schednetisr(NETISR_NS); 41225694Ssam inq = &nsintrq; 41325694Ssam break; 41425694Ssam 41525694Ssam #endif 41624007Ssam default: 41724007Ssam m_freem(m); 41824007Ssam goto setup; 41924007Ssam } 42024007Ssam if (IF_QFULL(inq)) { 42124007Ssam IF_DROP(inq); 42224007Ssam m_freem(m); 42324007Ssam goto setup; 42424007Ssam } 42524007Ssam s = splimp(); 42624007Ssam IF_ENQUEUE(inq, m); 42724007Ssam splx(s); 42824007Ssam setup: 42924007Ssam rxseg->rx_csr = 0; 43024007Ssam goto again; 43124007Ssam } 43224007Ssam 43324007Ssam /* 43424007Ssam * Ethernet output routine. 43524007Ssam * Encapsulate a packet of type family for the local net. 43624007Ssam * Use trailer local net encapsulation if enough data in first 43724007Ssam * packet leaves a multiple of 512 bytes of data in remainder. 43824007Ssam */ 43924007Ssam aceoutput(ifp, m0, dst) 44024007Ssam struct ifnet *ifp; 44124007Ssam struct mbuf *m0; 44224007Ssam struct sockaddr *dst; 44324007Ssam { 44424007Ssam register struct ace_softc *is = &ace_softc[ifp->if_unit]; 44524007Ssam register struct mbuf *m = m0; 44624007Ssam register struct ether_header *ace; 44724007Ssam register int off; 44824007Ssam struct mbuf *mcopy = (struct mbuf *)0; 44925957Ssam int type, s, error, usetrailers; 45025694Ssam u_char edst[6]; 45124007Ssam struct in_addr idst; 45224007Ssam 45325855Ssam if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 45425855Ssam error = ENETDOWN; 45525855Ssam goto bad; 45625855Ssam } 45724007Ssam switch (dst->sa_family) { 45824007Ssam 45924007Ssam #ifdef INET 46024007Ssam case AF_INET: 46124007Ssam idst = ((struct sockaddr_in *)dst)->sin_addr; 46225957Ssam if (!arpresolve(&is->is_ac, m, &idst, edst, &usetrailers)) 46324007Ssam return (0); /* if not yet resolved */ 46425694Ssam if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr, 46525937Ssam sizeof (edst))) 46624007Ssam mcopy = m_copy(m, 0, (int)M_COPYALL); 46724007Ssam off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; 46825957Ssam if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 46924007Ssam m->m_off >= MMINOFF + 2 * sizeof (u_short)) { 47025694Ssam type = ETHERTYPE_TRAIL + (off>>9); 47124007Ssam m->m_off -= 2 * sizeof (u_short); 47224007Ssam m->m_len += 2 * sizeof (u_short); 47325694Ssam *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 47424007Ssam *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 47524007Ssam goto gottrailertype; 47624007Ssam } 47725694Ssam type = ETHERTYPE_IP; 47824007Ssam off = 0; 47924007Ssam goto gottype; 48024007Ssam #endif 48125694Ssam #ifdef NS 48225694Ssam case AF_NS: 48325694Ssam bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 48425694Ssam (caddr_t)edst, sizeof (edst)); 48525694Ssam if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,sizeof(edst))) 48625694Ssam mcopy = m_copy(m, 0, (int)M_COPYALL); 48725694Ssam else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, 48825694Ssam sizeof(edst))) 48925694Ssam return(looutput(&loif, m, dst)); 49025694Ssam type = ETHERTYPE_NS; 49125694Ssam off = 0; 49225694Ssam goto gottype; 49325694Ssam #endif 49424007Ssam case AF_UNSPEC: 49524007Ssam ace = (struct ether_header *)dst->sa_data; 49625694Ssam bcopy((caddr_t)ace->ether_dhost, (caddr_t)edst, sizeof (edst)); 49724007Ssam type = ace->ether_type; 49824007Ssam goto gottype; 49924007Ssam 50024007Ssam default: 50125855Ssam log(LOG_ERR, "ace%d: can't handle af%d\n", 50224007Ssam ifp->if_unit, dst->sa_family); 50324007Ssam error = EAFNOSUPPORT; 50424007Ssam goto bad; 50524007Ssam } 50624007Ssam 50724007Ssam gottrailertype: 50824007Ssam /* 50924007Ssam * Packet to be sent as trailer: move first packet 51024007Ssam * (control information) to end of chain. 51124007Ssam */ 51224007Ssam while (m->m_next) 51324007Ssam m = m->m_next; 51424007Ssam m->m_next = m0; 51524007Ssam m = m0->m_next; 51624007Ssam m0->m_next = 0; 51724007Ssam m0 = m; 51824007Ssam 51924007Ssam gottype: 52024007Ssam /* 52124007Ssam * Add local net header. If no space in first mbuf, 52224007Ssam * allocate another. 52324007Ssam */ 52424007Ssam if (m->m_off > MMAXOFF || 52524007Ssam MMINOFF + sizeof (struct ether_header) > m->m_off) { 52624007Ssam m = m_get(M_DONTWAIT, MT_HEADER); 52724007Ssam if (m == 0) { 52824007Ssam error = ENOBUFS; 52924007Ssam goto bad; 53024007Ssam } 53124007Ssam m->m_next = m0; 53224007Ssam m->m_off = MMINOFF; 53324007Ssam m->m_len = sizeof (struct ether_header); 53424007Ssam } else { 53524007Ssam m->m_off -= sizeof (struct ether_header); 53624007Ssam m->m_len += sizeof (struct ether_header); 53724007Ssam } 53824007Ssam ace = mtod(m, struct ether_header *); 53925694Ssam bcopy((caddr_t)edst, (caddr_t)ace->ether_dhost, sizeof (edst)); 54025694Ssam bcopy((caddr_t)is->is_addr, (caddr_t)ace->ether_shost, 54125694Ssam sizeof (is->is_addr)); 54224007Ssam ace->ether_type = htons((u_short)type); 54324007Ssam 54424007Ssam /* 54524007Ssam * Queue message on interface, and start output if interface 54624007Ssam * not yet active. 54724007Ssam */ 54824007Ssam s = splimp(); 54924007Ssam if (IF_QFULL(&ifp->if_snd)) { 55024007Ssam IF_DROP(&ifp->if_snd); 55124007Ssam error = ENOBUFS; 55224007Ssam goto qfull; 55324007Ssam } 55424007Ssam IF_ENQUEUE(&ifp->if_snd, m); 55524007Ssam splx(s); 55625927Ssam acestart(ifp->if_unit); 55724007Ssam return (mcopy ? looutput(&loif, mcopy, dst) : 0); 55824007Ssam qfull: 55924007Ssam m0 = m; 56024007Ssam splx(s); 56124007Ssam bad: 56224007Ssam m_freem(m0); 56324007Ssam if (mcopy) 56424007Ssam m_freem(mcopy); 56524007Ssam return (error); 56624007Ssam } 56724007Ssam 56824007Ssam /* 56924007Ssam * Routine to copy from mbuf chain to transmit buffer on the VERSAbus 57024007Ssam * If packet size is less than the minimum legal size, 57124007Ssam * the buffer is expanded. We probably should zero out the extra 57224007Ssam * bytes for security, but that would slow things down. 57324007Ssam */ 57425694Ssam /*ARGSUSED*/ 57524007Ssam aceput(unit, txbuf, m) 57625855Ssam int unit; 57725694Ssam char *txbuf; 57824007Ssam struct mbuf *m; 57924007Ssam { 58025855Ssam register u_char *bp, *mcp; 58125855Ssam register short *s1, *s2; 58225694Ssam register u_int len; 58324007Ssam register struct mbuf *mp; 58425694Ssam int total; 58524007Ssam 58624007Ssam total = 0; 58725694Ssam bp = (u_char *)txbuf; 58825694Ssam for (mp = m; (mp); mp = mp->m_next) { 58924007Ssam len = mp->m_len; 59024007Ssam if (len == 0) 59124007Ssam continue; 59224007Ssam total += len; 59324007Ssam mcp = mtod(mp, u_char *); 59424007Ssam if (((int)mcp & 01) && ((int)bp & 01)) { 59524007Ssam /* source & destination at odd addresses */ 59625694Ssam movob(bp++, *mcp++); 59724007Ssam --len; 59824007Ssam } 59924007Ssam if (len > 1 && (((int)mcp & 01)==0) && (((int)bp & 01)==0)) { 60025694Ssam register u_int l; 60124007Ssam 60224007Ssam s1 = (short *)bp; 60324007Ssam s2 = (short *)mcp; 60424007Ssam l = len >> 1; /* count # of shorts */ 60525694Ssam while (l-- != 0) 60625694Ssam movow(s1++, *s2++); 60724007Ssam len &= 1; /* # remaining bytes */ 60824007Ssam bp = (u_char *)s1; 60924007Ssam mcp = (u_char *)s2; 61024007Ssam } 61125694Ssam while (len-- != 0) 61225694Ssam movob(bp++, *mcp++); 61324007Ssam } 61424007Ssam m_freem(m); 61524007Ssam return (total); 61624007Ssam } 61724007Ssam 61824007Ssam /* 61924007Ssam * Routine to copy from VERSAbus memory into mbufs. 62024007Ssam * 62124007Ssam * Warning: This makes the fairly safe assumption that 62224007Ssam * mbufs have even lengths. 62324007Ssam */ 62425694Ssam /*ARGSUSED*/ 62524007Ssam struct mbuf * 62625855Ssam aceget(rxbuf, totlen, off0, ifp) 62724007Ssam u_char *rxbuf; 62824007Ssam int totlen, off0; 62925855Ssam struct ifnet *ifp; 63024007Ssam { 63125855Ssam register u_char *cp, *mcp; 63224007Ssam register int tlen; 63324007Ssam register struct mbuf *m; 63424007Ssam struct mbuf *top = 0, **mp = ⊤ 63524007Ssam int len, off = off0; 63624007Ssam 63724007Ssam cp = rxbuf + sizeof (struct ether_header); 63824007Ssam while (totlen > 0) { 63924007Ssam MGET(m, M_DONTWAIT, MT_DATA); 64024007Ssam if (m == 0) 64124007Ssam goto bad; 64224007Ssam if (off) { 64324007Ssam len = totlen - off; 64424007Ssam cp = rxbuf + sizeof (struct ether_header) + off; 64524007Ssam } else 64624007Ssam len = totlen; 64725855Ssam if (ifp) 64825855Ssam len += sizeof(ifp); 649*29563Ssam if (len >= NBPG) { 650*29563Ssam MCLGET(m); 651*29563Ssam if (m->m_len == CLBYTES) 652*29563Ssam m->m_len = len = MIN(len, CLBYTES); 653*29563Ssam else 65424007Ssam m->m_len = len = MIN(MLEN, len); 65524007Ssam } else { 65624007Ssam m->m_len = len = MIN(MLEN, len); 65724007Ssam m->m_off = MMINOFF; 65824007Ssam } 65924007Ssam mcp = mtod(m, u_char *); 66025855Ssam if (ifp) { 66125855Ssam /* 66225855Ssam * Prepend interface pointer to first mbuf. 66325855Ssam */ 66425855Ssam *(mtod(m, struct ifnet **)) = ifp; 66525855Ssam mcp += sizeof(ifp); 66625855Ssam len -= sizeof(ifp); 66725855Ssam ifp = (struct ifnet *)0; 66825855Ssam } 66924007Ssam /*bcopy((caddr_t)cp, (caddr_t)mcp, len);*/ 67024007Ssam /*cp += len; mcp += len;*/ 67124007Ssam tlen = len; 67224007Ssam if (((int)mcp & 01) && ((int)cp & 01)) { 67324007Ssam /* source & destination at odd addresses */ 67424007Ssam *mcp++ = *cp++; 67524007Ssam --tlen; 67624007Ssam } 67724007Ssam if (tlen > 1 && (((int)mcp&01) == 0) && (((int)cp&01) == 0)) { 67824007Ssam register short *s1, *s2; 67924007Ssam register int l; 68024007Ssam 68124007Ssam s1 = (short *)mcp; 68224007Ssam s2 = (short *)cp; 68324007Ssam l = tlen >> 1; /* count # of shorts */ 68424007Ssam while (l-- > 0) /* copy shorts */ 68524007Ssam *s1++ = *s2++; 68624007Ssam tlen &= 1; /* # remaining bytes */ 68724007Ssam mcp = (u_char *)s1; 68824007Ssam cp = (u_char *)s2; 68924007Ssam } 69024007Ssam while (tlen-- > 0) 69124007Ssam *mcp++ = *cp++; 69224007Ssam *mp = m; 69324007Ssam mp = &m->m_next; 69424007Ssam if (off == 0) { 69524007Ssam totlen -= len; 69624007Ssam continue; 69724007Ssam } 69824007Ssam off += len; 69924007Ssam if (off == totlen) { 70024007Ssam cp = rxbuf + sizeof (struct ether_header); 70124007Ssam off = 0; 70224007Ssam totlen = off0; 70324007Ssam } 70424007Ssam } 70524007Ssam return (top); 70624007Ssam bad: 70724007Ssam m_freem(top); 70824007Ssam return (0); 70924007Ssam } 71024007Ssam 71129408Ssam /* backoff table masks */ 71229408Ssam short random_mask_tbl[16] = { 713*29563Ssam 0x0040, 0x00c0, 0x01c0, 0x03c0, 0x07c0, 0x0fc0, 0x1fc0, 0x3fc0, 714*29563Ssam 0x7fc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0 71529408Ssam }; 71629408Ssam 71724007Ssam acebakoff(is, txseg, retries) 71824007Ssam struct ace_softc *is; 71924007Ssam struct tx_segment *txseg; 72024007Ssam register int retries; 72124007Ssam { 72224007Ssam register short *pBakNum, random_num; 72324007Ssam short *pMask; 72424007Ssam 72524007Ssam pMask = &random_mask_tbl[0]; 72624007Ssam pBakNum = &txseg->tx_backoff[0]; 72724007Ssam while (--retries >= 0) { 72824007Ssam random_num = (is->is_currnd = (is->is_currnd * 18741)-13849); 72924007Ssam random_num &= *pMask++; 730*29563Ssam *pBakNum++ = random_num ^ (short)(0xff00 | 0x00fc); 73124007Ssam } 73224007Ssam } 73324007Ssam 73424007Ssam /* 73524007Ssam * Process an ioctl request. 73624007Ssam */ 73724007Ssam aceioctl(ifp, cmd, data) 73824007Ssam register struct ifnet *ifp; 73924007Ssam int cmd; 74024007Ssam caddr_t data; 74124007Ssam { 74225694Ssam register struct ifaddr *ifa = (struct ifaddr *)data; 74325855Ssam struct acedevice *addr; 74425694Ssam int s = splimp(), error = 0; 74524007Ssam 74624007Ssam switch (cmd) { 74724007Ssam 74824007Ssam case SIOCSIFADDR: 74925694Ssam ifp->if_flags |= IFF_UP; 75025855Ssam switch (ifa->ifa_addr.sa_family) { 75125855Ssam #ifdef INET 75225855Ssam case AF_INET: 75325855Ssam aceinit(ifp->if_unit); /* before arpwhohas */ 75425855Ssam ((struct arpcom *)ifp)->ac_ipaddr = 75525855Ssam IA_SIN(ifa)->sin_addr; 75625855Ssam arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr); 75725855Ssam break; 75825855Ssam #endif 75925855Ssam #ifdef NS 76025855Ssam case AF_NS: { 76125937Ssam struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 76225937Ssam struct ace_softc *is = &ace_softc[ifp->if_unit]; 76325855Ssam 76425855Ssam if (!ns_nullhost(*ina)) { 76525855Ssam ifp->if_flags &= ~IFF_RUNNING; 76625855Ssam addr = (struct acedevice *) 76725937Ssam aceinfo[ifp->if_unit]->ui_addr; 76825855Ssam movow(&addr->csr, CSR_RESET); 76925855Ssam DELAY(10000); 77025855Ssam /* set station address & copy addr to arp */ 77129408Ssam acesetaddr(ifp->if_unit, addr, 77225855Ssam ina->x_host.c_host); 77325855Ssam } else 77425937Ssam ina->x_host = *(union ns_host *)is->is_addr; 77525855Ssam aceinit(ifp->if_unit); 77625855Ssam break; 77725855Ssam } 77825855Ssam #endif 77925855Ssam default: 78025855Ssam aceinit(ifp->if_unit); 78125855Ssam break; 78225855Ssam } 78324007Ssam break; 78424007Ssam 78525855Ssam case SIOCSIFFLAGS: 78625855Ssam if ((ifp->if_flags&IFF_UP) == 0 && ifp->if_flags&IFF_RUNNING) { 78725855Ssam addr = (struct acedevice *) 78825855Ssam (aceinfo[ifp->if_unit]->ui_addr); 78925855Ssam movow(&addr->csr, CSR_RESET); 79025855Ssam ifp->if_flags &= ~IFF_RUNNING; 79125855Ssam } else if (ifp->if_flags&IFF_UP && 79225855Ssam (ifp->if_flags&IFF_RUNNING) == 0) 79325855Ssam aceinit(ifp->if_unit); 79424007Ssam break; 79524007Ssam 79624007Ssam default: 79724007Ssam error = EINVAL; 79824007Ssam } 79924007Ssam splx(s); 80024007Ssam return (error); 80124007Ssam } 80224007Ssam 80329408Ssam /* 80429408Ssam * Set the on-board station address, then read it back 80529408Ssam * to initialize the address used by ARP (among others). 80629408Ssam */ 80729408Ssam acesetaddr(unit, addr, station) 80829408Ssam short unit; 80929408Ssam struct acedevice *addr; 81029408Ssam char *station; 81129408Ssam { 81229408Ssam struct ace_softc *is = &ace_softc[unit]; 81329408Ssam register short *wp, i; 81429408Ssam 81529408Ssam for (wp = (short *)addr->station, i = 0; i < 6; i++) 81629408Ssam movow(wp++, ~*station++); 81729408Ssam for (wp = (short *)addr->station, i = 0; i < 6; i++) 81829408Ssam is->is_addr[i] = ~*wp++; 81929408Ssam printf("ace%d: hardware address %s\n", unit, 82029408Ssam ether_sprintf(is->is_addr)); 82129408Ssam } 82229408Ssam 82329408Ssam /* 82429408Ssam * Setup the device for use. Initialize dual-ported memory, 82529408Ssam * backoff parameters, and various other software state. 82629408Ssam */ 82725927Ssam acesetup(unit) 82824007Ssam int unit; 82924007Ssam { 83024007Ssam register struct ace_softc *is = &ace_softc[unit]; 83125927Ssam register char *pData1; 83225694Ssam register short i; 83325927Ssam struct acedevice *addr; 83424007Ssam 83525927Ssam bzero(is->is_dpm, 16384*2); 83624007Ssam is->is_currnd = 49123; 83725927Ssam addr = (struct acedevice *)aceinfo[unit]->ui_addr; 83824007Ssam is->is_segboundry = (addr->segb >> 11) & 0xf; 83925927Ssam pData1 = is->is_dpm + (is->is_segboundry << 11); 84024007Ssam for (i = SEG_MAX + 1 - is->is_segboundry; --i >= 0;) { 84124007Ssam acebakoff(is, (struct tx_segment *)pData1, 15); 84224007Ssam pData1 += sizeof (struct tx_segment); 84324007Ssam } 84424007Ssam is->is_eictr = 0; 84524007Ssam is->is_eoctr = is->is_txnext = is->is_segboundry; 84624007Ssam bzero((char *)&is->is_stats, sizeof (is->is_stats)); 84724007Ssam } 84824007Ssam #endif 849