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