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