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