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