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