xref: /csrg-svn/sys/vax/if/if_ec.c (revision 24227)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)if_ec.c	6.11 (Berkeley) 08/09/85
7  */
8 
9 #include "ec.h"
10 
11 /*
12  * 3Com Ethernet Controller interface
13  */
14 #include "../machine/pte.h"
15 
16 #include "param.h"
17 #include "systm.h"
18 #include "mbuf.h"
19 #include "buf.h"
20 #include "protosw.h"
21 #include "socket.h"
22 #include "vmmac.h"
23 #include "ioctl.h"
24 #include "errno.h"
25 
26 #include "../net/if.h"
27 #include "../net/netisr.h"
28 #include "../net/route.h"
29 
30 #ifdef INET
31 #include "../netinet/in.h"
32 #include "../netinet/in_systm.h"
33 #include "../netinet/in_var.h"
34 #include "../netinet/ip.h"
35 #include "../netinet/ip_var.h"
36 #include "../netinet/if_ether.h"
37 #endif
38 
39 #ifdef PUP
40 #include "../netpup/pup.h"
41 #endif PUP
42 
43 #ifdef NS
44 #include "../netns/ns.h"
45 #include "../netns/ns_if.h"
46 #endif
47 
48 #include "../vax/cpu.h"
49 #include "../vax/mtpr.h"
50 #include "if_ecreg.h"
51 #include "if_uba.h"
52 #include "../vaxuba/ubareg.h"
53 #include "../vaxuba/ubavar.h"
54 
55 #if CLSIZE == 2
56 #define ECBUFSIZE	32		/* on-board memory, clusters */
57 #endif
58 
59 int	ecubamem(), ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
60 struct	uba_device *ecinfo[NEC];
61 u_short ecstd[] = { 0 };
62 struct	uba_driver ecdriver =
63 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo, 0, 0, 0, ecubamem };
64 
65 int	ecinit(),ecioctl(),ecoutput(),ecreset();
66 struct	mbuf *ecget();
67 
68 extern struct ifnet loif;
69 
70 /*
71  * Ethernet software status per interface.
72  *
73  * Each interface is referenced by a network interface structure,
74  * es_if, which the routing code uses to locate the interface.
75  * This structure contains the output queue for the interface, its address, ...
76  * We also have, for each interface, a UBA interface structure, which
77  * contains information about the UNIBUS resources held by the interface:
78  * map registers, buffered data paths, etc.  Information is cached in this
79  * structure for use by the if_uba.c routines in running the interface
80  * efficiently.
81  */
82 struct	ec_softc {
83 	struct	arpcom es_ac;		/* common Ethernet structures */
84 #define	es_if	es_ac.ac_if		/* network-visible interface */
85 #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
86 	struct	ifuba es_ifuba;		/* UNIBUS resources */
87 	short	es_mask;		/* mask for current output delay */
88 	short	es_oactive;		/* is output active? */
89 	u_char	*es_buf[16];		/* virtual addresses of buffers */
90 } ec_softc[NEC];
91 
92 #ifdef DEBUG
93 ether_addr(s)
94 char *s;
95 {
96 
97 	printf("%x:%x:%x:%x:%x:%x\n",
98 		s[0]&0xff, s[1]&0xff, s[2]&0xff,
99 		s[3]&0xff, s[4]&0xff, s[5]&0xff);
100 }
101 #endif
102 
103 /*
104  * Configure on-board memory for an interface.
105  * Called from autoconfig and after a uba reset.
106  * The address of the memory on the uba is supplied in the device flags.
107  */
108 ecubamem(ui, uban)
109 	register struct uba_device *ui;
110 {
111 	register caddr_t ecbuf = (caddr_t) &umem[uban][ui->ui_flags];
112 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
113 
114 	/*
115 	 * Make sure csr is there (we run before ecprobe).
116 	 */
117 	if (badaddr((caddr_t)addr, 2))
118 		return (-1);
119 #if VAX780
120 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
121 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
122 		return (-1);
123 	}
124 #endif
125 	/*
126 	 * Make sure memory is turned on
127 	 */
128 	addr->ec_rcr = EC_AROM;
129 	/*
130 	 * Tell the system that the board has memory here, so it won't
131 	 * attempt to allocate the addresses later.
132 	 */
133 	if (ubamem(uban, ui->ui_flags, ECBUFSIZE*CLSIZE, 1) == 0) {
134 		printf("ec%d: cannot reserve uba addresses\n", ui->ui_unit);
135 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
136 		return (-1);
137 	}
138 	/*
139 	 * Check for existence of buffers on Unibus.
140 	 */
141 	if (badaddr((caddr_t)ecbuf, 2)) {
142 bad:
143 		printf("ec%d: buffer mem not found\n", ui->ui_unit);
144 		(void) ubamem(uban, ui->ui_flags, ECBUFSIZE*2, 0);
145 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
146 		return (-1);
147 	}
148 #if VAX780
149 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
150 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
151 		goto bad;
152 	}
153 #endif
154 	if (ui->ui_alive == 0)		/* Only printf from autoconfig */
155 		printf("ec%d: mem %x-%x\n", ui->ui_unit,
156 			ui->ui_flags, ui->ui_flags + ECBUFSIZE*CLBYTES - 1);
157 	ui->ui_type = 1;		/* Memory on, allocated */
158 	return (0);
159 }
160 
161 /*
162  * Do output DMA to determine interface presence and
163  * interrupt vector.  DMA is too short to disturb other hosts.
164  */
165 ecprobe(reg, ui)
166 	caddr_t reg;
167 	struct uba_device *ui;
168 {
169 	register int br, cvec;		/* r11, r10 value-result */
170 	register struct ecdevice *addr = (struct ecdevice *)reg;
171 	register caddr_t ecbuf = (caddr_t) &umem[ui->ui_ubanum][ui->ui_flags];
172 
173 #ifdef lint
174 	br = 0; cvec = br; br = cvec;
175 	ecrint(0); ecxint(0); eccollide(0);
176 #endif
177 
178 	/*
179 	 * Check that buffer memory was found and enabled.
180 	 */
181 	if (ui->ui_type == 0)
182 		return(0);
183 	/*
184 	 * Make a one byte packet in what should be buffer #0.
185 	 * Submit it for sending.  This should cause an xmit interrupt.
186 	 * The xmit interrupt vector is 8 bytes after the receive vector,
187 	 * so adjust for this before returning.
188 	 */
189 	*(u_short *)ecbuf = (u_short) 03777;
190 	ecbuf[03777] = '\0';
191 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
192 	DELAY(100000);
193 	addr->ec_xcr = EC_XCLR;
194 	if (cvec > 0 && cvec != 0x200) {
195 		if (cvec & 04) {	/* collision interrupt */
196 			cvec -= 04;
197 			br += 1;		/* rcv is collision + 1 */
198 		} else {		/* xmit interrupt */
199 			cvec -= 010;
200 			br += 2;		/* rcv is xmit + 2 */
201 		}
202 	}
203 	return (1);
204 }
205 
206 /*
207  * Interface exists: make available by filling in network interface
208  * record.  System will initialize the interface when it is ready
209  * to accept packets.
210  */
211 ecattach(ui)
212 	struct uba_device *ui;
213 {
214 	struct ec_softc *es = &ec_softc[ui->ui_unit];
215 	register struct ifnet *ifp = &es->es_if;
216 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
217 	int i, j;
218 	u_char *cp;
219 
220 	ifp->if_unit = ui->ui_unit;
221 	ifp->if_name = "ec";
222 	ifp->if_mtu = ETHERMTU;
223 
224 	/*
225 	 * Read the ethernet address off the board, one nibble at a time.
226 	 */
227 	addr->ec_xcr = EC_UECLR; /* zero address pointer */
228 	addr->ec_rcr = EC_AROM;
229 	cp = es->es_addr;
230 #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
231 	for (i=0; i < sizeof (es->es_addr); i++) {
232 		*cp = 0;
233 		for (j=0; j<=4; j+=4) {
234 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
235 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
236 		}
237 		cp++;
238 	}
239 #ifdef DEBUG
240 	printf("ecattach %d: addr=",ui->ui_unit);
241 	ether_addr(es->es_addr);
242 #endif
243 	ifp->if_init = ecinit;
244 	ifp->if_ioctl = ecioctl;
245 	ifp->if_output = ecoutput;
246 	ifp->if_reset = ecreset;
247 	ifp->if_flags = IFF_BROADCAST;
248 	for (i=0; i<16; i++)
249 		es->es_buf[i]
250 		    = (u_char *)&umem[ui->ui_ubanum][ui->ui_flags + 2048*i];
251 	if_attach(ifp);
252 }
253 
254 /*
255  * Reset of interface after UNIBUS reset.
256  * If interface is on specified uba, reset its state.
257  */
258 ecreset(unit, uban)
259 	int unit, uban;
260 {
261 	register struct uba_device *ui;
262 
263 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
264 	    ui->ui_ubanum != uban)
265 		return;
266 	printf(" ec%d", unit);
267 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
268 	ecinit(unit);
269 }
270 
271 /*
272  * Initialization of interface; clear recorded pending
273  * operations, and reinitialize UNIBUS usage.
274  */
275 ecinit(unit)
276 	int unit;
277 {
278 	struct ec_softc *es = &ec_softc[unit];
279 	struct ecdevice *addr;
280 	register struct ifnet *ifp = &es->es_if;
281 	int i, s;
282 
283 	/* not yet, if address still unknown */
284 	if (ifp->if_addrlist == (struct ifaddr *)0)
285 		return;
286 
287 	/*
288 	 * Hang receive buffers and start any pending writes.
289 	 * Writing into the rcr also makes sure the memory
290 	 * is turned on.
291 	 */
292 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
293 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
294 		s = splimp();
295 		/*
296 		 * write our ethernet address into the address recognition ROM
297 		 * so we can always use the same EC_READ bits (referencing ROM),
298 		 * in case we change the address sometime.
299 		 * Note that this is safe here as the reciever is NOT armed.
300 		 */
301 		ec_setaddr(es->es_addr, unit);
302 		/*
303 		 * Arm the reciever
304 		 */
305 		for (i = ECRHBF; i >= ECRLBF; i--)
306 			addr->ec_rcr = EC_READ | i;
307 		es->es_oactive = 0;
308 		es->es_mask = ~0;
309 		es->es_if.if_flags |= IFF_RUNNING;
310 		if (es->es_if.if_snd.ifq_head)
311 			ecstart(unit);
312 		splx(s);
313 	}
314 }
315 
316 /*
317  * Start or restart output on interface.
318  * If interface is already active, then this is a retransmit
319  * after a collision, and just restuff registers.
320  * If interface is not already active, get another datagram
321  * to send off of the interface queue, and map it to the interface
322  * before starting the output.
323  */
324 ecstart(unit)
325 {
326 	struct ec_softc *es = &ec_softc[unit];
327 	struct ecdevice *addr;
328 	struct mbuf *m;
329 
330 	if (es->es_oactive)
331 		goto restart;
332 
333 	IF_DEQUEUE(&es->es_if.if_snd, m);
334 	if (m == 0) {
335 		es->es_oactive = 0;
336 		return;
337 	}
338 	ecput(es->es_buf[ECTBF], m);
339 
340 restart:
341 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
342 	addr->ec_xcr = EC_WRITE|ECTBF;
343 	es->es_oactive = 1;
344 }
345 
346 /*
347  * Ethernet interface transmitter interrupt.
348  * Start another output if more data to send.
349  */
350 ecxint(unit)
351 	int unit;
352 {
353 	register struct ec_softc *es = &ec_softc[unit];
354 	register struct ecdevice *addr =
355 		(struct ecdevice *)ecinfo[unit]->ui_addr;
356 
357 	if (es->es_oactive == 0)
358 		return;
359 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
360 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
361 			addr->ec_xcr, EC_XBITS);
362 		es->es_oactive = 0;
363 		addr->ec_xcr = EC_XCLR;
364 		return;
365 	}
366 	es->es_if.if_opackets++;
367 	es->es_oactive = 0;
368 	es->es_mask = ~0;
369 	addr->ec_xcr = EC_XCLR;
370 	if (es->es_if.if_snd.ifq_head)
371 		ecstart(unit);
372 }
373 
374 /*
375  * Collision on ethernet interface.  Do exponential
376  * backoff, and retransmit.  If have backed off all
377  * the way print warning diagnostic, and drop packet.
378  */
379 eccollide(unit)
380 	int unit;
381 {
382 	struct ec_softc *es = &ec_softc[unit];
383 
384 	es->es_if.if_collisions++;
385 	if (es->es_oactive)
386 		ecdocoll(unit);
387 }
388 
389 ecdocoll(unit)
390 	int unit;
391 {
392 	register struct ec_softc *es = &ec_softc[unit];
393 	register struct ecdevice *addr =
394 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
395 	register i;
396 	int delay;
397 
398 	/*
399 	 * Es_mask is a 16 bit number with n low zero bits, with
400 	 * n the number of backoffs.  When es_mask is 0 we have
401 	 * backed off 16 times, and give up.
402 	 */
403 	if (es->es_mask == 0) {
404 		es->es_if.if_oerrors++;
405 		printf("ec%d: send error\n", unit);
406 		/*
407 		 * Reset interface, then requeue rcv buffers.
408 		 * Some incoming packets may be lost, but that
409 		 * can't be helped.
410 		 */
411 		addr->ec_xcr = EC_UECLR;
412 		for (i=ECRHBF; i>=ECRLBF; i--)
413 			addr->ec_rcr = EC_READ|i;
414 		/*
415 		 * Reset and transmit next packet (if any).
416 		 */
417 		es->es_oactive = 0;
418 		es->es_mask = ~0;
419 		if (es->es_if.if_snd.ifq_head)
420 			ecstart(unit);
421 		return;
422 	}
423 	/*
424 	 * Do exponential backoff.  Compute delay based on low bits
425 	 * of the interval timer.  Then delay for that number of
426 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
427 	 * This does not take into account the time already used to
428 	 * process the interrupt.
429 	 */
430 	es->es_mask <<= 1;
431 	delay = mfpr(ICR) &~ es->es_mask;
432 	DELAY(delay * 51);
433 	/*
434 	 * Clear the controller's collision flag, thus enabling retransmit.
435 	 */
436 	addr->ec_xcr = EC_CLEAR;
437 }
438 
439 /*
440  * Ethernet interface receiver interrupt.
441  * If input error just drop packet.
442  * Otherwise examine
443  * packet to determine type.  If can't determine length
444  * from type, then have to drop packet.  Othewise decapsulate
445  * packet based on type and pass to type specific higher-level
446  * input routine.
447  */
448 ecrint(unit)
449 	int unit;
450 {
451 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
452 
453 	while (addr->ec_rcr & EC_RDONE)
454 		ecread(unit);
455 }
456 
457 ecread(unit)
458 	int unit;
459 {
460 	register struct ec_softc *es = &ec_softc[unit];
461 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
462 	register struct ether_header *ec;
463     	struct mbuf *m;
464 	int len, off, resid, ecoff, rbuf;
465 	register struct ifqueue *inq;
466 	u_char *ecbuf;
467 
468 	es->es_if.if_ipackets++;
469 	rbuf = addr->ec_rcr & EC_RBN;
470 	if (rbuf < ECRLBF || rbuf > ECRHBF)
471 		panic("ecrint");
472 	ecbuf = es->es_buf[rbuf];
473 	ecoff = *(short *)ecbuf;
474 	if (ecoff <= ECRDOFF || ecoff > 2046) {
475 		es->es_if.if_ierrors++;
476 #ifdef notdef
477 		if (es->es_if.if_ierrors % 100 == 0)
478 			printf("ec%d: += 100 input errors\n", unit);
479 #endif
480 		goto setup;
481 	}
482 
483 	/*
484 	 * Get input data length.
485 	 * Get pointer to ethernet header (in input buffer).
486 	 * Deal with trailer protocol: if type is trailer type
487 	 * get true type from first 16-bit word past data.
488 	 * Remember that type was trailer by setting off.
489 	 */
490 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
491 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
492 	ec->ether_type = ntohs((u_short)ec->ether_type);
493 #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
494 	if (ec->ether_type >= ETHERTYPE_TRAIL &&
495 	    ec->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
496 		off = (ec->ether_type - ETHERTYPE_TRAIL) * 512;
497 		if (off >= ETHERMTU)
498 			goto setup;		/* sanity */
499 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
500 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
501 		if (off + resid > len)
502 			goto setup;		/* sanity */
503 		len = off + resid;
504 	} else
505 		off = 0;
506 	if (len == 0)
507 		goto setup;
508 
509 	/*
510 	 * Pull packet off interface.  Off is nonzero if packet
511 	 * has trailing header; ecget will then force this header
512 	 * information to be at the front, but we still have to drop
513 	 * the type and length which are at the front of any trailer data.
514 	 */
515 	m = ecget(ecbuf, len, off);
516 	if (m == 0)
517 		goto setup;
518 	if (off) {
519 		m->m_off += 2 * sizeof (u_short);
520 		m->m_len -= 2 * sizeof (u_short);
521 	}
522 	switch (ec->ether_type) {
523 
524 #ifdef INET
525 	case ETHERTYPE_IP:
526 		schednetisr(NETISR_IP);
527 		inq = &ipintrq;
528 		break;
529 
530 	case ETHERTYPE_ARP:
531 		arpinput(&es->es_ac, m);
532 		goto setup;
533 #endif
534 #ifdef NS
535 	case ETHERTYPE_NS:
536 		schednetisr(NETISR_NS);
537 		inq = &nsintrq;
538 		break;
539 
540 #endif
541 	default:
542 		m_freem(m);
543 		goto setup;
544 	}
545 
546 	if (IF_QFULL(inq)) {
547 		IF_DROP(inq);
548 		m_freem(m);
549 		goto setup;
550 	}
551 	IF_ENQUEUE(inq, m);
552 
553 setup:
554 	/*
555 	 * Reset for next packet.
556 	 */
557 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
558 }
559 
560 /*
561  * Ethernet output routine.
562  * Encapsulate a packet of type family for the local net.
563  * Use trailer local net encapsulation if enough data in first
564  * packet leaves a multiple of 512 bytes of data in remainder.
565  * If destination is this address or broadcast, send packet to
566  * loop device to kludge around the fact that 3com interfaces can't
567  * talk to themselves.
568  */
569 ecoutput(ifp, m0, dst)
570 	struct ifnet *ifp;
571 	struct mbuf *m0;
572 	struct sockaddr *dst;
573 {
574 	int type, s, error;
575  	u_char edst[6];
576 	struct in_addr idst;
577 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
578 	register struct mbuf *m = m0;
579 	register struct ether_header *ec;
580 	register int off;
581 	struct mbuf *mcopy = (struct mbuf *)0;
582 
583 	switch (dst->sa_family) {
584 
585 #ifdef INET
586 	case AF_INET:
587 		idst = ((struct sockaddr_in *)dst)->sin_addr;
588 		if (!arpresolve(&es->es_ac, m, &idst, edst))
589 			return (0);	/* if not yet resolved */
590 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
591 		    sizeof(edst)))
592 			mcopy = m_copy(m, 0, (int)M_COPYALL);
593 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
594 		/* need per host negotiation */
595 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
596 		if (off > 0 && (off & 0x1ff) == 0 &&
597 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
598 			type = ETHERTYPE_TRAIL + (off>>9);
599 			m->m_off -= 2 * sizeof (u_short);
600 			m->m_len += 2 * sizeof (u_short);
601 			*mtod(m, u_short *) = ntohs((u_short)ETHERTYPE_IP);
602 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
603 			goto gottrailertype;
604 		}
605 		type = ETHERTYPE_IP;
606 		off = 0;
607 		goto gottype;
608 #endif
609 #ifdef NS
610 	case AF_NS:
611  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
612 		    (caddr_t)edst, sizeof (edst));
613 
614 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,
615 			sizeof(edst))) {
616 
617 				mcopy = m_copy(m, 0, (int)M_COPYALL);
618 		} else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost,
619 			sizeof(edst))) {
620 
621 				return(looutput(&loif, m, dst));
622 		}
623 		type = ETHERTYPE_NS;
624 		off = 0;
625 		goto gottype;
626 #endif
627 
628 	case AF_UNSPEC:
629 		ec = (struct ether_header *)dst->sa_data;
630  		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
631 		type = ec->ether_type;
632 		goto gottype;
633 
634 	default:
635 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
636 			dst->sa_family);
637 		error = EAFNOSUPPORT;
638 		goto bad;
639 	}
640 
641 gottrailertype:
642 	/*
643 	 * Packet to be sent as trailer: move first packet
644 	 * (control information) to end of chain.
645 	 */
646 	while (m->m_next)
647 		m = m->m_next;
648 	m->m_next = m0;
649 	m = m0->m_next;
650 	m0->m_next = 0;
651 	m0 = m;
652 
653 gottype:
654 	/*
655 	 * Add local net header.  If no space in first mbuf,
656 	 * allocate another.
657 	 */
658 	if (m->m_off > MMAXOFF ||
659 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
660 		m = m_get(M_DONTWAIT, MT_HEADER);
661 		if (m == 0) {
662 			error = ENOBUFS;
663 			goto bad;
664 		}
665 		m->m_next = m0;
666 		m->m_off = MMINOFF;
667 		m->m_len = sizeof (struct ether_header);
668 	} else {
669 		m->m_off -= sizeof (struct ether_header);
670 		m->m_len += sizeof (struct ether_header);
671 	}
672 	ec = mtod(m, struct ether_header *);
673  	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
674 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost,
675 	    sizeof(ec->ether_shost));
676 	ec->ether_type = htons((u_short)type);
677 
678 	/*
679 	 * Queue message on interface, and start output if interface
680 	 * not yet active.
681 	 */
682 	s = splimp();
683 	if (IF_QFULL(&ifp->if_snd)) {
684 		IF_DROP(&ifp->if_snd);
685 		error = ENOBUFS;
686 		goto qfull;
687 	}
688 	IF_ENQUEUE(&ifp->if_snd, m);
689 	if (es->es_oactive == 0)
690 		ecstart(ifp->if_unit);
691 	splx(s);
692 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
693 
694 qfull:
695 	m0 = m;
696 	splx(s);
697 bad:
698 	m_freem(m0);
699 	if (mcopy)
700 		m_freem(mcopy);
701 	return (error);
702 }
703 
704 /*
705  * Routine to copy from mbuf chain to transmit
706  * buffer in UNIBUS memory.
707  * If packet size is less than the minimum legal size,
708  * the buffer is expanded.  We probably should zero out the extra
709  * bytes for security, but that would slow things down.
710  */
711 ecput(ecbuf, m)
712 	u_char *ecbuf;
713 	struct mbuf *m;
714 {
715 	register struct mbuf *mp;
716 	register int off;
717 	u_char *bp;
718 
719 	for (off = 2048, mp = m; mp; mp = mp->m_next)
720 		off -= mp->m_len;
721 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
722 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
723 	*(u_short *)ecbuf = off;
724 	bp = (u_char *)(ecbuf + off);
725 	for (mp = m; mp; mp = mp->m_next) {
726 		register unsigned len = mp->m_len;
727 		u_char *mcp;
728 
729 		if (len == 0)
730 			continue;
731 		mcp = mtod(mp, u_char *);
732 		if ((unsigned)bp & 01) {
733 			*bp++ = *mcp++;
734 			len--;
735 		}
736 		if (off = (len >> 1)) {
737 			register u_short *to, *from;
738 
739 			to = (u_short *)bp;
740 			from = (u_short *)mcp;
741 			do
742 				*to++ = *from++;
743 			while (--off > 0);
744 			bp = (u_char *)to,
745 			mcp = (u_char *)from;
746 		}
747 		if (len & 01)
748 			*bp++ = *mcp++;
749 	}
750 	m_freem(m);
751 }
752 
753 /*
754  * Routine to copy from UNIBUS memory into mbufs.
755  * Similar in spirit to if_rubaget.
756  *
757  * Warning: This makes the fairly safe assumption that
758  * mbufs have even lengths.
759  */
760 struct mbuf *
761 ecget(ecbuf, totlen, off0)
762 	u_char *ecbuf;
763 	int totlen, off0;
764 {
765 	register struct mbuf *m;
766 	struct mbuf *top = 0, **mp = &top;
767 	register int off = off0, len;
768 	u_char *cp;
769 
770 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
771 	while (totlen > 0) {
772 		register int words;
773 		u_char *mcp;
774 
775 		MGET(m, M_DONTWAIT, MT_DATA);
776 		if (m == 0)
777 			goto bad;
778 		if (off) {
779 			len = totlen - off;
780 			cp = ecbuf + ECRDOFF +
781 				sizeof (struct ether_header) + off;
782 		} else
783 			len = totlen;
784 		if (len >= CLBYTES) {
785 			struct mbuf *p;
786 
787 			MCLGET(p, 1);
788 			if (p != 0) {
789 				m->m_len = len = CLBYTES;
790 				m->m_off = (int)p - (int)m;
791 			} else {
792 				m->m_len = len = MIN(MLEN, len);
793 				m->m_off = MMINOFF;
794 			}
795 		} else {
796 			m->m_len = len = MIN(MLEN, len);
797 			m->m_off = MMINOFF;
798 		}
799 		mcp = mtod(m, u_char *);
800 		if (words = (len >> 1)) {
801 			register u_short *to, *from;
802 
803 			to = (u_short *)mcp;
804 			from = (u_short *)cp;
805 			do
806 				*to++ = *from++;
807 			while (--words > 0);
808 			mcp = (u_char *)to;
809 			cp = (u_char *)from;
810 		}
811 		if (len & 01)
812 			*mcp++ = *cp++;
813 		*mp = m;
814 		mp = &m->m_next;
815 		if (off == 0) {
816 			totlen -= len;
817 			continue;
818 		}
819 		off += len;
820 		if (off == totlen) {
821 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
822 			off = 0;
823 			totlen = off0;
824 		}
825 	}
826 	return (top);
827 bad:
828 	m_freem(top);
829 	return (0);
830 }
831 
832 /*
833  * Process an ioctl request.
834  */
835 ecioctl(ifp, cmd, data)
836 	register struct ifnet *ifp;
837 	int cmd;
838 	caddr_t data;
839 {
840 	register struct ifaddr *ifa = (struct ifaddr *)data;
841 	int s = splimp(), error = 0;
842 
843 	switch (cmd) {
844 
845 	case SIOCSIFADDR:
846 		ifp->if_flags |= IFF_UP;
847 
848 		switch (ifa->ifa_addr.sa_family) {
849 #ifdef INET
850 		case AF_INET:
851 			ecinit(ifp->if_unit); /* before, so we can send the ARP packet */
852 			((struct arpcom *)ifp)->ac_ipaddr =
853 				IA_SIN(ifa)->sin_addr;
854 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
855 			break;
856 #endif
857 #ifdef NS
858 		case AF_NS:
859 		    {
860 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
861 
862 			if (ns_nullhost(*ina)) {
863 				ina->x_host = * (union ns_host *)
864 				     (ec_softc[ifp->if_unit].es_addr);
865 			} else {
866 				/*
867 				 * The manual says we can't change the address
868 				 * while the reciever is armed so reset everything
869 				 */
870 				struct ec_softc *es = &ec_softc[ifp->if_unit];
871 				struct uba_device *ui = ecinfo[ifp->if_unit];
872 				struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
873 
874 				es->es_if.if_flags &= ~IFF_RUNNING;
875 				bcopy(ina->x_host.c_host, es->es_addr, sizeof(es->es_addr));
876 			}
877 			ecinit(ifp->if_unit); /* does ec_setaddr() */
878 			break;
879 		    }
880 #endif
881 		}
882 		break;
883 
884 	default:
885 		error = EINVAL;
886 	}
887 	splx(s);
888 	return (error);
889 }
890 
891 ec_setaddr(physaddr,unit)
892 u_char *physaddr;
893 int unit;
894 {
895 	struct ec_softc *es = &ec_softc[unit];
896 	struct uba_device *ui = ecinfo[unit];
897 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
898 	register char nibble;
899 	register int i, j;
900 	char *cp;
901 	/*
902 	 * Use the ethernet address supplied
903 	 * NOte that we do a UECLR here, so the recieve buffers
904 	 * must be requeued.
905 	 */
906 
907 #ifdef DEBUG
908 	printf("ec_setaddr: setting address for unit %d = ",
909 		unit);
910 	ether_addr(physaddr);
911 #endif
912 	addr->ec_xcr = EC_UECLR;
913 	addr->ec_rcr = 0;
914 	/* load requested address */
915 	for (i = 0; i < 6; i++) { /* 6 bytes of address */
916 	    es->es_addr[i] = physaddr[i];
917 	    nibble = physaddr[i] & 0xf; /* lower nibble */
918 	    addr->ec_rcr = (nibble << 8);
919 	    addr->ec_rcr = (nibble << 8) + EC_AWCLK; /* latch nibble */
920 	    addr->ec_rcr = (nibble << 8);
921 	    for (j=0; j < 4; j++) {
922 		addr->ec_rcr = 0;
923 		addr->ec_rcr = EC_ASTEP; /* step counter */
924 		addr->ec_rcr = 0;
925 	    }
926 	    nibble = (physaddr[i] >> 4) & 0xf; /* upper nibble */
927 	    addr->ec_rcr = (nibble << 8);
928 	    addr->ec_rcr = (nibble << 8) + EC_AWCLK; /* latch nibble */
929 	    addr->ec_rcr = (nibble << 8);
930 	    for (j=0; j < 4; j++) {
931 		addr->ec_rcr = 0;
932 		addr->ec_rcr = EC_ASTEP; /* step counter */
933 		addr->ec_rcr = 0;
934 	    }
935 	}
936 #ifdef DEBUG
937 	/*
938 	 * Read the ethernet address off the board, one nibble at a time.
939 	 */
940 	addr->ec_xcr = EC_UECLR;
941 	addr->ec_rcr = 0; /* read RAM */
942 	cp = es->es_addr;
943 #undef NEXTBIT
944 #define	NEXTBIT	addr->ec_rcr = EC_ASTEP; addr->ec_rcr = 0
945 	for (i=0; i < sizeof (es->es_addr); i++) {
946 		*cp = 0;
947 		for (j=0; j<=4; j+=4) {
948 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
949 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
950 		}
951 		cp++;
952 	}
953 	printf("ec_setaddr %d: ROM addr=",ui->ui_unit);
954 	ether_addr(es->es_addr);
955 #endif
956 }
957