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