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