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