if_pcn.c revision 188177
1139825Simp/*-
266131Swpaul * Copyright (c) 2000 Berkeley Software Design, Inc.
366131Swpaul * Copyright (c) 1997, 1998, 1999, 2000
466131Swpaul *	Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
566131Swpaul *
666131Swpaul * Redistribution and use in source and binary forms, with or without
766131Swpaul * modification, are permitted provided that the following conditions
866131Swpaul * are met:
966131Swpaul * 1. Redistributions of source code must retain the above copyright
1066131Swpaul *    notice, this list of conditions and the following disclaimer.
1166131Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1266131Swpaul *    notice, this list of conditions and the following disclaimer in the
1366131Swpaul *    documentation and/or other materials provided with the distribution.
1466131Swpaul * 3. All advertising materials mentioning features or use of this software
1566131Swpaul *    must display the following acknowledgement:
1666131Swpaul *	This product includes software developed by Bill Paul.
1766131Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1866131Swpaul *    may be used to endorse or promote products derived from this software
1966131Swpaul *    without specific prior written permission.
2066131Swpaul *
2166131Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2266131Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2366131Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2466131Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2566131Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2666131Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2766131Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2866131Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2966131Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3066131Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3166131Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3266131Swpaul */
3366131Swpaul
34122678Sobrien#include <sys/cdefs.h>
35122678Sobrien__FBSDID("$FreeBSD: head/sys/dev/pcn/if_pcn.c 188177 2009-02-05 19:36:14Z imp $");
36122678Sobrien
3766131Swpaul/*
38134842Sbrueffer * AMD Am79c972 fast ethernet PCI NIC driver. Datasheets are available
3966131Swpaul * from http://www.amd.com.
4066131Swpaul *
4166131Swpaul * The AMD PCnet/PCI controllers are more advanced and functional
4266131Swpaul * versions of the venerable 7990 LANCE. The PCnet/PCI chips retain
4366131Swpaul * backwards compatibility with the LANCE and thus can be made
4466131Swpaul * to work with older LANCE drivers. This is in fact how the
4566131Swpaul * PCnet/PCI chips were supported in FreeBSD originally. The trouble
4666131Swpaul * is that the PCnet/PCI devices offer several performance enhancements
4766131Swpaul * which can't be exploited in LANCE compatibility mode. Chief among
4866131Swpaul * these enhancements is the ability to perform PCI DMA operations
4966131Swpaul * using 32-bit addressing (which eliminates the need for ISA
5066131Swpaul * bounce-buffering), and special receive buffer alignment (which
5166131Swpaul * allows the receive handler to pass packets to the upper protocol
5266131Swpaul * layers without copying on both the x86 and alpha platforms).
5366131Swpaul */
5466131Swpaul
5566131Swpaul#include <sys/param.h>
5666131Swpaul#include <sys/systm.h>
5766131Swpaul#include <sys/sockio.h>
5866131Swpaul#include <sys/mbuf.h>
5966131Swpaul#include <sys/malloc.h>
6066131Swpaul#include <sys/kernel.h>
61129878Sphk#include <sys/module.h>
6266131Swpaul#include <sys/socket.h>
6366131Swpaul
6466131Swpaul#include <net/if.h>
6566131Swpaul#include <net/if_arp.h>
6666131Swpaul#include <net/ethernet.h>
6766131Swpaul#include <net/if_dl.h>
6866131Swpaul#include <net/if_media.h>
69147256Sbrooks#include <net/if_types.h>
7066131Swpaul
7166131Swpaul#include <net/bpf.h>
7266131Swpaul
7366131Swpaul#include <vm/vm.h>              /* for vtophys */
7466131Swpaul#include <vm/pmap.h>            /* for vtophys */
7566131Swpaul#include <machine/bus.h>
7666131Swpaul#include <machine/resource.h>
7766131Swpaul#include <sys/bus.h>
7866131Swpaul#include <sys/rman.h>
7966131Swpaul
8066131Swpaul#include <dev/mii/mii.h>
8166131Swpaul#include <dev/mii/miivar.h>
8266131Swpaul
83119288Simp#include <dev/pci/pcireg.h>
84119288Simp#include <dev/pci/pcivar.h>
8566131Swpaul
8666131Swpaul#define PCN_USEIOSPACE
8766131Swpaul
88181740Simp#include <dev/pcn/if_pcnreg.h>
8966131Swpaul
90113506SmdoddMODULE_DEPEND(pcn, pci, 1, 1, 1);
91113506SmdoddMODULE_DEPEND(pcn, ether, 1, 1, 1);
9266131SwpaulMODULE_DEPEND(pcn, miibus, 1, 1, 1);
9366131Swpaul
94149584Simp/* "device miibus" required.  See GENERIC if you get errors here. */
9566131Swpaul#include "miibus_if.h"
9666131Swpaul
9766131Swpaul/*
9866131Swpaul * Various supported device vendors/types and their names.
9966131Swpaul */
100164072Smariusstatic const struct pcn_type pcn_devs[] = {
10166131Swpaul	{ PCN_VENDORID, PCN_DEVICEID_PCNET, "AMD PCnet/PCI 10/100BaseTX" },
10266131Swpaul	{ PCN_VENDORID, PCN_DEVICEID_HOME, "AMD PCnet/Home HomePNA" },
10366131Swpaul	{ 0, 0, NULL }
10466131Swpaul};
10566131Swpaul
106164072Smariusstatic const struct pcn_chipid {
107138354Smdodd	u_int32_t	id;
108164072Smarius	const char	*name;
109138354Smdodd} pcn_chipid[] = {
110138354Smdodd	{ Am79C971,	"Am79C971" },
111138354Smdodd	{ Am79C972,	"Am79C972" },
112138354Smdodd	{ Am79C973,	"Am79C973" },
113138354Smdodd	{ Am79C978,	"Am79C978" },
114138354Smdodd	{ Am79C975,	"Am79C975" },
115138354Smdodd	{ Am79C976,	"Am79C976" },
116138354Smdodd	{ 0, NULL },
117138354Smdodd};
118138354Smdodd
119164072Smariusstatic const char *pcn_chipid_name(u_int32_t);
120142407Simpstatic u_int32_t pcn_chip_id(device_t);
121164072Smariusstatic const struct pcn_type *pcn_match(u_int16_t, u_int16_t);
122138354Smdodd
123142407Simpstatic u_int32_t pcn_csr_read(struct pcn_softc *, int);
124142407Simpstatic u_int16_t pcn_csr_read16(struct pcn_softc *, int);
125142407Simpstatic u_int16_t pcn_bcr_read16(struct pcn_softc *, int);
126142407Simpstatic void pcn_csr_write(struct pcn_softc *, int, int);
127142407Simpstatic u_int32_t pcn_bcr_read(struct pcn_softc *, int);
128142407Simpstatic void pcn_bcr_write(struct pcn_softc *, int, int);
12966131Swpaul
130142407Simpstatic int pcn_probe(device_t);
131142407Simpstatic int pcn_attach(device_t);
132142407Simpstatic int pcn_detach(device_t);
13366131Swpaul
134142407Simpstatic int pcn_newbuf(struct pcn_softc *, int, struct mbuf *);
135142407Simpstatic int pcn_encap(struct pcn_softc *, struct mbuf *, u_int32_t *);
136142407Simpstatic void pcn_rxeof(struct pcn_softc *);
137142407Simpstatic void pcn_txeof(struct pcn_softc *);
138142407Simpstatic void pcn_intr(void *);
139142407Simpstatic void pcn_tick(void *);
140142407Simpstatic void pcn_start(struct ifnet *);
141148738Sjhbstatic void pcn_start_locked(struct ifnet *);
142142407Simpstatic int pcn_ioctl(struct ifnet *, u_long, caddr_t);
143142407Simpstatic void pcn_init(void *);
144148738Sjhbstatic void pcn_init_locked(struct pcn_softc *);
145142407Simpstatic void pcn_stop(struct pcn_softc *);
146142407Simpstatic void pcn_watchdog(struct ifnet *);
147188177Simpstatic int pcn_shutdown(device_t);
148142407Simpstatic int pcn_ifmedia_upd(struct ifnet *);
149142407Simpstatic void pcn_ifmedia_sts(struct ifnet *, struct ifmediareq *);
15066131Swpaul
151142407Simpstatic int pcn_miibus_readreg(device_t, int, int);
152142407Simpstatic int pcn_miibus_writereg(device_t, int, int, int);
153142407Simpstatic void pcn_miibus_statchg(device_t);
15466131Swpaul
155142407Simpstatic void pcn_setfilt(struct ifnet *);
156142407Simpstatic void pcn_setmulti(struct pcn_softc *);
157142407Simpstatic void pcn_reset(struct pcn_softc *);
158142407Simpstatic int pcn_list_rx_init(struct pcn_softc *);
159142407Simpstatic int pcn_list_tx_init(struct pcn_softc *);
16066131Swpaul
16166131Swpaul#ifdef PCN_USEIOSPACE
16266131Swpaul#define PCN_RES			SYS_RES_IOPORT
16366131Swpaul#define PCN_RID			PCN_PCI_LOIO
16466131Swpaul#else
16566131Swpaul#define PCN_RES			SYS_RES_MEMORY
16666131Swpaul#define PCN_RID			PCN_PCI_LOMEM
16766131Swpaul#endif
16866131Swpaul
16966131Swpaulstatic device_method_t pcn_methods[] = {
17066131Swpaul	/* Device interface */
17166131Swpaul	DEVMETHOD(device_probe,		pcn_probe),
17266131Swpaul	DEVMETHOD(device_attach,	pcn_attach),
17366131Swpaul	DEVMETHOD(device_detach,	pcn_detach),
17466131Swpaul	DEVMETHOD(device_shutdown,	pcn_shutdown),
17566131Swpaul
17666131Swpaul	/* bus interface */
17766131Swpaul	DEVMETHOD(bus_print_child,	bus_generic_print_child),
17866131Swpaul	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
17966131Swpaul
18066131Swpaul	/* MII interface */
18166131Swpaul	DEVMETHOD(miibus_readreg,	pcn_miibus_readreg),
18266131Swpaul	DEVMETHOD(miibus_writereg,	pcn_miibus_writereg),
18366131Swpaul	DEVMETHOD(miibus_statchg,	pcn_miibus_statchg),
18466131Swpaul
18566131Swpaul	{ 0, 0 }
18666131Swpaul};
18766131Swpaul
18866131Swpaulstatic driver_t pcn_driver = {
18966131Swpaul	"pcn",
19066131Swpaul	pcn_methods,
19166131Swpaul	sizeof(struct pcn_softc)
19266131Swpaul};
19366131Swpaul
19466131Swpaulstatic devclass_t pcn_devclass;
19566131Swpaul
196113506SmdoddDRIVER_MODULE(pcn, pci, pcn_driver, pcn_devclass, 0, 0);
19766131SwpaulDRIVER_MODULE(miibus, pcn, miibus_driver, miibus_devclass, 0, 0);
19866131Swpaul
19966131Swpaul#define PCN_CSR_SETBIT(sc, reg, x)			\
20066131Swpaul	pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) | (x))
20166131Swpaul
20266131Swpaul#define PCN_CSR_CLRBIT(sc, reg, x)			\
20366131Swpaul	pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) & ~(x))
20466131Swpaul
20566131Swpaul#define PCN_BCR_SETBIT(sc, reg, x)			\
20666131Swpaul	pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) | (x))
20766131Swpaul
20866131Swpaul#define PCN_BCR_CLRBIT(sc, reg, x)			\
20966131Swpaul	pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) & ~(x))
21066131Swpaul
211102335Salfredstatic u_int32_t
212102335Salfredpcn_csr_read(sc, reg)
21366131Swpaul	struct pcn_softc	*sc;
21466131Swpaul	int			reg;
21566131Swpaul{
21666131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
21766131Swpaul	return(CSR_READ_4(sc, PCN_IO32_RDP));
21866131Swpaul}
21966131Swpaul
220102335Salfredstatic u_int16_t
221102335Salfredpcn_csr_read16(sc, reg)
22268837Swpaul	struct pcn_softc	*sc;
22368837Swpaul	int			reg;
22468837Swpaul{
22568837Swpaul	CSR_WRITE_2(sc, PCN_IO16_RAP, reg);
22668837Swpaul	return(CSR_READ_2(sc, PCN_IO16_RDP));
22768837Swpaul}
22868837Swpaul
229102335Salfredstatic void
230102335Salfredpcn_csr_write(sc, reg, val)
23166131Swpaul	struct pcn_softc	*sc;
23266131Swpaul	int			reg;
233113799Sobrien	int			val;
23466131Swpaul{
23566131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
23666131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RDP, val);
23766131Swpaul	return;
23866131Swpaul}
23966131Swpaul
240102335Salfredstatic u_int32_t
241102335Salfredpcn_bcr_read(sc, reg)
24266131Swpaul	struct pcn_softc	*sc;
24366131Swpaul	int			reg;
24466131Swpaul{
24566131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
24666131Swpaul	return(CSR_READ_4(sc, PCN_IO32_BDP));
24766131Swpaul}
24866131Swpaul
249102335Salfredstatic u_int16_t
250102335Salfredpcn_bcr_read16(sc, reg)
25169067Swpaul	struct pcn_softc	*sc;
25269067Swpaul	int			reg;
25369067Swpaul{
25469067Swpaul	CSR_WRITE_2(sc, PCN_IO16_RAP, reg);
25569067Swpaul	return(CSR_READ_2(sc, PCN_IO16_BDP));
25669067Swpaul}
25769067Swpaul
258102335Salfredstatic void
259102335Salfredpcn_bcr_write(sc, reg, val)
26066131Swpaul	struct pcn_softc	*sc;
26166131Swpaul	int			reg;
262113799Sobrien	int			val;
26366131Swpaul{
26466131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
26566131Swpaul	CSR_WRITE_4(sc, PCN_IO32_BDP, val);
26666131Swpaul	return;
26766131Swpaul}
26866131Swpaul
269102335Salfredstatic int
270102335Salfredpcn_miibus_readreg(dev, phy, reg)
27166131Swpaul	device_t		dev;
27266131Swpaul	int			phy, reg;
27366131Swpaul{
27466131Swpaul	struct pcn_softc	*sc;
27566131Swpaul	int			val;
27666131Swpaul
27766131Swpaul	sc = device_get_softc(dev);
27866131Swpaul
279164712Smarius	/*
280164712Smarius	 * At least Am79C971 with DP83840A wedge when isolating the
281164712Smarius	 * external PHY so we can't allow multiple external PHYs.
282165997Smarius	 * There are cards that use Am79C971 with both the internal
283165997Smarius	 * and an external PHY though.
284164712Smarius	 * For internal PHYs it doesn't really matter whether we can
285164712Smarius	 * isolate the remaining internal and the external ones in
286164712Smarius	 * the PHY drivers as the internal PHYs have to be enabled
287164712Smarius	 * individually in PCN_BCR_PHYSEL, PCN_CSR_MODE, etc.
288165997Smarius	 * With Am79C97{3,5,8} we don't support switching beetween
289165997Smarius	 * the internal and external PHYs, yet, so we can't allow
290165997Smarius	 * multiple PHYs with these either.
291165997Smarius	 * Am79C97{2,6} actually only support external PHYs (not
292165997Smarius	 * connectable internal ones respond at the usual addresses,
293165997Smarius	 * which don't hurt if we let them show up on the bus) and
294165997Smarius	 * isolating them works.
295164712Smarius	 */
296165997Smarius	if (((sc->pcn_type == Am79C971 && phy != PCN_PHYAD_10BT) ||
297165997Smarius	    sc->pcn_type == Am79C973 || sc->pcn_type == Am79C975 ||
298165997Smarius	    sc->pcn_type == Am79C978) && sc->pcn_extphyaddr != -1 &&
299164712Smarius	    phy != sc->pcn_extphyaddr)
30066131Swpaul		return(0);
30166131Swpaul
30266131Swpaul	pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5));
30366131Swpaul	val = pcn_bcr_read(sc, PCN_BCR_MIIDATA) & 0xFFFF;
30466131Swpaul	if (val == 0xFFFF)
30566131Swpaul		return(0);
30666208Swpaul
307165997Smarius	if (((sc->pcn_type == Am79C971 && phy != PCN_PHYAD_10BT) ||
308165997Smarius	    sc->pcn_type == Am79C973 || sc->pcn_type == Am79C975 ||
309165997Smarius	    sc->pcn_type == Am79C978) && sc->pcn_extphyaddr == -1)
310164712Smarius		sc->pcn_extphyaddr = phy;
31166208Swpaul
31266131Swpaul	return(val);
31366131Swpaul}
31466131Swpaul
315102335Salfredstatic int
316102335Salfredpcn_miibus_writereg(dev, phy, reg, data)
31766131Swpaul	device_t		dev;
31866131Swpaul	int			phy, reg, data;
31966131Swpaul{
32066131Swpaul	struct pcn_softc	*sc;
32166131Swpaul
32266131Swpaul	sc = device_get_softc(dev);
32366131Swpaul
32466131Swpaul	pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5));
32566131Swpaul	pcn_bcr_write(sc, PCN_BCR_MIIDATA, data);
32666131Swpaul
32766131Swpaul	return(0);
32866131Swpaul}
32966131Swpaul
330102335Salfredstatic void
331102335Salfredpcn_miibus_statchg(dev)
33266131Swpaul	device_t		dev;
33366131Swpaul{
33466131Swpaul	struct pcn_softc	*sc;
33566131Swpaul	struct mii_data		*mii;
33666131Swpaul
33766131Swpaul	sc = device_get_softc(dev);
33866131Swpaul	mii = device_get_softc(sc->pcn_miibus);
33966131Swpaul
34066131Swpaul	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
34166131Swpaul		PCN_BCR_SETBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN);
34266131Swpaul	} else {
34366131Swpaul		PCN_BCR_CLRBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN);
34466131Swpaul	}
34566131Swpaul
34666131Swpaul	return;
34766131Swpaul}
34866131Swpaul
349102335Salfredstatic void
350102335Salfredpcn_setmulti(sc)
35166131Swpaul	struct pcn_softc	*sc;
35266131Swpaul{
35366131Swpaul	struct ifnet		*ifp;
35466131Swpaul	struct ifmultiaddr	*ifma;
35566131Swpaul	u_int32_t		h, i;
35666131Swpaul	u_int16_t		hashes[4] = { 0, 0, 0, 0 };
35766131Swpaul
358147256Sbrooks	ifp = sc->pcn_ifp;
35966131Swpaul
36066131Swpaul	PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
36166131Swpaul
36266131Swpaul	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
36366131Swpaul		for (i = 0; i < 4; i++)
36466131Swpaul			pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0xFFFF);
36566131Swpaul		PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
36666131Swpaul		return;
36766131Swpaul	}
36866131Swpaul
36966131Swpaul	/* first, zot all the existing hash bits */
37066131Swpaul	for (i = 0; i < 4; i++)
37166131Swpaul		pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0);
37266131Swpaul
37366131Swpaul	/* now program new ones */
374148654Srwatson	IF_ADDR_LOCK(ifp);
37572084Sphk	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
37666131Swpaul		if (ifma->ifma_addr->sa_family != AF_LINK)
37766131Swpaul			continue;
378130270Snaddy		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
379130270Snaddy		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
38066131Swpaul		hashes[h >> 4] |= 1 << (h & 0xF);
38166131Swpaul	}
382148654Srwatson	IF_ADDR_UNLOCK(ifp);
38366131Swpaul
38466131Swpaul	for (i = 0; i < 4; i++)
38566131Swpaul		pcn_csr_write(sc, PCN_CSR_MAR0 + i, hashes[i]);
38666131Swpaul
38766131Swpaul	PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
38866131Swpaul
38966131Swpaul	return;
39066131Swpaul}
39166131Swpaul
392102335Salfredstatic void
393102335Salfredpcn_reset(sc)
39466131Swpaul	struct pcn_softc	*sc;
39566131Swpaul{
39666131Swpaul	/*
39766131Swpaul	 * Issue a reset by reading from the RESET register.
39866131Swpaul	 * Note that we don't know if the chip is operating in
39966131Swpaul	 * 16-bit or 32-bit mode at this point, so we attempt
40066131Swpaul	 * to reset the chip both ways. If one fails, the other
40166131Swpaul	 * will succeed.
40266131Swpaul	 */
40366131Swpaul	CSR_READ_2(sc, PCN_IO16_RESET);
40466131Swpaul	CSR_READ_4(sc, PCN_IO32_RESET);
40566131Swpaul
40666131Swpaul	/* Wait a little while for the chip to get its brains in order. */
40766131Swpaul	DELAY(1000);
40866131Swpaul
40966131Swpaul	/* Select 32-bit (DWIO) mode */
41066131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RDP, 0);
41166131Swpaul
41266131Swpaul	/* Select software style 3. */
41366131Swpaul	pcn_bcr_write(sc, PCN_BCR_SSTYLE, PCN_SWSTYLE_PCNETPCI_BURST);
41466131Swpaul
41566131Swpaul        return;
41666131Swpaul}
41766131Swpaul
418164072Smariusstatic const char *
419164072Smariuspcn_chipid_name(u_int32_t id)
420138354Smdodd{
421164072Smarius	const struct pcn_chipid *p;
422138354Smdodd
423164072Smarius	p = pcn_chipid;
424138354Smdodd	while (p->name) {
425138354Smdodd		if (id == p->id)
426138354Smdodd			return (p->name);
427138354Smdodd		p++;
428138354Smdodd	}
429138354Smdodd	return ("Unknown");
430138354Smdodd}
431138354Smdodd
432138354Smdoddstatic u_int32_t
433164072Smariuspcn_chip_id(device_t dev)
434138354Smdodd{
435138354Smdodd	struct pcn_softc	*sc;
436138354Smdodd	u_int32_t		chip_id;
437138354Smdodd
438138354Smdodd	sc = device_get_softc(dev);
439138354Smdodd	/*
440138354Smdodd	 * Note: we can *NOT* put the chip into
441158614Smarius	 * 32-bit mode yet. The le(4) driver will only
442138354Smdodd	 * work in 16-bit mode, and once the chip
443138354Smdodd	 * goes into 32-bit mode, the only way to
444138354Smdodd	 * get it out again is with a hardware reset.
445138354Smdodd	 * So if pcn_probe() is called before the
446158614Smarius	 * le(4) driver's probe routine, the chip will
447158614Smarius	 * be locked into 32-bit operation and the
448158614Smarius	 * le(4) driver will be unable to attach to it.
449138354Smdodd	 * Note II: if the chip happens to already
450138354Smdodd	 * be in 32-bit mode, we still need to check
451138354Smdodd	 * the chip ID, but first we have to detect
452138354Smdodd	 * 32-bit mode using only 16-bit operations.
453138354Smdodd	 * The safest way to do this is to read the
454138354Smdodd	 * PCI subsystem ID from BCR23/24 and compare
455138354Smdodd	 * that with the value read from PCI config
456138354Smdodd	 * space.
457138354Smdodd	 */
458138354Smdodd	chip_id = pcn_bcr_read16(sc, PCN_BCR_PCISUBSYSID);
459138354Smdodd	chip_id <<= 16;
460138354Smdodd	chip_id |= pcn_bcr_read16(sc, PCN_BCR_PCISUBVENID);
461138354Smdodd	/*
462138354Smdodd	 * Note III: the test for 0x10001000 is a hack to
463138354Smdodd	 * pacify VMware, who's pseudo-PCnet interface is
464138354Smdodd	 * broken. Reading the subsystem register from PCI
465138354Smdodd	 * config space yields 0x00000000 while reading the
466138354Smdodd	 * same value from I/O space yields 0x10001000. It's
467138354Smdodd	 * not supposed to be that way.
468138354Smdodd	 */
469138354Smdodd	if (chip_id == pci_read_config(dev,
470138354Smdodd	    PCIR_SUBVEND_0, 4) || chip_id == 0x10001000) {
471138354Smdodd		/* We're in 16-bit mode. */
472138354Smdodd		chip_id = pcn_csr_read16(sc, PCN_CSR_CHIPID1);
473138354Smdodd		chip_id <<= 16;
474138354Smdodd		chip_id |= pcn_csr_read16(sc, PCN_CSR_CHIPID0);
475138354Smdodd	} else {
476138354Smdodd		/* We're in 32-bit mode. */
477138354Smdodd		chip_id = pcn_csr_read(sc, PCN_CSR_CHIPID1);
478138354Smdodd		chip_id <<= 16;
479138354Smdodd		chip_id |= pcn_csr_read(sc, PCN_CSR_CHIPID0);
480138354Smdodd	}
481138354Smdodd
482138354Smdodd	return (chip_id);
483138354Smdodd}
484138354Smdodd
485164072Smariusstatic const struct pcn_type *
486164072Smariuspcn_match(u_int16_t vid, u_int16_t did)
487138355Smdodd{
488164072Smarius	const struct pcn_type	*t;
489164072Smarius
490138355Smdodd	t = pcn_devs;
491164072Smarius	while (t->pcn_name != NULL) {
492138355Smdodd		if ((vid == t->pcn_vid) && (did == t->pcn_did))
493138355Smdodd			return (t);
494138355Smdodd		t++;
495138355Smdodd	}
496138355Smdodd	return (NULL);
497138355Smdodd}
498138355Smdodd
49966131Swpaul/*
50066131Swpaul * Probe for an AMD chip. Check the PCI vendor and device
50166131Swpaul * IDs against our list and return a device name if we find a match.
50266131Swpaul */
503102335Salfredstatic int
504102335Salfredpcn_probe(dev)
50566131Swpaul	device_t		dev;
50666131Swpaul{
507164072Smarius	const struct pcn_type	*t;
50866131Swpaul	struct pcn_softc	*sc;
50966131Swpaul	int			rid;
51066131Swpaul	u_int32_t		chip_id;
51166131Swpaul
512138355Smdodd	t = pcn_match(pci_get_vendor(dev), pci_get_device(dev));
513138355Smdodd	if (t == NULL)
514138355Smdodd		return (ENXIO);
51566131Swpaul	sc = device_get_softc(dev);
51666131Swpaul
517138355Smdodd	/*
518138355Smdodd	 * Temporarily map the I/O space so we can read the chip ID register.
519138355Smdodd	 */
520138355Smdodd	rid = PCN_RID;
521138355Smdodd	sc->pcn_res = bus_alloc_resource_any(dev, PCN_RES, &rid, RF_ACTIVE);
522138355Smdodd	if (sc->pcn_res == NULL) {
523138355Smdodd		device_printf(dev, "couldn't map ports/memory\n");
524138355Smdodd		return(ENXIO);
52566131Swpaul	}
526138355Smdodd	sc->pcn_btag = rman_get_bustag(sc->pcn_res);
527138355Smdodd	sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
52866131Swpaul
529138355Smdodd	chip_id = pcn_chip_id(dev);
530138355Smdodd
531138355Smdodd	bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
532138355Smdodd
533138355Smdodd	switch((chip_id >> 12) & PART_MASK) {
534138355Smdodd	case Am79C971:
535138355Smdodd	case Am79C972:
536138355Smdodd	case Am79C973:
537138355Smdodd	case Am79C975:
538138355Smdodd	case Am79C976:
539138355Smdodd	case Am79C978:
540138355Smdodd		break;
541138355Smdodd	default:
542138355Smdodd		return(ENXIO);
543138355Smdodd	}
544138355Smdodd	device_set_desc(dev, t->pcn_name);
545142398Simp	return(BUS_PROBE_DEFAULT);
54666131Swpaul}
54766131Swpaul
54866131Swpaul/*
54966131Swpaul * Attach the interface. Allocate softc structures, do ifmedia
55066131Swpaul * setup and ethernet/BPF attach.
55166131Swpaul */
552102335Salfredstatic int
553102335Salfredpcn_attach(dev)
55466131Swpaul	device_t		dev;
55566131Swpaul{
55666131Swpaul	u_int32_t		eaddr[2];
55766131Swpaul	struct pcn_softc	*sc;
558164712Smarius	struct mii_data		*mii;
559164712Smarius	struct mii_softc	*miisc;
56066131Swpaul	struct ifnet		*ifp;
561164072Smarius	int			error = 0, rid;
56266131Swpaul
56366131Swpaul	sc = device_get_softc(dev);
56466131Swpaul
56569583Swpaul	/* Initialize our mutex. */
56693818Sjhb	mtx_init(&sc->pcn_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
567148738Sjhb	    MTX_DEF);
56866131Swpaul	/*
56966131Swpaul	 * Map control/status registers.
57066131Swpaul	 */
57172813Swpaul	pci_enable_busmaster(dev);
57266131Swpaul
573138354Smdodd	/* Retrieve the chip ID */
574138354Smdodd	sc->pcn_type = (pcn_chip_id(dev) >> 12) & PART_MASK;
575138354Smdodd	device_printf(dev, "Chip ID %04x (%s)\n",
576138354Smdodd		sc->pcn_type, pcn_chipid_name(sc->pcn_type));
577138354Smdodd
57866131Swpaul	rid = PCN_RID;
579127135Snjl	sc->pcn_res = bus_alloc_resource_any(dev, PCN_RES, &rid, RF_ACTIVE);
58066131Swpaul
58166131Swpaul	if (sc->pcn_res == NULL) {
582164072Smarius		device_printf(dev, "couldn't map ports/memory\n");
58366131Swpaul		error = ENXIO;
58466131Swpaul		goto fail;
58566131Swpaul	}
58666131Swpaul
58766131Swpaul	sc->pcn_btag = rman_get_bustag(sc->pcn_res);
58866131Swpaul	sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
58966131Swpaul
59066131Swpaul	/* Allocate interrupt */
59166131Swpaul	rid = 0;
592127135Snjl	sc->pcn_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
59366131Swpaul	    RF_SHAREABLE | RF_ACTIVE);
59466131Swpaul
59566131Swpaul	if (sc->pcn_irq == NULL) {
596164072Smarius		device_printf(dev, "couldn't map interrupt\n");
59766131Swpaul		error = ENXIO;
59866131Swpaul		goto fail;
59966131Swpaul	}
60066131Swpaul
60166131Swpaul	/* Reset the adapter. */
60266131Swpaul	pcn_reset(sc);
60366131Swpaul
60466131Swpaul	/*
60566131Swpaul	 * Get station address from the EEPROM.
60666131Swpaul	 */
60766131Swpaul	eaddr[0] = CSR_READ_4(sc, PCN_IO32_APROM00);
60866131Swpaul	eaddr[1] = CSR_READ_4(sc, PCN_IO32_APROM01);
60966131Swpaul
610149204Sjhb	callout_init_mtx(&sc->pcn_stat_callout, &sc->pcn_mtx, 0);
61166131Swpaul
61266131Swpaul	sc->pcn_ldata = contigmalloc(sizeof(struct pcn_list_data), M_DEVBUF,
61366131Swpaul	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
61466131Swpaul
61566131Swpaul	if (sc->pcn_ldata == NULL) {
616164072Smarius		device_printf(dev, "no memory for list buffers!\n");
61766131Swpaul		error = ENXIO;
61866131Swpaul		goto fail;
61966131Swpaul	}
62066131Swpaul	bzero(sc->pcn_ldata, sizeof(struct pcn_list_data));
62166131Swpaul
622147256Sbrooks	ifp = sc->pcn_ifp = if_alloc(IFT_ETHER);
623147256Sbrooks	if (ifp == NULL) {
624164072Smarius		device_printf(dev, "can not if_alloc()\n");
625147256Sbrooks		error = ENOSPC;
626147256Sbrooks		goto fail;
627147256Sbrooks	}
62866131Swpaul	ifp->if_softc = sc;
629121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
630148738Sjhb	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
63166131Swpaul	ifp->if_ioctl = pcn_ioctl;
63266131Swpaul	ifp->if_start = pcn_start;
63366131Swpaul	ifp->if_watchdog = pcn_watchdog;
63466131Swpaul	ifp->if_init = pcn_init;
63566131Swpaul	ifp->if_snd.ifq_maxlen = PCN_TX_LIST_CNT - 1;
63666131Swpaul
63766131Swpaul	/*
63866131Swpaul	 * Do MII setup.
63966131Swpaul	 */
640164712Smarius	sc->pcn_extphyaddr = -1;
64166131Swpaul	if (mii_phy_probe(dev, &sc->pcn_miibus,
64266131Swpaul	    pcn_ifmedia_upd, pcn_ifmedia_sts)) {
643164072Smarius		device_printf(dev, "MII without any PHY!\n");
64466131Swpaul		error = ENXIO;
64566131Swpaul		goto fail;
64666131Swpaul	}
647164712Smarius	/*
648164712Smarius	 * Record the media instances of internal PHYs, which map the
649164712Smarius	 * built-in interfaces to the MII, so we can set the active
650164712Smarius	 * PHY/port based on the currently selected media.
651164712Smarius	 */
652164712Smarius	sc->pcn_inst_10bt = -1;
653164712Smarius	mii = device_get_softc(sc->pcn_miibus);
654164712Smarius	LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
655164712Smarius		switch (miisc->mii_phy) {
656164712Smarius		case PCN_PHYAD_10BT:
657164712Smarius			sc->pcn_inst_10bt = miisc->mii_inst;
658164712Smarius			break;
659164712Smarius		/*
660164712Smarius		 * XXX deal with the Am79C97{3,5} internal 100baseT
661164712Smarius		 * and the Am79C978 internal HomePNA PHYs.
662164712Smarius		 */
663164712Smarius		}
664164712Smarius	}
66566131Swpaul
66666131Swpaul	/*
66766131Swpaul	 * Call MI attach routine.
66866131Swpaul	 */
669106936Ssam	ether_ifattach(ifp, (u_int8_t *) eaddr);
67066131Swpaul
671113609Snjl	/* Hook interrupt last to avoid having to lock softc */
672148738Sjhb	error = bus_setup_intr(dev, sc->pcn_irq, INTR_TYPE_NET | INTR_MPSAFE,
673166901Spiso	    NULL, pcn_intr, sc, &sc->pcn_intrhand);
674110472Smdodd
675112872Snjl	if (error) {
676164072Smarius		device_printf(dev, "couldn't set up irq\n");
677113609Snjl		ether_ifdetach(ifp);
678112872Snjl		goto fail;
679112872Snjl	}
680110472Smdodd
681112872Snjlfail:
682112872Snjl	if (error)
683112872Snjl		pcn_detach(dev);
68467087Swpaul
68566131Swpaul	return(error);
68666131Swpaul}
68766131Swpaul
688113609Snjl/*
689113609Snjl * Shutdown hardware and free up resources. This can be called any
690113609Snjl * time after the mutex has been initialized. It is called in both
691113609Snjl * the error case in attach and the normal detach case so it needs
692113609Snjl * to be careful about only freeing resources that have actually been
693113609Snjl * allocated.
694113609Snjl */
695102335Salfredstatic int
696102335Salfredpcn_detach(dev)
69766131Swpaul	device_t		dev;
69866131Swpaul{
69966131Swpaul	struct pcn_softc	*sc;
70066131Swpaul	struct ifnet		*ifp;
70166131Swpaul
70266131Swpaul	sc = device_get_softc(dev);
703147256Sbrooks	ifp = sc->pcn_ifp;
70466131Swpaul
705112880Sjhb	KASSERT(mtx_initialized(&sc->pcn_mtx), ("pcn mutex not initialized"));
70667087Swpaul
707113609Snjl	/* These should only be active if attach succeeded */
708113812Simp	if (device_is_attached(dev)) {
709148738Sjhb		PCN_LOCK(sc);
710113609Snjl		pcn_reset(sc);
711113609Snjl		pcn_stop(sc);
712148738Sjhb		PCN_UNLOCK(sc);
713148738Sjhb		callout_drain(&sc->pcn_stat_callout);
714112872Snjl		ether_ifdetach(ifp);
715150213Sru	}
716113609Snjl	if (sc->pcn_miibus)
717112872Snjl		device_delete_child(dev, sc->pcn_miibus);
718113609Snjl	bus_generic_detach(dev);
71966131Swpaul
720112872Snjl	if (sc->pcn_intrhand)
721112872Snjl		bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
722112872Snjl	if (sc->pcn_irq)
723112872Snjl		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
724112872Snjl	if (sc->pcn_res)
725112872Snjl		bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
72666131Swpaul
727151297Sru	if (ifp)
728151297Sru		if_free(ifp);
729151297Sru
730112872Snjl	if (sc->pcn_ldata) {
731112872Snjl		contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data),
732112872Snjl		    M_DEVBUF);
733112872Snjl	}
73466131Swpaul
73567087Swpaul	mtx_destroy(&sc->pcn_mtx);
73666131Swpaul
73766131Swpaul	return(0);
73866131Swpaul}
73966131Swpaul
74066131Swpaul/*
74166131Swpaul * Initialize the transmit descriptors.
74266131Swpaul */
743102335Salfredstatic int
744102335Salfredpcn_list_tx_init(sc)
74566131Swpaul	struct pcn_softc	*sc;
74666131Swpaul{
74766131Swpaul	struct pcn_list_data	*ld;
74866131Swpaul	struct pcn_ring_data	*cd;
74966131Swpaul	int			i;
75066131Swpaul
75166131Swpaul	cd = &sc->pcn_cdata;
75266131Swpaul	ld = sc->pcn_ldata;
75366131Swpaul
75466131Swpaul	for (i = 0; i < PCN_TX_LIST_CNT; i++) {
75566131Swpaul		cd->pcn_tx_chain[i] = NULL;
75666131Swpaul		ld->pcn_tx_list[i].pcn_tbaddr = 0;
75766131Swpaul		ld->pcn_tx_list[i].pcn_txctl = 0;
75866131Swpaul		ld->pcn_tx_list[i].pcn_txstat = 0;
75966131Swpaul	}
76066131Swpaul
76166131Swpaul	cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0;
76266131Swpaul
76366131Swpaul	return(0);
76466131Swpaul}
76566131Swpaul
76666131Swpaul
76766131Swpaul/*
76866131Swpaul * Initialize the RX descriptors and allocate mbufs for them.
76966131Swpaul */
770102335Salfredstatic int
771102335Salfredpcn_list_rx_init(sc)
77266131Swpaul	struct pcn_softc	*sc;
77366131Swpaul{
77466131Swpaul	struct pcn_ring_data	*cd;
77566131Swpaul	int			i;
77666131Swpaul
77766131Swpaul	cd = &sc->pcn_cdata;
77866131Swpaul
77966131Swpaul	for (i = 0; i < PCN_RX_LIST_CNT; i++) {
78066131Swpaul		if (pcn_newbuf(sc, i, NULL) == ENOBUFS)
78166131Swpaul			return(ENOBUFS);
78266131Swpaul	}
78366131Swpaul
78466131Swpaul	cd->pcn_rx_prod = 0;
78566131Swpaul
78666131Swpaul	return(0);
78766131Swpaul}
78866131Swpaul
78966131Swpaul/*
79066131Swpaul * Initialize an RX descriptor and attach an MBUF cluster.
79166131Swpaul */
792102335Salfredstatic int
793102335Salfredpcn_newbuf(sc, idx, m)
79466131Swpaul	struct pcn_softc	*sc;
79566131Swpaul	int			idx;
79666131Swpaul	struct mbuf		*m;
79766131Swpaul{
79866131Swpaul	struct mbuf		*m_new = NULL;
79966131Swpaul	struct pcn_rx_desc	*c;
80066131Swpaul
80166131Swpaul	c = &sc->pcn_ldata->pcn_rx_list[idx];
80266131Swpaul
80366131Swpaul	if (m == NULL) {
804111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
80587846Sluigi		if (m_new == NULL)
80666131Swpaul			return(ENOBUFS);
80766131Swpaul
808111119Simp		MCLGET(m_new, M_DONTWAIT);
80966131Swpaul		if (!(m_new->m_flags & M_EXT)) {
81066131Swpaul			m_freem(m_new);
81166131Swpaul			return(ENOBUFS);
81266131Swpaul		}
81366131Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
81466131Swpaul	} else {
81566131Swpaul		m_new = m;
81666131Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
81766131Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
81866131Swpaul	}
81966131Swpaul
82066131Swpaul	m_adj(m_new, ETHER_ALIGN);
82166131Swpaul
82266131Swpaul	sc->pcn_cdata.pcn_rx_chain[idx] = m_new;
82366131Swpaul	c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t));
82466131Swpaul	c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ;
82566131Swpaul	c->pcn_bufsz |= PCN_RXLEN_MBO;
82666131Swpaul	c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN;
82766131Swpaul
82866131Swpaul	return(0);
82966131Swpaul}
83066131Swpaul
83166131Swpaul/*
83266131Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to
83366131Swpaul * the higher level protocols.
83466131Swpaul */
835102335Salfredstatic void
836102335Salfredpcn_rxeof(sc)
83766131Swpaul	struct pcn_softc	*sc;
83866131Swpaul{
83966131Swpaul        struct mbuf		*m;
84066131Swpaul        struct ifnet		*ifp;
84166131Swpaul	struct pcn_rx_desc	*cur_rx;
84266131Swpaul	int			i;
84366131Swpaul
844122689Ssam	PCN_LOCK_ASSERT(sc);
845122689Ssam
846147256Sbrooks	ifp = sc->pcn_ifp;
84766131Swpaul	i = sc->pcn_cdata.pcn_rx_prod;
84866131Swpaul
84966131Swpaul	while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) {
85066131Swpaul		cur_rx = &sc->pcn_ldata->pcn_rx_list[i];
85166131Swpaul		m = sc->pcn_cdata.pcn_rx_chain[i];
85266131Swpaul		sc->pcn_cdata.pcn_rx_chain[i] = NULL;
85366131Swpaul
85466131Swpaul		/*
85566131Swpaul		 * If an error occurs, update stats, clear the
85666131Swpaul		 * status word and leave the mbuf cluster in place:
85766131Swpaul		 * it should simply get re-used next time this descriptor
85866131Swpaul	 	 * comes up in the ring.
85966131Swpaul		 */
86066131Swpaul		if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) {
86166131Swpaul			ifp->if_ierrors++;
86266131Swpaul			pcn_newbuf(sc, i, m);
86366131Swpaul			PCN_INC(i, PCN_RX_LIST_CNT);
86466131Swpaul			continue;
86566131Swpaul		}
86666131Swpaul
86766592Swpaul		if (pcn_newbuf(sc, i, NULL)) {
86866592Swpaul			/* Ran out of mbufs; recycle this one. */
86966592Swpaul			pcn_newbuf(sc, i, m);
87066592Swpaul			ifp->if_ierrors++;
87166592Swpaul			PCN_INC(i, PCN_RX_LIST_CNT);
87266592Swpaul			continue;
87366592Swpaul		}
87466592Swpaul
87566131Swpaul		PCN_INC(i, PCN_RX_LIST_CNT);
87666131Swpaul
87766131Swpaul		/* No errors; receive the packet. */
87866131Swpaul		ifp->if_ipackets++;
87966131Swpaul		m->m_len = m->m_pkthdr.len =
88066131Swpaul		    cur_rx->pcn_rxlen - ETHER_CRC_LEN;
88166131Swpaul		m->m_pkthdr.rcvif = ifp;
88266131Swpaul
883122689Ssam		PCN_UNLOCK(sc);
884106936Ssam		(*ifp->if_input)(ifp, m);
885122689Ssam		PCN_LOCK(sc);
88666131Swpaul	}
88766131Swpaul
88866131Swpaul	sc->pcn_cdata.pcn_rx_prod = i;
88966131Swpaul
89066131Swpaul	return;
89166131Swpaul}
89266131Swpaul
89366131Swpaul/*
89466131Swpaul * A frame was downloaded to the chip. It's safe for us to clean up
89566131Swpaul * the list buffers.
89666131Swpaul */
89766131Swpaul
898102335Salfredstatic void
899102335Salfredpcn_txeof(sc)
90066131Swpaul	struct pcn_softc	*sc;
90166131Swpaul{
90266131Swpaul	struct pcn_tx_desc	*cur_tx = NULL;
90366131Swpaul	struct ifnet		*ifp;
90466131Swpaul	u_int32_t		idx;
90566131Swpaul
906147256Sbrooks	ifp = sc->pcn_ifp;
90766131Swpaul
90866131Swpaul	/*
90966131Swpaul	 * Go through our tx list and free mbufs for those
91066131Swpaul	 * frames that have been transmitted.
91166131Swpaul	 */
91266131Swpaul	idx = sc->pcn_cdata.pcn_tx_cons;
91366131Swpaul	while (idx != sc->pcn_cdata.pcn_tx_prod) {
91466131Swpaul		cur_tx = &sc->pcn_ldata->pcn_tx_list[idx];
91566131Swpaul
91666131Swpaul		if (!PCN_OWN_TXDESC(cur_tx))
91766131Swpaul			break;
91866131Swpaul
91966131Swpaul		if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) {
92066131Swpaul			sc->pcn_cdata.pcn_tx_cnt--;
92166131Swpaul			PCN_INC(idx, PCN_TX_LIST_CNT);
92266131Swpaul			continue;
92366131Swpaul		}
92466131Swpaul
92566131Swpaul		if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) {
92666131Swpaul			ifp->if_oerrors++;
92766131Swpaul			if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF)
92866131Swpaul				ifp->if_collisions++;
92966131Swpaul			if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY)
93066131Swpaul				ifp->if_collisions++;
93166131Swpaul		}
93266131Swpaul
93366131Swpaul		ifp->if_collisions +=
93466131Swpaul		    cur_tx->pcn_txstat & PCN_TXSTAT_TRC;
93566131Swpaul
93666131Swpaul		ifp->if_opackets++;
93766131Swpaul		if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) {
93866131Swpaul			m_freem(sc->pcn_cdata.pcn_tx_chain[idx]);
93966131Swpaul			sc->pcn_cdata.pcn_tx_chain[idx] = NULL;
94066131Swpaul		}
94166131Swpaul
94266131Swpaul		sc->pcn_cdata.pcn_tx_cnt--;
94366131Swpaul		PCN_INC(idx, PCN_TX_LIST_CNT);
94466131Swpaul	}
94566131Swpaul
94699165Sluigi	if (idx != sc->pcn_cdata.pcn_tx_cons) {
94799165Sluigi		/* Some buffers have been freed. */
94899165Sluigi		sc->pcn_cdata.pcn_tx_cons = idx;
949148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
95099165Sluigi	}
95199165Sluigi	ifp->if_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5;
95266131Swpaul
95366131Swpaul	return;
95466131Swpaul}
95566131Swpaul
956102335Salfredstatic void
957102335Salfredpcn_tick(xsc)
95866131Swpaul	void			*xsc;
95966131Swpaul{
96066131Swpaul	struct pcn_softc	*sc;
96166131Swpaul	struct mii_data		*mii;
96266131Swpaul	struct ifnet		*ifp;
96366131Swpaul
96466131Swpaul	sc = xsc;
965147256Sbrooks	ifp = sc->pcn_ifp;
966149204Sjhb	PCN_LOCK_ASSERT(sc);
96766131Swpaul
96866131Swpaul	mii = device_get_softc(sc->pcn_miibus);
96966131Swpaul	mii_tick(mii);
97066131Swpaul
97184147Sjlemon	/* link just died */
97266131Swpaul	if (sc->pcn_link & !(mii->mii_media_status & IFM_ACTIVE))
97366131Swpaul		sc->pcn_link = 0;
97466131Swpaul
97584147Sjlemon	/* link just came up, restart */
97684147Sjlemon	if (!sc->pcn_link && mii->mii_media_status & IFM_ACTIVE &&
97784147Sjlemon	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
97884147Sjlemon		sc->pcn_link++;
97984147Sjlemon		if (ifp->if_snd.ifq_head != NULL)
980148738Sjhb			pcn_start_locked(ifp);
98166131Swpaul	}
98266131Swpaul
983148738Sjhb	callout_reset(&sc->pcn_stat_callout, hz, pcn_tick, sc);
98466131Swpaul
98566131Swpaul	return;
98666131Swpaul}
98766131Swpaul
988102335Salfredstatic void
989102335Salfredpcn_intr(arg)
99066131Swpaul	void			*arg;
99166131Swpaul{
99266131Swpaul	struct pcn_softc	*sc;
99366131Swpaul	struct ifnet		*ifp;
99466131Swpaul	u_int32_t		status;
99566131Swpaul
99666131Swpaul	sc = arg;
997147256Sbrooks	ifp = sc->pcn_ifp;
99866131Swpaul
999148738Sjhb	PCN_LOCK(sc);
1000148738Sjhb
1001134842Sbrueffer	/* Suppress unwanted interrupts */
100266131Swpaul	if (!(ifp->if_flags & IFF_UP)) {
100366131Swpaul		pcn_stop(sc);
1004148738Sjhb		PCN_UNLOCK(sc);
100566131Swpaul		return;
100666131Swpaul	}
100766131Swpaul
100866131Swpaul	CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR);
100966131Swpaul
101066131Swpaul	while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) {
101166131Swpaul		CSR_WRITE_4(sc, PCN_IO32_RDP, status);
101266131Swpaul
101366131Swpaul		if (status & PCN_CSR_RINT)
101466131Swpaul			pcn_rxeof(sc);
101566131Swpaul
101666131Swpaul		if (status & PCN_CSR_TINT)
101766131Swpaul			pcn_txeof(sc);
101866131Swpaul
101966131Swpaul		if (status & PCN_CSR_ERR) {
1020148738Sjhb			pcn_init_locked(sc);
102166131Swpaul			break;
102266131Swpaul		}
102366131Swpaul	}
102466131Swpaul
102566131Swpaul	if (ifp->if_snd.ifq_head != NULL)
1026148738Sjhb		pcn_start_locked(ifp);
102766131Swpaul
1028110567Smdodd	PCN_UNLOCK(sc);
102966131Swpaul	return;
103066131Swpaul}
103166131Swpaul
103266131Swpaul/*
103366131Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
103466131Swpaul * pointers to the fragment pointers.
103566131Swpaul */
1036102335Salfredstatic int
1037102335Salfredpcn_encap(sc, m_head, txidx)
103866131Swpaul	struct pcn_softc	*sc;
103966131Swpaul	struct mbuf		*m_head;
104066131Swpaul	u_int32_t		*txidx;
104166131Swpaul{
104266131Swpaul	struct pcn_tx_desc	*f = NULL;
104366131Swpaul	struct mbuf		*m;
104466131Swpaul	int			frag, cur, cnt = 0;
104566131Swpaul
104666131Swpaul	/*
104766131Swpaul 	 * Start packing the mbufs in this chain into
104866131Swpaul	 * the fragment pointers. Stop when we run out
104966131Swpaul 	 * of fragments or hit the end of the mbuf chain.
105066131Swpaul	 */
105166131Swpaul	m = m_head;
105266131Swpaul	cur = frag = *txidx;
105366131Swpaul
105466131Swpaul	for (m = m_head; m != NULL; m = m->m_next) {
1055144981Smdodd		if (m->m_len == 0)
1056144981Smdodd			continue;
1057144981Smdodd
1058144981Smdodd		if ((PCN_TX_LIST_CNT - (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2)
1059144981Smdodd			return(ENOBUFS);
1060144981Smdodd		f = &sc->pcn_ldata->pcn_tx_list[frag];
1061144981Smdodd		f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ;
1062144981Smdodd		f->pcn_txctl |= PCN_TXCTL_MBO;
1063144981Smdodd		f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t));
1064144981Smdodd		if (cnt == 0)
1065144981Smdodd			f->pcn_txctl |= PCN_TXCTL_STP;
1066144981Smdodd		else
1067144981Smdodd			f->pcn_txctl |= PCN_TXCTL_OWN;
1068144981Smdodd		cur = frag;
1069144981Smdodd		PCN_INC(frag, PCN_TX_LIST_CNT);
1070144981Smdodd		cnt++;
107166131Swpaul	}
107266131Swpaul
107366131Swpaul	if (m != NULL)
107466131Swpaul		return(ENOBUFS);
107566131Swpaul
107666131Swpaul	sc->pcn_cdata.pcn_tx_chain[cur] = m_head;
107766131Swpaul	sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |=
107866131Swpaul	    PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT;
107966131Swpaul	sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN;
108066131Swpaul	sc->pcn_cdata.pcn_tx_cnt += cnt;
108166131Swpaul	*txidx = frag;
108266131Swpaul
108366131Swpaul	return(0);
108466131Swpaul}
108566131Swpaul
108666131Swpaul/*
108766131Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
108866131Swpaul * to the mbuf data regions directly in the transmit lists. We also save a
108966131Swpaul * copy of the pointers since the transmit list fragment pointers are
109066131Swpaul * physical addresses.
109166131Swpaul */
1092102335Salfredstatic void
1093102335Salfredpcn_start(ifp)
109466131Swpaul	struct ifnet		*ifp;
109566131Swpaul{
109666131Swpaul	struct pcn_softc	*sc;
1097148738Sjhb
1098148738Sjhb	sc = ifp->if_softc;
1099148738Sjhb	PCN_LOCK(sc);
1100148738Sjhb	pcn_start_locked(ifp);
1101148738Sjhb	PCN_UNLOCK(sc);
1102148738Sjhb}
1103148738Sjhb
1104148738Sjhbstatic void
1105148738Sjhbpcn_start_locked(ifp)
1106148738Sjhb	struct ifnet		*ifp;
1107148738Sjhb{
1108148738Sjhb	struct pcn_softc	*sc;
110966131Swpaul	struct mbuf		*m_head = NULL;
111066131Swpaul	u_int32_t		idx;
111166131Swpaul
111266131Swpaul	sc = ifp->if_softc;
111366131Swpaul
1114148738Sjhb	PCN_LOCK_ASSERT(sc);
111567087Swpaul
1116148738Sjhb	if (!sc->pcn_link)
111766131Swpaul		return;
111866131Swpaul
111966131Swpaul	idx = sc->pcn_cdata.pcn_tx_prod;
112066131Swpaul
1121148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
112266131Swpaul		return;
112366131Swpaul
112466131Swpaul	while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) {
112566131Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
112666131Swpaul		if (m_head == NULL)
112766131Swpaul			break;
112866131Swpaul
112966131Swpaul		if (pcn_encap(sc, m_head, &idx)) {
113066131Swpaul			IF_PREPEND(&ifp->if_snd, m_head);
1131148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
113266131Swpaul			break;
113366131Swpaul		}
113466131Swpaul
113566131Swpaul		/*
113666131Swpaul		 * If there's a BPF listener, bounce a copy of this frame
113766131Swpaul		 * to him.
113866131Swpaul		 */
1139106936Ssam		BPF_MTAP(ifp, m_head);
114066131Swpaul
114166131Swpaul	}
114266131Swpaul
114366131Swpaul	/* Transmit */
114466131Swpaul	sc->pcn_cdata.pcn_tx_prod = idx;
114566131Swpaul	pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN);
114666131Swpaul
114766131Swpaul	/*
114866131Swpaul	 * Set a timeout in case the chip goes out to lunch.
114966131Swpaul	 */
115066131Swpaul	ifp->if_timer = 5;
115166131Swpaul
115266131Swpaul	return;
115366131Swpaul}
115466131Swpaul
1155102335Salfredstatic void
1156102335Salfredpcn_setfilt(ifp)
115768270Swpaul	struct ifnet		*ifp;
115868270Swpaul{
115968270Swpaul	struct pcn_softc	*sc;
116068270Swpaul
116168270Swpaul	sc = ifp->if_softc;
116268270Swpaul
116368270Swpaul	 /* If we want promiscuous mode, set the allframes bit. */
116468270Swpaul	if (ifp->if_flags & IFF_PROMISC) {
116568270Swpaul		PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
116668270Swpaul	} else {
116768270Swpaul		PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
116868270Swpaul	}
116968270Swpaul
117068270Swpaul	/* Set the capture broadcast bit to capture broadcast frames. */
117168270Swpaul	if (ifp->if_flags & IFF_BROADCAST) {
117268270Swpaul		PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
117368270Swpaul	} else {
117468270Swpaul		PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
117568270Swpaul	}
117668270Swpaul
117768270Swpaul	return;
117868270Swpaul}
117968270Swpaul
1180102335Salfredstatic void
1181102335Salfredpcn_init(xsc)
118266131Swpaul	void			*xsc;
118366131Swpaul{
118466131Swpaul	struct pcn_softc	*sc = xsc;
1185148738Sjhb
1186148738Sjhb	PCN_LOCK(sc);
1187148738Sjhb	pcn_init_locked(sc);
1188148738Sjhb	PCN_UNLOCK(sc);
1189148738Sjhb}
1190148738Sjhb
1191148738Sjhbstatic void
1192148738Sjhbpcn_init_locked(sc)
1193148738Sjhb	struct pcn_softc	*sc;
1194148738Sjhb{
1195147256Sbrooks	struct ifnet		*ifp = sc->pcn_ifp;
119666131Swpaul	struct mii_data		*mii = NULL;
1197164712Smarius	struct ifmedia_entry	*ife;
119866131Swpaul
1199148738Sjhb	PCN_LOCK_ASSERT(sc);
120066131Swpaul
120166131Swpaul	/*
120266131Swpaul	 * Cancel pending I/O and free all RX/TX buffers.
120366131Swpaul	 */
120466131Swpaul	pcn_stop(sc);
120566131Swpaul	pcn_reset(sc);
120666131Swpaul
120766131Swpaul	mii = device_get_softc(sc->pcn_miibus);
1208164712Smarius	ife = mii->mii_media.ifm_cur;
120966131Swpaul
121066131Swpaul	/* Set MAC address */
121166131Swpaul	pcn_csr_write(sc, PCN_CSR_PAR0,
1212152315Sru	    ((u_int16_t *)IF_LLADDR(sc->pcn_ifp))[0]);
121366131Swpaul	pcn_csr_write(sc, PCN_CSR_PAR1,
1214152315Sru	    ((u_int16_t *)IF_LLADDR(sc->pcn_ifp))[1]);
121566131Swpaul	pcn_csr_write(sc, PCN_CSR_PAR2,
1216152315Sru	    ((u_int16_t *)IF_LLADDR(sc->pcn_ifp))[2]);
121766131Swpaul
121866131Swpaul	/* Init circular RX list. */
121966131Swpaul	if (pcn_list_rx_init(sc) == ENOBUFS) {
1220164072Smarius		if_printf(ifp, "initialization failed: no "
1221164072Smarius		    "memory for rx buffers\n");
122266131Swpaul		pcn_stop(sc);
122366131Swpaul		return;
122466131Swpaul	}
122566131Swpaul
122666131Swpaul	/*
122766131Swpaul	 * Init tx descriptors.
122866131Swpaul	 */
122966131Swpaul	pcn_list_tx_init(sc);
123066131Swpaul
1231164712Smarius	/* Clear PCN_MISC_ASEL so we can set the port via PCN_CSR_MODE. */
1232164712Smarius	PCN_BCR_CLRBIT(sc, PCN_BCR_MISCCFG, PCN_MISC_ASEL);
123366131Swpaul
1234164712Smarius	/*
1235164712Smarius	 * Set up the port based on the currently selected media.
1236164712Smarius	 * For Am79C978 we've to unconditionally set PCN_PORT_MII and
1237164712Smarius	 * set the PHY in PCN_BCR_PHYSEL instead.
1238164712Smarius	 */
1239164712Smarius	if (sc->pcn_type != Am79C978 &&
1240164712Smarius	    IFM_INST(ife->ifm_media) == sc->pcn_inst_10bt)
1241164712Smarius		pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_10BASET);
1242164712Smarius	else
1243164712Smarius		pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII);
1244164712Smarius
124568270Swpaul	/* Set up RX filter. */
124668270Swpaul	pcn_setfilt(ifp);
124766131Swpaul
124866131Swpaul	/*
124966131Swpaul	 * Load the multicast filter.
125066131Swpaul	 */
125166131Swpaul	pcn_setmulti(sc);
125266131Swpaul
125366131Swpaul	/*
125466131Swpaul	 * Load the addresses of the RX and TX lists.
125566131Swpaul	 */
125666131Swpaul	pcn_csr_write(sc, PCN_CSR_RXADDR0,
125766131Swpaul	    vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF);
125866131Swpaul	pcn_csr_write(sc, PCN_CSR_RXADDR1,
125966131Swpaul	    (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF);
126066131Swpaul	pcn_csr_write(sc, PCN_CSR_TXADDR0,
126166131Swpaul	    vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF);
126266131Swpaul	pcn_csr_write(sc, PCN_CSR_TXADDR1,
126366131Swpaul	    (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF);
126466131Swpaul
126566131Swpaul	/* Set the RX and TX ring sizes. */
126666131Swpaul	pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1);
126766131Swpaul	pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1);
126866131Swpaul
126966131Swpaul	/* We're not using the initialization block. */
127066131Swpaul	pcn_csr_write(sc, PCN_CSR_IAB1, 0);
127166131Swpaul
127266131Swpaul	/* Enable fast suspend mode. */
127366131Swpaul	PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE);
127466131Swpaul
127566131Swpaul	/*
127666131Swpaul	 * Enable burst read and write. Also set the no underflow
127766131Swpaul	 * bit. This will avoid transmit underruns in certain
127866210Swpaul	 * conditions while still providing decent performance.
127966131Swpaul	 */
128066131Swpaul	PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW|
128166131Swpaul	    PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE);
128266131Swpaul
128366131Swpaul	/* Enable graceful recovery from underflow. */
128466131Swpaul	PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO);
128566131Swpaul
128666131Swpaul	/* Enable auto-padding of short TX frames. */
128766131Swpaul	PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX);
128866131Swpaul
128966131Swpaul	/* Disable MII autoneg (we handle this ourselves). */
129077786Swpaul	PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS);
129166131Swpaul
129266131Swpaul	if (sc->pcn_type == Am79C978)
1293164712Smarius		/* XXX support other PHYs? */
129466131Swpaul		pcn_bcr_write(sc, PCN_BCR_PHYSEL,
129566131Swpaul		    PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA);
129666131Swpaul
129766131Swpaul	/* Enable interrupts and start the controller running. */
129866131Swpaul	pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START);
129966131Swpaul
130066131Swpaul	mii_mediachg(mii);
130166131Swpaul
1302148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1303148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
130466131Swpaul
1305148738Sjhb	callout_reset(&sc->pcn_stat_callout, hz, pcn_tick, sc);
130666131Swpaul
130766131Swpaul	return;
130866131Swpaul}
130966131Swpaul
131066131Swpaul/*
131166131Swpaul * Set media options.
131266131Swpaul */
1313102335Salfredstatic int
1314102335Salfredpcn_ifmedia_upd(ifp)
131566131Swpaul	struct ifnet		*ifp;
131666131Swpaul{
131766131Swpaul	struct pcn_softc	*sc;
131866131Swpaul
131966131Swpaul	sc = ifp->if_softc;
132066131Swpaul
1321148738Sjhb	PCN_LOCK(sc);
1322164712Smarius
1323164712Smarius	/*
1324164712Smarius	 * At least Am79C971 with DP83840A can wedge when switching
1325164712Smarius	 * from the internal 10baseT PHY to the external PHY without
1326164712Smarius	 * issuing pcn_reset(). For setting the port in PCN_CSR_MODE
1327164712Smarius	 * the PCnet chip has to be powered down or stopped anyway
1328164712Smarius	 * and although documented otherwise it doesn't take effect
1329164712Smarius	 * until the next initialization.
1330164712Smarius	 */
133166131Swpaul	sc->pcn_link = 0;
1332164712Smarius	pcn_stop(sc);
1333164712Smarius	pcn_reset(sc);
1334164712Smarius	pcn_init_locked(sc);
1335164712Smarius	if (ifp->if_snd.ifq_head != NULL)
1336164712Smarius		pcn_start_locked(ifp);
1337164712Smarius
1338148738Sjhb	PCN_UNLOCK(sc);
133966131Swpaul
134066131Swpaul	return(0);
134166131Swpaul}
134266131Swpaul
134366131Swpaul/*
134466131Swpaul * Report current media status.
134566131Swpaul */
1346102335Salfredstatic void
1347102335Salfredpcn_ifmedia_sts(ifp, ifmr)
134866131Swpaul	struct ifnet		*ifp;
134966131Swpaul	struct ifmediareq	*ifmr;
135066131Swpaul{
135166131Swpaul	struct pcn_softc	*sc;
135266131Swpaul	struct mii_data		*mii;
135366131Swpaul
135466131Swpaul	sc = ifp->if_softc;
135566131Swpaul
135666131Swpaul	mii = device_get_softc(sc->pcn_miibus);
1357148738Sjhb	PCN_LOCK(sc);
135866131Swpaul	mii_pollstat(mii);
135966131Swpaul	ifmr->ifm_active = mii->mii_media_active;
136066131Swpaul	ifmr->ifm_status = mii->mii_media_status;
1361148738Sjhb	PCN_UNLOCK(sc);
136266131Swpaul
136366131Swpaul	return;
136466131Swpaul}
136566131Swpaul
1366102335Salfredstatic int
1367102335Salfredpcn_ioctl(ifp, command, data)
136866131Swpaul	struct ifnet		*ifp;
136966131Swpaul	u_long			command;
137066131Swpaul	caddr_t			data;
137166131Swpaul{
137266131Swpaul	struct pcn_softc	*sc = ifp->if_softc;
137366131Swpaul	struct ifreq		*ifr = (struct ifreq *) data;
137466131Swpaul	struct mii_data		*mii = NULL;
137567087Swpaul	int			error = 0;
137666131Swpaul
137766131Swpaul	switch(command) {
137866131Swpaul	case SIOCSIFFLAGS:
1379148738Sjhb		PCN_LOCK(sc);
138066131Swpaul		if (ifp->if_flags & IFF_UP) {
1381148887Srwatson                        if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
138266131Swpaul			    ifp->if_flags & IFF_PROMISC &&
138366131Swpaul			    !(sc->pcn_if_flags & IFF_PROMISC)) {
138466131Swpaul				PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
138566131Swpaul				    PCN_EXTCTL1_SPND);
138668270Swpaul				pcn_setfilt(ifp);
138766131Swpaul				PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
138866131Swpaul				    PCN_EXTCTL1_SPND);
138966771Swpaul				pcn_csr_write(sc, PCN_CSR_CSR,
139066771Swpaul				    PCN_CSR_INTEN|PCN_CSR_START);
1391148887Srwatson			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
139266131Swpaul			    !(ifp->if_flags & IFF_PROMISC) &&
139366131Swpaul				sc->pcn_if_flags & IFF_PROMISC) {
139466131Swpaul				PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
139566131Swpaul				    PCN_EXTCTL1_SPND);
139668270Swpaul				pcn_setfilt(ifp);
139766131Swpaul				PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
139866131Swpaul				    PCN_EXTCTL1_SPND);
139966771Swpaul				pcn_csr_write(sc, PCN_CSR_CSR,
140066771Swpaul				    PCN_CSR_INTEN|PCN_CSR_START);
1401148887Srwatson			} else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1402148738Sjhb				pcn_init_locked(sc);
140366131Swpaul		} else {
1404148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
140566131Swpaul				pcn_stop(sc);
140666131Swpaul		}
140766131Swpaul		sc->pcn_if_flags = ifp->if_flags;
1408148738Sjhb		PCN_UNLOCK(sc);
140966131Swpaul		error = 0;
141066131Swpaul		break;
141166131Swpaul	case SIOCADDMULTI:
141266131Swpaul	case SIOCDELMULTI:
1413148738Sjhb		PCN_LOCK(sc);
141466131Swpaul		pcn_setmulti(sc);
1415148738Sjhb		PCN_UNLOCK(sc);
141666131Swpaul		error = 0;
141766131Swpaul		break;
141866131Swpaul	case SIOCGIFMEDIA:
141966131Swpaul	case SIOCSIFMEDIA:
142066131Swpaul		mii = device_get_softc(sc->pcn_miibus);
142166131Swpaul		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
142266131Swpaul		break;
142366131Swpaul	default:
1424106936Ssam		error = ether_ioctl(ifp, command, data);
142566131Swpaul		break;
142666131Swpaul	}
142766131Swpaul
142866131Swpaul	return(error);
142966131Swpaul}
143066131Swpaul
1431102335Salfredstatic void
1432102335Salfredpcn_watchdog(ifp)
143366131Swpaul	struct ifnet		*ifp;
143466131Swpaul{
143566131Swpaul	struct pcn_softc	*sc;
143666131Swpaul
143766131Swpaul	sc = ifp->if_softc;
143866131Swpaul
143967087Swpaul	PCN_LOCK(sc);
144067087Swpaul
144166131Swpaul	ifp->if_oerrors++;
1442164072Smarius	if_printf(ifp, "watchdog timeout\n");
144366131Swpaul
144466131Swpaul	pcn_stop(sc);
144566131Swpaul	pcn_reset(sc);
1446148738Sjhb	pcn_init_locked(sc);
144766131Swpaul
144866131Swpaul	if (ifp->if_snd.ifq_head != NULL)
1449186715Sbrueffer		pcn_start_locked(ifp);
145066131Swpaul
145167087Swpaul	PCN_UNLOCK(sc);
145267087Swpaul
145366131Swpaul	return;
145466131Swpaul}
145566131Swpaul
145666131Swpaul/*
145766131Swpaul * Stop the adapter and free any mbufs allocated to the
145866131Swpaul * RX and TX lists.
145966131Swpaul */
1460102335Salfredstatic void
1461188177Simppcn_stop(struct pcn_softc *sc)
146266131Swpaul{
146366131Swpaul	register int		i;
146466131Swpaul	struct ifnet		*ifp;
146566131Swpaul
1466148738Sjhb	PCN_LOCK_ASSERT(sc);
1467147256Sbrooks	ifp = sc->pcn_ifp;
146866131Swpaul	ifp->if_timer = 0;
146966131Swpaul
1470148738Sjhb	callout_stop(&sc->pcn_stat_callout);
1471110495Smdodd
1472110495Smdodd	/* Turn off interrupts */
1473110495Smdodd	PCN_CSR_CLRBIT(sc, PCN_CSR_CSR, PCN_CSR_INTEN);
1474110495Smdodd	/* Stop adapter */
147566131Swpaul	PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP);
147666131Swpaul	sc->pcn_link = 0;
147766131Swpaul
147866131Swpaul	/*
147966131Swpaul	 * Free data in the RX lists.
148066131Swpaul	 */
148166131Swpaul	for (i = 0; i < PCN_RX_LIST_CNT; i++) {
148266131Swpaul		if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) {
148366131Swpaul			m_freem(sc->pcn_cdata.pcn_rx_chain[i]);
148466131Swpaul			sc->pcn_cdata.pcn_rx_chain[i] = NULL;
148566131Swpaul		}
148666131Swpaul	}
148766131Swpaul	bzero((char *)&sc->pcn_ldata->pcn_rx_list,
148866131Swpaul		sizeof(sc->pcn_ldata->pcn_rx_list));
148966131Swpaul
149066131Swpaul	/*
149166131Swpaul	 * Free the TX list buffers.
149266131Swpaul	 */
149366131Swpaul	for (i = 0; i < PCN_TX_LIST_CNT; i++) {
149466131Swpaul		if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) {
149566131Swpaul			m_freem(sc->pcn_cdata.pcn_tx_chain[i]);
149666131Swpaul			sc->pcn_cdata.pcn_tx_chain[i] = NULL;
149766131Swpaul		}
149866131Swpaul	}
149966131Swpaul
150066131Swpaul	bzero((char *)&sc->pcn_ldata->pcn_tx_list,
150166131Swpaul		sizeof(sc->pcn_ldata->pcn_tx_list));
150266131Swpaul
1503148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
150466131Swpaul
150566131Swpaul	return;
150666131Swpaul}
150766131Swpaul
150866131Swpaul/*
150966131Swpaul * Stop all chip I/O so that the kernel's probe routines don't
151066131Swpaul * get confused by errant DMAs when rebooting.
151166131Swpaul */
1512188177Simpstatic int
1513188177Simppcn_shutdown(device_t dev)
151466131Swpaul{
151566131Swpaul	struct pcn_softc	*sc;
151666131Swpaul
151766131Swpaul	sc = device_get_softc(dev);
151866131Swpaul
151967087Swpaul	PCN_LOCK(sc);
152066131Swpaul	pcn_reset(sc);
152166131Swpaul	pcn_stop(sc);
152267087Swpaul	PCN_UNLOCK(sc);
152366131Swpaul
1524188177Simp	return 0;
152566131Swpaul}
1526