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