restore keywords
[unix-history] / usr / src / sys / vax / if / if_ec.c
CommitLineData
949a0907 1/* if_ec.c 4.14 82/06/05 */
75334b2a
BF
2
3#include "ec.h"
4#include "imp.h"
be515f6e 5#include "loop.h"
75334b2a
BF
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
37int ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
38struct uba_device *ecinfo[NEC];
39u_short ecstd[] = { 0 };
40struct uba_driver ecdriver =
41 { ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
cf1ea369 42u_char ec_iltop[3] = { 0x02, 0x07, 0x01 };
75334b2a
BF
43#define ECUNIT(x) minor(x)
44
45int ecinit(),ecoutput(),ecreset();
46struct mbuf *ecget();
47
be515f6e
BF
48extern struct ifnet loif;
49
75334b2a
BF
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 */
62struct ec_softc {
63 struct ifnet es_if; /* network-visible interface */
64 struct ifuba es_ifuba; /* UNIBUS resources */
75334b2a 65 short es_mask; /* mask for current output delay */
75334b2a
BF
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 */
75ecprobe(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
82COUNT(ECPROBE);
83#ifdef lint
84 br = 0; cvec = br; br = cvec;
85 ecrint(0); ecxint(0); eccollide(0);
86#endif
f5a616ae
BF
87 /*
88 * Make sure memory is turned on
89 */
90 addr->ec_rcr = EC_AROM;
75334b2a
BF
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 */
128ecattach(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;
136COUNT(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)
dc39362e 189 eclhinit(&es->es_if, (ui->ui_flags &~ 0xff) | 0x0a);
75334b2a
BF
190#endif
191}
192
193/*
194 * Reset of interface after UNIBUS reset.
195 * If interface is on specified uba, reset its state.
196 */
197ecreset(unit, uban)
198 int unit, uban;
199{
200 register struct uba_device *ui;
201COUNT(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 */
214ecinit(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
75334b2a
BF
223 addr = (struct ecdevice *)ui->ui_addr;
224
225 /*
226 * Hang receive buffers and start any pending
227 * writes by faking a transmit complete.
f5a616ae
BF
228 * Writing into the rcr also makes sure the memory
229 * is turned on.
75334b2a
BF
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
75334b2a
BF
241/*
242 * Start or restart output on interface.
243 * If interface is already active, then this is a retransmit
be515f6e 244 * after a collision, and just restuff registers.
75334b2a
BF
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 */
249ecstart(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;
259COUNT(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 }
75334b2a
BF
274 ecput(es->es_buf[ECTBF], m);
275
75334b2a
BF
276restart:
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 */
289ecxint(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;
295COUNT(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;
75334b2a
BF
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 }
cf1ea369 312 if (es->es_if.if_snd.ifq_head == 0) {
75334b2a 313 return;
cf1ea369 314 }
75334b2a
BF
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 */
323eccollide(unit)
324 int unit;
325{
326 struct ec_softc *es = &ec_softc[unit];
327COUNT(ECCOLLIDE);
328
be515f6e 329 printf("ec%d: collision\n", unit);
75334b2a
BF
330 es->es_if.if_collisions++;
331 if (es->es_oactive == 0)
332 return;
333 ecdocoll(unit);
334}
335
336ecdocoll(unit)
337 int unit;
338{
339 register struct ec_softc *es = &ec_softc[unit];
be515f6e
BF
340 register struct ecdevice *addr =
341 (struct ecdevice *)ecinfo[unit]->ui_addr;
342 register i;
343 int delay;
75334b2a
BF
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) {
be515f6e 351 es->es_if.if_oerrors++;
75334b2a
BF
352 printf("ec%d: send error\n", unit);
353 /*
be515f6e
BF
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).
75334b2a 363 */
be515f6e
BF
364 es->es_oactive = 0;
365 es->es_mask = ~0;
366 if (es->es_if.if_snd.ifq_head)
367 ecstart(unit);
75334b2a
BF
368 return;
369 }
370 /*
be515f6e
BF
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.
75334b2a
BF
376 */
377 es->es_mask <<= 1;
be515f6e
BF
378 delay = mfpr(ICR) &~ es->es_mask;
379 DELAY(delay * 51);
75334b2a 380 /*
be515f6e 381 * Clear the controller's collision flag, thus enabling retransmit.
75334b2a 382 */
5470412f 383 addr->ec_xcr = EC_JINTEN|EC_XINTEN|EC_JCLR;
75334b2a
BF
384}
385
75334b2a
BF
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 */
395ecrint(unit)
396 int unit;
397{
398 struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
399COUNT(ECRINT);
400
75334b2a
BF
401 while (addr->ec_rcr & EC_RDONE)
402 ecread(unit);
403}
404
405ecread(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;
417COUNT(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;
be515f6e 425 if (ecoff <= ECRDOFF || ecoff > 2046) {
75334b2a
BF
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;
75334b2a
BF
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
492setup:
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.
be515f6e
BF
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.
75334b2a
BF
507 */
508ecoutput(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;
be515f6e 519 struct mbuf *mcopy = (struct mbuf *) 0; /* Null */
75334b2a
BF
520
521COUNT(ECOUTPUT);
522 switch (dst->sa_family) {
523
524#ifdef INET
525 case AF_INET:
526 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
be515f6e
BF
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 }
75334b2a
BF
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;
75334b2a
BF
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
556gottrailertype:
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
568gottype:
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];
f53ac196 590 if ((dest &~ 0xff) == 0)
75334b2a
BF
591 for (i=0; i<6; i++)
592 ec->ec_dhost[i] = 0xff;
593 else {
cf1ea369
BF
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;
75334b2a
BF
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);
be515f6e
BF
623gotlocal:
624 if (mcopy) /* Kludge, but it works! */
625 return(looutput(&loif, mcopy, dst));
626 else
627 return (0);
75334b2a
BF
628qfull:
629 m0 = m;
630 splx(s);
631bad:
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 */
640ecput(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
649COUNT(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 *);
4a404244
BJ
658 i = 0;
659 if ((int)bp&1) {
660 *bp++ = *mcp++;
661 i++;
662 }
75334b2a 663 for (i=0; i<mp->m_len; i++)
4a404244
BJ
664 while (i < mp->m_len) {
665 *(short *)bp = *(short *)mcp;
666 bp += 2;
667 mcp += 2;
668 i += 2;
669 }
670 if (mp->m_len&1)
75334b2a
BF
671 *bp++ = *mcp++;
672 mp = m_free(mp);
673 }
674 if (bp != ecbuf+2048)
675 printf("ec: bad ecput!\n");
676}
677
678/*
679 * Routine to copy from UNIBUS memory into mbufs.
680 * Similar in spirit to if_rubaget.
4a404244
BJ
681 *
682 * Warning: This makes the fairly safe assumption that
683 * mbufs have even lengths.
75334b2a
BF
684 */
685struct mbuf *
686ecget(ecbuf, totlen, off0)
687 char *ecbuf;
688 int totlen, off0;
689{
690 struct mbuf *top, **mp, *m;
691 int off = off0;
692 int len;
693 register char *cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
694 register char *mcp;
695 register int i;
696
697COUNT(ECGET);
698 top = 0;
699 mp = &top;
700 while (totlen > 0) {
701 MGET(m, 0);
702 if (m == 0)
703 goto bad;
704 if (off) {
705 len = totlen - off;
706 cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
707 } else
708 len = totlen;
709 if (len >= CLBYTES) {
710 struct mbuf *p;
711
712 MCLGET(p, 1);
713 if (p != 0) {
714 m->m_len = len = CLBYTES;
715 m->m_off = (int)p - (int)m;
716 } else {
717 m->m_len = len = MIN(MLEN, len);
718 m->m_off = MMINOFF;
719 }
720 } else {
721 m->m_len = len = MIN(MLEN, len);
722 m->m_off = MMINOFF;
723 }
724 mcp = mtod(m, char *);
4a404244
BJ
725 for (i=0; i<len; i+=2) {
726 *(short *)mcp = *(short *)cp;
727 mcp += 2;
728 cp += 2;
729 }
730 if (len&1)
75334b2a
BF
731 *mcp++ = *cp++;
732 *mp = m;
733 mp = &m->m_next;
734 if (off) {
735 off += len;
736 if (off == totlen) {
737 cp = ecbuf + ECRDOFF +
738 sizeof (struct ec_header);
739 off = 0;
740 totlen = off0;
741 }
742 } else
743 totlen -= len;
744 }
745 return (top);
746bad:
747 m_freem(top);
748 return (0);
749}
750
751#if NIMP == 0 && NEC > 0
752/*
753 * Logical host interface driver.
754 * Allows host to appear as an ARPAnet
755 * logical host. Must also have routing
756 * table entry set up to forward packets
757 * to appropriate gateway on localnet.
758 */
759
760struct ifnet eclhif;
dc39362e 761int looutput();
75334b2a
BF
762
763/*
764 * Called by localnet interface to allow logical
dc39362e 765 * host interface to "attach", it's purpose
75334b2a
BF
766 * is simply to establish the host's arpanet address.
767 */
dc39362e
BJ
768eclhinit(ecifp, addr)
769 struct ifnet *ecifp;
75334b2a
BF
770 int addr;
771{
772 register struct ifnet *ifp = &eclhif;
773 register struct sockaddr_in *sin;
774
775COUNT(ECLHINIT);
776 ifp->if_name = "lh";
777 ifp->if_mtu = ECMTU;
778 sin = (struct sockaddr_in *)&ifp->if_addr;
779 sin->sin_family = AF_INET;
780 sin->sin_addr.s_addr = addr;
34a8182d 781 sin->sin_addr.s_lh = ecifp->if_host[0];
75334b2a 782 ifp->if_net = sin->sin_addr.s_net;
34a8182d 783 ifp->if_dstaddr = ifp->if_addr;
dc39362e
BJ
784 ifp->if_flags = IFF_UP|IFF_POINTOPOINT;
785 ifp->if_output = looutput;
75334b2a 786 if_attach(ifp);
c46ba114 787 rtinit(&ifp->if_addr, &ifp->if_addr, RTF_UP|RTF_DIRECT|RTF_HOST);
75334b2a
BF
788}
789#endif