if_my.c revision 93746
193746Sjulian/*
293746Sjulian * Copyright (c) 2002 Myson Technology Inc.
393746Sjulian * All rights reserved.
493746Sjulian *
593746Sjulian * Redistribution and use in source and binary forms, with or without
693746Sjulian * modification, are permitted provided that the following conditions
793746Sjulian * are met:
893746Sjulian * 1. Redistributions of source code must retain the above copyright
993746Sjulian *    notice, this list of conditions, and the following disclaimer,
1093746Sjulian *    without modification, immediately at the beginning of the file.
1193746Sjulian * 2. The name of the author may not be used to endorse or promote products
1293746Sjulian *    derived from this software without specific prior written permission.
1393746Sjulian *
1493746Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1593746Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1693746Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1793746Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1893746Sjulian * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1993746Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2093746Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2193746Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2293746Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2393746Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2493746Sjulian * SUCH DAMAGE.
2593746Sjulian *
2693746Sjulian * Written by: yen_cw@myson.com.tw  available at: http://www.myson.com.tw/
2793746Sjulian *
2893746Sjulian * $FreeBSD: head/sys/dev/my/if_my.c 93746 2002-04-04 05:22:13Z julian $
2993746Sjulian *
3093746Sjulian * Myson fast ethernet PCI NIC driver
3193746Sjulian */
3293746Sjulian#include <sys/param.h>
3393746Sjulian#include <sys/systm.h>
3493746Sjulian#include <sys/sockio.h>
3593746Sjulian#include <sys/mbuf.h>
3693746Sjulian#include <sys/malloc.h>
3793746Sjulian#include <sys/kernel.h>
3893746Sjulian#include <sys/socket.h>
3993746Sjulian#include <sys/queue.h>
4093746Sjulian#include <sys/types.h>
4193746Sjulian#include <sys/bus.h>
4293746Sjulian#include <sys/module.h>
4393746Sjulian
4493746Sjulian#define NBPFILTER	1
4593746Sjulian
4693746Sjulian#include <net/if.h>
4793746Sjulian#include <net/if_arp.h>
4893746Sjulian#include <net/ethernet.h>
4993746Sjulian#include <net/if_media.h>
5093746Sjulian#include <net/if_dl.h>
5193746Sjulian#include <net/bpf.h>
5293746Sjulian
5393746Sjulian#include <vm/vm.h>		/* for vtophys */
5493746Sjulian#include <vm/pmap.h>		/* for vtophys */
5593746Sjulian#include <machine/clock.h>	/* for DELAY */
5693746Sjulian#include <machine/bus_memio.h>
5793746Sjulian#include <machine/bus_pio.h>
5893746Sjulian#include <machine/bus.h>
5993746Sjulian#include <machine/resource.h>
6093746Sjulian#include <sys/bus.h>
6193746Sjulian#include <sys/rman.h>
6293746Sjulian
6393746Sjulian#include <pci/pcireg.h>
6493746Sjulian#include <pci/pcivar.h>
6593746Sjulian
6693746Sjulian#include <dev/mii/mii.h>
6793746Sjulian#include <dev/mii/miivar.h>
6893746Sjulian
6993746Sjulian#include "miibus_if.h"
7093746Sjulian
7193746Sjulian/*
7293746Sjulian * #define MY_USEIOSPACE
7393746Sjulian */
7493746Sjulian
7593746Sjulianstatic int      MY_USEIOSPACE = 1;
7693746Sjulian
7793746Sjulian#if (MY_USEIOSPACE)
7893746Sjulian#define MY_RES			SYS_RES_IOPORT
7993746Sjulian#define MY_RID			MY_PCI_LOIO
8093746Sjulian#else
8193746Sjulian#define MY_RES			SYS_RES_MEMORY
8293746Sjulian#define MY_RID			MY_PCI_LOMEM
8393746Sjulian#endif
8493746Sjulian
8593746Sjulian
8693746Sjulian#include <dev/my/if_myreg.h>
8793746Sjulian
8893746Sjulian#ifndef lint
8993746Sjulianstatic          const char rcsid[] =
9093746Sjulian"$Id: if_my.c,v 1.50 2001/12/03 04:15:33 <yen_cw@myson.com.tw> wpaul Exp $";
9193746Sjulian#endif
9293746Sjulian
9393746Sjulian/*
9493746Sjulian * Various supported device vendors/types and their names.
9593746Sjulian */
9693746Sjulianstruct my_type *my_info_tmp;
9793746Sjulianstatic struct my_type my_devs[] = {
9893746Sjulian	{MYSONVENDORID, MTD800ID, "Myson MTD80X Based Fast Ethernet Card"},
9993746Sjulian	{MYSONVENDORID, MTD803ID, "Myson MTD80X Based Fast Ethernet Card"},
10093746Sjulian	{MYSONVENDORID, MTD891ID, "Myson MTD89X Based Giga Ethernet Card"},
10193746Sjulian	{0, 0, NULL}
10293746Sjulian};
10393746Sjulian
10493746Sjulian/*
10593746Sjulian * Various supported PHY vendors/types and their names. Note that this driver
10693746Sjulian * will work with pretty much any MII-compliant PHY, so failure to positively
10793746Sjulian * identify the chip is not a fatal error.
10893746Sjulian */
10993746Sjulianstatic struct my_type my_phys[] = {
11093746Sjulian	{MysonPHYID0, MysonPHYID0, "<MYSON MTD981>"},
11193746Sjulian	{SeeqPHYID0, SeeqPHYID0, "<SEEQ 80225>"},
11293746Sjulian	{AhdocPHYID0, AhdocPHYID0, "<AHDOC 101>"},
11393746Sjulian	{MarvellPHYID0, MarvellPHYID0, "<MARVELL 88E1000>"},
11493746Sjulian	{LevelOnePHYID0, LevelOnePHYID0, "<LevelOne LXT1000>"},
11593746Sjulian	{0, 0, "<MII-compliant physical interface>"}
11693746Sjulian};
11793746Sjulian
11893746Sjulianstatic int      my_probe(device_t);
11993746Sjulianstatic int      my_attach(device_t);
12093746Sjulianstatic int      my_detach(device_t);
12193746Sjulianstatic int      my_newbuf(struct my_softc *, struct my_chain_onefrag *);
12293746Sjulianstatic int      my_encap(struct my_softc *, struct my_chain *, struct mbuf *);
12393746Sjulianstatic void     my_rxeof(struct my_softc *);
12493746Sjulianstatic void     my_txeof(struct my_softc *);
12593746Sjulianstatic void     my_txeoc(struct my_softc *);
12693746Sjulianstatic void     my_intr(void *);
12793746Sjulianstatic void     my_start(struct ifnet *);
12893746Sjulianstatic int      my_ioctl(struct ifnet *, u_long, caddr_t);
12993746Sjulianstatic void     my_init(void *);
13093746Sjulianstatic void     my_stop(struct my_softc *);
13193746Sjulianstatic void     my_watchdog(struct ifnet *);
13293746Sjulianstatic void     my_shutdown(device_t);
13393746Sjulianstatic int      my_ifmedia_upd(struct ifnet *);
13493746Sjulianstatic void     my_ifmedia_sts(struct ifnet *, struct ifmediareq *);
13593746Sjulianstatic u_int16_t my_phy_readreg(struct my_softc *, int);
13693746Sjulianstatic void     my_phy_writereg(struct my_softc *, int, int);
13793746Sjulianstatic void     my_autoneg_xmit(struct my_softc *);
13893746Sjulianstatic void     my_autoneg_mii(struct my_softc *, int, int);
13993746Sjulianstatic void     my_setmode_mii(struct my_softc *, int);
14093746Sjulianstatic void     my_getmode_mii(struct my_softc *);
14193746Sjulianstatic void     my_setcfg(struct my_softc *, int);
14293746Sjulianstatic u_int8_t my_calchash(caddr_t);
14393746Sjulianstatic void     my_setmulti(struct my_softc *);
14493746Sjulianstatic void     my_reset(struct my_softc *);
14593746Sjulianstatic int      my_list_rx_init(struct my_softc *);
14693746Sjulianstatic int      my_list_tx_init(struct my_softc *);
14793746Sjulianstatic long     my_send_cmd_to_phy(struct my_softc *, int, int);
14893746Sjulian
14993746Sjulian#define MY_SETBIT(sc, reg, x) CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
15093746Sjulian#define MY_CLRBIT(sc, reg, x) CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
15193746Sjulian
15293746Sjulianstatic device_method_t my_methods[] = {
15393746Sjulian	/* Device interface */
15493746Sjulian	DEVMETHOD(device_probe, my_probe),
15593746Sjulian	DEVMETHOD(device_attach, my_attach),
15693746Sjulian	DEVMETHOD(device_detach, my_detach),
15793746Sjulian	DEVMETHOD(device_shutdown, my_shutdown),
15893746Sjulian
15993746Sjulian	{0, 0}
16093746Sjulian};
16193746Sjulian
16293746Sjulianstatic driver_t my_driver = {
16393746Sjulian	"my",
16493746Sjulian	my_methods,
16593746Sjulian	sizeof(struct my_softc)
16693746Sjulian};
16793746Sjulian
16893746Sjulianstatic devclass_t my_devclass;
16993746Sjulian
17093746SjulianDRIVER_MODULE(if_my, pci, my_driver, my_devclass, 0, 0);
17193746Sjulian
17293746Sjulianstatic long
17393746Sjulianmy_send_cmd_to_phy(struct my_softc * sc, int opcode, int regad)
17493746Sjulian{
17593746Sjulian	long            miir;
17693746Sjulian	int             i;
17793746Sjulian	int             mask, data;
17893746Sjulian
17993746Sjulian	MY_LOCK(sc);
18093746Sjulian
18193746Sjulian	/* enable MII output */
18293746Sjulian	miir = CSR_READ_4(sc, MY_MANAGEMENT);
18393746Sjulian	miir &= 0xfffffff0;
18493746Sjulian
18593746Sjulian	miir |= MY_MASK_MIIR_MII_WRITE + MY_MASK_MIIR_MII_MDO;
18693746Sjulian
18793746Sjulian	/* send 32 1's preamble */
18893746Sjulian	for (i = 0; i < 32; i++) {
18993746Sjulian		/* low MDC; MDO is already high (miir) */
19093746Sjulian		miir &= ~MY_MASK_MIIR_MII_MDC;
19193746Sjulian		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
19293746Sjulian
19393746Sjulian		/* high MDC */
19493746Sjulian		miir |= MY_MASK_MIIR_MII_MDC;
19593746Sjulian		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
19693746Sjulian	}
19793746Sjulian
19893746Sjulian	/* calculate ST+OP+PHYAD+REGAD+TA */
19993746Sjulian	data = opcode | (sc->my_phy_addr << 7) | (regad << 2);
20093746Sjulian
20193746Sjulian	/* sent out */
20293746Sjulian	mask = 0x8000;
20393746Sjulian	while (mask) {
20493746Sjulian		/* low MDC, prepare MDO */
20593746Sjulian		miir &= ~(MY_MASK_MIIR_MII_MDC + MY_MASK_MIIR_MII_MDO);
20693746Sjulian		if (mask & data)
20793746Sjulian			miir |= MY_MASK_MIIR_MII_MDO;
20893746Sjulian
20993746Sjulian		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
21093746Sjulian		/* high MDC */
21193746Sjulian		miir |= MY_MASK_MIIR_MII_MDC;
21293746Sjulian		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
21393746Sjulian		DELAY(30);
21493746Sjulian
21593746Sjulian		/* next */
21693746Sjulian		mask >>= 1;
21793746Sjulian		if (mask == 0x2 && opcode == MY_OP_READ)
21893746Sjulian			miir &= ~MY_MASK_MIIR_MII_WRITE;
21993746Sjulian	}
22093746Sjulian
22193746Sjulian	MY_UNLOCK(sc);
22293746Sjulian	return miir;
22393746Sjulian}
22493746Sjulian
22593746Sjulian
22693746Sjulianstatic          u_int16_t
22793746Sjulianmy_phy_readreg(struct my_softc * sc, int reg)
22893746Sjulian{
22993746Sjulian	long            miir;
23093746Sjulian	int             mask, data;
23193746Sjulian
23293746Sjulian	MY_LOCK(sc);
23393746Sjulian
23493746Sjulian	if (sc->my_info->my_did == MTD803ID)
23593746Sjulian		data = CSR_READ_2(sc, MY_PHYBASE + reg * 2);
23693746Sjulian	else {
23793746Sjulian		miir = my_send_cmd_to_phy(sc, MY_OP_READ, reg);
23893746Sjulian
23993746Sjulian		/* read data */
24093746Sjulian		mask = 0x8000;
24193746Sjulian		data = 0;
24293746Sjulian		while (mask) {
24393746Sjulian			/* low MDC */
24493746Sjulian			miir &= ~MY_MASK_MIIR_MII_MDC;
24593746Sjulian			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
24693746Sjulian
24793746Sjulian			/* read MDI */
24893746Sjulian			miir = CSR_READ_4(sc, MY_MANAGEMENT);
24993746Sjulian			if (miir & MY_MASK_MIIR_MII_MDI)
25093746Sjulian				data |= mask;
25193746Sjulian
25293746Sjulian			/* high MDC, and wait */
25393746Sjulian			miir |= MY_MASK_MIIR_MII_MDC;
25493746Sjulian			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
25593746Sjulian			DELAY(30);
25693746Sjulian
25793746Sjulian			/* next */
25893746Sjulian			mask >>= 1;
25993746Sjulian		}
26093746Sjulian
26193746Sjulian		/* low MDC */
26293746Sjulian		miir &= ~MY_MASK_MIIR_MII_MDC;
26393746Sjulian		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
26493746Sjulian	}
26593746Sjulian
26693746Sjulian	MY_UNLOCK(sc);
26793746Sjulian	return (u_int16_t) data;
26893746Sjulian}
26993746Sjulian
27093746Sjulian
27193746Sjulianstatic void
27293746Sjulianmy_phy_writereg(struct my_softc * sc, int reg, int data)
27393746Sjulian{
27493746Sjulian	long            miir;
27593746Sjulian	int             mask;
27693746Sjulian
27793746Sjulian	MY_LOCK(sc);
27893746Sjulian
27993746Sjulian	if (sc->my_info->my_did == MTD803ID)
28093746Sjulian		CSR_WRITE_2(sc, MY_PHYBASE + reg * 2, data);
28193746Sjulian	else {
28293746Sjulian		miir = my_send_cmd_to_phy(sc, MY_OP_WRITE, reg);
28393746Sjulian
28493746Sjulian		/* write data */
28593746Sjulian		mask = 0x8000;
28693746Sjulian		while (mask) {
28793746Sjulian			/* low MDC, prepare MDO */
28893746Sjulian			miir &= ~(MY_MASK_MIIR_MII_MDC + MY_MASK_MIIR_MII_MDO);
28993746Sjulian			if (mask & data)
29093746Sjulian				miir |= MY_MASK_MIIR_MII_MDO;
29193746Sjulian			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
29293746Sjulian			DELAY(1);
29393746Sjulian
29493746Sjulian			/* high MDC */
29593746Sjulian			miir |= MY_MASK_MIIR_MII_MDC;
29693746Sjulian			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
29793746Sjulian			DELAY(1);
29893746Sjulian
29993746Sjulian			/* next */
30093746Sjulian			mask >>= 1;
30193746Sjulian		}
30293746Sjulian
30393746Sjulian		/* low MDC */
30493746Sjulian		miir &= ~MY_MASK_MIIR_MII_MDC;
30593746Sjulian		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
30693746Sjulian	}
30793746Sjulian	MY_UNLOCK(sc);
30893746Sjulian	return;
30993746Sjulian}
31093746Sjulian
31193746Sjulianstatic          u_int8_t
31293746Sjulianmy_calchash(caddr_t addr)
31393746Sjulian{
31493746Sjulian	u_int32_t       crc, carry;
31593746Sjulian	int             i, j;
31693746Sjulian	u_int8_t        c;
31793746Sjulian
31893746Sjulian	/* Compute CRC for the address value. */
31993746Sjulian	crc = 0xFFFFFFFF;	/* initial value */
32093746Sjulian
32193746Sjulian	for (i = 0; i < 6; i++) {
32293746Sjulian		c = *(addr + i);
32393746Sjulian		for (j = 0; j < 8; j++) {
32493746Sjulian			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
32593746Sjulian			crc <<= 1;
32693746Sjulian			c >>= 1;
32793746Sjulian			if (carry)
32893746Sjulian				crc = (crc ^ 0x04c11db6) | carry;
32993746Sjulian		}
33093746Sjulian	}
33193746Sjulian
33293746Sjulian	/*
33393746Sjulian	 * return the filter bit position Note: I arrived at the following
33493746Sjulian	 * nonsense through experimentation. It's not the usual way to
33593746Sjulian	 * generate the bit position but it's the only thing I could come up
33693746Sjulian	 * with that works.
33793746Sjulian	 */
33893746Sjulian	return (~(crc >> 26) & 0x0000003F);
33993746Sjulian}
34093746Sjulian
34193746Sjulian
34293746Sjulian/*
34393746Sjulian * Program the 64-bit multicast hash filter.
34493746Sjulian */
34593746Sjulianstatic void
34693746Sjulianmy_setmulti(struct my_softc * sc)
34793746Sjulian{
34893746Sjulian	struct ifnet   *ifp;
34993746Sjulian	int             h = 0;
35093746Sjulian	u_int32_t       hashes[2] = {0, 0};
35193746Sjulian	struct ifmultiaddr *ifma;
35293746Sjulian	u_int32_t       rxfilt;
35393746Sjulian	int             mcnt = 0;
35493746Sjulian
35593746Sjulian	MY_LOCK(sc);
35693746Sjulian
35793746Sjulian	ifp = &sc->arpcom.ac_if;
35893746Sjulian
35993746Sjulian	rxfilt = CSR_READ_4(sc, MY_TCRRCR);
36093746Sjulian
36193746Sjulian	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
36293746Sjulian		rxfilt |= MY_AM;
36393746Sjulian		CSR_WRITE_4(sc, MY_TCRRCR, rxfilt);
36493746Sjulian		CSR_WRITE_4(sc, MY_MAR0, 0xFFFFFFFF);
36593746Sjulian		CSR_WRITE_4(sc, MY_MAR1, 0xFFFFFFFF);
36693746Sjulian
36793746Sjulian		MY_UNLOCK(sc);
36893746Sjulian
36993746Sjulian		return;
37093746Sjulian	}
37193746Sjulian	/* first, zot all the existing hash bits */
37293746Sjulian	CSR_WRITE_4(sc, MY_MAR0, 0);
37393746Sjulian	CSR_WRITE_4(sc, MY_MAR1, 0);
37493746Sjulian
37593746Sjulian	/* now program new ones */
37693746Sjulian	/*
37793746Sjulian	 * Add by Surfer for (ifma = ifp->if_multiaddrs.lh_first; ifma !=
37893746Sjulian	 * NULL; ifma = ifma->ifma_link.le_next) { if
37993746Sjulian	 * (ifma->ifma_addr->sa_family != AF_LINK) continue; h =
38093746Sjulian	 * my_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); if (h
38193746Sjulian	 * < 32) hashes[0] |= (1 << h); else hashes[1] |= (1 << (h - 32));
38293746Sjulian	 * mcnt++; }
38393746Sjulian	 */
38493746Sjulian
38593746Sjulian	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
38693746Sjulian		if (ifma->ifma_addr->sa_family != AF_LINK)
38793746Sjulian			continue;
38893746Sjulian		h = my_calchash(LLADDR((struct sockaddr_dl *) ifma->ifma_addr));
38993746Sjulian		if (h < 32)
39093746Sjulian			hashes[0] |= (1 << h);
39193746Sjulian		else
39293746Sjulian			hashes[1] |= (1 << (h - 32));
39393746Sjulian		mcnt++;
39493746Sjulian	}
39593746Sjulian
39693746Sjulian	if (mcnt)
39793746Sjulian		rxfilt |= MY_AM;
39893746Sjulian	else
39993746Sjulian		rxfilt &= ~MY_AM;
40093746Sjulian	CSR_WRITE_4(sc, MY_MAR0, hashes[0]);
40193746Sjulian	CSR_WRITE_4(sc, MY_MAR1, hashes[1]);
40293746Sjulian	CSR_WRITE_4(sc, MY_TCRRCR, rxfilt);
40393746Sjulian	MY_UNLOCK(sc);
40493746Sjulian	return;
40593746Sjulian}
40693746Sjulian
40793746Sjulian/*
40893746Sjulian * Initiate an autonegotiation session.
40993746Sjulian */
41093746Sjulianstatic void
41193746Sjulianmy_autoneg_xmit(struct my_softc * sc)
41293746Sjulian{
41393746Sjulian	u_int16_t       phy_sts = 0;
41493746Sjulian
41593746Sjulian	MY_LOCK(sc);
41693746Sjulian
41793746Sjulian	my_phy_writereg(sc, PHY_BMCR, PHY_BMCR_RESET);
41893746Sjulian	DELAY(500);
41993746Sjulian	while (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_RESET);
42093746Sjulian
42193746Sjulian	phy_sts = my_phy_readreg(sc, PHY_BMCR);
42293746Sjulian	phy_sts |= PHY_BMCR_AUTONEGENBL | PHY_BMCR_AUTONEGRSTR;
42393746Sjulian	my_phy_writereg(sc, PHY_BMCR, phy_sts);
42493746Sjulian
42593746Sjulian	MY_UNLOCK(sc);
42693746Sjulian	return;
42793746Sjulian}
42893746Sjulian
42993746Sjulian
43093746Sjulian/*
43193746Sjulian * Invoke autonegotiation on a PHY.
43293746Sjulian */
43393746Sjulianstatic void
43493746Sjulianmy_autoneg_mii(struct my_softc * sc, int flag, int verbose)
43593746Sjulian{
43693746Sjulian	u_int16_t       phy_sts = 0, media, advert, ability;
43793746Sjulian	u_int16_t       ability2 = 0;
43893746Sjulian	struct ifnet   *ifp;
43993746Sjulian	struct ifmedia *ifm;
44093746Sjulian
44193746Sjulian	MY_LOCK(sc);
44293746Sjulian
44393746Sjulian	ifm = &sc->ifmedia;
44493746Sjulian	ifp = &sc->arpcom.ac_if;
44593746Sjulian
44693746Sjulian	ifm->ifm_media = IFM_ETHER | IFM_AUTO;
44793746Sjulian
44893746Sjulian#ifndef FORCE_AUTONEG_TFOUR
44993746Sjulian	/*
45093746Sjulian	 * First, see if autoneg is supported. If not, there's no point in
45193746Sjulian	 * continuing.
45293746Sjulian	 */
45393746Sjulian	phy_sts = my_phy_readreg(sc, PHY_BMSR);
45493746Sjulian	if (!(phy_sts & PHY_BMSR_CANAUTONEG)) {
45593746Sjulian		if (verbose)
45693746Sjulian			printf("my%d: autonegotiation not supported\n",
45793746Sjulian			    sc->my_unit);
45893746Sjulian		ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX;
45993746Sjulian		MY_UNLOCK(sc);
46093746Sjulian		return;
46193746Sjulian	}
46293746Sjulian#endif
46393746Sjulian	switch (flag) {
46493746Sjulian	case MY_FLAG_FORCEDELAY:
46593746Sjulian		/*
46693746Sjulian		 * XXX Never use this option anywhere but in the probe
46793746Sjulian		 * routine: making the kernel stop dead in its tracks for
46893746Sjulian		 * three whole seconds after we've gone multi-user is really
46993746Sjulian		 * bad manners.
47093746Sjulian		 */
47193746Sjulian		my_autoneg_xmit(sc);
47293746Sjulian		DELAY(5000000);
47393746Sjulian		break;
47493746Sjulian	case MY_FLAG_SCHEDDELAY:
47593746Sjulian		/*
47693746Sjulian		 * Wait for the transmitter to go idle before starting an
47793746Sjulian		 * autoneg session, otherwise my_start() may clobber our
47893746Sjulian		 * timeout, and we don't want to allow transmission during an
47993746Sjulian		 * autoneg session since that can screw it up.
48093746Sjulian		 */
48193746Sjulian		if (sc->my_cdata.my_tx_head != NULL) {
48293746Sjulian			sc->my_want_auto = 1;
48393746Sjulian			MY_UNLOCK(sc);
48493746Sjulian			return;
48593746Sjulian		}
48693746Sjulian		my_autoneg_xmit(sc);
48793746Sjulian		ifp->if_timer = 5;
48893746Sjulian		sc->my_autoneg = 1;
48993746Sjulian		sc->my_want_auto = 0;
49093746Sjulian		MY_UNLOCK(sc);
49193746Sjulian		return;
49293746Sjulian	case MY_FLAG_DELAYTIMEO:
49393746Sjulian		ifp->if_timer = 0;
49493746Sjulian		sc->my_autoneg = 0;
49593746Sjulian		break;
49693746Sjulian	default:
49793746Sjulian		printf("my%d: invalid autoneg flag: %d\n", sc->my_unit, flag);
49893746Sjulian		MY_UNLOCK(sc);
49993746Sjulian		return;
50093746Sjulian	}
50193746Sjulian
50293746Sjulian	if (my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_AUTONEGCOMP) {
50393746Sjulian		if (verbose)
50493746Sjulian			printf("my%d: autoneg complete, ", sc->my_unit);
50593746Sjulian		phy_sts = my_phy_readreg(sc, PHY_BMSR);
50693746Sjulian	} else {
50793746Sjulian		if (verbose)
50893746Sjulian			printf("my%d: autoneg not complete, ", sc->my_unit);
50993746Sjulian	}
51093746Sjulian
51193746Sjulian	media = my_phy_readreg(sc, PHY_BMCR);
51293746Sjulian
51393746Sjulian	/* Link is good. Report modes and set duplex mode. */
51493746Sjulian	if (my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT) {
51593746Sjulian		if (verbose)
51693746Sjulian			printf("my%d: link status good. ", sc->my_unit);
51793746Sjulian		advert = my_phy_readreg(sc, PHY_ANAR);
51893746Sjulian		ability = my_phy_readreg(sc, PHY_LPAR);
51993746Sjulian		if ((sc->my_pinfo->my_vid == MarvellPHYID0) ||
52093746Sjulian		    (sc->my_pinfo->my_vid == LevelOnePHYID0)) {
52193746Sjulian			ability2 = my_phy_readreg(sc, PHY_1000SR);
52293746Sjulian			if (ability2 & PHY_1000SR_1000BTXFULL) {
52393746Sjulian				advert = 0;
52493746Sjulian				ability = 0;
52593746Sjulian				/*
52693746Sjulian				 * this version did not support 1000M,
52793746Sjulian				 * ifm->ifm_media =
52893746Sjulian				 * IFM_ETHER|IFM_1000_TX|IFM_FDX;
52993746Sjulian				 */
53093746Sjulian				ifm->ifm_media =
53193746Sjulian				    IFM_ETHER | IFM_100_TX | IFM_FDX;
53293746Sjulian				media &= ~PHY_BMCR_SPEEDSEL;
53393746Sjulian				media |= PHY_BMCR_1000;
53493746Sjulian				media |= PHY_BMCR_DUPLEX;
53593746Sjulian				printf("(full-duplex, 1000Mbps)\n");
53693746Sjulian			} else if (ability2 & PHY_1000SR_1000BTXHALF) {
53793746Sjulian				advert = 0;
53893746Sjulian				ability = 0;
53993746Sjulian				/*
54093746Sjulian				 * this version did not support 1000M,
54193746Sjulian				 * ifm->ifm_media = IFM_ETHER|IFM_1000_TX;
54293746Sjulian				 */
54393746Sjulian				ifm->ifm_media = IFM_ETHER | IFM_100_TX;
54493746Sjulian				media &= ~PHY_BMCR_SPEEDSEL;
54593746Sjulian				media &= ~PHY_BMCR_DUPLEX;
54693746Sjulian				media |= PHY_BMCR_1000;
54793746Sjulian				printf("(half-duplex, 1000Mbps)\n");
54893746Sjulian			}
54993746Sjulian		}
55093746Sjulian		if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4) {
55193746Sjulian			ifm->ifm_media = IFM_ETHER | IFM_100_T4;
55293746Sjulian			media |= PHY_BMCR_SPEEDSEL;
55393746Sjulian			media &= ~PHY_BMCR_DUPLEX;
55493746Sjulian			printf("(100baseT4)\n");
55593746Sjulian		} else if (advert & PHY_ANAR_100BTXFULL &&
55693746Sjulian			   ability & PHY_ANAR_100BTXFULL) {
55793746Sjulian			ifm->ifm_media = IFM_ETHER | IFM_100_TX | IFM_FDX;
55893746Sjulian			media |= PHY_BMCR_SPEEDSEL;
55993746Sjulian			media |= PHY_BMCR_DUPLEX;
56093746Sjulian			printf("(full-duplex, 100Mbps)\n");
56193746Sjulian		} else if (advert & PHY_ANAR_100BTXHALF &&
56293746Sjulian			   ability & PHY_ANAR_100BTXHALF) {
56393746Sjulian			ifm->ifm_media = IFM_ETHER | IFM_100_TX | IFM_HDX;
56493746Sjulian			media |= PHY_BMCR_SPEEDSEL;
56593746Sjulian			media &= ~PHY_BMCR_DUPLEX;
56693746Sjulian			printf("(half-duplex, 100Mbps)\n");
56793746Sjulian		} else if (advert & PHY_ANAR_10BTFULL &&
56893746Sjulian			   ability & PHY_ANAR_10BTFULL) {
56993746Sjulian			ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_FDX;
57093746Sjulian			media &= ~PHY_BMCR_SPEEDSEL;
57193746Sjulian			media |= PHY_BMCR_DUPLEX;
57293746Sjulian			printf("(full-duplex, 10Mbps)\n");
57393746Sjulian		} else if (advert) {
57493746Sjulian			ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX;
57593746Sjulian			media &= ~PHY_BMCR_SPEEDSEL;
57693746Sjulian			media &= ~PHY_BMCR_DUPLEX;
57793746Sjulian			printf("(half-duplex, 10Mbps)\n");
57893746Sjulian		}
57993746Sjulian		media &= ~PHY_BMCR_AUTONEGENBL;
58093746Sjulian
58193746Sjulian		/* Set ASIC's duplex mode to match the PHY. */
58293746Sjulian		my_phy_writereg(sc, PHY_BMCR, media);
58393746Sjulian		my_setcfg(sc, media);
58493746Sjulian	} else {
58593746Sjulian		if (verbose)
58693746Sjulian			printf("my%d: no carrier\n", sc->my_unit);
58793746Sjulian	}
58893746Sjulian
58993746Sjulian	my_init(sc);
59093746Sjulian	if (sc->my_tx_pend) {
59193746Sjulian		sc->my_autoneg = 0;
59293746Sjulian		sc->my_tx_pend = 0;
59393746Sjulian		my_start(ifp);
59493746Sjulian	}
59593746Sjulian	MY_UNLOCK(sc);
59693746Sjulian	return;
59793746Sjulian}
59893746Sjulian
59993746Sjulian/*
60093746Sjulian * To get PHY ability.
60193746Sjulian */
60293746Sjulianstatic void
60393746Sjulianmy_getmode_mii(struct my_softc * sc)
60493746Sjulian{
60593746Sjulian	u_int16_t       bmsr;
60693746Sjulian	struct ifnet   *ifp;
60793746Sjulian
60893746Sjulian	MY_LOCK(sc);
60993746Sjulian	ifp = &sc->arpcom.ac_if;
61093746Sjulian	bmsr = my_phy_readreg(sc, PHY_BMSR);
61193746Sjulian	if (bootverbose)
61293746Sjulian		printf("my%d: PHY status word: %x\n", sc->my_unit, bmsr);
61393746Sjulian
61493746Sjulian	/* fallback */
61593746Sjulian	sc->ifmedia.ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX;
61693746Sjulian
61793746Sjulian	if (bmsr & PHY_BMSR_10BTHALF) {
61893746Sjulian		if (bootverbose)
61993746Sjulian			printf("my%d: 10Mbps half-duplex mode supported\n",
62093746Sjulian			       sc->my_unit);
62193746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T | IFM_HDX,
62293746Sjulian		    0, NULL);
62393746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
62493746Sjulian	}
62593746Sjulian	if (bmsr & PHY_BMSR_10BTFULL) {
62693746Sjulian		if (bootverbose)
62793746Sjulian			printf("my%d: 10Mbps full-duplex mode supported\n",
62893746Sjulian			    sc->my_unit);
62993746Sjulian
63093746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX,
63193746Sjulian		    0, NULL);
63293746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_10_T | IFM_FDX;
63393746Sjulian	}
63493746Sjulian	if (bmsr & PHY_BMSR_100BTXHALF) {
63593746Sjulian		if (bootverbose)
63693746Sjulian			printf("my%d: 100Mbps half-duplex mode supported\n",
63793746Sjulian			       sc->my_unit);
63893746Sjulian		ifp->if_baudrate = 100000000;
63993746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL);
64093746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX | IFM_HDX,
64193746Sjulian			    0, NULL);
64293746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_TX | IFM_HDX;
64393746Sjulian	}
64493746Sjulian	if (bmsr & PHY_BMSR_100BTXFULL) {
64593746Sjulian		if (bootverbose)
64693746Sjulian			printf("my%d: 100Mbps full-duplex mode supported\n",
64793746Sjulian			    sc->my_unit);
64893746Sjulian		ifp->if_baudrate = 100000000;
64993746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX,
65093746Sjulian		    0, NULL);
65193746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_TX | IFM_FDX;
65293746Sjulian	}
65393746Sjulian	/* Some also support 100BaseT4. */
65493746Sjulian	if (bmsr & PHY_BMSR_100BT4) {
65593746Sjulian		if (bootverbose)
65693746Sjulian			printf("my%d: 100baseT4 mode supported\n", sc->my_unit);
65793746Sjulian		ifp->if_baudrate = 100000000;
65893746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_T4, 0, NULL);
65993746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_T4;
66093746Sjulian#ifdef FORCE_AUTONEG_TFOUR
66193746Sjulian		if (bootverbose)
66293746Sjulian			printf("my%d: forcing on autoneg support for BT4\n",
66393746Sjulian			    sc->my_unit);
66493746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0 NULL):
66593746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_AUTO;
66693746Sjulian#endif
66793746Sjulian	}
66893746Sjulian#if 0
66993746Sjulian	/* this version did not support 1000M, */
67093746Sjulian	if (sc->my_pinfo->my_vid == MarvellPHYID0) {
67193746Sjulian		if (bootverbose)
67293746Sjulian			printf("my%d: 1000Mbps half-duplex mode supported\n",
67393746Sjulian			       sc->my_unit);
67493746Sjulian
67593746Sjulian		ifp->if_baudrate = 1000000000;
67693746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_TX, 0, NULL);
67793746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_TX | IFM_HDX,
67893746Sjulian		    0, NULL);
67993746Sjulian		if (bootverbose)
68093746Sjulian			printf("my%d: 1000Mbps full-duplex mode supported\n",
68193746Sjulian			   sc->my_unit);
68293746Sjulian		ifp->if_baudrate = 1000000000;
68393746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_TX | IFM_FDX,
68493746Sjulian		    0, NULL);
68593746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_1000_TX | IFM_FDX;
68693746Sjulian	}
68793746Sjulian#endif
68893746Sjulian	if (bmsr & PHY_BMSR_CANAUTONEG) {
68993746Sjulian		if (bootverbose)
69093746Sjulian			printf("my%d: autoneg supported\n", sc->my_unit);
69193746Sjulian		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
69293746Sjulian		sc->ifmedia.ifm_media = IFM_ETHER | IFM_AUTO;
69393746Sjulian	}
69493746Sjulian	MY_UNLOCK(sc);
69593746Sjulian	return;
69693746Sjulian}
69793746Sjulian
69893746Sjulian/*
69993746Sjulian * Set speed and duplex mode.
70093746Sjulian */
70193746Sjulianstatic void
70293746Sjulianmy_setmode_mii(struct my_softc * sc, int media)
70393746Sjulian{
70493746Sjulian	u_int16_t       bmcr;
70593746Sjulian	struct ifnet   *ifp;
70693746Sjulian
70793746Sjulian	MY_LOCK(sc);
70893746Sjulian	ifp = &sc->arpcom.ac_if;
70993746Sjulian	/*
71093746Sjulian	 * If an autoneg session is in progress, stop it.
71193746Sjulian	 */
71293746Sjulian	if (sc->my_autoneg) {
71393746Sjulian		printf("my%d: canceling autoneg session\n", sc->my_unit);
71493746Sjulian		ifp->if_timer = sc->my_autoneg = sc->my_want_auto = 0;
71593746Sjulian		bmcr = my_phy_readreg(sc, PHY_BMCR);
71693746Sjulian		bmcr &= ~PHY_BMCR_AUTONEGENBL;
71793746Sjulian		my_phy_writereg(sc, PHY_BMCR, bmcr);
71893746Sjulian	}
71993746Sjulian	printf("my%d: selecting MII, ", sc->my_unit);
72093746Sjulian	bmcr = my_phy_readreg(sc, PHY_BMCR);
72193746Sjulian	bmcr &= ~(PHY_BMCR_AUTONEGENBL | PHY_BMCR_SPEEDSEL | PHY_BMCR_1000 |
72293746Sjulian		  PHY_BMCR_DUPLEX | PHY_BMCR_LOOPBK);
72393746Sjulian	/*
72493746Sjulian	 * this version did not support 1000M,
72593746Sjulian	 */
72693746Sjulian#if 0
72793746Sjulian	if (IFM_SUBTYPE(media) ==
72893746Sjulian	    IFM_1000_TX) {
72993746Sjulian		printf("1000Mbps/T4, half-duplex\n");
73093746Sjulian		bmcr &= ~PHY_BMCR_SPEEDSEL;
73193746Sjulian		bmcr &= ~PHY_BMCR_DUPLEX;
73293746Sjulian		bmcr |= PHY_BMCR_1000;
73393746Sjulian	}
73493746Sjulian#endif
73593746Sjulian	if (IFM_SUBTYPE(media) == IFM_100_T4) {
73693746Sjulian		printf("100Mbps/T4, half-duplex\n");
73793746Sjulian		bmcr |= PHY_BMCR_SPEEDSEL;
73893746Sjulian		bmcr &= ~PHY_BMCR_DUPLEX;
73993746Sjulian	}
74093746Sjulian	if (IFM_SUBTYPE(media) == IFM_100_TX) {
74193746Sjulian		printf("100Mbps, ");
74293746Sjulian		bmcr |= PHY_BMCR_SPEEDSEL;
74393746Sjulian	}
74493746Sjulian	if (IFM_SUBTYPE(media) == IFM_10_T) {
74593746Sjulian		printf("10Mbps, ");
74693746Sjulian		bmcr &= ~PHY_BMCR_SPEEDSEL;
74793746Sjulian	}
74893746Sjulian	if ((media & IFM_GMASK) == IFM_FDX) {
74993746Sjulian		printf("full duplex\n");
75093746Sjulian		bmcr |= PHY_BMCR_DUPLEX;
75193746Sjulian	} else {
75293746Sjulian		printf("half duplex\n");
75393746Sjulian		bmcr &= ~PHY_BMCR_DUPLEX;
75493746Sjulian	}
75593746Sjulian	my_phy_writereg(sc, PHY_BMCR, bmcr);
75693746Sjulian	my_setcfg(sc, bmcr);
75793746Sjulian	MY_UNLOCK(sc);
75893746Sjulian	return;
75993746Sjulian}
76093746Sjulian
76193746Sjulian/*
76293746Sjulian * The Myson manual states that in order to fiddle with the 'full-duplex' and
76393746Sjulian * '100Mbps' bits in the netconfig register, we first have to put the
76493746Sjulian * transmit and/or receive logic in the idle state.
76593746Sjulian */
76693746Sjulianstatic void
76793746Sjulianmy_setcfg(struct my_softc * sc, int bmcr)
76893746Sjulian{
76993746Sjulian	int             i, restart = 0;
77093746Sjulian
77193746Sjulian	MY_LOCK(sc);
77293746Sjulian	if (CSR_READ_4(sc, MY_TCRRCR) & (MY_TE | MY_RE)) {
77393746Sjulian		restart = 1;
77493746Sjulian		MY_CLRBIT(sc, MY_TCRRCR, (MY_TE | MY_RE));
77593746Sjulian		for (i = 0; i < MY_TIMEOUT; i++) {
77693746Sjulian			DELAY(10);
77793746Sjulian			if (!(CSR_READ_4(sc, MY_TCRRCR) &
77893746Sjulian			    (MY_TXRUN | MY_RXRUN)))
77993746Sjulian				break;
78093746Sjulian		}
78193746Sjulian		if (i == MY_TIMEOUT)
78293746Sjulian			printf("my%d: failed to force tx and rx to idle \n",
78393746Sjulian			    sc->my_unit);
78493746Sjulian	}
78593746Sjulian	MY_CLRBIT(sc, MY_TCRRCR, MY_PS1000);
78693746Sjulian	MY_CLRBIT(sc, MY_TCRRCR, MY_PS10);
78793746Sjulian	if (bmcr & PHY_BMCR_1000)
78893746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_PS1000);
78993746Sjulian	else if (!(bmcr & PHY_BMCR_SPEEDSEL))
79093746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_PS10);
79193746Sjulian	if (bmcr & PHY_BMCR_DUPLEX)
79293746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_FD);
79393746Sjulian	else
79493746Sjulian		MY_CLRBIT(sc, MY_TCRRCR, MY_FD);
79593746Sjulian	if (restart)
79693746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_TE | MY_RE);
79793746Sjulian	MY_UNLOCK(sc);
79893746Sjulian	return;
79993746Sjulian}
80093746Sjulian
80193746Sjulianstatic void
80293746Sjulianmy_reset(struct my_softc * sc)
80393746Sjulian{
80493746Sjulian	register int    i;
80593746Sjulian
80693746Sjulian	MY_LOCK(sc);
80793746Sjulian	MY_SETBIT(sc, MY_BCR, MY_SWR);
80893746Sjulian	for (i = 0; i < MY_TIMEOUT; i++) {
80993746Sjulian		DELAY(10);
81093746Sjulian		if (!(CSR_READ_4(sc, MY_BCR) & MY_SWR))
81193746Sjulian			break;
81293746Sjulian	}
81393746Sjulian	if (i == MY_TIMEOUT)
81493746Sjulian		printf("m0x%d: reset never completed!\n", sc->my_unit);
81593746Sjulian
81693746Sjulian	/* Wait a little while for the chip to get its brains in order. */
81793746Sjulian	DELAY(1000);
81893746Sjulian	MY_UNLOCK(sc);
81993746Sjulian	return;
82093746Sjulian}
82193746Sjulian
82293746Sjulian/*
82393746Sjulian * Probe for a Myson chip. Check the PCI vendor and device IDs against our
82493746Sjulian * list and return a device name if we find a match.
82593746Sjulian */
82693746Sjulianstatic int
82793746Sjulianmy_probe(device_t dev)
82893746Sjulian{
82993746Sjulian	struct my_type *t;
83093746Sjulian
83193746Sjulian	t = my_devs;
83293746Sjulian	while (t->my_name != NULL) {
83393746Sjulian		if ((pci_get_vendor(dev) == t->my_vid) &&
83493746Sjulian		    (pci_get_device(dev) == t->my_did)) {
83593746Sjulian			device_set_desc(dev, t->my_name);
83693746Sjulian			my_info_tmp = t;
83793746Sjulian			return (0);
83893746Sjulian		}
83993746Sjulian		t++;
84093746Sjulian	}
84193746Sjulian	return (ENXIO);
84293746Sjulian}
84393746Sjulian
84493746Sjulian/*
84593746Sjulian * Attach the interface. Allocate softc structures, do ifmedia setup and
84693746Sjulian * ethernet/BPF attach.
84793746Sjulian */
84893746Sjulianstatic int
84993746Sjulianmy_attach(device_t dev)
85093746Sjulian{
85193746Sjulian	int             s, i;
85293746Sjulian	u_char          eaddr[ETHER_ADDR_LEN];
85393746Sjulian	u_int32_t       command, iobase;
85493746Sjulian	struct my_softc *sc;
85593746Sjulian	struct ifnet   *ifp;
85693746Sjulian	int             media = IFM_ETHER | IFM_100_TX | IFM_FDX;
85793746Sjulian	unsigned int    round;
85893746Sjulian	caddr_t         roundptr;
85993746Sjulian	struct my_type *p;
86093746Sjulian	u_int16_t       phy_vid, phy_did, phy_sts = 0;
86193746Sjulian	int             rid, unit, error = 0;
86293746Sjulian
86393746Sjulian	s = splimp();
86493746Sjulian	sc = device_get_softc(dev);
86593746Sjulian	unit = device_get_unit(dev);
86693746Sjulian	if (sc == NULL) {
86793746Sjulian		printf("my%d: no memory for softc struct!\n", unit);
86893746Sjulian		error = ENXIO;
86993746Sjulian		goto fail;
87093746Sjulian
87193746Sjulian	}
87293746Sjulian	bzero(sc, sizeof(struct my_softc));
87393746Sjulian	mtx_init(&sc->my_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
87493746Sjulian	MY_LOCK(sc);
87593746Sjulian
87693746Sjulian	/*
87793746Sjulian	 * Map control/status registers.
87893746Sjulian	 */
87993746Sjulian	/*
88093746Sjulian	 * command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4); command
88193746Sjulian	 * |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
88293746Sjulian	 * pci_write_config(dev, PCI_COMMAND_STATUS_REG, command &
88393746Sjulian	 * 0x000000ff, 4); command = pci_read_config(dev,
88493746Sjulian	 * PCI_COMMAND_STATUS_REG, 4);
88593746Sjulian	 */
88693746Sjulian	command = pci_read_config(dev, PCIR_COMMAND, 4);
88793746Sjulian	command |= (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
88893746Sjulian	pci_write_config(dev, PCIR_COMMAND, command & 0x000000ff, 4);
88993746Sjulian	command = pci_read_config(dev, PCIR_COMMAND, 4);
89093746Sjulian
89193746Sjulian	if (my_info_tmp->my_did == MTD800ID) {
89293746Sjulian		iobase = pci_read_config(dev, MY_PCI_LOIO, 4);
89393746Sjulian		if (iobase & 0x300)
89493746Sjulian			MY_USEIOSPACE = 0;
89593746Sjulian	}
89693746Sjulian	if (MY_USEIOSPACE) {
89793746Sjulian		if (!(command & PCIM_CMD_PORTEN)) {
89893746Sjulian			printf("my%d: failed to enable I/O ports!\n", unit);
89993746Sjulian			free(sc, M_DEVBUF);
90093746Sjulian			error = ENXIO;
90193746Sjulian			goto fail;
90293746Sjulian		}
90393746Sjulian#if 0
90493746Sjulian		if (!pci_map_port(config_id, MY_PCI_LOIO,
90593746Sjulian		    (u_int16_t *)&(sc->my_bhandle))) {
90693746Sjulian			printf ("my%d: couldn't map ports\n",
90793746Sjulian			    unit); error = ENXIO; goto fail;
90893746Sjulian		}
90993746Sjulian
91093746Sjulian		sc->my_btag = I386_BUS_SPACE_IO;
91193746Sjulian#endif
91293746Sjulian	} else {
91393746Sjulian		if (!(command & PCIM_CMD_MEMEN)) {
91493746Sjulian			printf("my%d: failed to enable memory mapping!\n",
91593746Sjulian			    unit);
91693746Sjulian			error = ENXIO;
91793746Sjulian			goto fail;
91893746Sjulian		}
91993746Sjulian#if 0
92093746Sjulian		 if (!pci_map_mem(config_id, MY_PCI_LOMEM, &vbase, &pbase)) {
92193746Sjulian			printf ("my%d: couldn't map memory\n", unit);
92293746Sjulian			error = ENXIO;
92393746Sjulian			goto fail;
92493746Sjulian		}
92593746Sjulian
92693746Sjulian		sc->my_btag = I386_BUS_SPACE_MEM; sc->my_bhandle = vbase;
92793746Sjulian#endif
92893746Sjulian	}
92993746Sjulian
93093746Sjulian	rid = MY_RID;
93193746Sjulian	sc->my_res = bus_alloc_resource(dev, MY_RES, &rid,
93293746Sjulian					0, ~0, 1, RF_ACTIVE);
93393746Sjulian
93493746Sjulian	if (sc->my_res == NULL) {
93593746Sjulian		printf("my%d: couldn't map ports/memory\n", unit);
93693746Sjulian		error = ENXIO;
93793746Sjulian		goto fail;
93893746Sjulian	}
93993746Sjulian	sc->my_btag = rman_get_bustag(sc->my_res);
94093746Sjulian	sc->my_bhandle = rman_get_bushandle(sc->my_res);
94193746Sjulian
94293746Sjulian	rid = 0;
94393746Sjulian	sc->my_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
94493746Sjulian					RF_SHAREABLE | RF_ACTIVE);
94593746Sjulian
94693746Sjulian	if (sc->my_irq == NULL) {
94793746Sjulian		printf("my%d: couldn't map interrupt\n", unit);
94893746Sjulian		bus_release_resource(dev, MY_RES, MY_RID, sc->my_res);
94993746Sjulian		error = ENXIO;
95093746Sjulian		goto fail;
95193746Sjulian	}
95293746Sjulian	error = bus_setup_intr(dev, sc->my_irq, INTR_TYPE_NET,
95393746Sjulian			       my_intr, sc, &sc->my_intrhand);
95493746Sjulian
95593746Sjulian	if (error) {
95693746Sjulian		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->my_irq);
95793746Sjulian		bus_release_resource(dev, MY_RES, MY_RID, sc->my_res);
95893746Sjulian		printf("my%d: couldn't set up irq\n", unit);
95993746Sjulian		goto fail;
96093746Sjulian	}
96193746Sjulian	callout_handle_init(&sc->my_stat_ch);
96293746Sjulian
96393746Sjulian	sc->my_info = my_info_tmp;
96493746Sjulian
96593746Sjulian	/* Reset the adapter. */
96693746Sjulian	my_reset(sc);
96793746Sjulian
96893746Sjulian	/*
96993746Sjulian	 * Get station address
97093746Sjulian	 */
97193746Sjulian	for (i = 0; i < ETHER_ADDR_LEN; ++i)
97293746Sjulian		eaddr[i] = CSR_READ_1(sc, MY_PAR0 + i);
97393746Sjulian
97493746Sjulian	/*
97593746Sjulian	 * A Myson chip was detected. Inform the world.
97693746Sjulian	 */
97793746Sjulian	printf("my%d: Ethernet address: %6D\n", unit, eaddr, ":");
97893746Sjulian
97993746Sjulian	sc->my_unit = unit;
98093746Sjulian	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
98193746Sjulian
98293746Sjulian	sc->my_ldata_ptr = malloc(sizeof(struct my_list_data) + 8,
98393746Sjulian				  M_DEVBUF, M_NOWAIT);
98493746Sjulian	if (sc->my_ldata_ptr == NULL) {
98593746Sjulian		free(sc, M_DEVBUF);
98693746Sjulian		printf("my%d: no memory for list buffers!\n", unit);
98793746Sjulian		error = ENXIO;
98893746Sjulian		goto fail;
98993746Sjulian	}
99093746Sjulian	sc->my_ldata = (struct my_list_data *) sc->my_ldata_ptr;
99193746Sjulian	round = (unsigned int)sc->my_ldata_ptr & 0xF;
99293746Sjulian	roundptr = sc->my_ldata_ptr;
99393746Sjulian	for (i = 0; i < 8; i++) {
99493746Sjulian		if (round % 8) {
99593746Sjulian			round++;
99693746Sjulian			roundptr++;
99793746Sjulian		} else
99893746Sjulian			break;
99993746Sjulian	}
100093746Sjulian	sc->my_ldata = (struct my_list_data *) roundptr;
100193746Sjulian	bzero(sc->my_ldata, sizeof(struct my_list_data));
100293746Sjulian
100393746Sjulian	ifp = &sc->arpcom.ac_if;
100493746Sjulian	ifp->if_softc = sc;
100593746Sjulian	ifp->if_unit = unit;
100693746Sjulian	ifp->if_name = "my";
100793746Sjulian	ifp->if_mtu = ETHERMTU;
100893746Sjulian	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
100993746Sjulian	ifp->if_ioctl = my_ioctl;
101093746Sjulian	ifp->if_output = ether_output;
101193746Sjulian	ifp->if_start = my_start;
101293746Sjulian	ifp->if_watchdog = my_watchdog;
101393746Sjulian	ifp->if_init = my_init;
101493746Sjulian	ifp->if_baudrate = 10000000;
101593746Sjulian	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
101693746Sjulian
101793746Sjulian	if (sc->my_info->my_did == MTD803ID)
101893746Sjulian		sc->my_pinfo = my_phys;
101993746Sjulian	else {
102093746Sjulian		if (bootverbose)
102193746Sjulian			printf("my%d: probing for a PHY\n", sc->my_unit);
102293746Sjulian		for (i = MY_PHYADDR_MIN; i < MY_PHYADDR_MAX + 1; i++) {
102393746Sjulian			if (bootverbose)
102493746Sjulian				printf("my%d: checking address: %d\n",
102593746Sjulian				    sc->my_unit, i);
102693746Sjulian			sc->my_phy_addr = i;
102793746Sjulian			phy_sts = my_phy_readreg(sc, PHY_BMSR);
102893746Sjulian			if ((phy_sts != 0) && (phy_sts != 0xffff))
102993746Sjulian				break;
103093746Sjulian			else
103193746Sjulian				phy_sts = 0;
103293746Sjulian		}
103393746Sjulian		if (phy_sts) {
103493746Sjulian			phy_vid = my_phy_readreg(sc, PHY_VENID);
103593746Sjulian			phy_did = my_phy_readreg(sc, PHY_DEVID);
103693746Sjulian			if (bootverbose) {
103793746Sjulian				printf("my%d: found PHY at address %d, ",
103893746Sjulian				    sc->my_unit, sc->my_phy_addr);
103993746Sjulian				printf("vendor id: %x device id: %x\n",
104093746Sjulian				    phy_vid, phy_did);
104193746Sjulian			}
104293746Sjulian			p = my_phys;
104393746Sjulian			while (p->my_vid) {
104493746Sjulian				if (phy_vid == p->my_vid) {
104593746Sjulian					sc->my_pinfo = p;
104693746Sjulian					break;
104793746Sjulian				}
104893746Sjulian				p++;
104993746Sjulian			}
105093746Sjulian			if (sc->my_pinfo == NULL)
105193746Sjulian				sc->my_pinfo = &my_phys[PHY_UNKNOWN];
105293746Sjulian			if (bootverbose)
105393746Sjulian				printf("my%d: PHY type: %s\n",
105493746Sjulian				       sc->my_unit, sc->my_pinfo->my_name);
105593746Sjulian		} else {
105693746Sjulian			printf("my%d: MII without any phy!\n", sc->my_unit);
105793746Sjulian			error = ENXIO;
105893746Sjulian			goto fail;
105993746Sjulian		}
106093746Sjulian	}
106193746Sjulian
106293746Sjulian	/* Do ifmedia setup. */
106393746Sjulian	ifmedia_init(&sc->ifmedia, 0, my_ifmedia_upd, my_ifmedia_sts);
106493746Sjulian	my_getmode_mii(sc);
106593746Sjulian	my_autoneg_mii(sc, MY_FLAG_FORCEDELAY, 1);
106693746Sjulian	media = sc->ifmedia.ifm_media;
106793746Sjulian	my_stop(sc);
106893746Sjulian	ifmedia_set(&sc->ifmedia, media);
106993746Sjulian
107093746Sjulian	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
107193746Sjulian
107293746Sjulian#if 0
107393746Sjulian	at_shutdown(my_shutdown, sc, SHUTDOWN_POST_SYNC);
107493746Sjulian	shutdownhook_establish(my_shutdown, sc);
107593746Sjulian#endif
107693746Sjulian
107793746Sjulian	MY_UNLOCK(sc);
107893746Sjulian	return (0);
107993746Sjulian
108093746Sjulianfail:
108193746Sjulian	MY_UNLOCK(sc);
108293746Sjulian	mtx_destroy(&sc->my_mtx);
108393746Sjulian	splx(s);
108493746Sjulian	return (error);
108593746Sjulian}
108693746Sjulian
108793746Sjulianstatic int
108893746Sjulianmy_detach(device_t dev)
108993746Sjulian{
109093746Sjulian	struct my_softc *sc;
109193746Sjulian	struct ifnet   *ifp;
109293746Sjulian	int             s;
109393746Sjulian
109493746Sjulian	s = splimp();
109593746Sjulian	sc = device_get_softc(dev);
109693746Sjulian	MY_LOCK(sc);
109793746Sjulian	ifp = &sc->arpcom.ac_if;
109893746Sjulian	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
109993746Sjulian	my_stop(sc);
110093746Sjulian
110193746Sjulian	/*
110293746Sjulian	 * bus_generic_detach(dev); device_delete_child(dev, sc->rl_miibus);
110393746Sjulian	 */
110493746Sjulian	bus_teardown_intr(dev, sc->my_irq, sc->my_intrhand);
110593746Sjulian	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->my_irq);
110693746Sjulian	bus_release_resource(dev, MY_RES, MY_RID, sc->my_res);
110793746Sjulian	/*
110893746Sjulian	 * contigfree(sc->my_cdata.my_rx_buf, MY_RXBUFLEN + 32, M_DEVBUF);
110993746Sjulian	 */
111093746Sjulian	free(sc, M_DEVBUF);
111193746Sjulian	MY_UNLOCK(sc);
111293746Sjulian	splx(s);
111393746Sjulian	mtx_destroy(&sc->my_mtx);
111493746Sjulian	return (0);
111593746Sjulian}
111693746Sjulian
111793746Sjulian
111893746Sjulian/*
111993746Sjulian * Initialize the transmit descriptors.
112093746Sjulian */
112193746Sjulianstatic int
112293746Sjulianmy_list_tx_init(struct my_softc * sc)
112393746Sjulian{
112493746Sjulian	struct my_chain_data *cd;
112593746Sjulian	struct my_list_data *ld;
112693746Sjulian	int             i;
112793746Sjulian
112893746Sjulian	MY_LOCK(sc);
112993746Sjulian	cd = &sc->my_cdata;
113093746Sjulian	ld = sc->my_ldata;
113193746Sjulian	for (i = 0; i < MY_TX_LIST_CNT; i++) {
113293746Sjulian		cd->my_tx_chain[i].my_ptr = &ld->my_tx_list[i];
113393746Sjulian		if (i == (MY_TX_LIST_CNT - 1))
113493746Sjulian			cd->my_tx_chain[i].my_nextdesc = &cd->my_tx_chain[0];
113593746Sjulian		else
113693746Sjulian			cd->my_tx_chain[i].my_nextdesc =
113793746Sjulian			    &cd->my_tx_chain[i + 1];
113893746Sjulian	}
113993746Sjulian	cd->my_tx_free = &cd->my_tx_chain[0];
114093746Sjulian	cd->my_tx_tail = cd->my_tx_head = NULL;
114193746Sjulian	MY_UNLOCK(sc);
114293746Sjulian	return (0);
114393746Sjulian}
114493746Sjulian
114593746Sjulian/*
114693746Sjulian * Initialize the RX descriptors and allocate mbufs for them. Note that we
114793746Sjulian * arrange the descriptors in a closed ring, so that the last descriptor
114893746Sjulian * points back to the first.
114993746Sjulian */
115093746Sjulianstatic int
115193746Sjulianmy_list_rx_init(struct my_softc * sc)
115293746Sjulian{
115393746Sjulian	struct my_chain_data *cd;
115493746Sjulian	struct my_list_data *ld;
115593746Sjulian	int             i;
115693746Sjulian
115793746Sjulian	MY_LOCK(sc);
115893746Sjulian	cd = &sc->my_cdata;
115993746Sjulian	ld = sc->my_ldata;
116093746Sjulian	for (i = 0; i < MY_RX_LIST_CNT; i++) {
116193746Sjulian		cd->my_rx_chain[i].my_ptr =
116293746Sjulian		    (struct my_desc *) & ld->my_rx_list[i];
116393746Sjulian		if (my_newbuf(sc, &cd->my_rx_chain[i]) == ENOBUFS)
116493746Sjulian			return (ENOBUFS);
116593746Sjulian		if (i == (MY_RX_LIST_CNT - 1)) {
116693746Sjulian			cd->my_rx_chain[i].my_nextdesc = &cd->my_rx_chain[0];
116793746Sjulian			ld->my_rx_list[i].my_next = vtophys(&ld->my_rx_list[0]);
116893746Sjulian		} else {
116993746Sjulian			cd->my_rx_chain[i].my_nextdesc =
117093746Sjulian			    &cd->my_rx_chain[i + 1];
117193746Sjulian			ld->my_rx_list[i].my_next =
117293746Sjulian			    vtophys(&ld->my_rx_list[i + 1]);
117393746Sjulian		}
117493746Sjulian	}
117593746Sjulian	cd->my_rx_head = &cd->my_rx_chain[0];
117693746Sjulian	MY_UNLOCK(sc);
117793746Sjulian	return (0);
117893746Sjulian}
117993746Sjulian
118093746Sjulian/*
118193746Sjulian * Initialize an RX descriptor and attach an MBUF cluster.
118293746Sjulian */
118393746Sjulianstatic int
118493746Sjulianmy_newbuf(struct my_softc * sc, struct my_chain_onefrag * c)
118593746Sjulian{
118693746Sjulian	struct mbuf    *m_new = NULL;
118793746Sjulian
118893746Sjulian	MY_LOCK(sc);
118993746Sjulian	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
119093746Sjulian	if (m_new == NULL) {
119193746Sjulian		printf("my%d: no memory for rx list -- packet dropped!\n",
119293746Sjulian		       sc->my_unit);
119393746Sjulian		return (ENOBUFS);
119493746Sjulian	}
119593746Sjulian	MCLGET(m_new, M_DONTWAIT);
119693746Sjulian	if (!(m_new->m_flags & M_EXT)) {
119793746Sjulian		printf("my%d: no memory for rx list -- packet dropped!\n",
119893746Sjulian		       sc->my_unit);
119993746Sjulian		m_freem(m_new);
120093746Sjulian		return (ENOBUFS);
120193746Sjulian	}
120293746Sjulian	c->my_mbuf = m_new;
120393746Sjulian	c->my_ptr->my_data = vtophys(mtod(m_new, caddr_t));
120493746Sjulian	c->my_ptr->my_ctl = (MCLBYTES - 1) << MY_RBSShift;
120593746Sjulian	c->my_ptr->my_status = MY_OWNByNIC;
120693746Sjulian	MY_UNLOCK(sc);
120793746Sjulian	return (0);
120893746Sjulian}
120993746Sjulian
121093746Sjulian/*
121193746Sjulian * A frame has been uploaded: pass the resulting mbuf chain up to the higher
121293746Sjulian * level protocols.
121393746Sjulian */
121493746Sjulianstatic void
121593746Sjulianmy_rxeof(struct my_softc * sc)
121693746Sjulian{
121793746Sjulian	struct ether_header *eh;
121893746Sjulian	struct mbuf    *m;
121993746Sjulian	struct ifnet   *ifp;
122093746Sjulian	struct my_chain_onefrag *cur_rx;
122193746Sjulian	int             total_len = 0;
122293746Sjulian	u_int32_t       rxstat;
122393746Sjulian
122493746Sjulian	MY_LOCK(sc);
122593746Sjulian	ifp = &sc->arpcom.ac_if;
122693746Sjulian	while (!((rxstat = sc->my_cdata.my_rx_head->my_ptr->my_status)
122793746Sjulian	    & MY_OWNByNIC)) {
122893746Sjulian		cur_rx = sc->my_cdata.my_rx_head;
122993746Sjulian		sc->my_cdata.my_rx_head = cur_rx->my_nextdesc;
123093746Sjulian
123193746Sjulian		if (rxstat & MY_ES) {	/* error summary: give up this rx pkt */
123293746Sjulian			ifp->if_ierrors++;
123393746Sjulian			cur_rx->my_ptr->my_status = MY_OWNByNIC;
123493746Sjulian			continue;
123593746Sjulian		}
123693746Sjulian		/* No errors; receive the packet. */
123793746Sjulian		total_len = (rxstat & MY_FLNGMASK) >> MY_FLNGShift;
123893746Sjulian		total_len -= ETHER_CRC_LEN;
123993746Sjulian
124093746Sjulian		if (total_len < MINCLSIZE) {
124193746Sjulian			m = m_devget(mtod(cur_rx->my_mbuf, char *),
124293746Sjulian			    total_len, 0, ifp, NULL);
124393746Sjulian			cur_rx->my_ptr->my_status = MY_OWNByNIC;
124493746Sjulian			if (m == NULL) {
124593746Sjulian				ifp->if_ierrors++;
124693746Sjulian				continue;
124793746Sjulian			}
124893746Sjulian		} else {
124993746Sjulian			m = cur_rx->my_mbuf;
125093746Sjulian			/*
125193746Sjulian			 * Try to conjure up a new mbuf cluster. If that
125293746Sjulian			 * fails, it means we have an out of memory condition
125393746Sjulian			 * and should leave the buffer in place and continue.
125493746Sjulian			 * This will result in a lost packet, but there's
125593746Sjulian			 * little else we can do in this situation.
125693746Sjulian			 */
125793746Sjulian			if (my_newbuf(sc, cur_rx) == ENOBUFS) {
125893746Sjulian				ifp->if_ierrors++;
125993746Sjulian				cur_rx->my_ptr->my_status = MY_OWNByNIC;
126093746Sjulian				continue;
126193746Sjulian			}
126293746Sjulian			m->m_pkthdr.rcvif = ifp;
126393746Sjulian			m->m_pkthdr.len = m->m_len = total_len;
126493746Sjulian		}
126593746Sjulian		ifp->if_ipackets++;
126693746Sjulian		eh = mtod(m, struct ether_header *);
126793746Sjulian#if NBPFILTER > 0
126893746Sjulian		/*
126993746Sjulian		 * Handle BPF listeners. Let the BPF user see the packet, but
127093746Sjulian		 * don't pass it up to the ether_input() layer unless it's a
127193746Sjulian		 * broadcast packet, multicast packet, matches our ethernet
127293746Sjulian		 * address or the interface is in promiscuous mode.
127393746Sjulian		 */
127493746Sjulian		if (ifp->if_bpf) {
127593746Sjulian			bpf_mtap(ifp, m);
127693746Sjulian			if (ifp->if_flags & IFF_PROMISC &&
127793746Sjulian			    (bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
127893746Sjulian				ETHER_ADDR_LEN) &&
127993746Sjulian			     (eh->ether_dhost[0] & 1) == 0)) {
128093746Sjulian				m_freem(m);
128193746Sjulian				continue;
128293746Sjulian			}
128393746Sjulian		}
128493746Sjulian#endif
128593746Sjulian		/* Remove header from mbuf and pass it on. */
128693746Sjulian		m_adj(m, sizeof(struct ether_header));
128793746Sjulian		ether_input(ifp, eh, m);
128893746Sjulian	}
128993746Sjulian	MY_UNLOCK(sc);
129093746Sjulian	return;
129193746Sjulian}
129293746Sjulian
129393746Sjulian
129493746Sjulian/*
129593746Sjulian * A frame was downloaded to the chip. It's safe for us to clean up the list
129693746Sjulian * buffers.
129793746Sjulian */
129893746Sjulianstatic void
129993746Sjulianmy_txeof(struct my_softc * sc)
130093746Sjulian{
130193746Sjulian	struct my_chain *cur_tx;
130293746Sjulian	struct ifnet   *ifp;
130393746Sjulian
130493746Sjulian	MY_LOCK(sc);
130593746Sjulian	ifp = &sc->arpcom.ac_if;
130693746Sjulian	/* Clear the timeout timer. */
130793746Sjulian	ifp->if_timer = 0;
130893746Sjulian	if (sc->my_cdata.my_tx_head == NULL)
130993746Sjulian		return;
131093746Sjulian	/*
131193746Sjulian	 * Go through our tx list and free mbufs for those frames that have
131293746Sjulian	 * been transmitted.
131393746Sjulian	 */
131493746Sjulian	while (sc->my_cdata.my_tx_head->my_mbuf != NULL) {
131593746Sjulian		u_int32_t       txstat;
131693746Sjulian
131793746Sjulian		cur_tx = sc->my_cdata.my_tx_head;
131893746Sjulian		txstat = MY_TXSTATUS(cur_tx);
131993746Sjulian		if ((txstat & MY_OWNByNIC) || txstat == MY_UNSENT)
132093746Sjulian			break;
132193746Sjulian		if (!(CSR_READ_4(sc, MY_TCRRCR) & MY_Enhanced)) {
132293746Sjulian			if (txstat & MY_TXERR) {
132393746Sjulian				ifp->if_oerrors++;
132493746Sjulian				if (txstat & MY_EC) /* excessive collision */
132593746Sjulian					ifp->if_collisions++;
132693746Sjulian				if (txstat & MY_LC)	/* late collision */
132793746Sjulian					ifp->if_collisions++;
132893746Sjulian			}
132993746Sjulian			ifp->if_collisions += (txstat & MY_NCRMASK) >>
133093746Sjulian			    MY_NCRShift;
133193746Sjulian		}
133293746Sjulian		ifp->if_opackets++;
133393746Sjulian		m_freem(cur_tx->my_mbuf);
133493746Sjulian		cur_tx->my_mbuf = NULL;
133593746Sjulian		if (sc->my_cdata.my_tx_head == sc->my_cdata.my_tx_tail) {
133693746Sjulian			sc->my_cdata.my_tx_head = NULL;
133793746Sjulian			sc->my_cdata.my_tx_tail = NULL;
133893746Sjulian			break;
133993746Sjulian		}
134093746Sjulian		sc->my_cdata.my_tx_head = cur_tx->my_nextdesc;
134193746Sjulian	}
134293746Sjulian	if (CSR_READ_4(sc, MY_TCRRCR) & MY_Enhanced) {
134393746Sjulian		ifp->if_collisions += (CSR_READ_4(sc, MY_TSR) & MY_NCRMask);
134493746Sjulian	}
134593746Sjulian	MY_UNLOCK(sc);
134693746Sjulian	return;
134793746Sjulian}
134893746Sjulian
134993746Sjulian/*
135093746Sjulian * TX 'end of channel' interrupt handler.
135193746Sjulian */
135293746Sjulianstatic void
135393746Sjulianmy_txeoc(struct my_softc * sc)
135493746Sjulian{
135593746Sjulian	struct ifnet   *ifp;
135693746Sjulian
135793746Sjulian	MY_LOCK(sc);
135893746Sjulian	ifp = &sc->arpcom.ac_if;
135993746Sjulian	ifp->if_timer = 0;
136093746Sjulian	if (sc->my_cdata.my_tx_head == NULL) {
136193746Sjulian		ifp->if_flags &= ~IFF_OACTIVE;
136293746Sjulian		sc->my_cdata.my_tx_tail = NULL;
136393746Sjulian		if (sc->my_want_auto)
136493746Sjulian			my_autoneg_mii(sc, MY_FLAG_SCHEDDELAY, 1);
136593746Sjulian	} else {
136693746Sjulian		if (MY_TXOWN(sc->my_cdata.my_tx_head) == MY_UNSENT) {
136793746Sjulian			MY_TXOWN(sc->my_cdata.my_tx_head) = MY_OWNByNIC;
136893746Sjulian			ifp->if_timer = 5;
136993746Sjulian			CSR_WRITE_4(sc, MY_TXPDR, 0xFFFFFFFF);
137093746Sjulian		}
137193746Sjulian	}
137293746Sjulian	MY_UNLOCK(sc);
137393746Sjulian	return;
137493746Sjulian}
137593746Sjulian
137693746Sjulianstatic void
137793746Sjulianmy_intr(void *arg)
137893746Sjulian{
137993746Sjulian	struct my_softc *sc;
138093746Sjulian	struct ifnet   *ifp;
138193746Sjulian	u_int32_t       status;
138293746Sjulian
138393746Sjulian	sc = arg;
138493746Sjulian	MY_LOCK(sc);
138593746Sjulian	ifp = &sc->arpcom.ac_if;
138693746Sjulian	if (!(ifp->if_flags & IFF_UP)) {
138793746Sjulian		MY_UNLOCK(sc);
138893746Sjulian		return;
138993746Sjulian	}
139093746Sjulian	/* Disable interrupts. */
139193746Sjulian	CSR_WRITE_4(sc, MY_IMR, 0x00000000);
139293746Sjulian
139393746Sjulian	for (;;) {
139493746Sjulian		status = CSR_READ_4(sc, MY_ISR);
139593746Sjulian		status &= MY_INTRS;
139693746Sjulian		if (status)
139793746Sjulian			CSR_WRITE_4(sc, MY_ISR, status);
139893746Sjulian		else
139993746Sjulian			break;
140093746Sjulian
140193746Sjulian		if (status & MY_RI)	/* receive interrupt */
140293746Sjulian			my_rxeof(sc);
140393746Sjulian
140493746Sjulian		if ((status & MY_RBU) || (status & MY_RxErr)) {
140593746Sjulian			/* rx buffer unavailable or rx error */
140693746Sjulian			ifp->if_ierrors++;
140793746Sjulian#ifdef foo
140893746Sjulian			my_stop(sc);
140993746Sjulian			my_reset(sc);
141093746Sjulian			my_init(sc);
141193746Sjulian#endif
141293746Sjulian		}
141393746Sjulian		if (status & MY_TI)	/* tx interrupt */
141493746Sjulian			my_txeof(sc);
141593746Sjulian		if (status & MY_ETI)	/* tx early interrupt */
141693746Sjulian			my_txeof(sc);
141793746Sjulian		if (status & MY_TBU)	/* tx buffer unavailable */
141893746Sjulian			my_txeoc(sc);
141993746Sjulian		/*
142093746Sjulian		 * 90/1/18 delete if (status & MY_FBE) { my_reset(sc);
142193746Sjulian		 * my_init(sc); }
142293746Sjulian		 */
142393746Sjulian	}
142493746Sjulian
142593746Sjulian	/* Re-enable interrupts. */
142693746Sjulian	CSR_WRITE_4(sc, MY_IMR, MY_INTRS);
142793746Sjulian	if (ifp->if_snd.ifq_head != NULL)
142893746Sjulian		my_start(ifp);
142993746Sjulian	MY_UNLOCK(sc);
143093746Sjulian	return;
143193746Sjulian}
143293746Sjulian
143393746Sjulian/*
143493746Sjulian * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
143593746Sjulian * pointers to the fragment pointers.
143693746Sjulian */
143793746Sjulianstatic int
143893746Sjulianmy_encap(struct my_softc * sc, struct my_chain * c, struct mbuf * m_head)
143993746Sjulian{
144093746Sjulian	struct my_desc *f = NULL;
144193746Sjulian	int             total_len;
144293746Sjulian	struct mbuf    *m, *m_new = NULL;
144393746Sjulian
144493746Sjulian	MY_LOCK(sc);
144593746Sjulian	/* calculate the total tx pkt length */
144693746Sjulian	total_len = 0;
144793746Sjulian	for (m = m_head; m != NULL; m = m->m_next)
144893746Sjulian		total_len += m->m_len;
144993746Sjulian	/*
145093746Sjulian	 * Start packing the mbufs in this chain into the fragment pointers.
145193746Sjulian	 * Stop when we run out of fragments or hit the end of the mbuf
145293746Sjulian	 * chain.
145393746Sjulian	 */
145493746Sjulian	m = m_head;
145593746Sjulian	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
145693746Sjulian	if (m_new == NULL) {
145793746Sjulian		printf("my%d: no memory for tx list", sc->my_unit);
145893746Sjulian		return (1);
145993746Sjulian	}
146093746Sjulian	if (m_head->m_pkthdr.len > MHLEN) {
146193746Sjulian		MCLGET(m_new, M_DONTWAIT);
146293746Sjulian		if (!(m_new->m_flags & M_EXT)) {
146393746Sjulian			m_freem(m_new);
146493746Sjulian			printf("my%d: no memory for tx list", sc->my_unit);
146593746Sjulian			return (1);
146693746Sjulian		}
146793746Sjulian	}
146893746Sjulian	m_copydata(m_head, 0, m_head->m_pkthdr.len, mtod(m_new, caddr_t));
146993746Sjulian	m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
147093746Sjulian	m_freem(m_head);
147193746Sjulian	m_head = m_new;
147293746Sjulian	f = &c->my_ptr->my_frag[0];
147393746Sjulian	f->my_status = 0;
147493746Sjulian	f->my_data = vtophys(mtod(m_new, caddr_t));
147593746Sjulian	total_len = m_new->m_len;
147693746Sjulian	f->my_ctl = MY_TXFD | MY_TXLD | MY_CRCEnable | MY_PADEnable;
147793746Sjulian	f->my_ctl |= total_len << MY_PKTShift;	/* pkt size */
147893746Sjulian	f->my_ctl |= total_len;	/* buffer size */
147993746Sjulian	/* 89/12/29 add, for mtd891 */
148093746Sjulian	if (sc->my_info->my_did == MTD891ID)
148193746Sjulian		f->my_ctl |= MY_ETIControl | MY_RetryTxLC;
148293746Sjulian	c->my_mbuf = m_head;
148393746Sjulian	c->my_lastdesc = 0;
148493746Sjulian	MY_TXNEXT(c) = vtophys(&c->my_nextdesc->my_ptr->my_frag[0]);
148593746Sjulian	MY_UNLOCK(sc);
148693746Sjulian	return (0);
148793746Sjulian}
148893746Sjulian
148993746Sjulian/*
149093746Sjulian * Main transmit routine. To avoid having to do mbuf copies, we put pointers
149193746Sjulian * to the mbuf data regions directly in the transmit lists. We also save a
149293746Sjulian * copy of the pointers since the transmit list fragment pointers are
149393746Sjulian * physical addresses.
149493746Sjulian */
149593746Sjulianstatic void
149693746Sjulianmy_start(struct ifnet * ifp)
149793746Sjulian{
149893746Sjulian	struct my_softc *sc;
149993746Sjulian	struct mbuf    *m_head = NULL;
150093746Sjulian	struct my_chain *cur_tx = NULL, *start_tx;
150193746Sjulian
150293746Sjulian	sc = ifp->if_softc;
150393746Sjulian	MY_LOCK(sc);
150493746Sjulian	if (sc->my_autoneg) {
150593746Sjulian		sc->my_tx_pend = 1;
150693746Sjulian		MY_UNLOCK(sc);
150793746Sjulian		return;
150893746Sjulian	}
150993746Sjulian	/*
151093746Sjulian	 * Check for an available queue slot. If there are none, punt.
151193746Sjulian	 */
151293746Sjulian	if (sc->my_cdata.my_tx_free->my_mbuf != NULL) {
151393746Sjulian		ifp->if_flags |= IFF_OACTIVE;
151493746Sjulian		MY_UNLOCK(sc);
151593746Sjulian		return;
151693746Sjulian	}
151793746Sjulian	start_tx = sc->my_cdata.my_tx_free;
151893746Sjulian	while (sc->my_cdata.my_tx_free->my_mbuf == NULL) {
151993746Sjulian		IF_DEQUEUE(&ifp->if_snd, m_head);
152093746Sjulian		if (m_head == NULL)
152193746Sjulian			break;
152293746Sjulian
152393746Sjulian		/* Pick a descriptor off the free list. */
152493746Sjulian		cur_tx = sc->my_cdata.my_tx_free;
152593746Sjulian		sc->my_cdata.my_tx_free = cur_tx->my_nextdesc;
152693746Sjulian
152793746Sjulian		/* Pack the data into the descriptor. */
152893746Sjulian		my_encap(sc, cur_tx, m_head);
152993746Sjulian
153093746Sjulian		if (cur_tx != start_tx)
153193746Sjulian			MY_TXOWN(cur_tx) = MY_OWNByNIC;
153293746Sjulian#if NBPFILTER > 0
153393746Sjulian		/*
153493746Sjulian		 * If there's a BPF listener, bounce a copy of this frame to
153593746Sjulian		 * him.
153693746Sjulian		 */
153793746Sjulian		if (ifp->if_bpf)
153893746Sjulian			bpf_mtap(ifp, cur_tx->my_mbuf);
153993746Sjulian#endif
154093746Sjulian	}
154193746Sjulian	/*
154293746Sjulian	 * If there are no packets queued, bail.
154393746Sjulian	 */
154493746Sjulian	if (cur_tx == NULL) {
154593746Sjulian		MY_UNLOCK(sc);
154693746Sjulian		return;
154793746Sjulian	}
154893746Sjulian	/*
154993746Sjulian	 * Place the request for the upload interrupt in the last descriptor
155093746Sjulian	 * in the chain. This way, if we're chaining several packets at once,
155193746Sjulian	 * we'll only get an interupt once for the whole chain rather than
155293746Sjulian	 * once for each packet.
155393746Sjulian	 */
155493746Sjulian	MY_TXCTL(cur_tx) |= MY_TXIC;
155593746Sjulian	cur_tx->my_ptr->my_frag[0].my_ctl |= MY_TXIC;
155693746Sjulian	sc->my_cdata.my_tx_tail = cur_tx;
155793746Sjulian	if (sc->my_cdata.my_tx_head == NULL)
155893746Sjulian		sc->my_cdata.my_tx_head = start_tx;
155993746Sjulian	MY_TXOWN(start_tx) = MY_OWNByNIC;
156093746Sjulian	CSR_WRITE_4(sc, MY_TXPDR, 0xFFFFFFFF);	/* tx polling demand */
156193746Sjulian
156293746Sjulian	/*
156393746Sjulian	 * Set a timeout in case the chip goes out to lunch.
156493746Sjulian	 */
156593746Sjulian	ifp->if_timer = 5;
156693746Sjulian	MY_UNLOCK(sc);
156793746Sjulian	return;
156893746Sjulian}
156993746Sjulian
157093746Sjulianstatic void
157193746Sjulianmy_init(void *xsc)
157293746Sjulian{
157393746Sjulian	struct my_softc *sc = xsc;
157493746Sjulian	struct ifnet   *ifp = &sc->arpcom.ac_if;
157593746Sjulian	int             s;
157693746Sjulian	u_int16_t       phy_bmcr = 0;
157793746Sjulian
157893746Sjulian	MY_LOCK(sc);
157993746Sjulian	if (sc->my_autoneg) {
158093746Sjulian		MY_UNLOCK(sc);
158193746Sjulian		return;
158293746Sjulian	}
158393746Sjulian	s = splimp();
158493746Sjulian	if (sc->my_pinfo != NULL)
158593746Sjulian		phy_bmcr = my_phy_readreg(sc, PHY_BMCR);
158693746Sjulian	/*
158793746Sjulian	 * Cancel pending I/O and free all RX/TX buffers.
158893746Sjulian	 */
158993746Sjulian	my_stop(sc);
159093746Sjulian	my_reset(sc);
159193746Sjulian
159293746Sjulian	/*
159393746Sjulian	 * Set cache alignment and burst length.
159493746Sjulian	 */
159593746Sjulian	/*
159693746Sjulian	 * 89/9/1 modify, CSR_WRITE_4(sc, MY_BCR, MY_RPBLE512);
159793746Sjulian	 * CSR_WRITE_4(sc, MY_TCRRCR, MY_TFTSF);
159893746Sjulian	 */
159993746Sjulian	CSR_WRITE_4(sc, MY_BCR, MY_PBL8);
160093746Sjulian	CSR_WRITE_4(sc, MY_TCRRCR, MY_TFTSF | MY_RBLEN | MY_RPBLE512);
160193746Sjulian	/*
160293746Sjulian	 * 89/12/29 add, for mtd891,
160393746Sjulian	 */
160493746Sjulian	if (sc->my_info->my_did == MTD891ID) {
160593746Sjulian		MY_SETBIT(sc, MY_BCR, MY_PROG);
160693746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_Enhanced);
160793746Sjulian	}
160893746Sjulian	my_setcfg(sc, phy_bmcr);
160993746Sjulian	/* Init circular RX list. */
161093746Sjulian	if (my_list_rx_init(sc) == ENOBUFS) {
161193746Sjulian		printf("my%d: init failed: no memory for rx buffers\n",
161293746Sjulian		    sc->my_unit);
161393746Sjulian		my_stop(sc);
161493746Sjulian		(void)splx(s);
161593746Sjulian		MY_UNLOCK(sc);
161693746Sjulian		return;
161793746Sjulian	}
161893746Sjulian	/* Init TX descriptors. */
161993746Sjulian	my_list_tx_init(sc);
162093746Sjulian
162193746Sjulian	/* If we want promiscuous mode, set the allframes bit. */
162293746Sjulian	if (ifp->if_flags & IFF_PROMISC)
162393746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_PROM);
162493746Sjulian	else
162593746Sjulian		MY_CLRBIT(sc, MY_TCRRCR, MY_PROM);
162693746Sjulian
162793746Sjulian	/*
162893746Sjulian	 * Set capture broadcast bit to capture broadcast frames.
162993746Sjulian	 */
163093746Sjulian	if (ifp->if_flags & IFF_BROADCAST)
163193746Sjulian		MY_SETBIT(sc, MY_TCRRCR, MY_AB);
163293746Sjulian	else
163393746Sjulian		MY_CLRBIT(sc, MY_TCRRCR, MY_AB);
163493746Sjulian
163593746Sjulian	/*
163693746Sjulian	 * Program the multicast filter, if necessary.
163793746Sjulian	 */
163893746Sjulian	my_setmulti(sc);
163993746Sjulian
164093746Sjulian	/*
164193746Sjulian	 * Load the address of the RX list.
164293746Sjulian	 */
164393746Sjulian	MY_CLRBIT(sc, MY_TCRRCR, MY_RE);
164493746Sjulian	CSR_WRITE_4(sc, MY_RXLBA, vtophys(&sc->my_ldata->my_rx_list[0]));
164593746Sjulian
164693746Sjulian	/*
164793746Sjulian	 * Enable interrupts.
164893746Sjulian	 */
164993746Sjulian	CSR_WRITE_4(sc, MY_IMR, MY_INTRS);
165093746Sjulian	CSR_WRITE_4(sc, MY_ISR, 0xFFFFFFFF);
165193746Sjulian
165293746Sjulian	/* Enable receiver and transmitter. */
165393746Sjulian	MY_SETBIT(sc, MY_TCRRCR, MY_RE);
165493746Sjulian	MY_CLRBIT(sc, MY_TCRRCR, MY_TE);
165593746Sjulian	CSR_WRITE_4(sc, MY_TXLBA, vtophys(&sc->my_ldata->my_tx_list[0]));
165693746Sjulian	MY_SETBIT(sc, MY_TCRRCR, MY_TE);
165793746Sjulian
165893746Sjulian	/* Restore state of BMCR */
165993746Sjulian	if (sc->my_pinfo != NULL)
166093746Sjulian		my_phy_writereg(sc, PHY_BMCR, phy_bmcr);
166193746Sjulian	ifp->if_flags |= IFF_RUNNING;
166293746Sjulian	ifp->if_flags &= ~IFF_OACTIVE;
166393746Sjulian	(void)splx(s);
166493746Sjulian	MY_UNLOCK(sc);
166593746Sjulian	return;
166693746Sjulian}
166793746Sjulian
166893746Sjulian/*
166993746Sjulian * Set media options.
167093746Sjulian */
167193746Sjulian
167293746Sjulianstatic int
167393746Sjulianmy_ifmedia_upd(struct ifnet * ifp)
167493746Sjulian{
167593746Sjulian	struct my_softc *sc;
167693746Sjulian	struct ifmedia *ifm;
167793746Sjulian
167893746Sjulian	sc = ifp->if_softc;
167993746Sjulian	MY_LOCK(sc);
168093746Sjulian	ifm = &sc->ifmedia;
168193746Sjulian	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
168293746Sjulian		MY_UNLOCK(sc);
168393746Sjulian		return (EINVAL);
168493746Sjulian	}
168593746Sjulian	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
168693746Sjulian		my_autoneg_mii(sc, MY_FLAG_SCHEDDELAY, 1);
168793746Sjulian	else
168893746Sjulian		my_setmode_mii(sc, ifm->ifm_media);
168993746Sjulian	MY_UNLOCK(sc);
169093746Sjulian	return (0);
169193746Sjulian}
169293746Sjulian
169393746Sjulian/*
169493746Sjulian * Report current media status.
169593746Sjulian */
169693746Sjulian
169793746Sjulianstatic void
169893746Sjulianmy_ifmedia_sts(struct ifnet * ifp, struct ifmediareq * ifmr)
169993746Sjulian{
170093746Sjulian	struct my_softc *sc;
170193746Sjulian	u_int16_t advert = 0, ability = 0;
170293746Sjulian
170393746Sjulian	sc = ifp->if_softc;
170493746Sjulian	MY_LOCK(sc);
170593746Sjulian	ifmr->ifm_active = IFM_ETHER;
170693746Sjulian	if (!(my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_AUTONEGENBL)) {
170793746Sjulian		/*
170893746Sjulian		 * this version did not support 1000M, if (my_phy_readreg(sc,
170993746Sjulian		 * PHY_BMCR) & PHY_BMCR_1000) ifmr->ifm_active =
171093746Sjulian		 * IFM_ETHER|IFM_1000TX;
171193746Sjulian		 */
171293746Sjulian		if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_SPEEDSEL)
171393746Sjulian			ifmr->ifm_active = IFM_ETHER | IFM_100_TX;
171493746Sjulian		else
171593746Sjulian			ifmr->ifm_active = IFM_ETHER | IFM_10_T;
171693746Sjulian		if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_DUPLEX)
171793746Sjulian			ifmr->ifm_active |= IFM_FDX;
171893746Sjulian		else
171993746Sjulian			ifmr->ifm_active |= IFM_HDX;
172093746Sjulian
172193746Sjulian		MY_UNLOCK(sc);
172293746Sjulian		return;
172393746Sjulian	}
172493746Sjulian	ability = my_phy_readreg(sc, PHY_LPAR);
172593746Sjulian	advert = my_phy_readreg(sc, PHY_ANAR);
172693746Sjulian
172793746Sjulian	/*
172893746Sjulian	 * this version did not support 1000M,
172993746Sjulian	 */
173093746Sjulian#if 0
173193746Sjulian	if (sc->my_pinfo->my_vid = MarvellPHYID0) {
173293746Sjulian		ability2 = my_phy_readreg(sc, PHY_1000SR);
173393746Sjulian		if (ability2 & PHY_1000SR_1000BTXFULL) {
173493746Sjulian			advert = 0;
173593746Sjulian			ability = 0;
173693746Sjulian	  		ifmr->ifm_active = IFM_ETHER|IFM_1000_TX|IFM_FDX;
173793746Sjulian	  	} else if (ability & PHY_1000SR_1000BTXHALF) {
173893746Sjulian			advert = 0;
173993746Sjulian			ability = 0;
174093746Sjulian			ifmr->ifm_active = IFM_ETHER|IFM_1000_TX|IFM_HDX;
174193746Sjulian		}
174293746Sjulian	}
174393746Sjulian#endif
174493746Sjulian	if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4)
174593746Sjulian		ifmr->ifm_active = IFM_ETHER | IFM_100_T4;
174693746Sjulian	else if (advert & PHY_ANAR_100BTXFULL && ability & PHY_ANAR_100BTXFULL)
174793746Sjulian		ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
174893746Sjulian	else if (advert & PHY_ANAR_100BTXHALF && ability & PHY_ANAR_100BTXHALF)
174993746Sjulian		ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
175093746Sjulian	else if (advert & PHY_ANAR_10BTFULL && ability & PHY_ANAR_10BTFULL)
175193746Sjulian		ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_FDX;
175293746Sjulian	else if (advert & PHY_ANAR_10BTHALF && ability & PHY_ANAR_10BTHALF)
175393746Sjulian		ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_HDX;
175493746Sjulian	MY_UNLOCK(sc);
175593746Sjulian	return;
175693746Sjulian}
175793746Sjulian
175893746Sjulianstatic int
175993746Sjulianmy_ioctl(struct ifnet * ifp, u_long command, caddr_t data)
176093746Sjulian{
176193746Sjulian	struct my_softc *sc = ifp->if_softc;
176293746Sjulian	struct ifreq   *ifr = (struct ifreq *) data;
176393746Sjulian	int             s, error = 0;
176493746Sjulian
176593746Sjulian	s = splimp();
176693746Sjulian	MY_LOCK(sc);
176793746Sjulian	switch (command) {
176893746Sjulian	case SIOCSIFADDR:
176993746Sjulian	case SIOCGIFADDR:
177093746Sjulian	case SIOCSIFMTU:
177193746Sjulian		error = ether_ioctl(ifp, command, data);
177293746Sjulian		break;
177393746Sjulian	case SIOCSIFFLAGS:
177493746Sjulian		if (ifp->if_flags & IFF_UP)
177593746Sjulian			my_init(sc);
177693746Sjulian		else if (ifp->if_flags & IFF_RUNNING)
177793746Sjulian			my_stop(sc);
177893746Sjulian		error = 0;
177993746Sjulian		break;
178093746Sjulian	case SIOCADDMULTI:
178193746Sjulian	case SIOCDELMULTI:
178293746Sjulian		my_setmulti(sc);
178393746Sjulian		error = 0;
178493746Sjulian		break;
178593746Sjulian	case SIOCGIFMEDIA:
178693746Sjulian	case SIOCSIFMEDIA:
178793746Sjulian		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
178893746Sjulian		break;
178993746Sjulian	default:
179093746Sjulian		error = EINVAL;
179193746Sjulian		break;
179293746Sjulian	}
179393746Sjulian	MY_UNLOCK(sc);
179493746Sjulian	(void)splx(s);
179593746Sjulian	return (error);
179693746Sjulian}
179793746Sjulian
179893746Sjulianstatic void
179993746Sjulianmy_watchdog(struct ifnet * ifp)
180093746Sjulian{
180193746Sjulian	struct my_softc *sc;
180293746Sjulian
180393746Sjulian	sc = ifp->if_softc;
180493746Sjulian	MY_LOCK(sc);
180593746Sjulian	if (sc->my_autoneg) {
180693746Sjulian		my_autoneg_mii(sc, MY_FLAG_DELAYTIMEO, 1);
180793746Sjulian		MY_UNLOCK(sc);
180893746Sjulian		return;
180993746Sjulian	}
181093746Sjulian	ifp->if_oerrors++;
181193746Sjulian	printf("my%d: watchdog timeout\n", sc->my_unit);
181293746Sjulian	if (!(my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
181393746Sjulian		printf("my%d: no carrier - transceiver cable problem?\n",
181493746Sjulian		    sc->my_unit);
181593746Sjulian	my_stop(sc);
181693746Sjulian	my_reset(sc);
181793746Sjulian	my_init(sc);
181893746Sjulian	if (ifp->if_snd.ifq_head != NULL)
181993746Sjulian		my_start(ifp);
182093746Sjulian	MY_LOCK(sc);
182193746Sjulian	return;
182293746Sjulian}
182393746Sjulian
182493746Sjulian
182593746Sjulian/*
182693746Sjulian * Stop the adapter and free any mbufs allocated to the RX and TX lists.
182793746Sjulian */
182893746Sjulianstatic void
182993746Sjulianmy_stop(struct my_softc * sc)
183093746Sjulian{
183193746Sjulian	register int    i;
183293746Sjulian	struct ifnet   *ifp;
183393746Sjulian
183493746Sjulian	MY_LOCK(sc);
183593746Sjulian	ifp = &sc->arpcom.ac_if;
183693746Sjulian	ifp->if_timer = 0;
183793746Sjulian
183893746Sjulian	MY_CLRBIT(sc, MY_TCRRCR, (MY_RE | MY_TE));
183993746Sjulian	CSR_WRITE_4(sc, MY_IMR, 0x00000000);
184093746Sjulian	CSR_WRITE_4(sc, MY_TXLBA, 0x00000000);
184193746Sjulian	CSR_WRITE_4(sc, MY_RXLBA, 0x00000000);
184293746Sjulian
184393746Sjulian	/*
184493746Sjulian	 * Free data in the RX lists.
184593746Sjulian	 */
184693746Sjulian	for (i = 0; i < MY_RX_LIST_CNT; i++) {
184793746Sjulian		if (sc->my_cdata.my_rx_chain[i].my_mbuf != NULL) {
184893746Sjulian			m_freem(sc->my_cdata.my_rx_chain[i].my_mbuf);
184993746Sjulian			sc->my_cdata.my_rx_chain[i].my_mbuf = NULL;
185093746Sjulian		}
185193746Sjulian	}
185293746Sjulian	bzero((char *)&sc->my_ldata->my_rx_list,
185393746Sjulian	    sizeof(sc->my_ldata->my_rx_list));
185493746Sjulian	/*
185593746Sjulian	 * Free the TX list buffers.
185693746Sjulian	 */
185793746Sjulian	for (i = 0; i < MY_TX_LIST_CNT; i++) {
185893746Sjulian		if (sc->my_cdata.my_tx_chain[i].my_mbuf != NULL) {
185993746Sjulian			m_freem(sc->my_cdata.my_tx_chain[i].my_mbuf);
186093746Sjulian			sc->my_cdata.my_tx_chain[i].my_mbuf = NULL;
186193746Sjulian		}
186293746Sjulian	}
186393746Sjulian	bzero((char *)&sc->my_ldata->my_tx_list,
186493746Sjulian	    sizeof(sc->my_ldata->my_tx_list));
186593746Sjulian	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
186693746Sjulian	MY_UNLOCK(sc);
186793746Sjulian	return;
186893746Sjulian}
186993746Sjulian
187093746Sjulian/*
187193746Sjulian * Stop all chip I/O so that the kernel's probe routines don't get confused
187293746Sjulian * by errant DMAs when rebooting.
187393746Sjulian */
187493746Sjulianstatic void
187593746Sjulianmy_shutdown(device_t dev)
187693746Sjulian{
187793746Sjulian	struct my_softc *sc;
187893746Sjulian
187993746Sjulian	sc = device_get_softc(dev);
188093746Sjulian	my_stop(sc);
188193746Sjulian	return;
188293746Sjulian}
1883