xref: /csrg-svn/sys/vax/if/if_en.c (revision 5160)
1*5160Swnj /*	if_en.c	4.16	81/12/02	*/
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 
305083Swnj #define	ENMTU	1024
315083Swnj 
324686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
334686Swnj struct	uba_device *eninfo[NEN];
344686Swnj u_short enstd[] = { 0 };
354686Swnj struct	uba_driver endriver =
365083Swnj 	{ enprobe, 0, enattach, 0, enstd, "es", 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 {
704686Swnj 	register int br, cvec;
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;
995105Swnj 	es->es_if.if_mtu = ENMTU;
1005105Swnj 	es->es_if.if_net = ui->ui_flags;
1015105Swnj 	es->es_if.if_host[0] =
1025105Swnj 	    ~(((struct endevice *)eninfo[ui->ui_unit])->en_addr) & 0xff;
1035105Swnj 	es->es_if.if_addr =
1045105Swnj 	    if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
1055105Swnj 	es->es_if.if_output = enoutput;
1065105Swnj 	es->es_if.if_init = eninit;
1075105Swnj 	es->es_if.if_ubareset = enreset;
108*5160Swnj 	if_attach(&es->es_if);
1094686Swnj }
1104686Swnj 
1115105Swnj /*
1125105Swnj  * Reset of interface after UNIBUS reset.
1135105Swnj  * If interface is on specified uba, reset its state.
1145105Swnj  */
1155105Swnj enreset(unit, uban)
1165105Swnj 	int unit, uban;
1175105Swnj {
1185105Swnj 	register struct uba_device *ui;
1195105Swnj COUNT(ENRESET);
1205105Swnj 
1215105Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0) {
1225105Swnj 		printf("es%d: not alive\n", unit);
1235105Swnj 		return;
1245105Swnj 	}
1255105Swnj 	if (ui->ui_ubanum != uban)
1265105Swnj 		return;
1275105Swnj 	eninit(unit);
1285105Swnj }
1295105Swnj 
1305105Swnj /*
1315105Swnj  * Initialization of interface; clear recorded pending
1325105Swnj  * operations, and reinitialize UNIBUS usage.
1335105Swnj  */
1344688Swnj eninit(unit)
1354686Swnj 	int unit;
1365083Swnj {
1375083Swnj 	register struct uba_device *ui;
1384686Swnj 	register struct endevice *addr;
1395083Swnj 	register struct en_softc *es;
1405105Swnj 	int s;
1414686Swnj 
1425083Swnj 	es = &en_softc[unit];
1435105Swnj 	ui = eninfo[unit];
1445105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1455105Swnj 	    sizeof (struct en_header), (int)btop(ENMTU)) == 0) {
1465083Swnj 		printf("es%d: can't initialize\n", unit);
1475083Swnj 		return;
1484686Swnj 	}
1494686Swnj 	addr = (struct endevice *)ui->ui_addr;
1505083Swnj 	addr->en_istat = addr->en_ostat = 0;
1514686Swnj 
1525105Swnj 	/*
1535105Swnj 	 * Hang pending read, start any writes.
1545105Swnj 	 */
1555105Swnj 	s = splimp();
1565105Swnj 	enstart(unit);
1575105Swnj 	enxint(unit);
1585105Swnj 	splx(s);
1594686Swnj }
1604686Swnj 
1614717Swnj int	enlastdel = 25;
1625083Swnj 
1635105Swnj /*
1645105Swnj  * Start or restart output on interface.
1655105Swnj  * If interface is already active, then this is a retransmit
1665105Swnj  * after a collision, and just restuff registers and delay.
1675105Swnj  * If interface is not already active, get another datagram
1685105Swnj  * to send off of the interface queue, and map it to the interface
1695105Swnj  * before starting the output.
1705105Swnj  */
1714688Swnj enstart(dev)
1724686Swnj 	dev_t dev;
1734686Swnj {
1744686Swnj         int unit;
1754686Swnj 	struct uba_device *ui;
1765083Swnj 	register struct endevice *addr;
1775083Swnj 	register struct en_softc *es;
1785083Swnj 	struct mbuf *m;
1795083Swnj 	int dest;
1804688Swnj COUNT(ENSTART);
1814686Swnj 
1824686Swnj 	unit = ENUNIT(dev);
1834686Swnj 	ui = eninfo[unit];
1845083Swnj 	es = &en_softc[unit];
1855083Swnj 	if (es->es_oactive)
1865083Swnj 		goto restart;
1875105Swnj 
1885105Swnj 	/*
1895105Swnj 	 * Not already active: dequeue another request
1905105Swnj 	 * and map it to the UNIBUS.  If no more requests,
1915105Swnj 	 * just return.
1925105Swnj 	 */
1935105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
1945083Swnj 	if (m == 0) {
1955083Swnj 		es->es_oactive = 0;
1964686Swnj 		return;
1974686Swnj 	}
1985105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
1995105Swnj 
2005105Swnj 	/*
2015105Swnj 	 * Ethernet cannot take back-to-back packets (no
2025105Swnj 	 * buffering in interface.  To avoid overrunning
2035105Swnj 	 * receiver, enforce a small delay (about 1ms) in interface
2045105Swnj 	 * on successive packets sent to same host.
2055105Swnj 	 */
2065083Swnj 	dest = mtod(m, struct en_header *)->en_dhost;
2075083Swnj 	if (es->es_lastx && es->es_lastx == dest)
2085083Swnj 		es->es_delay = enlastdel;
2095083Swnj 	else
2105083Swnj 		es->es_lastx = dest;
2115105Swnj 
2125083Swnj restart:
2135105Swnj 	/*
2145105Swnj 	 * Have request mapped to UNIBUS for transmission.
2155105Swnj 	 * Purge any stale data from this BDP, and start the otput.
2165105Swnj 	 */
2175105Swnj 	UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2184686Swnj 	addr = (struct endevice *)ui->ui_addr;
219*5160Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
2205083Swnj 	addr->en_odelay = es->es_delay;
2215083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2224771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2235083Swnj 	es->es_oactive = 1;
2244686Swnj }
2254686Swnj 
2265105Swnj /*
2275105Swnj  * Ethernet interface transmitter interrupt.
2285105Swnj  * Start another output if more data to send.
2295105Swnj  */
2304686Swnj enxint(unit)
2314686Swnj 	int unit;
2324686Swnj {
2334686Swnj 	register struct endevice *addr;
2344686Swnj 	register struct uba_device *ui;
2355083Swnj 	register struct en_softc *es;
2364686Swnj COUNT(ENXINT);
2374686Swnj 
2384686Swnj 	ui = eninfo[unit];
2395083Swnj 	es = &en_softc[unit];
2405083Swnj 	if (es->es_oactive == 0)
2415083Swnj 		return;
2424686Swnj 	addr = (struct endevice *)ui->ui_addr;
2435083Swnj 	es = &en_softc[unit];
2445083Swnj 	es->es_oactive = 0;
2455083Swnj 	es->es_delay = 0;
2465083Swnj 	es->es_mask = ~0;
2475083Swnj 	if (addr->en_ostat&EN_OERROR)
2485083Swnj 		printf("es%d: output error\n", unit);
2495105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
2505083Swnj 		es->es_lastx = 0;
2514686Swnj 		return;
2524686Swnj 	}
2535083Swnj 	enstart(unit);
2544686Swnj }
2554686Swnj 
2565105Swnj /*
2575105Swnj  * Collision on ethernet interface.  Do exponential
2585105Swnj  * backoff, and retransmit.  If have backed off all
2595105Swnj  * the way printing warning diagnostic, and drop packet.
2605105Swnj  */
2614686Swnj encollide(unit)
2624686Swnj 	int unit;
2634686Swnj {
2645083Swnj 	register struct en_softc *es;
2654686Swnj COUNT(ENCOLLIDE);
2664686Swnj 
2675083Swnj 	es = &en_softc[unit];
2685105Swnj 	es->es_if.if_collisions++;
2695083Swnj 	if (es->es_oactive == 0)
2704686Swnj 		return;
2715083Swnj 	if (es->es_mask == 0) {
2725083Swnj 		printf("es%d: send error\n", unit);
2735083Swnj 		enxint(unit);
2744686Swnj 	} else {
2755083Swnj 		es->es_mask <<= 1;
2765083Swnj 		es->es_delay = mfpr(ICR) &~ es->es_mask;
2775083Swnj 		enstart(unit);
2784686Swnj 	}
2794686Swnj }
2804686Swnj 
2815105Swnj /*
2825105Swnj  * Ethernet interface receiver interrupt.
2835105Swnj  * If input error just drop packet.
2845105Swnj  * Otherwise purge input buffered data path and examine
2855105Swnj  * packet to determine type.  If can't determine length
2865105Swnj  * from type, then have to drop packet.  Othewise decapsulate
2875105Swnj  * packet based on type and pass to type specific higher-level
2885105Swnj  * input routine.
2895105Swnj  */
2904686Swnj enrint(unit)
2914686Swnj 	int unit;
2924686Swnj {
2935083Swnj 	struct endevice *addr;
2945083Swnj 	register struct en_softc *es;
2955083Swnj 	struct en_header *en;
2965083Swnj     	struct mbuf *m;
2975083Swnj 	struct ifqueue *inq;
2984686Swnj 	register int len;
2995083Swnj 	int off;
3004686Swnj COUNT(ENRINT);
3014686Swnj 
3025105Swnj 	es = &en_softc[unit];
3035083Swnj 	addr = (struct endevice *)eninfo[unit]->ui_addr;
3045105Swnj 
3055105Swnj 	/*
3065105Swnj 	 * Purge BDP; drop error packets.
3075105Swnj 	 */
3085105Swnj 	UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
3094771Swnj 	if (addr->en_istat&EN_IERROR) {
3105105Swnj 		es->es_if.if_ierrors++;
3115083Swnj 		printf("es%d: input error\n", unit);
3125083Swnj 		goto setup;
3134686Swnj 	}
3145105Swnj 
3155105Swnj 	/*
3165105Swnj 	 * Get pointer to ethernet header (in input buffer).
3175105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
3185105Swnj 	 * get true type from first 16-bit word past data.
3195105Swnj 	 * Remember that type was trailer by setting off.
3205105Swnj 	 */
3215105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
3225083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
3235083Swnj 	if (en->en_type >= ENPUP_TRAIL &&
3245083Swnj 	    en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
3255083Swnj 		off = (en->en_type - ENPUP_TRAIL) * 512;
3265083Swnj 		en->en_type = *endataaddr(en, off, u_short *);
3275083Swnj 		off += 2;
3285083Swnj 	} else
3295083Swnj 		off = 0;
3305105Swnj 
3315105Swnj 	/*
3325105Swnj 	 * Attempt to infer packet length from type;
3335105Swnj 	 * can't deal with packet if can't infer length.
3345105Swnj 	 */
3355083Swnj 	switch (en->en_type) {
3365083Swnj 
3375083Swnj #ifdef INET
3385083Swnj 	case ENPUP_IPTYPE:
3395083Swnj 		len = endataaddr(en, off, struct ip *)->ip_len;
3405083Swnj 		setipintr();
3415083Swnj 		inq = &ipintrq;
3424686Swnj 		break;
3434686Swnj #endif
3444686Swnj 
3455083Swnj 	default:
3465083Swnj 		printf("en%d: unknow pkt type 0x%x\n", en->en_type);
3475083Swnj 		goto setup;
3484686Swnj 	}
3495083Swnj 	if (len == 0)
3505083Swnj 		goto setup;
3515105Swnj 
3525105Swnj 	/*
3535105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
3545105Swnj 	 * has trailing header; if_rubaget will then force this header
3555105Swnj 	 * information to be at the front, but we still have to drop
3565105Swnj 	 * the two-byte type which is at the front of the trailer data
3575105Swnj 	 * (which we ``consumed'' above).
3585105Swnj 	 */
3595105Swnj 	m = if_rubaget(&es->es_ifuba, len, off);
3605105Swnj 	if (off) {
3615105Swnj 		m->m_off += 2;
3625105Swnj 		m->m_len -= 2;
3635105Swnj 	}
3645083Swnj 	IF_ENQUEUE(inq, m);
3655105Swnj 
3664688Swnj setup:
3675105Swnj 	/*
3685105Swnj 	 * Reset for next packet.
3695105Swnj 	 */
3705105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
3715083Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
3724771Swnj 	addr->en_istat = EN_IEN|EN_GO;
3734688Swnj }
3744686Swnj 
3755083Swnj /*
3765083Swnj  * Ethernet output routine.
3775083Swnj  * Encapsulate a packet of type family for the local net.
3785105Swnj  * Use trailer local net encapsulation if enough data in first
3795105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
3805083Swnj  */
3815083Swnj enoutput(ifp, m0, pf)
3825083Swnj 	struct ifnet *ifp;
3835083Swnj 	struct mbuf *m0;
3845083Swnj 	int pf;
3854686Swnj {
3865083Swnj 	int type, dest;
3875105Swnj 	register struct mbuf *m = m0;
3885083Swnj 	register struct en_header *en;
3895083Swnj 	int s;
3904686Swnj 
3915083Swnj 	switch (pf) {
3925083Swnj 
3935083Swnj #ifdef INET
3945083Swnj 	case PF_INET: {
3955083Swnj 		register struct ip *ip = mtod(m0, struct ip *);
3965083Swnj 		int off;
3975083Swnj 
3985105Swnj 		dest = ip->ip_dst.s_addr >> 24;
3995105Swnj 		off = ip->ip_len - m->m_len;
4005105Swnj 		if (off && off % 512 == 0 && m->m_off >= MMINOFF + 2) {
4015083Swnj 			type = ENPUP_TRAIL + (off>>9);
4025105Swnj 			m->m_off -= 2;
4035105Swnj 			m->m_len += 2;
4045105Swnj 			*mtod(m, u_short *) = ENPUP_IPTYPE;
4055105Swnj 			goto gottrailertype;
4065083Swnj 		}
4075105Swnj 		type = ENPUP_IPTYPE;
4085105Swnj 		off = 0;
4095105Swnj 		goto gottype;
4105083Swnj 		}
4115083Swnj #endif
4125083Swnj 
4135083Swnj 	default:
4145083Swnj 		printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
4155083Swnj 		m_freem(m0);
4165083Swnj 		return (0);
4174686Swnj 	}
4185105Swnj 
4195105Swnj 	/*
4205105Swnj 	 * Packet to be sent as trailer: move first packet
4215105Swnj 	 * (control information) to end of chain.
4225105Swnj 	 */
4235105Swnj gottrailertype:
4245105Swnj 	while (m->m_next)
4255105Swnj 		m = m->m_next;
4265105Swnj 	m->m_next = m0;
4275105Swnj 	m = m0->m_next;
4285105Swnj 	m0->m_next = 0;
4295105Swnj 
4305105Swnj 	/*
4315105Swnj 	 * Add local net header.  If no space in first mbuf,
4325105Swnj 	 * allocate another.
4335105Swnj 	 */
4345105Swnj gottype:
4355105Swnj 	m0 = m;
4365105Swnj 	if (MMINOFF + sizeof (struct en_header) > m->m_off) {
4375083Swnj 		m = m_get(0);
4385083Swnj 		if (m == 0) {
4395083Swnj 			m_freem(m0);
4405083Swnj 			return (0);
4415083Swnj 		}
4425083Swnj 		m->m_next = m0;
4435083Swnj 		m->m_off = MMINOFF;
4445083Swnj 		m->m_len = sizeof (struct en_header);
4455083Swnj 	} else {
4465083Swnj 		m = m0;
4475083Swnj 		m->m_off -= sizeof (struct en_header);
4485083Swnj 		m->m_len += sizeof (struct en_header);
4495083Swnj 	}
4505083Swnj 	en = mtod(m, struct en_header *);
4515083Swnj 	en->en_shost = ifp->if_host[0];
4525083Swnj 	en->en_dhost = dest;
4535083Swnj 	en->en_type = type;
4545105Swnj 
4555105Swnj 	/*
4565105Swnj 	 * Queue message on interface, and start output if interface
4575105Swnj 	 * not yet active.
4585105Swnj 	 */
4595083Swnj 	s = splimp();
4605083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
4615083Swnj 	splx(s);
4625083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
4635083Swnj 		enstart(ifp->if_unit);
4645105Swnj 	return (1);
4654686Swnj }
466