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