Home | History | Annotate | Line # | Download | only in dev
if_ec.c revision 1.26
      1 /*	$NetBSD: if_ec.c,v 1.26 2016/12/08 01:12:01 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.26 2016/12/08 01:12:01 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 	if_deferred_start_init(ifp, NULL);
    236 	idprom_etheraddr(myaddr);
    237 	ether_ifattach(ifp, myaddr);
    238 	aprint_normal_dev(self, "address %s\n", ether_sprintf(myaddr));
    239 
    240 	bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_NET, 0,
    241 	    ec_intr, sc);
    242 
    243 	rnd_attach_source(&sc->rnd_source, device_xname(self),
    244 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    245 }
    246 
    247 /*
    248  * Reset interface.
    249  */
    250 void
    251 ec_reset(struct ifnet *ifp)
    252 {
    253         int s;
    254 
    255         s = splnet();
    256         ec_init(ifp);
    257         splx(s);
    258 }
    259 
    260 
    261 /*
    262  * Initialize interface.
    263  */
    264 int
    265 ec_init(struct ifnet *ifp)
    266 {
    267 	struct ec_softc *sc = ifp->if_softc;
    268 
    269 	/* Reset the board. */
    270 	ECREG_CSR_WR(EC_CSR_RESET);
    271 	delay(160);
    272 
    273 	/* Set the Ethernet address. */
    274 	bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
    275 	    ECREG_ARAM, CLLADDR(sc->sc_ethercom.ec_if.if_sadl), ETHER_ADDR_LEN);
    276 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_AMSW);
    277 	ECREG_CSR_WR(ECREG_CSR_RD & 0);
    278 
    279 	/* Enable interrupts. */
    280 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
    281 	    EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_BINT | EC_CSR_AINT |
    282 	    (ifp->if_flags & IFF_PROMISC ? EC_CSR_PROMISC : EC_CSR_PA));
    283 
    284 	/* Set flags appropriately. */
    285 	ifp->if_flags |= IFF_RUNNING;
    286 	ifp->if_flags &= ~IFF_OACTIVE;
    287 
    288 	/* Start output. */
    289 	ec_start(ifp);
    290 
    291 	return 0;
    292 }
    293 
    294 /*
    295  * Start output on interface.
    296  */
    297 void
    298 ec_start(struct ifnet *ifp)
    299 {
    300 	struct ec_softc *sc = ifp->if_softc;
    301 	struct mbuf *m, *m0;
    302 	int s;
    303 	u_int count, realcount;
    304 	bus_size_t off;
    305 	static uint8_t padding[ETHER_MIN_LEN - ETHER_CRC_LEN] = {0};
    306 
    307 	s = splnet();
    308 
    309 	/* Don't do anything if output is active. */
    310 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
    311 		splx(s);
    312 		return;
    313 	}
    314 	/* Don't do anything if the output queue is empty. */
    315 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    316 	if (m0 == NULL) {
    317 		splx(s);
    318 		return;
    319 	}
    320 
    321 	/* The BPF tap. */
    322 	bpf_mtap(ifp, m0);
    323 
    324 	/* Size the packet. */
    325 	count = EC_BUF_SZ - m0->m_pkthdr.len;
    326 
    327 	/* Copy the packet into the xmit buffer. */
    328 	realcount = MIN(count, EC_PKT_MAXTDOFF);
    329 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, realcount);
    330 	for (off = realcount, m = m0; m != 0; off += m->m_len, m = m->m_next)
    331 		ec_copyout(sc, mtod(m, uint8_t *), ECREG_TBUF + off, m->m_len);
    332 	m_freem(m0);
    333 	if (count - realcount)
    334 		ec_copyout(sc, padding, ECREG_TBUF + off, count - realcount);
    335 
    336 	/* Enable the transmitter. */
    337 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_PA) |
    338 	    EC_CSR_TBSW | EC_CSR_TINT | EC_CSR_JINT);
    339 	ifp->if_flags |= IFF_OACTIVE;
    340 
    341 	/* Done. */
    342 	splx(s);
    343 }
    344 
    345 /*
    346  * Controller interrupt.
    347  */
    348 int
    349 ec_intr(void *arg)
    350 {
    351 	struct ec_softc *sc = arg;
    352 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    353 	int recv_first;
    354 	int recv_second;
    355 	int retval;
    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 			if_schedule_deferred_start(ifp);
    438 		}
    439 	} else {
    440 
    441 		/* Make sure we disable transmitter interrupts. */
    442 		ECREG_CSR_WR(ECREG_CSR_RD &
    443 		    (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
    444 	}
    445 
    446 	return retval;
    447 }
    448 
    449 /*
    450  * Read in a packet from the board.
    451  */
    452 void
    453 ec_recv(struct ec_softc *sc, int intbit)
    454 {
    455 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    456 	struct mbuf *m0, *m, *newm;
    457 	bus_size_t buf;
    458 	uint16_t status;
    459 	uint16_t doff;
    460 	int length, total_length;
    461 
    462 	buf = EC_CSR_INT_BUF(intbit);
    463 
    464 	/* Read in the packet status. */
    465 	status = bus_space_read_2(sc->sc_iot, sc->sc_ioh, buf);
    466 	doff = status & EC_PKT_DOFF;
    467 
    468 	for (total_length = -1, m0 = NULL;;) {
    469 
    470 		/* Check for an error. */
    471 		if (status & (EC_PKT_FCSERR | EC_PKT_RGERR | EC_PKT_FRERR) ||
    472 		    doff < EC_PKT_MINRDOFF ||
    473 		    doff > EC_PKT_MAXRDOFF) {
    474 			printf("%s: garbled packet, status 0x%04x; dropping\n",
    475 			    device_xname(sc->sc_dev), (unsigned int)status);
    476 			break;
    477 		}
    478 
    479 		/* Adjust for the header. */
    480 		total_length = doff - EC_PKT_RDOFF;
    481 		buf += EC_PKT_RDOFF;
    482 
    483 		/* XXX - sometimes the card reports a large data offset. */
    484 		if (total_length > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
    485 #ifdef DEBUG
    486 			printf("%s: fixing too-large length of %d\n",
    487 			    device_xname(sc->sc_dev), total_length);
    488 #endif
    489 			total_length = (ETHER_MAX_LEN - ETHER_CRC_LEN);
    490 		}
    491 
    492 		MGETHDR(m0, M_DONTWAIT, MT_DATA);
    493 		if (m0 == NULL)
    494 			break;
    495 		m_set_rcvif(m0, ifp);
    496 		m0->m_pkthdr.len = total_length;
    497 		length = MHLEN;
    498 		m = m0;
    499 
    500 		while (total_length > 0) {
    501 			if (total_length >= MINCLSIZE) {
    502 				MCLGET(m, M_DONTWAIT);
    503 				if ((m->m_flags & M_EXT) == 0)
    504 					break;
    505 				length = MCLBYTES;
    506 			}
    507 			m->m_len = length = min(total_length, length);
    508 			ec_copyin(sc, mtod(m, uint8_t *), buf, length);
    509 			total_length -= length;
    510 			buf += length;
    511 
    512 			if (total_length > 0) {
    513 				MGET(newm, M_DONTWAIT, MT_DATA);
    514 				if (newm == NULL)
    515 					break;
    516 				length = MLEN;
    517 				m = m->m_next = newm;
    518 			}
    519 		}
    520 		break;
    521 	}
    522 
    523 	if (total_length == 0) {
    524 		ifp->if_ipackets++;
    525 
    526 		/*
    527 	 	* Check if there's a BPF listener on this interface.
    528 	 	* If so, hand off the raw packet to BPF.
    529 	 	*/
    530 		bpf_mtap(ifp, m0);
    531 
    532 		/* Pass the packet up. */
    533 		if_percpuq_enqueue(ifp->if_percpuq, m0);
    534 
    535 	} else {
    536 		/* Something went wrong. */
    537 		if (m0 != NULL)
    538 			m_freem(m0);
    539 		ifp->if_ierrors++;
    540 	}
    541 
    542 	/* Give the receive buffer back to the card. */
    543 	buf = EC_CSR_INT_BUF(intbit);
    544 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
    545 	ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
    546 	    EC_CSR_INT_BSW(intbit) | intbit);
    547 }
    548 
    549 int
    550 ec_mediachange(struct ifnet *ifp)
    551 {
    552 
    553 	return 0;
    554 }
    555 
    556 void
    557 ec_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    558 {
    559 
    560 	if ((ifp->if_flags & IFF_UP) == 0)
    561 		return;
    562 
    563 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
    564 }
    565 
    566 /*
    567  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    568  */
    569 int
    570 ec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    571 {
    572 	struct ifaddr *ifa = (struct ifaddr *)data;
    573 	struct ifreq *ifr = (struct ifreq *)data;
    574 	struct ec_softc *sc = ifp->if_softc;
    575 	int s, error = 0;
    576 
    577 	s = splnet();
    578 
    579 	switch (cmd) {
    580 
    581 	case SIOCINITIFADDR:
    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 		default:
    592 			ec_init(ifp);
    593 			break;
    594 		}
    595 		break;
    596 
    597 	case SIOCSIFFLAGS:
    598 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    599 			break;
    600 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    601 		case IFF_RUNNING:
    602 			/*
    603 			 * If interface is marked down and it is running, then
    604 			 * stop it.
    605 			 */
    606 			ifp->if_flags &= ~IFF_RUNNING;
    607 			break;
    608 		case IFF_UP:
    609 			/*
    610 			 * If interface is marked up and it is stopped, then
    611 			 * start it.
    612 			 */
    613 			ec_init(ifp);
    614 			break;
    615 		default:
    616 			/*
    617 			 * Some other important flag might have changed, so
    618 			 * reset.
    619 			 */
    620 			ec_reset(ifp);
    621 			break;
    622 		}
    623 		break;
    624 
    625 	case SIOCGIFMEDIA:
    626 	case SIOCSIFMEDIA:
    627 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    628 		break;
    629 
    630 	default:
    631 		error = ether_ioctl(ifp, cmd, data);
    632 		break;
    633 	}
    634 
    635 	splx(s);
    636 	return error;
    637 }
    638 
    639 /*
    640  * Collision routine.
    641  */
    642 void
    643 ec_coll(struct ec_softc *sc)
    644 {
    645 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    646 	u_short jams;
    647 
    648 	if ((++sc->sc_colliding) >= EC_COLLISIONS_JAMMED) {
    649 		sc->sc_ethercom.ec_if.if_oerrors++;
    650 		if (!sc->sc_jammed)
    651 			printf("%s: ethernet jammed\n",
    652 			    device_xname(sc->sc_dev));
    653 		sc->sc_jammed = 1;
    654 		sc->sc_colliding = 0;
    655 		ifp->if_flags &= ~IFF_OACTIVE;
    656 		if_schedule_deferred_start(ifp);
    657 	} else {
    658 		jams = MAX(sc->sc_colliding, EC_BACKOFF_PRNG_COLL_MAX);
    659 		sc->sc_backoff_seed =
    660 		    ((sc->sc_backoff_seed * EC_BACKOFF_PRNG_MUL) +
    661 		    EC_BACKOFF_PRNG_ADD) & EC_BACKOFF_PRNG_MASK;
    662 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_BACKOFF,
    663 		    -(((sc->sc_backoff_seed >> 8) & ~(-1 << jams)) + 1));
    664 		ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
    665 		    EC_CSR_JAM | EC_CSR_TINT | EC_CSR_JINT);
    666 	}
    667 }
    668 
    669 /*
    670  * Device timeout routine.
    671  */
    672 void
    673 ec_watchdog(struct ifnet *ifp)
    674 {
    675 	struct ec_softc *sc = ifp->if_softc;
    676 
    677 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    678 	sc->sc_ethercom.ec_if.if_oerrors++;
    679 
    680 	ec_reset(ifp);
    681 }
    682