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