xref: /dragonfly/sys/dev/netif/pcn/if_pcn.c (revision 1ab20d67)
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.9 2004/03/23 22:19:02 hsu Exp $
35  *
36  * $FreeBSD: src/sys/pci/if_pcn.c,v 1.5.2.10 2003/03/05 18:42:33 njl Exp $
37  */
38 
39 /*
40  * AMD Am79c972 fast ethernet PCI NIC driver. Datatheets are available
41  * from http://www.amd.com.
42  *
43  * Written by Bill Paul <wpaul@osd.bsdi.com>
44  */
45 
46 /*
47  * The AMD PCnet/PCI controllers are more advanced and functional
48  * versions of the venerable 7990 LANCE. The PCnet/PCI chips retain
49  * backwards compatibility with the LANCE and thus can be made
50  * to work with older LANCE drivers. This is in fact how the
51  * PCnet/PCI chips were supported in FreeBSD originally. The trouble
52  * is that the PCnet/PCI devices offer several performance enhancements
53  * which can't be exploited in LANCE compatibility mode. Chief among
54  * these enhancements is the ability to perform PCI DMA operations
55  * using 32-bit addressing (which eliminates the need for ISA
56  * bounce-buffering), and special receive buffer alignment (which
57  * allows the receive handler to pass packets to the upper protocol
58  * layers without copying on both the x86 and alpha platforms).
59  */
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
64 #include <sys/mbuf.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67 #include <sys/socket.h>
68 
69 #include <net/if.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 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
346 	    ifma = ifma->ifma_link.le_next) {
347 		if (ifma->ifma_addr->sa_family != AF_LINK)
348 			continue;
349 		h = pcn_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
350 		hashes[h >> 4] |= 1 << (h & 0xF);
351 	}
352 
353 	for (i = 0; i < 4; i++)
354 		pcn_csr_write(sc, PCN_CSR_MAR0 + i, hashes[i]);
355 
356 	PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND);
357 
358 	return;
359 }
360 
361 static void pcn_reset(sc)
362 	struct pcn_softc	*sc;
363 {
364 	/*
365 	 * Issue a reset by reading from the RESET register.
366 	 * Note that we don't know if the chip is operating in
367 	 * 16-bit or 32-bit mode at this point, so we attempt
368 	 * to reset the chip both ways. If one fails, the other
369 	 * will succeed.
370 	 */
371 	CSR_READ_2(sc, PCN_IO16_RESET);
372 	CSR_READ_4(sc, PCN_IO32_RESET);
373 
374 	/* Wait a little while for the chip to get its brains in order. */
375 	DELAY(1000);
376 
377 	/* Select 32-bit (DWIO) mode */
378 	CSR_WRITE_4(sc, PCN_IO32_RDP, 0);
379 
380 	/* Select software style 3. */
381 	pcn_bcr_write(sc, PCN_BCR_SSTYLE, PCN_SWSTYLE_PCNETPCI_BURST);
382 
383         return;
384 }
385 
386 /*
387  * Probe for an AMD chip. Check the PCI vendor and device
388  * IDs against our list and return a device name if we find a match.
389  */
390 static int pcn_probe(dev)
391 	device_t		dev;
392 {
393 	struct pcn_type		*t;
394 	struct pcn_softc	*sc;
395 	int			rid;
396 	u_int32_t		chip_id;
397 
398 	t = pcn_devs;
399 	sc = device_get_softc(dev);
400 
401 	while(t->pcn_name != NULL) {
402 		if ((pci_get_vendor(dev) == t->pcn_vid) &&
403 		    (pci_get_device(dev) == t->pcn_did)) {
404 			/*
405 			 * Temporarily map the I/O space
406 			 * so we can read the chip ID register.
407 			 */
408 			rid = PCN_RID;
409 			sc->pcn_res = bus_alloc_resource(dev, PCN_RES, &rid,
410 			    0, ~0, 1, RF_ACTIVE);
411 			if (sc->pcn_res == NULL) {
412 				device_printf(dev,
413 				    "couldn't map ports/memory\n");
414 				return(ENXIO);
415 			}
416 			sc->pcn_btag = rman_get_bustag(sc->pcn_res);
417 			sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
418 			/*
419 			 * Note: we can *NOT* put the chip into
420 			 * 32-bit mode yet. The lnc driver will only
421 			 * work in 16-bit mode, and once the chip
422 			 * goes into 32-bit mode, the only way to
423 			 * get it out again is with a hardware reset.
424 			 * So if pcn_probe() is called before the
425 			 * lnc driver's probe routine, the chip will
426 			 * be locked into 32-bit operation and the lnc
427 			 * driver will be unable to attach to it.
428 			 * Note II: if the chip happens to already
429 			 * be in 32-bit mode, we still need to check
430 			 * the chip ID, but first we have to detect
431 			 * 32-bit mode using only 16-bit operations.
432 			 * The safest way to do this is to read the
433 			 * PCI subsystem ID from BCR23/24 and compare
434 			 * that with the value read from PCI config
435 			 * space.
436 			 */
437 			chip_id = pcn_bcr_read16(sc, PCN_BCR_PCISUBSYSID);
438 			chip_id <<= 16;
439 			chip_id |= pcn_bcr_read16(sc, PCN_BCR_PCISUBVENID);
440 			/*
441 			 * Note III: the test for 0x10001000 is a hack to
442 			 * pacify VMware, who's pseudo-PCnet interface is
443 			 * broken. Reading the subsystem register from PCI
444 			 * config space yeilds 0x00000000 while reading the
445 			 * same value from I/O space yeilds 0x10001000. It's
446 			 * not supposed to be that way.
447 			 */
448 			if (chip_id == pci_read_config(dev,
449 			    PCIR_SUBVEND_0, 4) || chip_id == 0x10001000) {
450 				/* We're in 16-bit mode. */
451 				chip_id = pcn_csr_read16(sc, PCN_CSR_CHIPID1);
452 				chip_id <<= 16;
453 				chip_id |= pcn_csr_read16(sc, PCN_CSR_CHIPID0);
454 			} else {
455 				/* We're in 32-bit mode. */
456 				chip_id = pcn_csr_read(sc, PCN_CSR_CHIPID1);
457 				chip_id <<= 16;
458 				chip_id |= pcn_csr_read(sc, PCN_CSR_CHIPID0);
459 			}
460 			bus_release_resource(dev, PCN_RES,
461 			    PCN_RID, sc->pcn_res);
462 			chip_id >>= 12;
463 			sc->pcn_type = chip_id & PART_MASK;
464 			switch(sc->pcn_type) {
465 			case Am79C971:
466 			case Am79C972:
467 			case Am79C973:
468 			case Am79C975:
469 			case Am79C976:
470 			case Am79C978:
471 				break;
472 			default:
473 				return(ENXIO);
474 				break;
475 			}
476 			device_set_desc(dev, t->pcn_name);
477 			return(0);
478 		}
479 		t++;
480 	}
481 
482 	return(ENXIO);
483 }
484 
485 /*
486  * Attach the interface. Allocate softc structures, do ifmedia
487  * setup and ethernet/BPF attach.
488  */
489 static int pcn_attach(dev)
490 	device_t		dev;
491 {
492 	int			s;
493 	u_int32_t		eaddr[2];
494 	u_int32_t		command;
495 	struct pcn_softc	*sc;
496 	struct ifnet		*ifp;
497 	int			unit, error = 0, rid;
498 
499 	s = splimp();
500 
501 	sc = device_get_softc(dev);
502 	unit = device_get_unit(dev);
503 
504 	/*
505 	 * Handle power management nonsense.
506 	 */
507 
508 	command = pci_read_config(dev, PCN_PCI_CAPID, 4) & 0x000000FF;
509 	if (command == 0x01) {
510 
511 		command = pci_read_config(dev, PCN_PCI_PWRMGMTCTRL, 4);
512 		if (command & PCN_PSTATE_MASK) {
513 			u_int32_t		iobase, membase, irq;
514 
515 			/* Save important PCI config data. */
516 			iobase = pci_read_config(dev, PCN_PCI_LOIO, 4);
517 			membase = pci_read_config(dev, PCN_PCI_LOMEM, 4);
518 			irq = pci_read_config(dev, PCN_PCI_INTLINE, 4);
519 
520 			/* Reset the power state. */
521 			printf("pcn%d: chip is in D%d power mode "
522 			"-- setting to D0\n", unit, command & PCN_PSTATE_MASK);
523 			command &= 0xFFFFFFFC;
524 			pci_write_config(dev, PCN_PCI_PWRMGMTCTRL, command, 4);
525 
526 			/* Restore PCI config data. */
527 			pci_write_config(dev, PCN_PCI_LOIO, iobase, 4);
528 			pci_write_config(dev, PCN_PCI_LOMEM, membase, 4);
529 			pci_write_config(dev, PCN_PCI_INTLINE, irq, 4);
530 		}
531 	}
532 
533 	/*
534 	 * Map control/status registers.
535 	 */
536 	command = pci_read_config(dev, PCIR_COMMAND, 4);
537 	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
538 	pci_write_config(dev, PCIR_COMMAND, command, 4);
539 	command = pci_read_config(dev, PCIR_COMMAND, 4);
540 
541 #ifdef PCN_USEIOSPACE
542 	if (!(command & PCIM_CMD_PORTEN)) {
543 		printf("pcn%d: failed to enable I/O ports!\n", unit);
544 		error = ENXIO;;
545 		goto fail;
546 	}
547 #else
548 	if (!(command & PCIM_CMD_MEMEN)) {
549 		printf("pcn%d: failed to enable memory mapping!\n", unit);
550 		error = ENXIO;;
551 		goto fail;
552 	}
553 #endif
554 
555 	rid = PCN_RID;
556 	sc->pcn_res = bus_alloc_resource(dev, PCN_RES, &rid,
557 	    0, ~0, 1, RF_ACTIVE);
558 
559 	if (sc->pcn_res == NULL) {
560 		printf("pcn%d: couldn't map ports/memory\n", unit);
561 		error = ENXIO;
562 		goto fail;
563 	}
564 
565 	sc->pcn_btag = rman_get_bustag(sc->pcn_res);
566 	sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res);
567 
568 	/* Allocate interrupt */
569 	rid = 0;
570 	sc->pcn_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
571 	    RF_SHAREABLE | RF_ACTIVE);
572 
573 	if (sc->pcn_irq == NULL) {
574 		printf("pcn%d: couldn't map interrupt\n", unit);
575 		bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
576 		error = ENXIO;
577 		goto fail;
578 	}
579 
580 	error = bus_setup_intr(dev, sc->pcn_irq, INTR_TYPE_NET,
581 	    pcn_intr, sc, &sc->pcn_intrhand);
582 
583 	if (error) {
584 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_res);
585 		bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
586 		printf("pcn%d: couldn't set up irq\n", unit);
587 		goto fail;
588 	}
589 
590 	/* Reset the adapter. */
591 	pcn_reset(sc);
592 
593 	/*
594 	 * Get station address from the EEPROM.
595 	 */
596 	eaddr[0] = CSR_READ_4(sc, PCN_IO32_APROM00);
597 	eaddr[1] = CSR_READ_4(sc, PCN_IO32_APROM01);
598 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
599 
600 	/*
601 	 * An AMD chip was detected. Inform the world.
602 	 */
603 	printf("pcn%d: Ethernet address: %6D\n", unit,
604 	    sc->arpcom.ac_enaddr, ":");
605 
606 	sc->pcn_unit = unit;
607 	callout_handle_init(&sc->pcn_stat_ch);
608 
609 	sc->pcn_ldata = contigmalloc(sizeof(struct pcn_list_data), M_DEVBUF,
610 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
611 
612 	if (sc->pcn_ldata == NULL) {
613 		printf("pcn%d: no memory for list buffers!\n", unit);
614 		bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
615 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
616 		bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
617 		error = ENXIO;
618 		goto fail;
619 	}
620 	bzero(sc->pcn_ldata, sizeof(struct pcn_list_data));
621 
622 	ifp = &sc->arpcom.ac_if;
623 	ifp->if_softc = sc;
624 	if_initname(ifp, "pcn", unit);
625 	ifp->if_mtu = ETHERMTU;
626 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
627 	ifp->if_ioctl = pcn_ioctl;
628 	ifp->if_output = ether_output;
629 	ifp->if_start = pcn_start;
630 	ifp->if_watchdog = pcn_watchdog;
631 	ifp->if_init = pcn_init;
632 	ifp->if_baudrate = 10000000;
633 	ifp->if_snd.ifq_maxlen = PCN_TX_LIST_CNT - 1;
634 
635 	/*
636 	 * Do MII setup.
637 	 */
638 	if (mii_phy_probe(dev, &sc->pcn_miibus,
639 	    pcn_ifmedia_upd, pcn_ifmedia_sts)) {
640 		printf("pcn%d: MII without any PHY!\n", sc->pcn_unit);
641 		contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data),
642 		    M_DEVBUF);
643 		bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
644 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
645 		bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
646 		error = ENXIO;
647 		goto fail;
648 	}
649 
650 	/*
651 	 * Call MI attach routine.
652 	 */
653 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
654 	callout_handle_init(&sc->pcn_stat_ch);
655 
656 fail:
657 	splx(s);
658 	return(error);
659 }
660 
661 static int pcn_detach(dev)
662 	device_t		dev;
663 {
664 	struct pcn_softc	*sc;
665 	struct ifnet		*ifp;
666 	int			s;
667 
668 	s = splimp();
669 
670 	sc = device_get_softc(dev);
671 	ifp = &sc->arpcom.ac_if;
672 
673 	pcn_reset(sc);
674 	pcn_stop(sc);
675 	ether_ifdetach(ifp);
676 
677 	if (sc->pcn_miibus != NULL) {
678 		bus_generic_detach(dev);
679 		device_delete_child(dev, sc->pcn_miibus);
680 	}
681 
682 	bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand);
683 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq);
684 	bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res);
685 
686 	contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data), M_DEVBUF);
687 
688 	splx(s);
689 
690 	return(0);
691 }
692 
693 /*
694  * Initialize the transmit descriptors.
695  */
696 static int pcn_list_tx_init(sc)
697 	struct pcn_softc	*sc;
698 {
699 	struct pcn_list_data	*ld;
700 	struct pcn_ring_data	*cd;
701 	int			i;
702 
703 	cd = &sc->pcn_cdata;
704 	ld = sc->pcn_ldata;
705 
706 	for (i = 0; i < PCN_TX_LIST_CNT; i++) {
707 		cd->pcn_tx_chain[i] = NULL;
708 		ld->pcn_tx_list[i].pcn_tbaddr = 0;
709 		ld->pcn_tx_list[i].pcn_txctl = 0;
710 		ld->pcn_tx_list[i].pcn_txstat = 0;
711 	}
712 
713 	cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0;
714 
715 	return(0);
716 }
717 
718 
719 /*
720  * Initialize the RX descriptors and allocate mbufs for them.
721  */
722 static int pcn_list_rx_init(sc)
723 	struct pcn_softc	*sc;
724 {
725 	struct pcn_list_data	*ld;
726 	struct pcn_ring_data	*cd;
727 	int			i;
728 
729 	ld = sc->pcn_ldata;
730 	cd = &sc->pcn_cdata;
731 
732 	for (i = 0; i < PCN_RX_LIST_CNT; i++) {
733 		if (pcn_newbuf(sc, i, NULL) == ENOBUFS)
734 			return(ENOBUFS);
735 	}
736 
737 	cd->pcn_rx_prod = 0;
738 
739 	return(0);
740 }
741 
742 /*
743  * Initialize an RX descriptor and attach an MBUF cluster.
744  */
745 static int pcn_newbuf(sc, idx, m)
746 	struct pcn_softc	*sc;
747 	int			idx;
748 	struct mbuf		*m;
749 {
750 	struct mbuf		*m_new = NULL;
751 	struct pcn_rx_desc	*c;
752 
753 	c = &sc->pcn_ldata->pcn_rx_list[idx];
754 
755 	if (m == NULL) {
756 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
757 		if (m_new == NULL)
758 			return(ENOBUFS);
759 
760 		MCLGET(m_new, M_DONTWAIT);
761 		if (!(m_new->m_flags & M_EXT)) {
762 			m_freem(m_new);
763 			return(ENOBUFS);
764 		}
765 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
766 	} else {
767 		m_new = m;
768 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
769 		m_new->m_data = m_new->m_ext.ext_buf;
770 	}
771 
772 	m_adj(m_new, ETHER_ALIGN);
773 
774 	sc->pcn_cdata.pcn_rx_chain[idx] = m_new;
775 	c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t));
776 	c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ;
777 	c->pcn_bufsz |= PCN_RXLEN_MBO;
778 	c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN;
779 
780 	return(0);
781 }
782 
783 /*
784  * A frame has been uploaded: pass the resulting mbuf chain up to
785  * the higher level protocols.
786  */
787 static void pcn_rxeof(sc)
788 	struct pcn_softc	*sc;
789 {
790         struct ether_header	*eh;
791         struct mbuf		*m;
792         struct ifnet		*ifp;
793 	struct pcn_rx_desc	*cur_rx;
794 	int			i;
795 
796 	ifp = &sc->arpcom.ac_if;
797 	i = sc->pcn_cdata.pcn_rx_prod;
798 
799 	while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) {
800 		cur_rx = &sc->pcn_ldata->pcn_rx_list[i];
801 		m = sc->pcn_cdata.pcn_rx_chain[i];
802 		sc->pcn_cdata.pcn_rx_chain[i] = NULL;
803 
804 		/*
805 		 * If an error occurs, update stats, clear the
806 		 * status word and leave the mbuf cluster in place:
807 		 * it should simply get re-used next time this descriptor
808 	 	 * comes up in the ring.
809 		 */
810 		if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) {
811 			ifp->if_ierrors++;
812 			pcn_newbuf(sc, i, m);
813 			PCN_INC(i, PCN_RX_LIST_CNT);
814 			continue;
815 		}
816 
817 		if (pcn_newbuf(sc, i, NULL)) {
818 			/* Ran out of mbufs; recycle this one. */
819 			pcn_newbuf(sc, i, m);
820 			ifp->if_ierrors++;
821 			PCN_INC(i, PCN_RX_LIST_CNT);
822 			continue;
823 		}
824 
825 		PCN_INC(i, PCN_RX_LIST_CNT);
826 
827 		/* No errors; receive the packet. */
828 		ifp->if_ipackets++;
829 		eh = mtod(m, struct ether_header *);
830 		m->m_len = m->m_pkthdr.len =
831 		    cur_rx->pcn_rxlen - ETHER_CRC_LEN;
832 		m->m_pkthdr.rcvif = ifp;
833 
834 		/* Remove header from mbuf and pass it on. */
835 		m_adj(m, sizeof(struct ether_header));
836 		ether_input(ifp, eh, m);
837 	}
838 
839 	sc->pcn_cdata.pcn_rx_prod = i;
840 
841 	return;
842 }
843 
844 /*
845  * A frame was downloaded to the chip. It's safe for us to clean up
846  * the list buffers.
847  */
848 
849 static void pcn_txeof(sc)
850 	struct pcn_softc	*sc;
851 {
852 	struct pcn_tx_desc	*cur_tx = NULL;
853 	struct ifnet		*ifp;
854 	u_int32_t		idx;
855 
856 	ifp = &sc->arpcom.ac_if;
857 
858 	/*
859 	 * Go through our tx list and free mbufs for those
860 	 * frames that have been transmitted.
861 	 */
862 	idx = sc->pcn_cdata.pcn_tx_cons;
863 	while (idx != sc->pcn_cdata.pcn_tx_prod) {
864 		cur_tx = &sc->pcn_ldata->pcn_tx_list[idx];
865 
866 		if (!PCN_OWN_TXDESC(cur_tx))
867 			break;
868 
869 		if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) {
870 			sc->pcn_cdata.pcn_tx_cnt--;
871 			PCN_INC(idx, PCN_TX_LIST_CNT);
872 			continue;
873 		}
874 
875 		if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) {
876 			ifp->if_oerrors++;
877 			if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF)
878 				ifp->if_collisions++;
879 			if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY)
880 				ifp->if_collisions++;
881 		}
882 
883 		ifp->if_collisions +=
884 		    cur_tx->pcn_txstat & PCN_TXSTAT_TRC;
885 
886 		ifp->if_opackets++;
887 		if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) {
888 			m_freem(sc->pcn_cdata.pcn_tx_chain[idx]);
889 			sc->pcn_cdata.pcn_tx_chain[idx] = NULL;
890 		}
891 
892 		sc->pcn_cdata.pcn_tx_cnt--;
893 		PCN_INC(idx, PCN_TX_LIST_CNT);
894 	}
895 
896 	if (idx != sc->pcn_cdata.pcn_tx_cons) {
897 		/* Some buffers have been freed. */
898 		sc->pcn_cdata.pcn_tx_cons = idx;
899 		ifp->if_flags &= ~IFF_OACTIVE;
900 	}
901 	ifp->if_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5;
902 
903 	return;
904 }
905 
906 static void pcn_tick(xsc)
907 	void			*xsc;
908 {
909 	struct pcn_softc	*sc;
910 	struct mii_data		*mii;
911 	struct ifnet		*ifp;
912 	int			s;
913 
914 	s = splimp();
915 
916 	sc = xsc;
917 	ifp = &sc->arpcom.ac_if;
918 
919 	mii = device_get_softc(sc->pcn_miibus);
920 	mii_tick(mii);
921 
922 	if (sc->pcn_link & !(mii->mii_media_status & IFM_ACTIVE))
923 		sc->pcn_link = 0;
924 
925 	if (!sc->pcn_link) {
926 		mii_pollstat(mii);
927 		if (mii->mii_media_status & IFM_ACTIVE &&
928 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
929 			sc->pcn_link++;
930 			if (ifp->if_snd.ifq_head != NULL)
931 				pcn_start(ifp);
932 	}
933 
934 	sc->pcn_stat_ch = timeout(pcn_tick, sc, hz);
935 
936 	splx(s);
937 
938 	return;
939 }
940 
941 static void pcn_intr(arg)
942 	void			*arg;
943 {
944 	struct pcn_softc	*sc;
945 	struct ifnet		*ifp;
946 	u_int32_t		status;
947 
948 	sc = arg;
949 	ifp = &sc->arpcom.ac_if;
950 
951 	/* Supress unwanted interrupts */
952 	if (!(ifp->if_flags & IFF_UP)) {
953 		pcn_stop(sc);
954 		return;
955 	}
956 
957 	CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR);
958 
959 	while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) {
960 		CSR_WRITE_4(sc, PCN_IO32_RDP, status);
961 
962 		if (status & PCN_CSR_RINT)
963 			pcn_rxeof(sc);
964 
965 		if (status & PCN_CSR_TINT)
966 			pcn_txeof(sc);
967 
968 		if (status & PCN_CSR_ERR) {
969 			pcn_init(sc);
970 			break;
971 		}
972 	}
973 
974 	if (ifp->if_snd.ifq_head != NULL)
975 		pcn_start(ifp);
976 
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  */
984 static int pcn_encap(sc, m_head, txidx)
985 	struct pcn_softc	*sc;
986 	struct mbuf		*m_head;
987 	u_int32_t		*txidx;
988 {
989 	struct pcn_tx_desc	*f = NULL;
990 	struct mbuf		*m;
991 	int			frag, cur, cnt = 0;
992 
993 	/*
994  	 * Start packing the mbufs in this chain into
995 	 * the fragment pointers. Stop when we run out
996  	 * of fragments or hit the end of the mbuf chain.
997 	 */
998 	m = m_head;
999 	cur = frag = *txidx;
1000 
1001 	for (m = m_head; m != NULL; m = m->m_next) {
1002 		if (m->m_len != 0) {
1003 			if ((PCN_TX_LIST_CNT -
1004 			    (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2)
1005 				return(ENOBUFS);
1006 			f = &sc->pcn_ldata->pcn_tx_list[frag];
1007 			f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ;
1008 			f->pcn_txctl |= PCN_TXCTL_MBO;
1009 			f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t));
1010 			if (cnt == 0)
1011 				f->pcn_txctl |= PCN_TXCTL_STP;
1012 			else
1013 				f->pcn_txctl |= PCN_TXCTL_OWN;
1014 			cur = frag;
1015 			PCN_INC(frag, PCN_TX_LIST_CNT);
1016 			cnt++;
1017 		}
1018 	}
1019 
1020 	if (m != NULL)
1021 		return(ENOBUFS);
1022 
1023 	sc->pcn_cdata.pcn_tx_chain[cur] = m_head;
1024 	sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |=
1025 	    PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT;
1026 	sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN;
1027 	sc->pcn_cdata.pcn_tx_cnt += cnt;
1028 	*txidx = frag;
1029 
1030 	return(0);
1031 }
1032 
1033 /*
1034  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1035  * to the mbuf data regions directly in the transmit lists. We also save a
1036  * copy of the pointers since the transmit list fragment pointers are
1037  * physical addresses.
1038  */
1039 static void pcn_start(ifp)
1040 	struct ifnet		*ifp;
1041 {
1042 	struct pcn_softc	*sc;
1043 	struct mbuf		*m_head = NULL;
1044 	u_int32_t		idx;
1045 
1046 	sc = ifp->if_softc;
1047 
1048 	if (!sc->pcn_link)
1049 		return;
1050 
1051 	idx = sc->pcn_cdata.pcn_tx_prod;
1052 
1053 	if (ifp->if_flags & IFF_OACTIVE)
1054 		return;
1055 
1056 	while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) {
1057 		IF_DEQUEUE(&ifp->if_snd, m_head);
1058 		if (m_head == NULL)
1059 			break;
1060 
1061 		if (pcn_encap(sc, m_head, &idx)) {
1062 			IF_PREPEND(&ifp->if_snd, m_head);
1063 			ifp->if_flags |= IFF_OACTIVE;
1064 			break;
1065 		}
1066 
1067 		/*
1068 		 * If there's a BPF listener, bounce a copy of this frame
1069 		 * to him.
1070 		 */
1071 		if (ifp->if_bpf)
1072 			bpf_mtap(ifp, m_head);
1073 
1074 	}
1075 
1076 	/* Transmit */
1077 	sc->pcn_cdata.pcn_tx_prod = idx;
1078 	pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN);
1079 
1080 	/*
1081 	 * Set a timeout in case the chip goes out to lunch.
1082 	 */
1083 	ifp->if_timer = 5;
1084 
1085 	return;
1086 }
1087 
1088 void pcn_setfilt(ifp)
1089 	struct ifnet		*ifp;
1090 {
1091 	struct pcn_softc	*sc;
1092 
1093 	sc = ifp->if_softc;
1094 
1095 	/* If we want promiscuous mode, set the allframes bit. */
1096 	if (ifp->if_flags & IFF_PROMISC) {
1097 		PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1098 	} else {
1099 		PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC);
1100 	}
1101 
1102 	/* Set the capture broadcast bit to capture broadcast frames. */
1103 	if (ifp->if_flags & IFF_BROADCAST) {
1104 		PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1105 	} else {
1106 		PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD);
1107 	}
1108 
1109 	return;
1110 }
1111 
1112 static void pcn_init(xsc)
1113 	void			*xsc;
1114 {
1115 	struct pcn_softc	*sc = xsc;
1116 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1117 	struct mii_data		*mii = NULL;
1118 	int			s;
1119 
1120 	s = splimp();
1121 
1122 	/*
1123 	 * Cancel pending I/O and free all RX/TX buffers.
1124 	 */
1125 	pcn_stop(sc);
1126 	pcn_reset(sc);
1127 
1128 	mii = device_get_softc(sc->pcn_miibus);
1129 
1130 	/* Set MAC address */
1131 	pcn_csr_write(sc, PCN_CSR_PAR0,
1132 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1133 	pcn_csr_write(sc, PCN_CSR_PAR1,
1134 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1135 	pcn_csr_write(sc, PCN_CSR_PAR2,
1136 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1137 
1138 	/* Init circular RX list. */
1139 	if (pcn_list_rx_init(sc) == ENOBUFS) {
1140 		printf("pcn%d: initialization failed: no "
1141 		    "memory for rx buffers\n", sc->pcn_unit);
1142 		pcn_stop(sc);
1143 		(void)splx(s);
1144 		return;
1145 	}
1146 
1147 	/* Set up RX filter. */
1148 	pcn_setfilt(ifp);
1149 
1150 	/*
1151 	 * Init tx descriptors.
1152 	 */
1153 	pcn_list_tx_init(sc);
1154 
1155 	/* Set up the mode register. */
1156 	pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII);
1157 
1158 	/*
1159 	 * Load the multicast filter.
1160 	 */
1161 	pcn_setmulti(sc);
1162 
1163 	/*
1164 	 * Load the addresses of the RX and TX lists.
1165 	 */
1166 	pcn_csr_write(sc, PCN_CSR_RXADDR0,
1167 	    vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF);
1168 	pcn_csr_write(sc, PCN_CSR_RXADDR1,
1169 	    (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF);
1170 	pcn_csr_write(sc, PCN_CSR_TXADDR0,
1171 	    vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF);
1172 	pcn_csr_write(sc, PCN_CSR_TXADDR1,
1173 	    (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF);
1174 
1175 	/* Set the RX and TX ring sizes. */
1176 	pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1);
1177 	pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1);
1178 
1179 	/* We're not using the initialization block. */
1180 	pcn_csr_write(sc, PCN_CSR_IAB1, 0);
1181 
1182 	/* Enable fast suspend mode. */
1183 	PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE);
1184 
1185 	/*
1186 	 * Enable burst read and write. Also set the no underflow
1187 	 * bit. This will avoid transmit underruns in certain
1188 	 * conditions while still providing decent performance.
1189 	 */
1190 	PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW|
1191 	    PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE);
1192 
1193 	/* Enable graceful recovery from underflow. */
1194 	PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO);
1195 
1196 	/* Enable auto-padding of short TX frames. */
1197 	PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX);
1198 
1199 	/* Disable MII autoneg (we handle this ourselves). */
1200 	PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS);
1201 
1202 	if (sc->pcn_type == Am79C978)
1203 		pcn_bcr_write(sc, PCN_BCR_PHYSEL,
1204 		    PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA);
1205 
1206 	/* Enable interrupts and start the controller running. */
1207 	pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START);
1208 
1209 	mii_mediachg(mii);
1210 
1211 	ifp->if_flags |= IFF_RUNNING;
1212 	ifp->if_flags &= ~IFF_OACTIVE;
1213 
1214 	(void)splx(s);
1215 	sc->pcn_stat_ch = timeout(pcn_tick, sc, hz);
1216 
1217 	return;
1218 }
1219 
1220 /*
1221  * Set media options.
1222  */
1223 static int pcn_ifmedia_upd(ifp)
1224 	struct ifnet		*ifp;
1225 {
1226 	struct pcn_softc	*sc;
1227 	struct mii_data		*mii;
1228 
1229 	sc = ifp->if_softc;
1230 	mii = device_get_softc(sc->pcn_miibus);
1231 
1232 	sc->pcn_link = 0;
1233 	if (mii->mii_instance) {
1234 		struct mii_softc        *miisc;
1235 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1236 		    miisc = LIST_NEXT(miisc, mii_list))
1237 			mii_phy_reset(miisc);
1238 	}
1239 	mii_mediachg(mii);
1240 
1241 	return(0);
1242 }
1243 
1244 /*
1245  * Report current media status.
1246  */
1247 static void pcn_ifmedia_sts(ifp, ifmr)
1248 	struct ifnet		*ifp;
1249 	struct ifmediareq	*ifmr;
1250 {
1251 	struct pcn_softc	*sc;
1252 	struct mii_data		*mii;
1253 
1254 	sc = ifp->if_softc;
1255 
1256 	mii = device_get_softc(sc->pcn_miibus);
1257 	mii_pollstat(mii);
1258 	ifmr->ifm_active = mii->mii_media_active;
1259 	ifmr->ifm_status = mii->mii_media_status;
1260 
1261 	return;
1262 }
1263 
1264 static int pcn_ioctl(ifp, command, data, cr)
1265 	struct ifnet		*ifp;
1266 	u_long			command;
1267 	caddr_t			data;
1268 	struct ucred		*cr;
1269 {
1270 	struct pcn_softc	*sc = ifp->if_softc;
1271 	struct ifreq		*ifr = (struct ifreq *) data;
1272 	struct mii_data		*mii = NULL;
1273 	int			s, error = 0;
1274 
1275 	s = splimp();
1276 
1277 	switch(command) {
1278 	case SIOCSIFADDR:
1279 	case SIOCGIFADDR:
1280 	case SIOCSIFMTU:
1281 		error = ether_ioctl(ifp, command, data);
1282 		break;
1283 	case SIOCSIFFLAGS:
1284 		if (ifp->if_flags & IFF_UP) {
1285                         if (ifp->if_flags & IFF_RUNNING &&
1286 			    ifp->if_flags & IFF_PROMISC &&
1287 			    !(sc->pcn_if_flags & IFF_PROMISC)) {
1288 				PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1289 				    PCN_EXTCTL1_SPND);
1290 				pcn_setfilt(ifp);
1291 				PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1292 				    PCN_EXTCTL1_SPND);
1293 				pcn_csr_write(sc, PCN_CSR_CSR,
1294 				    PCN_CSR_INTEN|PCN_CSR_START);
1295 			} else if (ifp->if_flags & IFF_RUNNING &&
1296 			    !(ifp->if_flags & IFF_PROMISC) &&
1297 				sc->pcn_if_flags & IFF_PROMISC) {
1298 				PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1,
1299 				    PCN_EXTCTL1_SPND);
1300 				pcn_setfilt(ifp);
1301 				PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1,
1302 				    PCN_EXTCTL1_SPND);
1303 				pcn_csr_write(sc, PCN_CSR_CSR,
1304 				    PCN_CSR_INTEN|PCN_CSR_START);
1305 			} else if (!(ifp->if_flags & IFF_RUNNING))
1306 				pcn_init(sc);
1307 		} else {
1308 			if (ifp->if_flags & IFF_RUNNING)
1309 				pcn_stop(sc);
1310 		}
1311 		sc->pcn_if_flags = ifp->if_flags;
1312 		error = 0;
1313 		break;
1314 	case SIOCADDMULTI:
1315 	case SIOCDELMULTI:
1316 		pcn_setmulti(sc);
1317 		error = 0;
1318 		break;
1319 	case SIOCGIFMEDIA:
1320 	case SIOCSIFMEDIA:
1321 		mii = device_get_softc(sc->pcn_miibus);
1322 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1323 		break;
1324 	default:
1325 		error = EINVAL;
1326 		break;
1327 	}
1328 
1329 	(void)splx(s);
1330 
1331 	return(error);
1332 }
1333 
1334 static void pcn_watchdog(ifp)
1335 	struct ifnet		*ifp;
1336 {
1337 	struct pcn_softc	*sc;
1338 
1339 	sc = ifp->if_softc;
1340 
1341 	ifp->if_oerrors++;
1342 	printf("pcn%d: watchdog timeout\n", sc->pcn_unit);
1343 
1344 	pcn_stop(sc);
1345 	pcn_reset(sc);
1346 	pcn_init(sc);
1347 
1348 	if (ifp->if_snd.ifq_head != NULL)
1349 		pcn_start(ifp);
1350 
1351 	return;
1352 }
1353 
1354 /*
1355  * Stop the adapter and free any mbufs allocated to the
1356  * RX and TX lists.
1357  */
1358 static void pcn_stop(sc)
1359 	struct pcn_softc	*sc;
1360 {
1361 	int		i;
1362 	struct ifnet		*ifp;
1363 
1364 	ifp = &sc->arpcom.ac_if;
1365 	ifp->if_timer = 0;
1366 
1367 	untimeout(pcn_tick, sc, sc->pcn_stat_ch);
1368 	PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP);
1369 	sc->pcn_link = 0;
1370 
1371 	/*
1372 	 * Free data in the RX lists.
1373 	 */
1374 	for (i = 0; i < PCN_RX_LIST_CNT; i++) {
1375 		if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) {
1376 			m_freem(sc->pcn_cdata.pcn_rx_chain[i]);
1377 			sc->pcn_cdata.pcn_rx_chain[i] = NULL;
1378 		}
1379 	}
1380 	bzero((char *)&sc->pcn_ldata->pcn_rx_list,
1381 		sizeof(sc->pcn_ldata->pcn_rx_list));
1382 
1383 	/*
1384 	 * Free the TX list buffers.
1385 	 */
1386 	for (i = 0; i < PCN_TX_LIST_CNT; i++) {
1387 		if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) {
1388 			m_freem(sc->pcn_cdata.pcn_tx_chain[i]);
1389 			sc->pcn_cdata.pcn_tx_chain[i] = NULL;
1390 		}
1391 	}
1392 
1393 	bzero((char *)&sc->pcn_ldata->pcn_tx_list,
1394 		sizeof(sc->pcn_ldata->pcn_tx_list));
1395 
1396 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1397 
1398 	return;
1399 }
1400 
1401 /*
1402  * Stop all chip I/O so that the kernel's probe routines don't
1403  * get confused by errant DMAs when rebooting.
1404  */
1405 static void pcn_shutdown(dev)
1406 	device_t		dev;
1407 {
1408 	struct pcn_softc	*sc;
1409 
1410 	sc = device_get_softc(dev);
1411 
1412 	pcn_reset(sc);
1413 	pcn_stop(sc);
1414 
1415 	return;
1416 }
1417