xref: /csrg-svn/sys/vax/if/if_en.c (revision 5213)
1*5213Swnj /*	if_en.c	4.18	81/12/09	*/
24686Swnj 
34686Swnj #include "en.h"
45105Swnj 
54686Swnj /*
65105Swnj  * Xerox prototype (3 Mb) Ethernet interface driver.
74686Swnj  */
84686Swnj 
94686Swnj #include "../h/param.h"
104686Swnj #include "../h/systm.h"
114686Swnj #include "../h/mbuf.h"
124686Swnj #include "../h/pte.h"
134686Swnj #include "../h/buf.h"
145083Swnj #include "../h/protosw.h"
155083Swnj #include "../h/socket.h"
164686Swnj #include "../h/ubareg.h"
174686Swnj #include "../h/ubavar.h"
184686Swnj #include "../h/enreg.h"
195083Swnj #include "../h/cpu.h"
204686Swnj #include "../h/mtpr.h"
215083Swnj #include "../h/vmmac.h"
225083Swnj #include "../net/in.h"
235083Swnj #include "../net/in_systm.h"
245083Swnj #include "../net/if.h"
255083Swnj #include "../net/if_en.h"
265083Swnj #include "../net/if_uba.h"
275083Swnj #include "../net/ip.h"
285083Swnj #include "../net/ip_var.h"
294686Swnj 
30*5213Swnj #define	ENMTU	(1024+512)
315083Swnj 
324686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
334686Swnj struct	uba_device *eninfo[NEN];
344686Swnj u_short enstd[] = { 0 };
354686Swnj struct	uba_driver endriver =
365171Swnj 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
374686Swnj #define	ENUNIT(x)	minor(x)
384686Swnj 
395105Swnj int	eninit(),enoutput(),enreset();
405105Swnj 
415105Swnj /*
425105Swnj  * Ethernet software status per interface.
435105Swnj  *
445105Swnj  * Each interface is referenced by a network interface structure,
455105Swnj  * es_if, which the routing code uses to locate the interface.
465105Swnj  * This structure contains the output queue for the interface, its address, ...
475105Swnj  * We also have, for each interface, a UBA interface structure, which
485105Swnj  * contains information about the UNIBUS resources held by the interface:
495105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
505105Swnj  * structure for use by the if_uba.c routines in running the interface
515105Swnj  * efficiently.
525105Swnj  */
535083Swnj struct	en_softc {
545105Swnj 	struct	ifnet es_if;		/* network-visible interface */
555105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
565105Swnj 	short	es_delay;		/* current output delay */
575105Swnj 	short	es_mask;		/* mask for current output delay */
585105Swnj 	u_char	es_lastx;		/* host last transmitted to */
595105Swnj 	short	es_oactive;		/* is output active? */
605105Swnj 	short	es_olen;		/* length of last output */
615083Swnj } en_softc[NEN];
624686Swnj 
635105Swnj /*
645105Swnj  * Do output DMA to determine interface presence and
655105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
665105Swnj  */
674686Swnj enprobe(reg)
684686Swnj 	caddr_t reg;
694686Swnj {
705171Swnj 	register int br, cvec;		/* r11, r10 value-result */
714686Swnj 	register struct endevice *addr = (struct endevice *)reg;
724686Swnj 
735083Swnj COUNT(ENPROBE);
744686Swnj #ifdef lint
754686Swnj 	br = 0; cvec = br; br = cvec;
764922Swnj 	enrint(0); enxint(0); encollide(0);
774686Swnj #endif
784686Swnj 	addr->en_istat = 0;
794686Swnj 	addr->en_owc = -1;
804686Swnj 	addr->en_oba = 0;
814771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
824686Swnj 	DELAY(100000);
834686Swnj 	addr->en_ostat = 0;
844686Swnj 	return (1);
854686Swnj }
864686Swnj 
875105Swnj /*
885105Swnj  * Interface exists: make available by filling in network interface
895105Swnj  * record.  System will initialize the interface when it is ready
905105Swnj  * to accept packets.
915105Swnj  */
924686Swnj enattach(ui)
934686Swnj 	struct uba_device *ui;
944686Swnj {
955105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
965105Swnj COUNT(ENATTACH);
974688Swnj 
985105Swnj 	es->es_if.if_unit = ui->ui_unit;
995171Swnj 	es->es_if.if_name = "en";
1005105Swnj 	es->es_if.if_mtu = ENMTU;
1015105Swnj 	es->es_if.if_net = ui->ui_flags;
1025105Swnj 	es->es_if.if_host[0] =
103*5213Swnj 	    (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff;
1045105Swnj 	es->es_if.if_addr =
1055105Swnj 	    if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
1065171Swnj 	es->es_if.if_init = eninit;
1075105Swnj 	es->es_if.if_output = enoutput;
1085105Swnj 	es->es_if.if_ubareset = enreset;
1095160Swnj 	if_attach(&es->es_if);
1104686Swnj }
1114686Swnj 
1125105Swnj /*
1135105Swnj  * Reset of interface after UNIBUS reset.
1145105Swnj  * If interface is on specified uba, reset its state.
1155105Swnj  */
1165105Swnj enreset(unit, uban)
1175105Swnj 	int unit, uban;
1185105Swnj {
1195105Swnj 	register struct uba_device *ui;
1205105Swnj COUNT(ENRESET);
1215105Swnj 
1225171Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
1235171Swnj 	    ui->ui_ubanum != uban)
1245105Swnj 		return;
1255171Swnj 	printf(" en%d", unit);
1265105Swnj 	eninit(unit);
1275105Swnj }
1285105Swnj 
1295105Swnj /*
1305105Swnj  * Initialization of interface; clear recorded pending
1315105Swnj  * operations, and reinitialize UNIBUS usage.
1325105Swnj  */
1334688Swnj eninit(unit)
1344686Swnj 	int unit;
1355083Swnj {
1365171Swnj 	register struct en_softc *es = &en_softc[unit];
1375171Swnj 	register struct uba_device *ui = eninfo[unit];
1384686Swnj 	register struct endevice *addr;
1395105Swnj 	int s;
1404686Swnj 
1415105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1425105Swnj 	    sizeof (struct en_header), (int)btop(ENMTU)) == 0) {
1435171Swnj 		printf("en%d: can't initialize\n", unit);
1445083Swnj 		return;
1454686Swnj 	}
1464686Swnj 	addr = (struct endevice *)ui->ui_addr;
1475083Swnj 	addr->en_istat = addr->en_ostat = 0;
1484686Swnj 
1495105Swnj 	/*
1505171Swnj 	 * Hang a receive and start any
1515171Swnj 	 * pending writes by faking a transmit complete.
1525105Swnj 	 */
1535105Swnj 	s = splimp();
1545171Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
1555171Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
1565171Swnj 	addr->en_istat = EN_IEN|EN_GO;
1575171Swnj 	es->es_oactive = 1;
1585105Swnj 	enxint(unit);
1595105Swnj 	splx(s);
1604686Swnj }
1614686Swnj 
1624717Swnj int	enlastdel = 25;
1635083Swnj 
1645105Swnj /*
1655105Swnj  * Start or restart output on interface.
1665105Swnj  * If interface is already active, then this is a retransmit
1675105Swnj  * after a collision, and just restuff registers and delay.
1685105Swnj  * If interface is not already active, get another datagram
1695105Swnj  * to send off of the interface queue, and map it to the interface
1705105Swnj  * before starting the output.
1715105Swnj  */
1724688Swnj enstart(dev)
1734686Swnj 	dev_t dev;
1744686Swnj {
1755171Swnj         int unit = ENUNIT(dev);
1765171Swnj 	struct uba_device *ui = eninfo[unit];
1775171Swnj 	register struct en_softc *es = &en_softc[unit];
1785083Swnj 	register struct endevice *addr;
1795083Swnj 	struct mbuf *m;
1805083Swnj 	int dest;
1814688Swnj COUNT(ENSTART);
1824686Swnj 
1835083Swnj 	if (es->es_oactive)
1845083Swnj 		goto restart;
1855105Swnj 
1865105Swnj 	/*
1875105Swnj 	 * Not already active: dequeue another request
1885105Swnj 	 * and map it to the UNIBUS.  If no more requests,
1895105Swnj 	 * just return.
1905105Swnj 	 */
1915105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
1925083Swnj 	if (m == 0) {
1935083Swnj 		es->es_oactive = 0;
1944686Swnj 		return;
1954686Swnj 	}
196*5213Swnj 	dest = mtod(m, struct en_header *)->en_dhost;
197*5213Swnj printf("if_wubaput m=%x\n", m);
1985105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
199*5213Swnj printf("wubaput to %x len %d\n", es->es_ifuba.ifu_w.ifrw_addr, es->es_olen);
200*5213Swnj asm("halt");
2015105Swnj 
2025105Swnj 	/*
2035105Swnj 	 * Ethernet cannot take back-to-back packets (no
2045105Swnj 	 * buffering in interface.  To avoid overrunning
2055105Swnj 	 * receiver, enforce a small delay (about 1ms) in interface
2065105Swnj 	 * on successive packets sent to same host.
2075105Swnj 	 */
2085083Swnj 	if (es->es_lastx && es->es_lastx == dest)
2095083Swnj 		es->es_delay = enlastdel;
2105083Swnj 	else
2115083Swnj 		es->es_lastx = dest;
2125105Swnj 
2135083Swnj restart:
2145105Swnj 	/*
2155105Swnj 	 * Have request mapped to UNIBUS for transmission.
2165105Swnj 	 * Purge any stale data from this BDP, and start the otput.
2175105Swnj 	 */
2185105Swnj 	UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2194686Swnj 	addr = (struct endevice *)ui->ui_addr;
2205160Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
2215083Swnj 	addr->en_odelay = es->es_delay;
2225083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2234771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2245083Swnj 	es->es_oactive = 1;
2254686Swnj }
2264686Swnj 
2275105Swnj /*
2285105Swnj  * Ethernet interface transmitter interrupt.
2295105Swnj  * Start another output if more data to send.
2305105Swnj  */
2314686Swnj enxint(unit)
2324686Swnj 	int unit;
2334686Swnj {
2345171Swnj 	register struct uba_device *ui = eninfo[unit];
2355171Swnj 	register struct en_softc *es = &en_softc[unit];
2364686Swnj 	register struct endevice *addr;
2374686Swnj COUNT(ENXINT);
2384686Swnj 
239*5213Swnj printf("enxint\n");
2405083Swnj 	if (es->es_oactive == 0)
2415083Swnj 		return;
2424686Swnj 	addr = (struct endevice *)ui->ui_addr;
2435171Swnj 	es->es_if.if_opackets++;
2445083Swnj 	es->es_oactive = 0;
2455083Swnj 	es->es_delay = 0;
2465083Swnj 	es->es_mask = ~0;
2475083Swnj 	if (addr->en_ostat&EN_OERROR)
248*5213Swnj 		printf("en%d: output error\n", unit);
2495105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
2505171Swnj 		es->es_if.if_oerrors++;
2515171Swnj 		if (es->es_ifuba.ifu_xtofree) {
2525171Swnj 			m_freem(es->es_ifuba.ifu_xtofree);
2535171Swnj 			es->es_ifuba.ifu_xtofree = 0;
2545171Swnj 		}
2555083Swnj 		es->es_lastx = 0;
2564686Swnj 		return;
2574686Swnj 	}
2585083Swnj 	enstart(unit);
2594686Swnj }
2604686Swnj 
2615105Swnj /*
2625105Swnj  * Collision on ethernet interface.  Do exponential
2635105Swnj  * backoff, and retransmit.  If have backed off all
2645105Swnj  * the way printing warning diagnostic, and drop packet.
2655105Swnj  */
2664686Swnj encollide(unit)
2674686Swnj 	int unit;
2684686Swnj {
2695171Swnj 	register struct en_softc *es = &en_softc[unit];
2704686Swnj COUNT(ENCOLLIDE);
2714686Swnj 
272*5213Swnj printf("encollide\n");
2735105Swnj 	es->es_if.if_collisions++;
2745083Swnj 	if (es->es_oactive == 0)
2754686Swnj 		return;
2765171Swnj 	/*
2775171Swnj 	 * Es_mask is a 16 bit number with n low zero bits, with
2785171Swnj 	 * n the number of backoffs.  When es_mask is 0 we have
2795171Swnj 	 * backed off 16 times, and give up.
2805171Swnj 	 */
2815083Swnj 	if (es->es_mask == 0) {
282*5213Swnj 		printf("en%d: send error\n", unit);
2835083Swnj 		enxint(unit);
2845171Swnj 		return;
2854686Swnj 	}
2865171Swnj 	/*
2875171Swnj 	 * Another backoff.  Restart with delay based on n low bits
2885171Swnj 	 * of the interval timer.
2895171Swnj 	 */
2905171Swnj 	es->es_mask <<= 1;
2915171Swnj 	es->es_delay = mfpr(ICR) &~ es->es_mask;
2925171Swnj 	enstart(unit);
2934686Swnj }
2944686Swnj 
2955105Swnj /*
2965105Swnj  * Ethernet interface receiver interrupt.
2975105Swnj  * If input error just drop packet.
2985105Swnj  * Otherwise purge input buffered data path and examine
2995105Swnj  * packet to determine type.  If can't determine length
3005105Swnj  * from type, then have to drop packet.  Othewise decapsulate
3015105Swnj  * packet based on type and pass to type specific higher-level
3025105Swnj  * input routine.
3035105Swnj  */
3044686Swnj enrint(unit)
3054686Swnj 	int unit;
3064686Swnj {
3075171Swnj 	register struct en_softc *es = &en_softc[unit];
3085171Swnj 	struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
3095171Swnj 	register struct en_header *en;
3105083Swnj     	struct mbuf *m;
3115171Swnj 	int len;
3125171Swnj 	register struct ifqueue *inq;
3135083Swnj 	int off;
3144686Swnj COUNT(ENRINT);
3154686Swnj 
316*5213Swnj printf("enrint\n");
3175171Swnj 	es->es_if.if_ipackets++;
3185105Swnj 
3195105Swnj 	/*
3205171Swnj 	 * Purge BDP; drop if input error indicated.
3215105Swnj 	 */
3225105Swnj 	UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
3234771Swnj 	if (addr->en_istat&EN_IERROR) {
3245105Swnj 		es->es_if.if_ierrors++;
325*5213Swnj 		printf("en%d: input error\n", unit);
3265083Swnj 		goto setup;
3274686Swnj 	}
3285105Swnj 
3295105Swnj 	/*
3305105Swnj 	 * Get pointer to ethernet header (in input buffer).
3315105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
3325105Swnj 	 * get true type from first 16-bit word past data.
3335105Swnj 	 * Remember that type was trailer by setting off.
3345105Swnj 	 */
3355105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
336*5213Swnj printf("en %x, en->en_type %d\n", en, en->en_type);
3375083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
3385083Swnj 	if (en->en_type >= ENPUP_TRAIL &&
3395083Swnj 	    en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
3405083Swnj 		off = (en->en_type - ENPUP_TRAIL) * 512;
3415171Swnj 		if (off >= ENMTU)
3425171Swnj 			goto setup;		/* sanity */
3435083Swnj 		en->en_type = *endataaddr(en, off, u_short *);
3445083Swnj 	} else
3455083Swnj 		off = 0;
3465105Swnj 
347*5213Swnj printf("off %d\n", off);
3485105Swnj 	/*
3495105Swnj 	 * Attempt to infer packet length from type;
3505105Swnj 	 * can't deal with packet if can't infer length.
3515105Swnj 	 */
3525083Swnj 	switch (en->en_type) {
3535083Swnj 
3545083Swnj #ifdef INET
3555083Swnj 	case ENPUP_IPTYPE:
356*5213Swnj 		len = htons(endataaddr(en, off+2, struct ip *)->ip_len)+2;
3575083Swnj 		setipintr();
3585083Swnj 		inq = &ipintrq;
3594686Swnj 		break;
3604686Swnj #endif
3614686Swnj 
3625083Swnj 	default:
3635083Swnj 		printf("en%d: unknow pkt type 0x%x\n", en->en_type);
3645083Swnj 		goto setup;
3654686Swnj 	}
3665083Swnj 	if (len == 0)
3675083Swnj 		goto setup;
3685105Swnj 
3695105Swnj 	/*
3705105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
3715105Swnj 	 * has trailing header; if_rubaget will then force this header
3725105Swnj 	 * information to be at the front, but we still have to drop
3735171Swnj 	 * the two-byte type which is at the front of any trailer data.
3745105Swnj 	 */
3755105Swnj 	m = if_rubaget(&es->es_ifuba, len, off);
376*5213Swnj 	if (m == 0)
377*5213Swnj 		goto setup;
378*5213Swnj 	printf("rubaget returns m %x\n", m);
379*5213Swnj 	asm("halt");
3805105Swnj 	if (off) {
3815105Swnj 		m->m_off += 2;
3825105Swnj 		m->m_len -= 2;
3835105Swnj 	}
3845083Swnj 	IF_ENQUEUE(inq, m);
3855105Swnj 
3864688Swnj setup:
3875105Swnj 	/*
3885105Swnj 	 * Reset for next packet.
3895105Swnj 	 */
3905105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
3915083Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
3924771Swnj 	addr->en_istat = EN_IEN|EN_GO;
3934688Swnj }
3944686Swnj 
3955083Swnj /*
3965083Swnj  * Ethernet output routine.
3975083Swnj  * Encapsulate a packet of type family for the local net.
3985105Swnj  * Use trailer local net encapsulation if enough data in first
3995105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
4005083Swnj  */
4015083Swnj enoutput(ifp, m0, pf)
4025083Swnj 	struct ifnet *ifp;
4035083Swnj 	struct mbuf *m0;
4045083Swnj 	int pf;
4054686Swnj {
4065083Swnj 	int type, dest;
4075105Swnj 	register struct mbuf *m = m0;
4085083Swnj 	register struct en_header *en;
4095083Swnj 	int s;
4104686Swnj 
4115083Swnj 	switch (pf) {
4125083Swnj 
4135083Swnj #ifdef INET
4145083Swnj 	case PF_INET: {
4155083Swnj 		register struct ip *ip = mtod(m0, struct ip *);
4165083Swnj 		int off;
4175083Swnj 
4185105Swnj 		dest = ip->ip_dst.s_addr >> 24;
419*5213Swnj 		off = ntohs(ip->ip_len) - m->m_len;
420*5213Swnj printf("PF_INET enoutput off %d m->m_off %d m->m_len %d\n", off, m->m_off, m->m_len);
4215171Swnj 		if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) {
4225083Swnj 			type = ENPUP_TRAIL + (off>>9);
4235105Swnj 			m->m_off -= 2;
4245105Swnj 			m->m_len += 2;
4255105Swnj 			*mtod(m, u_short *) = ENPUP_IPTYPE;
4265105Swnj 			goto gottrailertype;
4275083Swnj 		}
4285105Swnj 		type = ENPUP_IPTYPE;
4295105Swnj 		off = 0;
4305105Swnj 		goto gottype;
4315083Swnj 		}
4325083Swnj #endif
4335083Swnj 
4345083Swnj 	default:
4355083Swnj 		printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
4365083Swnj 		m_freem(m0);
4375083Swnj 		return (0);
4384686Swnj 	}
4395105Swnj 
4405171Swnj gottrailertype:
4415105Swnj 	/*
4425105Swnj 	 * Packet to be sent as trailer: move first packet
4435105Swnj 	 * (control information) to end of chain.
4445105Swnj 	 */
4455105Swnj 	while (m->m_next)
4465105Swnj 		m = m->m_next;
4475105Swnj 	m->m_next = m0;
4485105Swnj 	m = m0->m_next;
4495105Swnj 	m0->m_next = 0;
4505171Swnj 	m0 = m;
4515105Swnj 
452*5213Swnj printf("m %x after trailer futz\n", m);
453*5213Swnj asm("halt");
4545171Swnj gottype:
4555105Swnj 	/*
4565105Swnj 	 * Add local net header.  If no space in first mbuf,
4575105Swnj 	 * allocate another.
4585105Swnj 	 */
459*5213Swnj 	if (m->m_off > MMAXOFF ||
460*5213Swnj 	    MMINOFF + sizeof (struct en_header) > m->m_off) {
4615083Swnj 		m = m_get(0);
4625083Swnj 		if (m == 0) {
4635083Swnj 			m_freem(m0);
4645083Swnj 			return (0);
4655083Swnj 		}
4665083Swnj 		m->m_next = m0;
4675083Swnj 		m->m_off = MMINOFF;
4685083Swnj 		m->m_len = sizeof (struct en_header);
4695083Swnj 	} else {
4705083Swnj 		m->m_off -= sizeof (struct en_header);
4715083Swnj 		m->m_len += sizeof (struct en_header);
4725083Swnj 	}
4735083Swnj 	en = mtod(m, struct en_header *);
4745083Swnj 	en->en_shost = ifp->if_host[0];
4755083Swnj 	en->en_dhost = dest;
4765083Swnj 	en->en_type = type;
4775105Swnj 
4785105Swnj 	/*
4795105Swnj 	 * Queue message on interface, and start output if interface
4805105Swnj 	 * not yet active.
4815105Swnj 	 */
4825083Swnj 	s = splimp();
483*5213Swnj printf("queueing %x\n", m);
484*5213Swnj asm("halt");
4855083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
4865083Swnj 	splx(s);
4875083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
4885083Swnj 		enstart(ifp->if_unit);
4895105Swnj 	return (1);
4904686Swnj }
491