xref: /csrg-svn/sys/vax/if/if_ec.c (revision 6931)
1 /*	if_ec.c	4.11	82/05/25	*/
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 u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
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	ifnet es_if;		/* network-visible interface */
64 	struct	ifuba es_ifuba;		/* UNIBUS resources */
65 	short	es_mask;		/* mask for current output delay */
66 	short	es_oactive;		/* is output active? */
67 	caddr_t	es_buf[16];		/* virtual addresses of buffers */
68 	u_char	es_enaddr[6];		/* board's ethernet address */
69 } ec_softc[NEC];
70 
71 /*
72  * Do output DMA to determine interface presence and
73  * interrupt vector.  DMA is too short to disturb other hosts.
74  */
75 ecprobe(reg)
76 	caddr_t reg;
77 {
78 	register int br, cvec;		/* r11, r10 value-result */
79 	register struct ecdevice *addr = (struct ecdevice *)reg;
80 	register caddr_t ecbuf = (caddr_t) &umem[0][0600000];
81 
82 COUNT(ECPROBE);
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 	 * Check for existence of buffers on Unibus.
93 	 * This won't work on a 780 until more work is done.
94 	 */
95 	if (badaddr((caddr_t) ecbuf, 2)) {
96 		printf("ec: buffer mem not found");
97 		return (0);
98 	}
99 
100 	/*
101 	 * Tell the system that the board has memory here, so it won't
102 	 * attempt to allocate the addresses later.
103 	 */
104 	ubamem(0, 0600000, 32*2);
105 
106 	/*
107 	 * Make a one byte packet in what should be buffer #0.
108 	 * Submit it for sending.  This whould cause an xmit interrupt.
109 	 * The xmit interrupt vector is 8 bytes after the receive vector,
110 	 * so adjust for this before returning.
111 	 */
112 	*(u_short *)ecbuf = (u_short) 03777;
113 	ecbuf[03777] = '\0';
114 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
115 	DELAY(100000);
116 	addr->ec_xcr = EC_XCLR;
117 	if (cvec > 0 && cvec != 0x200)
118 		cvec -= 010;
119 		br += 2;
120 	return (1);
121 }
122 
123 /*
124  * Interface exists: make available by filling in network interface
125  * record.  System will initialize the interface when it is ready
126  * to accept packets.
127  */
128 ecattach(ui)
129 	struct uba_device *ui;
130 {
131 	register struct ec_softc *es = &ec_softc[ui->ui_unit];
132 	register struct sockaddr_in *sin;
133 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
134 	register int i, j;
135 	register u_char *cp;
136 COUNT(ECATTACH);
137 
138 	es->es_if.if_unit = ui->ui_unit;
139 	es->es_if.if_name = "ec";
140 	es->es_if.if_mtu = ECMTU;
141 	es->es_if.if_net = ui->ui_flags & 0xff;
142 
143 	/*
144 	 * Read the ethernet address off the board,
145 	 * one nibble at a time!
146 	 */
147 	addr->ec_xcr = EC_UECLR;
148 	addr->ec_rcr = EC_AROM;
149 	cp = es->es_enaddr;
150 	for (i=0; i<6; i++) {
151 		*cp = 0;
152 		for (j=0; j<=4; j+=4) {
153 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
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 			addr->ec_rcr = EC_AROM|EC_ASTEP;
161 			addr->ec_rcr = EC_AROM;
162 		}
163 		cp++;
164 	}
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 	es->es_if.if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
170 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
171 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
172 	sin->sin_family = AF_INET;
173 	sin->sin_addr = if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
174 
175 	sin = (struct sockaddr_in *)&es->es_if.if_broadaddr;
176 	sin->sin_family = AF_INET;
177 	sin->sin_addr = if_makeaddr(es->es_if.if_net, 0);
178 	es->es_if.if_flags = IFF_BROADCAST;
179 
180 	es->es_if.if_init = ecinit;
181 	es->es_if.if_output = ecoutput;
182 	es->es_if.if_ubareset = ecreset;
183 	for (i=0; i<16; i++)
184 		es->es_buf[i] = &umem[ui->ui_ubanum][0600000+2048*i];
185 	if_attach(&es->es_if);
186 #if NIMP == 0
187 	/* here's one for you john baby.... */
188 	if (ui->ui_flags &~ 0xff)
189 		eclhinit(&es->es_if, (ui->ui_flags &~ 0xff) | 0x0a);
190 #endif
191 }
192 
193 /*
194  * Reset of interface after UNIBUS reset.
195  * If interface is on specified uba, reset its state.
196  */
197 ecreset(unit, uban)
198 	int unit, uban;
199 {
200 	register struct uba_device *ui;
201 COUNT(ECRESET);
202 
203 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
204 	    ui->ui_ubanum != uban)
205 		return;
206 	printf(" ec%d", unit);
207 	ecinit(unit);
208 }
209 
210 /*
211  * Initialization of interface; clear recorded pending
212  * operations, and reinitialize UNIBUS usage.
213  */
214 ecinit(unit)
215 	int unit;
216 {
217 	register struct ec_softc *es = &ec_softc[unit];
218 	register struct uba_device *ui = ecinfo[unit];
219 	register struct ecdevice *addr;
220 	register i;
221 	int s;
222 
223 	addr = (struct ecdevice *)ui->ui_addr;
224 
225 	/*
226 	 * Hang receive buffers and start any pending
227 	 * writes by faking a transmit complete.
228 	 * Writing into the rcr also makes sure the memory
229 	 * is turned on.
230 	 */
231 	s = splimp();
232 	for (i=ECRHBF; i>=ECRLBF; i--)
233 		addr->ec_rcr = EC_READ|i;
234 	es->es_oactive = 1;
235 	es->es_if.if_flags |= IFF_UP;
236 	ecxint(unit);
237 	splx(s);
238 	if_rtinit(&es->es_if, RTF_DIRECT|RTF_UP);
239 }
240 
241 /*
242  * Start or restart output on interface.
243  * If interface is already active, then this is a retransmit
244  * after a collision, and just restuff registers.
245  * If interface is not already active, get another datagram
246  * to send off of the interface queue, and map it to the interface
247  * before starting the output.
248  */
249 ecstart(dev)
250 	dev_t dev;
251 {
252         int unit = ECUNIT(dev);
253 	struct uba_device *ui = ecinfo[unit];
254 	register struct ec_softc *es = &ec_softc[unit];
255 	register struct ecdevice *addr;
256 	struct mbuf *m;
257 	caddr_t ecbuf;
258 	int dest;
259 COUNT(ECSTART);
260 
261 	if (es->es_oactive)
262 		goto restart;
263 
264 	/*
265 	 * Not already active: dequeue another request
266 	 * and copy it into the buffer.  If no more requests,
267 	 * just return.
268 	 */
269 	IF_DEQUEUE(&es->es_if.if_snd, m);
270 	if (m == 0) {
271 		es->es_oactive = 0;
272 		return;
273 	}
274 	ecput(es->es_buf[ECTBF], m);
275 
276 restart:
277 	/*
278 	 * Start the output.
279 	 */
280 	addr = (struct ecdevice *)ui->ui_addr;
281 	addr->ec_xcr = EC_WRITE|ECTBF;
282 	es->es_oactive = 1;
283 }
284 
285 /*
286  * Ethernet interface transmitter interrupt.
287  * Start another output if more data to send.
288  */
289 ecxint(unit)
290 	int unit;
291 {
292 	register struct uba_device *ui = ecinfo[unit];
293 	register struct ec_softc *es = &ec_softc[unit];
294 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
295 COUNT(ECXINT);
296 
297 	if (es->es_oactive == 0)
298 		return;
299 	if (addr->ec_xcr&EC_XDONE == 0 || addr->ec_xcr&EC_XBN != ECTBF)
300 		printf("ec%d: strange xmit interrupt!\n", unit);
301 	es->es_if.if_opackets++;
302 	es->es_oactive = 0;
303 	es->es_mask = ~0;
304 	addr->ec_xcr = EC_XCLR;
305 	/*
306 	 * There shouldn't ever be any mbuf's to free, but just in case...
307 	 */
308 	if (es->es_ifuba.ifu_xtofree) {
309 		m_freem(es->es_ifuba.ifu_xtofree);
310 		es->es_ifuba.ifu_xtofree = 0;
311 	}
312 	if (es->es_if.if_snd.ifq_head == 0) {
313 		return;
314 	}
315 	ecstart(unit);
316 }
317 
318 /*
319  * Collision on ethernet interface.  Do exponential
320  * backoff, and retransmit.  If have backed off all
321  * the way print warning diagnostic, and drop packet.
322  */
323 eccollide(unit)
324 	int unit;
325 {
326 	struct ec_softc *es = &ec_softc[unit];
327 COUNT(ECCOLLIDE);
328 
329 	printf("ec%d: collision\n", unit);
330 	es->es_if.if_collisions++;
331 	if (es->es_oactive == 0)
332 		return;
333 	ecdocoll(unit);
334 }
335 
336 ecdocoll(unit)
337 	int unit;
338 {
339 	register struct ec_softc *es = &ec_softc[unit];
340 	register struct ecdevice *addr =
341 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
342 	register i;
343 	int delay;
344 
345 	/*
346 	 * Es_mask is a 16 bit number with n low zero bits, with
347 	 * n the number of backoffs.  When es_mask is 0 we have
348 	 * backed off 16 times, and give up.
349 	 */
350 	if (es->es_mask == 0) {
351 		es->es_if.if_oerrors++;
352 		printf("ec%d: send error\n", unit);
353 		/*
354 		 * Reset interface, then requeue rcv buffers.
355 		 * Some incoming packets may be lost, but that
356 		 * can't be helped.
357 		 */
358 		addr->ec_xcr = EC_UECLR;
359 		for (i=ECRHBF; i>=ECRLBF; i--)
360 			addr->ec_rcr = EC_READ|i;
361 		/*
362 		 * Reset and transmit next packet (if any).
363 		 */
364 		es->es_oactive = 0;
365 		es->es_mask = ~0;
366 		if (es->es_if.if_snd.ifq_head)
367 			ecstart(unit);
368 		return;
369 	}
370 	/*
371 	 * Do exponential backoff.  Compute delay based on low bits
372 	 * of the interval timer.  Then delay for that number of
373 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
374 	 * This does not take into account the time already used to
375 	 * process the interrupt.
376 	 */
377 	es->es_mask <<= 1;
378 	delay = mfpr(ICR) &~ es->es_mask;
379 	DELAY(delay * 51);
380 	/*
381 	 * Clear the controller's collision flag, thus enabling retransmit.
382 	 */
383 	addr->ec_xcr = EC_JINTEN|EC_XINTEN|EC_JCLR;
384 }
385 
386 /*
387  * Ethernet interface receiver interrupt.
388  * If input error just drop packet.
389  * Otherwise purge input buffered data path and examine
390  * packet to determine type.  If can't determine length
391  * from type, then have to drop packet.  Othewise decapsulate
392  * packet based on type and pass to type specific higher-level
393  * input routine.
394  */
395 ecrint(unit)
396 	int unit;
397 {
398 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
399 COUNT(ECRINT);
400 
401 	while (addr->ec_rcr & EC_RDONE)
402 		ecread(unit);
403 }
404 
405 ecread(unit)
406 	int unit;
407 {
408 	register struct ec_softc *es = &ec_softc[unit];
409 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
410 	register struct ec_header *ec;
411     	struct mbuf *m;
412 	int len, off, resid;
413 	register struct ifqueue *inq;
414 	caddr_t ecbuf;
415 	int ecoff;
416 	int buf;
417 COUNT(ECREAD);
418 
419 	es->es_if.if_ipackets++;
420 	buf = addr->ec_rcr & EC_RBN;
421 	if (buf < ECRLBF || buf > ECRHBF)
422 		panic("ecrint");
423 	ecbuf = es->es_buf[buf];
424 	ecoff = *(short *)ecbuf;
425 	if (ecoff <= ECRDOFF || ecoff > 2046) {
426 		es->es_if.if_ierrors++;
427 #ifdef notdef
428 		if (es->es_if.if_ierrors % 100 == 0)
429 			printf("ec%d: += 100 input errors\n", unit);
430 #endif
431 		printf("ec%d: input error (offset=%d)\n", unit, ecoff);
432 		goto setup;
433 	}
434 
435 	/*
436 	 * Get input data length.
437 	 * Get pointer to ethernet header (in input buffer).
438 	 * Deal with trailer protocol: if type is PUP trailer
439 	 * get true type from first 16-bit word past data.
440 	 * Remember that type was trailer by setting off.
441 	 */
442 	len = ecoff - ECRDOFF - sizeof (struct ec_header);
443 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
444 #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
445 	if (ec->ec_type >= ECPUP_TRAIL &&
446 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
447 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
448 		if (off >= ECMTU)
449 			goto setup;		/* sanity */
450 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
451 		resid = *(ecdataaddr(ec, off+2, u_short *));
452 		if (off + resid > len)
453 			goto setup;		/* sanity */
454 		len = off + resid;
455 	} else
456 		off = 0;
457 	if (len == 0)
458 		goto setup;
459 
460 	/*
461 	 * Pull packet off interface.  Off is nonzero if packet
462 	 * has trailing header; ecget will then force this header
463 	 * information to be at the front, but we still have to drop
464 	 * the type and length which are at the front of any trailer data.
465 	 */
466 	m = ecget(ecbuf, len, off);
467 	if (m == 0)
468 		goto setup;
469 	if (off) {
470 		m->m_off += 2 * sizeof (u_short);
471 		m->m_len -= 2 * sizeof (u_short);
472 	}
473 	switch (ec->ec_type) {
474 
475 #ifdef INET
476 	case ECPUP_IPTYPE:
477 		schednetisr(NETISR_IP);
478 		inq = &ipintrq;
479 		break;
480 #endif
481 	default:
482 		m_freem(m);
483 		goto setup;
484 	}
485 
486 	if (IF_QFULL(inq)) {
487 		IF_DROP(inq);
488 		m_freem(m);
489 	} else
490 		IF_ENQUEUE(inq, m);
491 
492 setup:
493 	/*
494 	 * Reset for next packet.
495 	 */
496 	addr->ec_rcr = EC_READ|EC_RCLR|buf;
497 }
498 
499 /*
500  * Ethernet output routine.
501  * Encapsulate a packet of type family for the local net.
502  * Use trailer local net encapsulation if enough data in first
503  * packet leaves a multiple of 512 bytes of data in remainder.
504  * If destination is this address or broadcast, send packet to
505  * loop device to kludge around the fact that 3com interfaces can't
506  * talk to themselves.
507  */
508 ecoutput(ifp, m0, dst)
509 	struct ifnet *ifp;
510 	struct mbuf *m0;
511 	struct sockaddr *dst;
512 {
513 	int type, dest, s, error;
514 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
515 	register struct mbuf *m = m0;
516 	register struct ec_header *ec;
517 	register int off;
518 	register int i;
519 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
520 
521 COUNT(ECOUTPUT);
522 	switch (dst->sa_family) {
523 
524 #ifdef INET
525 	case AF_INET:
526 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
527 		if ((dest &~ 0xff) == 0)
528 			mcopy = m_copy(m, 0, M_COPYALL);
529 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
530 		    sin_addr.s_addr) {
531 			mcopy = m;
532 			goto gotlocal;
533 		}
534 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
535 		if (off > 0 && (off & 0x1ff) == 0 &&
536 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
537 			type = ECPUP_TRAIL + (off>>9);
538 			m->m_off -= 2 * sizeof (u_short);
539 			m->m_len += 2 * sizeof (u_short);
540 			*mtod(m, u_short *) = ECPUP_IPTYPE;
541 			*(mtod(m, u_short *) + 1) = m->m_len;
542 			goto gottrailertype;
543 		}
544 		type = ECPUP_IPTYPE;
545 		off = 0;
546 		goto gottype;
547 #endif
548 
549 	default:
550 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
551 			dst->sa_family);
552 		error = EAFNOSUPPORT;
553 		goto bad;
554 	}
555 
556 gottrailertype:
557 	/*
558 	 * Packet to be sent as trailer: move first packet
559 	 * (control information) to end of chain.
560 	 */
561 	while (m->m_next)
562 		m = m->m_next;
563 	m->m_next = m0;
564 	m = m0->m_next;
565 	m0->m_next = 0;
566 	m0 = m;
567 
568 gottype:
569 	/*
570 	 * Add local net header.  If no space in first mbuf,
571 	 * allocate another.
572 	 */
573 	if (m->m_off > MMAXOFF ||
574 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
575 		m = m_get(M_DONTWAIT);
576 		if (m == 0) {
577 			error = ENOBUFS;
578 			goto bad;
579 		}
580 		m->m_next = m0;
581 		m->m_off = MMINOFF;
582 		m->m_len = sizeof (struct ec_header);
583 	} else {
584 		m->m_off -= sizeof (struct ec_header);
585 		m->m_len += sizeof (struct ec_header);
586 	}
587 	ec = mtod(m, struct ec_header *);
588 	for (i=0; i<6; i++)
589 		ec->ec_shost[i] = es->es_enaddr[i];
590 	if ((dest &~ 0xff) == 0)
591 		for (i=0; i<6; i++)
592 			ec->ec_dhost[i] = 0xff;
593 	else {
594 		if (dest & 0x8000) {
595 			ec->ec_dhost[0] = ec_iltop[0];
596 			ec->ec_dhost[1] = ec_iltop[1];
597 			ec->ec_dhost[2] = ec_iltop[2];
598 		} else {
599 			ec->ec_dhost[0] = es->es_enaddr[0];
600 			ec->ec_dhost[1] = es->es_enaddr[1];
601 			ec->ec_dhost[2] = es->es_enaddr[2];
602 		}
603 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
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	looutput();
742 
743 /*
744  * Called by localnet interface to allow logical
745  * host interface to "attach", it's purpose
746  * is simply to establish the host's arpanet address.
747  */
748 eclhinit(ecifp, addr)
749 	struct ifnet *ecifp;
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_dstaddr = ecifp->if_addr;
763 	ifp->if_flags = IFF_UP|IFF_POINTOPOINT;
764 	ifp->if_output = looutput;
765 	if_attach(ifp);
766 	rtinit(&ifp->if_addr, &ifp->if_addr, RTF_UP|RTF_DIRECT|RTF_HOST);
767 }
768 #endif
769