xref: /netbsd-src/sys/dev/pci/if_pcn.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: if_pcn.c,v 1.24 2004/10/30 18:09:22 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Device driver for the AMD PCnet-PCI series of Ethernet
40  * chips:
41  *
42  *	* Am79c970 PCnet-PCI Single-Chip Ethernet Controller for PCI
43  *	  Local Bus
44  *
45  *	* Am79c970A PCnet-PCI II Single-Chip Full-Duplex Ethernet Controller
46  *	  for PCI Local Bus
47  *
48  *	* Am79c971 PCnet-FAST Single-Chip Full-Duplex 10/100Mbps
49  *	  Ethernet Controller for PCI Local Bus
50  *
51  *	* Am79c972 PCnet-FAST+ Enhanced 10/100Mbps PCI Ethernet Controller
52  *	  with OnNow Support
53  *
54  *	* Am79c973/Am79c975 PCnet-FAST III Single-Chip 10/100Mbps PCI
55  *	  Ethernet Controller with Integrated PHY
56  *
57  * This also supports the virtual PCnet-PCI Ethernet interface found
58  * in VMware.
59  *
60  * TODO:
61  *
62  *	* Split this into bus-specific and bus-independent portions.
63  *	  The core could also be used for the ILACC (Am79900) 32-bit
64  *	  Ethernet chip (XXX only if we use an ILACC-compatible SWSTYLE).
65  */
66 
67 #include "opt_pcn.h"
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: if_pcn.c,v 1.24 2004/10/30 18:09:22 thorpej Exp $");
71 
72 #include "bpfilter.h"
73 #include "rnd.h"
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/callout.h>
78 #include <sys/mbuf.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/errno.h>
84 #include <sys/device.h>
85 #include <sys/queue.h>
86 
87 #if NRND > 0
88 #include <sys/rnd.h>
89 #endif
90 
91 #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
92 
93 #include <net/if.h>
94 #include <net/if_dl.h>
95 #include <net/if_media.h>
96 #include <net/if_ether.h>
97 
98 #if NBPFILTER > 0
99 #include <net/bpf.h>
100 #endif
101 
102 #include <machine/bus.h>
103 #include <machine/intr.h>
104 #include <machine/endian.h>
105 
106 #include <dev/mii/mii.h>
107 #include <dev/mii/miivar.h>
108 
109 #include <dev/ic/am79900reg.h>
110 #include <dev/ic/lancereg.h>
111 
112 #include <dev/pci/pcireg.h>
113 #include <dev/pci/pcivar.h>
114 #include <dev/pci/pcidevs.h>
115 
116 #include <dev/pci/if_pcnreg.h>
117 
118 /*
119  * Transmit descriptor list size.  This is arbitrary, but allocate
120  * enough descriptors for 128 pending transmissions, and 4 segments
121  * per packet.  This MUST work out to a power of 2.
122  *
123  * NOTE: We can't have any more than 512 Tx descriptors, SO BE CAREFUL!
124  *
125  * So we play a little trick here.  We give each packet up to 16
126  * DMA segments, but only allocate the max of 512 descriptors.  The
127  * transmit logic can deal with this, we just are hoping to sneak by.
128  */
129 #define	PCN_NTXSEGS		16
130 
131 #define	PCN_TXQUEUELEN		128
132 #define	PCN_TXQUEUELEN_MASK	(PCN_TXQUEUELEN - 1)
133 #define	PCN_NTXDESC		512
134 #define	PCN_NTXDESC_MASK	(PCN_NTXDESC - 1)
135 #define	PCN_NEXTTX(x)		(((x) + 1) & PCN_NTXDESC_MASK)
136 #define	PCN_NEXTTXS(x)		(((x) + 1) & PCN_TXQUEUELEN_MASK)
137 
138 /* Tx interrupt every N + 1 packets. */
139 #define	PCN_TXINTR_MASK		7
140 
141 /*
142  * Receive descriptor list size.  We have one Rx buffer per incoming
143  * packet, so this logic is a little simpler.
144  */
145 #define	PCN_NRXDESC		128
146 #define	PCN_NRXDESC_MASK	(PCN_NRXDESC - 1)
147 #define	PCN_NEXTRX(x)		(((x) + 1) & PCN_NRXDESC_MASK)
148 
149 /*
150  * Control structures are DMA'd to the PCnet chip.  We allocate them in
151  * a single clump that maps to a single DMA segment to make several things
152  * easier.
153  */
154 struct pcn_control_data {
155 	/* The transmit descriptors. */
156 	struct letmd pcd_txdescs[PCN_NTXDESC];
157 
158 	/* The receive descriptors. */
159 	struct lermd pcd_rxdescs[PCN_NRXDESC];
160 
161 	/* The init block. */
162 	struct leinit pcd_initblock;
163 };
164 
165 #define	PCN_CDOFF(x)	offsetof(struct pcn_control_data, x)
166 #define	PCN_CDTXOFF(x)	PCN_CDOFF(pcd_txdescs[(x)])
167 #define	PCN_CDRXOFF(x)	PCN_CDOFF(pcd_rxdescs[(x)])
168 #define	PCN_CDINITOFF	PCN_CDOFF(pcd_initblock)
169 
170 /*
171  * Software state for transmit jobs.
172  */
173 struct pcn_txsoft {
174 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
175 	bus_dmamap_t txs_dmamap;	/* our DMA map */
176 	int txs_firstdesc;		/* first descriptor in packet */
177 	int txs_lastdesc;		/* last descriptor in packet */
178 };
179 
180 /*
181  * Software state for receive jobs.
182  */
183 struct pcn_rxsoft {
184 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
185 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
186 };
187 
188 /*
189  * Description of Rx FIFO watermarks for various revisions.
190  */
191 static const char * const pcn_79c970_rcvfw[] = {
192 	"16 bytes",
193 	"64 bytes",
194 	"128 bytes",
195 	NULL,
196 };
197 
198 static const char * const pcn_79c971_rcvfw[] = {
199 	"16 bytes",
200 	"64 bytes",
201 	"112 bytes",
202 	NULL,
203 };
204 
205 /*
206  * Description of Tx start points for various revisions.
207  */
208 static const char * const pcn_79c970_xmtsp[] = {
209 	"8 bytes",
210 	"64 bytes",
211 	"128 bytes",
212 	"248 bytes",
213 };
214 
215 static const char * const pcn_79c971_xmtsp[] = {
216 	"20 bytes",
217 	"64 bytes",
218 	"128 bytes",
219 	"248 bytes",
220 };
221 
222 static const char * const pcn_79c971_xmtsp_sram[] = {
223 	"44 bytes",
224 	"64 bytes",
225 	"128 bytes",
226 	"store-and-forward",
227 };
228 
229 /*
230  * Description of Tx FIFO watermarks for various revisions.
231  */
232 static const char * const pcn_79c970_xmtfw[] = {
233 	"16 bytes",
234 	"64 bytes",
235 	"128 bytes",
236 	NULL,
237 };
238 
239 static const char * const pcn_79c971_xmtfw[] = {
240 	"16 bytes",
241 	"64 bytes",
242 	"108 bytes",
243 	NULL,
244 };
245 
246 /*
247  * Software state per device.
248  */
249 struct pcn_softc {
250 	struct device sc_dev;		/* generic device information */
251 	bus_space_tag_t sc_st;		/* bus space tag */
252 	bus_space_handle_t sc_sh;	/* bus space handle */
253 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
254 	struct ethercom sc_ethercom;	/* Ethernet common data */
255 	void *sc_sdhook;		/* shutdown hook */
256 
257 	/* Points to our media routines, etc. */
258 	const struct pcn_variant *sc_variant;
259 
260 	void *sc_ih;			/* interrupt cookie */
261 
262 	struct mii_data sc_mii;		/* MII/media information */
263 
264 	struct callout sc_tick_ch;	/* tick callout */
265 
266 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
267 #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
268 
269 	/* Software state for transmit and receive descriptors. */
270 	struct pcn_txsoft sc_txsoft[PCN_TXQUEUELEN];
271 	struct pcn_rxsoft sc_rxsoft[PCN_NRXDESC];
272 
273 	/* Control data structures */
274 	struct pcn_control_data *sc_control_data;
275 #define	sc_txdescs	sc_control_data->pcd_txdescs
276 #define	sc_rxdescs	sc_control_data->pcd_rxdescs
277 #define	sc_initblock	sc_control_data->pcd_initblock
278 
279 #ifdef PCN_EVENT_COUNTERS
280 	/* Event counters. */
281 	struct evcnt sc_ev_txsstall;	/* Tx stalled due to no txs */
282 	struct evcnt sc_ev_txdstall;	/* Tx stalled due to no txd */
283 	struct evcnt sc_ev_txintr;	/* Tx interrupts */
284 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
285 	struct evcnt sc_ev_babl;	/* BABL in pcn_intr() */
286 	struct evcnt sc_ev_miss;	/* MISS in pcn_intr() */
287 	struct evcnt sc_ev_merr;	/* MERR in pcn_intr() */
288 
289 	struct evcnt sc_ev_txseg1;	/* Tx packets w/ 1 segment */
290 	struct evcnt sc_ev_txseg2;	/* Tx packets w/ 2 segments */
291 	struct evcnt sc_ev_txseg3;	/* Tx packets w/ 3 segments */
292 	struct evcnt sc_ev_txseg4;	/* Tx packets w/ 4 segments */
293 	struct evcnt sc_ev_txseg5;	/* Tx packets w/ 5 segments */
294 	struct evcnt sc_ev_txsegmore;	/* Tx packets w/ more than 5 segments */
295 	struct evcnt sc_ev_txcopy;	/* Tx copies required */
296 #endif /* PCN_EVENT_COUNTERS */
297 
298 	const char * const *sc_rcvfw_desc;	/* Rx FIFO watermark info */
299 	int sc_rcvfw;
300 
301 	const char * const *sc_xmtsp_desc;	/* Tx start point info */
302 	int sc_xmtsp;
303 
304 	const char * const *sc_xmtfw_desc;	/* Tx FIFO watermark info */
305 	int sc_xmtfw;
306 
307 	int sc_flags;			/* misc. flags; see below */
308 	int sc_swstyle;			/* the software style in use */
309 
310 	int sc_txfree;			/* number of free Tx descriptors */
311 	int sc_txnext;			/* next ready Tx descriptor */
312 
313 	int sc_txsfree;			/* number of free Tx jobs */
314 	int sc_txsnext;			/* next free Tx job */
315 	int sc_txsdirty;		/* dirty Tx jobs */
316 
317 	int sc_rxptr;			/* next ready Rx descriptor/job */
318 
319 	uint32_t sc_csr5;		/* prototype CSR5 register */
320 	uint32_t sc_mode;		/* prototype MODE register */
321 	int sc_phyaddr;			/* PHY address */
322 
323 #if NRND > 0
324 	rndsource_element_t rnd_source;	/* random source */
325 #endif
326 };
327 
328 /* sc_flags */
329 #define	PCN_F_HAS_MII		0x0001	/* has MII */
330 
331 #ifdef PCN_EVENT_COUNTERS
332 #define	PCN_EVCNT_INCR(ev)	(ev)->ev_count++
333 #else
334 #define	PCN_EVCNT_INCR(ev)	/* nothing */
335 #endif
336 
337 #define	PCN_CDTXADDR(sc, x)	((sc)->sc_cddma + PCN_CDTXOFF((x)))
338 #define	PCN_CDRXADDR(sc, x)	((sc)->sc_cddma + PCN_CDRXOFF((x)))
339 #define	PCN_CDINITADDR(sc)	((sc)->sc_cddma + PCN_CDINITOFF)
340 
341 #define	PCN_CDTXSYNC(sc, x, n, ops)					\
342 do {									\
343 	int __x, __n;							\
344 									\
345 	__x = (x);							\
346 	__n = (n);							\
347 									\
348 	/* If it will wrap around, sync to the end of the ring. */	\
349 	if ((__x + __n) > PCN_NTXDESC) {				\
350 		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
351 		    PCN_CDTXOFF(__x), sizeof(struct letmd) *		\
352 		    (PCN_NTXDESC - __x), (ops));			\
353 		__n -= (PCN_NTXDESC - __x);				\
354 		__x = 0;						\
355 	}								\
356 									\
357 	/* Now sync whatever is left. */				\
358 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
359 	    PCN_CDTXOFF(__x), sizeof(struct letmd) * __n, (ops));	\
360 } while (/*CONSTCOND*/0)
361 
362 #define	PCN_CDRXSYNC(sc, x, ops)					\
363 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
364 	    PCN_CDRXOFF((x)), sizeof(struct lermd), (ops))
365 
366 #define	PCN_CDINITSYNC(sc, ops)						\
367 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
368 	    PCN_CDINITOFF, sizeof(struct leinit), (ops))
369 
370 #define	PCN_INIT_RXDESC(sc, x)						\
371 do {									\
372 	struct pcn_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
373 	struct lermd *__rmd = &(sc)->sc_rxdescs[(x)];			\
374 	struct mbuf *__m = __rxs->rxs_mbuf;				\
375 									\
376 	/*								\
377 	 * Note: We scoot the packet forward 2 bytes in the buffer	\
378 	 * so that the payload after the Ethernet header is aligned	\
379 	 * to a 4-byte boundary.					\
380 	 */								\
381 	__m->m_data = __m->m_ext.ext_buf + 2;				\
382 									\
383 	if ((sc)->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) {		\
384 		__rmd->rmd2 =						\
385 		    htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2);	\
386 		__rmd->rmd0 = 0;					\
387 	} else {							\
388 		__rmd->rmd2 = 0;					\
389 		__rmd->rmd0 =						\
390 		    htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2);	\
391 	}								\
392 	__rmd->rmd1 = htole32(LE_R1_OWN|LE_R1_ONES| 			\
393 	    (LE_BCNT(MCLBYTES - 2) & LE_R1_BCNT_MASK));			\
394 	PCN_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);\
395 } while(/*CONSTCOND*/0)
396 
397 static void	pcn_start(struct ifnet *);
398 static void	pcn_watchdog(struct ifnet *);
399 static int	pcn_ioctl(struct ifnet *, u_long, caddr_t);
400 static int	pcn_init(struct ifnet *);
401 static void	pcn_stop(struct ifnet *, int);
402 
403 static void	pcn_shutdown(void *);
404 
405 static void	pcn_reset(struct pcn_softc *);
406 static void	pcn_rxdrain(struct pcn_softc *);
407 static int	pcn_add_rxbuf(struct pcn_softc *, int);
408 static void	pcn_tick(void *);
409 
410 static void	pcn_spnd(struct pcn_softc *);
411 
412 static void	pcn_set_filter(struct pcn_softc *);
413 
414 static int	pcn_intr(void *);
415 static void	pcn_txintr(struct pcn_softc *);
416 static int	pcn_rxintr(struct pcn_softc *);
417 
418 static int	pcn_mii_readreg(struct device *, int, int);
419 static void	pcn_mii_writereg(struct device *, int, int, int);
420 static void	pcn_mii_statchg(struct device *);
421 
422 static void	pcn_79c970_mediainit(struct pcn_softc *);
423 static int	pcn_79c970_mediachange(struct ifnet *);
424 static void	pcn_79c970_mediastatus(struct ifnet *, struct ifmediareq *);
425 
426 static void	pcn_79c971_mediainit(struct pcn_softc *);
427 static int	pcn_79c971_mediachange(struct ifnet *);
428 static void	pcn_79c971_mediastatus(struct ifnet *, struct ifmediareq *);
429 
430 /*
431  * Description of a PCnet-PCI variant.  Used to select media access
432  * method, mostly, and to print a nice description of the chip.
433  */
434 static const struct pcn_variant {
435 	const char *pcv_desc;
436 	void (*pcv_mediainit)(struct pcn_softc *);
437 	uint16_t pcv_chipid;
438 } pcn_variants[] = {
439 	{ "Am79c970 PCnet-PCI",
440 	  pcn_79c970_mediainit,
441 	  PARTID_Am79c970 },
442 
443 	{ "Am79c970A PCnet-PCI II",
444 	  pcn_79c970_mediainit,
445 	  PARTID_Am79c970A },
446 
447 	{ "Am79c971 PCnet-FAST",
448 	  pcn_79c971_mediainit,
449 	  PARTID_Am79c971 },
450 
451 	{ "Am79c972 PCnet-FAST+",
452 	  pcn_79c971_mediainit,
453 	  PARTID_Am79c972 },
454 
455 	{ "Am79c973 PCnet-FAST III",
456 	  pcn_79c971_mediainit,
457 	  PARTID_Am79c973 },
458 
459 	{ "Am79c975 PCnet-FAST III",
460 	  pcn_79c971_mediainit,
461 	  PARTID_Am79c975 },
462 
463 	{ "Unknown PCnet-PCI variant",
464 	  pcn_79c971_mediainit,
465 	  0 },
466 };
467 
468 int	pcn_copy_small = 0;
469 
470 static int	pcn_match(struct device *, struct cfdata *, void *);
471 static void	pcn_attach(struct device *, struct device *, void *);
472 
473 CFATTACH_DECL(pcn, sizeof(struct pcn_softc),
474     pcn_match, pcn_attach, NULL, NULL);
475 
476 /*
477  * Routines to read and write the PCnet-PCI CSR/BCR space.
478  */
479 
480 static __inline uint32_t
481 pcn_csr_read(struct pcn_softc *sc, int reg)
482 {
483 
484 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
485 	return (bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_RDP));
486 }
487 
488 static __inline void
489 pcn_csr_write(struct pcn_softc *sc, int reg, uint32_t val)
490 {
491 
492 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
493 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RDP, val);
494 }
495 
496 static __inline uint32_t
497 pcn_bcr_read(struct pcn_softc *sc, int reg)
498 {
499 
500 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
501 	return (bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_BDP));
502 }
503 
504 static __inline void
505 pcn_bcr_write(struct pcn_softc *sc, int reg, uint32_t val)
506 {
507 
508 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
509 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_BDP, val);
510 }
511 
512 static const struct pcn_variant *
513 pcn_lookup_variant(uint16_t chipid)
514 {
515 	const struct pcn_variant *pcv;
516 
517 	for (pcv = pcn_variants; pcv->pcv_chipid != 0; pcv++) {
518 		if (chipid == pcv->pcv_chipid)
519 			return (pcv);
520 	}
521 
522 	/*
523 	 * This covers unknown chips, which we simply treat like
524 	 * a generic PCnet-FAST.
525 	 */
526 	return (pcv);
527 }
528 
529 static int
530 pcn_match(struct device *parent, struct cfdata *cf, void *aux)
531 {
532 	struct pci_attach_args *pa = aux;
533 
534 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
535 		return (0);
536 
537 	switch (PCI_PRODUCT(pa->pa_id)) {
538 	case PCI_PRODUCT_AMD_PCNET_PCI:
539 		/* Beat if_le_pci.c */
540 		return (10);
541 	}
542 
543 	return (0);
544 }
545 
546 static void
547 pcn_attach(struct device *parent, struct device *self, void *aux)
548 {
549 	struct pcn_softc *sc = (struct pcn_softc *) self;
550 	struct pci_attach_args *pa = aux;
551 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
552 	pci_chipset_tag_t pc = pa->pa_pc;
553 	pci_intr_handle_t ih;
554 	const char *intrstr = NULL;
555 	bus_space_tag_t iot, memt;
556 	bus_space_handle_t ioh, memh;
557 	bus_dma_segment_t seg;
558 	int ioh_valid, memh_valid;
559 	int i, rseg, error;
560 	pcireg_t pmode;
561 	uint32_t chipid, reg;
562 	uint8_t enaddr[ETHER_ADDR_LEN];
563 	int pmreg;
564 
565 	callout_init(&sc->sc_tick_ch);
566 
567 	printf(": AMD PCnet-PCI Ethernet\n");
568 
569 	/*
570 	 * Map the device.
571 	 */
572 	ioh_valid = (pci_mapreg_map(pa, PCN_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
573 	    &iot, &ioh, NULL, NULL) == 0);
574 	memh_valid = (pci_mapreg_map(pa, PCN_PCI_CBMEM,
575 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
576 	    &memt, &memh, NULL, NULL) == 0);
577 
578 	if (memh_valid) {
579 		sc->sc_st = memt;
580 		sc->sc_sh = memh;
581 	} else if (ioh_valid) {
582 		sc->sc_st = iot;
583 		sc->sc_sh = ioh;
584 	} else {
585 		printf("%s: unable to map device registers\n",
586 		    sc->sc_dev.dv_xname);
587 		return;
588 	}
589 
590 	sc->sc_dmat = pa->pa_dmat;
591 
592 	/* Make sure bus mastering is enabled. */
593 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
594 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
595 	    PCI_COMMAND_MASTER_ENABLE);
596 
597 	/* Get it out of power save mode, if needed. */
598 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
599 		pmode = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR) &
600 		    PCI_PMCSR_STATE_MASK;
601 		if (pmode == PCI_PMCSR_STATE_D3) {
602 			/*
603 			 * The card has lost all configuration data in
604 			 * this state, so punt.
605 			 */
606 			printf("%s: unable to wake from power state D3\n",
607 			    sc->sc_dev.dv_xname);
608 			return;
609 		}
610 		if (pmode != PCI_PMCSR_STATE_D0) {
611 			printf("%s: waking up from power date D%d\n",
612 			    sc->sc_dev.dv_xname, pmode);
613 			pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
614 			    PCI_PMCSR_STATE_D0);
615 		}
616 	}
617 
618 	/*
619 	 * Reset the chip to a known state.  This also puts the
620 	 * chip into 32-bit mode.
621 	 */
622 	pcn_reset(sc);
623 
624 #if !defined(PCN_NO_PROM)
625 
626 	/*
627 	 * Read the Ethernet address from the EEPROM.
628 	 */
629 	for (i = 0; i < ETHER_ADDR_LEN; i++)
630 		enaddr[i] = bus_space_read_1(sc->sc_st, sc->sc_sh,
631 		    PCN32_APROM + i);
632 #else
633 	/*
634 	 * The PROM is not used; instead we assume that the MAC address
635 	 * has been programmed into the device's physical address
636 	 * registers by the boot firmware
637 	 */
638 
639         for (i=0; i < 3; i++) {
640 		uint32_t val;
641 		val = pcn_csr_read(sc, LE_CSR12 + i);
642 		enaddr[2*i] = val & 0x0ff;
643 		enaddr[2*i+1] = (val >> 8) & 0x0ff;
644 	}
645 #endif
646 
647 	/*
648 	 * Now that the device is mapped, attempt to figure out what
649 	 * kind of chip we have.  Note that IDL has all 32 bits of
650 	 * the chip ID when we're in 32-bit mode.
651 	 */
652 	chipid = pcn_csr_read(sc, LE_CSR88);
653 	sc->sc_variant = pcn_lookup_variant(CHIPID_PARTID(chipid));
654 
655 	printf("%s: %s rev %d, Ethernet address %s\n",
656 	    sc->sc_dev.dv_xname, sc->sc_variant->pcv_desc, CHIPID_VER(chipid),
657 	    ether_sprintf(enaddr));
658 
659 	/*
660 	 * Map and establish our interrupt.
661 	 */
662 	if (pci_intr_map(pa, &ih)) {
663 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
664 		return;
665 	}
666 	intrstr = pci_intr_string(pc, ih);
667 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, pcn_intr, sc);
668 	if (sc->sc_ih == NULL) {
669 		printf("%s: unable to establish interrupt",
670 		    sc->sc_dev.dv_xname);
671 		if (intrstr != NULL)
672 			printf(" at %s", intrstr);
673 		printf("\n");
674 		return;
675 	}
676 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
677 
678 	/*
679 	 * Allocate the control data structures, and create and load the
680 	 * DMA map for it.
681 	 */
682 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
683 	     sizeof(struct pcn_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
684 	     0)) != 0) {
685 		printf("%s: unable to allocate control data, error = %d\n",
686 		    sc->sc_dev.dv_xname, error);
687 		goto fail_0;
688 	}
689 
690 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
691 	     sizeof(struct pcn_control_data), (caddr_t *)&sc->sc_control_data,
692 	     BUS_DMA_COHERENT)) != 0) {
693 		printf("%s: unable to map control data, error = %d\n",
694 		    sc->sc_dev.dv_xname, error);
695 		goto fail_1;
696 	}
697 
698 	if ((error = bus_dmamap_create(sc->sc_dmat,
699 	     sizeof(struct pcn_control_data), 1,
700 	     sizeof(struct pcn_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
701 		printf("%s: unable to create control data DMA map, "
702 		    "error = %d\n", sc->sc_dev.dv_xname, error);
703 		goto fail_2;
704 	}
705 
706 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
707 	     sc->sc_control_data, sizeof(struct pcn_control_data), NULL,
708 	     0)) != 0) {
709 		printf("%s: unable to load control data DMA map, error = %d\n",
710 		    sc->sc_dev.dv_xname, error);
711 		goto fail_3;
712 	}
713 
714 	/* Create the transmit buffer DMA maps. */
715 	for (i = 0; i < PCN_TXQUEUELEN; i++) {
716 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
717 		     PCN_NTXSEGS, MCLBYTES, 0, 0,
718 		     &sc->sc_txsoft[i].txs_dmamap)) != 0) {
719 			printf("%s: unable to create tx DMA map %d, "
720 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
721 			goto fail_4;
722 		}
723 	}
724 
725 	/* Create the receive buffer DMA maps. */
726 	for (i = 0; i < PCN_NRXDESC; i++) {
727 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
728 		     MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
729 			printf("%s: unable to create rx DMA map %d, "
730 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
731 			goto fail_5;
732 		}
733 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
734 	}
735 
736 	/* Initialize our media structures. */
737 	(*sc->sc_variant->pcv_mediainit)(sc);
738 
739 	/*
740 	 * Initialize FIFO watermark info.
741 	 */
742 	switch (sc->sc_variant->pcv_chipid) {
743 	case PARTID_Am79c970:
744 	case PARTID_Am79c970A:
745 		sc->sc_rcvfw_desc = pcn_79c970_rcvfw;
746 		sc->sc_xmtsp_desc = pcn_79c970_xmtsp;
747 		sc->sc_xmtfw_desc = pcn_79c970_xmtfw;
748 		break;
749 
750 	default:
751 		sc->sc_rcvfw_desc = pcn_79c971_rcvfw;
752 		/*
753 		 * Read BCR25 to determine how much SRAM is
754 		 * on the board.  If > 0, then we the chip
755 		 * uses different Start Point thresholds.
756 		 *
757 		 * Note BCR25 and BCR26 are loaded from the
758 		 * EEPROM on RST, and unaffected by S_RESET,
759 		 * so we don't really have to worry about
760 		 * them except for this.
761 		 */
762 		reg = pcn_bcr_read(sc, LE_BCR25) & 0x00ff;
763 		if (reg != 0)
764 			sc->sc_xmtsp_desc = pcn_79c971_xmtsp_sram;
765 		else
766 			sc->sc_xmtsp_desc = pcn_79c971_xmtsp;
767 		sc->sc_xmtfw_desc = pcn_79c971_xmtfw;
768 		break;
769 	}
770 
771 	/*
772 	 * Set up defaults -- see the tables above for what these
773 	 * values mean.
774 	 *
775 	 * XXX How should we tune RCVFW and XMTFW?
776 	 */
777 	sc->sc_rcvfw = 1;	/* minimum for full-duplex */
778 	sc->sc_xmtsp = 1;
779 	sc->sc_xmtfw = 0;
780 
781 	ifp = &sc->sc_ethercom.ec_if;
782 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
783 	ifp->if_softc = sc;
784 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
785 	ifp->if_ioctl = pcn_ioctl;
786 	ifp->if_start = pcn_start;
787 	ifp->if_watchdog = pcn_watchdog;
788 	ifp->if_init = pcn_init;
789 	ifp->if_stop = pcn_stop;
790 	IFQ_SET_READY(&ifp->if_snd);
791 
792 	/* Attach the interface. */
793 	if_attach(ifp);
794 	ether_ifattach(ifp, enaddr);
795 #if NRND > 0
796 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
797 	    RND_TYPE_NET, 0);
798 #endif
799 
800 #ifdef PCN_EVENT_COUNTERS
801 	/* Attach event counters. */
802 	evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
803 	    NULL, sc->sc_dev.dv_xname, "txsstall");
804 	evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
805 	    NULL, sc->sc_dev.dv_xname, "txdstall");
806 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
807 	    NULL, sc->sc_dev.dv_xname, "txintr");
808 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
809 	    NULL, sc->sc_dev.dv_xname, "rxintr");
810 	evcnt_attach_dynamic(&sc->sc_ev_babl, EVCNT_TYPE_MISC,
811 	    NULL, sc->sc_dev.dv_xname, "babl");
812 	evcnt_attach_dynamic(&sc->sc_ev_miss, EVCNT_TYPE_MISC,
813 	    NULL, sc->sc_dev.dv_xname, "miss");
814 	evcnt_attach_dynamic(&sc->sc_ev_merr, EVCNT_TYPE_MISC,
815 	    NULL, sc->sc_dev.dv_xname, "merr");
816 
817 	evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC,
818 	    NULL, sc->sc_dev.dv_xname, "txseg1");
819 	evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC,
820 	    NULL, sc->sc_dev.dv_xname, "txseg2");
821 	evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC,
822 	    NULL, sc->sc_dev.dv_xname, "txseg3");
823 	evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC,
824 	    NULL, sc->sc_dev.dv_xname, "txseg4");
825 	evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC,
826 	    NULL, sc->sc_dev.dv_xname, "txseg5");
827 	evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC,
828 	    NULL, sc->sc_dev.dv_xname, "txsegmore");
829 	evcnt_attach_dynamic(&sc->sc_ev_txcopy, EVCNT_TYPE_MISC,
830 	    NULL, sc->sc_dev.dv_xname, "txcopy");
831 #endif /* PCN_EVENT_COUNTERS */
832 
833 	/* Make sure the interface is shutdown during reboot. */
834 	sc->sc_sdhook = shutdownhook_establish(pcn_shutdown, sc);
835 	if (sc->sc_sdhook == NULL)
836 		printf("%s: WARNING: unable to establish shutdown hook\n",
837 		    sc->sc_dev.dv_xname);
838 	return;
839 
840 	/*
841 	 * Free any resources we've allocated during the failed attach
842 	 * attempt.  Do this in reverse order and fall through.
843 	 */
844  fail_5:
845 	for (i = 0; i < PCN_NRXDESC; i++) {
846 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
847 			bus_dmamap_destroy(sc->sc_dmat,
848 			    sc->sc_rxsoft[i].rxs_dmamap);
849 	}
850  fail_4:
851 	for (i = 0; i < PCN_TXQUEUELEN; i++) {
852 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
853 			bus_dmamap_destroy(sc->sc_dmat,
854 			    sc->sc_txsoft[i].txs_dmamap);
855 	}
856 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
857  fail_3:
858 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
859  fail_2:
860 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
861 	    sizeof(struct pcn_control_data));
862  fail_1:
863 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
864  fail_0:
865 	return;
866 }
867 
868 /*
869  * pcn_shutdown:
870  *
871  *	Make sure the interface is stopped at reboot time.
872  */
873 static void
874 pcn_shutdown(void *arg)
875 {
876 	struct pcn_softc *sc = arg;
877 
878 	pcn_stop(&sc->sc_ethercom.ec_if, 1);
879 }
880 
881 /*
882  * pcn_start:		[ifnet interface function]
883  *
884  *	Start packet transmission on the interface.
885  */
886 static void
887 pcn_start(struct ifnet *ifp)
888 {
889 	struct pcn_softc *sc = ifp->if_softc;
890 	struct mbuf *m0, *m;
891 	struct pcn_txsoft *txs;
892 	bus_dmamap_t dmamap;
893 	int error, nexttx, lasttx = -1, ofree, seg;
894 
895 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
896 		return;
897 
898 	/*
899 	 * Remember the previous number of free descriptors and
900 	 * the first descriptor we'll use.
901 	 */
902 	ofree = sc->sc_txfree;
903 
904 	/*
905 	 * Loop through the send queue, setting up transmit descriptors
906 	 * until we drain the queue, or use up all available transmit
907 	 * descriptors.
908 	 */
909 	for (;;) {
910 		/* Grab a packet off the queue. */
911 		IFQ_POLL(&ifp->if_snd, m0);
912 		if (m0 == NULL)
913 			break;
914 		m = NULL;
915 
916 		/* Get a work queue entry. */
917 		if (sc->sc_txsfree == 0) {
918 			PCN_EVCNT_INCR(&sc->sc_ev_txsstall);
919 			break;
920 		}
921 
922 		txs = &sc->sc_txsoft[sc->sc_txsnext];
923 		dmamap = txs->txs_dmamap;
924 
925 		/*
926 		 * Load the DMA map.  If this fails, the packet either
927 		 * didn't fit in the alloted number of segments, or we
928 		 * were short on resources.  In this case, we'll copy
929 		 * and try again.
930 		 */
931 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
932 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
933 			PCN_EVCNT_INCR(&sc->sc_ev_txcopy);
934 			MGETHDR(m, M_DONTWAIT, MT_DATA);
935 			if (m == NULL) {
936 				printf("%s: unable to allocate Tx mbuf\n",
937 				    sc->sc_dev.dv_xname);
938 				break;
939 			}
940 			if (m0->m_pkthdr.len > MHLEN) {
941 				MCLGET(m, M_DONTWAIT);
942 				if ((m->m_flags & M_EXT) == 0) {
943 					printf("%s: unable to allocate Tx "
944 					    "cluster\n", sc->sc_dev.dv_xname);
945 					m_freem(m);
946 					break;
947 				}
948 			}
949 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
950 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
951 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
952 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
953 			if (error) {
954 				printf("%s: unable to load Tx buffer, "
955 				    "error = %d\n", sc->sc_dev.dv_xname, error);
956 				break;
957 			}
958 		}
959 
960 		/*
961 		 * Ensure we have enough descriptors free to describe
962 		 * the packet.  Note, we always reserve one descriptor
963 		 * at the end of the ring as a termination point, to
964 		 * prevent wrap-around.
965 		 */
966 		if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) {
967 			/*
968 			 * Not enough free descriptors to transmit this
969 			 * packet.  We haven't committed anything yet,
970 			 * so just unload the DMA map, put the packet
971 			 * back on the queue, and punt.  Notify the upper
972 			 * layer that there are not more slots left.
973 			 *
974 			 * XXX We could allocate an mbuf and copy, but
975 			 * XXX is it worth it?
976 			 */
977 			ifp->if_flags |= IFF_OACTIVE;
978 			bus_dmamap_unload(sc->sc_dmat, dmamap);
979 			if (m != NULL)
980 				m_freem(m);
981 			PCN_EVCNT_INCR(&sc->sc_ev_txdstall);
982 			break;
983 		}
984 
985 		IFQ_DEQUEUE(&ifp->if_snd, m0);
986 		if (m != NULL) {
987 			m_freem(m0);
988 			m0 = m;
989 		}
990 
991 		/*
992 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
993 		 */
994 
995 		/* Sync the DMA map. */
996 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
997 		    BUS_DMASYNC_PREWRITE);
998 
999 #ifdef PCN_EVENT_COUNTERS
1000 		switch (dmamap->dm_nsegs) {
1001 		case 1:
1002 			PCN_EVCNT_INCR(&sc->sc_ev_txseg1);
1003 			break;
1004 		case 2:
1005 			PCN_EVCNT_INCR(&sc->sc_ev_txseg2);
1006 			break;
1007 		case 3:
1008 			PCN_EVCNT_INCR(&sc->sc_ev_txseg3);
1009 			break;
1010 		case 4:
1011 			PCN_EVCNT_INCR(&sc->sc_ev_txseg4);
1012 			break;
1013 		case 5:
1014 			PCN_EVCNT_INCR(&sc->sc_ev_txseg5);
1015 			break;
1016 		default:
1017 			PCN_EVCNT_INCR(&sc->sc_ev_txsegmore);
1018 			break;
1019 		}
1020 #endif /* PCN_EVENT_COUNTERS */
1021 
1022 		/*
1023 		 * Initialize the transmit descriptors.
1024 		 */
1025 		if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) {
1026 			for (nexttx = sc->sc_txnext, seg = 0;
1027 			     seg < dmamap->dm_nsegs;
1028 			     seg++, nexttx = PCN_NEXTTX(nexttx)) {
1029 				/*
1030 				 * If this is the first descriptor we're
1031 				 * enqueueing, don't set the OWN bit just
1032 				 * yet.  That could cause a race condition.
1033 				 * We'll do it below.
1034 				 */
1035 				sc->sc_txdescs[nexttx].tmd0 = 0;
1036 				sc->sc_txdescs[nexttx].tmd2 =
1037 				    htole32(dmamap->dm_segs[seg].ds_addr);
1038 				sc->sc_txdescs[nexttx].tmd1 =
1039 				    htole32(LE_T1_ONES |
1040 				    (nexttx == sc->sc_txnext ? 0 : LE_T1_OWN) |
1041 				    (LE_BCNT(dmamap->dm_segs[seg].ds_len) &
1042 				     LE_T1_BCNT_MASK));
1043 				lasttx = nexttx;
1044 			}
1045 		} else {
1046 			for (nexttx = sc->sc_txnext, seg = 0;
1047 			     seg < dmamap->dm_nsegs;
1048 			     seg++, nexttx = PCN_NEXTTX(nexttx)) {
1049 				/*
1050 				 * If this is the first descriptor we're
1051 				 * enqueueing, don't set the OWN bit just
1052 				 * yet.  That could cause a race condition.
1053 				 * We'll do it below.
1054 				 */
1055 				sc->sc_txdescs[nexttx].tmd0 =
1056 				    htole32(dmamap->dm_segs[seg].ds_addr);
1057 				sc->sc_txdescs[nexttx].tmd2 = 0;
1058 				sc->sc_txdescs[nexttx].tmd1 =
1059 				    htole32(LE_T1_ONES |
1060 				    (nexttx == sc->sc_txnext ? 0 : LE_T1_OWN) |
1061 				    (LE_BCNT(dmamap->dm_segs[seg].ds_len) &
1062 				     LE_T1_BCNT_MASK));
1063 				lasttx = nexttx;
1064 			}
1065 		}
1066 
1067 		KASSERT(lasttx != -1);
1068 		/* Interrupt on the packet, if appropriate. */
1069 		if ((sc->sc_txsnext & PCN_TXINTR_MASK) == 0)
1070 			sc->sc_txdescs[lasttx].tmd1 |= htole32(LE_T1_LTINT);
1071 
1072 		/* Set `start of packet' and `end of packet' appropriately. */
1073 		sc->sc_txdescs[lasttx].tmd1 |= htole32(LE_T1_ENP);
1074 		sc->sc_txdescs[sc->sc_txnext].tmd1 |=
1075 		    htole32(LE_T1_OWN|LE_T1_STP);
1076 
1077 		/* Sync the descriptors we're using. */
1078 		PCN_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1079 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1080 
1081 		/* Kick the transmitter. */
1082 		pcn_csr_write(sc, LE_CSR0, LE_C0_INEA|LE_C0_TDMD);
1083 
1084 		/*
1085 		 * Store a pointer to the packet so we can free it later,
1086 		 * and remember what txdirty will be once the packet is
1087 		 * done.
1088 		 */
1089 		txs->txs_mbuf = m0;
1090 		txs->txs_firstdesc = sc->sc_txnext;
1091 		txs->txs_lastdesc = lasttx;
1092 
1093 		/* Advance the tx pointer. */
1094 		sc->sc_txfree -= dmamap->dm_nsegs;
1095 		sc->sc_txnext = nexttx;
1096 
1097 		sc->sc_txsfree--;
1098 		sc->sc_txsnext = PCN_NEXTTXS(sc->sc_txsnext);
1099 
1100 #if NBPFILTER > 0
1101 		/* Pass the packet to any BPF listeners. */
1102 		if (ifp->if_bpf)
1103 			bpf_mtap(ifp->if_bpf, m0);
1104 #endif /* NBPFILTER > 0 */
1105 	}
1106 
1107 	if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) {
1108 		/* No more slots left; notify upper layer. */
1109 		ifp->if_flags |= IFF_OACTIVE;
1110 	}
1111 
1112 	if (sc->sc_txfree != ofree) {
1113 		/* Set a watchdog timer in case the chip flakes out. */
1114 		ifp->if_timer = 5;
1115 	}
1116 }
1117 
1118 /*
1119  * pcn_watchdog:	[ifnet interface function]
1120  *
1121  *	Watchdog timer handler.
1122  */
1123 static void
1124 pcn_watchdog(struct ifnet *ifp)
1125 {
1126 	struct pcn_softc *sc = ifp->if_softc;
1127 
1128 	/*
1129 	 * Since we're not interrupting every packet, sweep
1130 	 * up before we report an error.
1131 	 */
1132 	pcn_txintr(sc);
1133 
1134 	if (sc->sc_txfree != PCN_NTXDESC) {
1135 		printf("%s: device timeout (txfree %d txsfree %d)\n",
1136 		    sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree);
1137 		ifp->if_oerrors++;
1138 
1139 		/* Reset the interface. */
1140 		(void) pcn_init(ifp);
1141 	}
1142 
1143 	/* Try to get more packets going. */
1144 	pcn_start(ifp);
1145 }
1146 
1147 /*
1148  * pcn_ioctl:		[ifnet interface function]
1149  *
1150  *	Handle control requests from the operator.
1151  */
1152 static int
1153 pcn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1154 {
1155 	struct pcn_softc *sc = ifp->if_softc;
1156 	struct ifreq *ifr = (struct ifreq *) data;
1157 	int s, error;
1158 
1159 	s = splnet();
1160 
1161 	switch (cmd) {
1162 	case SIOCSIFMEDIA:
1163 	case SIOCGIFMEDIA:
1164 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1165 		break;
1166 
1167 	default:
1168 		error = ether_ioctl(ifp, cmd, data);
1169 		if (error == ENETRESET) {
1170 			/*
1171 			 * Multicast list has changed; set the hardware filter
1172 			 * accordingly.
1173 			 */
1174 			if (ifp->if_flags & IFF_RUNNING)
1175 				error = pcn_init(ifp);
1176 			else
1177 				error = 0;
1178 		}
1179 		break;
1180 	}
1181 
1182 	/* Try to get more packets going. */
1183 	pcn_start(ifp);
1184 
1185 	splx(s);
1186 	return (error);
1187 }
1188 
1189 /*
1190  * pcn_intr:
1191  *
1192  *	Interrupt service routine.
1193  */
1194 static int
1195 pcn_intr(void *arg)
1196 {
1197 	struct pcn_softc *sc = arg;
1198 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1199 	uint32_t csr0;
1200 	int wantinit, handled = 0;
1201 
1202 	for (wantinit = 0; wantinit == 0;) {
1203 		csr0 = pcn_csr_read(sc, LE_CSR0);
1204 		if ((csr0 & LE_C0_INTR) == 0)
1205 			break;
1206 
1207 #if NRND > 0
1208 		if (RND_ENABLED(&sc->rnd_source))
1209 			rnd_add_uint32(&sc->rnd_source, csr0);
1210 #endif
1211 
1212 		/* ACK the bits and re-enable interrupts. */
1213 		pcn_csr_write(sc, LE_CSR0, csr0 &
1214 		    (LE_C0_INEA|LE_C0_BABL|LE_C0_MISS|LE_C0_MERR|LE_C0_RINT|
1215 		     LE_C0_TINT|LE_C0_IDON));
1216 
1217 		handled = 1;
1218 
1219 		if (csr0 & LE_C0_RINT) {
1220 			PCN_EVCNT_INCR(&sc->sc_ev_rxintr);
1221 			wantinit = pcn_rxintr(sc);
1222 		}
1223 
1224 		if (csr0 & LE_C0_TINT) {
1225 			PCN_EVCNT_INCR(&sc->sc_ev_txintr);
1226 			pcn_txintr(sc);
1227 		}
1228 
1229 		if (csr0 & LE_C0_ERR) {
1230 			if (csr0 & LE_C0_BABL) {
1231 				PCN_EVCNT_INCR(&sc->sc_ev_babl);
1232 				ifp->if_oerrors++;
1233 			}
1234 			if (csr0 & LE_C0_MISS) {
1235 				PCN_EVCNT_INCR(&sc->sc_ev_miss);
1236 				ifp->if_ierrors++;
1237 			}
1238 			if (csr0 & LE_C0_MERR) {
1239 				PCN_EVCNT_INCR(&sc->sc_ev_merr);
1240 				printf("%s: memory error\n",
1241 				    sc->sc_dev.dv_xname);
1242 				wantinit = 1;
1243 				break;
1244 			}
1245 		}
1246 
1247 		if ((csr0 & LE_C0_RXON) == 0) {
1248 			printf("%s: receiver disabled\n",
1249 			    sc->sc_dev.dv_xname);
1250 			ifp->if_ierrors++;
1251 			wantinit = 1;
1252 		}
1253 
1254 		if ((csr0 & LE_C0_TXON) == 0) {
1255 			printf("%s: transmitter disabled\n",
1256 			    sc->sc_dev.dv_xname);
1257 			ifp->if_oerrors++;
1258 			wantinit = 1;
1259 		}
1260 	}
1261 
1262 	if (handled) {
1263 		if (wantinit)
1264 			pcn_init(ifp);
1265 
1266 		/* Try to get more packets going. */
1267 		pcn_start(ifp);
1268 	}
1269 
1270 	return (handled);
1271 }
1272 
1273 /*
1274  * pcn_spnd:
1275  *
1276  *	Suspend the chip.
1277  */
1278 static void
1279 pcn_spnd(struct pcn_softc *sc)
1280 {
1281 	int i;
1282 
1283 	pcn_csr_write(sc, LE_CSR5, sc->sc_csr5 | LE_C5_SPND);
1284 
1285 	for (i = 0; i < 10000; i++) {
1286 		if (pcn_csr_read(sc, LE_CSR5) & LE_C5_SPND)
1287 			return;
1288 		delay(5);
1289 	}
1290 
1291 	printf("%s: WARNING: chip failed to enter suspended state\n",
1292 	    sc->sc_dev.dv_xname);
1293 }
1294 
1295 /*
1296  * pcn_txintr:
1297  *
1298  *	Helper; handle transmit interrupts.
1299  */
1300 static void
1301 pcn_txintr(struct pcn_softc *sc)
1302 {
1303 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1304 	struct pcn_txsoft *txs;
1305 	uint32_t tmd1, tmd2, tmd;
1306 	int i, j;
1307 
1308 	ifp->if_flags &= ~IFF_OACTIVE;
1309 
1310 	/*
1311 	 * Go through our Tx list and free mbufs for those
1312 	 * frames which have been transmitted.
1313 	 */
1314 	for (i = sc->sc_txsdirty; sc->sc_txsfree != PCN_TXQUEUELEN;
1315 	     i = PCN_NEXTTXS(i), sc->sc_txsfree++) {
1316 		txs = &sc->sc_txsoft[i];
1317 
1318 		PCN_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1319 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1320 
1321 		tmd1 = le32toh(sc->sc_txdescs[txs->txs_lastdesc].tmd1);
1322 		if (tmd1 & LE_T1_OWN)
1323 			break;
1324 
1325 		/*
1326 		 * Slightly annoying -- we have to loop through the
1327 		 * descriptors we've used looking for ERR, since it
1328 		 * can appear on any descriptor in the chain.
1329 		 */
1330 		for (j = txs->txs_firstdesc;; j = PCN_NEXTTX(j)) {
1331 			tmd = le32toh(sc->sc_txdescs[j].tmd1);
1332 			if (tmd & LE_T1_ERR) {
1333 				ifp->if_oerrors++;
1334 				if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3)
1335 					tmd2 = le32toh(sc->sc_txdescs[j].tmd0);
1336 				else
1337 					tmd2 = le32toh(sc->sc_txdescs[j].tmd2);
1338 				if (tmd2 & LE_T2_UFLO) {
1339 					if (sc->sc_xmtsp < LE_C80_XMTSP_MAX) {
1340 						sc->sc_xmtsp++;
1341 						printf("%s: transmit "
1342 						    "underrun; new threshold: "
1343 						    "%s\n",
1344 						    sc->sc_dev.dv_xname,
1345 						    sc->sc_xmtsp_desc[
1346 						    sc->sc_xmtsp]);
1347 						pcn_spnd(sc);
1348 						pcn_csr_write(sc, LE_CSR80,
1349 						    LE_C80_RCVFW(sc->sc_rcvfw) |
1350 						    LE_C80_XMTSP(sc->sc_xmtsp) |
1351 						    LE_C80_XMTFW(sc->sc_xmtfw));
1352 						pcn_csr_write(sc, LE_CSR5,
1353 						    sc->sc_csr5);
1354 					} else {
1355 						printf("%s: transmit "
1356 						    "underrun\n",
1357 						    sc->sc_dev.dv_xname);
1358 					}
1359 				} else if (tmd2 & LE_T2_BUFF) {
1360 					printf("%s: transmit buffer error\n",
1361 					    sc->sc_dev.dv_xname);
1362 				}
1363 				if (tmd2 & LE_T2_LCOL)
1364 					ifp->if_collisions++;
1365 				if (tmd2 & LE_T2_RTRY)
1366 					ifp->if_collisions += 16;
1367 				goto next_packet;
1368 			}
1369 			if (j == txs->txs_lastdesc)
1370 				break;
1371 		}
1372 		if (tmd1 & LE_T1_ONE)
1373 			ifp->if_collisions++;
1374 		else if (tmd & LE_T1_MORE) {
1375 			/* Real number is unknown. */
1376 			ifp->if_collisions += 2;
1377 		}
1378 		ifp->if_opackets++;
1379  next_packet:
1380 		sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1381 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1382 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1383 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1384 		m_freem(txs->txs_mbuf);
1385 		txs->txs_mbuf = NULL;
1386 	}
1387 
1388 	/* Update the dirty transmit buffer pointer. */
1389 	sc->sc_txsdirty = i;
1390 
1391 	/*
1392 	 * If there are no more pending transmissions, cancel the watchdog
1393 	 * timer.
1394 	 */
1395 	if (sc->sc_txsfree == PCN_TXQUEUELEN)
1396 		ifp->if_timer = 0;
1397 }
1398 
1399 /*
1400  * pcn_rxintr:
1401  *
1402  *	Helper; handle receive interrupts.
1403  */
1404 static int
1405 pcn_rxintr(struct pcn_softc *sc)
1406 {
1407 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1408 	struct pcn_rxsoft *rxs;
1409 	struct mbuf *m;
1410 	uint32_t rmd1;
1411 	int i, len;
1412 
1413 	for (i = sc->sc_rxptr;; i = PCN_NEXTRX(i)) {
1414 		rxs = &sc->sc_rxsoft[i];
1415 
1416 		PCN_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1417 
1418 		rmd1 = le32toh(sc->sc_rxdescs[i].rmd1);
1419 
1420 		if (rmd1 & LE_R1_OWN)
1421 			break;
1422 
1423 		/*
1424 		 * Check for errors and make sure the packet fit into
1425 		 * a single buffer.  We have structured this block of
1426 		 * code the way it is in order to compress it into
1427 		 * one test in the common case (no error).
1428 		 */
1429 		if (__predict_false((rmd1 & (LE_R1_STP|LE_R1_ENP|LE_R1_ERR)) !=
1430 		    (LE_R1_STP|LE_R1_ENP))) {
1431 			/* Make sure the packet is in a single buffer. */
1432 			if ((rmd1 & (LE_R1_STP|LE_R1_ENP)) !=
1433 			    (LE_R1_STP|LE_R1_ENP)) {
1434 				printf("%s: packet spilled into next buffer\n",
1435 				    sc->sc_dev.dv_xname);
1436 				return (1);	/* pcn_intr() will re-init */
1437 			}
1438 
1439 			/*
1440 			 * If the packet had an error, simple recycle the
1441 			 * buffer.
1442 			 */
1443 			if (rmd1 & LE_R1_ERR) {
1444 				ifp->if_ierrors++;
1445 				/*
1446 				 * If we got an overflow error, chances
1447 				 * are there will be a CRC error.  In
1448 				 * this case, just print the overflow
1449 				 * error, and skip the others.
1450 				 */
1451 				if (rmd1 & LE_R1_OFLO)
1452 					printf("%s: overflow error\n",
1453 					    sc->sc_dev.dv_xname);
1454 				else {
1455 #define	PRINTIT(x, str)							\
1456 					if (rmd1 & (x))			\
1457 						printf("%s: %s\n",	\
1458 						    sc->sc_dev.dv_xname, str);
1459 					PRINTIT(LE_R1_FRAM, "framing error");
1460 					PRINTIT(LE_R1_CRC, "CRC error");
1461 					PRINTIT(LE_R1_BUFF, "buffer error");
1462 				}
1463 #undef PRINTIT
1464 				PCN_INIT_RXDESC(sc, i);
1465 				continue;
1466 			}
1467 		}
1468 
1469 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1470 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1471 
1472 		/*
1473 		 * No errors; receive the packet.
1474 		 */
1475 		if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3)
1476 			len = le32toh(sc->sc_rxdescs[i].rmd0) & LE_R1_BCNT_MASK;
1477 		else
1478 			len = le32toh(sc->sc_rxdescs[i].rmd2) & LE_R1_BCNT_MASK;
1479 
1480 		/*
1481 		 * The LANCE family includes the CRC with every packet;
1482 		 * trim it off here.
1483 		 */
1484 		len -= ETHER_CRC_LEN;
1485 
1486 		/*
1487 		 * If the packet is small enough to fit in a
1488 		 * single header mbuf, allocate one and copy
1489 		 * the data into it.  This greatly reduces
1490 		 * memory consumption when we receive lots
1491 		 * of small packets.
1492 		 *
1493 		 * Otherwise, we add a new buffer to the receive
1494 		 * chain.  If this fails, we drop the packet and
1495 		 * recycle the old buffer.
1496 		 */
1497 		if (pcn_copy_small != 0 && len <= (MHLEN - 2)) {
1498 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1499 			if (m == NULL)
1500 				goto dropit;
1501 			m->m_data += 2;
1502 			memcpy(mtod(m, caddr_t),
1503 			    mtod(rxs->rxs_mbuf, caddr_t), len);
1504 			PCN_INIT_RXDESC(sc, i);
1505 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1506 			    rxs->rxs_dmamap->dm_mapsize,
1507 			    BUS_DMASYNC_PREREAD);
1508 		} else {
1509 			m = rxs->rxs_mbuf;
1510 			if (pcn_add_rxbuf(sc, i) != 0) {
1511  dropit:
1512 				ifp->if_ierrors++;
1513 				PCN_INIT_RXDESC(sc, i);
1514 				bus_dmamap_sync(sc->sc_dmat,
1515 				    rxs->rxs_dmamap, 0,
1516 				    rxs->rxs_dmamap->dm_mapsize,
1517 				    BUS_DMASYNC_PREREAD);
1518 				continue;
1519 			}
1520 		}
1521 
1522 		m->m_pkthdr.rcvif = ifp;
1523 		m->m_pkthdr.len = m->m_len = len;
1524 
1525 #if NBPFILTER > 0
1526 		/* Pass this up to any BPF listeners. */
1527 		if (ifp->if_bpf)
1528 			bpf_mtap(ifp->if_bpf, m);
1529 #endif /* NBPFILTER > 0 */
1530 
1531 		/* Pass it on. */
1532 		(*ifp->if_input)(ifp, m);
1533 		ifp->if_ipackets++;
1534 	}
1535 
1536 	/* Update the receive pointer. */
1537 	sc->sc_rxptr = i;
1538 	return (0);
1539 }
1540 
1541 /*
1542  * pcn_tick:
1543  *
1544  *	One second timer, used to tick the MII.
1545  */
1546 static void
1547 pcn_tick(void *arg)
1548 {
1549 	struct pcn_softc *sc = arg;
1550 	int s;
1551 
1552 	s = splnet();
1553 	mii_tick(&sc->sc_mii);
1554 	splx(s);
1555 
1556 	callout_reset(&sc->sc_tick_ch, hz, pcn_tick, sc);
1557 }
1558 
1559 /*
1560  * pcn_reset:
1561  *
1562  *	Perform a soft reset on the PCnet-PCI.
1563  */
1564 static void
1565 pcn_reset(struct pcn_softc *sc)
1566 {
1567 
1568 	/*
1569 	 * The PCnet-PCI chip is reset by reading from the
1570 	 * RESET register.  Note that while the NE2100 LANCE
1571 	 * boards require a write after the read, the PCnet-PCI
1572 	 * chips do not require this.
1573 	 *
1574 	 * Since we don't know if we're in 16-bit or 32-bit
1575 	 * mode right now, issue both (it's safe) in the
1576 	 * hopes that one will succeed.
1577 	 */
1578 	(void) bus_space_read_2(sc->sc_st, sc->sc_sh, PCN16_RESET);
1579 	(void) bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_RESET);
1580 
1581 	/* Wait 1ms for it to finish. */
1582 	delay(1000);
1583 
1584 	/*
1585 	 * Select 32-bit I/O mode by issuing a 32-bit write to the
1586 	 * RDP.  Since the RAP is 0 after a reset, writing a 0
1587 	 * to RDP is safe (since it simply clears CSR0).
1588 	 */
1589 	bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RDP, 0);
1590 }
1591 
1592 /*
1593  * pcn_init:		[ifnet interface function]
1594  *
1595  *	Initialize the interface.  Must be called at splnet().
1596  */
1597 static int
1598 pcn_init(struct ifnet *ifp)
1599 {
1600 	struct pcn_softc *sc = ifp->if_softc;
1601 	struct pcn_rxsoft *rxs;
1602 	uint8_t *enaddr = LLADDR(ifp->if_sadl);
1603 	int i, error = 0;
1604 	uint32_t reg;
1605 
1606 	/* Cancel any pending I/O. */
1607 	pcn_stop(ifp, 0);
1608 
1609 	/* Reset the chip to a known state. */
1610 	pcn_reset(sc);
1611 
1612 	/*
1613 	 * On the Am79c970, select SSTYLE 2, and SSTYLE 3 on everything
1614 	 * else.
1615 	 *
1616 	 * XXX It'd be really nice to use SSTYLE 2 on all the chips,
1617 	 * because the structure layout is compatible with ILACC,
1618 	 * but the burst mode is only available in SSTYLE 3, and
1619 	 * burst mode should provide some performance enhancement.
1620 	 */
1621 	if (sc->sc_variant->pcv_chipid == PARTID_Am79c970)
1622 		sc->sc_swstyle = LE_B20_SSTYLE_PCNETPCI2;
1623 	else
1624 		sc->sc_swstyle = LE_B20_SSTYLE_PCNETPCI3;
1625 	pcn_bcr_write(sc, LE_BCR20, sc->sc_swstyle);
1626 
1627 	/* Initialize the transmit descriptor ring. */
1628 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1629 	PCN_CDTXSYNC(sc, 0, PCN_NTXDESC,
1630 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1631 	sc->sc_txfree = PCN_NTXDESC;
1632 	sc->sc_txnext = 0;
1633 
1634 	/* Initialize the transmit job descriptors. */
1635 	for (i = 0; i < PCN_TXQUEUELEN; i++)
1636 		sc->sc_txsoft[i].txs_mbuf = NULL;
1637 	sc->sc_txsfree = PCN_TXQUEUELEN;
1638 	sc->sc_txsnext = 0;
1639 	sc->sc_txsdirty = 0;
1640 
1641 	/*
1642 	 * Initialize the receive descriptor and receive job
1643 	 * descriptor rings.
1644 	 */
1645 	for (i = 0; i < PCN_NRXDESC; i++) {
1646 		rxs = &sc->sc_rxsoft[i];
1647 		if (rxs->rxs_mbuf == NULL) {
1648 			if ((error = pcn_add_rxbuf(sc, i)) != 0) {
1649 				printf("%s: unable to allocate or map rx "
1650 				    "buffer %d, error = %d\n",
1651 				    sc->sc_dev.dv_xname, i, error);
1652 				/*
1653 				 * XXX Should attempt to run with fewer receive
1654 				 * XXX buffers instead of just failing.
1655 				 */
1656 				pcn_rxdrain(sc);
1657 				goto out;
1658 			}
1659 		} else
1660 			PCN_INIT_RXDESC(sc, i);
1661 	}
1662 	sc->sc_rxptr = 0;
1663 
1664 	/* Initialize MODE for the initialization block. */
1665 	sc->sc_mode = 0;
1666 	if (ifp->if_flags & IFF_PROMISC)
1667 		sc->sc_mode |= LE_C15_PROM;
1668 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1669 		sc->sc_mode |= LE_C15_DRCVBC;
1670 
1671 	/*
1672 	 * If we have MII, simply select MII in the MODE register,
1673 	 * and clear ASEL.  Otherwise, let ASEL stand (for now),
1674 	 * and leave PORTSEL alone (it is ignored with ASEL is set).
1675 	 */
1676 	if (sc->sc_flags & PCN_F_HAS_MII) {
1677 		pcn_bcr_write(sc, LE_BCR2,
1678 		    pcn_bcr_read(sc, LE_BCR2) & ~LE_B2_ASEL);
1679 		sc->sc_mode |= LE_C15_PORTSEL(PORTSEL_MII);
1680 
1681 		/*
1682 		 * Disable MII auto-negotiation.  We handle that in
1683 		 * our own MII layer.
1684 		 */
1685 		pcn_bcr_write(sc, LE_BCR32,
1686 		    pcn_bcr_read(sc, LE_BCR32) | LE_B32_DANAS);
1687 	}
1688 
1689 	/*
1690 	 * Set the Tx and Rx descriptor ring addresses in the init
1691 	 * block, the TLEN and RLEN other fields of the init block
1692 	 * MODE register.
1693 	 */
1694 	sc->sc_initblock.init_rdra = htole32(PCN_CDRXADDR(sc, 0));
1695 	sc->sc_initblock.init_tdra = htole32(PCN_CDTXADDR(sc, 0));
1696 	sc->sc_initblock.init_mode = htole32(sc->sc_mode |
1697 	    ((ffs(PCN_NTXDESC) - 1) << 28) |
1698 	    ((ffs(PCN_NRXDESC) - 1) << 20));
1699 
1700 	/* Set the station address in the init block. */
1701 	sc->sc_initblock.init_padr[0] = htole32(enaddr[0] |
1702 	    (enaddr[1] << 8) | (enaddr[2] << 16) | (enaddr[3] << 24));
1703 	sc->sc_initblock.init_padr[1] = htole32(enaddr[4] |
1704 	    (enaddr[5] << 8));
1705 
1706 	/* Set the multicast filter in the init block. */
1707 	pcn_set_filter(sc);
1708 
1709 	/* Initialize CSR3. */
1710 	pcn_csr_write(sc, LE_CSR3, LE_C3_MISSM|LE_C3_IDONM|LE_C3_DXSUFLO);
1711 
1712 	/* Initialize CSR4. */
1713 	pcn_csr_write(sc, LE_CSR4, LE_C4_DMAPLUS|LE_C4_APAD_XMT|
1714 	    LE_C4_MFCOM|LE_C4_RCVCCOM|LE_C4_TXSTRTM);
1715 
1716 	/* Initialize CSR5. */
1717 	sc->sc_csr5 = LE_C5_LTINTEN|LE_C5_SINTE;
1718 	pcn_csr_write(sc, LE_CSR5, sc->sc_csr5);
1719 
1720 	/*
1721 	 * If we have an Am79c971 or greater, initialize CSR7.
1722 	 *
1723 	 * XXX Might be nice to use the MII auto-poll interrupt someday.
1724 	 */
1725 	switch (sc->sc_variant->pcv_chipid) {
1726 	case PARTID_Am79c970:
1727 	case PARTID_Am79c970A:
1728 		/* Not available on these chips. */
1729 		break;
1730 
1731 	default:
1732 		pcn_csr_write(sc, LE_CSR7, LE_C7_FASTSPNDE);
1733 		break;
1734 	}
1735 
1736 	/*
1737 	 * On the Am79c970A and greater, initialize BCR18 to
1738 	 * enable burst mode.
1739 	 *
1740 	 * Also enable the "no underflow" option on the Am79c971 and
1741 	 * higher, which prevents the chip from generating transmit
1742 	 * underflows, yet sill provides decent performance.  Note if
1743 	 * chip is not connected to external SRAM, then we still have
1744 	 * to handle underflow errors (the NOUFLO bit is ignored in
1745 	 * that case).
1746 	 */
1747 	reg = pcn_bcr_read(sc, LE_BCR18);
1748 	switch (sc->sc_variant->pcv_chipid) {
1749 	case PARTID_Am79c970:
1750 		break;
1751 
1752 	case PARTID_Am79c970A:
1753 		reg |= LE_B18_BREADE|LE_B18_BWRITE;
1754 		break;
1755 
1756 	default:
1757 		reg |= LE_B18_BREADE|LE_B18_BWRITE|LE_B18_NOUFLO;
1758 		break;
1759 	}
1760 	pcn_bcr_write(sc, LE_BCR18, reg);
1761 
1762 	/*
1763 	 * Initialize CSR80 (FIFO thresholds for Tx and Rx).
1764 	 */
1765 	pcn_csr_write(sc, LE_CSR80, LE_C80_RCVFW(sc->sc_rcvfw) |
1766 	    LE_C80_XMTSP(sc->sc_xmtsp) | LE_C80_XMTFW(sc->sc_xmtfw));
1767 
1768 	/*
1769 	 * Send the init block to the chip, and wait for it
1770 	 * to be processed.
1771 	 */
1772 	PCN_CDINITSYNC(sc, BUS_DMASYNC_PREWRITE);
1773 	pcn_csr_write(sc, LE_CSR1, PCN_CDINITADDR(sc) & 0xffff);
1774 	pcn_csr_write(sc, LE_CSR2, (PCN_CDINITADDR(sc) >> 16) & 0xffff);
1775 	pcn_csr_write(sc, LE_CSR0, LE_C0_INIT);
1776 	delay(100);
1777 	for (i = 0; i < 10000; i++) {
1778 		if (pcn_csr_read(sc, LE_CSR0) & LE_C0_IDON)
1779 			break;
1780 		delay(10);
1781 	}
1782 	PCN_CDINITSYNC(sc, BUS_DMASYNC_POSTWRITE);
1783 	if (i == 10000) {
1784 		printf("%s: timeout processing init block\n",
1785 		    sc->sc_dev.dv_xname);
1786 		error = EIO;
1787 		goto out;
1788 	}
1789 
1790 	/* Set the media. */
1791 	(void) (*sc->sc_mii.mii_media.ifm_change)(ifp);
1792 
1793 	/* Enable interrupts and external activity (and ACK IDON). */
1794 	pcn_csr_write(sc, LE_CSR0, LE_C0_INEA|LE_C0_STRT|LE_C0_IDON);
1795 
1796 	if (sc->sc_flags & PCN_F_HAS_MII) {
1797 		/* Start the one second MII clock. */
1798 		callout_reset(&sc->sc_tick_ch, hz, pcn_tick, sc);
1799 	}
1800 
1801 	/* ...all done! */
1802 	ifp->if_flags |= IFF_RUNNING;
1803 	ifp->if_flags &= ~IFF_OACTIVE;
1804 
1805  out:
1806 	if (error)
1807 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1808 	return (error);
1809 }
1810 
1811 /*
1812  * pcn_rxdrain:
1813  *
1814  *	Drain the receive queue.
1815  */
1816 static void
1817 pcn_rxdrain(struct pcn_softc *sc)
1818 {
1819 	struct pcn_rxsoft *rxs;
1820 	int i;
1821 
1822 	for (i = 0; i < PCN_NRXDESC; i++) {
1823 		rxs = &sc->sc_rxsoft[i];
1824 		if (rxs->rxs_mbuf != NULL) {
1825 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1826 			m_freem(rxs->rxs_mbuf);
1827 			rxs->rxs_mbuf = NULL;
1828 		}
1829 	}
1830 }
1831 
1832 /*
1833  * pcn_stop:		[ifnet interface function]
1834  *
1835  *	Stop transmission on the interface.
1836  */
1837 static void
1838 pcn_stop(struct ifnet *ifp, int disable)
1839 {
1840 	struct pcn_softc *sc = ifp->if_softc;
1841 	struct pcn_txsoft *txs;
1842 	int i;
1843 
1844 	if (sc->sc_flags & PCN_F_HAS_MII) {
1845 		/* Stop the one second clock. */
1846 		callout_stop(&sc->sc_tick_ch);
1847 
1848 		/* Down the MII. */
1849 		mii_down(&sc->sc_mii);
1850 	}
1851 
1852 	/* Stop the chip. */
1853 	pcn_csr_write(sc, LE_CSR0, LE_C0_STOP);
1854 
1855 	/* Release any queued transmit buffers. */
1856 	for (i = 0; i < PCN_TXQUEUELEN; i++) {
1857 		txs = &sc->sc_txsoft[i];
1858 		if (txs->txs_mbuf != NULL) {
1859 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1860 			m_freem(txs->txs_mbuf);
1861 			txs->txs_mbuf = NULL;
1862 		}
1863 	}
1864 
1865 	if (disable)
1866 		pcn_rxdrain(sc);
1867 
1868 	/* Mark the interface as down and cancel the watchdog timer. */
1869 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1870 	ifp->if_timer = 0;
1871 }
1872 
1873 /*
1874  * pcn_add_rxbuf:
1875  *
1876  *	Add a receive buffer to the indicated descriptor.
1877  */
1878 static int
1879 pcn_add_rxbuf(struct pcn_softc *sc, int idx)
1880 {
1881 	struct pcn_rxsoft *rxs = &sc->sc_rxsoft[idx];
1882 	struct mbuf *m;
1883 	int error;
1884 
1885 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1886 	if (m == NULL)
1887 		return (ENOBUFS);
1888 
1889 	MCLGET(m, M_DONTWAIT);
1890 	if ((m->m_flags & M_EXT) == 0) {
1891 		m_freem(m);
1892 		return (ENOBUFS);
1893 	}
1894 
1895 	if (rxs->rxs_mbuf != NULL)
1896 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1897 
1898 	rxs->rxs_mbuf = m;
1899 
1900 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1901 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1902 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
1903 	if (error) {
1904 		printf("%s: can't load rx DMA map %d, error = %d\n",
1905 		    sc->sc_dev.dv_xname, idx, error);
1906 		panic("pcn_add_rxbuf");
1907 	}
1908 
1909 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1910 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1911 
1912 	PCN_INIT_RXDESC(sc, idx);
1913 
1914 	return (0);
1915 }
1916 
1917 /*
1918  * pcn_set_filter:
1919  *
1920  *	Set up the receive filter.
1921  */
1922 static void
1923 pcn_set_filter(struct pcn_softc *sc)
1924 {
1925 	struct ethercom *ec = &sc->sc_ethercom;
1926 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1927 	struct ether_multi *enm;
1928 	struct ether_multistep step;
1929 	uint32_t crc;
1930 
1931 	/*
1932 	 * Set up the multicast address filter by passing all multicast
1933 	 * addresses through a CRC generator, and then using the high
1934 	 * order 6 bits as an index into the 64-bit logical address
1935 	 * filter.  The high order bits select the word, while the rest
1936 	 * of the bits select the bit within the word.
1937 	 */
1938 
1939 	if (ifp->if_flags & IFF_PROMISC)
1940 		goto allmulti;
1941 
1942 	sc->sc_initblock.init_ladrf[0] =
1943 	    sc->sc_initblock.init_ladrf[1] =
1944 	    sc->sc_initblock.init_ladrf[2] =
1945 	    sc->sc_initblock.init_ladrf[3] = 0;
1946 
1947 	ETHER_FIRST_MULTI(step, ec, enm);
1948 	while (enm != NULL) {
1949 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1950 			/*
1951 			 * We must listen to a range of multicast addresses.
1952 			 * For now, just accept all multicasts, rather than
1953 			 * trying to set only those filter bits needed to match
1954 			 * the range.  (At this time, the only use of address
1955 			 * ranges is for IP multicast routing, for which the
1956 			 * range is big enough to require all bits set.)
1957 			 */
1958 			goto allmulti;
1959 		}
1960 
1961 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1962 
1963 		/* Just want the 6 most significant bits. */
1964 		crc >>= 26;
1965 
1966 		/* Set the corresponding bit in the filter. */
1967 		sc->sc_initblock.init_ladrf[crc >> 4] |=
1968 		    htole16(1 << (crc & 0xf));
1969 
1970 		ETHER_NEXT_MULTI(step, enm);
1971 	}
1972 
1973 	ifp->if_flags &= ~IFF_ALLMULTI;
1974 	return;
1975 
1976  allmulti:
1977 	ifp->if_flags |= IFF_ALLMULTI;
1978 	sc->sc_initblock.init_ladrf[0] =
1979 	    sc->sc_initblock.init_ladrf[1] =
1980 	    sc->sc_initblock.init_ladrf[2] =
1981 	    sc->sc_initblock.init_ladrf[3] = 0xffff;
1982 }
1983 
1984 /*
1985  * pcn_79c970_mediainit:
1986  *
1987  *	Initialize media for the Am79c970.
1988  */
1989 static void
1990 pcn_79c970_mediainit(struct pcn_softc *sc)
1991 {
1992 	const char *sep = "";
1993 
1994 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, pcn_79c970_mediachange,
1995 	    pcn_79c970_mediastatus);
1996 
1997 #define	ADD(str, m, d)							\
1998 do {									\
1999 	printf("%s%s", sep, str);					\
2000 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|(m), (d), NULL);	\
2001 	sep = ", ";							\
2002 } while (/*CONSTCOND*/0)
2003 
2004 	printf("%s: ", sc->sc_dev.dv_xname);
2005 	ADD("10base5", IFM_10_5, PORTSEL_AUI);
2006 	if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A)
2007 		ADD("10base5-FDX", IFM_10_5|IFM_FDX, PORTSEL_AUI);
2008 	ADD("10baseT", IFM_10_T, PORTSEL_10T);
2009 	if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A)
2010 		ADD("10baseT-FDX", IFM_10_T|IFM_FDX, PORTSEL_10T);
2011 	ADD("auto", IFM_AUTO, 0);
2012 	if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A)
2013 		ADD("auto-FDX", IFM_AUTO|IFM_FDX, 0);
2014 	printf("\n");
2015 
2016 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2017 }
2018 
2019 /*
2020  * pcn_79c970_mediastatus:	[ifmedia interface function]
2021  *
2022  *	Get the current interface media status (Am79c970 version).
2023  */
2024 static void
2025 pcn_79c970_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2026 {
2027 	struct pcn_softc *sc = ifp->if_softc;
2028 
2029 	/*
2030 	 * The currently selected media is always the active media.
2031 	 * Note: We have no way to determine what media the AUTO
2032 	 * process picked.
2033 	 */
2034 	ifmr->ifm_active = sc->sc_mii.mii_media.ifm_media;
2035 }
2036 
2037 /*
2038  * pcn_79c970_mediachange:	[ifmedia interface function]
2039  *
2040  *	Set hardware to newly-selected media (Am79c970 version).
2041  */
2042 static int
2043 pcn_79c970_mediachange(struct ifnet *ifp)
2044 {
2045 	struct pcn_softc *sc = ifp->if_softc;
2046 	uint32_t reg;
2047 
2048 	if (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media) == IFM_AUTO) {
2049 		/*
2050 		 * CSR15:PORTSEL doesn't matter.  Just set BCR2:ASEL.
2051 		 */
2052 		reg = pcn_bcr_read(sc, LE_BCR2);
2053 		reg |= LE_B2_ASEL;
2054 		pcn_bcr_write(sc, LE_BCR2, reg);
2055 	} else {
2056 		/*
2057 		 * Clear BCR2:ASEL and set the new CSR15:PORTSEL value.
2058 		 */
2059 		reg = pcn_bcr_read(sc, LE_BCR2);
2060 		reg &= ~LE_B2_ASEL;
2061 		pcn_bcr_write(sc, LE_BCR2, reg);
2062 
2063 		reg = pcn_csr_read(sc, LE_CSR15);
2064 		reg = (reg & ~LE_C15_PORTSEL(PORTSEL_MASK)) |
2065 		    LE_C15_PORTSEL(sc->sc_mii.mii_media.ifm_cur->ifm_data);
2066 		pcn_csr_write(sc, LE_CSR15, reg);
2067 	}
2068 
2069 	if ((sc->sc_mii.mii_media.ifm_media & IFM_FDX) != 0) {
2070 		reg = LE_B9_FDEN;
2071 		if (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media) == IFM_10_5)
2072 			reg |= LE_B9_AUIFD;
2073 		pcn_bcr_write(sc, LE_BCR9, reg);
2074 	} else
2075 		pcn_bcr_write(sc, LE_BCR9, 0);
2076 
2077 	return (0);
2078 }
2079 
2080 /*
2081  * pcn_79c971_mediainit:
2082  *
2083  *	Initialize media for the Am79c971.
2084  */
2085 static void
2086 pcn_79c971_mediainit(struct pcn_softc *sc)
2087 {
2088 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2089 
2090 	/* We have MII. */
2091 	sc->sc_flags |= PCN_F_HAS_MII;
2092 
2093 	/*
2094 	 * The built-in 10BASE-T interface is mapped to the MII
2095 	 * on the PCNet-FAST.  Unfortunately, there's no EEPROM
2096 	 * word that tells us which PHY to use.  Since the 10BASE-T
2097 	 * interface is always at PHY 31, we make a note of the
2098 	 * first PHY that responds, and disallow any PHYs after
2099 	 * it.  This is all handled in the MII read routine.
2100 	 */
2101 	sc->sc_phyaddr = -1;
2102 
2103 	/* Initialize our media structures and probe the MII. */
2104 	sc->sc_mii.mii_ifp = ifp;
2105 	sc->sc_mii.mii_readreg = pcn_mii_readreg;
2106 	sc->sc_mii.mii_writereg = pcn_mii_writereg;
2107 	sc->sc_mii.mii_statchg = pcn_mii_statchg;
2108 	ifmedia_init(&sc->sc_mii.mii_media, 0, pcn_79c971_mediachange,
2109 	    pcn_79c971_mediastatus);
2110 
2111 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
2112 	    MII_OFFSET_ANY, 0);
2113 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
2114 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
2115 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
2116 	} else
2117 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2118 }
2119 
2120 /*
2121  * pcn_79c971_mediastatus:	[ifmedia interface function]
2122  *
2123  *	Get the current interface media status (Am79c971 version).
2124  */
2125 static void
2126 pcn_79c971_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2127 {
2128 	struct pcn_softc *sc = ifp->if_softc;
2129 
2130 	mii_pollstat(&sc->sc_mii);
2131 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
2132 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
2133 }
2134 
2135 /*
2136  * pcn_79c971_mediachange:	[ifmedia interface function]
2137  *
2138  *	Set hardware to newly-selected media (Am79c971 version).
2139  */
2140 static int
2141 pcn_79c971_mediachange(struct ifnet *ifp)
2142 {
2143 	struct pcn_softc *sc = ifp->if_softc;
2144 
2145 	if (ifp->if_flags & IFF_UP)
2146 		mii_mediachg(&sc->sc_mii);
2147 	return (0);
2148 }
2149 
2150 /*
2151  * pcn_mii_readreg:	[mii interface function]
2152  *
2153  *	Read a PHY register on the MII.
2154  */
2155 static int
2156 pcn_mii_readreg(struct device *self, int phy, int reg)
2157 {
2158 	struct pcn_softc *sc = (void *) self;
2159 	uint32_t rv;
2160 
2161 	if (sc->sc_phyaddr != -1 && phy != sc->sc_phyaddr)
2162 		return (0);
2163 
2164 	pcn_bcr_write(sc, LE_BCR33, reg | (phy << PHYAD_SHIFT));
2165 	rv = pcn_bcr_read(sc, LE_BCR34) & LE_B34_MIIMD;
2166 	if (rv == 0xffff)
2167 		return (0);
2168 
2169 	if (sc->sc_phyaddr == -1)
2170 		sc->sc_phyaddr = phy;
2171 
2172 	return (rv);
2173 }
2174 
2175 /*
2176  * pcn_mii_writereg:	[mii interface function]
2177  *
2178  *	Write a PHY register on the MII.
2179  */
2180 static void
2181 pcn_mii_writereg(struct device *self, int phy, int reg, int val)
2182 {
2183 	struct pcn_softc *sc = (void *) self;
2184 
2185 	pcn_bcr_write(sc, LE_BCR33, reg | (phy << PHYAD_SHIFT));
2186 	pcn_bcr_write(sc, LE_BCR34, val);
2187 }
2188 
2189 /*
2190  * pcn_mii_statchg:	[mii interface function]
2191  *
2192  *	Callback from MII layer when media changes.
2193  */
2194 static void
2195 pcn_mii_statchg(struct device *self)
2196 {
2197 	struct pcn_softc *sc = (void *) self;
2198 
2199 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2200 		pcn_bcr_write(sc, LE_BCR9, LE_B9_FDEN);
2201 	else
2202 		pcn_bcr_write(sc, LE_BCR9, 0);
2203 }
2204