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