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