xref: /netbsd-src/sys/arch/sun2/dev/if_ec.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: if_ec.c,v 1.11 2005/12/11 12:19:16 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matthew Fredette.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * 3Com 3C400 device driver
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: if_ec.c,v 1.11 2005/12/11 12:19:16 christos Exp $");
45 
46 #include "opt_inet.h"
47 #include "opt_ns.h"
48 #include "bpfilter.h"
49 #include "rnd.h"
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/errno.h>
54 #include <sys/ioctl.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <sys/device.h>
59 #include <sys/endian.h>
60 #if NRND > 0
61 #include <sys/rnd.h>
62 #endif
63 
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 
68 #include <net/if_ether.h>
69 #include <net/if_media.h>
70 
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/if_inarp.h>
77 #endif
78 
79 #ifdef NS
80 #include <netns/ns.h>
81 #include <netns/ns_if.h>
82 #endif
83 
84 #if NBPFILTER > 0
85 #include <net/bpf.h>
86 #include <net/bpfdesc.h>
87 #endif
88 
89 #include <machine/cpu.h>
90 #include <machine/autoconf.h>
91 #include <machine/idprom.h>
92 #include <machine/bus.h>
93 #include <machine/intr.h>
94 
95 #include <sun2/dev/if_ecreg.h>
96 
97 /*
98  * Interface softc.
99  */
100 struct ec_softc {
101 	struct device sc_dev;
102 	void *sc_ih;
103 
104 	struct ethercom sc_ethercom;	/* ethernet common */
105 	struct ifmedia sc_media;	/* our supported media */
106 
107 	bus_space_tag_t sc_iot;	/* bus space tag */
108 	bus_space_handle_t sc_ioh;	/* bus space handle */
109 
110 	u_char sc_jammed;	/* nonzero if the net is jammed */
111 	u_char sc_colliding;	/* nonzero if the net is colliding */
112 	uint32_t sc_backoff_seed;	/* seed for the backoff PRNG */
113 
114 #if NRND > 0
115 	rndsource_element_t rnd_source;
116 #endif
117 };
118 
119 /* Macros to read and write the CSR. */
120 #define	ECREG_CSR_RD bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR)
121 #define	ECREG_CSR_WR(val) bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR, val)
122 
123 /* After this many collisions, the packet is dropped. */
124 #define	EC_COLLISIONS_JAMMED		16
125 
126 /*
127  * Various constants used in the backoff pseudorandom
128  * number generator.
129  */
130 #define	EC_BACKOFF_PRNG_COLL_MAX	10
131 #define	EC_BACKOFF_PRNG_MUL		1103515245
132 #define	EC_BACKOFF_PRNG_ADD		12345
133 #define	EC_BACKOFF_PRNG_MASK		0x7fffffff
134 
135 /*
136  * Prototypes
137  */
138 int ec_intr(void *);
139 void ec_reset(struct ifnet *);
140 int ec_init(struct ifnet *);
141 int ec_ioctl(struct ifnet *, u_long, caddr_t);
142 void ec_watchdog(struct ifnet *);
143 void ec_start(struct ifnet *);
144 
145 void ec_recv(struct ec_softc *, int);
146 void ec_coll(struct ec_softc *);
147 void ec_copyin(struct ec_softc *, void *, int, size_t);
148 void ec_copyout(struct ec_softc *, const void *, int, size_t);
149 
150 int ec_mediachange(struct ifnet *);
151 void ec_mediastatus(struct ifnet *, struct ifmediareq *);
152 
153 int ec_match(struct device *, struct cfdata *, void *);
154 void ec_attach(struct device *, struct device *, void *);
155 
156 CFATTACH_DECL(ec, sizeof(struct ec_softc),
157     ec_match, ec_attach, NULL, NULL);
158 
159 /*
160  * Copy board memory to kernel.
161  */
162 void
163 ec_copyin(struct ec_softc *sc, void *p, int offset, size_t size)
164 {
165 	bus_space_copyin(sc->sc_iot, sc->sc_ioh, offset, p, size);
166 }
167 
168 /*
169  * Copy from kernel space to board memory.
170  */
171 void
172 ec_copyout(struct ec_softc *sc, const void *p, int offset, size_t size)
173 {
174 	bus_space_copyout(sc->sc_iot, sc->sc_ioh, offset, p, size);
175 }
176 
177 int
178 ec_match(struct device *parent, struct cfdata *match, void *aux)
179 {
180 	struct mbmem_attach_args *mbma = aux;
181 	bus_space_handle_t bh;
182 	int matched;
183 
184 	/* No default Multibus address. */
185 	if (mbma->mbma_paddr == -1)
186 		return (0);
187 
188 	/* Make sure there is something there... */
189 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
190 			  0, &bh))
191 		return (0);
192 	matched = (bus_space_peek_2(mbma->mbma_bustag, bh, 0, NULL) == 0);
193 	bus_space_unmap(mbma->mbma_bustag, bh, ECREG_BANK_SZ);
194 	if (!matched)
195 		return (0);
196 
197 	/* Default interrupt priority. */
198 	if (mbma->mbma_pri == -1)
199 		mbma->mbma_pri = 3;
200 
201 	return (1);
202 }
203 
204 void
205 ec_attach(struct device *parent, struct device *self, void *aux)
206 {
207 	struct ec_softc *sc = (void *) self;
208 	struct mbmem_attach_args *mbma = aux;
209 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
210 	uint8_t myaddr[ETHER_ADDR_LEN];
211 
212 	printf("\n");
213 
214 	/* Map in the board control regs. */
215 	sc->sc_iot = mbma->mbma_bustag;
216 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
217 		0, &sc->sc_ioh))
218 		panic("ec_attach: can't map regs");
219 
220 	/* Reset the board. */
221 	ECREG_CSR_WR(EC_CSR_RESET);
222 	delay(160);
223 
224 	/*
225 	 * Copy out the board ROM Ethernet address,
226 	 * and use the non-vendor-ID part to seed
227 	 * our backoff pseudorandom number generator.
228 	 */
229 	bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, ECREG_AROM, myaddr, ETHER_ADDR_LEN);
230 	sc->sc_backoff_seed = (myaddr[3] << 16) | (myaddr[4] << 8) | (myaddr[5]) | 1;
231 
232 	/* Initialize ifnet structure. */
233 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
234 	ifp->if_softc = sc;
235 	ifp->if_start = ec_start;
236 	ifp->if_ioctl = ec_ioctl;
237 	ifp->if_init = ec_init;
238 	ifp->if_watchdog = ec_watchdog;
239 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
240 	IFQ_SET_READY(&ifp->if_snd);
241 
242         /* Initialize ifmedia structures. */
243 	ifmedia_init(&sc->sc_media, 0, ec_mediachange, ec_mediastatus);
244 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
245 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
246 
247 	/* Now we can attach the interface. */
248 	if_attach(ifp);
249 	idprom_etheraddr(myaddr);
250 	ether_ifattach(ifp, myaddr);
251 	printf("%s: address %s\n", self->dv_xname, ether_sprintf(myaddr));
252 
253 	bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_NET, 0,
254 	    ec_intr, sc);
255 
256 #if NRND > 0
257 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
258 	    RND_TYPE_NET, 0);
259 #endif
260 }
261 
262 /*
263  * Reset interface.
264  */
265 void
266 ec_reset(struct ifnet *ifp)
267 {
268         int s;
269 
270         s = splnet();
271         ec_init(ifp);
272         splx(s);
273 }
274 
275 
276 /*
277  * Initialize interface.
278  */
279 int
280 ec_init(struct ifnet *ifp)
281 {
282 	struct ec_softc *sc = ifp->if_softc;
283 
284 	/* Reset the board. */
285 	ECREG_CSR_WR(EC_CSR_RESET);
286 	delay(160);
287 
288 	/* Set the Ethernet address. */
289 	bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, ECREG_ARAM, LLADDR(sc->sc_ethercom.ec_if.if_sadl), ETHER_ADDR_LEN);
290 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_AMSW);
291 	ECREG_CSR_WR(ECREG_CSR_RD & 0);
292 
293 	/* Enable interrupts. */
294 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_BINT | EC_CSR_AINT | (ifp->if_flags & IFF_PROMISC ? EC_CSR_PROMISC : EC_CSR_PA));
295 
296 	/* Set flags appropriately. */
297 	ifp->if_flags |= IFF_RUNNING;
298 	ifp->if_flags &= ~IFF_OACTIVE;
299 
300 	/* Start output. */
301 	ec_start(ifp);
302 
303 	return (0);
304 }
305 
306 /*
307  * Start output on interface.
308  */
309 void
310 ec_start(struct ifnet *ifp)
311 {
312 	struct ec_softc *sc = ifp->if_softc;
313 	struct mbuf *m, *m0;
314 	int s;
315 	u_int count, realcount;
316 	bus_size_t off;
317 	static uint8_t padding[ETHER_MIN_LEN - ETHER_CRC_LEN] = {0};
318 
319 	s = splnet();
320 
321 	/* Don't do anything if output is active. */
322 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
323 		splx(s);
324 		return;
325 	}
326 	/* Don't do anything if the output queue is empty. */
327 	IFQ_DEQUEUE(&ifp->if_snd, m0);
328 	if (m0 == NULL) {
329 		splx(s);
330 		return;
331 	}
332 
333 #if NBPFILTER > 0
334 	/* The BPF tap. */
335 	if (ifp->if_bpf)
336 		bpf_mtap(ifp->if_bpf, m0);
337 #endif
338 
339 	/* Size the packet. */
340 	count = EC_BUF_SZ - m0->m_pkthdr.len;
341 
342 	/* Copy the packet into the xmit buffer. */
343 	realcount = MIN(count, EC_PKT_MAXTDOFF);
344 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, realcount);
345 	for (off = realcount, m = m0; m != 0; off += m->m_len, m = m->m_next)
346 		ec_copyout(sc, mtod(m, uint8_t *), ECREG_TBUF + off, m->m_len);
347 	m_freem(m0);
348 	if (count - realcount)
349 		ec_copyout(sc, padding, ECREG_TBUF + off, count - realcount);
350 
351 	/* Enable the transmitter. */
352 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_PA) | EC_CSR_TBSW | EC_CSR_TINT | EC_CSR_JINT);
353 	ifp->if_flags |= IFF_OACTIVE;
354 
355 	/* Done. */
356 	splx(s);
357 }
358 
359 /*
360  * Controller interrupt.
361  */
362 int
363 ec_intr(void *arg)
364 {
365 	struct ec_softc *sc = arg;
366 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
367 	int recv_first;
368 	int recv_second;
369 	int retval;
370 	struct mbuf *m0;
371 
372 	retval = 0;
373 
374 	/* Check for received packet(s). */
375 	recv_first = recv_second = 0;
376 	switch (ECREG_CSR_RD & (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA)) {
377 
378 	case (EC_CSR_BBSW | EC_CSR_ABSW):
379 	case (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA):
380 		/* Neither buffer is full.  Is this a transmit interrupt?
381 		 * Acknowledge the interrupt ourselves. */
382 		ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK));
383 		ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_BINT | EC_CSR_AINT);
384 		break;
385 
386 	case EC_CSR_BBSW:
387 	case (EC_CSR_BBSW | EC_CSR_RBBA):
388 		/* Only the A buffer is full. */
389 		recv_first = EC_CSR_AINT;
390 		break;
391 
392 	case EC_CSR_ABSW:
393 	case (EC_CSR_ABSW | EC_CSR_RBBA):
394 		/* Only the B buffer is full. */
395 		recv_first = EC_CSR_BINT;
396 		break;
397 
398 	case 0:
399 		/* Both the A buffer and the B buffer are full, and the A
400 		 * buffer is older than the B buffer. */
401 		recv_first = EC_CSR_AINT;
402 		recv_second = EC_CSR_BINT;
403 		break;
404 
405 	case EC_CSR_RBBA:
406 		/* Both the A buffer and the B buffer are full, and the B
407 		 * buffer is older than the A buffer. */
408 		recv_first = EC_CSR_BINT;
409 		recv_second = EC_CSR_AINT;
410 		break;
411 	}
412 
413 	/* Receive packets. */
414 	if (recv_first) {
415 
416 		/* Acknowledge the interrupt. */
417 		ECREG_CSR_WR(ECREG_CSR_RD & ((EC_CSR_BINT | EC_CSR_AINT | EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK) ^ (recv_first | recv_second)));
418 
419 		/* Receive a packet. */
420 		ec_recv(sc, recv_first);
421 
422 		/* Receive a packet. */
423 		if (recv_second)
424 			ec_recv(sc, recv_second);
425 
426 		retval++;
427 	}
428 	/* Check for a transmitted packet. */
429 	if (ifp->if_flags & IFF_OACTIVE) {
430 
431 		/* If we got a collision. */
432 		if (ECREG_CSR_RD & EC_CSR_JAM) {
433 			ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
434 			sc->sc_ethercom.ec_if.if_collisions++;
435 			retval++;
436 			ec_coll(sc);
437 
438 		}
439 		/* If we transmitted a packet. */
440 		else if ((ECREG_CSR_RD & EC_CSR_TBSW) == 0) {
441 			ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
442 			retval++;
443 			sc->sc_ethercom.ec_if.if_opackets++;
444 			sc->sc_jammed = 0;
445 			ifp->if_flags &= ~IFF_OACTIVE;
446 			IFQ_POLL(&ifp->if_snd, m0);
447 			if (m0 != NULL)
448 				ec_start(ifp);
449 		}
450 	} else {
451 
452 		/* Make sure we disable transmitter interrupts. */
453 		ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
454 	}
455 
456 	return retval;
457 }
458 
459 /*
460  * Read in a packet from the board.
461  */
462 void
463 ec_recv(struct ec_softc *sc, int intbit)
464 {
465 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
466 	struct mbuf *m0, *m, *newm;
467 	bus_size_t buf;
468 	uint16_t status;
469 	uint16_t doff;
470 	int length, total_length;
471 
472 	buf = EC_CSR_INT_BUF(intbit);
473 
474 	/* Read in the packet status. */
475 	status = bus_space_read_2(sc->sc_iot, sc->sc_ioh, buf);
476 	doff = status & EC_PKT_DOFF;
477 
478 	for (total_length = -1, m0 = 0;;) {
479 
480 		/* Check for an error. */
481 		if (status & (EC_PKT_FCSERR | EC_PKT_RGERR | EC_PKT_FRERR) ||
482 		    doff < EC_PKT_MINRDOFF ||
483 		    doff > EC_PKT_MAXRDOFF) {
484 			printf("%s: garbled packet, status 0x%04x; dropping\n",
485 			    sc->sc_dev.dv_xname, (unsigned int) status);
486 			break;
487 		}
488 
489 		/* Adjust for the header. */
490 		total_length = doff - EC_PKT_RDOFF;
491 		buf += EC_PKT_RDOFF;
492 
493 		/* XXX - sometimes the card reports a large data offset. */
494 		if (total_length > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
495 #ifdef DEBUG
496 			printf("%s: fixing too-large length of %d\n",
497 			    sc->sc_dev.dv_xname, total_length);
498 #endif
499 			total_length = (ETHER_MAX_LEN - ETHER_CRC_LEN);
500 		}
501 
502 		MGETHDR(m0, M_DONTWAIT, MT_DATA);
503 		if (m0 == 0)
504 			break;
505 		m0->m_pkthdr.rcvif = ifp;
506 		m0->m_pkthdr.len = total_length;
507 		length = MHLEN;
508 		m = m0;
509 
510 		while (total_length > 0) {
511 			if (total_length >= MINCLSIZE) {
512 				MCLGET(m, M_DONTWAIT);
513 				if ((m->m_flags & M_EXT) == 0)
514 					break;
515 				length = MCLBYTES;
516 			}
517 			m->m_len = length = min(total_length, length);
518 			ec_copyin(sc, mtod(m, uint8_t *), buf, length);
519 			total_length -= length;
520 			buf += length;
521 
522 			if (total_length > 0) {
523 				MGET(newm, M_DONTWAIT, MT_DATA);
524 				if (newm == 0)
525 					break;
526 				length = MLEN;
527 				m = m->m_next = newm;
528 			}
529 		}
530 		break;
531 	}
532 
533 	if (total_length == 0) {
534 		ifp->if_ipackets++;
535 
536 #if NBPFILTER > 0
537 		/*
538 	 	* Check if there's a BPF listener on this interface.
539 	 	* If so, hand off the raw packet to BPF.
540 	 	*/
541 		if (ifp->if_bpf)
542 			bpf_mtap(ifp->if_bpf, m0);
543 #endif
544 
545 		/* Pass the packet up. */
546 		(*ifp->if_input) (ifp, m0);
547 
548 	} else {
549 		/* Something went wrong. */
550 		if (m0)
551 			m_freem(m0);
552 		ifp->if_ierrors++;
553 	}
554 
555 	/* Give the receive buffer back to the card. */
556 	buf = EC_CSR_INT_BUF(intbit);
557 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
558 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_INT_BSW(intbit) | intbit);
559 }
560 
561 int
562 ec_mediachange(struct ifnet *ifp)
563 {
564 	return (0);
565 }
566 
567 void
568 ec_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
569 {
570 	if ((ifp->if_flags & IFF_UP) == 0)
571 		return;
572 
573 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
574 }
575 
576 /*
577  * Process an ioctl request. This code needs some work - it looks pretty ugly.
578  */
579 int
580 ec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
581 {
582 	struct ifaddr *ifa = (struct ifaddr *) data;
583 	struct ifreq *ifr = (struct ifreq *)data;
584 	struct ec_softc *sc = ifp->if_softc;
585 	int s, error = 0;
586 
587 	s = splnet();
588 
589 	switch (cmd) {
590 
591 	case SIOCSIFADDR:
592 		ifp->if_flags |= IFF_UP;
593 
594 		switch (ifa->ifa_addr->sa_family) {
595 #ifdef INET
596 		case AF_INET:
597 			ec_init(ifp);
598 			arp_ifinit(ifp, ifa);
599 			break;
600 #endif
601 #ifdef NS
602 			/* XXX - This code is probably wrong. */
603 		case AF_NS:
604 			{
605 				struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
606 
607 				if (ns_nullhost(*ina))
608 					ina->x_host =
609 					    *(union ns_host *) LLADDR(ifp->if_sadl);
610 				else
611 					bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
612 					    ETHER_ADDR_LEN);
613 				/* Set new address. */
614 				ec_init(ifp);
615 				break;
616 			}
617 #endif
618 		default:
619 			ec_init(ifp);
620 			break;
621 		}
622 		break;
623 
624 	case SIOCSIFFLAGS:
625 		if ((ifp->if_flags & IFF_UP) == 0 &&
626 		    (ifp->if_flags & IFF_RUNNING) != 0) {
627 			/*
628 			 * If interface is marked down and it is running, then
629 			 * stop it.
630 			 */
631 			ifp->if_flags &= ~IFF_RUNNING;
632 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
633 		    (ifp->if_flags & IFF_RUNNING) == 0) {
634 			/*
635 			 * If interface is marked up and it is stopped, then
636 			 * start it.
637 			 */
638 			ec_init(ifp);
639 		} else {
640 			/*
641 			 * Some other important flag might have changed, so
642 			 * reset.
643 			 */
644 			ec_reset(ifp);
645 		}
646 		break;
647 
648 	case SIOCGIFMEDIA:
649 	case SIOCSIFMEDIA:
650 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
651 		break;
652 
653 	default:
654 		error = EINVAL;
655 		break;
656 	}
657 
658 	splx(s);
659 	return error;
660 }
661 
662 /*
663  * Collision routine.
664  */
665 void
666 ec_coll(struct ec_softc *sc)
667 {
668 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
669 	u_short jams;
670 	struct mbuf *m0;
671 
672 	if ((++sc->sc_colliding) >= EC_COLLISIONS_JAMMED) {
673 		sc->sc_ethercom.ec_if.if_oerrors++;
674 		if (!sc->sc_jammed)
675 			printf("%s: ethernet jammed\n",
676 			    sc->sc_dev.dv_xname);
677 		sc->sc_jammed = 1;
678 		sc->sc_colliding = 0;
679 		ifp->if_flags &= ~IFF_OACTIVE;
680 		IFQ_POLL(&ifp->if_snd, m0);
681 		if (m0 != NULL)
682 			ec_start(ifp);
683 	} else {
684 		jams = MAX(sc->sc_colliding, EC_BACKOFF_PRNG_COLL_MAX);
685 		sc->sc_backoff_seed = ((sc->sc_backoff_seed * EC_BACKOFF_PRNG_MUL) + EC_BACKOFF_PRNG_ADD) & EC_BACKOFF_PRNG_MASK;
686 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_BACKOFF, -(((sc->sc_backoff_seed >> 8) & ~(-1 << jams)) + 1));
687 		ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_JAM | EC_CSR_TINT | EC_CSR_JINT);
688 	}
689 }
690 
691 /*
692  * Device timeout routine.
693  */
694 void
695 ec_watchdog(struct ifnet *ifp)
696 {
697 	struct ec_softc *sc = ifp->if_softc;
698 
699 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
700 	sc->sc_ethercom.ec_if.if_oerrors++;
701 
702 	ec_reset(ifp);
703 }
704