if_pcn.c revision 127135
1/*
2 * Copyright (c) 2000 Berkeley Software Design, Inc.
3 * Copyright (c) 1997, 1998, 1999, 2000
4 *	Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/pci/if_pcn.c 127135 2004-03-17 17:50:55Z njl $");
36
37/*
38 * AMD Am79c972 fast ethernet PCI NIC driver. Datatheets are available
39 * from http://www.amd.com.
40 *
41 * The AMD PCnet/PCI controllers are more advanced and functional
42 * versions of the venerable 7990 LANCE. The PCnet/PCI chips retain
43 * backwards compatibility with the LANCE and thus can be made
44 * to work with older LANCE drivers. This is in fact how the
45 * PCnet/PCI chips were supported in FreeBSD originally. The trouble
46 * is that the PCnet/PCI devices offer several performance enhancements
47 * which can't be exploited in LANCE compatibility mode. Chief among
48 * these enhancements is the ability to perform PCI DMA operations
49 * using 32-bit addressing (which eliminates the need for ISA
50 * bounce-buffering), and special receive buffer alignment (which
51 * allows the receive handler to pass packets to the upper protocol
52 * layers without copying on both the x86 and alpha platforms).
53 */
54
55#include <sys/param.h>
56#include <sys/systm.h>
57#include <sys/sockio.h>
58#include <sys/mbuf.h>
59#include <sys/malloc.h>
60#include <sys/kernel.h>
61#include <sys/socket.h>
62
63#include <net/if.h>
64#include <net/if_arp.h>
65#include <net/ethernet.h>
66#include <net/if_dl.h>
67#include <net/if_media.h>
68
69#include <net/bpf.h>
70
71#include <vm/vm.h>              /* for vtophys */
72#include <vm/pmap.h>            /* for vtophys */
73#include <machine/bus_pio.h>
74#include <machine/bus_memio.h>
75#include <machine/bus.h>
76#include <machine/resource.h>
77#include <sys/bus.h>
78#include <sys/rman.h>
79
80#include <dev/mii/mii.h>
81#include <dev/mii/miivar.h>
82
83#include <dev/pci/pcireg.h>
84#include <dev/pci/pcivar.h>
85
86#define PCN_USEIOSPACE
87
88#include <pci/if_pcnreg.h>
89
90MODULE_DEPEND(pcn, pci, 1, 1, 1);
91MODULE_DEPEND(pcn, ether, 1, 1, 1);
92MODULE_DEPEND(pcn, miibus, 1, 1, 1);
93
94/* "controller miibus0" required.  See GENERIC if you get errors here. */
95#include "miibus_if.h"
96
97/*
98 * Various supported device vendors/types and their names.
99 */
100static struct pcn_type pcn_devs[] = {
101	{ PCN_VENDORID, PCN_DEVICEID_PCNET, "AMD PCnet/PCI 10/100BaseTX" },
102	{ PCN_VENDORID, PCN_DEVICEID_HOME, "AMD PCnet/Home HomePNA" },
103	{ 0, 0, NULL }
104};
105
106static u_int32_t pcn_csr_read	(struct pcn_softc *, int);
107static u_int16_t pcn_csr_read16	(struct pcn_softc *, int);
108static u_int16_t pcn_bcr_read16	(struct pcn_softc *, int);
109static void pcn_csr_write	(struct pcn_softc *, int, int);
110static u_int32_t pcn_bcr_read	(struct pcn_softc *, int);
111static void pcn_bcr_write	(struct pcn_softc *, int, int);
112
113static int pcn_probe		(device_t);
114static int pcn_attach		(device_t);
115static int pcn_detach		(device_t);
116
117static int pcn_newbuf		(struct pcn_softc *, int, struct mbuf *);
118static int pcn_encap		(struct pcn_softc *,
119					struct mbuf *, u_int32_t *);
120static void pcn_rxeof		(struct pcn_softc *);
121static void pcn_txeof		(struct pcn_softc *);
122static void pcn_intr		(void *);
123static void pcn_tick		(void *);
124static void pcn_start		(struct ifnet *);
125static int pcn_ioctl		(struct ifnet *, u_long, caddr_t);
126static void pcn_init		(void *);
127static void pcn_stop		(struct pcn_softc *);
128static void pcn_watchdog		(struct ifnet *);
129static void pcn_shutdown		(device_t);
130static int pcn_ifmedia_upd	(struct ifnet *);
131static void pcn_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
132
133static int pcn_miibus_readreg	(device_t, int, int);
134static int pcn_miibus_writereg	(device_t, int, int, int);
135static void pcn_miibus_statchg	(device_t);
136
137static void pcn_setfilt		(struct ifnet *);
138static void pcn_setmulti	(struct pcn_softc *);
139static uint32_t pcn_mchash	(const uint8_t *);
140static void pcn_reset		(struct pcn_softc *);
141static int pcn_list_rx_init	(struct pcn_softc *);
142static int pcn_list_tx_init	(struct pcn_softc *);
143
144#ifdef PCN_USEIOSPACE
145#define PCN_RES			SYS_RES_IOPORT
146#define PCN_RID			PCN_PCI_LOIO
147#else
148#define PCN_RES			SYS_RES_MEMORY
149#define PCN_RID			PCN_PCI_LOMEM
150#endif
151
152static device_method_t pcn_methods[] = {
153	/* Device interface */
154	DEVMETHOD(device_probe,		pcn_probe),
155	DEVMETHOD(device_attach,	pcn_attach),
156	DEVMETHOD(device_detach,	pcn_detach),
157	DEVMETHOD(device_shutdown,	pcn_shutdown),
158
159	/* bus interface */
160	DEVMETHOD(bus_print_child,	bus_generic_print_child),
161	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
162
163	/* MII interface */
164	DEVMETHOD(miibus_readreg,	pcn_miibus_readreg),
165	DEVMETHOD(miibus_writereg,	pcn_miibus_writereg),
166	DEVMETHOD(miibus_statchg,	pcn_miibus_statchg),
167
168	{ 0, 0 }
169};
170
171static driver_t pcn_driver = {
172	"pcn",
173	pcn_methods,
174	sizeof(struct pcn_softc)
175};
176
177static devclass_t pcn_devclass;
178
179DRIVER_MODULE(pcn, pci, pcn_driver, pcn_devclass, 0, 0);
180DRIVER_MODULE(miibus, pcn, miibus_driver, miibus_devclass, 0, 0);
181
182#define PCN_CSR_SETBIT(sc, reg, x)			\
183	pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) | (x))
184
185#define PCN_CSR_CLRBIT(sc, reg, x)			\
186	pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) & ~(x))
187
188#define PCN_BCR_SETBIT(sc, reg, x)			\
189	pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) | (x))
190
191#define PCN_BCR_CLRBIT(sc, reg, x)			\
192	pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) & ~(x))
193
194static u_int32_t
195pcn_csr_read(sc, reg)
196	struct pcn_softc	*sc;
197	int			reg;
198{
199	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
200	return(CSR_READ_4(sc, PCN_IO32_RDP));
201}
202
203static u_int16_t
204pcn_csr_read16(sc, reg)
205	struct pcn_softc	*sc;
206	int			reg;
207{
208	CSR_WRITE_2(sc, PCN_IO16_RAP, reg);
209	return(CSR_READ_2(sc, PCN_IO16_RDP));
210}
211
212static void
213pcn_csr_write(sc, reg, val)
214	struct pcn_softc	*sc;
215	int			reg;
216	int			val;
217{
218	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
219	CSR_WRITE_4(sc, PCN_IO32_RDP, val);
220	return;
221}
222
223static u_int32_t
224pcn_bcr_read(sc, reg)
225	struct pcn_softc	*sc;
226	int			reg;
227{
228	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
229	return(CSR_READ_4(sc, PCN_IO32_BDP));
230}
231
232static u_int16_t
233pcn_bcr_read16(sc, reg)
234	struct pcn_softc	*sc;
235	int			reg;
236{
237	CSR_WRITE_2(sc, PCN_IO16_RAP, reg);
238	return(CSR_READ_2(sc, PCN_IO16_BDP));
239}
240
241static void
242pcn_bcr_write(sc, reg, val)
243	struct pcn_softc	*sc;
244	int			reg;
245	int			val;
246{
247	CSR_WRITE_4(sc, PCN_IO32_RAP, reg);
248	CSR_WRITE_4(sc, PCN_IO32_BDP, val);
249	return;
250}
251
252static int
253pcn_miibus_readreg(dev, phy, reg)
254	device_t		dev;
255	int			phy, reg;
256{
257	struct pcn_softc	*sc;
258	int			val;
259
260	sc = device_get_softc(dev);
261
262	if (sc->pcn_phyaddr && phy > sc->pcn_phyaddr)
263		return(0);
264
265	pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5));
266	val = pcn_bcr_read(sc, PCN_BCR_MIIDATA) & 0xFFFF;
267	if (val == 0xFFFF)
268		return(0);
269
270	sc->pcn_phyaddr = phy;
271
272	return(val);
273}
274
275static int
276pcn_miibus_writereg(dev, phy, reg, data)
277	device_t		dev;
278	int			phy, reg, data;
279{
280	struct pcn_softc	*sc;
281
282	sc = device_get_softc(dev);
283
284	pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5));
285	pcn_bcr_write(sc, PCN_BCR_MIIDATA, data);
286
287	return(0);
288}
289
290static void
291pcn_miibus_statchg(dev)
292	device_t		dev;
293{
294	struct pcn_softc	*sc;
295	struct mii_data		*mii;
296
297	sc = device_get_softc(dev);
298	mii = device_get_softc(sc->pcn_miibus);
299
300	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
301		PCN_BCR_SETBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN);
302	} else {
303		PCN_BCR_CLRBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN);
304	}
305
306	return;
307}
308
309#define DC_POLY		0xEDB88320
310
311static u_int32_t
312pcn_mchash(addr)
313	const uint8_t *addr;
314{
315	uint32_t crc;
316	int idx, bit;
317	uint8_t data;
318
319	/* Compute CRC for the address value. */
320	crc = 0xFFFFFFFF; /* initial value */
321
322	for (idx = 0; idx < 6; idx++) {
323		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
324			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
325	}
326
327	return ((crc >> 26) & 0x3F);
328}
329
330static void
331pcn_setmulti(sc)
332	struct pcn_softc	*sc;
333{
334	struct ifnet		*ifp;
335	struct ifmultiaddr	*ifma;
336	u_int32_t		h, i;
337	u_int16_t		hashes[4] = { 0, 0, 0, 0 };
338
339	ifp = &sc->arpcom.ac_if;
340
341	PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
342
343	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
344		for (i = 0; i < 4; i++)
345			pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0xFFFF);
346		PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
347		return;
348	}
349
350	/* first, zot all the existing hash bits */
351	for (i = 0; i < 4; i++)
352		pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0);
353
354	/* now program new ones */
355	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
356		if (ifma->ifma_addr->sa_family != AF_LINK)
357			continue;
358		h = pcn_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
359		hashes[h >> 4] |= 1 << (h & 0xF);
360	}
361
362	for (i = 0; i < 4; i++)
363		pcn_csr_write(sc, PCN_CSR_MAR0 + i, hashes[i]);
364
365	PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
366
367	return;
368}
369
370static void
371pcn_reset(sc)
372	struct pcn_softc	*sc;
373{
374	/*
375	 * Issue a reset by reading from the RESET register.
376	 * Note that we don't know if the chip is operating in
377	 * 16-bit or 32-bit mode at this point, so we attempt
378	 * to reset the chip both ways. If one fails, the other
379	 * will succeed.
380	 */
381	CSR_READ_2(sc, PCN_IO16_RESET);
382	CSR_READ_4(sc, PCN_IO32_RESET);
383
384	/* Wait a little while for the chip to get its brains in order. */
385	DELAY(1000);
386
387	/* Select 32-bit (DWIO) mode */
388	CSR_WRITE_4(sc, PCN_IO32_RDP, 0);
389
390	/* Select software style 3. */
391	pcn_bcr_write(sc, PCN_BCR_SSTYLE, PCN_SWSTYLE_PCNETPCI_BURST);
392
393        return;
394}
395
396/*
397 * Probe for an AMD chip. Check the PCI vendor and device
398 * IDs against our list and return a device name if we find a match.
399 */
400static int
401pcn_probe(dev)
402	device_t		dev;
403{
404	struct pcn_type		*t;
405	struct pcn_softc	*sc;
406	int			rid;
407	u_int32_t		chip_id;
408
409	t = pcn_devs;
410	sc = device_get_softc(dev);
411
412	while(t->pcn_name != NULL) {
413		if ((pci_get_vendor(dev) == t->pcn_vid) &&
414		    (pci_get_device(dev) == t->pcn_did)) {
415			/*
416			 * Temporarily map the I/O space
417			 * so we can read the chip ID register.
418			 */
419			rid = PCN_RID;
420			sc->pcn_res = bus_alloc_resource_any(dev, PCN_RES, &rid,
421			    RF_ACTIVE);
422			if (sc->pcn_res == NULL) {
423				device_printf(dev,
424				    "couldn't map ports/memory\n");
425				return(ENXIO);
426			}
427			sc->pcn_btag = rman_get_bustag(sc->pcn_res);
428			sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
429			mtx_init(&sc->pcn_mtx,
430			    device_get_nameunit(dev), MTX_NETWORK_LOCK,
431			    MTX_DEF);
432			PCN_LOCK(sc);
433			/*
434			 * Note: we can *NOT* put the chip into
435			 * 32-bit mode yet. The lnc driver will only
436			 * work in 16-bit mode, and once the chip
437			 * goes into 32-bit mode, the only way to
438			 * get it out again is with a hardware reset.
439			 * So if pcn_probe() is called before the
440			 * lnc driver's probe routine, the chip will
441			 * be locked into 32-bit operation and the lnc
442			 * driver will be unable to attach to it.
443			 * Note II: if the chip happens to already
444			 * be in 32-bit mode, we still need to check
445			 * the chip ID, but first we have to detect
446			 * 32-bit mode using only 16-bit operations.
447			 * The safest way to do this is to read the
448			 * PCI subsystem ID from BCR23/24 and compare
449			 * that with the value read from PCI config
450			 * space.
451			 */
452			chip_id = pcn_bcr_read16(sc, PCN_BCR_PCISUBSYSID);
453			chip_id <<= 16;
454			chip_id |= pcn_bcr_read16(sc, PCN_BCR_PCISUBVENID);
455			/*
456			 * Note III: the test for 0x10001000 is a hack to
457			 * pacify VMware, who's pseudo-PCnet interface is
458			 * broken. Reading the subsystem register from PCI
459			 * config space yeilds 0x00000000 while reading the
460			 * same value from I/O space yeilds 0x10001000. It's
461			 * not supposed to be that way.
462			 */
463			if (chip_id == pci_read_config(dev,
464			    PCIR_SUBVEND_0, 4) || chip_id == 0x10001000) {
465				/* We're in 16-bit mode. */
466				chip_id = pcn_csr_read16(sc, PCN_CSR_CHIPID1);
467				chip_id <<= 16;
468				chip_id |= pcn_csr_read16(sc, PCN_CSR_CHIPID0);
469			} else {
470				/* We're in 32-bit mode. */
471				chip_id = pcn_csr_read(sc, PCN_CSR_CHIPID1);
472				chip_id <<= 16;
473				chip_id |= pcn_csr_read(sc, PCN_CSR_CHIPID0);
474			}
475			bus_release_resource(dev, PCN_RES,
476			    PCN_RID, sc->pcn_res);
477			PCN_UNLOCK(sc);
478			mtx_destroy(&sc->pcn_mtx);
479			chip_id >>= 12;
480			sc->pcn_type = chip_id & PART_MASK;
481			switch(sc->pcn_type) {
482			case Am79C971:
483			case Am79C972:
484			case Am79C973:
485			case Am79C975:
486			case Am79C976:
487			case Am79C978:
488				break;
489			default:
490				return(ENXIO);
491			}
492			device_set_desc(dev, t->pcn_name);
493			return(0);
494		}
495		t++;
496	}
497
498	return(ENXIO);
499}
500
501/*
502 * Attach the interface. Allocate softc structures, do ifmedia
503 * setup and ethernet/BPF attach.
504 */
505static int
506pcn_attach(dev)
507	device_t		dev;
508{
509	u_int32_t		eaddr[2];
510	struct pcn_softc	*sc;
511	struct ifnet		*ifp;
512	int			unit, error = 0, rid;
513
514	sc = device_get_softc(dev);
515	unit = device_get_unit(dev);
516
517	/* Initialize our mutex. */
518	mtx_init(&sc->pcn_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
519	    MTX_DEF | MTX_RECURSE);
520#ifndef BURN_BRIDGES
521	/*
522	 * Handle power management nonsense.
523	 */
524	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
525		u_int32_t		iobase, membase, irq;
526
527		/* Save important PCI config data. */
528		iobase = pci_read_config(dev, PCN_PCI_LOIO, 4);
529		membase = pci_read_config(dev, PCN_PCI_LOMEM, 4);
530		irq = pci_read_config(dev, PCN_PCI_INTLINE, 4);
531
532		/* Reset the power state. */
533		printf("pcn%d: chip is in D%d power mode "
534		    "-- setting to D0\n", unit,
535		    pci_get_powerstate(dev));
536		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
537
538		/* Restore PCI config data. */
539		pci_write_config(dev, PCN_PCI_LOIO, iobase, 4);
540		pci_write_config(dev, PCN_PCI_LOMEM, membase, 4);
541		pci_write_config(dev, PCN_PCI_INTLINE, irq, 4);
542	}
543#endif
544	/*
545	 * Map control/status registers.
546	 */
547	pci_enable_busmaster(dev);
548
549	rid = PCN_RID;
550	sc->pcn_res = bus_alloc_resource_any(dev, PCN_RES, &rid, RF_ACTIVE);
551
552	if (sc->pcn_res == NULL) {
553		printf("pcn%d: couldn't map ports/memory\n", unit);
554		error = ENXIO;
555		goto fail;
556	}
557
558	sc->pcn_btag = rman_get_bustag(sc->pcn_res);
559	sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
560
561	/* Allocate interrupt */
562	rid = 0;
563	sc->pcn_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
564	    RF_SHAREABLE | RF_ACTIVE);
565
566	if (sc->pcn_irq == NULL) {
567		printf("pcn%d: couldn't map interrupt\n", unit);
568		error = ENXIO;
569		goto fail;
570	}
571
572	/* Reset the adapter. */
573	pcn_reset(sc);
574
575	/*
576	 * Get station address from the EEPROM.
577	 */
578	eaddr[0] = CSR_READ_4(sc, PCN_IO32_APROM00);
579	eaddr[1] = CSR_READ_4(sc, PCN_IO32_APROM01);
580	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
581
582	sc->pcn_unit = unit;
583	callout_handle_init(&sc->pcn_stat_ch);
584
585	sc->pcn_ldata = contigmalloc(sizeof(struct pcn_list_data), M_DEVBUF,
586	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
587
588	if (sc->pcn_ldata == NULL) {
589		printf("pcn%d: no memory for list buffers!\n", unit);
590		error = ENXIO;
591		goto fail;
592	}
593	bzero(sc->pcn_ldata, sizeof(struct pcn_list_data));
594
595	ifp = &sc->arpcom.ac_if;
596	ifp->if_softc = sc;
597	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
598	ifp->if_mtu = ETHERMTU;
599	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
600	ifp->if_ioctl = pcn_ioctl;
601	ifp->if_start = pcn_start;
602	ifp->if_watchdog = pcn_watchdog;
603	ifp->if_init = pcn_init;
604	ifp->if_baudrate = 10000000;
605	ifp->if_snd.ifq_maxlen = PCN_TX_LIST_CNT - 1;
606
607	/*
608	 * Do MII setup.
609	 */
610	if (mii_phy_probe(dev, &sc->pcn_miibus,
611	    pcn_ifmedia_upd, pcn_ifmedia_sts)) {
612		printf("pcn%d: MII without any PHY!\n", sc->pcn_unit);
613		error = ENXIO;
614		goto fail;
615	}
616
617	/*
618	 * Call MI attach routine.
619	 */
620	ether_ifattach(ifp, (u_int8_t *) eaddr);
621
622	/* Hook interrupt last to avoid having to lock softc */
623	error = bus_setup_intr(dev, sc->pcn_irq, INTR_TYPE_NET,
624	    pcn_intr, sc, &sc->pcn_intrhand);
625
626	if (error) {
627		printf("pcn%d: couldn't set up irq\n", unit);
628		ether_ifdetach(ifp);
629		goto fail;
630	}
631
632fail:
633	if (error)
634		pcn_detach(dev);
635
636	return(error);
637}
638
639/*
640 * Shutdown hardware and free up resources. This can be called any
641 * time after the mutex has been initialized. It is called in both
642 * the error case in attach and the normal detach case so it needs
643 * to be careful about only freeing resources that have actually been
644 * allocated.
645 */
646static int
647pcn_detach(dev)
648	device_t		dev;
649{
650	struct pcn_softc	*sc;
651	struct ifnet		*ifp;
652
653	sc = device_get_softc(dev);
654	ifp = &sc->arpcom.ac_if;
655
656	KASSERT(mtx_initialized(&sc->pcn_mtx), ("pcn mutex not initialized"));
657	PCN_LOCK(sc);
658
659	/* These should only be active if attach succeeded */
660	if (device_is_attached(dev)) {
661		pcn_reset(sc);
662		pcn_stop(sc);
663		ether_ifdetach(ifp);
664	}
665	if (sc->pcn_miibus)
666		device_delete_child(dev, sc->pcn_miibus);
667	bus_generic_detach(dev);
668
669	if (sc->pcn_intrhand)
670		bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
671	if (sc->pcn_irq)
672		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
673	if (sc->pcn_res)
674		bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
675
676	if (sc->pcn_ldata) {
677		contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data),
678		    M_DEVBUF);
679	}
680	PCN_UNLOCK(sc);
681
682	mtx_destroy(&sc->pcn_mtx);
683
684	return(0);
685}
686
687/*
688 * Initialize the transmit descriptors.
689 */
690static int
691pcn_list_tx_init(sc)
692	struct pcn_softc	*sc;
693{
694	struct pcn_list_data	*ld;
695	struct pcn_ring_data	*cd;
696	int			i;
697
698	cd = &sc->pcn_cdata;
699	ld = sc->pcn_ldata;
700
701	for (i = 0; i < PCN_TX_LIST_CNT; i++) {
702		cd->pcn_tx_chain[i] = NULL;
703		ld->pcn_tx_list[i].pcn_tbaddr = 0;
704		ld->pcn_tx_list[i].pcn_txctl = 0;
705		ld->pcn_tx_list[i].pcn_txstat = 0;
706	}
707
708	cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0;
709
710	return(0);
711}
712
713
714/*
715 * Initialize the RX descriptors and allocate mbufs for them.
716 */
717static int
718pcn_list_rx_init(sc)
719	struct pcn_softc	*sc;
720{
721	struct pcn_ring_data	*cd;
722	int			i;
723
724	cd = &sc->pcn_cdata;
725
726	for (i = 0; i < PCN_RX_LIST_CNT; i++) {
727		if (pcn_newbuf(sc, i, NULL) == ENOBUFS)
728			return(ENOBUFS);
729	}
730
731	cd->pcn_rx_prod = 0;
732
733	return(0);
734}
735
736/*
737 * Initialize an RX descriptor and attach an MBUF cluster.
738 */
739static int
740pcn_newbuf(sc, idx, m)
741	struct pcn_softc	*sc;
742	int			idx;
743	struct mbuf		*m;
744{
745	struct mbuf		*m_new = NULL;
746	struct pcn_rx_desc	*c;
747
748	c = &sc->pcn_ldata->pcn_rx_list[idx];
749
750	if (m == NULL) {
751		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
752		if (m_new == NULL)
753			return(ENOBUFS);
754
755		MCLGET(m_new, M_DONTWAIT);
756		if (!(m_new->m_flags & M_EXT)) {
757			m_freem(m_new);
758			return(ENOBUFS);
759		}
760		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
761	} else {
762		m_new = m;
763		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
764		m_new->m_data = m_new->m_ext.ext_buf;
765	}
766
767	m_adj(m_new, ETHER_ALIGN);
768
769	sc->pcn_cdata.pcn_rx_chain[idx] = m_new;
770	c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t));
771	c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ;
772	c->pcn_bufsz |= PCN_RXLEN_MBO;
773	c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN;
774
775	return(0);
776}
777
778/*
779 * A frame has been uploaded: pass the resulting mbuf chain up to
780 * the higher level protocols.
781 */
782static void
783pcn_rxeof(sc)
784	struct pcn_softc	*sc;
785{
786        struct mbuf		*m;
787        struct ifnet		*ifp;
788	struct pcn_rx_desc	*cur_rx;
789	int			i;
790
791	PCN_LOCK_ASSERT(sc);
792
793	ifp = &sc->arpcom.ac_if;
794	i = sc->pcn_cdata.pcn_rx_prod;
795
796	while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) {
797		cur_rx = &sc->pcn_ldata->pcn_rx_list[i];
798		m = sc->pcn_cdata.pcn_rx_chain[i];
799		sc->pcn_cdata.pcn_rx_chain[i] = NULL;
800
801		/*
802		 * If an error occurs, update stats, clear the
803		 * status word and leave the mbuf cluster in place:
804		 * it should simply get re-used next time this descriptor
805	 	 * comes up in the ring.
806		 */
807		if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) {
808			ifp->if_ierrors++;
809			pcn_newbuf(sc, i, m);
810			PCN_INC(i, PCN_RX_LIST_CNT);
811			continue;
812		}
813
814		if (pcn_newbuf(sc, i, NULL)) {
815			/* Ran out of mbufs; recycle this one. */
816			pcn_newbuf(sc, i, m);
817			ifp->if_ierrors++;
818			PCN_INC(i, PCN_RX_LIST_CNT);
819			continue;
820		}
821
822		PCN_INC(i, PCN_RX_LIST_CNT);
823
824		/* No errors; receive the packet. */
825		ifp->if_ipackets++;
826		m->m_len = m->m_pkthdr.len =
827		    cur_rx->pcn_rxlen - ETHER_CRC_LEN;
828		m->m_pkthdr.rcvif = ifp;
829
830		PCN_UNLOCK(sc);
831		(*ifp->if_input)(ifp, m);
832		PCN_LOCK(sc);
833	}
834
835	sc->pcn_cdata.pcn_rx_prod = i;
836
837	return;
838}
839
840/*
841 * A frame was downloaded to the chip. It's safe for us to clean up
842 * the list buffers.
843 */
844
845static void
846pcn_txeof(sc)
847	struct pcn_softc	*sc;
848{
849	struct pcn_tx_desc	*cur_tx = NULL;
850	struct ifnet		*ifp;
851	u_int32_t		idx;
852
853	ifp = &sc->arpcom.ac_if;
854
855	/*
856	 * Go through our tx list and free mbufs for those
857	 * frames that have been transmitted.
858	 */
859	idx = sc->pcn_cdata.pcn_tx_cons;
860	while (idx != sc->pcn_cdata.pcn_tx_prod) {
861		cur_tx = &sc->pcn_ldata->pcn_tx_list[idx];
862
863		if (!PCN_OWN_TXDESC(cur_tx))
864			break;
865
866		if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) {
867			sc->pcn_cdata.pcn_tx_cnt--;
868			PCN_INC(idx, PCN_TX_LIST_CNT);
869			continue;
870		}
871
872		if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) {
873			ifp->if_oerrors++;
874			if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF)
875				ifp->if_collisions++;
876			if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY)
877				ifp->if_collisions++;
878		}
879
880		ifp->if_collisions +=
881		    cur_tx->pcn_txstat & PCN_TXSTAT_TRC;
882
883		ifp->if_opackets++;
884		if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) {
885			m_freem(sc->pcn_cdata.pcn_tx_chain[idx]);
886			sc->pcn_cdata.pcn_tx_chain[idx] = NULL;
887		}
888
889		sc->pcn_cdata.pcn_tx_cnt--;
890		PCN_INC(idx, PCN_TX_LIST_CNT);
891	}
892
893	if (idx != sc->pcn_cdata.pcn_tx_cons) {
894		/* Some buffers have been freed. */
895		sc->pcn_cdata.pcn_tx_cons = idx;
896		ifp->if_flags &= ~IFF_OACTIVE;
897	}
898	ifp->if_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5;
899
900	return;
901}
902
903static void
904pcn_tick(xsc)
905	void			*xsc;
906{
907	struct pcn_softc	*sc;
908	struct mii_data		*mii;
909	struct ifnet		*ifp;
910
911	sc = xsc;
912	ifp = &sc->arpcom.ac_if;
913	PCN_LOCK(sc);
914
915	mii = device_get_softc(sc->pcn_miibus);
916	mii_tick(mii);
917
918	/* link just died */
919	if (sc->pcn_link & !(mii->mii_media_status & IFM_ACTIVE))
920		sc->pcn_link = 0;
921
922	/* link just came up, restart */
923	if (!sc->pcn_link && mii->mii_media_status & IFM_ACTIVE &&
924	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
925		sc->pcn_link++;
926		if (ifp->if_snd.ifq_head != NULL)
927			pcn_start(ifp);
928	}
929
930	sc->pcn_stat_ch = timeout(pcn_tick, sc, hz);
931
932	PCN_UNLOCK(sc);
933
934	return;
935}
936
937static void
938pcn_intr(arg)
939	void			*arg;
940{
941	struct pcn_softc	*sc;
942	struct ifnet		*ifp;
943	u_int32_t		status;
944
945	sc = arg;
946	ifp = &sc->arpcom.ac_if;
947
948	/* Supress unwanted interrupts */
949	if (!(ifp->if_flags & IFF_UP)) {
950		pcn_stop(sc);
951		return;
952	}
953
954	PCN_LOCK(sc);
955
956	CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR);
957
958	while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) {
959		CSR_WRITE_4(sc, PCN_IO32_RDP, status);
960
961		if (status & PCN_CSR_RINT)
962			pcn_rxeof(sc);
963
964		if (status & PCN_CSR_TINT)
965			pcn_txeof(sc);
966
967		if (status & PCN_CSR_ERR) {
968			pcn_init(sc);
969			break;
970		}
971	}
972
973	if (ifp->if_snd.ifq_head != NULL)
974		pcn_start(ifp);
975
976	PCN_UNLOCK(sc);
977	return;
978}
979
980/*
981 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
982 * pointers to the fragment pointers.
983 */
984static int
985pcn_encap(sc, m_head, txidx)
986	struct pcn_softc	*sc;
987	struct mbuf		*m_head;
988	u_int32_t		*txidx;
989{
990	struct pcn_tx_desc	*f = NULL;
991	struct mbuf		*m;
992	int			frag, cur, cnt = 0;
993
994	/*
995 	 * Start packing the mbufs in this chain into
996	 * the fragment pointers. Stop when we run out
997 	 * of fragments or hit the end of the mbuf chain.
998	 */
999	m = m_head;
1000	cur = frag = *txidx;
1001
1002	for (m = m_head; m != NULL; m = m->m_next) {
1003		if (m->m_len != 0) {
1004			if ((PCN_TX_LIST_CNT -
1005			    (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2)
1006				return(ENOBUFS);
1007			f = &sc->pcn_ldata->pcn_tx_list[frag];
1008			f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ;
1009			f->pcn_txctl |= PCN_TXCTL_MBO;
1010			f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t));
1011			if (cnt == 0)
1012				f->pcn_txctl |= PCN_TXCTL_STP;
1013			else
1014				f->pcn_txctl |= PCN_TXCTL_OWN;
1015			cur = frag;
1016			PCN_INC(frag, PCN_TX_LIST_CNT);
1017			cnt++;
1018		}
1019	}
1020
1021	if (m != NULL)
1022		return(ENOBUFS);
1023
1024	sc->pcn_cdata.pcn_tx_chain[cur] = m_head;
1025	sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |=
1026	    PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT;
1027	sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN;
1028	sc->pcn_cdata.pcn_tx_cnt += cnt;
1029	*txidx = frag;
1030
1031	return(0);
1032}
1033
1034/*
1035 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1036 * to the mbuf data regions directly in the transmit lists. We also save a
1037 * copy of the pointers since the transmit list fragment pointers are
1038 * physical addresses.
1039 */
1040static void
1041pcn_start(ifp)
1042	struct ifnet		*ifp;
1043{
1044	struct pcn_softc	*sc;
1045	struct mbuf		*m_head = NULL;
1046	u_int32_t		idx;
1047
1048	sc = ifp->if_softc;
1049
1050	PCN_LOCK(sc);
1051
1052	if (!sc->pcn_link) {
1053		PCN_UNLOCK(sc);
1054		return;
1055	}
1056
1057	idx = sc->pcn_cdata.pcn_tx_prod;
1058
1059	if (ifp->if_flags & IFF_OACTIVE) {
1060		PCN_UNLOCK(sc);
1061		return;
1062	}
1063
1064	while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) {
1065		IF_DEQUEUE(&ifp->if_snd, m_head);
1066		if (m_head == NULL)
1067			break;
1068
1069		if (pcn_encap(sc, m_head, &idx)) {
1070			IF_PREPEND(&ifp->if_snd, m_head);
1071			ifp->if_flags |= IFF_OACTIVE;
1072			break;
1073		}
1074
1075		/*
1076		 * If there's a BPF listener, bounce a copy of this frame
1077		 * to him.
1078		 */
1079		BPF_MTAP(ifp, m_head);
1080
1081	}
1082
1083	/* Transmit */
1084	sc->pcn_cdata.pcn_tx_prod = idx;
1085	pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN);
1086
1087	/*
1088	 * Set a timeout in case the chip goes out to lunch.
1089	 */
1090	ifp->if_timer = 5;
1091
1092	PCN_UNLOCK(sc);
1093
1094	return;
1095}
1096
1097static void
1098pcn_setfilt(ifp)
1099	struct ifnet		*ifp;
1100{
1101	struct pcn_softc	*sc;
1102
1103	sc = ifp->if_softc;
1104
1105	 /* If we want promiscuous mode, set the allframes bit. */
1106	if (ifp->if_flags & IFF_PROMISC) {
1107		PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1108	} else {
1109		PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1110	}
1111
1112	/* Set the capture broadcast bit to capture broadcast frames. */
1113	if (ifp->if_flags & IFF_BROADCAST) {
1114		PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1115	} else {
1116		PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1117	}
1118
1119	return;
1120}
1121
1122static void
1123pcn_init(xsc)
1124	void			*xsc;
1125{
1126	struct pcn_softc	*sc = xsc;
1127	struct ifnet		*ifp = &sc->arpcom.ac_if;
1128	struct mii_data		*mii = NULL;
1129
1130	PCN_LOCK(sc);
1131
1132	/*
1133	 * Cancel pending I/O and free all RX/TX buffers.
1134	 */
1135	pcn_stop(sc);
1136	pcn_reset(sc);
1137
1138	mii = device_get_softc(sc->pcn_miibus);
1139
1140	/* Set MAC address */
1141	pcn_csr_write(sc, PCN_CSR_PAR0,
1142	    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1143	pcn_csr_write(sc, PCN_CSR_PAR1,
1144	    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1145	pcn_csr_write(sc, PCN_CSR_PAR2,
1146	    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1147
1148	/* Init circular RX list. */
1149	if (pcn_list_rx_init(sc) == ENOBUFS) {
1150		printf("pcn%d: initialization failed: no "
1151		    "memory for rx buffers\n", sc->pcn_unit);
1152		pcn_stop(sc);
1153		PCN_UNLOCK(sc);
1154		return;
1155	}
1156
1157	/*
1158	 * Init tx descriptors.
1159	 */
1160	pcn_list_tx_init(sc);
1161
1162	/* Set up the mode register. */
1163	pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII);
1164
1165	/* Set up RX filter. */
1166	pcn_setfilt(ifp);
1167
1168	/*
1169	 * Load the multicast filter.
1170	 */
1171	pcn_setmulti(sc);
1172
1173	/*
1174	 * Load the addresses of the RX and TX lists.
1175	 */
1176	pcn_csr_write(sc, PCN_CSR_RXADDR0,
1177	    vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF);
1178	pcn_csr_write(sc, PCN_CSR_RXADDR1,
1179	    (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF);
1180	pcn_csr_write(sc, PCN_CSR_TXADDR0,
1181	    vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF);
1182	pcn_csr_write(sc, PCN_CSR_TXADDR1,
1183	    (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF);
1184
1185	/* Set the RX and TX ring sizes. */
1186	pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1);
1187	pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1);
1188
1189	/* We're not using the initialization block. */
1190	pcn_csr_write(sc, PCN_CSR_IAB1, 0);
1191
1192	/* Enable fast suspend mode. */
1193	PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE);
1194
1195	/*
1196	 * Enable burst read and write. Also set the no underflow
1197	 * bit. This will avoid transmit underruns in certain
1198	 * conditions while still providing decent performance.
1199	 */
1200	PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW|
1201	    PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE);
1202
1203	/* Enable graceful recovery from underflow. */
1204	PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO);
1205
1206	/* Enable auto-padding of short TX frames. */
1207	PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX);
1208
1209	/* Disable MII autoneg (we handle this ourselves). */
1210	PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS);
1211
1212	if (sc->pcn_type == Am79C978)
1213		pcn_bcr_write(sc, PCN_BCR_PHYSEL,
1214		    PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA);
1215
1216	/* Enable interrupts and start the controller running. */
1217	pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START);
1218
1219	mii_mediachg(mii);
1220
1221	ifp->if_flags |= IFF_RUNNING;
1222	ifp->if_flags &= ~IFF_OACTIVE;
1223
1224	sc->pcn_stat_ch = timeout(pcn_tick, sc, hz);
1225	PCN_UNLOCK(sc);
1226
1227	return;
1228}
1229
1230/*
1231 * Set media options.
1232 */
1233static int
1234pcn_ifmedia_upd(ifp)
1235	struct ifnet		*ifp;
1236{
1237	struct pcn_softc	*sc;
1238	struct mii_data		*mii;
1239
1240	sc = ifp->if_softc;
1241	mii = device_get_softc(sc->pcn_miibus);
1242
1243	sc->pcn_link = 0;
1244	if (mii->mii_instance) {
1245		struct mii_softc        *miisc;
1246		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1247			mii_phy_reset(miisc);
1248	}
1249	mii_mediachg(mii);
1250
1251	return(0);
1252}
1253
1254/*
1255 * Report current media status.
1256 */
1257static void
1258pcn_ifmedia_sts(ifp, ifmr)
1259	struct ifnet		*ifp;
1260	struct ifmediareq	*ifmr;
1261{
1262	struct pcn_softc	*sc;
1263	struct mii_data		*mii;
1264
1265	sc = ifp->if_softc;
1266
1267	mii = device_get_softc(sc->pcn_miibus);
1268	mii_pollstat(mii);
1269	ifmr->ifm_active = mii->mii_media_active;
1270	ifmr->ifm_status = mii->mii_media_status;
1271
1272	return;
1273}
1274
1275static int
1276pcn_ioctl(ifp, command, data)
1277	struct ifnet		*ifp;
1278	u_long			command;
1279	caddr_t			data;
1280{
1281	struct pcn_softc	*sc = ifp->if_softc;
1282	struct ifreq		*ifr = (struct ifreq *) data;
1283	struct mii_data		*mii = NULL;
1284	int			error = 0;
1285
1286	PCN_LOCK(sc);
1287
1288	switch(command) {
1289	case SIOCSIFFLAGS:
1290		if (ifp->if_flags & IFF_UP) {
1291                        if (ifp->if_flags & IFF_RUNNING &&
1292			    ifp->if_flags & IFF_PROMISC &&
1293			    !(sc->pcn_if_flags & IFF_PROMISC)) {
1294				PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1295				    PCN_EXTCTL1_SPND);
1296				pcn_setfilt(ifp);
1297				PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1298				    PCN_EXTCTL1_SPND);
1299				pcn_csr_write(sc, PCN_CSR_CSR,
1300				    PCN_CSR_INTEN|PCN_CSR_START);
1301			} else if (ifp->if_flags & IFF_RUNNING &&
1302			    !(ifp->if_flags & IFF_PROMISC) &&
1303				sc->pcn_if_flags & IFF_PROMISC) {
1304				PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1305				    PCN_EXTCTL1_SPND);
1306				pcn_setfilt(ifp);
1307				PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1308				    PCN_EXTCTL1_SPND);
1309				pcn_csr_write(sc, PCN_CSR_CSR,
1310				    PCN_CSR_INTEN|PCN_CSR_START);
1311			} else if (!(ifp->if_flags & IFF_RUNNING))
1312				pcn_init(sc);
1313		} else {
1314			if (ifp->if_flags & IFF_RUNNING)
1315				pcn_stop(sc);
1316		}
1317		sc->pcn_if_flags = ifp->if_flags;
1318		error = 0;
1319		break;
1320	case SIOCADDMULTI:
1321	case SIOCDELMULTI:
1322		pcn_setmulti(sc);
1323		error = 0;
1324		break;
1325	case SIOCGIFMEDIA:
1326	case SIOCSIFMEDIA:
1327		mii = device_get_softc(sc->pcn_miibus);
1328		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1329		break;
1330	default:
1331		error = ether_ioctl(ifp, command, data);
1332		break;
1333	}
1334
1335	PCN_UNLOCK(sc);
1336
1337	return(error);
1338}
1339
1340static void
1341pcn_watchdog(ifp)
1342	struct ifnet		*ifp;
1343{
1344	struct pcn_softc	*sc;
1345
1346	sc = ifp->if_softc;
1347
1348	PCN_LOCK(sc);
1349
1350	ifp->if_oerrors++;
1351	printf("pcn%d: watchdog timeout\n", sc->pcn_unit);
1352
1353	pcn_stop(sc);
1354	pcn_reset(sc);
1355	pcn_init(sc);
1356
1357	if (ifp->if_snd.ifq_head != NULL)
1358		pcn_start(ifp);
1359
1360	PCN_UNLOCK(sc);
1361
1362	return;
1363}
1364
1365/*
1366 * Stop the adapter and free any mbufs allocated to the
1367 * RX and TX lists.
1368 */
1369static void
1370pcn_stop(sc)
1371	struct pcn_softc	*sc;
1372{
1373	register int		i;
1374	struct ifnet		*ifp;
1375
1376	ifp = &sc->arpcom.ac_if;
1377	PCN_LOCK(sc);
1378	ifp->if_timer = 0;
1379
1380	untimeout(pcn_tick, sc, sc->pcn_stat_ch);
1381
1382	/* Turn off interrupts */
1383	PCN_CSR_CLRBIT(sc, PCN_CSR_CSR, PCN_CSR_INTEN);
1384	/* Stop adapter */
1385	PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP);
1386	sc->pcn_link = 0;
1387
1388	/*
1389	 * Free data in the RX lists.
1390	 */
1391	for (i = 0; i < PCN_RX_LIST_CNT; i++) {
1392		if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) {
1393			m_freem(sc->pcn_cdata.pcn_rx_chain[i]);
1394			sc->pcn_cdata.pcn_rx_chain[i] = NULL;
1395		}
1396	}
1397	bzero((char *)&sc->pcn_ldata->pcn_rx_list,
1398		sizeof(sc->pcn_ldata->pcn_rx_list));
1399
1400	/*
1401	 * Free the TX list buffers.
1402	 */
1403	for (i = 0; i < PCN_TX_LIST_CNT; i++) {
1404		if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) {
1405			m_freem(sc->pcn_cdata.pcn_tx_chain[i]);
1406			sc->pcn_cdata.pcn_tx_chain[i] = NULL;
1407		}
1408	}
1409
1410	bzero((char *)&sc->pcn_ldata->pcn_tx_list,
1411		sizeof(sc->pcn_ldata->pcn_tx_list));
1412
1413	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1414	PCN_UNLOCK(sc);
1415
1416	return;
1417}
1418
1419/*
1420 * Stop all chip I/O so that the kernel's probe routines don't
1421 * get confused by errant DMAs when rebooting.
1422 */
1423static void
1424pcn_shutdown(dev)
1425	device_t		dev;
1426{
1427	struct pcn_softc	*sc;
1428
1429	sc = device_get_softc(dev);
1430
1431	PCN_LOCK(sc);
1432	pcn_reset(sc);
1433	pcn_stop(sc);
1434	PCN_UNLOCK(sc);
1435
1436	return;
1437}
1438