xref: /csrg-svn/sys/vax/if/if_il.c (revision 7220)
1 /*	if_il.c	4.7	82/06/18	*/
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();
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 	short	is_oactive;		/* is output active? */
61 	short	is_startrcv;		/* hang receive next chance */
62 	u_char	is_enaddr[6];		/* board's ethernet address */
63 } il_softc[NIL];
64 
65 /*
66  * Do an OFFLINE command.  This will cause an interrupt for the
67  * autoconfigure stuff.
68  */
69 ilprobe(reg)
70 	caddr_t reg;
71 {
72 	register int br, cvec;		/* r11, r10 value-result */
73 	register struct ildevice *addr = (struct ildevice *)reg;
74 	register i;
75 
76 COUNT(ILPROBE);
77 #ifdef lint
78 	br = 0; cvec = br; br = cvec;
79 	ilrint(0); ilcint(0);
80 #endif
81 
82 	addr->il_csr = ILC_OFFLINE|IL_CIE;
83 	DELAY(100000);
84 	i = addr->il_csr;		/* Clear CDONE */
85 	if (cvec > 0 && cvec != 0x200)
86 		cvec -= 4;
87 	return (1);
88 }
89 
90 struct il_stat ilbuf;
91 /*
92  * Interface exists: make available by filling in network interface
93  * record.  System will initialize the interface when it is ready
94  * to accept packets.  A STATUS command is done to get the ethernet
95  * address and other interesting data.
96  */
97 ilattach(ui)
98 	struct uba_device *ui;
99 {
100 	register struct il_softc *is = &il_softc[ui->ui_unit];
101 	register struct ifnet *ifp = &is->is_if;
102 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
103 	register short csr;
104 	struct sockaddr_in *sin;
105 	int i, s, ubaddr;
106 COUNT(ILATTACH);
107 
108 	ifp->if_unit = ui->ui_unit;
109 	ifp->if_name = "il";
110 	ifp->if_mtu = ILMTU;
111 	ifp->if_net = ui->ui_flags;
112 
113 	/*
114 	 * Reset the board
115 	 */
116 	s = splimp();
117 	csr = ((ubaddr>>2)&0xc000)|ILC_RESET;
118 	addr->il_csr = csr;
119 	do
120 		csr = addr->il_csr;
121 	while ((csr&IL_CDONE) == 0);
122 	if (csr &= IL_STATUS)
123 		printf("il%d: %s\n", ui->ui_unit, ildiag[csr]);
124 	splx(s);
125 
126 	/*
127 	 * Map the status buffer to the Unibus, do the status command,
128 	 * and unmap the buffer.
129 	 */
130 	ubaddr = uballoc(ui->ui_ubanum, &ilbuf, sizeof(ilbuf), 0);
131 	s = splimp();
132 	addr->il_bar = ubaddr & 0xffff;
133 	addr->il_bcr = sizeof(ilbuf);
134 	csr = ((ubaddr>>2)&0xc000)|ILC_STAT;
135 	addr->il_csr = csr;
136 	do
137 		csr = addr->il_csr;
138 	while ((csr&IL_CDONE) == 0);
139 	if (csr &= IL_STATUS)
140 		printf("il%d: %s\n", ui->ui_unit, ilerrs[csr]);
141 	splx(s);
142 	ubarelse(ui->ui_ubanum, &ubaddr);
143 	/*
144 	 * Fill in the Ethernet address from the status buffer
145 	 */
146 	bcopy(ilbuf.ils_addr, is->is_enaddr, 6);
147 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
148 		ui->ui_unit,
149 		is->is_enaddr[0]&0xff, is->is_enaddr[1]&0xff,
150 		is->is_enaddr[2]&0xff, is->is_enaddr[3]&0xff,
151 		is->is_enaddr[4]&0xff, is->is_enaddr[5]&0xff,
152 		ilbuf.ils_module, ilbuf.ils_firmware);
153 	ifp->if_host[0] = ((is->is_enaddr[3]&0xff)<<16) | 0x800000 |
154 	    ((is->is_enaddr[4]&0xff)<<8) | (is->is_enaddr[5]&0xff);
155 	sin = (struct sockaddr_in *)&ifp->if_addr;
156 	sin->sin_family = AF_INET;
157 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
158 
159 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
160 	sin->sin_family = AF_INET;
161 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
162 	ifp->if_flags = IFF_BROADCAST;
163 
164 	ifp->if_init = ilinit;
165 	ifp->if_output = iloutput;
166 	ifp->if_ubareset = ilreset;
167 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
168 #ifdef notdef
169 	is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
170 #endif
171 	if_attach(ifp);
172 }
173 
174 /*
175  * Reset of interface after UNIBUS reset.
176  * If interface is on specified uba, reset its state.
177  */
178 ilreset(unit, uban)
179 	int unit, uban;
180 {
181 	register struct uba_device *ui;
182 COUNT(ILRESET);
183 
184 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
185 	    ui->ui_ubanum != uban)
186 		return;
187 	printf(" il%d", unit);
188 	ilinit(unit);
189 }
190 
191 /*
192  * Initialization of interface; clear recorded pending
193  * operations, and reinitialize UNIBUS usage.
194  */
195 ilinit(unit)
196 	int unit;
197 {
198 	register struct il_softc *is = &il_softc[unit];
199 	register struct uba_device *ui = ilinfo[unit];
200 	register struct ildevice *addr;
201 	int i, s;
202 	short csr;
203 
204 	if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
205 	    sizeof (struct il_rheader), (int)btoc(ILMTU)) == 0) {
206 		printf("il%d: can't initialize\n", unit);
207 		is->is_if.if_flags &= ~IFF_UP;
208 		return;
209 	}
210 	addr = (struct ildevice *)ui->ui_addr;
211 
212 	/*
213 	 * Set board online.
214 	 * Hang receive buffer and start any pending
215 	 * writes by faking a transmit complete.
216 	 * Receive bcr is not a muliple of 4 so buffer
217 	 * chaining can't happen.
218 	 */
219 	s = splimp();
220 	addr->il_csr = ILC_ONLINE;
221 	while ((addr->il_csr & IL_CDONE) == 0)
222 		;
223 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
224 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
225 	csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|ILC_RCV|IL_RIE;
226 	addr->il_csr = csr;
227 	while ((addr->il_csr & IL_CDONE) == 0)
228 		;
229 	is->is_startrcv = 0;
230 	is->is_oactive = 1;
231 	is->is_if.if_flags |= IFF_UP;
232 	ilcint(unit);
233 	splx(s);
234 	if_rtinit(&is->is_if, RTF_UP);
235 }
236 
237 /*
238  * Start output on interface.
239  * Get another datagram to send off of the interface queue,
240  * and map it to the interface before starting the output.
241  */
242 ilstart(dev)
243 	dev_t dev;
244 {
245         int unit = ILUNIT(dev), dest, len;
246 	struct uba_device *ui = ilinfo[unit];
247 	register struct il_softc *is = &il_softc[unit];
248 	register struct ildevice *addr;
249 	struct mbuf *m;
250 	short csr;
251 COUNT(ILSTART);
252 
253 	IF_DEQUEUE(&is->is_if.if_snd, m);
254 	if (m == 0)
255 		return;
256 	len = if_wubaput(&is->is_ifuba, m);
257 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
258 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
259 	addr = (struct ildevice *)ui->ui_addr;
260 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
261 	addr->il_bcr = len;
262 	csr = ((is->is_ifuba.ifu_w.ifrw_info>>2)&0xc000)|ILC_XMIT|IL_CIE|IL_RIE;
263 	addr->il_csr = csr;
264 	is->is_oactive = 1;
265 }
266 
267 /*
268  * Command done interrupt.
269  * This should only happen after a transmit command,
270  * so it is equivalent to a transmit interrupt.
271  * Start another output if more data to send.
272  */
273 ilcint(unit)
274 	int unit;
275 {
276 	register struct il_softc *is = &il_softc[unit];
277 	struct uba_device *ui = ilinfo[unit];
278 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
279 	short csr;
280 COUNT(ILCINT);
281 
282 	csr = addr->il_csr;
283 	if (is->is_oactive == 0) {
284 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
285 			csr, IL_BITS);
286 		return;
287 	}
288 	is->is_if.if_opackets++;
289 	is->is_oactive = 0;
290 	if (csr &= IL_STATUS) {
291 		is->is_if.if_oerrors++;
292 		printf("il%d: output error %d\n", unit, csr);
293 	}
294 
295 	/*
296 	 * Hang receive buffer if it couldn't be done earlier (in ilrint).
297 	 */
298 	if (is->is_startrcv) {
299 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
300 		addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
301 		csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|ILC_RCV|IL_RIE;
302 		addr->il_csr = csr;
303 		while ((addr->il_csr & IL_CDONE) == 0)
304 			;
305 		is->is_startrcv = 0;
306 	}
307 	if (is->is_ifuba.ifu_xtofree) {
308 		m_freem(is->is_ifuba.ifu_xtofree);
309 		is->is_ifuba.ifu_xtofree = 0;
310 	}
311 	if (is->is_if.if_snd.ifq_head)
312 		ilstart(unit);
313 }
314 
315 /*
316  * Ethernet interface receiver interrupt.
317  * If input error just drop packet.
318  * Otherwise purge input buffered data path and examine
319  * packet to determine type.  If can't determine length
320  * from type, then have to drop packet.  Othewise decapsulate
321  * packet based on type and pass to type specific higher-level
322  * input routine.
323  */
324 ilrint(unit)
325 	int unit;
326 {
327 	register struct il_softc *is = &il_softc[unit];
328 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
329 	register struct il_rheader *il;
330     	struct mbuf *m;
331 	int len, off, resid;
332 	register struct ifqueue *inq;
333 	short csr;
334 
335 COUNT(ILRINT);
336 	is->is_if.if_ipackets++;
337 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
338 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
339 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
340 	len = il->ilr_length - sizeof(struct il_rheader);
341 	if ((il->ilr_status&0x3) || len < 46 || len > ILMTU) {
342 		is->is_if.if_ierrors++;
343 #ifdef notdef
344 		if (is->is_if.if_ierrors % 100 == 0)
345 			printf("il%d: += 100 input errors\n", unit);
346 #endif
347 		printf("il%d: input error (status=%x, len=%d)\n", unit,
348 		    il->ilr_status, len);
349 		goto setup;
350 	}
351 
352 	/*
353 	 * Deal with trailer protocol: if type is PUP trailer
354 	 * get true type from first 16-bit word past data.
355 	 * Remember that type was trailer by setting off.
356 	 */
357 #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
358 	if (il->ilr_type >= ILPUP_TRAIL &&
359 	    il->ilr_type < ILPUP_TRAIL+ILPUP_NTRAILER) {
360 		off = (il->ilr_type - ILPUP_TRAIL) * 512;
361 		if (off >= ILMTU)
362 			goto setup;		/* sanity */
363 		il->ilr_type = *ildataaddr(il, off, u_short *);
364 		resid = *(ildataaddr(il, off+2, u_short *));
365 		if (off + resid > len)
366 			goto setup;		/* sanity */
367 		len = off + resid;
368 	} else
369 		off = 0;
370 	if (len == 0)
371 		goto setup;
372 
373 	/*
374 	 * Pull packet off interface.  Off is nonzero if packet
375 	 * has trailing header; ilget will then force this header
376 	 * information to be at the front, but we still have to drop
377 	 * the type and length which are at the front of any trailer data.
378 	 */
379 	m = if_rubaget(&is->is_ifuba, len, off);
380 	if (m == 0)
381 		goto setup;
382 	if (off) {
383 		m->m_off += 2 * sizeof (u_short);
384 		m->m_len -= 2 * sizeof (u_short);
385 	}
386 	switch (il->ilr_type) {
387 
388 #ifdef INET
389 	case ILPUP_IPTYPE:
390 		schednetisr(NETISR_IP);
391 		inq = &ipintrq;
392 		break;
393 #endif
394 	default:
395 		m_freem(m);
396 		goto setup;
397 	}
398 
399 	if (IF_QFULL(inq)) {
400 		IF_DROP(inq);
401 		m_freem(m);
402 		goto setup;
403 	}
404 	IF_ENQUEUE(inq, m);
405 
406 setup:
407 	/*
408 	 * Reset for next packet if possible.
409 	 * If waiting for transmit command completion, set flag
410 	 * and wait until command completes.
411 	 */
412 	if (is->is_oactive) {
413 		is->is_startrcv = 1;
414 		return;
415 	}
416 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
417 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
418 	csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|ILC_RCV|IL_RIE;
419 	addr->il_csr = csr;
420 	while ((addr->il_csr & IL_CDONE) == 0)
421 		;
422 }
423 
424 /*
425  * Ethernet output routine.
426  * Encapsulate a packet of type family for the local net.
427  * Use trailer local net encapsulation if enough data in first
428  * packet leaves a multiple of 512 bytes of data in remainder.
429  */
430 iloutput(ifp, m0, dst)
431 	struct ifnet *ifp;
432 	struct mbuf *m0;
433 	struct sockaddr *dst;
434 {
435 	int type, dest, s, error;
436 	register struct il_softc *is = &il_softc[ifp->if_unit];
437 	register struct mbuf *m = m0;
438 	register struct il_xheader *il;
439 	register int off, i;
440 
441 COUNT(ILOUTPUT);
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_enaddr : 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_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