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