xref: /original-bsd/sys/vax/if/if_ec.c (revision f0fd5f8a)
1 /*	if_ec.c	4.32	82/12/17	*/
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 <errno.h>
18 
19 #include "../net/if.h"
20 #include "../net/netisr.h"
21 #include "../net/route.h"
22 #include "../netinet/in.h"
23 #include "../netinet/in_systm.h"
24 #include "../netinet/ip.h"
25 #include "../netinet/ip_var.h"
26 #include "../netpup/pup.h"
27 
28 #include "../vax/cpu.h"
29 #include "../vax/mtpr.h"
30 #include "../vaxif/if_ether.h"
31 #include "../vaxif/if_ecreg.h"
32 #include "../vaxif/if_uba.h"
33 #include "../vaxuba/ubareg.h"
34 #include "../vaxuba/ubavar.h"
35 
36 #define	ECMEM	0000000
37 
38 int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
39 struct	uba_device *ecinfo[NEC];
40 u_short ecstd[] = { 0 };
41 struct	uba_driver ecdriver =
42 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
43 u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
44 u_char	ecbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
45 #define	ECUNIT(x)	minor(x)
46 
47 int	ecinit(),ecoutput(),ecreset();
48 struct	mbuf *ecget();
49 
50 extern struct ifnet loif;
51 
52 /*
53  * Ethernet software status per interface.
54  *
55  * Each interface is referenced by a network interface structure,
56  * es_if, which the routing code uses to locate the interface.
57  * This structure contains the output queue for the interface, its address, ...
58  * We also have, for each interface, a UBA interface structure, which
59  * contains information about the UNIBUS resources held by the interface:
60  * map registers, buffered data paths, etc.  Information is cached in this
61  * structure for use by the if_uba.c routines in running the interface
62  * efficiently.
63  */
64 struct	ec_softc {
65 	struct	ifnet es_if;		/* network-visible interface */
66 	struct	ifuba es_ifuba;		/* UNIBUS resources */
67 	short	es_mask;		/* mask for current output delay */
68 	short	es_oactive;		/* is output active? */
69 	u_char	*es_buf[16];		/* virtual addresses of buffers */
70 	u_char	es_enaddr[6];		/* board's ethernet address */
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 	ifp->if_net = ui->ui_flags;
166 
167 	/*
168 	 * Read the ethernet address off the board, one nibble at a time.
169 	 */
170 	addr->ec_xcr = EC_UECLR;
171 	addr->ec_rcr = EC_AROM;
172 	cp = es->es_enaddr;
173 #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
174 	for (i=0; i<6; i++) {
175 		*cp = 0;
176 		for (j=0; j<=4; j+=4) {
177 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
178 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
179 		}
180 		cp++;
181 	}
182 #ifdef notdef
183 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
184 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
185 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
186 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
187 #endif
188 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
189 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
190 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
191 	sin->sin_family = AF_INET;
192 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
193 
194 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
195 	sin->sin_family = AF_INET;
196 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
197 	ifp->if_flags = IFF_BROADCAST;
198 
199 	ifp->if_init = ecinit;
200 	ifp->if_output = ecoutput;
201 	ifp->if_reset = ecreset;
202 	for (i=0; i<16; i++)
203 		es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i];
204 	if_attach(ifp);
205 }
206 
207 /*
208  * Reset of interface after UNIBUS reset.
209  * If interface is on specified uba, reset its state.
210  */
211 ecreset(unit, uban)
212 	int unit, uban;
213 {
214 	register struct uba_device *ui;
215 
216 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
217 	    ui->ui_ubanum != uban)
218 		return;
219 	printf(" ec%d", unit);
220 	(void) ubamem(uban, ECMEM, 32*2, 0);	/* mr disable (no alloc) */
221 	ecinit(unit);
222 }
223 
224 /*
225  * Initialization of interface; clear recorded pending
226  * operations, and reinitialize UNIBUS usage.
227  */
228 ecinit(unit)
229 	int unit;
230 {
231 	struct ec_softc *es = &ec_softc[unit];
232 	struct ecdevice *addr;
233 	int i, s;
234 
235 	/*
236 	 * Hang receive buffers and start any pending writes.
237 	 * Writing into the rcr also makes sure the memory
238 	 * is turned on.
239 	 */
240 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
241 	s = splimp();
242 	for (i=ECRHBF; i>=ECRLBF; i--)
243 		addr->ec_rcr = EC_READ|i;
244 	es->es_oactive = 0;
245 	es->es_mask = ~0;
246 	es->es_if.if_flags |= IFF_UP;
247 	if (es->es_if.if_snd.ifq_head)
248 		ecstart(unit);
249 	splx(s);
250 	if_rtinit(&es->es_if, RTF_UP);
251 }
252 
253 /*
254  * Start or restart output on interface.
255  * If interface is already active, then this is a retransmit
256  * after a collision, and just restuff registers.
257  * If interface is not already active, get another datagram
258  * to send off of the interface queue, and map it to the interface
259  * before starting the output.
260  */
261 ecstart(dev)
262 	dev_t dev;
263 {
264         int unit = ECUNIT(dev);
265 	struct ec_softc *es = &ec_softc[unit];
266 	struct ecdevice *addr;
267 	struct mbuf *m;
268 
269 	if (es->es_oactive)
270 		goto restart;
271 
272 	IF_DEQUEUE(&es->es_if.if_snd, m);
273 	if (m == 0) {
274 		es->es_oactive = 0;
275 		return;
276 	}
277 	ecput(es->es_buf[ECTBF], m);
278 
279 restart:
280 	addr = (struct ecdevice *)ecinfo[unit]->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 ec_softc *es = &ec_softc[unit];
293 	register struct ecdevice *addr =
294 		(struct ecdevice *)ecinfo[unit]->ui_addr;
295 
296 	if (es->es_oactive == 0)
297 		return;
298 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
299 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
300 			addr->ec_xcr, EC_XBITS);
301 		es->es_oactive = 0;
302 		addr->ec_xcr = EC_XCLR;
303 		return;
304 	}
305 	es->es_if.if_opackets++;
306 	es->es_oactive = 0;
307 	es->es_mask = ~0;
308 	addr->ec_xcr = EC_XCLR;
309 	if (es->es_if.if_snd.ifq_head)
310 		ecstart(unit);
311 }
312 
313 /*
314  * Collision on ethernet interface.  Do exponential
315  * backoff, and retransmit.  If have backed off all
316  * the way print warning diagnostic, and drop packet.
317  */
318 eccollide(unit)
319 	int unit;
320 {
321 	struct ec_softc *es = &ec_softc[unit];
322 
323 	es->es_if.if_collisions++;
324 	if (es->es_oactive)
325 		ecdocoll(unit);
326 }
327 
328 ecdocoll(unit)
329 	int unit;
330 {
331 	register struct ec_softc *es = &ec_softc[unit];
332 	register struct ecdevice *addr =
333 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
334 	register i;
335 	int delay;
336 
337 	/*
338 	 * Es_mask is a 16 bit number with n low zero bits, with
339 	 * n the number of backoffs.  When es_mask is 0 we have
340 	 * backed off 16 times, and give up.
341 	 */
342 	if (es->es_mask == 0) {
343 		es->es_if.if_oerrors++;
344 		printf("ec%d: send error\n", unit);
345 		/*
346 		 * Reset interface, then requeue rcv buffers.
347 		 * Some incoming packets may be lost, but that
348 		 * can't be helped.
349 		 */
350 		addr->ec_xcr = EC_UECLR;
351 		for (i=ECRHBF; i>=ECRLBF; i--)
352 			addr->ec_rcr = EC_READ|i;
353 		/*
354 		 * Reset and transmit next packet (if any).
355 		 */
356 		es->es_oactive = 0;
357 		es->es_mask = ~0;
358 		if (es->es_if.if_snd.ifq_head)
359 			ecstart(unit);
360 		return;
361 	}
362 	/*
363 	 * Do exponential backoff.  Compute delay based on low bits
364 	 * of the interval timer.  Then delay for that number of
365 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
366 	 * This does not take into account the time already used to
367 	 * process the interrupt.
368 	 */
369 	es->es_mask <<= 1;
370 	delay = mfpr(ICR) &~ es->es_mask;
371 	DELAY(delay * 51);
372 	/*
373 	 * Clear the controller's collision flag, thus enabling retransmit.
374 	 */
375 	addr->ec_xcr = EC_CLEAR;
376 }
377 
378 /*
379  * Ethernet interface receiver interrupt.
380  * If input error just drop packet.
381  * Otherwise purge input buffered data path and examine
382  * packet to determine type.  If can't determine length
383  * from type, then have to drop packet.  Othewise decapsulate
384  * packet based on type and pass to type specific higher-level
385  * input routine.
386  */
387 ecrint(unit)
388 	int unit;
389 {
390 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
391 
392 	while (addr->ec_rcr & EC_RDONE)
393 		ecread(unit);
394 }
395 
396 ecread(unit)
397 	int unit;
398 {
399 	register struct ec_softc *es = &ec_softc[unit];
400 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
401 	register struct ether_header *ec;
402     	struct mbuf *m;
403 	int len, off, resid, ecoff, rbuf;
404 	register struct ifqueue *inq;
405 	u_char *ecbuf;
406 
407 	es->es_if.if_ipackets++;
408 	rbuf = addr->ec_rcr & EC_RBN;
409 	if (rbuf < ECRLBF || rbuf > ECRHBF)
410 		panic("ecrint");
411 	ecbuf = es->es_buf[rbuf];
412 	ecoff = *(short *)ecbuf;
413 	if (ecoff <= ECRDOFF || ecoff > 2046) {
414 		es->es_if.if_ierrors++;
415 #ifdef notdef
416 		if (es->es_if.if_ierrors % 100 == 0)
417 			printf("ec%d: += 100 input errors\n", unit);
418 #endif
419 		goto setup;
420 	}
421 
422 	/*
423 	 * Get input data length.
424 	 * Get pointer to ethernet header (in input buffer).
425 	 * Deal with trailer protocol: if type is PUP trailer
426 	 * get true type from first 16-bit word past data.
427 	 * Remember that type was trailer by setting off.
428 	 */
429 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
430 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
431 	ec->ether_type = ntohs((u_short)ec->ether_type);
432 #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
433 	if (ec->ether_type >= ETHERPUP_TRAIL &&
434 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
435 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
436 		if (off >= ETHERMTU)
437 			goto setup;		/* sanity */
438 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
439 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
440 		if (off + resid > len)
441 			goto setup;		/* sanity */
442 		len = off + resid;
443 	} else
444 		off = 0;
445 	if (len == 0)
446 		goto setup;
447 
448 	/*
449 	 * Pull packet off interface.  Off is nonzero if packet
450 	 * has trailing header; ecget will then force this header
451 	 * information to be at the front, but we still have to drop
452 	 * the type and length which are at the front of any trailer data.
453 	 */
454 	m = ecget(ecbuf, len, off);
455 	if (m == 0)
456 		goto setup;
457 	if (off) {
458 		m->m_off += 2 * sizeof (u_short);
459 		m->m_len -= 2 * sizeof (u_short);
460 	}
461 	switch (ec->ether_type) {
462 
463 #ifdef INET
464 	case ETHERPUP_IPTYPE:
465 		schednetisr(NETISR_IP);
466 		inq = &ipintrq;
467 		break;
468 #endif
469 	default:
470 		m_freem(m);
471 		goto setup;
472 	}
473 
474 	if (IF_QFULL(inq)) {
475 		IF_DROP(inq);
476 		m_freem(m);
477 		goto setup;
478 	}
479 	IF_ENQUEUE(inq, m);
480 
481 setup:
482 	/*
483 	 * Reset for next packet.
484 	 */
485 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
486 }
487 
488 /*
489  * Ethernet output routine.
490  * Encapsulate a packet of type family for the local net.
491  * Use trailer local net encapsulation if enough data in first
492  * packet leaves a multiple of 512 bytes of data in remainder.
493  * If destination is this address or broadcast, send packet to
494  * loop device to kludge around the fact that 3com interfaces can't
495  * talk to themselves.
496  */
497 ecoutput(ifp, m0, dst)
498 	struct ifnet *ifp;
499 	struct mbuf *m0;
500 	struct sockaddr *dst;
501 {
502 	int type, dest, s, error;
503 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
504 	register struct mbuf *m = m0;
505 	register struct ether_header *ec;
506 	register int off, i;
507 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
508 
509 	switch (dst->sa_family) {
510 
511 #ifdef INET
512 	case AF_INET:
513 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
514 		if ((dest &~ 0xff) == 0)
515 			mcopy = m_copy(m, 0, (int)M_COPYALL);
516 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
517 		    sin_addr.s_addr) {
518 			mcopy = m;
519 			goto gotlocal;
520 		}
521 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
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 	default:
537 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
538 			dst->sa_family);
539 		error = EAFNOSUPPORT;
540 		goto bad;
541 	}
542 
543 gottrailertype:
544 	/*
545 	 * Packet to be sent as trailer: move first packet
546 	 * (control information) to end of chain.
547 	 */
548 	while (m->m_next)
549 		m = m->m_next;
550 	m->m_next = m0;
551 	m = m0->m_next;
552 	m0->m_next = 0;
553 	m0 = m;
554 
555 gottype:
556 	/*
557 	 * Add local net header.  If no space in first mbuf,
558 	 * allocate another.
559 	 */
560 	if (m->m_off > MMAXOFF ||
561 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
562 		m = m_get(M_DONTWAIT, MT_HEADER);
563 		if (m == 0) {
564 			error = ENOBUFS;
565 			goto bad;
566 		}
567 		m->m_next = m0;
568 		m->m_off = MMINOFF;
569 		m->m_len = sizeof (struct ether_header);
570 	} else {
571 		m->m_off -= sizeof (struct ether_header);
572 		m->m_len += sizeof (struct ether_header);
573 	}
574 	ec = mtod(m, struct ether_header *);
575 	bcopy((caddr_t)es->es_enaddr, (caddr_t)ec->ether_shost, 6);
576 	if ((dest &~ 0xff) == 0)
577 		bcopy((caddr_t)ecbroadcastaddr, (caddr_t)ec->ether_dhost, 6);
578 	else {
579 		u_char *to = dest & 0x8000 ? ec_iltop : es->es_enaddr;
580 
581 		bcopy((caddr_t)to, (caddr_t)ec->ether_dhost, 3);
582 		ec->ether_dhost[3] = (dest>>8) & 0x7f;
583 		ec->ether_dhost[4] = (dest>>16) & 0xff;
584 		ec->ether_dhost[5] = (dest>>24) & 0xff;
585 	}
586 	ec->ether_type = htons((u_short)type);
587 
588 	/*
589 	 * Queue message on interface, and start output if interface
590 	 * not yet active.
591 	 */
592 	s = splimp();
593 	if (IF_QFULL(&ifp->if_snd)) {
594 		IF_DROP(&ifp->if_snd);
595 		error = ENOBUFS;
596 		goto qfull;
597 	}
598 	IF_ENQUEUE(&ifp->if_snd, m);
599 	if (es->es_oactive == 0)
600 		ecstart(ifp->if_unit);
601 	splx(s);
602 
603 gotlocal:
604 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
605 
606 qfull:
607 	m0 = m;
608 	splx(s);
609 bad:
610 	m_freem(m0);
611 	return(error);
612 }
613 
614 /*
615  * Routine to copy from mbuf chain to transmit
616  * buffer in UNIBUS memory.
617  * If packet size is less than the minimum legal size,
618  * the buffer is expanded.  We probably should zero out the extra
619  * bytes for security, but that would slow things down.
620  */
621 ecput(ecbuf, m)
622 	u_char *ecbuf;
623 	struct mbuf *m;
624 {
625 	register struct mbuf *mp;
626 	register int off;
627 	u_char *bp;
628 
629 	for (off = 2048, mp = m; mp; mp = mp->m_next)
630 		off -= mp->m_len;
631 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
632 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
633 	*(u_short *)ecbuf = off;
634 	bp = (u_char *)(ecbuf + off);
635 	for (mp = m; mp; mp = mp->m_next) {
636 		register unsigned len = mp->m_len;
637 		u_char *mcp;
638 
639 		if (len == 0)
640 			continue;
641 		mcp = mtod(mp, u_char *);
642 		if ((unsigned)bp & 01) {
643 			*bp++ = *mcp++;
644 			len--;
645 		}
646 		if (off = (len >> 1)) {
647 			register u_short *to, *from;
648 
649 			to = (u_short *)bp;
650 			from = (u_short *)mcp;
651 			do
652 				*to++ = *from++;
653 			while (--off > 0);
654 			bp = (u_char *)to,
655 			mcp = (u_char *)from;
656 		}
657 		if (len & 01)
658 			*bp++ = *mcp++;
659 	}
660 	m_freem(m);
661 }
662 
663 /*
664  * Routine to copy from UNIBUS memory into mbufs.
665  * Similar in spirit to if_rubaget.
666  *
667  * Warning: This makes the fairly safe assumption that
668  * mbufs have even lengths.
669  */
670 struct mbuf *
671 ecget(ecbuf, totlen, off0)
672 	u_char *ecbuf;
673 	int totlen, off0;
674 {
675 	register struct mbuf *m;
676 	struct mbuf *top = 0, **mp = &top;
677 	register int off = off0, len;
678 	u_char *cp;
679 
680 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
681 	while (totlen > 0) {
682 		register int words;
683 		u_char *mcp;
684 
685 		MGET(m, M_DONTWAIT, MT_DATA);
686 		if (m == 0)
687 			goto bad;
688 		if (off) {
689 			len = totlen - off;
690 			cp = ecbuf + ECRDOFF +
691 				sizeof (struct ether_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, u_char *);
710 		if (words = (len >> 1)) {
711 			register u_short *to, *from;
712 
713 			to = (u_short *)mcp;
714 			from = (u_short *)cp;
715 			do
716 				*to++ = *from++;
717 			while (--words > 0);
718 			mcp = (u_char *)to;
719 			cp = (u_char *)from;
720 		}
721 		if (len & 01)
722 			*mcp++ = *cp++;
723 		*mp = m;
724 		mp = &m->m_next;
725 		if (off == 0) {
726 			totlen -= len;
727 			continue;
728 		}
729 		off += len;
730 		if (off == totlen) {
731 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
732 			off = 0;
733 			totlen = off0;
734 		}
735 	}
736 	return (top);
737 bad:
738 	m_freem(top);
739 	return (0);
740 }
741