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