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