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