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