xref: /original-bsd/sys/vax/if/if_en.c (revision b3b53e97)
1 /*	if_en.c	4.50	82/04/04	*/
2 
3 #include "en.h"
4 #include "imp.h"
5 
6 /*
7  * Xerox prototype (3 Mb) Ethernet interface driver.
8  */
9 
10 #include "../h/param.h"
11 #include "../h/systm.h"
12 #include "../h/mbuf.h"
13 #include "../h/pte.h"
14 #include "../h/buf.h"
15 #include "../h/protosw.h"
16 #include "../h/socket.h"
17 #include "../h/ubareg.h"
18 #include "../h/ubavar.h"
19 #include "../h/enreg.h"
20 #include "../h/cpu.h"
21 #include "../h/mtpr.h"
22 #include "../h/vmmac.h"
23 #include "../net/in.h"
24 #include "../net/in_systm.h"
25 #include "../net/if.h"
26 #include "../net/if_en.h"
27 #include "../net/if_uba.h"
28 #include "../net/ip.h"
29 #include "../net/ip_var.h"
30 #include "../net/pup.h"
31 #include "../net/route.h"
32 
33 #define	ENMTU	(1024+512)
34 
35 int	enprobe(), enattach(), enrint(), enxint(), encollide();
36 struct	uba_device *eninfo[NEN];
37 u_short enstd[] = { 0 };
38 struct	uba_driver endriver =
39 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
40 #define	ENUNIT(x)	minor(x)
41 
42 int	eninit(),enoutput(),enreset();
43 
44 /*
45  * Ethernet software status per interface.
46  *
47  * Each interface is referenced by a network interface structure,
48  * es_if, which the routing code uses to locate the interface.
49  * This structure contains the output queue for the interface, its address, ...
50  * We also have, for each interface, a UBA interface structure, which
51  * contains information about the UNIBUS resources held by the interface:
52  * map registers, buffered data paths, etc.  Information is cached in this
53  * structure for use by the if_uba.c routines in running the interface
54  * efficiently.
55  */
56 struct	en_softc {
57 	struct	ifnet es_if;		/* network-visible interface */
58 	struct	ifuba es_ifuba;		/* UNIBUS resources */
59 	short	es_delay;		/* current output delay */
60 	short	es_mask;		/* mask for current output delay */
61 	short	es_lastx;		/* host last transmitted to */
62 	short	es_oactive;		/* is output active? */
63 	short	es_olen;		/* length of last output */
64 } en_softc[NEN];
65 
66 /*
67  * Do output DMA to determine interface presence and
68  * interrupt vector.  DMA is too short to disturb other hosts.
69  */
70 enprobe(reg)
71 	caddr_t reg;
72 {
73 	register int br, cvec;		/* r11, r10 value-result */
74 	register struct endevice *addr = (struct endevice *)reg;
75 
76 COUNT(ENPROBE);
77 #ifdef lint
78 	br = 0; cvec = br; br = cvec;
79 	enrint(0); enxint(0); encollide(0);
80 #endif
81 	addr->en_istat = 0;
82 	addr->en_owc = -1;
83 	addr->en_oba = 0;
84 	addr->en_ostat = EN_IEN|EN_GO;
85 	DELAY(100000);
86 	addr->en_ostat = 0;
87 	return (1);
88 }
89 
90 /*
91  * Interface exists: make available by filling in network interface
92  * record.  System will initialize the interface when it is ready
93  * to accept packets.
94  */
95 enattach(ui)
96 	struct uba_device *ui;
97 {
98 	register struct en_softc *es = &en_softc[ui->ui_unit];
99 	register struct sockaddr_in *sin;
100 COUNT(ENATTACH);
101 
102 	es->es_if.if_unit = ui->ui_unit;
103 	es->es_if.if_name = "en";
104 	es->es_if.if_mtu = ENMTU;
105 	es->es_if.if_net = ui->ui_flags & 0xff;
106 	es->es_if.if_host[0] =
107 	 (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff;
108 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
109 	sin->sin_family = AF_INET;
110 	sin->sin_addr = if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
111 	sin = (struct sockaddr_in *)&es->es_if.if_broadaddr;
112 	sin->sin_family = AF_INET;
113 	sin->sin_addr = if_makeaddr(es->es_if.if_net, 0);
114 	es->es_if.if_flags = IFF_BROADCAST;
115 	es->es_if.if_init = eninit;
116 	es->es_if.if_output = enoutput;
117 	es->es_if.if_ubareset = enreset;
118 	es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16;
119 	if_attach(&es->es_if);
120 #if NIMP == 0
121 	/* here's one for you john baby.... */
122 	enlhinit((ui->ui_flags &~ 0xff) | 0x0a);
123 #endif
124 }
125 
126 /*
127  * Reset of interface after UNIBUS reset.
128  * If interface is on specified uba, reset its state.
129  */
130 enreset(unit, uban)
131 	int unit, uban;
132 {
133 	register struct uba_device *ui;
134 COUNT(ENRESET);
135 
136 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
137 	    ui->ui_ubanum != uban)
138 		return;
139 	printf(" en%d", unit);
140 	eninit(unit);
141 }
142 
143 /*
144  * Initialization of interface; clear recorded pending
145  * operations, and reinitialize UNIBUS usage.
146  */
147 eninit(unit)
148 	int unit;
149 {
150 	register struct en_softc *es = &en_softc[unit];
151 	register struct uba_device *ui = eninfo[unit];
152 	register struct endevice *addr;
153 	int s;
154 
155 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
156 	    sizeof (struct en_header), (int)btoc(ENMTU)) == 0) {
157 		printf("en%d: can't initialize\n", unit);
158 		es->es_if.if_flags &= ~IFF_UP;
159 		return;
160 	}
161 	addr = (struct endevice *)ui->ui_addr;
162 	addr->en_istat = addr->en_ostat = 0;
163 
164 	/*
165 	 * Hang a receive and start any
166 	 * pending writes by faking a transmit complete.
167 	 */
168 	s = splimp();
169 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
170 	addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
171 	addr->en_istat = EN_IEN|EN_GO;
172 	es->es_oactive = 1;
173 	es->es_if.if_flags |= IFF_UP;
174 	enxint(unit);
175 	splx(s);
176 	if_rtinit(&es->es_if, RTF_DIRECT|RTF_UP);
177 }
178 
179 int	enalldelay = 0;
180 int	enlastdel = 25;
181 int	enlastmask = (~0) << 5;
182 
183 /*
184  * Start or restart output on interface.
185  * If interface is already active, then this is a retransmit
186  * after a collision, and just restuff registers and delay.
187  * If interface is not already active, get another datagram
188  * to send off of the interface queue, and map it to the interface
189  * before starting the output.
190  */
191 enstart(dev)
192 	dev_t dev;
193 {
194         int unit = ENUNIT(dev);
195 	struct uba_device *ui = eninfo[unit];
196 	register struct en_softc *es = &en_softc[unit];
197 	register struct endevice *addr;
198 	struct mbuf *m;
199 	int dest;
200 COUNT(ENSTART);
201 
202 	if (es->es_oactive)
203 		goto restart;
204 
205 	/*
206 	 * Not already active: dequeue another request
207 	 * and map it to the UNIBUS.  If no more requests,
208 	 * just return.
209 	 */
210 	IF_DEQUEUE(&es->es_if.if_snd, m);
211 	if (m == 0) {
212 		es->es_oactive = 0;
213 		return;
214 	}
215 	dest = mtod(m, struct en_header *)->en_dhost;
216 	es->es_olen = if_wubaput(&es->es_ifuba, m);
217 
218 	/*
219 	 * Ethernet cannot take back-to-back packets (no
220 	 * buffering in interface.  To help avoid overrunning
221 	 * receivers, enforce a small delay (about 1ms) in interface:
222 	 *	* between all packets when enalldelay
223 	 *	* whenever last packet was broadcast
224 	 *	* whenever this packet is to same host as last packet
225 	 */
226 	if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
227 		es->es_delay = enlastdel;
228 		es->es_mask = enlastmask;
229 	}
230 	es->es_lastx = dest;
231 
232 restart:
233 	/*
234 	 * Have request mapped to UNIBUS for transmission.
235 	 * Purge any stale data from this BDP, and start the otput.
236 	 */
237 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
238 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
239 	addr = (struct endevice *)ui->ui_addr;
240 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
241 	addr->en_odelay = es->es_delay;
242 	addr->en_owc = -((es->es_olen + 1) >> 1);
243 	addr->en_ostat = EN_IEN|EN_GO;
244 	es->es_oactive = 1;
245 }
246 
247 /*
248  * Ethernet interface transmitter interrupt.
249  * Start another output if more data to send.
250  */
251 enxint(unit)
252 	int unit;
253 {
254 	register struct uba_device *ui = eninfo[unit];
255 	register struct en_softc *es = &en_softc[unit];
256 	register struct endevice *addr = (struct endevice *)ui->ui_addr;
257 COUNT(ENXINT);
258 
259 	if (es->es_oactive == 0)
260 		return;
261 	if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
262 		es->es_if.if_oerrors++;
263 		if (es->es_if.if_oerrors % 100 == 0)
264 			printf("en%d: += 100 output errors\n", unit);
265 		endocoll(unit);
266 		return;
267 	}
268 	es->es_if.if_opackets++;
269 	es->es_oactive = 0;
270 	es->es_delay = 0;
271 	es->es_mask = ~0;
272 	if (es->es_ifuba.ifu_xtofree) {
273 		m_freem(es->es_ifuba.ifu_xtofree);
274 		es->es_ifuba.ifu_xtofree = 0;
275 	}
276 	if (es->es_if.if_snd.ifq_head == 0) {
277 		es->es_lastx = 256;		/* putatively illegal */
278 		return;
279 	}
280 	enstart(unit);
281 }
282 
283 /*
284  * Collision on ethernet interface.  Do exponential
285  * backoff, and retransmit.  If have backed off all
286  * the way print warning diagnostic, and drop packet.
287  */
288 encollide(unit)
289 	int unit;
290 {
291 	struct en_softc *es = &en_softc[unit];
292 COUNT(ENCOLLIDE);
293 
294 	es->es_if.if_collisions++;
295 	if (es->es_oactive == 0)
296 		return;
297 	endocoll(unit);
298 }
299 
300 endocoll(unit)
301 	int unit;
302 {
303 	register struct en_softc *es = &en_softc[unit];
304 
305 	/*
306 	 * Es_mask is a 16 bit number with n low zero bits, with
307 	 * n the number of backoffs.  When es_mask is 0 we have
308 	 * backed off 16 times, and give up.
309 	 */
310 	if (es->es_mask == 0) {
311 		printf("en%d: send error\n", unit);
312 		enxint(unit);
313 		return;
314 	}
315 	/*
316 	 * Another backoff.  Restart with delay based on n low bits
317 	 * of the interval timer.
318 	 */
319 	es->es_mask <<= 1;
320 	es->es_delay = mfpr(ICR) &~ es->es_mask;
321 	enstart(unit);
322 }
323 
324 struct	sockaddr_pup pupsrc = { AF_PUP };
325 struct	sockaddr_pup pupdst = { AF_PUP };
326 struct	sockproto pupproto = { PF_PUP };
327 /*
328  * Ethernet interface receiver interrupt.
329  * If input error just drop packet.
330  * Otherwise purge input buffered data path and examine
331  * packet to determine type.  If can't determine length
332  * from type, then have to drop packet.  Othewise decapsulate
333  * packet based on type and pass to type specific higher-level
334  * input routine.
335  */
336 enrint(unit)
337 	int unit;
338 {
339 	register struct en_softc *es = &en_softc[unit];
340 	struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
341 	register struct en_header *en;
342     	struct mbuf *m;
343 	int len, plen; short resid;
344 	register struct ifqueue *inq;
345 	int off;
346 COUNT(ENRINT);
347 
348 	es->es_if.if_ipackets++;
349 
350 	/*
351 	 * Purge BDP; drop if input error indicated.
352 	 */
353 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
354 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
355 	if (addr->en_istat&EN_IERROR) {
356 		es->es_if.if_ierrors++;
357 		if (es->es_if.if_ierrors % 100 == 0)
358 			printf("en%d: += 100 input errors\n", unit);
359 		goto setup;
360 	}
361 
362 	/*
363 	 * Calculate input data length.
364 	 * Get pointer to ethernet header (in input buffer).
365 	 * Deal with trailer protocol: if type is PUP trailer
366 	 * get true type from first 16-bit word past data.
367 	 * Remember that type was trailer by setting off.
368 	 */
369 	resid = addr->en_iwc;
370 	if (resid)
371 		resid |= 0176000;
372 	len = (((sizeof (struct en_header) + ENMTU) >> 1) + resid) << 1;
373 	len -= sizeof (struct en_header);
374 	if (len >= ENMTU)
375 		goto setup;			/* sanity */
376 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
377 #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
378 	if (en->en_type >= ENPUP_TRAIL &&
379 	    en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
380 		off = (en->en_type - ENPUP_TRAIL) * 512;
381 		if (off >= ENMTU)
382 			goto setup;		/* sanity */
383 		en->en_type = *endataaddr(en, off, u_short *);
384 		resid = *(endataaddr(en, off+2, u_short *));
385 		if (off + resid > len)
386 			goto setup;		/* sanity */
387 		len = off + resid;
388 	} else
389 		off = 0;
390 	if (len == 0)
391 		goto setup;
392 	/*
393 	 * Pull packet off interface.  Off is nonzero if packet
394 	 * has trailing header; if_rubaget will then force this header
395 	 * information to be at the front, but we still have to drop
396 	 * the type and length which are at the front of any trailer data.
397 	 */
398 	m = if_rubaget(&es->es_ifuba, len, off);
399 	if (m == 0)
400 		goto setup;
401 	if (off) {
402 		m->m_off += 2 * sizeof (u_short);
403 		m->m_len -= 2 * sizeof (u_short);
404 	}
405 	switch (en->en_type) {
406 
407 #ifdef INET
408 	case ENPUP_IPTYPE:
409 		schednetisr(NETISR_IP);
410 		inq = &ipintrq;
411 		break;
412 #endif
413 #ifdef PUP
414 	case ENPUP_PUPTYPE: {
415 		struct pup_header *pup = mtod(m, struct pup_header *);
416 
417 		pupproto.sp_protocol = pup->pup_type;
418 		pupdst.spup_addr = pup->pup_dport;
419 		pupsrc.spup_addr = pup->pup_sport;
420 		raw_input(m, &pupproto, (struct sockaddr *)&pupdst,
421 		  (struct sockaddr *)&pupsrc);
422 		goto setup;
423 	}
424 #endif
425 	}
426 
427 	if (IF_QFULL(inq)) {
428 		IF_DROP(inq);
429 		m_freem(m);
430 	} else
431 		IF_ENQUEUE(inq, m);
432 
433 setup:
434 	/*
435 	 * Reset for next packet.
436 	 */
437 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
438 	addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
439 	addr->en_istat = EN_IEN|EN_GO;
440 }
441 
442 /*
443  * Ethernet output routine.
444  * Encapsulate a packet of type family for the local net.
445  * Use trailer local net encapsulation if enough data in first
446  * packet leaves a multiple of 512 bytes of data in remainder.
447  */
448 enoutput(ifp, m0, dst)
449 	struct ifnet *ifp;
450 	struct mbuf *m0;
451 	struct sockaddr *dst;
452 {
453 	int type, dest, s;
454 	register struct mbuf *m = m0;
455 	register struct en_header *en;
456 	register int off;
457 
458 COUNT(ENOUTPUT);
459 	switch (dst->sa_family) {
460 
461 #ifdef INET
462 	case AF_INET:
463 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr >> 24;
464 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
465 		if (off > 0 && (off & 0x1ff) == 0 &&
466 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
467 			type = ENPUP_TRAIL + (off>>9);
468 			m->m_off -= 2 * sizeof (u_short);
469 			m->m_len += 2 * sizeof (u_short);
470 			*mtod(m, u_short *) = ENPUP_IPTYPE;
471 			*(mtod(m, u_short *) + 1) = m->m_len;
472 			goto gottrailertype;
473 		}
474 		type = ENPUP_IPTYPE;
475 		off = 0;
476 		goto gottype;
477 #endif
478 #ifdef PUP
479 	case AF_PUP:
480 		dest = ((struct sockaddr_pup *)dst)->spup_addr.pp_host;
481 		type = ENPUP_PUPTYPE;
482 		off = 0;
483 		goto gottype;
484 #endif
485 
486 	default:
487 		printf("en%d: can't handle af%d\n", ifp->if_unit,
488 			dst->sa_family);
489 		m_freem(m0);
490 		return (0);
491 	}
492 
493 gottrailertype:
494 	/*
495 	 * Packet to be sent as trailer: move first packet
496 	 * (control information) to end of chain.
497 	 */
498 	while (m->m_next)
499 		m = m->m_next;
500 	m->m_next = m0;
501 	m = m0->m_next;
502 	m0->m_next = 0;
503 	m0 = m;
504 
505 gottype:
506 	/*
507 	 * Add local net header.  If no space in first mbuf,
508 	 * allocate another.
509 	 */
510 	if (m->m_off > MMAXOFF ||
511 	    MMINOFF + sizeof (struct en_header) > m->m_off) {
512 		m = m_get(M_DONTWAIT);
513 		if (m == 0) {
514 			m_freem(m0);
515 			return (0);
516 		}
517 		m->m_next = m0;
518 		m->m_off = MMINOFF;
519 		m->m_len = sizeof (struct en_header);
520 	} else {
521 		m->m_off -= sizeof (struct en_header);
522 		m->m_len += sizeof (struct en_header);
523 	}
524 	en = mtod(m, struct en_header *);
525 	en->en_shost = ifp->if_host[0];
526 	en->en_dhost = dest;
527 	en->en_type = type;
528 
529 	/*
530 	 * Queue message on interface, and start output if interface
531 	 * not yet active.
532 	 */
533 	s = splimp();
534 	if (IF_QFULL(&ifp->if_snd)) {
535 		IF_DROP(&ifp->if_snd);
536 		m_freem(m);
537 		splx(s);
538 		return (0);
539 	}
540 	IF_ENQUEUE(&ifp->if_snd, m);
541 	if (en_softc[ifp->if_unit].es_oactive == 0)
542 		enstart(ifp->if_unit);
543 	splx(s);
544 	return (1);
545 }
546 
547 #if NIMP == 0 && NEN > 0
548 /*
549  * Logical host interface driver.
550  * Allows host to appear as an ARPAnet
551  * logical host.  Must also have routing
552  * table entry set up to forward packets
553  * to appropriate gateway on localnet.
554  */
555 
556 struct	ifnet enlhif;
557 int	enlhoutput();
558 
559 /*
560  * Called by localnet interface to allow logical
561  * host interface to "attach".  Nothing should ever
562  * be sent locally to this interface, it's purpose
563  * is simply to establish the host's arpanet address.
564  */
565 enlhinit(addr)
566 	int addr;
567 {
568 	register struct ifnet *ifp = &enlhif;
569 	register struct sockaddr_in *sin;
570 
571 COUNT(ENLHINIT);
572 	ifp->if_name = "lh";
573 	ifp->if_mtu = ENMTU;
574 	sin = (struct sockaddr_in *)&ifp->if_addr;
575 	sin->sin_family = AF_INET;
576 	sin->sin_addr.s_addr = addr;
577 	ifp->if_net = sin->sin_addr.s_net;
578 	ifp->if_flags = IFF_UP;
579 	ifp->if_output = enlhoutput;	/* should never be used */
580 	if_attach(ifp);
581 }
582 
583 enlhoutput(ifp, m0, dst)
584 	struct ifnet *ifp;
585 	struct mbuf *m0;
586 	struct sockaddr *dst;
587 {
588 COUNT(ENLHOUTPUT);
589 	ifp->if_oerrors++;
590 	m_freem(m0);
591 	return (0);
592 }
593 #endif
594