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