xref: /csrg-svn/sys/vax/if/if_il.c (revision 9650)
1 /*	if_il.c	4.15	82/12/14	*/
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_ilreg.h"
30 #include "../vaxif/if_il.h"
31 #include "../vaxif/if_uba.h"
32 #include "../vaxuba/ubareg.h"
33 #include "../vaxuba/ubavar.h"
34 
35 #define	ILMTU	1500
36 #define	ILMIN	(60-14)
37 
38 int	ilprobe(), ilattach(), ilrint(), ilcint();
39 struct	uba_device *ilinfo[NIL];
40 u_short ilstd[] = { 0 };
41 struct	uba_driver ildriver =
42 	{ ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
43 #define	ILUNIT(x)	minor(x)
44 int	ilinit(),iloutput(),ilreset(),ilwatch();
45 
46 u_char	il_ectop[3] = { 0x02, 0x60, 0x8c };
47 u_char	ilbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
48 
49 /*
50  * Ethernet software status per interface.
51  *
52  * Each interface is referenced by a network interface structure,
53  * is_if, which the routing code uses to locate the interface.
54  * This structure contains the output queue for the interface, its address, ...
55  * We also have, for each interface, a UBA interface structure, which
56  * contains information about the UNIBUS resources held by the interface:
57  * map registers, buffered data paths, etc.  Information is cached in this
58  * structure for use by the if_uba.c routines in running the interface
59  * efficiently.
60  */
61 struct	il_softc {
62 	struct	ifnet is_if;		/* network-visible interface */
63 	struct	ifuba is_ifuba;		/* UNIBUS resources */
64 	int	is_flags;
65 #define	ILF_OACTIVE	0x1		/* output is active */
66 #define	ILF_RCVPENDING	0x2		/* start rcv in ilcint */
67 #define	ILF_STATPENDING	0x4		/* stat cmd pending */
68 	short	is_lastcmd;		/* can't read csr, so must save it */
69 	short	is_scaninterval;	/* interval of stat collection */
70 #define	ILWATCHINTERVAL	60		/* once every 60 seconds */
71 	struct	il_stats is_stats;	/* holds on-board statistics */
72 	struct	il_stats is_sum;	/* summation over time */
73 	int	is_ubaddr;		/* mapping registers of is_stats */
74 } il_softc[NIL];
75 
76 ilprobe(reg)
77 	caddr_t reg;
78 {
79 	register int br, cvec;		/* r11, r10 value-result */
80 	register struct ildevice *addr = (struct ildevice *)reg;
81 	register i;
82 
83 #ifdef lint
84 	br = 0; cvec = br; br = cvec;
85 	i = 0; ilrint(i); ilcint(i); ilwatch(i);
86 #endif
87 
88 	addr->il_csr = ILC_OFFLINE|IL_CIE;
89 	DELAY(100000);
90 	i = addr->il_csr;		/* clear CDONE */
91 	if (cvec > 0 && cvec != 0x200)
92 		cvec -= 4;
93 	return (1);
94 }
95 
96 /*
97  * Interface exists: make available by filling in network interface
98  * record.  System will initialize the interface when it is ready
99  * to accept packets.  A STATUS command is done to get the ethernet
100  * address and other interesting data.
101  */
102 ilattach(ui)
103 	struct uba_device *ui;
104 {
105 	register struct il_softc *is = &il_softc[ui->ui_unit];
106 	register struct ifnet *ifp = &is->is_if;
107 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
108 	struct sockaddr_in *sin;
109 
110 	ifp->if_unit = ui->ui_unit;
111 	ifp->if_name = "il";
112 	ifp->if_mtu = ILMTU;
113 	ifp->if_net = ui->ui_flags;
114 
115 	/*
116 	 * Reset the board and map the statistics
117 	 * buffer onto the Unibus.
118 	 */
119 	addr->il_csr = ILC_RESET;
120 	while ((addr->il_csr&IL_CDONE) == 0)
121 		;
122 	if (addr->il_csr&IL_STATUS)
123 		printf("il%d: reset failed, csr=%b\n", ui->ui_unit,
124 			addr->il_csr, IL_BITS);
125 
126 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
127 		sizeof (struct il_stats), 0);
128 	addr->il_bar = is->is_ubaddr & 0xffff;
129 	addr->il_bcr = sizeof (struct il_stats);
130 	addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT;
131 	while ((addr->il_csr&IL_CDONE) == 0)
132 		;
133 	if (addr->il_csr&IL_STATUS)
134 		printf("il%d: status failed, csr=%b\n", ui->ui_unit,
135 			addr->il_csr, IL_BITS);
136 	ubarelse(ui->ui_ubanum, &is->is_ubaddr);
137 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
138 		ui->ui_unit,
139 		is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff,
140 		is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff,
141 		is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff,
142 		is->is_stats.ils_module, is->is_stats.ils_firmware);
143 	ifp->if_host[0] =
144 	    ((is->is_stats.ils_addr[3]&0xff)<<16) | 0x800000 |
145 	    ((is->is_stats.ils_addr[4]&0xff)<<8) |
146 	    (is->is_stats.ils_addr[5]&0xff);
147 	sin = (struct sockaddr_in *)&ifp->if_addr;
148 	sin->sin_family = AF_INET;
149 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
150 
151 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
152 	sin->sin_family = AF_INET;
153 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
154 	ifp->if_flags = IFF_BROADCAST;
155 
156 	ifp->if_init = ilinit;
157 	ifp->if_output = iloutput;
158 	ifp->if_reset = ilreset;
159 	ifp->if_watchdog = ilwatch;
160 	is->is_scaninterval = ILWATCHINTERVAL;
161 	ifp->if_timer = is->is_scaninterval;
162 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
163 #ifdef notdef
164 	is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
165 #endif
166 	if_attach(ifp);
167 }
168 
169 /*
170  * Reset of interface after UNIBUS reset.
171  * If interface is on specified uba, reset its state.
172  */
173 ilreset(unit, uban)
174 	int unit, uban;
175 {
176 	register struct uba_device *ui;
177 
178 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
179 	    ui->ui_ubanum != uban)
180 		return;
181 	printf(" il%d", unit);
182 	ilinit(unit);
183 }
184 
185 /*
186  * Initialization of interface; clear recorded pending
187  * operations, and reinitialize UNIBUS usage.
188  */
189 ilinit(unit)
190 	int unit;
191 {
192 	register struct il_softc *is = &il_softc[unit];
193 	register struct uba_device *ui = ilinfo[unit];
194 	register struct ildevice *addr;
195 	int s;
196 
197 	if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
198 	    sizeof (struct il_rheader), (int)btoc(ILMTU)) == 0) {
199 		printf("il%d: can't initialize\n", unit);
200 		is->is_if.if_flags &= ~IFF_UP;
201 		return;
202 	}
203 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
204 		sizeof (struct il_stats), 0);
205 	addr = (struct ildevice *)ui->ui_addr;
206 
207 	/*
208 	 * Turn off source address insertion (it's faster this way),
209 	 * and set board online.
210 	 */
211 	s = splimp();
212 	addr->il_csr = ILC_CISA;
213 	while ((addr->il_csr & IL_CDONE) == 0)
214 		;
215 	addr->il_csr = ILC_ONLINE;
216 	while ((addr->il_csr & IL_CDONE) == 0)
217 		;
218 	/*
219 	 * Set board online.
220 	 * Hang receive buffer and start any pending
221 	 * writes by faking a transmit complete.
222 	 * Receive bcr is not a muliple of 4 so buffer
223 	 * chaining can't happen.
224 	 */
225 	s = splimp();
226 	addr->il_csr = ILC_ONLINE;
227 	while ((addr->il_csr & IL_CDONE) == 0)
228 		;
229 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
230 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
231 	addr->il_csr =
232 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
233 	while ((addr->il_csr & IL_CDONE) == 0)
234 		;
235 	is->is_flags = ILF_OACTIVE;
236 	is->is_if.if_flags |= IFF_UP;
237 	is->is_lastcmd = 0;
238 	ilcint(unit);
239 	splx(s);
240 	if_rtinit(&is->is_if, RTF_UP);
241 }
242 
243 /*
244  * Start output on interface.
245  * Get another datagram to send off of the interface queue,
246  * and map it to the interface before starting the output.
247  */
248 ilstart(dev)
249 	dev_t dev;
250 {
251         int unit = ILUNIT(dev), len;
252 	struct uba_device *ui = ilinfo[unit];
253 	register struct il_softc *is = &il_softc[unit];
254 	register struct ildevice *addr;
255 	struct mbuf *m;
256 	short csr;
257 
258 	IF_DEQUEUE(&is->is_if.if_snd, m);
259 	addr = (struct ildevice *)ui->ui_addr;
260 	if (m == 0) {
261 		if ((is->is_flags & ILF_STATPENDING) == 0)
262 			return;
263 		addr->il_bar = is->is_ubaddr & 0xffff;
264 		addr->il_bcr = sizeof (struct il_stats);
265 		csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE;
266 		is->is_flags &= ~ILF_STATPENDING;
267 		goto startcmd;
268 	}
269 	len = if_wubaput(&is->is_ifuba, m);
270 	/*
271 	 * Ensure minimum packet length.
272 	 * This makes the safe assumtion that there are no virtual holes
273 	 * after the data.
274 	 * For security, it might be wise to zero out the added bytes,
275 	 * but we're mainly interested in speed at the moment.
276 	 */
277 	if (len - sizeof(struct il_xheader) < ILMIN)
278 		len = ILMIN + sizeof(struct il_xheader);
279 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
280 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
281 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
282 	addr->il_bcr = len;
283 	csr =
284 	  ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE;
285 
286 startcmd:
287 	is->is_lastcmd = csr & IL_CMD;
288 	addr->il_csr = csr;
289 	is->is_flags |= ILF_OACTIVE;
290 }
291 
292 /*
293  * Command done interrupt.
294  */
295 ilcint(unit)
296 	int unit;
297 {
298 	register struct il_softc *is = &il_softc[unit];
299 	struct uba_device *ui = ilinfo[unit];
300 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
301 	short csr;
302 
303 	if ((is->is_flags & ILF_OACTIVE) == 0) {
304 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
305 			addr->il_csr, IL_BITS);
306 		return;
307 	}
308 
309 	csr = addr->il_csr;
310 	/*
311 	 * Hang receive buffer if it couldn't
312 	 * be done earlier (in ilrint).
313 	 */
314 	if (is->is_flags & ILF_RCVPENDING) {
315 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
316 		addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
317 		addr->il_csr =
318 		  ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
319 		while ((addr->il_csr & IL_CDONE) == 0)
320 			;
321 		is->is_flags &= ~ILF_RCVPENDING;
322 	}
323 	is->is_flags &= ~ILF_OACTIVE;
324 	csr &= IL_STATUS;
325 	switch (is->is_lastcmd) {
326 
327 	case ILC_XMIT:
328 		is->is_if.if_opackets++;
329 		if (csr > ILERR_RETRIES)
330 			is->is_if.if_oerrors++;
331 		break;
332 
333 	case ILC_STAT:
334 		if (csr == ILERR_SUCCESS)
335 			iltotal(is);
336 		break;
337 	}
338 	if (is->is_ifuba.ifu_xtofree) {
339 		m_freem(is->is_ifuba.ifu_xtofree);
340 		is->is_ifuba.ifu_xtofree = 0;
341 	}
342 	ilstart(unit);
343 }
344 
345 /*
346  * Ethernet interface receiver interrupt.
347  * If input error just drop packet.
348  * Otherwise purge input buffered data path and examine
349  * packet to determine type.  If can't determine length
350  * from type, then have to drop packet.  Othewise decapsulate
351  * packet based on type and pass to type specific higher-level
352  * input routine.
353  */
354 ilrint(unit)
355 	int unit;
356 {
357 	register struct il_softc *is = &il_softc[unit];
358 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
359 	register struct il_rheader *il;
360     	struct mbuf *m;
361 	int len, off, resid;
362 	register struct ifqueue *inq;
363 
364 	is->is_if.if_ipackets++;
365 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
366 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
367 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
368 	len = il->ilr_length - sizeof(struct il_rheader);
369 	if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 || len > ILMTU) {
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 #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
384 	if (il->ilr_type >= ILPUP_TRAIL &&
385 	    il->ilr_type < ILPUP_TRAIL+ILPUP_NTRAILER) {
386 		off = (il->ilr_type - ILPUP_TRAIL) * 512;
387 		if (off >= ILMTU)
388 			goto setup;		/* sanity */
389 		il->ilr_type = *ildataaddr(il, off, u_short *);
390 		resid = *(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 ILPUP_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) + ILMTU + 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 il_xheader *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 = ILPUP_TRAIL + (off>>9);
476 			m->m_off -= 2 * sizeof (u_short);
477 			m->m_len += 2 * sizeof (u_short);
478 			*mtod(m, u_short *) = ILPUP_IPTYPE;
479 			*(mtod(m, u_short *) + 1) = m->m_len;
480 			goto gottrailertype;
481 		}
482 		type = ILPUP_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 il_xheader) > 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 il_xheader);
521 	} else {
522 		m->m_off -= sizeof (struct il_xheader);
523 		m->m_len += sizeof (struct il_xheader);
524 	}
525 	il = mtod(m, struct il_xheader *);
526 	if ((dest &~ 0xff) == 0)
527 		bcopy((caddr_t)ilbroadcastaddr, (caddr_t)il->ilx_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->ilx_dhost, 3);
532 		il->ilx_dhost[3] = (dest>>8) & 0x7f;
533 		il->ilx_dhost[4] = (dest>>16) & 0xff;
534 		il->ilx_dhost[5] = (dest>>24) & 0xff;
535 	}
536 	bcopy((caddr_t)is->is_stats.ils_addr, (caddr_t)il->ilx_shost, 6);
537 	il->ilx_type = 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