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