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