xref: /original-bsd/sys/vax/if/if_ec.c (revision e74403ba)
1 /*	if_ec.c	6.3	84/03/22	*/
2 
3 #include "ec.h"
4 
5 /*
6  * 3Com Ethernet Controller interface
7  */
8 #include "../machine/pte.h"
9 
10 #include "../h/param.h"
11 #include "../h/systm.h"
12 #include "../h/mbuf.h"
13 #include "../h/buf.h"
14 #include "../h/protosw.h"
15 #include "../h/socket.h"
16 #include "../h/vmmac.h"
17 #include "../h/ioctl.h"
18 #include "../h/errno.h"
19 
20 #include "../net/if.h"
21 #include "../net/netisr.h"
22 #include "../net/route.h"
23 #include "../netinet/in.h"
24 #include "../netinet/in_systm.h"
25 #include "../netinet/ip.h"
26 #include "../netinet/ip_var.h"
27 #include "../netinet/if_ether.h"
28 #include "../netpup/pup.h"
29 
30 #include "../vax/cpu.h"
31 #include "../vax/mtpr.h"
32 #include "../vaxif/if_ecreg.h"
33 #include "../vaxif/if_uba.h"
34 #include "../vaxuba/ubareg.h"
35 #include "../vaxuba/ubavar.h"
36 
37 #define	ECMEM	0000000
38 
39 int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
40 struct	uba_device *ecinfo[NEC];
41 u_short ecstd[] = { 0 };
42 struct	uba_driver ecdriver =
43 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
44 #define	ECUNIT(x)	minor(x)
45 
46 int	ecinit(),ecioctl(),ecoutput(),ecreset();
47 struct	mbuf *ecget();
48 
49 extern struct ifnet loif;
50 
51 /*
52  * Ethernet software status per interface.
53  *
54  * Each interface is referenced by a network interface structure,
55  * es_if, which the routing code uses to locate the interface.
56  * This structure contains the output queue for the interface, its address, ...
57  * We also have, for each interface, a UBA interface structure, which
58  * contains information about the UNIBUS resources held by the interface:
59  * map registers, buffered data paths, etc.  Information is cached in this
60  * structure for use by the if_uba.c routines in running the interface
61  * efficiently.
62  */
63 struct	ec_softc {
64 	struct	arpcom es_ac;		/* common Ethernet structures */
65 #define	es_if	es_ac.ac_if		/* network-visible interface */
66 #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
67 	struct	ifuba es_ifuba;		/* UNIBUS resources */
68 	short	es_mask;		/* mask for current output delay */
69 	short	es_oactive;		/* is output active? */
70 	u_char	*es_buf[16];		/* virtual addresses of buffers */
71 } ec_softc[NEC];
72 
73 /*
74  * Do output DMA to determine interface presence and
75  * interrupt vector.  DMA is too short to disturb other hosts.
76  */
77 ecprobe(reg)
78 	caddr_t reg;
79 {
80 	register int br, cvec;		/* r11, r10 value-result */
81 	register struct ecdevice *addr = (struct ecdevice *)reg;
82 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM];
83 
84 #ifdef lint
85 	br = 0; cvec = br; br = cvec;
86 	ecrint(0); ecxint(0); eccollide(0);
87 #endif
88 	/*
89 	 * Make sure memory is turned on
90 	 */
91 	addr->ec_rcr = EC_AROM;
92 	/*
93 	 * Disable map registers for ec unibus space,
94 	 * but don't allocate yet.
95 	 */
96 	(void) ubamem(numuba, ECMEM, 32*2, 0);
97 	/*
98 	 * Check for existence of buffers on Unibus.
99 	 */
100 	if (badaddr((caddr_t)ecbuf, 2)) {
101 	bad1:
102 		printf("ec: buffer mem not found\n");
103 	bad2:
104 		(void) ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
105 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
106 		return (0);
107 	}
108 #if VAX780
109 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
110 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
111 		goto bad1;
112 	}
113 #endif
114 
115 	/*
116 	 * Tell the system that the board has memory here, so it won't
117 	 * attempt to allocate the addresses later.
118 	 */
119 	if (ubamem(numuba, ECMEM, 32*2, 1) == 0) {
120 		printf("ecprobe: cannot reserve uba addresses\n");
121 		goto bad2;
122 	}
123 
124 	/*
125 	 * Make a one byte packet in what should be buffer #0.
126 	 * Submit it for sending.  This whould cause an xmit interrupt.
127 	 * The xmit interrupt vector is 8 bytes after the receive vector,
128 	 * so adjust for this before returning.
129 	 */
130 	*(u_short *)ecbuf = (u_short) 03777;
131 	ecbuf[03777] = '\0';
132 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
133 	DELAY(100000);
134 	addr->ec_xcr = EC_XCLR;
135 	if (cvec > 0 && cvec != 0x200) {
136 		if (cvec & 04) {	/* collision interrupt */
137 			cvec -= 04;
138 			br += 1;		/* rcv is collision + 1 */
139 		} else {		/* xmit interrupt */
140 			cvec -= 010;
141 			br += 2;		/* rcv is xmit + 2 */
142 		}
143 	}
144 	return (1);
145 }
146 
147 /*
148  * Interface exists: make available by filling in network interface
149  * record.  System will initialize the interface when it is ready
150  * to accept packets.
151  */
152 ecattach(ui)
153 	struct uba_device *ui;
154 {
155 	struct ec_softc *es = &ec_softc[ui->ui_unit];
156 	register struct ifnet *ifp = &es->es_if;
157 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
158 	struct sockaddr_in *sin;
159 	int i, j;
160 	u_char *cp;
161 
162 	ifp->if_unit = ui->ui_unit;
163 	ifp->if_name = "ec";
164 	ifp->if_mtu = ETHERMTU;
165 
166 	/*
167 	 * Read the ethernet address off the board, one nibble at a time.
168 	 */
169 	addr->ec_xcr = EC_UECLR;
170 	addr->ec_rcr = EC_AROM;
171 	cp = (u_char *) &es->es_addr;
172 #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
173 	for (i=0; i < sizeof (es->es_addr); i++) {
174 		*cp = 0;
175 		for (j=0; j<=4; j+=4) {
176 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
177 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
178 		}
179 		cp++;
180 	}
181 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
182 	sin->sin_family = AF_INET;
183 	ifp->if_init = ecinit;
184 	ifp->if_ioctl = ecioctl;
185 	ifp->if_output = ecoutput;
186 	ifp->if_reset = ecreset;
187 	for (i=0; i<16; i++)
188 		es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i];
189 	if_attach(ifp);
190 }
191 
192 /*
193  * Reset of interface after UNIBUS reset.
194  * If interface is on specified uba, reset its state.
195  */
196 ecreset(unit, uban)
197 	int unit, uban;
198 {
199 	register struct uba_device *ui;
200 
201 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
202 	    ui->ui_ubanum != uban)
203 		return;
204 	printf(" ec%d", unit);
205 	(void) ubamem(uban, ECMEM, 32*2, 0);	/* mr disable (no alloc) */
206 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
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 	struct ec_softc *es = &ec_softc[unit];
218 	struct ecdevice *addr;
219 	register struct ifnet *ifp = &es->es_if;
220 	register struct sockaddr_in *sin;
221 	int i, s;
222 
223 	sin = (struct sockaddr_in *)&ifp->if_addr;
224 	if (sin->sin_addr.s_addr == 0)		/* address still unknown */
225 		return;
226 
227 	/*
228 	 * Hang receive buffers and start any pending writes.
229 	 * Writing into the rcr also makes sure the memory
230 	 * is turned on.
231 	 */
232 	if ((es->es_if.if_flags & IFF_RUNNING) == 0) {
233 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
234 		s = splimp();
235 		for (i = ECRHBF; i >= ECRLBF; i--)
236 			addr->ec_rcr = EC_READ | i;
237 		es->es_oactive = 0;
238 		es->es_mask = ~0;
239 		es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
240 		if (es->es_if.if_snd.ifq_head)
241 			ecstart(unit);
242 		splx(s);
243 	}
244 	if_rtinit(&es->es_if, RTF_UP);
245 	arpwhohas(&es->es_ac, &sin->sin_addr);
246 }
247 
248 /*
249  * Start or restart output on interface.
250  * If interface is already active, then this is a retransmit
251  * after a collision, and just restuff registers.
252  * If interface is not already active, get another datagram
253  * to send off of the interface queue, and map it to the interface
254  * before starting the output.
255  */
256 ecstart(dev)
257 	dev_t dev;
258 {
259         int unit = ECUNIT(dev);
260 	struct ec_softc *es = &ec_softc[unit];
261 	struct ecdevice *addr;
262 	struct mbuf *m;
263 
264 	if (es->es_oactive)
265 		goto restart;
266 
267 	IF_DEQUEUE(&es->es_if.if_snd, m);
268 	if (m == 0) {
269 		es->es_oactive = 0;
270 		return;
271 	}
272 	ecput(es->es_buf[ECTBF], m);
273 
274 restart:
275 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
276 	addr->ec_xcr = EC_WRITE|ECTBF;
277 	es->es_oactive = 1;
278 }
279 
280 /*
281  * Ethernet interface transmitter interrupt.
282  * Start another output if more data to send.
283  */
284 ecxint(unit)
285 	int unit;
286 {
287 	register struct ec_softc *es = &ec_softc[unit];
288 	register struct ecdevice *addr =
289 		(struct ecdevice *)ecinfo[unit]->ui_addr;
290 
291 	if (es->es_oactive == 0)
292 		return;
293 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
294 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
295 			addr->ec_xcr, EC_XBITS);
296 		es->es_oactive = 0;
297 		addr->ec_xcr = EC_XCLR;
298 		return;
299 	}
300 	es->es_if.if_opackets++;
301 	es->es_oactive = 0;
302 	es->es_mask = ~0;
303 	addr->ec_xcr = EC_XCLR;
304 	if (es->es_if.if_snd.ifq_head)
305 		ecstart(unit);
306 }
307 
308 /*
309  * Collision on ethernet interface.  Do exponential
310  * backoff, and retransmit.  If have backed off all
311  * the way print warning diagnostic, and drop packet.
312  */
313 eccollide(unit)
314 	int unit;
315 {
316 	struct ec_softc *es = &ec_softc[unit];
317 
318 	es->es_if.if_collisions++;
319 	if (es->es_oactive)
320 		ecdocoll(unit);
321 }
322 
323 ecdocoll(unit)
324 	int unit;
325 {
326 	register struct ec_softc *es = &ec_softc[unit];
327 	register struct ecdevice *addr =
328 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
329 	register i;
330 	int delay;
331 
332 	/*
333 	 * Es_mask is a 16 bit number with n low zero bits, with
334 	 * n the number of backoffs.  When es_mask is 0 we have
335 	 * backed off 16 times, and give up.
336 	 */
337 	if (es->es_mask == 0) {
338 		es->es_if.if_oerrors++;
339 		printf("ec%d: send error\n", unit);
340 		/*
341 		 * Reset interface, then requeue rcv buffers.
342 		 * Some incoming packets may be lost, but that
343 		 * can't be helped.
344 		 */
345 		addr->ec_xcr = EC_UECLR;
346 		for (i=ECRHBF; i>=ECRLBF; i--)
347 			addr->ec_rcr = EC_READ|i;
348 		/*
349 		 * Reset and transmit next packet (if any).
350 		 */
351 		es->es_oactive = 0;
352 		es->es_mask = ~0;
353 		if (es->es_if.if_snd.ifq_head)
354 			ecstart(unit);
355 		return;
356 	}
357 	/*
358 	 * Do exponential backoff.  Compute delay based on low bits
359 	 * of the interval timer.  Then delay for that number of
360 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
361 	 * This does not take into account the time already used to
362 	 * process the interrupt.
363 	 */
364 	es->es_mask <<= 1;
365 	delay = mfpr(ICR) &~ es->es_mask;
366 	DELAY(delay * 51);
367 	/*
368 	 * Clear the controller's collision flag, thus enabling retransmit.
369 	 */
370 	addr->ec_xcr = EC_CLEAR;
371 }
372 
373 /*
374  * Ethernet interface receiver interrupt.
375  * If input error just drop packet.
376  * Otherwise purge input buffered data path and examine
377  * packet to determine type.  If can't determine length
378  * from type, then have to drop packet.  Othewise decapsulate
379  * packet based on type and pass to type specific higher-level
380  * input routine.
381  */
382 ecrint(unit)
383 	int unit;
384 {
385 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
386 
387 	while (addr->ec_rcr & EC_RDONE)
388 		ecread(unit);
389 }
390 
391 ecread(unit)
392 	int unit;
393 {
394 	register struct ec_softc *es = &ec_softc[unit];
395 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
396 	register struct ether_header *ec;
397     	struct mbuf *m;
398 	int len, off, resid, ecoff, rbuf;
399 	register struct ifqueue *inq;
400 	u_char *ecbuf;
401 
402 	es->es_if.if_ipackets++;
403 	rbuf = addr->ec_rcr & EC_RBN;
404 	if (rbuf < ECRLBF || rbuf > ECRHBF)
405 		panic("ecrint");
406 	ecbuf = es->es_buf[rbuf];
407 	ecoff = *(short *)ecbuf;
408 	if (ecoff <= ECRDOFF || ecoff > 2046) {
409 		es->es_if.if_ierrors++;
410 #ifdef notdef
411 		if (es->es_if.if_ierrors % 100 == 0)
412 			printf("ec%d: += 100 input errors\n", unit);
413 #endif
414 		goto setup;
415 	}
416 
417 	/*
418 	 * Get input data length.
419 	 * Get pointer to ethernet header (in input buffer).
420 	 * Deal with trailer protocol: if type is PUP trailer
421 	 * get true type from first 16-bit word past data.
422 	 * Remember that type was trailer by setting off.
423 	 */
424 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
425 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
426 	ec->ether_type = ntohs((u_short)ec->ether_type);
427 #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
428 	if (ec->ether_type >= ETHERPUP_TRAIL &&
429 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
430 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
431 		if (off >= ETHERMTU)
432 			goto setup;		/* sanity */
433 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
434 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
435 		if (off + resid > len)
436 			goto setup;		/* sanity */
437 		len = off + resid;
438 	} else
439 		off = 0;
440 	if (len == 0)
441 		goto setup;
442 
443 	/*
444 	 * Pull packet off interface.  Off is nonzero if packet
445 	 * has trailing header; ecget will then force this header
446 	 * information to be at the front, but we still have to drop
447 	 * the type and length which are at the front of any trailer data.
448 	 */
449 	m = ecget(ecbuf, len, off);
450 	if (m == 0)
451 		goto setup;
452 	if (off) {
453 		m->m_off += 2 * sizeof (u_short);
454 		m->m_len -= 2 * sizeof (u_short);
455 	}
456 	switch (ec->ether_type) {
457 
458 #ifdef INET
459 	case ETHERPUP_IPTYPE:
460 		schednetisr(NETISR_IP);
461 		inq = &ipintrq;
462 		break;
463 
464 	case ETHERPUP_ARPTYPE:
465 		arpinput(&es->es_ac, m);
466 		goto setup;
467 #endif
468 	default:
469 		m_freem(m);
470 		goto setup;
471 	}
472 
473 	if (IF_QFULL(inq)) {
474 		IF_DROP(inq);
475 		m_freem(m);
476 		goto setup;
477 	}
478 	IF_ENQUEUE(inq, m);
479 
480 setup:
481 	/*
482 	 * Reset for next packet.
483 	 */
484 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
485 }
486 
487 /*
488  * Ethernet output routine.
489  * Encapsulate a packet of type family for the local net.
490  * Use trailer local net encapsulation if enough data in first
491  * packet leaves a multiple of 512 bytes of data in remainder.
492  * If destination is this address or broadcast, send packet to
493  * loop device to kludge around the fact that 3com interfaces can't
494  * talk to themselves.
495  */
496 ecoutput(ifp, m0, dst)
497 	struct ifnet *ifp;
498 	struct mbuf *m0;
499 	struct sockaddr *dst;
500 {
501 	int type, s, error;
502 	struct ether_addr edst;
503 	struct in_addr idst;
504 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
505 	register struct mbuf *m = m0;
506 	register struct ether_header *ec;
507 	register int off;
508 	struct mbuf *mcopy = (struct mbuf *)0;
509 
510 	switch (dst->sa_family) {
511 
512 #ifdef INET
513 	case AF_INET:
514 		idst = ((struct sockaddr_in *)dst)->sin_addr;
515 		if (!arpresolve(&es->es_ac, m, &idst, &edst))
516 			return (0);	/* if not yet resolved */
517 		if (in_lnaof(idst) == INADDR_ANY)
518 			mcopy = m_copy(m, 0, (int)M_COPYALL);
519 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
520 		/* need per host negotiation */
521 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
522 		if (off > 0 && (off & 0x1ff) == 0 &&
523 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
524 			type = ETHERPUP_TRAIL + (off>>9);
525 			m->m_off -= 2 * sizeof (u_short);
526 			m->m_len += 2 * sizeof (u_short);
527 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
528 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
529 			goto gottrailertype;
530 		}
531 		type = ETHERPUP_IPTYPE;
532 		off = 0;
533 		goto gottype;
534 #endif
535 
536 	case AF_UNSPEC:
537 		ec = (struct ether_header *)dst->sa_data;
538 		edst = ec->ether_dhost;
539 		type = ec->ether_type;
540 		goto gottype;
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 ether_header) > m->m_off) {
568 		m = m_get(M_DONTWAIT, MT_HEADER);
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 ether_header);
576 	} else {
577 		m->m_off -= sizeof (struct ether_header);
578 		m->m_len += sizeof (struct ether_header);
579 	}
580 	ec = mtod(m, struct ether_header *);
581 	ec->ether_dhost = edst;
582 	ec->ether_shost = es->es_addr;
583 	ec->ether_type = htons((u_short)type);
584 
585 	/*
586 	 * Queue message on interface, and start output if interface
587 	 * not yet active.
588 	 */
589 	s = splimp();
590 	if (IF_QFULL(&ifp->if_snd)) {
591 		IF_DROP(&ifp->if_snd);
592 		error = ENOBUFS;
593 		goto qfull;
594 	}
595 	IF_ENQUEUE(&ifp->if_snd, m);
596 	if (es->es_oactive == 0)
597 		ecstart(ifp->if_unit);
598 	splx(s);
599 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
600 
601 qfull:
602 	m0 = m;
603 	splx(s);
604 bad:
605 	m_freem(m0);
606 	if (mcopy)
607 		m_freem(mcopy);
608 	return (error);
609 }
610 
611 /*
612  * Routine to copy from mbuf chain to transmit
613  * buffer in UNIBUS memory.
614  * If packet size is less than the minimum legal size,
615  * the buffer is expanded.  We probably should zero out the extra
616  * bytes for security, but that would slow things down.
617  */
618 ecput(ecbuf, m)
619 	u_char *ecbuf;
620 	struct mbuf *m;
621 {
622 	register struct mbuf *mp;
623 	register int off;
624 	u_char *bp;
625 
626 	for (off = 2048, mp = m; mp; mp = mp->m_next)
627 		off -= mp->m_len;
628 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
629 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
630 	*(u_short *)ecbuf = off;
631 	bp = (u_char *)(ecbuf + off);
632 	for (mp = m; mp; mp = mp->m_next) {
633 		register unsigned len = mp->m_len;
634 		u_char *mcp;
635 
636 		if (len == 0)
637 			continue;
638 		mcp = mtod(mp, u_char *);
639 		if ((unsigned)bp & 01) {
640 			*bp++ = *mcp++;
641 			len--;
642 		}
643 		if (off = (len >> 1)) {
644 			register u_short *to, *from;
645 
646 			to = (u_short *)bp;
647 			from = (u_short *)mcp;
648 			do
649 				*to++ = *from++;
650 			while (--off > 0);
651 			bp = (u_char *)to,
652 			mcp = (u_char *)from;
653 		}
654 		if (len & 01)
655 			*bp++ = *mcp++;
656 	}
657 	m_freem(m);
658 }
659 
660 /*
661  * Routine to copy from UNIBUS memory into mbufs.
662  * Similar in spirit to if_rubaget.
663  *
664  * Warning: This makes the fairly safe assumption that
665  * mbufs have even lengths.
666  */
667 struct mbuf *
668 ecget(ecbuf, totlen, off0)
669 	u_char *ecbuf;
670 	int totlen, off0;
671 {
672 	register struct mbuf *m;
673 	struct mbuf *top = 0, **mp = &top;
674 	register int off = off0, len;
675 	u_char *cp;
676 
677 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
678 	while (totlen > 0) {
679 		register int words;
680 		u_char *mcp;
681 
682 		MGET(m, M_DONTWAIT, MT_DATA);
683 		if (m == 0)
684 			goto bad;
685 		if (off) {
686 			len = totlen - off;
687 			cp = ecbuf + ECRDOFF +
688 				sizeof (struct ether_header) + off;
689 		} else
690 			len = totlen;
691 		if (len >= CLBYTES) {
692 			struct mbuf *p;
693 
694 			MCLGET(p, 1);
695 			if (p != 0) {
696 				m->m_len = len = CLBYTES;
697 				m->m_off = (int)p - (int)m;
698 			} else {
699 				m->m_len = len = MIN(MLEN, len);
700 				m->m_off = MMINOFF;
701 			}
702 		} else {
703 			m->m_len = len = MIN(MLEN, len);
704 			m->m_off = MMINOFF;
705 		}
706 		mcp = mtod(m, u_char *);
707 		if (words = (len >> 1)) {
708 			register u_short *to, *from;
709 
710 			to = (u_short *)mcp;
711 			from = (u_short *)cp;
712 			do
713 				*to++ = *from++;
714 			while (--words > 0);
715 			mcp = (u_char *)to;
716 			cp = (u_char *)from;
717 		}
718 		if (len & 01)
719 			*mcp++ = *cp++;
720 		*mp = m;
721 		mp = &m->m_next;
722 		if (off == 0) {
723 			totlen -= len;
724 			continue;
725 		}
726 		off += len;
727 		if (off == totlen) {
728 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
729 			off = 0;
730 			totlen = off0;
731 		}
732 	}
733 	return (top);
734 bad:
735 	m_freem(top);
736 	return (0);
737 }
738 
739 /*
740  * Process an ioctl request.
741  */
742 ecioctl(ifp, cmd, data)
743 	register struct ifnet *ifp;
744 	int cmd;
745 	caddr_t data;
746 {
747 	register struct ifreq *ifr = (struct ifreq *)data;
748 	int s = splimp(), error = 0;
749 
750 	switch (cmd) {
751 
752 	case SIOCSIFADDR:
753 		if (ifp->if_flags & IFF_RUNNING)
754 			if_rtinit(ifp, -1);	/* delete previous route */
755 		ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
756 		ecinit(ifp->if_unit);
757 		break;
758 
759 	default:
760 		error = EINVAL;
761 	}
762 	splx(s);
763 	return (error);
764 }
765 
766 ecsetaddr(ifp, sin)
767 	register struct ifnet *ifp;
768 	register struct sockaddr_in *sin;
769 {
770 
771 	ifp->if_addr = *(struct sockaddr *)sin;
772 	ifp->if_net = in_netof(sin->sin_addr);
773 	ifp->if_host[0] = in_lnaof(sin->sin_addr);
774 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
775 	sin->sin_family = AF_INET;
776 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
777 	ifp->if_flags |= IFF_BROADCAST;
778 }
779