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