xref: /csrg-svn/sys/vax/if/if_il.c (revision 7164)
1 /*	if_il.c	4.6	82/06/13	*/
2 
3 #include "il.h"
4 
5 /*
6  * Interlan Ethernet Communications Controller interface
7  */
8 
9 #include "../h/param.h"
10 #include "../h/systm.h"
11 #include "../h/mbuf.h"
12 #include "../h/pte.h"
13 #include "../h/buf.h"
14 #include "../h/protosw.h"
15 #include "../h/socket.h"
16 #include "../h/ubareg.h"
17 #include "../h/ubavar.h"
18 #include "../h/ilreg.h"
19 #include "../h/cpu.h"
20 #include "../h/mtpr.h"
21 #include "../h/vmmac.h"
22 #include "../net/in.h"
23 #include "../net/in_systm.h"
24 #include "../net/if.h"
25 #include "../net/if_il.h"
26 #include "../net/if_uba.h"
27 #include "../net/ip.h"
28 #include "../net/ip_var.h"
29 #include "../net/pup.h"
30 #include "../net/route.h"
31 #include <errno.h>
32 
33 #define	ILMTU	1500
34 
35 int	ilprobe(), ilattach(), ilrint(), ilcint();
36 struct	uba_device *ilinfo[NIL];
37 u_short ilstd[] = { 0 };
38 struct	uba_driver ildriver =
39 	{ ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
40 u_char	il_ectop[3] = { 0x02, 0x60, 0x8c };
41 #define	ILUNIT(x)	minor(x)
42 
43 int	ilinit(),iloutput(),ilreset();
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 sockaddr_in *sin;
102 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
103 	register int i;
104 	int s;
105 	int ubaddr;
106 COUNT(ILATTACH);
107 
108 	is->is_if.if_unit = ui->ui_unit;
109 	is->is_if.if_name = "il";
110 	is->is_if.if_mtu = ILMTU;
111 	is->is_if.if_net = ui->ui_flags;
112 
113 	/*
114 	 * Reset the board
115 	 */
116 	s = splimp();
117 	addr->il_csr = ((ubaddr>>2)&0xc000)|ILC_RESET;
118 	while (!(addr->il_csr & IL_CDONE))
119 		/* Busy wait */;
120 	if (addr->il_csr & IL_STATUS)
121 		printf("il%d: %s\n", ui->ui_unit,
122 		    ildiag[addr->il_csr & IL_STATUS]);
123 	splx(s);
124 
125 	/*
126 	 * Map the status buffer to the Unibus, do the status command,
127 	 * and unmap the buffer.
128 	 */
129 	ubaddr = uballoc(ui->ui_ubanum, &ilbuf, sizeof(ilbuf), 0);
130 	s = splimp();
131 	addr->il_bar = ubaddr & 0xffff;
132 	addr->il_bcr = sizeof(ilbuf);
133 	addr->il_csr = ((ubaddr>>2)&0xc000)|ILC_STAT;
134 	while (!(addr->il_csr & IL_CDONE))
135 		/* Busy wait */;
136 	if (addr->il_csr & IL_STATUS)
137 		printf("il%d: %s\n", ui->ui_unit,
138 		    ilerrs[addr->il_csr & IL_STATUS]);
139 	splx(s);
140 	ubarelse(ui->ui_ubanum, &ubaddr);
141 	/*
142 	 * Fill in the Ethernet address from the status buffer
143 	 */
144 	for (i=0; i<6; i++)
145 		is->is_enaddr[i] = ilbuf.ils_addr[i];
146 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
147 		ui->ui_unit,
148 		is->is_enaddr[0]&0xff, is->is_enaddr[1]&0xff,
149 		is->is_enaddr[2]&0xff, is->is_enaddr[3]&0xff,
150 		is->is_enaddr[4]&0xff, is->is_enaddr[5]&0xff,
151 		ilbuf.ils_module, ilbuf.ils_firmware);
152 	is->is_if.if_host[0] = ((is->is_enaddr[3]&0xff)<<16) | 0x800000 |
153 	    ((is->is_enaddr[4]&0xff)<<8) | (is->is_enaddr[5]&0xff);
154 	sin = (struct sockaddr_in *)&is->is_if.if_addr;
155 	sin->sin_family = AF_INET;
156 	sin->sin_addr = if_makeaddr(is->is_if.if_net, is->is_if.if_host[0]);
157 
158 	sin = (struct sockaddr_in *)&is->is_if.if_broadaddr;
159 	sin->sin_family = AF_INET;
160 	sin->sin_addr = if_makeaddr(is->is_if.if_net, 0);
161 	is->is_if.if_flags = IFF_BROADCAST;
162 
163 	is->is_if.if_init = ilinit;
164 	is->is_if.if_output = iloutput;
165 	is->is_if.if_ubareset = ilreset;
166 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
167 	if_attach(&is->is_if);
168 }
169 
170 /*
171  * Reset of interface after UNIBUS reset.
172  * If interface is on specified uba, reset its state.
173  */
174 ilreset(unit, uban)
175 	int unit, uban;
176 {
177 	register struct uba_device *ui;
178 COUNT(ILRESET);
179 
180 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
181 	    ui->ui_ubanum != uban)
182 		return;
183 	printf(" il%d", unit);
184 	ilinit(unit);
185 }
186 
187 /*
188  * Initialization of interface; clear recorded pending
189  * operations, and reinitialize UNIBUS usage.
190  */
191 ilinit(unit)
192 	int unit;
193 {
194 	register struct il_softc *is = &il_softc[unit];
195 	register struct uba_device *ui = ilinfo[unit];
196 	register struct ildevice *addr;
197 	register i;
198 	int s;
199 
200 	if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
201 	    sizeof (struct il_rheader), (int)btoc(ILMTU)) == 0) {
202 		printf("il%d: can't initialize\n", unit);
203 		is->is_if.if_flags &= ~IFF_UP;
204 		return;
205 	}
206 	addr = (struct ildevice *)ui->ui_addr;
207 
208 	/*
209 	 * Set board online.
210 	 * Hang receive buffer and start any pending
211 	 * writes by faking a transmit complete.
212 	 * Receive bcr is not a muliple of 4 so buffer
213 	 * chaining can't happen.
214 	 */
215 	s = splimp();
216 	addr->il_csr = ILC_ONLINE;
217 	while (!(addr->il_csr & IL_CDONE))
218 		/* Busy wait */;
219 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
220 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
221 	addr->il_csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|
222 			ILC_RCV|IL_RIE;
223 	while (!(addr->il_csr & IL_CDONE))
224 		/* Busy wait */;
225 	is->is_startrcv = 0;
226 	is->is_oactive = 1;
227 	is->is_if.if_flags |= IFF_UP;
228 	ilcint(unit);
229 	splx(s);
230 	if_rtinit(&is->is_if, RTF_UP);
231 }
232 
233 /*
234  * Start output on interface.
235  * Get another datagram to send off of the interface queue,
236  * and map it to the interface before starting the output.
237  */
238 ilstart(dev)
239 	dev_t dev;
240 {
241         int unit = ILUNIT(dev);
242 	struct uba_device *ui = ilinfo[unit];
243 	register struct il_softc *is = &il_softc[unit];
244 	register struct ildevice *addr;
245 	register len;
246 	struct mbuf *m;
247 	int dest;
248 COUNT(ILSTART);
249 
250 	/*
251 	 * Dequeue another request and copy it into the buffer.
252 	 * If no more requests, just return.
253 	 */
254 	IF_DEQUEUE(&is->is_if.if_snd, m);
255 	if (m == 0)
256 		return;
257 	len = if_wubaput(&is->is_ifuba, m);
258 
259 	/*
260 	 * Flush BDP, then start the output.
261 	 */
262 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
263 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
264 	addr = (struct ildevice *)ui->ui_addr;
265 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
266 	addr->il_bcr = len;
267 	addr->il_csr = ((is->is_ifuba.ifu_w.ifrw_info>>2)&0xc000)|
268 			ILC_XMIT|IL_CIE|IL_RIE;
269 	is->is_oactive = 1;
270 }
271 
272 /*
273  * Command done interrupt.
274  * This should only happen after a transmit command,
275  * so it is equivalent to a transmit interrupt.
276  * Start another output if more data to send.
277  */
278 ilcint(unit)
279 	int unit;
280 {
281 	register struct uba_device *ui = ilinfo[unit];
282 	register struct il_softc *is = &il_softc[unit];
283 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
284 	register int err;
285 COUNT(ILCINT);
286 
287 	if (is->is_oactive == 0) {
288 		printf("il%d: strange xmit interrupt!\n", unit);
289 		return;
290 	}
291 	is->is_if.if_opackets++;
292 	is->is_oactive = 0;
293 	if (err = (addr->il_csr & IL_STATUS)){
294 		is->is_if.if_oerrors++;
295 		printf("il%d: output error %d\n", unit, err);
296 	}
297 	/*
298 	 * Hang receive buffer if it couldn't be done earlier (in ilrint).
299 	 */
300 	if (is->is_startrcv) {
301 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
302 		addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
303 		addr->il_csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|
304 				ILC_RCV|IL_RIE;
305 		while (!(addr->il_csr & IL_CDONE))
306 			/* Busy wait */;
307 		is->is_startrcv = 0;
308 	}
309 	if (is->is_ifuba.ifu_xtofree) {
310 		m_freem(is->is_ifuba.ifu_xtofree);
311 		is->is_ifuba.ifu_xtofree = 0;
312 	}
313 	if (is->is_if.if_snd.ifq_head == 0) {
314 		return;
315 	}
316 	ilstart(unit);
317 }
318 
319 /*
320  * Ethernet interface receiver interrupt.
321  * If input error just drop packet.
322  * Otherwise purge input buffered data path and examine
323  * packet to determine type.  If can't determine length
324  * from type, then have to drop packet.  Othewise decapsulate
325  * packet based on type and pass to type specific higher-level
326  * input routine.
327  */
328 ilrint(unit)
329 	int unit;
330 {
331 	register struct il_softc *is = &il_softc[unit];
332 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
333 	register struct il_rheader *il;
334     	struct mbuf *m;
335 	int len, off, resid;
336 	register struct ifqueue *inq;
337 
338 COUNT(ILRINT);
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&0x3 || 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 		printf("il%d: input error (status=%x, len=%d)\n", unit,
351 		    il->ilr_status, len);
352 		goto setup;
353 	}
354 
355 	/*
356 	 * Deal with trailer protocol: if type is PUP trailer
357 	 * get true type from first 16-bit word past data.
358 	 * Remember that type was trailer by setting off.
359 	 */
360 #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
361 	if (il->ilr_type >= ILPUP_TRAIL &&
362 	    il->ilr_type < ILPUP_TRAIL+ILPUP_NTRAILER) {
363 		off = (il->ilr_type - ILPUP_TRAIL) * 512;
364 		if (off >= ILMTU)
365 			goto setup;		/* sanity */
366 		il->ilr_type = *ildataaddr(il, off, u_short *);
367 		resid = *(ildataaddr(il, off+2, u_short *));
368 		if (off + resid > len)
369 			goto setup;		/* sanity */
370 		len = off + resid;
371 	} else
372 		off = 0;
373 	if (len == 0)
374 		goto setup;
375 
376 	/*
377 	 * Pull packet off interface.  Off is nonzero if packet
378 	 * has trailing header; ilget will then force this header
379 	 * information to be at the front, but we still have to drop
380 	 * the type and length which are at the front of any trailer data.
381 	 */
382 	m = if_rubaget(&is->is_ifuba, len, off);
383 	if (m == 0)
384 		goto setup;
385 	if (off) {
386 		m->m_off += 2 * sizeof (u_short);
387 		m->m_len -= 2 * sizeof (u_short);
388 	}
389 	switch (il->ilr_type) {
390 
391 #ifdef INET
392 	case ILPUP_IPTYPE:
393 		schednetisr(NETISR_IP);
394 		inq = &ipintrq;
395 		break;
396 #endif
397 	default:
398 		m_freem(m);
399 		goto setup;
400 	}
401 
402 	if (IF_QFULL(inq)) {
403 		IF_DROP(inq);
404 		m_freem(m);
405 	} else
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_oactive) {
415 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
416 		addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
417 		addr->il_csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|
418 				ILC_RCV|IL_RIE;
419 		while (!(addr->il_csr & IL_CDONE))
420 			/* Busy wait */;
421 	} else
422 		is->is_startrcv = 1;
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;
441 	register int i;
442 
443 COUNT(ILOUTPUT);
444 	switch (dst->sa_family) {
445 
446 #ifdef INET
447 	case AF_INET:
448 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
449 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
450 		if (off > 0 && (off & 0x1ff) == 0 &&
451 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
452 			type = ILPUP_TRAIL + (off>>9);
453 			m->m_off -= 2 * sizeof (u_short);
454 			m->m_len += 2 * sizeof (u_short);
455 			*mtod(m, u_short *) = ILPUP_IPTYPE;
456 			*(mtod(m, u_short *) + 1) = m->m_len;
457 			goto gottrailertype;
458 		}
459 		type = ILPUP_IPTYPE;
460 		off = 0;
461 		goto gottype;
462 #endif
463 
464 	default:
465 		printf("il%d: can't handle af%d\n", ifp->if_unit,
466 			dst->sa_family);
467 		error = EAFNOSUPPORT;
468 		goto bad;
469 	}
470 
471 gottrailertype:
472 	/*
473 	 * Packet to be sent as trailer: move first packet
474 	 * (control information) to end of chain.
475 	 */
476 	while (m->m_next)
477 		m = m->m_next;
478 	m->m_next = m0;
479 	m = m0->m_next;
480 	m0->m_next = 0;
481 	m0 = m;
482 
483 gottype:
484 	/*
485 	 * Add local net header.  If no space in first mbuf,
486 	 * allocate another.
487 	 */
488 	if (m->m_off > MMAXOFF ||
489 	    MMINOFF + sizeof (struct il_xheader) > m->m_off) {
490 		m = m_get(M_DONTWAIT);
491 		if (m == 0) {
492 			error = ENOBUFS;
493 			goto bad;
494 		}
495 		m->m_next = m0;
496 		m->m_off = MMINOFF;
497 		m->m_len = sizeof (struct il_xheader);
498 	} else {
499 		m->m_off -= sizeof (struct il_xheader);
500 		m->m_len += sizeof (struct il_xheader);
501 	}
502 	il = mtod(m, struct il_xheader *);
503 	if ((dest &~ 0xff) == 0)
504 		for (i=0; i<6; i++)
505 			il->ilx_dhost[i] = 0xff;
506 	else {
507 		if (dest & 0x8000) {
508 			il->ilx_dhost[0] = is->is_enaddr[0];
509 			il->ilx_dhost[1] = is->is_enaddr[1];
510 			il->ilx_dhost[2] = is->is_enaddr[2];
511 		} else {
512 			il->ilx_dhost[0] = il_ectop[0];
513 			il->ilx_dhost[1] = il_ectop[1];
514 			il->ilx_dhost[2] = il_ectop[2];
515 		}
516 		il->ilx_dhost[3] = (dest>>8) & 0x7f;
517 		il->ilx_dhost[4] = (dest>>16) & 0xff;
518 		il->ilx_dhost[5] = (dest>>24) & 0xff;
519 	}
520 	il->ilx_type = type;
521 
522 	/*
523 	 * Queue message on interface, and start output if interface
524 	 * not yet active.
525 	 */
526 	s = splimp();
527 	if (IF_QFULL(&ifp->if_snd)) {
528 		IF_DROP(&ifp->if_snd);
529 		error = ENOBUFS;
530 		goto qfull;
531 	}
532 	IF_ENQUEUE(&ifp->if_snd, m);
533 	if (is->is_oactive == 0)
534 		ilstart(ifp->if_unit);
535 	splx(s);
536 	return (0);
537 qfull:
538 	m0 = m;
539 	splx(s);
540 bad:
541 	m_freem(m0);
542 	return(error);
543 }
544