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