xref: /csrg-svn/sys/vax/if/if_il.c (revision 8463)
1 /*	if_il.c	4.12	82/10/10	*/
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 
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 	ilrint(0); ilcint(0); ilwatch(0);
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 = ILMTU;
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, &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_ubareset = 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(ILMTU)) == 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, &is->is_stats,
203 		sizeof (struct il_stats), 0);
204 	addr = (struct ildevice *)ui->ui_addr;
205 
206 	/*
207 	 * Set board online.
208 	 * Hang receive buffer and start any pending
209 	 * writes by faking a transmit complete.
210 	 * Receive bcr is not a muliple of 4 so buffer
211 	 * chaining can't happen.
212 	 */
213 	s = splimp();
214 	addr->il_csr = ILC_ONLINE;
215 	while ((addr->il_csr & IL_CDONE) == 0)
216 		;
217 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
218 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
219 	addr->il_csr =
220 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
221 	while ((addr->il_csr & IL_CDONE) == 0)
222 		;
223 	is->is_flags = ILF_OACTIVE;
224 	is->is_if.if_flags |= IFF_UP;
225 	is->is_lastcmd = 0;
226 	ilcint(unit);
227 	splx(s);
228 	if_rtinit(&is->is_if, RTF_UP);
229 }
230 
231 /*
232  * Start output on interface.
233  * Get another datagram to send off of the interface queue,
234  * and map it to the interface before starting the output.
235  */
236 ilstart(dev)
237 	dev_t dev;
238 {
239         int unit = ILUNIT(dev), dest, len;
240 	struct uba_device *ui = ilinfo[unit];
241 	register struct il_softc *is = &il_softc[unit];
242 	register struct ildevice *addr;
243 	struct mbuf *m;
244 	short csr;
245 
246 	IF_DEQUEUE(&is->is_if.if_snd, m);
247 	addr = (struct ildevice *)ui->ui_addr;
248 	if (m == 0) {
249 		if ((is->is_flags & ILF_STATPENDING) == 0)
250 			return;
251 		addr->il_bar = is->is_ubaddr & 0xfff;
252 		addr->il_bcr = sizeof (struct il_stats);
253 		csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE;
254 		is->is_flags &= ~ILF_STATPENDING;
255 		goto startcmd;
256 	}
257 	len = if_wubaput(&is->is_ifuba, m);
258 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
259 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
260 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
261 	addr->il_bcr = len;
262 	csr =
263 	  ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE;
264 
265 startcmd:
266 	is->is_lastcmd = csr & IL_CMD;
267 	addr->il_csr = csr;
268 	is->is_flags |= ILF_OACTIVE;
269 }
270 
271 /*
272  * Command done interrupt.
273  */
274 ilcint(unit)
275 	int unit;
276 {
277 	register struct il_softc *is = &il_softc[unit];
278 	struct uba_device *ui = ilinfo[unit];
279 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
280 	short csr;
281 
282 	if ((is->is_flags & ILF_OACTIVE) == 0) {
283 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
284 			addr->il_csr, IL_BITS);
285 		return;
286 	}
287 
288 	csr = addr->il_csr;
289 	/*
290 	 * Hang receive buffer if it couldn't
291 	 * be done earlier (in ilrint).
292 	 */
293 	if (is->is_flags & ILF_RCVPENDING) {
294 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
295 		addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
296 		addr->il_csr =
297 		  ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
298 		while ((addr->il_csr & IL_CDONE) == 0)
299 			;
300 		is->is_flags &= ~ILF_RCVPENDING;
301 	}
302 	is->is_flags &= ~ILF_OACTIVE;
303 	csr &= IL_STATUS;
304 	switch (is->is_lastcmd) {
305 
306 	case ILC_XMIT:
307 		is->is_if.if_opackets++;
308 		if (csr > ILERR_RETRIES)
309 			is->is_if.if_oerrors++;
310 		break;
311 
312 	case ILC_STAT:
313 		if (csr == ILERR_SUCCESS)
314 			iltotal(is);
315 		break;
316 	}
317 	if (is->is_ifuba.ifu_xtofree) {
318 		m_freem(is->is_ifuba.ifu_xtofree);
319 		is->is_ifuba.ifu_xtofree = 0;
320 	}
321 	ilstart(unit);
322 }
323 
324 /*
325  * Ethernet interface receiver interrupt.
326  * If input error just drop packet.
327  * Otherwise purge input buffered data path and examine
328  * packet to determine type.  If can't determine length
329  * from type, then have to drop packet.  Othewise decapsulate
330  * packet based on type and pass to type specific higher-level
331  * input routine.
332  */
333 ilrint(unit)
334 	int unit;
335 {
336 	register struct il_softc *is = &il_softc[unit];
337 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
338 	register struct il_rheader *il;
339     	struct mbuf *m;
340 	int len, off, resid;
341 	register struct ifqueue *inq;
342 
343 	is->is_if.if_ipackets++;
344 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
345 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
346 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
347 	len = il->ilr_length - sizeof(struct il_rheader);
348 	if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 || len > ILMTU) {
349 		is->is_if.if_ierrors++;
350 #ifdef notdef
351 		if (is->is_if.if_ierrors % 100 == 0)
352 			printf("il%d: += 100 input errors\n", unit);
353 #endif
354 		goto setup;
355 	}
356 
357 	/*
358 	 * Deal with trailer protocol: if type is PUP trailer
359 	 * get true type from first 16-bit word past data.
360 	 * Remember that type was trailer by setting off.
361 	 */
362 #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
363 	if (il->ilr_type >= ILPUP_TRAIL &&
364 	    il->ilr_type < ILPUP_TRAIL+ILPUP_NTRAILER) {
365 		off = (il->ilr_type - ILPUP_TRAIL) * 512;
366 		if (off >= ILMTU)
367 			goto setup;		/* sanity */
368 		il->ilr_type = *ildataaddr(il, off, u_short *);
369 		resid = *(ildataaddr(il, off+2, u_short *));
370 		if (off + resid > len)
371 			goto setup;		/* sanity */
372 		len = off + resid;
373 	} else
374 		off = 0;
375 	if (len == 0)
376 		goto setup;
377 
378 	/*
379 	 * Pull packet off interface.  Off is nonzero if packet
380 	 * has trailing header; ilget will then force this header
381 	 * information to be at the front, but we still have to drop
382 	 * the type and length which are at the front of any trailer data.
383 	 */
384 	m = if_rubaget(&is->is_ifuba, len, off);
385 	if (m == 0)
386 		goto setup;
387 	if (off) {
388 		m->m_off += 2 * sizeof (u_short);
389 		m->m_len -= 2 * sizeof (u_short);
390 	}
391 	switch (il->ilr_type) {
392 
393 #ifdef INET
394 	case ILPUP_IPTYPE:
395 		schednetisr(NETISR_IP);
396 		inq = &ipintrq;
397 		break;
398 #endif
399 	default:
400 		m_freem(m);
401 		goto setup;
402 	}
403 
404 	if (IF_QFULL(inq)) {
405 		IF_DROP(inq);
406 		m_freem(m);
407 		goto setup;
408 	}
409 	IF_ENQUEUE(inq, m);
410 
411 setup:
412 	/*
413 	 * Reset for next packet if possible.
414 	 * If waiting for transmit command completion, set flag
415 	 * and wait until command completes.
416 	 */
417 	if (is->is_flags & ILF_OACTIVE) {
418 		is->is_flags |= ILF_RCVPENDING;
419 		return;
420 	}
421 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
422 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
423 	addr->il_csr =
424 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
425 	while ((addr->il_csr & IL_CDONE) == 0)
426 		;
427 }
428 
429 /*
430  * Ethernet output routine.
431  * Encapsulate a packet of type family for the local net.
432  * Use trailer local net encapsulation if enough data in first
433  * packet leaves a multiple of 512 bytes of data in remainder.
434  */
435 iloutput(ifp, m0, dst)
436 	struct ifnet *ifp;
437 	struct mbuf *m0;
438 	struct sockaddr *dst;
439 {
440 	int type, dest, s, error;
441 	register struct il_softc *is = &il_softc[ifp->if_unit];
442 	register struct mbuf *m = m0;
443 	register struct il_xheader *il;
444 	register int off, i;
445 
446 	switch (dst->sa_family) {
447 
448 #ifdef INET
449 	case AF_INET:
450 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
451 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
452 		if (off > 0 && (off & 0x1ff) == 0 &&
453 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
454 			type = ILPUP_TRAIL + (off>>9);
455 			m->m_off -= 2 * sizeof (u_short);
456 			m->m_len += 2 * sizeof (u_short);
457 			*mtod(m, u_short *) = ILPUP_IPTYPE;
458 			*(mtod(m, u_short *) + 1) = m->m_len;
459 			goto gottrailertype;
460 		}
461 		type = ILPUP_IPTYPE;
462 		off = 0;
463 		goto gottype;
464 #endif
465 
466 	default:
467 		printf("il%d: can't handle af%d\n", ifp->if_unit,
468 			dst->sa_family);
469 		error = EAFNOSUPPORT;
470 		goto bad;
471 	}
472 
473 gottrailertype:
474 	/*
475 	 * Packet to be sent as trailer: move first packet
476 	 * (control information) to end of chain.
477 	 */
478 	while (m->m_next)
479 		m = m->m_next;
480 	m->m_next = m0;
481 	m = m0->m_next;
482 	m0->m_next = 0;
483 	m0 = m;
484 
485 gottype:
486 	/*
487 	 * Add local net header.  If no space in first mbuf,
488 	 * allocate another.
489 	 */
490 	if (m->m_off > MMAXOFF ||
491 	    MMINOFF + sizeof (struct il_xheader) > m->m_off) {
492 		m = m_get(M_DONTWAIT);
493 		if (m == 0) {
494 			error = ENOBUFS;
495 			goto bad;
496 		}
497 		m->m_next = m0;
498 		m->m_off = MMINOFF;
499 		m->m_len = sizeof (struct il_xheader);
500 	} else {
501 		m->m_off -= sizeof (struct il_xheader);
502 		m->m_len += sizeof (struct il_xheader);
503 	}
504 	il = mtod(m, struct il_xheader *);
505 	if ((dest &~ 0xff) == 0)
506 		bcopy(ilbroadcastaddr, il->ilx_dhost, 6);
507 	else {
508 		u_char *to = dest & 0x8000 ? is->is_stats.ils_addr : il_ectop;
509 
510 		bcopy(to, il->ilx_dhost, 3);
511 		il->ilx_dhost[3] = (dest>>8) & 0x7f;
512 		il->ilx_dhost[4] = (dest>>16) & 0xff;
513 		il->ilx_dhost[5] = (dest>>24) & 0xff;
514 	}
515 	il->ilx_type = type;
516 
517 	/*
518 	 * Queue message on interface, and start output if interface
519 	 * not yet active.
520 	 */
521 	s = splimp();
522 	if (IF_QFULL(&ifp->if_snd)) {
523 		IF_DROP(&ifp->if_snd);
524 		splx(s);
525 		m_freem(m);
526 		return (ENOBUFS);
527 	}
528 	IF_ENQUEUE(&ifp->if_snd, m);
529 	if ((is->is_flags & ILF_OACTIVE) == 0)
530 		ilstart(ifp->if_unit);
531 	splx(s);
532 	return (0);
533 
534 bad:
535 	m_freem(m0);
536 	return (error);
537 }
538 
539 /*
540  * Watchdog routine, request statistics from board.
541  */
542 ilwatch(unit)
543 	int unit;
544 {
545 	register struct il_softc *is = &il_softc[unit];
546 	register struct ifnet *ifp = &is->is_if;
547 	int s;
548 
549 	if (is->is_flags & ILF_STATPENDING) {
550 		ifp->if_timer = is->is_scaninterval;
551 		return;
552 	}
553 	s = splimp();
554 	is->is_flags |= ILF_STATPENDING;
555 	if ((is->is_flags & ILF_OACTIVE) == 0)
556 		ilstart(ifp->if_unit);
557 	splx(s);
558 	ifp->if_timer = is->is_scaninterval;
559 }
560 
561 /*
562  * Total up the on-board statistics.
563  */
564 iltotal(is)
565 	register struct il_softc *is;
566 {
567 	register u_short *interval, *sum, *end;
568 
569 	interval = &is->is_stats.ils_frames;
570 	sum = &is->is_sum.ils_frames;
571 	end = is->is_sum.ils_fill2;
572 	while (sum < end)
573 		*sum++ += *interval++;
574 	is->is_if.if_collisions = is->is_sum.ils_collis;
575 }
576