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