xref: /csrg-svn/sys/vax/if/if_en.c (revision 5105)
1*5105Swnj /*	if_en.c	4.15	81/11/29	*/
24686Swnj 
34686Swnj #include "en.h"
4*5105Swnj 
54686Swnj /*
6*5105Swnj  * 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 
39*5105Swnj int	eninit(),enoutput(),enreset();
40*5105Swnj 
41*5105Swnj /*
42*5105Swnj  * Ethernet software status per interface.
43*5105Swnj  *
44*5105Swnj  * Each interface is referenced by a network interface structure,
45*5105Swnj  * es_if, which the routing code uses to locate the interface.
46*5105Swnj  * This structure contains the output queue for the interface, its address, ...
47*5105Swnj  * We also have, for each interface, a UBA interface structure, which
48*5105Swnj  * contains information about the UNIBUS resources held by the interface:
49*5105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
50*5105Swnj  * structure for use by the if_uba.c routines in running the interface
51*5105Swnj  * efficiently.
52*5105Swnj  */
535083Swnj struct	en_softc {
54*5105Swnj 	struct	ifnet es_if;		/* network-visible interface */
55*5105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
56*5105Swnj 	short	es_delay;		/* current output delay */
57*5105Swnj 	short	es_mask;		/* mask for current output delay */
58*5105Swnj 	u_char	es_lastx;		/* host last transmitted to */
59*5105Swnj 	short	es_oactive;		/* is output active? */
60*5105Swnj 	short	es_olen;		/* length of last output */
615083Swnj } en_softc[NEN];
624686Swnj 
63*5105Swnj /*
64*5105Swnj  * Do output DMA to determine interface presence and
65*5105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
66*5105Swnj  */
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 
87*5105Swnj /*
88*5105Swnj  * Interface exists: make available by filling in network interface
89*5105Swnj  * record.  System will initialize the interface when it is ready
90*5105Swnj  * to accept packets.
91*5105Swnj  */
924686Swnj enattach(ui)
934686Swnj 	struct uba_device *ui;
944686Swnj {
95*5105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
96*5105Swnj COUNT(ENATTACH);
974688Swnj 
98*5105Swnj 	es->es_if.if_unit = ui->ui_unit;
99*5105Swnj 	es->es_if.if_mtu = ENMTU;
100*5105Swnj 	es->es_if.if_net = ui->ui_flags;
101*5105Swnj 	es->es_if.if_host[0] =
102*5105Swnj 	    ~(((struct endevice *)eninfo[ui->ui_unit])->en_addr) & 0xff;
103*5105Swnj 	es->es_if.if_addr =
104*5105Swnj 	    if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
105*5105Swnj 	es->es_if.if_output = enoutput;
106*5105Swnj 	es->es_if.if_init = eninit;
107*5105Swnj 	es->es_if.if_ubareset = enreset;
1084686Swnj }
1094686Swnj 
110*5105Swnj /*
111*5105Swnj  * Reset of interface after UNIBUS reset.
112*5105Swnj  * If interface is on specified uba, reset its state.
113*5105Swnj  */
114*5105Swnj enreset(unit, uban)
115*5105Swnj 	int unit, uban;
116*5105Swnj {
117*5105Swnj 	register struct uba_device *ui;
118*5105Swnj COUNT(ENRESET);
119*5105Swnj 
120*5105Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0) {
121*5105Swnj 		printf("es%d: not alive\n", unit);
122*5105Swnj 		return;
123*5105Swnj 	}
124*5105Swnj 	if (ui->ui_ubanum != uban)
125*5105Swnj 		return;
126*5105Swnj 	eninit(unit);
127*5105Swnj }
128*5105Swnj 
129*5105Swnj /*
130*5105Swnj  * Initialization of interface; clear recorded pending
131*5105Swnj  * operations, and reinitialize UNIBUS usage.
132*5105Swnj  */
1334688Swnj eninit(unit)
1344686Swnj 	int unit;
1355083Swnj {
1365083Swnj 	register struct uba_device *ui;
1374686Swnj 	register struct endevice *addr;
1385083Swnj 	register struct en_softc *es;
139*5105Swnj 	int s;
1404686Swnj 
1415083Swnj 	es = &en_softc[unit];
142*5105Swnj 	ui = eninfo[unit];
143*5105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
144*5105Swnj 	    sizeof (struct en_header), (int)btop(ENMTU)) == 0) {
1455083Swnj 		printf("es%d: can't initialize\n", unit);
1465083Swnj 		return;
1474686Swnj 	}
1484686Swnj 	addr = (struct endevice *)ui->ui_addr;
1495083Swnj 	addr->en_istat = addr->en_ostat = 0;
1504686Swnj 
151*5105Swnj 	/*
152*5105Swnj 	 * Hang pending read, start any writes.
153*5105Swnj 	 */
154*5105Swnj 	s = splimp();
155*5105Swnj 	enstart(unit);
156*5105Swnj 	enxint(unit);
157*5105Swnj 	splx(s);
1584686Swnj }
1594686Swnj 
1604717Swnj int	enlastdel = 25;
1615083Swnj 
162*5105Swnj /*
163*5105Swnj  * Start or restart output on interface.
164*5105Swnj  * If interface is already active, then this is a retransmit
165*5105Swnj  * after a collision, and just restuff registers and delay.
166*5105Swnj  * If interface is not already active, get another datagram
167*5105Swnj  * to send off of the interface queue, and map it to the interface
168*5105Swnj  * before starting the output.
169*5105Swnj  */
1704688Swnj enstart(dev)
1714686Swnj 	dev_t dev;
1724686Swnj {
1734686Swnj         int unit;
1744686Swnj 	struct uba_device *ui;
1755083Swnj 	register struct endevice *addr;
1765083Swnj 	register struct en_softc *es;
1775083Swnj 	struct mbuf *m;
1785083Swnj 	int dest;
1794688Swnj COUNT(ENSTART);
1804686Swnj 
1814686Swnj 	unit = ENUNIT(dev);
1824686Swnj 	ui = eninfo[unit];
1835083Swnj 	es = &en_softc[unit];
1845083Swnj 	if (es->es_oactive)
1855083Swnj 		goto restart;
186*5105Swnj 
187*5105Swnj 	/*
188*5105Swnj 	 * Not already active: dequeue another request
189*5105Swnj 	 * and map it to the UNIBUS.  If no more requests,
190*5105Swnj 	 * just return.
191*5105Swnj 	 */
192*5105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
1935083Swnj 	if (m == 0) {
1945083Swnj 		es->es_oactive = 0;
1954686Swnj 		return;
1964686Swnj 	}
197*5105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
198*5105Swnj 
199*5105Swnj 	/*
200*5105Swnj 	 * Ethernet cannot take back-to-back packets (no
201*5105Swnj 	 * buffering in interface.  To avoid overrunning
202*5105Swnj 	 * receiver, enforce a small delay (about 1ms) in interface
203*5105Swnj 	 * on successive packets sent to same host.
204*5105Swnj 	 */
2055083Swnj 	dest = mtod(m, struct en_header *)->en_dhost;
2065083Swnj 	if (es->es_lastx && es->es_lastx == dest)
2075083Swnj 		es->es_delay = enlastdel;
2085083Swnj 	else
2095083Swnj 		es->es_lastx = dest;
210*5105Swnj 
2115083Swnj restart:
212*5105Swnj 	/*
213*5105Swnj 	 * Have request mapped to UNIBUS for transmission.
214*5105Swnj 	 * Purge any stale data from this BDP, and start the otput.
215*5105Swnj 	 */
216*5105Swnj 	UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2174686Swnj 	addr = (struct endevice *)ui->ui_addr;
218*5105Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_addr;
2195083Swnj 	addr->en_odelay = es->es_delay;
2205083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2214771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2225083Swnj 	es->es_oactive = 1;
2234686Swnj }
2244686Swnj 
225*5105Swnj /*
226*5105Swnj  * Ethernet interface transmitter interrupt.
227*5105Swnj  * Start another output if more data to send.
228*5105Swnj  */
2294686Swnj enxint(unit)
2304686Swnj 	int unit;
2314686Swnj {
2324686Swnj 	register struct endevice *addr;
2334686Swnj 	register struct uba_device *ui;
2345083Swnj 	register struct en_softc *es;
2354686Swnj COUNT(ENXINT);
2364686Swnj 
2374686Swnj 	ui = eninfo[unit];
2385083Swnj 	es = &en_softc[unit];
2395083Swnj 	if (es->es_oactive == 0)
2405083Swnj 		return;
2414686Swnj 	addr = (struct endevice *)ui->ui_addr;
2425083Swnj 	es = &en_softc[unit];
2435083Swnj 	es->es_oactive = 0;
2445083Swnj 	es->es_delay = 0;
2455083Swnj 	es->es_mask = ~0;
2465083Swnj 	if (addr->en_ostat&EN_OERROR)
2475083Swnj 		printf("es%d: output error\n", unit);
248*5105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
2495083Swnj 		es->es_lastx = 0;
2504686Swnj 		return;
2514686Swnj 	}
2525083Swnj 	enstart(unit);
2534686Swnj }
2544686Swnj 
255*5105Swnj /*
256*5105Swnj  * Collision on ethernet interface.  Do exponential
257*5105Swnj  * backoff, and retransmit.  If have backed off all
258*5105Swnj  * the way printing warning diagnostic, and drop packet.
259*5105Swnj  */
2604686Swnj encollide(unit)
2614686Swnj 	int unit;
2624686Swnj {
2635083Swnj 	register struct en_softc *es;
2644686Swnj COUNT(ENCOLLIDE);
2654686Swnj 
2665083Swnj 	es = &en_softc[unit];
267*5105Swnj 	es->es_if.if_collisions++;
2685083Swnj 	if (es->es_oactive == 0)
2694686Swnj 		return;
2705083Swnj 	if (es->es_mask == 0) {
2715083Swnj 		printf("es%d: send error\n", unit);
2725083Swnj 		enxint(unit);
2734686Swnj 	} else {
2745083Swnj 		es->es_mask <<= 1;
2755083Swnj 		es->es_delay = mfpr(ICR) &~ es->es_mask;
2765083Swnj 		enstart(unit);
2774686Swnj 	}
2784686Swnj }
2794686Swnj 
280*5105Swnj /*
281*5105Swnj  * Ethernet interface receiver interrupt.
282*5105Swnj  * If input error just drop packet.
283*5105Swnj  * Otherwise purge input buffered data path and examine
284*5105Swnj  * packet to determine type.  If can't determine length
285*5105Swnj  * from type, then have to drop packet.  Othewise decapsulate
286*5105Swnj  * packet based on type and pass to type specific higher-level
287*5105Swnj  * input routine.
288*5105Swnj  */
2894686Swnj enrint(unit)
2904686Swnj 	int unit;
2914686Swnj {
2925083Swnj 	struct endevice *addr;
2935083Swnj 	register struct en_softc *es;
2945083Swnj 	struct en_header *en;
2955083Swnj     	struct mbuf *m;
2965083Swnj 	struct ifqueue *inq;
2974686Swnj 	register int len;
2985083Swnj 	int off;
2994686Swnj COUNT(ENRINT);
3004686Swnj 
301*5105Swnj 	es = &en_softc[unit];
3025083Swnj 	addr = (struct endevice *)eninfo[unit]->ui_addr;
303*5105Swnj 
304*5105Swnj 	/*
305*5105Swnj 	 * Purge BDP; drop error packets.
306*5105Swnj 	 */
307*5105Swnj 	UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
3084771Swnj 	if (addr->en_istat&EN_IERROR) {
309*5105Swnj 		es->es_if.if_ierrors++;
3105083Swnj 		printf("es%d: input error\n", unit);
3115083Swnj 		goto setup;
3124686Swnj 	}
313*5105Swnj 
314*5105Swnj 	/*
315*5105Swnj 	 * Get pointer to ethernet header (in input buffer).
316*5105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
317*5105Swnj 	 * get true type from first 16-bit word past data.
318*5105Swnj 	 * Remember that type was trailer by setting off.
319*5105Swnj 	 */
320*5105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
3215083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
3225083Swnj 	if (en->en_type >= ENPUP_TRAIL &&
3235083Swnj 	    en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
3245083Swnj 		off = (en->en_type - ENPUP_TRAIL) * 512;
3255083Swnj 		en->en_type = *endataaddr(en, off, u_short *);
3265083Swnj 		off += 2;
3275083Swnj 	} else
3285083Swnj 		off = 0;
329*5105Swnj 
330*5105Swnj 	/*
331*5105Swnj 	 * Attempt to infer packet length from type;
332*5105Swnj 	 * can't deal with packet if can't infer length.
333*5105Swnj 	 */
3345083Swnj 	switch (en->en_type) {
3355083Swnj 
3365083Swnj #ifdef INET
3375083Swnj 	case ENPUP_IPTYPE:
3385083Swnj 		len = endataaddr(en, off, struct ip *)->ip_len;
3395083Swnj 		setipintr();
3405083Swnj 		inq = &ipintrq;
3414686Swnj 		break;
3424686Swnj #endif
3434686Swnj 
3445083Swnj 	default:
3455083Swnj 		printf("en%d: unknow pkt type 0x%x\n", en->en_type);
3465083Swnj 		goto setup;
3474686Swnj 	}
3485083Swnj 	if (len == 0)
3495083Swnj 		goto setup;
350*5105Swnj 
351*5105Swnj 	/*
352*5105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
353*5105Swnj 	 * has trailing header; if_rubaget will then force this header
354*5105Swnj 	 * information to be at the front, but we still have to drop
355*5105Swnj 	 * the two-byte type which is at the front of the trailer data
356*5105Swnj 	 * (which we ``consumed'' above).
357*5105Swnj 	 */
358*5105Swnj 	m = if_rubaget(&es->es_ifuba, len, off);
359*5105Swnj 	if (off) {
360*5105Swnj 		m->m_off += 2;
361*5105Swnj 		m->m_len -= 2;
362*5105Swnj 	}
3635083Swnj 	IF_ENQUEUE(inq, m);
364*5105Swnj 
3654688Swnj setup:
366*5105Swnj 	/*
367*5105Swnj 	 * Reset for next packet.
368*5105Swnj 	 */
369*5105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
3705083Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
3714771Swnj 	addr->en_istat = EN_IEN|EN_GO;
3724688Swnj }
3734686Swnj 
3745083Swnj /*
3755083Swnj  * Ethernet output routine.
3765083Swnj  * Encapsulate a packet of type family for the local net.
377*5105Swnj  * Use trailer local net encapsulation if enough data in first
378*5105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
3795083Swnj  */
3805083Swnj enoutput(ifp, m0, pf)
3815083Swnj 	struct ifnet *ifp;
3825083Swnj 	struct mbuf *m0;
3835083Swnj 	int pf;
3844686Swnj {
3855083Swnj 	int type, dest;
386*5105Swnj 	register struct mbuf *m = m0;
3875083Swnj 	register struct en_header *en;
3885083Swnj 	int s;
3894686Swnj 
3905083Swnj 	switch (pf) {
3915083Swnj 
3925083Swnj #ifdef INET
3935083Swnj 	case PF_INET: {
3945083Swnj 		register struct ip *ip = mtod(m0, struct ip *);
3955083Swnj 		int off;
3965083Swnj 
397*5105Swnj 		dest = ip->ip_dst.s_addr >> 24;
398*5105Swnj 		off = ip->ip_len - m->m_len;
399*5105Swnj 		if (off && off % 512 == 0 && m->m_off >= MMINOFF + 2) {
4005083Swnj 			type = ENPUP_TRAIL + (off>>9);
401*5105Swnj 			m->m_off -= 2;
402*5105Swnj 			m->m_len += 2;
403*5105Swnj 			*mtod(m, u_short *) = ENPUP_IPTYPE;
404*5105Swnj 			goto gottrailertype;
4055083Swnj 		}
406*5105Swnj 		type = ENPUP_IPTYPE;
407*5105Swnj 		off = 0;
408*5105Swnj 		goto gottype;
4095083Swnj 		}
4105083Swnj #endif
4115083Swnj 
4125083Swnj 	default:
4135083Swnj 		printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
4145083Swnj 		m_freem(m0);
4155083Swnj 		return (0);
4164686Swnj 	}
417*5105Swnj 
418*5105Swnj 	/*
419*5105Swnj 	 * Packet to be sent as trailer: move first packet
420*5105Swnj 	 * (control information) to end of chain.
421*5105Swnj 	 */
422*5105Swnj gottrailertype:
423*5105Swnj 	while (m->m_next)
424*5105Swnj 		m = m->m_next;
425*5105Swnj 	m->m_next = m0;
426*5105Swnj 	m = m0->m_next;
427*5105Swnj 	m0->m_next = 0;
428*5105Swnj 
429*5105Swnj 	/*
430*5105Swnj 	 * Add local net header.  If no space in first mbuf,
431*5105Swnj 	 * allocate another.
432*5105Swnj 	 */
433*5105Swnj gottype:
434*5105Swnj 	m0 = m;
435*5105Swnj 	if (MMINOFF + sizeof (struct en_header) > m->m_off) {
4365083Swnj 		m = m_get(0);
4375083Swnj 		if (m == 0) {
4385083Swnj 			m_freem(m0);
4395083Swnj 			return (0);
4405083Swnj 		}
4415083Swnj 		m->m_next = m0;
4425083Swnj 		m->m_off = MMINOFF;
4435083Swnj 		m->m_len = sizeof (struct en_header);
4445083Swnj 	} else {
4455083Swnj 		m = m0;
4465083Swnj 		m->m_off -= sizeof (struct en_header);
4475083Swnj 		m->m_len += sizeof (struct en_header);
4485083Swnj 	}
4495083Swnj 	en = mtod(m, struct en_header *);
4505083Swnj 	en->en_shost = ifp->if_host[0];
4515083Swnj 	en->en_dhost = dest;
4525083Swnj 	en->en_type = type;
453*5105Swnj 
454*5105Swnj 	/*
455*5105Swnj 	 * Queue message on interface, and start output if interface
456*5105Swnj 	 * not yet active.
457*5105Swnj 	 */
4585083Swnj 	s = splimp();
4595083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
4605083Swnj 	splx(s);
4615083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
4625083Swnj 		enstart(ifp->if_unit);
463*5105Swnj 	return (1);
4644686Swnj }
465