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