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