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