xref: /csrg-svn/sys/vax/if/if_il.c (revision 12773)
1 /*	if_il.c	4.20	83/05/27	*/
2 
3 #include "il.h"
4 
5 /*
6  * Interlan Ethernet Communications Controller interface
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 <errno.h>
18 
19 #include "../net/if.h"
20 #include "../net/netisr.h"
21 #include "../net/route.h"
22 #include "../netinet/in.h"
23 #include "../netinet/in_systm.h"
24 #include "../netinet/ip.h"
25 #include "../netinet/ip_var.h"
26 #include "../netinet/if_ether.h"
27 #include "../netpup/pup.h"
28 
29 #include "../vax/cpu.h"
30 #include "../vax/mtpr.h"
31 #include "../vaxif/if_il.h"
32 #include "../vaxif/if_ilreg.h"
33 #include "../vaxif/if_uba.h"
34 #include "../vaxuba/ubareg.h"
35 #include "../vaxuba/ubavar.h"
36 
37 int	ilprobe(), ilattach(), ilrint(), ilcint();
38 struct	uba_device *ilinfo[NIL];
39 u_short ilstd[] = { 0 };
40 struct	uba_driver ildriver =
41 	{ ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
42 #define	ILUNIT(x)	minor(x)
43 int	ilinit(),iloutput(),ilreset(),ilwatch();
44 
45 /*
46  * Ethernet software status per interface.
47  *
48  * Each interface is referenced by a network interface structure,
49  * is_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	il_softc {
58 	struct	arpcom is_ac;		/* Ethernet common part */
59 #define	is_if	is_ac.ac_if		/* network-visible interface */
60 #define	is_addr	is_ac.ac_enaddr		/* hardware Ethernet address */
61 	struct	ifuba is_ifuba;		/* UNIBUS resources */
62 	int	is_flags;
63 #define	ILF_OACTIVE	0x1		/* output is active */
64 #define	ILF_RCVPENDING	0x2		/* start rcv in ilcint */
65 #define	ILF_STATPENDING	0x4		/* stat cmd pending */
66 	short	is_lastcmd;		/* can't read csr, so must save it */
67 	short	is_scaninterval;	/* interval of stat collection */
68 #define	ILWATCHINTERVAL	60		/* once every 60 seconds */
69 	struct	il_stats is_stats;	/* holds on-board statistics */
70 	struct	il_stats is_sum;	/* summation over time */
71 	int	is_ubaddr;		/* mapping registers of is_stats */
72 } il_softc[NIL];
73 
74 ilprobe(reg)
75 	caddr_t reg;
76 {
77 	register int br, cvec;		/* r11, r10 value-result */
78 	register struct ildevice *addr = (struct ildevice *)reg;
79 	register i;
80 
81 #ifdef lint
82 	br = 0; cvec = br; br = cvec;
83 	i = 0; ilrint(i); ilcint(i); ilwatch(i);
84 #endif
85 
86 	addr->il_csr = ILC_OFFLINE|IL_CIE;
87 	DELAY(100000);
88 	i = addr->il_csr;		/* clear CDONE */
89 	if (cvec > 0 && cvec != 0x200)
90 		cvec -= 4;
91 	return (1);
92 }
93 
94 /*
95  * Interface exists: make available by filling in network interface
96  * record.  System will initialize the interface when it is ready
97  * to accept packets.  A STATUS command is done to get the ethernet
98  * address and other interesting data.
99  */
100 ilattach(ui)
101 	struct uba_device *ui;
102 {
103 	register struct il_softc *is = &il_softc[ui->ui_unit];
104 	register struct ifnet *ifp = &is->is_if;
105 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
106 	struct sockaddr_in *sin;
107 
108 	ifp->if_unit = ui->ui_unit;
109 	ifp->if_name = "il";
110 	ifp->if_mtu = ETHERMTU;
111 
112 	/*
113 	 * Reset the board and map the statistics
114 	 * buffer onto the Unibus.
115 	 */
116 	addr->il_csr = ILC_RESET;
117 	while ((addr->il_csr&IL_CDONE) == 0)
118 		;
119 	if (addr->il_csr&IL_STATUS)
120 		printf("il%d: reset failed, csr=%b\n", ui->ui_unit,
121 			addr->il_csr, IL_BITS);
122 
123 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
124 		sizeof (struct il_stats), 0);
125 	addr->il_bar = is->is_ubaddr & 0xffff;
126 	addr->il_bcr = sizeof (struct il_stats);
127 	addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT;
128 	while ((addr->il_csr&IL_CDONE) == 0)
129 		;
130 	if (addr->il_csr&IL_STATUS)
131 		printf("il%d: status failed, csr=%b\n", ui->ui_unit,
132 			addr->il_csr, IL_BITS);
133 	ubarelse(ui->ui_ubanum, &is->is_ubaddr);
134 #ifdef notdef
135 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
136 		ui->ui_unit,
137 		is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff,
138 		is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff,
139 		is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff,
140 		is->is_stats.ils_module, is->is_stats.ils_firmware);
141 #endif
142 	bcopy((caddr_t)is->is_stats.ils_addr, (caddr_t)is->is_addr,
143 	    sizeof (is->is_addr));
144 	sin = (struct sockaddr_in *)&ifp->if_addr;
145 	sin->sin_family = AF_INET;
146 	sin->sin_addr = arpmyaddr((struct arpcom *)0);
147 	ifp->if_init = ilinit;
148 	ifp->if_output = iloutput;
149 	ifp->if_reset = ilreset;
150 	ifp->if_watchdog = ilwatch;
151 	is->is_scaninterval = ILWATCHINTERVAL;
152 	ifp->if_timer = is->is_scaninterval;
153 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
154 #ifdef notdef
155 	is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
156 #endif
157 	if_attach(ifp);
158 }
159 
160 /*
161  * Reset of interface after UNIBUS reset.
162  * If interface is on specified uba, reset its state.
163  */
164 ilreset(unit, uban)
165 	int unit, uban;
166 {
167 	register struct uba_device *ui;
168 
169 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
170 	    ui->ui_ubanum != uban)
171 		return;
172 	printf(" il%d", unit);
173 	ilinit(unit);
174 }
175 
176 /*
177  * Initialization of interface; clear recorded pending
178  * operations, and reinitialize UNIBUS usage.
179  */
180 ilinit(unit)
181 	int unit;
182 {
183 	register struct il_softc *is = &il_softc[unit];
184 	register struct uba_device *ui = ilinfo[unit];
185 	register struct ildevice *addr;
186 	int s;
187 	register struct ifnet *ifp = &is->is_if;
188 	register struct sockaddr_in *sin, *sinb;
189 
190 	sin = (struct sockaddr_in *)&ifp->if_addr;
191 	if (sin->sin_addr.s_addr == 0)	/* if address still unknown */
192 		return;
193 	ifp->if_net = in_netof(sin->sin_addr);
194 	ifp->if_host[0] = in_lnaof(sin->sin_addr);
195 	sinb = (struct sockaddr_in *)&ifp->if_broadaddr;
196 	sinb->sin_family = AF_INET;
197 	sinb->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
198 	ifp->if_flags = IFF_BROADCAST;
199 
200 	if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
201 	    sizeof (struct il_rheader), (int)btoc(ETHERMTU)) == 0) {
202 		printf("il%d: can't initialize\n", unit);
203 		is->is_if.if_flags &= ~IFF_UP;
204 		return;
205 	}
206 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
207 		sizeof (struct il_stats), 0);
208 	addr = (struct ildevice *)ui->ui_addr;
209 
210 	/*
211 	 * Turn off source address insertion (it's faster this way),
212 	 * and set board online.  Former doesn't work if board is
213 	 * already online (happens on ubareset), so we put it offline
214 	 * first.
215 	 */
216 	s = splimp();
217 	addr->il_csr = ILC_OFFLINE;
218 	while ((addr->il_csr & IL_CDONE) == 0)
219 		;
220 	addr->il_csr = ILC_CISA;
221 	while ((addr->il_csr & IL_CDONE) == 0)
222 		;
223 	/*
224 	 * Set board online.
225 	 * Hang receive buffer and start any pending
226 	 * writes by faking a transmit complete.
227 	 * Receive bcr is not a muliple of 4 so buffer
228 	 * chaining can't happen.
229 	 */
230 	addr->il_csr = ILC_ONLINE;
231 	while ((addr->il_csr & IL_CDONE) == 0)
232 		;
233 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
234 	addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
235 	addr->il_csr =
236 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
237 	while ((addr->il_csr & IL_CDONE) == 0)
238 		;
239 	is->is_flags = ILF_OACTIVE;
240 	is->is_if.if_flags |= IFF_UP;
241 	is->is_lastcmd = 0;
242 	ilcint(unit);
243 	splx(s);
244 	if_rtinit(&is->is_if, RTF_UP);
245 	arpattach(&is->is_ac);
246 	arpwhohas(&is->is_ac, &sin->sin_addr);
247 }
248 
249 /*
250  * Start output on interface.
251  * Get another datagram to send off of the interface queue,
252  * and map it to the interface before starting the output.
253  */
254 ilstart(dev)
255 	dev_t dev;
256 {
257         int unit = ILUNIT(dev), len;
258 	struct uba_device *ui = ilinfo[unit];
259 	register struct il_softc *is = &il_softc[unit];
260 	register struct ildevice *addr;
261 	struct mbuf *m;
262 	short csr;
263 
264 	IF_DEQUEUE(&is->is_if.if_snd, m);
265 	addr = (struct ildevice *)ui->ui_addr;
266 	if (m == 0) {
267 		if ((is->is_flags & ILF_STATPENDING) == 0)
268 			return;
269 		addr->il_bar = is->is_ubaddr & 0xffff;
270 		addr->il_bcr = sizeof (struct il_stats);
271 		csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE;
272 		is->is_flags &= ~ILF_STATPENDING;
273 		goto startcmd;
274 	}
275 	len = if_wubaput(&is->is_ifuba, m);
276 	/*
277 	 * Ensure minimum packet length.
278 	 * This makes the safe assumtion that there are no virtual holes
279 	 * after the data.
280 	 * For security, it might be wise to zero out the added bytes,
281 	 * but we're mainly interested in speed at the moment.
282 	 */
283 	if (len - sizeof(struct ether_header) < ETHERMIN)
284 		len = ETHERMIN + sizeof(struct ether_header);
285 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
286 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
287 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
288 	addr->il_bcr = len;
289 	csr =
290 	  ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE;
291 
292 startcmd:
293 	is->is_lastcmd = csr & IL_CMD;
294 	addr->il_csr = csr;
295 	is->is_flags |= ILF_OACTIVE;
296 }
297 
298 /*
299  * Command done interrupt.
300  */
301 ilcint(unit)
302 	int unit;
303 {
304 	register struct il_softc *is = &il_softc[unit];
305 	struct uba_device *ui = ilinfo[unit];
306 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
307 	short csr;
308 
309 	if ((is->is_flags & ILF_OACTIVE) == 0) {
310 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
311 			addr->il_csr, IL_BITS);
312 		return;
313 	}
314 
315 	csr = addr->il_csr;
316 	/*
317 	 * Hang receive buffer if it couldn't
318 	 * be done earlier (in ilrint).
319 	 */
320 	if (is->is_flags & ILF_RCVPENDING) {
321 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
322 		addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
323 		addr->il_csr =
324 		  ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
325 		while ((addr->il_csr & IL_CDONE) == 0)
326 			;
327 		is->is_flags &= ~ILF_RCVPENDING;
328 	}
329 	is->is_flags &= ~ILF_OACTIVE;
330 	csr &= IL_STATUS;
331 	switch (is->is_lastcmd) {
332 
333 	case ILC_XMIT:
334 		is->is_if.if_opackets++;
335 		if (csr > ILERR_RETRIES)
336 			is->is_if.if_oerrors++;
337 		break;
338 
339 	case ILC_STAT:
340 		if (csr == ILERR_SUCCESS)
341 			iltotal(is);
342 		break;
343 	}
344 	if (is->is_ifuba.ifu_xtofree) {
345 		m_freem(is->is_ifuba.ifu_xtofree);
346 		is->is_ifuba.ifu_xtofree = 0;
347 	}
348 	ilstart(unit);
349 }
350 
351 /*
352  * Ethernet interface receiver interrupt.
353  * If input error just drop packet.
354  * Otherwise purge input buffered data path and examine
355  * packet to determine type.  If can't determine length
356  * from type, then have to drop packet.  Othewise decapsulate
357  * packet based on type and pass to type specific higher-level
358  * input routine.
359  */
360 ilrint(unit)
361 	int unit;
362 {
363 	register struct il_softc *is = &il_softc[unit];
364 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
365 	register struct il_rheader *il;
366     	struct mbuf *m;
367 	int len, off, resid;
368 	register struct ifqueue *inq;
369 
370 	is->is_if.if_ipackets++;
371 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
372 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
373 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
374 	len = il->ilr_length - sizeof(struct il_rheader);
375 	if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 ||
376 	    len > ETHERMTU) {
377 		is->is_if.if_ierrors++;
378 #ifdef notdef
379 		if (is->is_if.if_ierrors % 100 == 0)
380 			printf("il%d: += 100 input errors\n", unit);
381 #endif
382 		goto setup;
383 	}
384 
385 	/*
386 	 * Deal with trailer protocol: if type is PUP trailer
387 	 * get true type from first 16-bit word past data.
388 	 * Remember that type was trailer by setting off.
389 	 */
390 	il->ilr_type = ntohs((u_short)il->ilr_type);
391 #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
392 	if (il->ilr_type >= ETHERPUP_TRAIL &&
393 	    il->ilr_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
394 		off = (il->ilr_type - ETHERPUP_TRAIL) * 512;
395 		if (off >= ETHERMTU)
396 			goto setup;		/* sanity */
397 		il->ilr_type = ntohs(*ildataaddr(il, off, u_short *));
398 		resid = ntohs(*(ildataaddr(il, off+2, u_short *)));
399 		if (off + resid > len)
400 			goto setup;		/* sanity */
401 		len = off + resid;
402 	} else
403 		off = 0;
404 	if (len == 0)
405 		goto setup;
406 
407 	/*
408 	 * Pull packet off interface.  Off is nonzero if packet
409 	 * has trailing header; ilget will then force this header
410 	 * information to be at the front, but we still have to drop
411 	 * the type and length which are at the front of any trailer data.
412 	 */
413 	m = if_rubaget(&is->is_ifuba, len, off);
414 	if (m == 0)
415 		goto setup;
416 	if (off) {
417 		m->m_off += 2 * sizeof (u_short);
418 		m->m_len -= 2 * sizeof (u_short);
419 	}
420 	switch (il->ilr_type) {
421 
422 #ifdef INET
423 	case ETHERPUP_IPTYPE:
424 		schednetisr(NETISR_IP);
425 		inq = &ipintrq;
426 		break;
427 
428 	case ETHERPUP_ARPTYPE:
429 		arpinput(&is->is_ac, m);
430 		return;
431 #endif
432 	default:
433 		m_freem(m);
434 		goto setup;
435 	}
436 
437 	if (IF_QFULL(inq)) {
438 		IF_DROP(inq);
439 		m_freem(m);
440 		goto setup;
441 	}
442 	IF_ENQUEUE(inq, m);
443 
444 setup:
445 	/*
446 	 * Reset for next packet if possible.
447 	 * If waiting for transmit command completion, set flag
448 	 * and wait until command completes.
449 	 */
450 	if (is->is_flags & ILF_OACTIVE) {
451 		is->is_flags |= ILF_RCVPENDING;
452 		return;
453 	}
454 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
455 	addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
456 	addr->il_csr =
457 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
458 	while ((addr->il_csr & IL_CDONE) == 0)
459 		;
460 }
461 
462 /*
463  * Ethernet output routine.
464  * Encapsulate a packet of type family for the local net.
465  * Use trailer local net encapsulation if enough data in first
466  * packet leaves a multiple of 512 bytes of data in remainder.
467  */
468 iloutput(ifp, m0, dst)
469 	struct ifnet *ifp;
470 	struct mbuf *m0;
471 	struct sockaddr *dst;
472 {
473 	int type, s, error;
474 	u_char edst[6];
475 	struct in_addr idst;
476 	register struct il_softc *is = &il_softc[ifp->if_unit];
477 	register struct mbuf *m = m0;
478 	register struct ether_header *il;
479 	register int off;
480 
481 	switch (dst->sa_family) {
482 
483 #ifdef INET
484 	case AF_INET:
485 		idst = ((struct sockaddr_in *)dst)->sin_addr;
486 		if (!arpresolve(&is->is_ac, m, &idst, edst))
487 			return (0);	/* if not yet resolved */
488 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
489 		if (off > 0 && (off & 0x1ff) == 0 &&
490 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
491 			type = ETHERPUP_TRAIL + (off>>9);
492 			m->m_off -= 2 * sizeof (u_short);
493 			m->m_len += 2 * sizeof (u_short);
494 			*mtod(m, u_short *) = htons((u_short)ETHERPUP_IPTYPE);
495 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
496 			goto gottrailertype;
497 		}
498 		type = ETHERPUP_IPTYPE;
499 		off = 0;
500 		goto gottype;
501 #endif
502 
503 	case AF_UNSPEC:
504 		il = (struct ether_header *)dst->sa_data;
505 		bcopy((caddr_t)il->ether_dhost, (caddr_t)edst, sizeof (edst));
506 		type = il->ether_type;
507 		goto gottype;
508 
509 	default:
510 		printf("il%d: can't handle af%d\n", ifp->if_unit,
511 			dst->sa_family);
512 		error = EAFNOSUPPORT;
513 		goto bad;
514 	}
515 
516 gottrailertype:
517 	/*
518 	 * Packet to be sent as trailer: move first packet
519 	 * (control information) to end of chain.
520 	 */
521 	while (m->m_next)
522 		m = m->m_next;
523 	m->m_next = m0;
524 	m = m0->m_next;
525 	m0->m_next = 0;
526 	m0 = m;
527 
528 gottype:
529 	/*
530 	 * Add local net header.  If no space in first mbuf,
531 	 * allocate another.
532 	 */
533 	if (m->m_off > MMAXOFF ||
534 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
535 		m = m_get(M_DONTWAIT, MT_HEADER);
536 		if (m == 0) {
537 			error = ENOBUFS;
538 			goto bad;
539 		}
540 		m->m_next = m0;
541 		m->m_off = MMINOFF;
542 		m->m_len = sizeof (struct ether_header);
543 	} else {
544 		m->m_off -= sizeof (struct ether_header);
545 		m->m_len += sizeof (struct ether_header);
546 	}
547 	il = mtod(m, struct ether_header *);
548 	il->ether_type = htons((u_short)type);
549 	bcopy((caddr_t)edst, (caddr_t)il->ether_dhost, sizeof (edst));
550 	bcopy((caddr_t)is->is_addr, (caddr_t)il->ether_shost, 6);
551 
552 	/*
553 	 * Queue message on interface, and start output if interface
554 	 * not yet active.
555 	 */
556 	s = splimp();
557 	if (IF_QFULL(&ifp->if_snd)) {
558 		IF_DROP(&ifp->if_snd);
559 		splx(s);
560 		m_freem(m);
561 		return (ENOBUFS);
562 	}
563 	IF_ENQUEUE(&ifp->if_snd, m);
564 	if ((is->is_flags & ILF_OACTIVE) == 0)
565 		ilstart(ifp->if_unit);
566 	splx(s);
567 	return (0);
568 
569 bad:
570 	m_freem(m0);
571 	return (error);
572 }
573 
574 /*
575  * Watchdog routine, request statistics from board.
576  */
577 ilwatch(unit)
578 	int unit;
579 {
580 	register struct il_softc *is = &il_softc[unit];
581 	register struct ifnet *ifp = &is->is_if;
582 	int s;
583 
584 	if (is->is_flags & ILF_STATPENDING) {
585 		ifp->if_timer = is->is_scaninterval;
586 		return;
587 	}
588 	s = splimp();
589 	is->is_flags |= ILF_STATPENDING;
590 	if ((is->is_flags & ILF_OACTIVE) == 0)
591 		ilstart(ifp->if_unit);
592 	splx(s);
593 	ifp->if_timer = is->is_scaninterval;
594 }
595 
596 /*
597  * Total up the on-board statistics.
598  */
599 iltotal(is)
600 	register struct il_softc *is;
601 {
602 	register u_short *interval, *sum, *end;
603 
604 	interval = &is->is_stats.ils_frames;
605 	sum = &is->is_sum.ils_frames;
606 	end = is->is_sum.ils_fill2;
607 	while (sum < end)
608 		*sum++ += *interval++;
609 	is->is_if.if_collisions = is->is_sum.ils_collis;
610 }
611