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