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