xref: /original-bsd/sys/vax/if/if_ec.c (revision 9de3f1a2)
1 /*	if_ec.c	4.22	82/07/21	*/
2 
3 #include "ec.h"
4 
5 /*
6  * 3Com Ethernet Controller interface
7  */
8 
9 #include "../h/param.h"
10 #include "../h/systm.h"
11 #include "../h/mbuf.h"
12 #include "../h/pte.h"
13 #include "../h/buf.h"
14 #include "../h/protosw.h"
15 #include "../h/socket.h"
16 #include "../h/ubareg.h"
17 #include "../h/ubavar.h"
18 #include "../h/ecreg.h"
19 #include "../h/cpu.h"
20 #include "../h/mtpr.h"
21 #include "../h/vmmac.h"
22 #include "../net/in.h"
23 #include "../net/in_systm.h"
24 #include "../net/if.h"
25 #include "../net/if_ec.h"
26 #include "../net/if_uba.h"
27 #include "../net/ip.h"
28 #include "../net/ip_var.h"
29 #include "../net/pup.h"
30 #include "../net/route.h"
31 #include <errno.h>
32 
33 #define	ECMTU	1500
34 #define	ECMEM	0000000
35 
36 int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
37 struct	uba_device *ecinfo[NEC];
38 u_short ecstd[] = { 0 };
39 struct	uba_driver ecdriver =
40 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
41 u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
42 #define	ECUNIT(x)	minor(x)
43 
44 int	ecinit(),ecoutput(),ecreset();
45 struct mbuf *ecget();
46 
47 extern struct ifnet loif;
48 
49 /*
50  * Ethernet software status per interface.
51  *
52  * Each interface is referenced by a network interface structure,
53  * es_if, which the routing code uses to locate the interface.
54  * This structure contains the output queue for the interface, its address, ...
55  * We also have, for each interface, a UBA interface structure, which
56  * contains information about the UNIBUS resources held by the interface:
57  * map registers, buffered data paths, etc.  Information is cached in this
58  * structure for use by the if_uba.c routines in running the interface
59  * efficiently.
60  */
61 struct	ec_softc {
62 	struct	ifnet es_if;		/* network-visible interface */
63 	struct	ifuba es_ifuba;		/* UNIBUS resources */
64 	short	es_mask;		/* mask for current output delay */
65 	short	es_oactive;		/* is output active? */
66 	caddr_t	es_buf[16];		/* virtual addresses of buffers */
67 	u_char	es_enaddr[6];		/* board's ethernet address */
68 } ec_softc[NEC];
69 
70 /*
71  * Do output DMA to determine interface presence and
72  * interrupt vector.  DMA is too short to disturb other hosts.
73  */
74 ecprobe(reg)
75 	caddr_t reg;
76 {
77 	register int br, cvec;		/* r11, r10 value-result */
78 	register struct ecdevice *addr = (struct ecdevice *)reg;
79 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM];
80 
81 #ifdef lint
82 	br = 0; cvec = br; br = cvec;
83 	ecrint(0); ecxint(0); eccollide(0);
84 #endif
85 	/*
86 	 * Make sure memory is turned on
87 	 */
88 	addr->ec_rcr = EC_AROM;
89 	/*
90 	 * Disable map registers for ec unibus space,
91 	 * but don't allocate yet.
92 	 */
93 	ubamem(numuba, ECMEM, 32*2, 0);
94 	/*
95 	 * Check for existence of buffers on Unibus.
96 	 */
97 	if (badaddr((caddr_t) ecbuf, 2)) {
98 	bad1:
99 		printf("ec: buffer mem not found\n");
100 	bad2:
101 		ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
102 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
103 		return (0);
104 	}
105 #if VAX780
106 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
107 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
108 		goto bad1;
109 	}
110 #endif
111 
112 	/*
113 	 * Tell the system that the board has memory here, so it won't
114 	 * attempt to allocate the addresses later.
115 	 */
116 	if (ubamem(numuba, ECMEM, 32*2, 1) == 0) {
117 		printf("ecprobe: cannot reserve uba addresses\n");
118 		goto bad2;
119 	}
120 
121 	/*
122 	 * Make a one byte packet in what should be buffer #0.
123 	 * Submit it for sending.  This whould cause an xmit interrupt.
124 	 * The xmit interrupt vector is 8 bytes after the receive vector,
125 	 * so adjust for this before returning.
126 	 */
127 	*(u_short *)ecbuf = (u_short) 03777;
128 	ecbuf[03777] = '\0';
129 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
130 	DELAY(100000);
131 	addr->ec_xcr = EC_XCLR;
132 	if (cvec > 0 && cvec != 0x200) {
133 		if (cvec & 04) {	/* collision interrupt */
134 			cvec -= 04;
135 			br += 1;		/* rcv is collision + 1 */
136 		} else {		/* xmit interrupt */
137 			cvec -= 010;
138 			br += 2;		/* rcv is xmit + 2 */
139 		}
140 	}
141 	return (1);
142 }
143 
144 /*
145  * Interface exists: make available by filling in network interface
146  * record.  System will initialize the interface when it is ready
147  * to accept packets.
148  */
149 ecattach(ui)
150 	struct uba_device *ui;
151 {
152 	struct ec_softc *es = &ec_softc[ui->ui_unit];
153 	register struct ifnet *ifp = &es->es_if;
154 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
155 	struct sockaddr_in *sin;
156 	int i, j;
157 	u_char *cp;
158 
159 	ifp->if_unit = ui->ui_unit;
160 	ifp->if_name = "ec";
161 	ifp->if_mtu = ECMTU;
162 	ifp->if_net = ui->ui_flags;
163 
164 	/*
165 	 * Read the ethernet address off the board, one nibble at a time.
166 	 */
167 	addr->ec_xcr = EC_UECLR;
168 	addr->ec_rcr = EC_AROM;
169 	cp = es->es_enaddr;
170 #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
171 	for (i=0; i<6; i++) {
172 		*cp = 0;
173 		for (j=0; j<=4; j+=4) {
174 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
175 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
176 		}
177 		cp++;
178 	}
179 #ifdef notdef
180 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
181 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
182 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
183 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
184 #endif
185 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
186 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
187 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
188 	sin->sin_family = AF_INET;
189 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
190 
191 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
192 	sin->sin_family = AF_INET;
193 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
194 	ifp->if_flags = IFF_BROADCAST;
195 
196 	ifp->if_init = ecinit;
197 	ifp->if_output = ecoutput;
198 	ifp->if_ubareset = ecreset;
199 	for (i=0; i<16; i++)
200 		es->es_buf[i] = &umem[ui->ui_ubanum][ECMEM+2048*i];
201 	if_attach(ifp);
202 }
203 
204 /*
205  * Reset of interface after UNIBUS reset.
206  * If interface is on specified uba, reset its state.
207  */
208 ecreset(unit, uban)
209 	int unit, uban;
210 {
211 	register struct uba_device *ui;
212 
213 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
214 	    ui->ui_ubanum != uban)
215 		return;
216 	printf(" ec%d", unit);
217 	ubamem(uban, ECMEM, 32*2, 0);	/* map register disable (no alloc) */
218 	ecinit(unit);
219 }
220 
221 /*
222  * Initialization of interface; clear recorded pending
223  * operations, and reinitialize UNIBUS usage.
224  */
225 ecinit(unit)
226 	int unit;
227 {
228 	struct ec_softc *es = &ec_softc[unit];
229 	struct ecdevice *addr;
230 	int i, s;
231 
232 	/*
233 	 * Hang receive buffers and start any pending writes.
234 	 * Writing into the rcr also makes sure the memory
235 	 * is turned on.
236 	 */
237 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
238 	s = splimp();
239 	for (i=ECRHBF; i>=ECRLBF; i--)
240 		addr->ec_rcr = EC_READ|i;
241 	es->es_oactive = 0;
242 	es->es_mask = ~0;
243 	es->es_if.if_flags |= IFF_UP;
244 	if (es->es_if.if_snd.ifq_head)
245 		ecstart(unit);
246 	splx(s);
247 	if_rtinit(&es->es_if, RTF_UP);
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), dest;
262 	struct ec_softc *es = &ec_softc[unit];
263 	struct ecdevice *addr;
264 	struct mbuf *m;
265 	caddr_t ecbuf;
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 ec_header *ec;
400     	struct mbuf *m;
401 	int len, off, resid, ecoff, buf;
402 	register struct ifqueue *inq;
403 	caddr_t ecbuf;
404 
405 	es->es_if.if_ipackets++;
406 	buf = addr->ec_rcr & EC_RBN;
407 	if (buf < ECRLBF || buf > ECRHBF)
408 		panic("ecrint");
409 	ecbuf = es->es_buf[buf];
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 ec_header);
428 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
429 #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
430 	if (ec->ec_type >= ECPUP_TRAIL &&
431 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
432 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
433 		if (off >= ECMTU)
434 			goto setup;		/* sanity */
435 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
436 		resid = *(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->ec_type) {
459 
460 #ifdef INET
461 	case ECPUP_IPTYPE:
462 		schednetisr(NETISR_IP);
463 		inq = &ipintrq;
464 		break;
465 #endif
466 	default:
467 		m_freem(m);
468 		goto setup;
469 	}
470 
471 	if (IF_QFULL(inq)) {
472 		IF_DROP(inq);
473 		m_freem(m);
474 		goto setup;
475 	}
476 	IF_ENQUEUE(inq, m);
477 
478 setup:
479 	/*
480 	 * Reset for next packet.
481 	 */
482 	addr->ec_rcr = EC_READ|EC_RCLR|buf;
483 }
484 
485 /*
486  * Ethernet output routine.
487  * Encapsulate a packet of type family for the local net.
488  * Use trailer local net encapsulation if enough data in first
489  * packet leaves a multiple of 512 bytes of data in remainder.
490  * If destination is this address or broadcast, send packet to
491  * loop device to kludge around the fact that 3com interfaces can't
492  * talk to themselves.
493  */
494 ecoutput(ifp, m0, dst)
495 	struct ifnet *ifp;
496 	struct mbuf *m0;
497 	struct sockaddr *dst;
498 {
499 	int type, dest, s, error;
500 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
501 	register struct mbuf *m = m0;
502 	register struct ec_header *ec;
503 	register int off, i;
504 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
505 
506 	switch (dst->sa_family) {
507 
508 #ifdef INET
509 	case AF_INET:
510 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
511 		if ((dest &~ 0xff) == 0)
512 			mcopy = m_copy(m, 0, M_COPYALL);
513 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
514 		    sin_addr.s_addr) {
515 			mcopy = m;
516 			goto gotlocal;
517 		}
518 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
519 		if (off > 0 && (off & 0x1ff) == 0 &&
520 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
521 			type = ECPUP_TRAIL + (off>>9);
522 			m->m_off -= 2 * sizeof (u_short);
523 			m->m_len += 2 * sizeof (u_short);
524 			*mtod(m, u_short *) = ECPUP_IPTYPE;
525 			*(mtod(m, u_short *) + 1) = m->m_len;
526 			goto gottrailertype;
527 		}
528 		type = ECPUP_IPTYPE;
529 		off = 0;
530 		goto gottype;
531 #endif
532 
533 	default:
534 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
535 			dst->sa_family);
536 		error = EAFNOSUPPORT;
537 		goto bad;
538 	}
539 
540 gottrailertype:
541 	/*
542 	 * Packet to be sent as trailer: move first packet
543 	 * (control information) to end of chain.
544 	 */
545 	while (m->m_next)
546 		m = m->m_next;
547 	m->m_next = m0;
548 	m = m0->m_next;
549 	m0->m_next = 0;
550 	m0 = m;
551 
552 gottype:
553 	/*
554 	 * Add local net header.  If no space in first mbuf,
555 	 * allocate another.
556 	 */
557 	if (m->m_off > MMAXOFF ||
558 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
559 		m = m_get(M_DONTWAIT);
560 		if (m == 0) {
561 			error = ENOBUFS;
562 			goto bad;
563 		}
564 		m->m_next = m0;
565 		m->m_off = MMINOFF;
566 		m->m_len = sizeof (struct ec_header);
567 	} else {
568 		m->m_off -= sizeof (struct ec_header);
569 		m->m_len += sizeof (struct ec_header);
570 	}
571 	ec = mtod(m, struct ec_header *);
572 	for (i=0; i<6; i++)
573 		ec->ec_shost[i] = es->es_enaddr[i];
574 	if ((dest &~ 0xff) == 0)
575 		/* broadcast address */
576 		for (i=0; i<6; i++)
577 			ec->ec_dhost[i] = 0xff;
578 	else {
579 		if (dest & 0x8000) {
580 			ec->ec_dhost[0] = ec_iltop[0];
581 			ec->ec_dhost[1] = ec_iltop[1];
582 			ec->ec_dhost[2] = ec_iltop[2];
583 		} else {
584 			ec->ec_dhost[0] = es->es_enaddr[0];
585 			ec->ec_dhost[1] = es->es_enaddr[1];
586 			ec->ec_dhost[2] = es->es_enaddr[2];
587 		}
588 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
589 		ec->ec_dhost[4] = (dest>>16) & 0xff;
590 		ec->ec_dhost[5] = (dest>>24) & 0xff;
591 	}
592 	ec->ec_type = type;
593 
594 	/*
595 	 * Queue message on interface, and start output if interface
596 	 * not yet active.
597 	 */
598 	s = splimp();
599 	if (IF_QFULL(&ifp->if_snd)) {
600 		IF_DROP(&ifp->if_snd);
601 		error = ENOBUFS;
602 		goto qfull;
603 	}
604 	IF_ENQUEUE(&ifp->if_snd, m);
605 	if (es->es_oactive == 0)
606 		ecstart(ifp->if_unit);
607 	splx(s);
608 
609 gotlocal:
610 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
611 
612 qfull:
613 	m0 = m;
614 	splx(s);
615 bad:
616 	m_freem(m0);
617 	return(error);
618 }
619 
620 /*
621  * Routine to copy from mbuf chain to transmitter
622  * buffer in UNIBUS memory.
623  */
624 ecput(ecbuf, m)
625 	u_char *ecbuf;
626 	struct mbuf *m;
627 {
628 	register struct mbuf *mp;
629 	register int off;
630 	u_char *bp;
631 
632 	for (off = 2048, mp = m; mp; mp = mp->m_next)
633 		off -= mp->m_len;
634 	*(u_short *)ecbuf = off;
635 	bp = (u_char *)(ecbuf + off);
636 	for (mp = m; mp; mp = mp->m_next) {
637 		register unsigned len = mp->m_len;
638 		u_char *mcp;
639 
640 		if (len == 0)
641 			continue;
642 		mcp = mtod(mp, u_char *);
643 		if ((unsigned)bp & 01) {
644 			*bp++ = *mcp++;
645 			len--;
646 		}
647 		if (off = (len >> 1)) {
648 			register u_short *to, *from;
649 
650 			to = (u_short *)bp;
651 			from = (u_short *)mcp;
652 			do
653 				*to++ = *from++;
654 			while (--off > 0);
655 			bp = (u_char *)to,
656 			mcp = (u_char *)from;
657 		}
658 		if (len & 01)
659 			*bp++ = *mcp++;
660 	}
661 #ifdef notdef
662 	if (bp - ecbuf != 2048)
663 		printf("ec: bad ecput, diff=%d\n", bp-ecbuf);
664 #endif
665 	m_freem(m);
666 }
667 
668 /*
669  * Routine to copy from UNIBUS memory into mbufs.
670  * Similar in spirit to if_rubaget.
671  *
672  * Warning: This makes the fairly safe assumption that
673  * mbufs have even lengths.
674  */
675 struct mbuf *
676 ecget(ecbuf, totlen, off0)
677 	u_char *ecbuf;
678 	int totlen, off0;
679 {
680 	register struct mbuf *m;
681 	struct mbuf *top = 0, **mp = &top;
682 	register int off = off0, len;
683 	u_char *cp;
684 
685 	cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
686 	while (totlen > 0) {
687 		register int words;
688 		u_char *mcp;
689 
690 		MGET(m, 0);
691 		if (m == 0)
692 			goto bad;
693 		if (off) {
694 			len = totlen - off;
695 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
696 		} else
697 			len = totlen;
698 		if (len >= CLBYTES) {
699 			struct mbuf *p;
700 
701 			MCLGET(p, 1);
702 			if (p != 0) {
703 				m->m_len = len = CLBYTES;
704 				m->m_off = (int)p - (int)m;
705 			} else {
706 				m->m_len = len = MIN(MLEN, len);
707 				m->m_off = MMINOFF;
708 			}
709 		} else {
710 			m->m_len = len = MIN(MLEN, len);
711 			m->m_off = MMINOFF;
712 		}
713 		mcp = mtod(m, u_char *);
714 		if (words = (len >> 1)) {
715 			register u_short *to, *from;
716 
717 			to = (u_short *)mcp;
718 			from = (u_short *)cp;
719 			do
720 				*to++ = *from++;
721 			while (--words > 0);
722 			mcp = (u_char *)to;
723 			cp = (u_char *)from;
724 		}
725 		if (len & 01)
726 			*mcp++ = *cp++;
727 		*mp = m;
728 		mp = &m->m_next;
729 		if (off == 0) {
730 			totlen -= len;
731 			continue;
732 		}
733 		off += len;
734 		if (off == totlen) {
735 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
736 			off = 0;
737 			totlen = off0;
738 		}
739 	}
740 	return (top);
741 bad:
742 	m_freem(top);
743 	return (0);
744 }
745