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