Home | History | Annotate | Line # | Download | only in pci
if_cas.c revision 1.30
      1  1.30   msaitoh /*	$NetBSD: if_cas.c,v 1.30 2019/01/22 03:42:27 msaitoh Exp $	*/
      2   1.1       jdc /*	$OpenBSD: if_cas.c,v 1.29 2009/11/29 16:19:38 kettenis Exp $	*/
      3   1.1       jdc 
      4   1.1       jdc /*
      5   1.1       jdc  *
      6   1.1       jdc  * Copyright (C) 2007 Mark Kettenis.
      7   1.1       jdc  * Copyright (C) 2001 Eduardo Horvath.
      8   1.1       jdc  * All rights reserved.
      9   1.1       jdc  *
     10   1.1       jdc  *
     11   1.1       jdc  * Redistribution and use in source and binary forms, with or without
     12   1.1       jdc  * modification, are permitted provided that the following conditions
     13   1.1       jdc  * are met:
     14   1.1       jdc  * 1. Redistributions of source code must retain the above copyright
     15   1.1       jdc  *    notice, this list of conditions and the following disclaimer.
     16   1.1       jdc  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       jdc  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       jdc  *    documentation and/or other materials provided with the distribution.
     19   1.1       jdc  *
     20   1.1       jdc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     21   1.1       jdc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1       jdc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1       jdc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     24   1.1       jdc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1       jdc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1       jdc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1       jdc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1       jdc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1       jdc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1       jdc  * SUCH DAMAGE.
     31   1.1       jdc  *
     32   1.1       jdc  */
     33   1.1       jdc 
     34   1.1       jdc /*
     35   1.1       jdc  * Driver for Sun Cassini ethernet controllers.
     36   1.1       jdc  *
     37   1.1       jdc  * There are basically two variants of this chip: Cassini and
     38   1.1       jdc  * Cassini+.  We can distinguish between the two by revision: 0x10 and
     39   1.1       jdc  * up are Cassini+.  The most important difference is that Cassini+
     40   1.1       jdc  * has a second RX descriptor ring.  Cassini+ will not work without
     41   1.1       jdc  * configuring that second ring.  However, since we don't use it we
     42   1.1       jdc  * don't actually fill the descriptors, and only hand off the first
     43   1.1       jdc  * four to the chip.
     44   1.1       jdc  */
     45   1.1       jdc 
     46   1.1       jdc #include <sys/cdefs.h>
     47  1.30   msaitoh __KERNEL_RCSID(0, "$NetBSD: if_cas.c,v 1.30 2019/01/22 03:42:27 msaitoh Exp $");
     48   1.1       jdc 
     49  1.13  jmcneill #ifndef _MODULE
     50   1.1       jdc #include "opt_inet.h"
     51  1.13  jmcneill #endif
     52   1.1       jdc 
     53   1.1       jdc #include <sys/param.h>
     54   1.1       jdc #include <sys/systm.h>
     55   1.1       jdc #include <sys/callout.h>
     56   1.1       jdc #include <sys/mbuf.h>
     57   1.1       jdc #include <sys/syslog.h>
     58   1.1       jdc #include <sys/malloc.h>
     59   1.1       jdc #include <sys/kernel.h>
     60   1.1       jdc #include <sys/socket.h>
     61   1.1       jdc #include <sys/ioctl.h>
     62   1.1       jdc #include <sys/errno.h>
     63   1.1       jdc #include <sys/device.h>
     64  1.13  jmcneill #include <sys/module.h>
     65   1.1       jdc 
     66   1.1       jdc #include <machine/endian.h>
     67   1.1       jdc 
     68   1.1       jdc #include <net/if.h>
     69   1.1       jdc #include <net/if_dl.h>
     70   1.1       jdc #include <net/if_media.h>
     71   1.1       jdc #include <net/if_ether.h>
     72   1.1       jdc 
     73   1.1       jdc #ifdef INET
     74   1.1       jdc #include <netinet/in.h>
     75   1.1       jdc #include <netinet/in_systm.h>
     76   1.1       jdc #include <netinet/in_var.h>
     77   1.1       jdc #include <netinet/ip.h>
     78   1.1       jdc #include <netinet/tcp.h>
     79   1.1       jdc #include <netinet/udp.h>
     80   1.1       jdc #endif
     81   1.1       jdc 
     82   1.1       jdc #include <net/bpf.h>
     83   1.1       jdc 
     84   1.1       jdc #include <sys/bus.h>
     85   1.1       jdc #include <sys/intr.h>
     86  1.23  riastrad #include <sys/rndsource.h>
     87   1.1       jdc 
     88   1.1       jdc #include <dev/mii/mii.h>
     89   1.1       jdc #include <dev/mii/miivar.h>
     90   1.1       jdc #include <dev/mii/mii_bitbang.h>
     91   1.1       jdc 
     92   1.1       jdc #include <dev/pci/pcivar.h>
     93   1.1       jdc #include <dev/pci/pcireg.h>
     94   1.1       jdc #include <dev/pci/pcidevs.h>
     95   1.5       jdc #include <prop/proplib.h>
     96   1.1       jdc 
     97   1.1       jdc #include <dev/pci/if_casreg.h>
     98   1.1       jdc #include <dev/pci/if_casvar.h>
     99   1.1       jdc 
    100   1.1       jdc #define TRIES	10000
    101   1.1       jdc 
    102   1.3       jdc static bool	cas_estintr(struct cas_softc *sc, int);
    103   1.1       jdc bool		cas_shutdown(device_t, int);
    104   1.6    dyoung static bool	cas_suspend(device_t, const pmf_qual_t *);
    105   1.6    dyoung static bool	cas_resume(device_t, const pmf_qual_t *);
    106   1.1       jdc static int	cas_detach(device_t, int);
    107   1.1       jdc static void	cas_partial_detach(struct cas_softc *, enum cas_attach_stage);
    108   1.1       jdc 
    109   1.1       jdc int		cas_match(device_t, cfdata_t, void *);
    110   1.1       jdc void		cas_attach(device_t, device_t, void *);
    111   1.1       jdc 
    112   1.1       jdc 
    113   1.1       jdc CFATTACH_DECL3_NEW(cas, sizeof(struct cas_softc),
    114   1.1       jdc     cas_match, cas_attach, cas_detach, NULL, NULL, NULL,
    115   1.1       jdc     DVF_DETACH_SHUTDOWN);
    116   1.1       jdc 
    117   1.1       jdc int	cas_pci_enaddr(struct cas_softc *, struct pci_attach_args *, uint8_t *);
    118   1.1       jdc 
    119   1.1       jdc void		cas_config(struct cas_softc *, const uint8_t *);
    120   1.1       jdc void		cas_start(struct ifnet *);
    121   1.1       jdc void		cas_stop(struct ifnet *, int);
    122   1.1       jdc int		cas_ioctl(struct ifnet *, u_long, void *);
    123   1.1       jdc void		cas_tick(void *);
    124   1.1       jdc void		cas_watchdog(struct ifnet *);
    125   1.1       jdc int		cas_init(struct ifnet *);
    126   1.1       jdc void		cas_init_regs(struct cas_softc *);
    127   1.1       jdc int		cas_ringsize(int);
    128   1.1       jdc int		cas_cringsize(int);
    129   1.1       jdc int		cas_meminit(struct cas_softc *);
    130   1.1       jdc void		cas_mifinit(struct cas_softc *);
    131   1.1       jdc int		cas_bitwait(struct cas_softc *, bus_space_handle_t, int,
    132   1.1       jdc 		    u_int32_t, u_int32_t);
    133   1.1       jdc void		cas_reset(struct cas_softc *);
    134   1.1       jdc int		cas_reset_rx(struct cas_softc *);
    135   1.1       jdc int		cas_reset_tx(struct cas_softc *);
    136   1.1       jdc int		cas_disable_rx(struct cas_softc *);
    137   1.1       jdc int		cas_disable_tx(struct cas_softc *);
    138   1.1       jdc void		cas_rxdrain(struct cas_softc *);
    139   1.1       jdc int		cas_add_rxbuf(struct cas_softc *, int idx);
    140   1.1       jdc void		cas_iff(struct cas_softc *);
    141   1.1       jdc int		cas_encap(struct cas_softc *, struct mbuf *, u_int32_t *);
    142   1.1       jdc 
    143   1.1       jdc /* MII methods & callbacks */
    144  1.30   msaitoh int		cas_mii_readreg(device_t, int, int, uint16_t*);
    145  1.30   msaitoh int		cas_mii_writereg(device_t, int, int, uint16_t);
    146  1.18      matt void		cas_mii_statchg(struct ifnet *);
    147  1.30   msaitoh int		cas_pcs_readreg(device_t, int, int, uint16_t *);
    148  1.30   msaitoh int		cas_pcs_writereg(device_t, int, int, uint16_t);
    149   1.1       jdc 
    150   1.1       jdc int		cas_mediachange(struct ifnet *);
    151   1.1       jdc void		cas_mediastatus(struct ifnet *, struct ifmediareq *);
    152   1.1       jdc 
    153   1.1       jdc int		cas_eint(struct cas_softc *, u_int);
    154   1.1       jdc int		cas_rint(struct cas_softc *);
    155   1.1       jdc int		cas_tint(struct cas_softc *, u_int32_t);
    156   1.1       jdc int		cas_pint(struct cas_softc *);
    157   1.1       jdc int		cas_intr(void *);
    158   1.1       jdc 
    159   1.1       jdc #ifdef CAS_DEBUG
    160   1.1       jdc #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    161   1.1       jdc 				printf x
    162   1.1       jdc #else
    163   1.1       jdc #define	DPRINTF(sc, x)	/* nothing */
    164   1.1       jdc #endif
    165   1.1       jdc 
    166   1.1       jdc int
    167   1.1       jdc cas_match(device_t parent, cfdata_t cf, void *aux)
    168   1.1       jdc {
    169   1.1       jdc 	struct pci_attach_args *pa = aux;
    170   1.1       jdc 
    171   1.1       jdc 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    172   1.1       jdc 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_CASSINI))
    173   1.1       jdc 		return 1;
    174   1.1       jdc 
    175   1.1       jdc 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
    176   1.1       jdc 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SATURN))
    177   1.1       jdc 		return 1;
    178   1.1       jdc 
    179   1.1       jdc 	return 0;
    180   1.1       jdc }
    181   1.1       jdc 
    182   1.1       jdc #define	PROMHDR_PTR_DATA	0x18
    183   1.1       jdc #define	PROMDATA_PTR_VPD	0x08
    184   1.1       jdc #define	PROMDATA_DATA2		0x0a
    185   1.1       jdc 
    186   1.1       jdc static const u_int8_t cas_promhdr[] = { 0x55, 0xaa };
    187   1.1       jdc static const u_int8_t cas_promdat[] = {
    188   1.1       jdc 	'P', 'C', 'I', 'R',
    189   1.1       jdc 	PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
    190   1.1       jdc 	PCI_PRODUCT_SUN_CASSINI & 0xff, PCI_PRODUCT_SUN_CASSINI >> 8
    191   1.1       jdc };
    192  1.11   jnemeth static const u_int8_t cas_promdat_ns[] = {
    193  1.11   jnemeth 	'P', 'C', 'I', 'R',
    194  1.11   jnemeth 	PCI_VENDOR_NS & 0xff, PCI_VENDOR_NS >> 8,
    195  1.11   jnemeth 	PCI_PRODUCT_NS_SATURN & 0xff, PCI_PRODUCT_NS_SATURN >> 8
    196  1.11   jnemeth };
    197   1.1       jdc 
    198   1.1       jdc static const u_int8_t cas_promdat2[] = {
    199   1.1       jdc 	0x18, 0x00,			/* structure length */
    200   1.1       jdc 	0x00,				/* structure revision */
    201   1.1       jdc 	0x00,				/* interface revision */
    202   1.1       jdc 	PCI_SUBCLASS_NETWORK_ETHERNET,	/* subclass code */
    203   1.1       jdc 	PCI_CLASS_NETWORK		/* class code */
    204   1.1       jdc };
    205   1.1       jdc 
    206   1.1       jdc int
    207   1.1       jdc cas_pci_enaddr(struct cas_softc *sc, struct pci_attach_args *pa,
    208   1.1       jdc     uint8_t *enaddr)
    209   1.1       jdc {
    210   1.1       jdc 	struct pci_vpd_largeres *res;
    211   1.1       jdc 	struct pci_vpd *vpd;
    212   1.1       jdc 	bus_space_handle_t romh;
    213   1.1       jdc 	bus_space_tag_t romt;
    214   1.1       jdc 	bus_size_t romsize = 0;
    215   1.1       jdc 	u_int8_t buf[32], *desc;
    216   1.1       jdc 	pcireg_t address;
    217   1.1       jdc 	int dataoff, vpdoff, len;
    218   1.1       jdc 	int rv = -1;
    219   1.1       jdc 
    220   1.1       jdc 	if (pci_mapreg_map(pa, PCI_MAPREG_ROM, PCI_MAPREG_TYPE_MEM, 0,
    221   1.1       jdc 	    &romt, &romh, NULL, &romsize))
    222   1.1       jdc 		return (-1);
    223   1.1       jdc 
    224   1.1       jdc 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
    225   1.1       jdc 	address |= PCI_MAPREG_ROM_ENABLE;
    226   1.1       jdc 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START, address);
    227   1.1       jdc 
    228   1.1       jdc 	bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf));
    229   1.1       jdc 	if (bcmp(buf, cas_promhdr, sizeof(cas_promhdr)))
    230   1.1       jdc 		goto fail;
    231   1.1       jdc 
    232   1.1       jdc 	dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8);
    233   1.1       jdc 	if (dataoff < 0x1c)
    234   1.1       jdc 		goto fail;
    235   1.1       jdc 
    236   1.1       jdc 	bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf));
    237  1.11   jnemeth 	if ((bcmp(buf, cas_promdat, sizeof(cas_promdat)) &&
    238  1.11   jnemeth 	     bcmp(buf, cas_promdat_ns, sizeof(cas_promdat_ns))) ||
    239   1.1       jdc 	    bcmp(buf + PROMDATA_DATA2, cas_promdat2, sizeof(cas_promdat2)))
    240   1.1       jdc 		goto fail;
    241   1.1       jdc 
    242   1.1       jdc 	vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8);
    243   1.1       jdc 	if (vpdoff < 0x1c)
    244   1.1       jdc 		goto fail;
    245   1.1       jdc 
    246   1.1       jdc next:
    247   1.1       jdc 	bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf));
    248   1.1       jdc 	if (!PCI_VPDRES_ISLARGE(buf[0]))
    249   1.1       jdc 		goto fail;
    250   1.1       jdc 
    251   1.1       jdc 	res = (struct pci_vpd_largeres *)buf;
    252   1.1       jdc 	vpdoff += sizeof(*res);
    253   1.1       jdc 
    254   1.1       jdc 	len = ((res->vpdres_len_msb << 8) + res->vpdres_len_lsb);
    255   1.1       jdc 	switch(PCI_VPDRES_LARGE_NAME(res->vpdres_byte0)) {
    256   1.1       jdc 	case PCI_VPDRES_TYPE_IDENTIFIER_STRING:
    257   1.1       jdc 		/* Skip identifier string. */
    258   1.1       jdc 		vpdoff += len;
    259   1.1       jdc 		goto next;
    260   1.1       jdc 
    261   1.1       jdc 	case PCI_VPDRES_TYPE_VPD:
    262   1.1       jdc 		while (len > 0) {
    263   1.1       jdc 			bus_space_read_region_1(romt, romh, vpdoff,
    264   1.1       jdc 			     buf, sizeof(buf));
    265   1.1       jdc 
    266   1.1       jdc 			vpd = (struct pci_vpd *)buf;
    267   1.1       jdc 			vpdoff += sizeof(*vpd) + vpd->vpd_len;
    268   1.1       jdc 			len -= sizeof(*vpd) + vpd->vpd_len;
    269   1.1       jdc 
    270   1.1       jdc 			/*
    271   1.1       jdc 			 * We're looking for an "Enhanced" VPD...
    272   1.1       jdc 			 */
    273   1.1       jdc 			if (vpd->vpd_key0 != 'Z')
    274   1.1       jdc 				continue;
    275   1.1       jdc 
    276   1.1       jdc 			desc = buf + sizeof(*vpd);
    277   1.1       jdc 
    278  1.19  christos 			/*
    279   1.1       jdc 			 * ...which is an instance property...
    280   1.1       jdc 			 */
    281   1.1       jdc 			if (desc[0] != 'I')
    282   1.1       jdc 				continue;
    283   1.1       jdc 			desc += 3;
    284   1.1       jdc 
    285  1.19  christos 			/*
    286   1.1       jdc 			 * ...that's a byte array with the proper
    287   1.1       jdc 			 * length for a MAC address...
    288   1.1       jdc 			 */
    289   1.1       jdc 			if (desc[0] != 'B' || desc[1] != ETHER_ADDR_LEN)
    290   1.1       jdc 				continue;
    291   1.1       jdc 			desc += 2;
    292   1.1       jdc 
    293   1.1       jdc 			/*
    294   1.1       jdc 			 * ...named "local-mac-address".
    295   1.1       jdc 			 */
    296   1.1       jdc 			if (strcmp(desc, "local-mac-address") != 0)
    297   1.1       jdc 				continue;
    298   1.1       jdc 			desc += strlen("local-mac-address") + 1;
    299  1.19  christos 
    300   1.5       jdc 			memcpy(enaddr, desc, ETHER_ADDR_LEN);
    301   1.1       jdc 			rv = 0;
    302   1.1       jdc 		}
    303   1.1       jdc 		break;
    304   1.1       jdc 
    305   1.1       jdc 	default:
    306   1.1       jdc 		goto fail;
    307   1.1       jdc 	}
    308   1.1       jdc 
    309   1.1       jdc  fail:
    310   1.1       jdc 	if (romsize != 0)
    311   1.1       jdc 		bus_space_unmap(romt, romh, romsize);
    312   1.1       jdc 
    313   1.1       jdc 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
    314   1.1       jdc 	address &= ~PCI_MAPREG_ROM_ENABLE;
    315   1.1       jdc 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, address);
    316   1.1       jdc 
    317   1.1       jdc 	return (rv);
    318   1.1       jdc }
    319   1.1       jdc 
    320   1.1       jdc void
    321   1.1       jdc cas_attach(device_t parent, device_t self, void *aux)
    322   1.1       jdc {
    323   1.1       jdc 	struct pci_attach_args *pa = aux;
    324   1.1       jdc 	struct cas_softc *sc = device_private(self);
    325   1.5       jdc 	prop_data_t data;
    326   1.1       jdc 	uint8_t enaddr[ETHER_ADDR_LEN];
    327   1.1       jdc 
    328   1.1       jdc 	sc->sc_dev = self;
    329  1.15  drochner 	pci_aprint_devinfo(pa, NULL);
    330   1.1       jdc 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    331   1.1       jdc 	sc->sc_dmatag = pa->pa_dmat;
    332   1.1       jdc 
    333   1.1       jdc #define PCI_CAS_BASEADDR	0x10
    334   1.1       jdc 	if (pci_mapreg_map(pa, PCI_CAS_BASEADDR, PCI_MAPREG_TYPE_MEM, 0,
    335   1.1       jdc 	    &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_size) != 0) {
    336   1.1       jdc 		aprint_error_dev(sc->sc_dev,
    337   1.1       jdc 		    "unable to map device registers\n");
    338   1.1       jdc 		return;
    339   1.1       jdc 	}
    340   1.1       jdc 
    341   1.5       jdc 	if ((data = prop_dictionary_get(device_properties(sc->sc_dev),
    342   1.5       jdc 	    "mac-address")) != NULL)
    343   1.5       jdc 		memcpy(enaddr, prop_data_data_nocopy(data), ETHER_ADDR_LEN);
    344  1.10   jnemeth 	else if (cas_pci_enaddr(sc, pa, enaddr) != 0) {
    345   1.1       jdc 		aprint_error_dev(sc->sc_dev, "no Ethernet address found\n");
    346  1.10   jnemeth 		memset(enaddr, 0, sizeof(enaddr));
    347  1.10   jnemeth 	}
    348   1.1       jdc 
    349   1.1       jdc 	sc->sc_burst = 16;	/* XXX */
    350   1.1       jdc 
    351   1.1       jdc 	sc->sc_att_stage = CAS_ATT_BACKEND_0;
    352   1.1       jdc 
    353   1.1       jdc 	if (pci_intr_map(pa, &sc->sc_handle) != 0) {
    354   1.1       jdc 		aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
    355   1.1       jdc 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
    356   1.1       jdc 		return;
    357   1.1       jdc 	}
    358   1.1       jdc 	sc->sc_pc = pa->pa_pc;
    359   1.3       jdc 	if (!cas_estintr(sc, CAS_INTR_PCI)) {
    360   1.1       jdc 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
    361   1.1       jdc 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    362   1.1       jdc 		return;
    363   1.1       jdc 	}
    364   1.1       jdc 
    365   1.1       jdc 	sc->sc_att_stage = CAS_ATT_BACKEND_1;
    366   1.1       jdc 
    367   1.1       jdc 	/*
    368   1.1       jdc 	 * call the main configure
    369   1.1       jdc 	 */
    370   1.1       jdc 	cas_config(sc, enaddr);
    371   1.1       jdc 
    372   1.1       jdc 	if (pmf_device_register1(sc->sc_dev,
    373   1.1       jdc 	    cas_suspend, cas_resume, cas_shutdown))
    374   1.1       jdc 		pmf_class_network_register(sc->sc_dev, &sc->sc_ethercom.ec_if);
    375   1.1       jdc 	else
    376   1.1       jdc 		aprint_error_dev(sc->sc_dev,
    377   1.1       jdc 		    "could not establish power handlers\n");
    378   1.1       jdc 
    379   1.1       jdc 	sc->sc_att_stage = CAS_ATT_FINISHED;
    380   1.1       jdc 		/*FALLTHROUGH*/
    381   1.1       jdc }
    382   1.1       jdc 
    383   1.1       jdc /*
    384   1.1       jdc  * cas_config:
    385   1.1       jdc  *
    386   1.1       jdc  *	Attach a Cassini interface to the system.
    387   1.1       jdc  */
    388   1.1       jdc void
    389   1.1       jdc cas_config(struct cas_softc *sc, const uint8_t *enaddr)
    390   1.1       jdc {
    391   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    392   1.1       jdc 	struct mii_data *mii = &sc->sc_mii;
    393   1.1       jdc 	struct mii_softc *child;
    394   1.1       jdc 	int i, error;
    395   1.1       jdc 
    396   1.1       jdc 	/* Make sure the chip is stopped. */
    397   1.1       jdc 	ifp->if_softc = sc;
    398   1.1       jdc 	cas_reset(sc);
    399   1.1       jdc 
    400   1.1       jdc 	/*
    401   1.1       jdc 	 * Allocate the control data structures, and create and load the
    402   1.1       jdc 	 * DMA map for it.
    403   1.1       jdc 	 */
    404   1.1       jdc 	if ((error = bus_dmamem_alloc(sc->sc_dmatag,
    405   1.1       jdc 	    sizeof(struct cas_control_data), CAS_PAGE_SIZE, 0, &sc->sc_cdseg,
    406   1.1       jdc 	    1, &sc->sc_cdnseg, 0)) != 0) {
    407   1.1       jdc 		aprint_error_dev(sc->sc_dev,
    408   1.1       jdc 		    "unable to allocate control data, error = %d\n",
    409   1.1       jdc 		    error);
    410   1.1       jdc 		cas_partial_detach(sc, CAS_ATT_0);
    411   1.1       jdc 	}
    412   1.1       jdc 
    413   1.1       jdc 	/* XXX should map this in with correct endianness */
    414   1.1       jdc 	if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
    415   1.1       jdc 	    sizeof(struct cas_control_data), (void **)&sc->sc_control_data,
    416   1.1       jdc 	    BUS_DMA_COHERENT)) != 0) {
    417   1.1       jdc 		aprint_error_dev(sc->sc_dev,
    418   1.1       jdc 		    "unable to map control data, error = %d\n", error);
    419   1.1       jdc 		cas_partial_detach(sc, CAS_ATT_1);
    420   1.1       jdc 	}
    421   1.1       jdc 
    422   1.1       jdc 	if ((error = bus_dmamap_create(sc->sc_dmatag,
    423   1.1       jdc 	    sizeof(struct cas_control_data), 1,
    424   1.1       jdc 	    sizeof(struct cas_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    425   1.1       jdc 		aprint_error_dev(sc->sc_dev,
    426   1.1       jdc 		    "unable to create control data DMA map, error = %d\n", error);
    427   1.1       jdc 		cas_partial_detach(sc, CAS_ATT_2);
    428   1.1       jdc 	}
    429   1.1       jdc 
    430   1.1       jdc 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
    431   1.1       jdc 	    sc->sc_control_data, sizeof(struct cas_control_data), NULL,
    432   1.1       jdc 	    0)) != 0) {
    433   1.1       jdc 		aprint_error_dev(sc->sc_dev,
    434   1.1       jdc 		    "unable to load control data DMA map, error = %d\n",
    435   1.1       jdc 		    error);
    436   1.1       jdc 		cas_partial_detach(sc, CAS_ATT_3);
    437   1.1       jdc 	}
    438   1.1       jdc 
    439   1.1       jdc 	memset(sc->sc_control_data, 0, sizeof(struct cas_control_data));
    440   1.1       jdc 
    441   1.1       jdc 	/*
    442   1.1       jdc 	 * Create the receive buffer DMA maps.
    443   1.1       jdc 	 */
    444   1.1       jdc 	for (i = 0; i < CAS_NRXDESC; i++) {
    445   1.1       jdc 		bus_dma_segment_t seg;
    446   1.1       jdc 		char *kva;
    447   1.1       jdc 		int rseg;
    448   1.1       jdc 
    449   1.1       jdc 		if ((error = bus_dmamem_alloc(sc->sc_dmatag, CAS_PAGE_SIZE,
    450   1.1       jdc 		    CAS_PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    451   1.1       jdc 			aprint_error_dev(sc->sc_dev,
    452   1.1       jdc 			    "unable to alloc rx DMA mem %d, error = %d\n",
    453   1.1       jdc 			    i, error);
    454   1.1       jdc 			cas_partial_detach(sc, CAS_ATT_5);
    455   1.1       jdc 		}
    456   1.1       jdc 		sc->sc_rxsoft[i].rxs_dmaseg = seg;
    457   1.1       jdc 
    458   1.1       jdc 		if ((error = bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
    459   1.1       jdc 		    CAS_PAGE_SIZE, (void **)&kva, BUS_DMA_NOWAIT)) != 0) {
    460   1.1       jdc 			aprint_error_dev(sc->sc_dev,
    461   1.1       jdc 			    "unable to alloc rx DMA mem %d, error = %d\n",
    462   1.1       jdc 			    i, error);
    463   1.1       jdc 			cas_partial_detach(sc, CAS_ATT_5);
    464   1.1       jdc 		}
    465   1.1       jdc 		sc->sc_rxsoft[i].rxs_kva = kva;
    466   1.1       jdc 
    467   1.1       jdc 		if ((error = bus_dmamap_create(sc->sc_dmatag, CAS_PAGE_SIZE, 1,
    468   1.1       jdc 		    CAS_PAGE_SIZE, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    469   1.1       jdc 			aprint_error_dev(sc->sc_dev,
    470   1.1       jdc 			    "unable to create rx DMA map %d, error = %d\n",
    471   1.1       jdc 			    i, error);
    472   1.1       jdc 			cas_partial_detach(sc, CAS_ATT_5);
    473   1.1       jdc 		}
    474   1.1       jdc 
    475   1.1       jdc 		if ((error = bus_dmamap_load(sc->sc_dmatag,
    476   1.1       jdc 		   sc->sc_rxsoft[i].rxs_dmamap, kva, CAS_PAGE_SIZE, NULL,
    477   1.1       jdc 		   BUS_DMA_NOWAIT)) != 0) {
    478   1.1       jdc 			aprint_error_dev(sc->sc_dev,
    479   1.1       jdc 			    "unable to load rx DMA map %d, error = %d\n",
    480   1.1       jdc 			    i, error);
    481   1.1       jdc 			cas_partial_detach(sc, CAS_ATT_5);
    482   1.1       jdc 		}
    483   1.1       jdc 	}
    484   1.1       jdc 
    485   1.1       jdc 	/*
    486   1.1       jdc 	 * Create the transmit buffer DMA maps.
    487   1.1       jdc 	 */
    488   1.1       jdc 	for (i = 0; i < CAS_NTXDESC; i++) {
    489   1.1       jdc 		if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES,
    490   1.1       jdc 		    CAS_NTXSEGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    491   1.1       jdc 		    &sc->sc_txd[i].sd_map)) != 0) {
    492   1.1       jdc 			aprint_error_dev(sc->sc_dev,
    493   1.1       jdc 			    "unable to create tx DMA map %d, error = %d\n",
    494   1.1       jdc 			    i, error);
    495   1.1       jdc 			cas_partial_detach(sc, CAS_ATT_6);
    496   1.1       jdc 		}
    497   1.1       jdc 		sc->sc_txd[i].sd_mbuf = NULL;
    498   1.1       jdc 	}
    499   1.1       jdc 
    500   1.1       jdc 	/*
    501   1.1       jdc 	 * From this point forward, the attachment cannot fail.  A failure
    502   1.1       jdc 	 * before this point releases all resources that may have been
    503   1.1       jdc 	 * allocated.
    504   1.1       jdc 	 */
    505   1.1       jdc 
    506   1.1       jdc 	/* Announce ourselves. */
    507   1.1       jdc 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    508   1.1       jdc 	    ether_sprintf(enaddr));
    509   1.7       mrg 	aprint_naive(": Ethernet controller\n");
    510   1.1       jdc 
    511   1.1       jdc 	/* Get RX FIFO size */
    512   1.1       jdc 	sc->sc_rxfifosize = 16 * 1024;
    513   1.1       jdc 
    514   1.1       jdc 	/* Initialize ifnet structure. */
    515   1.1       jdc 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    516   1.1       jdc 	ifp->if_softc = sc;
    517   1.1       jdc 	ifp->if_flags =
    518   1.1       jdc 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    519   1.1       jdc 	ifp->if_start = cas_start;
    520   1.1       jdc 	ifp->if_ioctl = cas_ioctl;
    521   1.1       jdc 	ifp->if_watchdog = cas_watchdog;
    522   1.1       jdc 	ifp->if_stop = cas_stop;
    523   1.1       jdc 	ifp->if_init = cas_init;
    524   1.1       jdc 	IFQ_SET_MAXLEN(&ifp->if_snd, CAS_NTXDESC - 1);
    525   1.1       jdc 	IFQ_SET_READY(&ifp->if_snd);
    526   1.1       jdc 
    527   1.1       jdc 	/* Initialize ifmedia structures and MII info */
    528   1.1       jdc 	mii->mii_ifp = ifp;
    529   1.1       jdc 	mii->mii_readreg = cas_mii_readreg;
    530   1.1       jdc 	mii->mii_writereg = cas_mii_writereg;
    531   1.1       jdc 	mii->mii_statchg = cas_mii_statchg;
    532   1.1       jdc 
    533   1.1       jdc 	ifmedia_init(&mii->mii_media, 0, cas_mediachange, cas_mediastatus);
    534   1.1       jdc 	sc->sc_ethercom.ec_mii = mii;
    535   1.1       jdc 
    536   1.1       jdc 	bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_MII_DATAPATH_MODE, 0);
    537   1.1       jdc 
    538   1.1       jdc 	cas_mifinit(sc);
    539   1.1       jdc 
    540   1.1       jdc 	if (sc->sc_mif_config & CAS_MIF_CONFIG_MDI1) {
    541   1.1       jdc 		sc->sc_mif_config |= CAS_MIF_CONFIG_PHY_SEL;
    542   1.1       jdc 		bus_space_write_4(sc->sc_memt, sc->sc_memh,
    543   1.1       jdc 	            CAS_MIF_CONFIG, sc->sc_mif_config);
    544   1.1       jdc 	}
    545   1.1       jdc 
    546   1.1       jdc 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
    547   1.1       jdc 	    MII_OFFSET_ANY, 0);
    548   1.1       jdc 
    549   1.1       jdc 	child = LIST_FIRST(&mii->mii_phys);
    550   1.1       jdc 	if (child == NULL &&
    551   1.1       jdc 	    sc->sc_mif_config & (CAS_MIF_CONFIG_MDI0|CAS_MIF_CONFIG_MDI1)) {
    552  1.19  christos 		/*
    553   1.1       jdc 		 * Try the external PCS SERDES if we didn't find any
    554   1.1       jdc 		 * MII devices.
    555   1.1       jdc 		 */
    556   1.1       jdc 		bus_space_write_4(sc->sc_memt, sc->sc_memh,
    557   1.1       jdc 		    CAS_MII_DATAPATH_MODE, CAS_MII_DATAPATH_SERDES);
    558   1.1       jdc 
    559   1.1       jdc 		bus_space_write_4(sc->sc_memt, sc->sc_memh,
    560   1.1       jdc 		     CAS_MII_CONFIG, CAS_MII_CONFIG_ENABLE);
    561   1.1       jdc 
    562   1.1       jdc 		mii->mii_readreg = cas_pcs_readreg;
    563   1.1       jdc 		mii->mii_writereg = cas_pcs_writereg;
    564   1.1       jdc 
    565   1.1       jdc 		mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
    566   1.1       jdc 		    MII_OFFSET_ANY, MIIF_NOISOLATE);
    567   1.1       jdc 	}
    568   1.1       jdc 
    569   1.1       jdc 	child = LIST_FIRST(&mii->mii_phys);
    570   1.1       jdc 	if (child == NULL) {
    571   1.1       jdc 		/* No PHY attached */
    572   1.1       jdc 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    573   1.1       jdc 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    574   1.1       jdc 	} else {
    575   1.1       jdc 		/*
    576   1.1       jdc 		 * Walk along the list of attached MII devices and
    577   1.1       jdc 		 * establish an `MII instance' to `phy number'
    578   1.1       jdc 		 * mapping. We'll use this mapping in media change
    579   1.1       jdc 		 * requests to determine which phy to use to program
    580   1.1       jdc 		 * the MIF configuration register.
    581   1.1       jdc 		 */
    582   1.1       jdc 		for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
    583   1.1       jdc 			/*
    584   1.1       jdc 			 * Note: we support just two PHYs: the built-in
    585   1.1       jdc 			 * internal device and an external on the MII
    586   1.1       jdc 			 * connector.
    587   1.1       jdc 			 */
    588   1.1       jdc 			if (child->mii_phy > 1 || child->mii_inst > 1) {
    589   1.1       jdc 				aprint_error_dev(sc->sc_dev,
    590   1.1       jdc 				    "cannot accommodate MII device %s"
    591   1.1       jdc 				    " at phy %d, instance %d\n",
    592   1.1       jdc 				    device_xname(child->mii_dev),
    593   1.1       jdc 				    child->mii_phy, child->mii_inst);
    594   1.1       jdc 				continue;
    595   1.1       jdc 			}
    596   1.1       jdc 
    597   1.1       jdc 			sc->sc_phys[child->mii_inst] = child->mii_phy;
    598   1.1       jdc 		}
    599   1.1       jdc 
    600   1.1       jdc 		/*
    601   1.1       jdc 		 * XXX - we can really do the following ONLY if the
    602   1.1       jdc 		 * phy indeed has the auto negotiation capability!!
    603   1.1       jdc 		 */
    604   1.1       jdc 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    605   1.1       jdc 	}
    606   1.1       jdc 
    607   1.1       jdc 	/* claim 802.1q capability */
    608   1.1       jdc 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    609   1.1       jdc 
    610   1.1       jdc 	/* Attach the interface. */
    611   1.1       jdc 	if_attach(ifp);
    612  1.25     ozaki 	if_deferred_start_init(ifp, NULL);
    613   1.1       jdc 	ether_ifattach(ifp, enaddr);
    614   1.1       jdc 
    615   1.1       jdc 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    616  1.22       tls 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
    617   1.1       jdc 
    618   1.1       jdc 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
    619   1.1       jdc 	    NULL, device_xname(sc->sc_dev), "interrupts");
    620   1.1       jdc 
    621   1.1       jdc 	callout_init(&sc->sc_tick_ch, 0);
    622   1.1       jdc 
    623   1.1       jdc 	return;
    624   1.1       jdc }
    625   1.1       jdc 
    626   1.1       jdc int
    627   1.1       jdc cas_detach(device_t self, int flags)
    628   1.1       jdc {
    629   1.1       jdc 	int i;
    630   1.1       jdc 	struct cas_softc *sc = device_private(self);
    631   1.3       jdc 	bus_space_tag_t t = sc->sc_memt;
    632   1.3       jdc 	bus_space_handle_t h = sc->sc_memh;
    633   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    634   1.1       jdc 
    635   1.1       jdc 	/*
    636   1.1       jdc 	 * Free any resources we've allocated during the failed attach
    637   1.1       jdc 	 * attempt.  Do this in reverse order and fall through.
    638   1.1       jdc 	 */
    639   1.1       jdc 	switch (sc->sc_att_stage) {
    640   1.1       jdc 	case CAS_ATT_FINISHED:
    641   1.3       jdc 		bus_space_write_4(t, h, CAS_INTMASK, ~(uint32_t)0);
    642   1.1       jdc 		pmf_device_deregister(self);
    643   1.1       jdc 		cas_stop(&sc->sc_ethercom.ec_if, 1);
    644   1.1       jdc 		evcnt_detach(&sc->sc_ev_intr);
    645   1.1       jdc 
    646   1.1       jdc 		rnd_detach_source(&sc->rnd_source);
    647   1.1       jdc 
    648   1.1       jdc 		ether_ifdetach(ifp);
    649   1.1       jdc 		if_detach(ifp);
    650   1.1       jdc 		ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    651   1.1       jdc 
    652   1.1       jdc 		callout_destroy(&sc->sc_tick_ch);
    653   1.1       jdc 
    654   1.1       jdc 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    655   1.1       jdc 
    656   1.1       jdc 		/*FALLTHROUGH*/
    657   1.1       jdc 	case CAS_ATT_MII:
    658   1.1       jdc 	case CAS_ATT_7:
    659   1.1       jdc 	case CAS_ATT_6:
    660   1.1       jdc 		for (i = 0; i < CAS_NTXDESC; i++) {
    661   1.1       jdc 			if (sc->sc_txd[i].sd_map != NULL)
    662   1.1       jdc 				bus_dmamap_destroy(sc->sc_dmatag,
    663   1.1       jdc 				    sc->sc_txd[i].sd_map);
    664   1.1       jdc 		}
    665   1.1       jdc 		/*FALLTHROUGH*/
    666   1.1       jdc 	case CAS_ATT_5:
    667   1.1       jdc 		for (i = 0; i < CAS_NRXDESC; i++) {
    668   1.1       jdc 			if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    669   1.1       jdc 				bus_dmamap_unload(sc->sc_dmatag,
    670   1.3       jdc 				    sc->sc_rxsoft[i].rxs_dmamap);
    671   1.1       jdc 			if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    672   1.1       jdc 				bus_dmamap_destroy(sc->sc_dmatag,
    673   1.1       jdc 				    sc->sc_rxsoft[i].rxs_dmamap);
    674   1.1       jdc 			if (sc->sc_rxsoft[i].rxs_kva != NULL)
    675   1.1       jdc 				bus_dmamem_unmap(sc->sc_dmatag,
    676   1.1       jdc 				    sc->sc_rxsoft[i].rxs_kva, CAS_PAGE_SIZE);
    677   1.1       jdc 			/* XXX   need to check that bus_dmamem_alloc suceeded
    678   1.1       jdc 			if (sc->sc_rxsoft[i].rxs_dmaseg != NULL)
    679   1.1       jdc 			*/
    680   1.1       jdc 				bus_dmamem_free(sc->sc_dmatag,
    681   1.1       jdc 				    &(sc->sc_rxsoft[i].rxs_dmaseg), 1);
    682   1.1       jdc 		}
    683   1.1       jdc 		bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
    684   1.1       jdc 		/*FALLTHROUGH*/
    685   1.1       jdc 	case CAS_ATT_4:
    686   1.1       jdc 	case CAS_ATT_3:
    687   1.1       jdc 		bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
    688   1.1       jdc 		/*FALLTHROUGH*/
    689   1.1       jdc 	case CAS_ATT_2:
    690   1.1       jdc 		bus_dmamem_unmap(sc->sc_dmatag, sc->sc_control_data,
    691   1.1       jdc 		    sizeof(struct cas_control_data));
    692   1.1       jdc 		/*FALLTHROUGH*/
    693   1.1       jdc 	case CAS_ATT_1:
    694   1.1       jdc 		bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
    695   1.1       jdc 		/*FALLTHROUGH*/
    696   1.1       jdc 	case CAS_ATT_0:
    697   1.1       jdc 		sc->sc_att_stage = CAS_ATT_0;
    698   1.1       jdc 		/*FALLTHROUGH*/
    699   1.1       jdc 	case CAS_ATT_BACKEND_2:
    700   1.1       jdc 	case CAS_ATT_BACKEND_1:
    701   1.1       jdc 		if (sc->sc_ih != NULL) {
    702   1.1       jdc 			pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    703   1.1       jdc 			sc->sc_ih = NULL;
    704   1.1       jdc 		}
    705   1.1       jdc 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_size);
    706   1.1       jdc 		/*FALLTHROUGH*/
    707   1.1       jdc 	case CAS_ATT_BACKEND_0:
    708   1.1       jdc 		break;
    709   1.1       jdc 	}
    710   1.1       jdc 	return 0;
    711   1.1       jdc }
    712   1.1       jdc 
    713   1.1       jdc static void
    714   1.1       jdc cas_partial_detach(struct cas_softc *sc, enum cas_attach_stage stage)
    715   1.1       jdc {
    716   1.1       jdc 	cfattach_t ca = device_cfattach(sc->sc_dev);
    717   1.1       jdc 
    718   1.1       jdc 	sc->sc_att_stage = stage;
    719   1.1       jdc 	(*ca->ca_detach)(sc->sc_dev, 0);
    720   1.1       jdc }
    721   1.1       jdc 
    722   1.1       jdc void
    723   1.1       jdc cas_tick(void *arg)
    724   1.1       jdc {
    725   1.1       jdc 	struct cas_softc *sc = arg;
    726   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    727   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
    728   1.1       jdc 	bus_space_handle_t mac = sc->sc_memh;
    729   1.1       jdc 	int s;
    730   1.1       jdc 	u_int32_t v;
    731   1.1       jdc 
    732   1.1       jdc 	/* unload collisions counters */
    733   1.1       jdc 	v = bus_space_read_4(t, mac, CAS_MAC_EXCESS_COLL_CNT) +
    734   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_LATE_COLL_CNT);
    735   1.1       jdc 	ifp->if_collisions += v +
    736   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_NORM_COLL_CNT) +
    737   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_FIRST_COLL_CNT);
    738   1.1       jdc 	ifp->if_oerrors += v;
    739   1.1       jdc 
    740   1.1       jdc 	/* read error counters */
    741   1.1       jdc 	ifp->if_ierrors +=
    742   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_RX_LEN_ERR_CNT) +
    743   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_RX_ALIGN_ERR) +
    744   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_RX_CRC_ERR_CNT) +
    745   1.1       jdc 	    bus_space_read_4(t, mac, CAS_MAC_RX_CODE_VIOL);
    746   1.1       jdc 
    747   1.1       jdc 	/* clear the hardware counters */
    748   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_NORM_COLL_CNT, 0);
    749   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_FIRST_COLL_CNT, 0);
    750   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_EXCESS_COLL_CNT, 0);
    751   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_LATE_COLL_CNT, 0);
    752   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_RX_LEN_ERR_CNT, 0);
    753   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_RX_ALIGN_ERR, 0);
    754   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_RX_CRC_ERR_CNT, 0);
    755   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_RX_CODE_VIOL, 0);
    756   1.1       jdc 
    757   1.1       jdc 	s = splnet();
    758   1.1       jdc 	mii_tick(&sc->sc_mii);
    759   1.1       jdc 	splx(s);
    760   1.1       jdc 
    761   1.1       jdc 	callout_reset(&sc->sc_tick_ch, hz, cas_tick, sc);
    762   1.1       jdc }
    763   1.1       jdc 
    764   1.1       jdc int
    765   1.1       jdc cas_bitwait(struct cas_softc *sc, bus_space_handle_t h, int r,
    766   1.1       jdc     u_int32_t clr, u_int32_t set)
    767   1.1       jdc {
    768   1.1       jdc 	int i;
    769   1.1       jdc 	u_int32_t reg;
    770   1.1       jdc 
    771   1.1       jdc 	for (i = TRIES; i--; DELAY(100)) {
    772   1.1       jdc 		reg = bus_space_read_4(sc->sc_memt, h, r);
    773   1.1       jdc 		if ((reg & clr) == 0 && (reg & set) == set)
    774   1.1       jdc 			return (1);
    775   1.1       jdc 	}
    776   1.1       jdc 
    777   1.1       jdc 	return (0);
    778   1.1       jdc }
    779   1.1       jdc 
    780   1.1       jdc void
    781   1.1       jdc cas_reset(struct cas_softc *sc)
    782   1.1       jdc {
    783   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
    784   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
    785   1.1       jdc 	int s;
    786   1.1       jdc 
    787   1.1       jdc 	s = splnet();
    788   1.1       jdc 	DPRINTF(sc, ("%s: cas_reset\n", device_xname(sc->sc_dev)));
    789   1.1       jdc 	cas_reset_rx(sc);
    790   1.1       jdc 	cas_reset_tx(sc);
    791   1.1       jdc 
    792   1.9       mrg 	/* Disable interrupts */
    793   1.9       mrg 	bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_INTMASK, ~(uint32_t)0);
    794   1.9       mrg 
    795   1.1       jdc 	/* Do a full reset */
    796   1.1       jdc 	bus_space_write_4(t, h, CAS_RESET,
    797   1.1       jdc 	    CAS_RESET_RX | CAS_RESET_TX | CAS_RESET_BLOCK_PCS);
    798   1.1       jdc 	if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX | CAS_RESET_TX, 0))
    799   1.1       jdc 		aprint_error_dev(sc->sc_dev, "cannot reset device\n");
    800   1.1       jdc 	splx(s);
    801   1.1       jdc }
    802   1.1       jdc 
    803   1.1       jdc 
    804   1.1       jdc /*
    805   1.1       jdc  * cas_rxdrain:
    806   1.1       jdc  *
    807   1.1       jdc  *	Drain the receive queue.
    808   1.1       jdc  */
    809   1.1       jdc void
    810   1.1       jdc cas_rxdrain(struct cas_softc *sc)
    811   1.1       jdc {
    812   1.1       jdc 	/* Nothing to do yet. */
    813   1.1       jdc }
    814   1.1       jdc 
    815   1.1       jdc /*
    816   1.1       jdc  * Reset the whole thing.
    817   1.1       jdc  */
    818   1.1       jdc void
    819   1.1       jdc cas_stop(struct ifnet *ifp, int disable)
    820   1.1       jdc {
    821   1.1       jdc 	struct cas_softc *sc = (struct cas_softc *)ifp->if_softc;
    822   1.1       jdc 	struct cas_sxd *sd;
    823   1.1       jdc 	u_int32_t i;
    824   1.1       jdc 
    825   1.1       jdc 	DPRINTF(sc, ("%s: cas_stop\n", device_xname(sc->sc_dev)));
    826   1.1       jdc 
    827   1.1       jdc 	callout_stop(&sc->sc_tick_ch);
    828   1.1       jdc 
    829   1.1       jdc 	/*
    830   1.1       jdc 	 * Mark the interface down and cancel the watchdog timer.
    831   1.1       jdc 	 */
    832   1.1       jdc 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    833   1.1       jdc 	ifp->if_timer = 0;
    834   1.1       jdc 
    835   1.1       jdc 	mii_down(&sc->sc_mii);
    836   1.1       jdc 
    837   1.1       jdc 	cas_reset_rx(sc);
    838   1.1       jdc 	cas_reset_tx(sc);
    839   1.1       jdc 
    840   1.1       jdc 	/*
    841   1.1       jdc 	 * Release any queued transmit buffers.
    842   1.1       jdc 	 */
    843   1.1       jdc 	for (i = 0; i < CAS_NTXDESC; i++) {
    844   1.1       jdc 		sd = &sc->sc_txd[i];
    845   1.1       jdc 		if (sd->sd_mbuf != NULL) {
    846   1.1       jdc 			bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
    847   1.1       jdc 			    sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    848   1.1       jdc 			bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
    849   1.1       jdc 			m_freem(sd->sd_mbuf);
    850   1.1       jdc 			sd->sd_mbuf = NULL;
    851   1.1       jdc 		}
    852   1.1       jdc 	}
    853   1.1       jdc 	sc->sc_tx_cnt = sc->sc_tx_prod = sc->sc_tx_cons = 0;
    854   1.1       jdc 
    855   1.1       jdc 	if (disable)
    856   1.1       jdc 		cas_rxdrain(sc);
    857   1.1       jdc }
    858   1.1       jdc 
    859   1.1       jdc 
    860   1.1       jdc /*
    861   1.1       jdc  * Reset the receiver
    862   1.1       jdc  */
    863   1.1       jdc int
    864   1.1       jdc cas_reset_rx(struct cas_softc *sc)
    865   1.1       jdc {
    866   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
    867   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
    868   1.1       jdc 
    869   1.1       jdc 	/*
    870   1.1       jdc 	 * Resetting while DMA is in progress can cause a bus hang, so we
    871   1.1       jdc 	 * disable DMA first.
    872   1.1       jdc 	 */
    873   1.1       jdc 	cas_disable_rx(sc);
    874   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_CONFIG, 0);
    875   1.1       jdc 	/* Wait till it finishes */
    876   1.1       jdc 	if (!cas_bitwait(sc, h, CAS_RX_CONFIG, 1, 0))
    877   1.1       jdc 		aprint_error_dev(sc->sc_dev, "cannot disable rx dma\n");
    878   1.1       jdc 	/* Wait 5ms extra. */
    879   1.1       jdc 	delay(5000);
    880   1.1       jdc 
    881   1.1       jdc 	/* Finally, reset the ERX */
    882   1.1       jdc 	bus_space_write_4(t, h, CAS_RESET, CAS_RESET_RX);
    883   1.1       jdc 	/* Wait till it finishes */
    884   1.1       jdc 	if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_RX, 0)) {
    885   1.1       jdc 		aprint_error_dev(sc->sc_dev, "cannot reset receiver\n");
    886   1.1       jdc 		return (1);
    887   1.1       jdc 	}
    888   1.1       jdc 	return (0);
    889   1.1       jdc }
    890   1.1       jdc 
    891   1.1       jdc 
    892   1.1       jdc /*
    893   1.1       jdc  * Reset the transmitter
    894   1.1       jdc  */
    895   1.1       jdc int
    896   1.1       jdc cas_reset_tx(struct cas_softc *sc)
    897   1.1       jdc {
    898   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
    899   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
    900   1.1       jdc 
    901   1.1       jdc 	/*
    902   1.1       jdc 	 * Resetting while DMA is in progress can cause a bus hang, so we
    903   1.1       jdc 	 * disable DMA first.
    904   1.1       jdc 	 */
    905   1.1       jdc 	cas_disable_tx(sc);
    906   1.1       jdc 	bus_space_write_4(t, h, CAS_TX_CONFIG, 0);
    907   1.1       jdc 	/* Wait till it finishes */
    908   1.1       jdc 	if (!cas_bitwait(sc, h, CAS_TX_CONFIG, 1, 0))
    909   1.1       jdc 		aprint_error_dev(sc->sc_dev, "cannot disable tx dma\n");
    910   1.1       jdc 	/* Wait 5ms extra. */
    911   1.1       jdc 	delay(5000);
    912   1.1       jdc 
    913   1.1       jdc 	/* Finally, reset the ETX */
    914   1.1       jdc 	bus_space_write_4(t, h, CAS_RESET, CAS_RESET_TX);
    915   1.1       jdc 	/* Wait till it finishes */
    916   1.1       jdc 	if (!cas_bitwait(sc, h, CAS_RESET, CAS_RESET_TX, 0)) {
    917   1.1       jdc 		aprint_error_dev(sc->sc_dev, "cannot reset transmitter\n");
    918   1.1       jdc 		return (1);
    919   1.1       jdc 	}
    920   1.1       jdc 	return (0);
    921   1.1       jdc }
    922   1.1       jdc 
    923   1.1       jdc /*
    924   1.1       jdc  * Disable receiver.
    925   1.1       jdc  */
    926   1.1       jdc int
    927   1.1       jdc cas_disable_rx(struct cas_softc *sc)
    928   1.1       jdc {
    929   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
    930   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
    931   1.1       jdc 	u_int32_t cfg;
    932   1.1       jdc 
    933   1.1       jdc 	/* Flip the enable bit */
    934   1.1       jdc 	cfg = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
    935   1.1       jdc 	cfg &= ~CAS_MAC_RX_ENABLE;
    936   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, cfg);
    937   1.1       jdc 
    938   1.1       jdc 	/* Wait for it to finish */
    939   1.1       jdc 	return (cas_bitwait(sc, h, CAS_MAC_RX_CONFIG, CAS_MAC_RX_ENABLE, 0));
    940   1.1       jdc }
    941   1.1       jdc 
    942   1.1       jdc /*
    943   1.1       jdc  * Disable transmitter.
    944   1.1       jdc  */
    945   1.1       jdc int
    946   1.1       jdc cas_disable_tx(struct cas_softc *sc)
    947   1.1       jdc {
    948   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
    949   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
    950   1.1       jdc 	u_int32_t cfg;
    951   1.1       jdc 
    952   1.1       jdc 	/* Flip the enable bit */
    953   1.1       jdc 	cfg = bus_space_read_4(t, h, CAS_MAC_TX_CONFIG);
    954   1.1       jdc 	cfg &= ~CAS_MAC_TX_ENABLE;
    955   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_TX_CONFIG, cfg);
    956   1.1       jdc 
    957   1.1       jdc 	/* Wait for it to finish */
    958   1.1       jdc 	return (cas_bitwait(sc, h, CAS_MAC_TX_CONFIG, CAS_MAC_TX_ENABLE, 0));
    959   1.1       jdc }
    960   1.1       jdc 
    961   1.1       jdc /*
    962   1.1       jdc  * Initialize interface.
    963   1.1       jdc  */
    964   1.1       jdc int
    965   1.1       jdc cas_meminit(struct cas_softc *sc)
    966   1.1       jdc {
    967  1.20    martin 	int i;
    968   1.1       jdc 
    969   1.1       jdc 	/*
    970   1.1       jdc 	 * Initialize the transmit descriptor ring.
    971   1.1       jdc 	 */
    972   1.1       jdc 	for (i = 0; i < CAS_NTXDESC; i++) {
    973   1.1       jdc 		sc->sc_txdescs[i].cd_flags = 0;
    974   1.1       jdc 		sc->sc_txdescs[i].cd_addr = 0;
    975   1.1       jdc 	}
    976   1.1       jdc 	CAS_CDTXSYNC(sc, 0, CAS_NTXDESC,
    977   1.1       jdc 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    978   1.1       jdc 
    979   1.1       jdc 	/*
    980   1.1       jdc 	 * Initialize the receive descriptor and receive job
    981   1.1       jdc 	 * descriptor rings.
    982   1.1       jdc 	 */
    983   1.1       jdc 	for (i = 0; i < CAS_NRXDESC; i++)
    984   1.1       jdc 		CAS_INIT_RXDESC(sc, i, i);
    985   1.1       jdc 	sc->sc_rxdptr = 0;
    986   1.1       jdc 	sc->sc_rxptr = 0;
    987   1.1       jdc 
    988   1.1       jdc 	/*
    989   1.1       jdc 	 * Initialize the receive completion ring.
    990   1.1       jdc 	 */
    991   1.1       jdc 	for (i = 0; i < CAS_NRXCOMP; i++) {
    992   1.1       jdc 		sc->sc_rxcomps[i].cc_word[0] = 0;
    993   1.1       jdc 		sc->sc_rxcomps[i].cc_word[1] = 0;
    994   1.1       jdc 		sc->sc_rxcomps[i].cc_word[2] = 0;
    995   1.1       jdc 		sc->sc_rxcomps[i].cc_word[3] = CAS_DMA_WRITE(CAS_RC3_OWN);
    996   1.1       jdc 		CAS_CDRXCSYNC(sc, i,
    997   1.1       jdc 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    998   1.1       jdc 	}
    999   1.1       jdc 
   1000   1.1       jdc 	return (0);
   1001   1.1       jdc }
   1002   1.1       jdc 
   1003   1.1       jdc int
   1004   1.1       jdc cas_ringsize(int sz)
   1005   1.1       jdc {
   1006   1.1       jdc 	switch (sz) {
   1007   1.1       jdc 	case 32:
   1008   1.1       jdc 		return CAS_RING_SZ_32;
   1009   1.1       jdc 	case 64:
   1010   1.1       jdc 		return CAS_RING_SZ_64;
   1011   1.1       jdc 	case 128:
   1012   1.1       jdc 		return CAS_RING_SZ_128;
   1013   1.1       jdc 	case 256:
   1014   1.1       jdc 		return CAS_RING_SZ_256;
   1015   1.1       jdc 	case 512:
   1016   1.1       jdc 		return CAS_RING_SZ_512;
   1017   1.1       jdc 	case 1024:
   1018   1.1       jdc 		return CAS_RING_SZ_1024;
   1019   1.1       jdc 	case 2048:
   1020   1.1       jdc 		return CAS_RING_SZ_2048;
   1021   1.1       jdc 	case 4096:
   1022   1.1       jdc 		return CAS_RING_SZ_4096;
   1023   1.1       jdc 	case 8192:
   1024   1.1       jdc 		return CAS_RING_SZ_8192;
   1025   1.1       jdc 	default:
   1026   1.1       jdc 		aprint_error("cas: invalid Receive Descriptor ring size %d\n",
   1027   1.1       jdc 		    sz);
   1028   1.1       jdc 		return CAS_RING_SZ_32;
   1029   1.1       jdc 	}
   1030   1.1       jdc }
   1031   1.1       jdc 
   1032   1.1       jdc int
   1033   1.1       jdc cas_cringsize(int sz)
   1034   1.1       jdc {
   1035   1.1       jdc 	int i;
   1036   1.1       jdc 
   1037   1.1       jdc 	for (i = 0; i < 9; i++)
   1038   1.1       jdc 		if (sz == (128 << i))
   1039   1.1       jdc 			return i;
   1040   1.1       jdc 
   1041   1.1       jdc 	aprint_error("cas: invalid completion ring size %d\n", sz);
   1042   1.1       jdc 	return 128;
   1043   1.1       jdc }
   1044   1.1       jdc 
   1045   1.1       jdc /*
   1046   1.1       jdc  * Initialization of interface; set up initialization block
   1047   1.1       jdc  * and transmit/receive descriptor rings.
   1048   1.1       jdc  */
   1049   1.1       jdc int
   1050   1.1       jdc cas_init(struct ifnet *ifp)
   1051   1.1       jdc {
   1052   1.1       jdc 	struct cas_softc *sc = (struct cas_softc *)ifp->if_softc;
   1053   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1054   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
   1055   1.1       jdc 	int s;
   1056   1.1       jdc 	u_int max_frame_size;
   1057   1.1       jdc 	u_int32_t v;
   1058   1.1       jdc 
   1059   1.1       jdc 	s = splnet();
   1060   1.1       jdc 
   1061   1.1       jdc 	DPRINTF(sc, ("%s: cas_init: calling stop\n", device_xname(sc->sc_dev)));
   1062   1.1       jdc 	/*
   1063   1.1       jdc 	 * Initialization sequence. The numbered steps below correspond
   1064   1.1       jdc 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
   1065   1.1       jdc 	 * Channel Engine manual (part of the PCIO manual).
   1066   1.1       jdc 	 * See also the STP2002-STQ document from Sun Microsystems.
   1067   1.1       jdc 	 */
   1068   1.1       jdc 
   1069   1.1       jdc 	/* step 1 & 2. Reset the Ethernet Channel */
   1070   1.1       jdc 	cas_stop(ifp, 0);
   1071   1.1       jdc 	cas_reset(sc);
   1072   1.1       jdc 	DPRINTF(sc, ("%s: cas_init: restarting\n", device_xname(sc->sc_dev)));
   1073   1.1       jdc 
   1074   1.1       jdc 	/* Re-initialize the MIF */
   1075   1.1       jdc 	cas_mifinit(sc);
   1076   1.1       jdc 
   1077   1.1       jdc 	/* step 3. Setup data structures in host memory */
   1078   1.1       jdc 	cas_meminit(sc);
   1079   1.1       jdc 
   1080   1.1       jdc 	/* step 4. TX MAC registers & counters */
   1081   1.1       jdc 	cas_init_regs(sc);
   1082   1.1       jdc 	max_frame_size = ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN;
   1083   1.1       jdc 	v = (max_frame_size) | (0x2000 << 16) /* Burst size */;
   1084   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_MAC_MAX_FRAME, v);
   1085   1.1       jdc 
   1086   1.1       jdc 	/* step 5. RX MAC registers & counters */
   1087   1.1       jdc 	cas_iff(sc);
   1088   1.1       jdc 
   1089   1.1       jdc 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
   1090   1.1       jdc 	KASSERT((CAS_CDTXADDR(sc, 0) & 0x1fff) == 0);
   1091   1.1       jdc 	bus_space_write_4(t, h, CAS_TX_RING_PTR_HI,
   1092   1.1       jdc 	    (((uint64_t)CAS_CDTXADDR(sc,0)) >> 32));
   1093   1.1       jdc 	bus_space_write_4(t, h, CAS_TX_RING_PTR_LO, CAS_CDTXADDR(sc, 0));
   1094   1.1       jdc 
   1095   1.1       jdc 	KASSERT((CAS_CDRXADDR(sc, 0) & 0x1fff) == 0);
   1096   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_DRING_PTR_HI,
   1097   1.1       jdc 	    (((uint64_t)CAS_CDRXADDR(sc,0)) >> 32));
   1098   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_DRING_PTR_LO, CAS_CDRXADDR(sc, 0));
   1099   1.1       jdc 
   1100   1.1       jdc 	KASSERT((CAS_CDRXCADDR(sc, 0) & 0x1fff) == 0);
   1101   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_CRING_PTR_HI,
   1102   1.1       jdc 	    (((uint64_t)CAS_CDRXCADDR(sc,0)) >> 32));
   1103   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_CRING_PTR_LO, CAS_CDRXCADDR(sc, 0));
   1104   1.1       jdc 
   1105   1.1       jdc 	if (CAS_PLUS(sc)) {
   1106   1.1       jdc 		KASSERT((CAS_CDRXADDR2(sc, 0) & 0x1fff) == 0);
   1107   1.1       jdc 		bus_space_write_4(t, h, CAS_RX_DRING_PTR_HI2,
   1108   1.1       jdc 		    (((uint64_t)CAS_CDRXADDR2(sc,0)) >> 32));
   1109   1.1       jdc 		bus_space_write_4(t, h, CAS_RX_DRING_PTR_LO2,
   1110   1.1       jdc 		    CAS_CDRXADDR2(sc, 0));
   1111   1.1       jdc 	}
   1112   1.1       jdc 
   1113   1.1       jdc 	/* step 8. Global Configuration & Interrupt Mask */
   1114   1.3       jdc 	cas_estintr(sc, CAS_INTR_REG);
   1115   1.1       jdc 
   1116   1.1       jdc 	/* step 9. ETX Configuration: use mostly default values */
   1117   1.1       jdc 
   1118   1.1       jdc 	/* Enable DMA */
   1119   1.1       jdc 	v = cas_ringsize(CAS_NTXDESC /*XXX*/) << 10;
   1120   1.1       jdc 	bus_space_write_4(t, h, CAS_TX_CONFIG,
   1121   1.1       jdc 	    v|CAS_TX_CONFIG_TXDMA_EN|(1<<24)|(1<<29));
   1122   1.1       jdc 	bus_space_write_4(t, h, CAS_TX_KICK, 0);
   1123   1.1       jdc 
   1124   1.1       jdc 	/* step 10. ERX Configuration */
   1125   1.1       jdc 
   1126   1.1       jdc 	/* Encode Receive Descriptor ring size */
   1127   1.1       jdc 	v = cas_ringsize(CAS_NRXDESC) << CAS_RX_CONFIG_RXDRNG_SZ_SHIFT;
   1128   1.1       jdc 	if (CAS_PLUS(sc))
   1129   1.1       jdc 		v |= cas_ringsize(32) << CAS_RX_CONFIG_RXDRNG2_SZ_SHIFT;
   1130   1.1       jdc 
   1131   1.1       jdc 	/* Encode Receive Completion ring size */
   1132   1.1       jdc 	v |= cas_cringsize(CAS_NRXCOMP) << CAS_RX_CONFIG_RXCRNG_SZ_SHIFT;
   1133   1.1       jdc 
   1134   1.1       jdc 	/* Enable DMA */
   1135   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_CONFIG,
   1136   1.1       jdc 	    v|(2<<CAS_RX_CONFIG_FBOFF_SHFT)|CAS_RX_CONFIG_RXDMA_EN);
   1137   1.1       jdc 
   1138   1.1       jdc 	/*
   1139   1.1       jdc 	 * The following value is for an OFF Threshold of about 3/4 full
   1140   1.1       jdc 	 * and an ON Threshold of 1/4 full.
   1141   1.1       jdc 	 */
   1142   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_PAUSE_THRESH,
   1143   1.1       jdc 	    (3 * sc->sc_rxfifosize / 256) |
   1144   1.1       jdc 	    ((sc->sc_rxfifosize / 256) << 12));
   1145   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_BLANKING, (6 << 12) | 6);
   1146   1.1       jdc 
   1147   1.1       jdc 	/* step 11. Configure Media */
   1148   1.1       jdc 	mii_ifmedia_change(&sc->sc_mii);
   1149   1.1       jdc 
   1150   1.1       jdc 	/* step 12. RX_MAC Configuration Register */
   1151   1.1       jdc 	v = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
   1152   1.1       jdc 	v |= CAS_MAC_RX_ENABLE | CAS_MAC_RX_STRIP_CRC;
   1153   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, v);
   1154   1.1       jdc 
   1155   1.1       jdc 	/* step 14. Issue Transmit Pending command */
   1156   1.1       jdc 
   1157   1.1       jdc 	/* step 15.  Give the receiver a swift kick */
   1158   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_KICK, CAS_NRXDESC-4);
   1159   1.1       jdc 	if (CAS_PLUS(sc))
   1160   1.1       jdc 		bus_space_write_4(t, h, CAS_RX_KICK2, 4);
   1161   1.1       jdc 
   1162   1.1       jdc 	/* Start the one second timer. */
   1163   1.1       jdc 	callout_reset(&sc->sc_tick_ch, hz, cas_tick, sc);
   1164   1.1       jdc 
   1165   1.1       jdc 	ifp->if_flags |= IFF_RUNNING;
   1166   1.1       jdc 	ifp->if_flags &= ~IFF_OACTIVE;
   1167   1.1       jdc 	ifp->if_timer = 0;
   1168   1.1       jdc 	splx(s);
   1169   1.1       jdc 
   1170   1.1       jdc 	return (0);
   1171   1.1       jdc }
   1172   1.1       jdc 
   1173   1.1       jdc void
   1174   1.1       jdc cas_init_regs(struct cas_softc *sc)
   1175   1.1       jdc {
   1176   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1177   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1178   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
   1179   1.1       jdc 	const u_char *laddr = CLLADDR(ifp->if_sadl);
   1180   1.1       jdc 	u_int32_t v, r;
   1181   1.1       jdc 
   1182   1.1       jdc 	/* These regs are not cleared on reset */
   1183   1.1       jdc 	sc->sc_inited = 0;
   1184   1.1       jdc 	if (!sc->sc_inited) {
   1185   1.1       jdc 		/* Load recommended values  */
   1186   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_IPG0, 0x00);
   1187   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_IPG1, 0x08);
   1188   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_IPG2, 0x04);
   1189   1.1       jdc 
   1190   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
   1191   1.1       jdc 		/* Max frame and max burst size */
   1192   1.1       jdc 		v = ETHER_MAX_LEN | (0x2000 << 16) /* Burst size */;
   1193   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_MAC_MAX_FRAME, v);
   1194   1.1       jdc 
   1195   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_PREAMBLE_LEN, 0x07);
   1196   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_JAM_SIZE, 0x04);
   1197   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ATTEMPT_LIMIT, 0x10);
   1198   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_CONTROL_TYPE, 0x8088);
   1199   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_RANDOM_SEED,
   1200   1.1       jdc 		    ((laddr[5]<<8)|laddr[4])&0x3ff);
   1201   1.1       jdc 
   1202   1.1       jdc 		/* Secondary MAC addresses set to 0:0:0:0:0:0 */
   1203   1.1       jdc 		for (r = CAS_MAC_ADDR3; r < CAS_MAC_ADDR42; r += 4)
   1204   1.3       jdc 			bus_space_write_4(t, h, r, 0);
   1205   1.1       jdc 
   1206   1.1       jdc 		/* MAC control addr set to 0:1:c2:0:1:80 */
   1207   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADDR42, 0x0001);
   1208   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADDR43, 0xc200);
   1209   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADDR44, 0x0180);
   1210   1.1       jdc 
   1211   1.1       jdc 		/* MAC filter addr set to 0:0:0:0:0:0 */
   1212   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER0, 0);
   1213   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER1, 0);
   1214   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADDR_FILTER2, 0);
   1215   1.1       jdc 
   1216   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADR_FLT_MASK1_2, 0);
   1217   1.1       jdc 		bus_space_write_4(t, h, CAS_MAC_ADR_FLT_MASK0, 0);
   1218   1.1       jdc 
   1219   1.1       jdc 		/* Hash table initialized to 0 */
   1220   1.1       jdc 		for (r = CAS_MAC_HASH0; r <= CAS_MAC_HASH15; r += 4)
   1221   1.1       jdc 			bus_space_write_4(t, h, r, 0);
   1222   1.1       jdc 
   1223   1.1       jdc 		sc->sc_inited = 1;
   1224   1.1       jdc 	}
   1225   1.1       jdc 
   1226   1.1       jdc 	/* Counters need to be zeroed */
   1227   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_NORM_COLL_CNT, 0);
   1228   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_FIRST_COLL_CNT, 0);
   1229   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_EXCESS_COLL_CNT, 0);
   1230   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_LATE_COLL_CNT, 0);
   1231   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_DEFER_TMR_CNT, 0);
   1232   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_PEAK_ATTEMPTS, 0);
   1233   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_FRAME_COUNT, 0);
   1234   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_LEN_ERR_CNT, 0);
   1235   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_ALIGN_ERR, 0);
   1236   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_CRC_ERR_CNT, 0);
   1237   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_CODE_VIOL, 0);
   1238   1.1       jdc 
   1239   1.1       jdc 	/* Un-pause stuff */
   1240   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_SEND_PAUSE_CMD, 0);
   1241   1.1       jdc 
   1242   1.1       jdc 	/*
   1243   1.1       jdc 	 * Set the station address.
   1244   1.1       jdc 	 */
   1245   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_ADDR0, (laddr[4]<<8) | laddr[5]);
   1246   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_ADDR1, (laddr[2]<<8) | laddr[3]);
   1247   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_ADDR2, (laddr[0]<<8) | laddr[1]);
   1248   1.1       jdc }
   1249   1.1       jdc 
   1250   1.1       jdc /*
   1251   1.1       jdc  * Receive interrupt.
   1252   1.1       jdc  */
   1253   1.1       jdc int
   1254   1.1       jdc cas_rint(struct cas_softc *sc)
   1255   1.1       jdc {
   1256   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1257   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1258   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
   1259   1.1       jdc 	struct cas_rxsoft *rxs;
   1260   1.1       jdc 	struct mbuf *m;
   1261   1.1       jdc 	u_int64_t word[4];
   1262   1.1       jdc 	int len, off, idx;
   1263   1.1       jdc 	int i, skip;
   1264   1.1       jdc 	void *cp;
   1265   1.1       jdc 
   1266   1.1       jdc 	for (i = sc->sc_rxptr;; i = CAS_NEXTRX(i + skip)) {
   1267   1.1       jdc 		CAS_CDRXCSYNC(sc, i,
   1268   1.1       jdc 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1269   1.1       jdc 
   1270   1.1       jdc 		word[0] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[0]);
   1271   1.1       jdc 		word[1] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[1]);
   1272   1.1       jdc 		word[2] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[2]);
   1273   1.1       jdc 		word[3] = CAS_DMA_READ(sc->sc_rxcomps[i].cc_word[3]);
   1274   1.1       jdc 
   1275   1.1       jdc 		/* Stop if the hardware still owns the descriptor. */
   1276   1.1       jdc 		if ((word[0] & CAS_RC0_TYPE) == 0 || word[3] & CAS_RC3_OWN)
   1277   1.1       jdc 			break;
   1278   1.1       jdc 
   1279   1.1       jdc 		len = CAS_RC1_HDR_LEN(word[1]);
   1280   1.1       jdc 		if (len > 0) {
   1281   1.1       jdc 			off = CAS_RC1_HDR_OFF(word[1]);
   1282   1.1       jdc 			idx = CAS_RC1_HDR_IDX(word[1]);
   1283   1.1       jdc 			rxs = &sc->sc_rxsoft[idx];
   1284   1.1       jdc 
   1285   1.1       jdc 			DPRINTF(sc, ("hdr at idx %d, off %d, len %d\n",
   1286   1.1       jdc 			    idx, off, len));
   1287   1.1       jdc 
   1288   1.1       jdc 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1289   1.1       jdc 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1290   1.1       jdc 
   1291   1.1       jdc 			cp = rxs->rxs_kva + off * 256 + ETHER_ALIGN;
   1292  1.28      maxv 			m = m_devget(cp, len, 0, ifp);
   1293  1.19  christos 
   1294   1.1       jdc 			if (word[0] & CAS_RC0_RELEASE_HDR)
   1295   1.1       jdc 				cas_add_rxbuf(sc, idx);
   1296   1.1       jdc 
   1297   1.1       jdc 			if (m != NULL) {
   1298   1.1       jdc 
   1299   1.1       jdc 				/*
   1300   1.1       jdc 				 * Pass this up to any BPF listeners, but only
   1301   1.1       jdc 				 * pass it up the stack if its for us.
   1302   1.1       jdc 				 */
   1303   1.1       jdc 				m->m_pkthdr.csum_flags = 0;
   1304  1.24     ozaki 				if_percpuq_enqueue(ifp->if_percpuq, m);
   1305   1.1       jdc 			} else
   1306   1.1       jdc 				ifp->if_ierrors++;
   1307   1.1       jdc 		}
   1308   1.1       jdc 
   1309   1.1       jdc 		len = CAS_RC0_DATA_LEN(word[0]);
   1310   1.1       jdc 		if (len > 0) {
   1311   1.1       jdc 			off = CAS_RC0_DATA_OFF(word[0]);
   1312   1.1       jdc 			idx = CAS_RC0_DATA_IDX(word[0]);
   1313   1.1       jdc 			rxs = &sc->sc_rxsoft[idx];
   1314   1.1       jdc 
   1315   1.1       jdc 			DPRINTF(sc, ("data at idx %d, off %d, len %d\n",
   1316   1.1       jdc 			    idx, off, len));
   1317   1.1       jdc 
   1318   1.1       jdc 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1319   1.1       jdc 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1320   1.1       jdc 
   1321   1.1       jdc 			/* XXX We should not be copying the packet here. */
   1322   1.1       jdc 			cp = rxs->rxs_kva + off + ETHER_ALIGN;
   1323  1.28      maxv 			m = m_devget(cp, len, 0, ifp);
   1324   1.1       jdc 
   1325   1.1       jdc 			if (word[0] & CAS_RC0_RELEASE_DATA)
   1326   1.1       jdc 				cas_add_rxbuf(sc, idx);
   1327   1.1       jdc 
   1328   1.1       jdc 			if (m != NULL) {
   1329   1.1       jdc 				/*
   1330   1.1       jdc 				 * Pass this up to any BPF listeners, but only
   1331   1.1       jdc 				 * pass it up the stack if its for us.
   1332   1.1       jdc 				 */
   1333   1.1       jdc 				m->m_pkthdr.csum_flags = 0;
   1334  1.24     ozaki 				if_percpuq_enqueue(ifp->if_percpuq, m);
   1335   1.1       jdc 			} else
   1336   1.1       jdc 				ifp->if_ierrors++;
   1337   1.1       jdc 		}
   1338   1.1       jdc 
   1339   1.1       jdc 		if (word[0] & CAS_RC0_SPLIT)
   1340   1.1       jdc 			aprint_error_dev(sc->sc_dev, "split packet\n");
   1341   1.1       jdc 
   1342   1.1       jdc 		skip = CAS_RC0_SKIP(word[0]);
   1343   1.1       jdc 	}
   1344   1.1       jdc 
   1345   1.1       jdc 	while (sc->sc_rxptr != i) {
   1346   1.1       jdc 		sc->sc_rxcomps[sc->sc_rxptr].cc_word[0] = 0;
   1347   1.1       jdc 		sc->sc_rxcomps[sc->sc_rxptr].cc_word[1] = 0;
   1348   1.1       jdc 		sc->sc_rxcomps[sc->sc_rxptr].cc_word[2] = 0;
   1349   1.1       jdc 		sc->sc_rxcomps[sc->sc_rxptr].cc_word[3] =
   1350   1.1       jdc 		    CAS_DMA_WRITE(CAS_RC3_OWN);
   1351   1.1       jdc 		CAS_CDRXCSYNC(sc, sc->sc_rxptr,
   1352   1.1       jdc 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1353   1.1       jdc 
   1354   1.1       jdc 		sc->sc_rxptr = CAS_NEXTRX(sc->sc_rxptr);
   1355   1.1       jdc 	}
   1356   1.1       jdc 
   1357   1.1       jdc 	bus_space_write_4(t, h, CAS_RX_COMP_TAIL, sc->sc_rxptr);
   1358   1.1       jdc 
   1359   1.1       jdc 	DPRINTF(sc, ("cas_rint: done sc->rxptr %d, complete %d\n",
   1360   1.1       jdc 		sc->sc_rxptr, bus_space_read_4(t, h, CAS_RX_COMPLETION)));
   1361   1.1       jdc 
   1362   1.1       jdc 	return (1);
   1363   1.1       jdc }
   1364   1.1       jdc 
   1365   1.1       jdc /*
   1366   1.1       jdc  * cas_add_rxbuf:
   1367   1.1       jdc  *
   1368   1.1       jdc  *	Add a receive buffer to the indicated descriptor.
   1369   1.1       jdc  */
   1370   1.1       jdc int
   1371   1.1       jdc cas_add_rxbuf(struct cas_softc *sc, int idx)
   1372   1.1       jdc {
   1373   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1374   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
   1375   1.1       jdc 
   1376   1.1       jdc 	CAS_INIT_RXDESC(sc, sc->sc_rxdptr, idx);
   1377   1.1       jdc 
   1378   1.1       jdc 	if ((sc->sc_rxdptr % 4) == 0)
   1379   1.1       jdc 		bus_space_write_4(t, h, CAS_RX_KICK, sc->sc_rxdptr);
   1380   1.1       jdc 
   1381   1.1       jdc 	if (++sc->sc_rxdptr == CAS_NRXDESC)
   1382   1.1       jdc 		sc->sc_rxdptr = 0;
   1383   1.1       jdc 
   1384   1.1       jdc 	return (0);
   1385   1.1       jdc }
   1386   1.1       jdc 
   1387   1.1       jdc int
   1388   1.1       jdc cas_eint(struct cas_softc *sc, u_int status)
   1389   1.1       jdc {
   1390   1.1       jdc 	char bits[128];
   1391   1.1       jdc 	if ((status & CAS_INTR_MIF) != 0) {
   1392   1.1       jdc 		DPRINTF(sc, ("%s: link status changed\n",
   1393   1.1       jdc 		    device_xname(sc->sc_dev)));
   1394   1.1       jdc 		return (1);
   1395   1.1       jdc 	}
   1396   1.1       jdc 
   1397   1.1       jdc 	snprintb(bits, sizeof(bits), CAS_INTR_BITS, status);
   1398   1.1       jdc 	printf("%s: status=%s\n", device_xname(sc->sc_dev), bits);
   1399   1.1       jdc 	return (1);
   1400   1.1       jdc }
   1401   1.1       jdc 
   1402   1.1       jdc int
   1403   1.1       jdc cas_pint(struct cas_softc *sc)
   1404   1.1       jdc {
   1405   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1406   1.1       jdc 	bus_space_handle_t seb = sc->sc_memh;
   1407   1.1       jdc 	u_int32_t status;
   1408   1.1       jdc 
   1409   1.1       jdc 	status = bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS);
   1410   1.1       jdc 	status |= bus_space_read_4(t, seb, CAS_MII_INTERRUP_STATUS);
   1411   1.1       jdc #ifdef CAS_DEBUG
   1412   1.1       jdc 	if (status)
   1413   1.1       jdc 		printf("%s: link status changed\n", device_xname(sc->sc_dev));
   1414   1.1       jdc #endif
   1415   1.1       jdc 	return (1);
   1416   1.1       jdc }
   1417   1.1       jdc 
   1418   1.1       jdc int
   1419   1.1       jdc cas_intr(void *v)
   1420   1.1       jdc {
   1421   1.1       jdc 	struct cas_softc *sc = (struct cas_softc *)v;
   1422   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1423   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1424   1.1       jdc 	bus_space_handle_t seb = sc->sc_memh;
   1425   1.1       jdc 	u_int32_t status;
   1426   1.1       jdc 	int r = 0;
   1427   1.1       jdc #ifdef CAS_DEBUG
   1428   1.1       jdc 	char bits[128];
   1429   1.1       jdc #endif
   1430   1.1       jdc 
   1431   1.1       jdc 	sc->sc_ev_intr.ev_count++;
   1432   1.1       jdc 
   1433   1.1       jdc 	status = bus_space_read_4(t, seb, CAS_STATUS);
   1434   1.1       jdc #ifdef CAS_DEBUG
   1435   1.1       jdc 	snprintb(bits, sizeof(bits), CAS_INTR_BITS, status);
   1436   1.1       jdc #endif
   1437   1.1       jdc 	DPRINTF(sc, ("%s: cas_intr: cplt %x status %s\n",
   1438   1.1       jdc 		device_xname(sc->sc_dev), (status>>19), bits));
   1439   1.1       jdc 
   1440   1.1       jdc 	if ((status & CAS_INTR_PCS) != 0)
   1441   1.1       jdc 		r |= cas_pint(sc);
   1442   1.1       jdc 
   1443   1.1       jdc 	if ((status & (CAS_INTR_TX_TAG_ERR | CAS_INTR_RX_TAG_ERR |
   1444   1.1       jdc 	    CAS_INTR_RX_COMP_FULL | CAS_INTR_BERR)) != 0)
   1445   1.1       jdc 		r |= cas_eint(sc, status);
   1446   1.1       jdc 
   1447   1.1       jdc 	if ((status & (CAS_INTR_TX_EMPTY | CAS_INTR_TX_INTME)) != 0)
   1448   1.1       jdc 		r |= cas_tint(sc, status);
   1449   1.1       jdc 
   1450   1.1       jdc 	if ((status & (CAS_INTR_RX_DONE | CAS_INTR_RX_NOBUF)) != 0)
   1451   1.1       jdc 		r |= cas_rint(sc);
   1452   1.1       jdc 
   1453   1.1       jdc 	/* We should eventually do more than just print out error stats. */
   1454   1.1       jdc 	if (status & CAS_INTR_TX_MAC) {
   1455   1.1       jdc 		int txstat = bus_space_read_4(t, seb, CAS_MAC_TX_STATUS);
   1456   1.1       jdc #ifdef CAS_DEBUG
   1457   1.1       jdc 		if (txstat & ~CAS_MAC_TX_XMIT_DONE)
   1458   1.1       jdc 			printf("%s: MAC tx fault, status %x\n",
   1459   1.1       jdc 			    device_xname(sc->sc_dev), txstat);
   1460   1.1       jdc #endif
   1461   1.1       jdc 		if (txstat & (CAS_MAC_TX_UNDERRUN | CAS_MAC_TX_PKT_TOO_LONG))
   1462   1.1       jdc 			cas_init(ifp);
   1463   1.1       jdc 	}
   1464   1.1       jdc 	if (status & CAS_INTR_RX_MAC) {
   1465   1.1       jdc 		int rxstat = bus_space_read_4(t, seb, CAS_MAC_RX_STATUS);
   1466   1.1       jdc #ifdef CAS_DEBUG
   1467   1.3       jdc 		if (rxstat & ~CAS_MAC_RX_DONE)
   1468   1.3       jdc 			printf("%s: MAC rx fault, status %x\n",
   1469   1.3       jdc 			    device_xname(sc->sc_dev), rxstat);
   1470   1.1       jdc #endif
   1471   1.1       jdc 		/*
   1472   1.1       jdc 		 * On some chip revisions CAS_MAC_RX_OVERFLOW happen often
   1473   1.1       jdc 		 * due to a silicon bug so handle them silently.
   1474   1.1       jdc 		 */
   1475   1.1       jdc 		if (rxstat & CAS_MAC_RX_OVERFLOW) {
   1476   1.1       jdc 			ifp->if_ierrors++;
   1477   1.1       jdc 			cas_init(ifp);
   1478   1.1       jdc 		}
   1479   1.1       jdc #ifdef CAS_DEBUG
   1480   1.1       jdc 		else if (rxstat & ~(CAS_MAC_RX_DONE | CAS_MAC_RX_FRAME_CNT))
   1481   1.1       jdc 			printf("%s: MAC rx fault, status %x\n",
   1482   1.1       jdc 			    device_xname(sc->sc_dev), rxstat);
   1483   1.1       jdc #endif
   1484   1.1       jdc 	}
   1485   1.1       jdc 	rnd_add_uint32(&sc->rnd_source, status);
   1486   1.1       jdc 	return (r);
   1487   1.1       jdc }
   1488   1.1       jdc 
   1489   1.1       jdc 
   1490   1.1       jdc void
   1491   1.1       jdc cas_watchdog(struct ifnet *ifp)
   1492   1.1       jdc {
   1493   1.1       jdc 	struct cas_softc *sc = ifp->if_softc;
   1494   1.1       jdc 
   1495   1.1       jdc 	DPRINTF(sc, ("cas_watchdog: CAS_RX_CONFIG %x CAS_MAC_RX_STATUS %x "
   1496   1.1       jdc 		"CAS_MAC_RX_CONFIG %x\n",
   1497   1.1       jdc 		bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_RX_CONFIG),
   1498   1.1       jdc 		bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_MAC_RX_STATUS),
   1499   1.1       jdc 		bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_MAC_RX_CONFIG)));
   1500   1.1       jdc 
   1501   1.1       jdc 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
   1502   1.1       jdc 	++ifp->if_oerrors;
   1503   1.1       jdc 
   1504   1.1       jdc 	/* Try to get more packets going. */
   1505   1.1       jdc 	cas_init(ifp);
   1506   1.1       jdc }
   1507   1.1       jdc 
   1508   1.1       jdc /*
   1509   1.1       jdc  * Initialize the MII Management Interface
   1510   1.1       jdc  */
   1511   1.1       jdc void
   1512   1.1       jdc cas_mifinit(struct cas_softc *sc)
   1513   1.1       jdc {
   1514   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1515   1.1       jdc 	bus_space_handle_t mif = sc->sc_memh;
   1516   1.1       jdc 
   1517   1.1       jdc 	/* Configure the MIF in frame mode */
   1518   1.1       jdc 	sc->sc_mif_config = bus_space_read_4(t, mif, CAS_MIF_CONFIG);
   1519   1.1       jdc 	sc->sc_mif_config &= ~CAS_MIF_CONFIG_BB_ENA;
   1520   1.1       jdc 	bus_space_write_4(t, mif, CAS_MIF_CONFIG, sc->sc_mif_config);
   1521   1.1       jdc }
   1522   1.1       jdc 
   1523   1.1       jdc /*
   1524   1.1       jdc  * MII interface
   1525   1.1       jdc  *
   1526   1.1       jdc  * The Cassini MII interface supports at least three different operating modes:
   1527   1.1       jdc  *
   1528   1.1       jdc  * Bitbang mode is implemented using data, clock and output enable registers.
   1529   1.1       jdc  *
   1530   1.1       jdc  * Frame mode is implemented by loading a complete frame into the frame
   1531   1.1       jdc  * register and polling the valid bit for completion.
   1532   1.1       jdc  *
   1533   1.1       jdc  * Polling mode uses the frame register but completion is indicated by
   1534   1.1       jdc  * an interrupt.
   1535   1.1       jdc  *
   1536   1.1       jdc  */
   1537   1.1       jdc int
   1538  1.30   msaitoh cas_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   1539   1.1       jdc {
   1540   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1541   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1542   1.1       jdc 	bus_space_handle_t mif = sc->sc_memh;
   1543   1.1       jdc 	int n;
   1544   1.1       jdc 	u_int32_t v;
   1545   1.1       jdc 
   1546   1.1       jdc #ifdef CAS_DEBUG
   1547   1.1       jdc 	if (sc->sc_debug)
   1548   1.1       jdc 		printf("cas_mii_readreg: phy %d reg %d\n", phy, reg);
   1549   1.1       jdc #endif
   1550   1.1       jdc 
   1551   1.1       jdc 	/* Construct the frame command */
   1552   1.1       jdc 	v = (reg << CAS_MIF_REG_SHIFT)	| (phy << CAS_MIF_PHY_SHIFT) |
   1553   1.1       jdc 		CAS_MIF_FRAME_READ;
   1554   1.1       jdc 
   1555   1.1       jdc 	bus_space_write_4(t, mif, CAS_MIF_FRAME, v);
   1556   1.1       jdc 	for (n = 0; n < 100; n++) {
   1557   1.1       jdc 		DELAY(1);
   1558   1.1       jdc 		v = bus_space_read_4(t, mif, CAS_MIF_FRAME);
   1559  1.30   msaitoh 		if (v & CAS_MIF_FRAME_TA0) {
   1560  1.30   msaitoh 			*val = v & CAS_MIF_FRAME_DATA;
   1561  1.30   msaitoh 			return 0;
   1562  1.30   msaitoh 		}
   1563   1.1       jdc 	}
   1564   1.1       jdc 
   1565   1.1       jdc 	printf("%s: mii_read timeout\n", device_xname(sc->sc_dev));
   1566  1.30   msaitoh 	return ETIMEDOUT;
   1567   1.1       jdc }
   1568   1.1       jdc 
   1569  1.30   msaitoh int
   1570  1.30   msaitoh cas_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   1571   1.1       jdc {
   1572   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1573   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1574   1.1       jdc 	bus_space_handle_t mif = sc->sc_memh;
   1575   1.1       jdc 	int n;
   1576   1.1       jdc 	u_int32_t v;
   1577   1.1       jdc 
   1578   1.1       jdc #ifdef CAS_DEBUG
   1579   1.1       jdc 	if (sc->sc_debug)
   1580   1.1       jdc 		printf("cas_mii_writereg: phy %d reg %d val %x\n",
   1581   1.1       jdc 			phy, reg, val);
   1582   1.1       jdc #endif
   1583   1.1       jdc 
   1584   1.1       jdc 	/* Construct the frame command */
   1585   1.1       jdc 	v = CAS_MIF_FRAME_WRITE			|
   1586   1.1       jdc 	    (phy << CAS_MIF_PHY_SHIFT)		|
   1587   1.1       jdc 	    (reg << CAS_MIF_REG_SHIFT)		|
   1588   1.1       jdc 	    (val & CAS_MIF_FRAME_DATA);
   1589   1.1       jdc 
   1590   1.1       jdc 	bus_space_write_4(t, mif, CAS_MIF_FRAME, v);
   1591   1.1       jdc 	for (n = 0; n < 100; n++) {
   1592   1.1       jdc 		DELAY(1);
   1593   1.1       jdc 		v = bus_space_read_4(t, mif, CAS_MIF_FRAME);
   1594   1.1       jdc 		if (v & CAS_MIF_FRAME_TA0)
   1595  1.30   msaitoh 			return 0;
   1596   1.1       jdc 	}
   1597   1.1       jdc 
   1598   1.1       jdc 	printf("%s: mii_write timeout\n", device_xname(sc->sc_dev));
   1599  1.30   msaitoh 	return ETIMEDOUT;
   1600   1.1       jdc }
   1601   1.1       jdc 
   1602   1.1       jdc void
   1603  1.18      matt cas_mii_statchg(struct ifnet *ifp)
   1604   1.1       jdc {
   1605  1.18      matt 	struct cas_softc *sc = ifp->if_softc;
   1606   1.1       jdc #ifdef CAS_DEBUG
   1607   1.1       jdc 	int instance = IFM_INST(sc->sc_media.ifm_cur->ifm_media);
   1608   1.1       jdc #endif
   1609   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1610   1.1       jdc 	bus_space_handle_t mac = sc->sc_memh;
   1611   1.1       jdc 	u_int32_t v;
   1612   1.1       jdc 
   1613   1.1       jdc #ifdef CAS_DEBUG
   1614   1.1       jdc 	if (sc->sc_debug)
   1615   1.1       jdc 		printf("cas_mii_statchg: status change: phy = %d\n",
   1616   1.1       jdc 		    sc->sc_phys[instance]);
   1617   1.1       jdc #endif
   1618   1.1       jdc 
   1619   1.1       jdc 	/* Set tx full duplex options */
   1620   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, 0);
   1621   1.1       jdc 	delay(10000); /* reg must be cleared and delay before changing. */
   1622   1.1       jdc 	v = CAS_MAC_TX_ENA_IPG0|CAS_MAC_TX_NGU|CAS_MAC_TX_NGU_LIMIT|
   1623   1.1       jdc 		CAS_MAC_TX_ENABLE;
   1624   1.1       jdc 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
   1625   1.1       jdc 		v |= CAS_MAC_TX_IGN_CARRIER|CAS_MAC_TX_IGN_COLLIS;
   1626   1.1       jdc 	}
   1627   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_TX_CONFIG, v);
   1628   1.1       jdc 
   1629   1.1       jdc 	/* XIF Configuration */
   1630   1.1       jdc 	v = CAS_MAC_XIF_TX_MII_ENA;
   1631   1.1       jdc 	v |= CAS_MAC_XIF_LINK_LED;
   1632   1.1       jdc 
   1633   1.1       jdc 	/* MII needs echo disable if half duplex. */
   1634   1.1       jdc 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   1635   1.1       jdc 		/* turn on full duplex LED */
   1636   1.1       jdc 		v |= CAS_MAC_XIF_FDPLX_LED;
   1637   1.1       jdc 	else
   1638   1.1       jdc 		/* half duplex -- disable echo */
   1639   1.1       jdc 		v |= CAS_MAC_XIF_ECHO_DISABL;
   1640   1.1       jdc 
   1641   1.1       jdc 	switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) {
   1642   1.1       jdc 	case IFM_1000_T:  /* Gigabit using GMII interface */
   1643   1.1       jdc 	case IFM_1000_SX:
   1644   1.1       jdc 		v |= CAS_MAC_XIF_GMII_MODE;
   1645   1.1       jdc 		break;
   1646   1.1       jdc 	default:
   1647   1.1       jdc 		v &= ~CAS_MAC_XIF_GMII_MODE;
   1648   1.1       jdc 	}
   1649   1.1       jdc 	bus_space_write_4(t, mac, CAS_MAC_XIF_CONFIG, v);
   1650   1.1       jdc }
   1651   1.1       jdc 
   1652   1.1       jdc int
   1653  1.30   msaitoh cas_pcs_readreg(device_t self, int phy, int reg, uint16_t *val)
   1654   1.1       jdc {
   1655   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1656   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1657   1.1       jdc 	bus_space_handle_t pcs = sc->sc_memh;
   1658   1.1       jdc 
   1659   1.1       jdc #ifdef CAS_DEBUG
   1660   1.1       jdc 	if (sc->sc_debug)
   1661   1.1       jdc 		printf("cas_pcs_readreg: phy %d reg %d\n", phy, reg);
   1662   1.1       jdc #endif
   1663   1.1       jdc 
   1664   1.1       jdc 	if (phy != CAS_PHYAD_EXTERNAL)
   1665  1.30   msaitoh 		return -1;
   1666   1.1       jdc 
   1667   1.1       jdc 	switch (reg) {
   1668   1.1       jdc 	case MII_BMCR:
   1669   1.1       jdc 		reg = CAS_MII_CONTROL;
   1670   1.1       jdc 		break;
   1671   1.1       jdc 	case MII_BMSR:
   1672   1.1       jdc 		reg = CAS_MII_STATUS;
   1673   1.1       jdc 		break;
   1674   1.1       jdc 	case MII_ANAR:
   1675   1.1       jdc 		reg = CAS_MII_ANAR;
   1676   1.1       jdc 		break;
   1677   1.1       jdc 	case MII_ANLPAR:
   1678   1.1       jdc 		reg = CAS_MII_ANLPAR;
   1679   1.1       jdc 		break;
   1680   1.1       jdc 	case MII_EXTSR:
   1681  1.30   msaitoh 		*val = EXTSR_1000XFDX | EXTSR_1000XHDX;
   1682  1.30   msaitoh 		return 0;
   1683   1.1       jdc 	default:
   1684   1.1       jdc 		return (0);
   1685   1.1       jdc 	}
   1686   1.1       jdc 
   1687  1.30   msaitoh 	*val = bus_space_read_4(t, pcs, reg) & 0xffff;
   1688  1.30   msaitoh 	return 0;
   1689   1.1       jdc }
   1690   1.1       jdc 
   1691  1.30   msaitoh int
   1692  1.30   msaitoh cas_pcs_writereg(device_t self, int phy, int reg, uint16_t val)
   1693   1.1       jdc {
   1694   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1695   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1696   1.1       jdc 	bus_space_handle_t pcs = sc->sc_memh;
   1697   1.1       jdc 	int reset = 0;
   1698   1.1       jdc 
   1699   1.1       jdc #ifdef CAS_DEBUG
   1700   1.1       jdc 	if (sc->sc_debug)
   1701   1.1       jdc 		printf("cas_pcs_writereg: phy %d reg %d val %x\n",
   1702   1.1       jdc 			phy, reg, val);
   1703   1.1       jdc #endif
   1704   1.1       jdc 
   1705   1.1       jdc 	if (phy != CAS_PHYAD_EXTERNAL)
   1706  1.30   msaitoh 		return -1;
   1707   1.1       jdc 
   1708   1.1       jdc 	if (reg == MII_ANAR)
   1709   1.1       jdc 		bus_space_write_4(t, pcs, CAS_MII_CONFIG, 0);
   1710   1.1       jdc 
   1711   1.1       jdc 	switch (reg) {
   1712   1.1       jdc 	case MII_BMCR:
   1713   1.1       jdc 		reset = (val & CAS_MII_CONTROL_RESET);
   1714   1.1       jdc 		reg = CAS_MII_CONTROL;
   1715   1.1       jdc 		break;
   1716   1.1       jdc 	case MII_BMSR:
   1717   1.1       jdc 		reg = CAS_MII_STATUS;
   1718   1.1       jdc 		break;
   1719   1.1       jdc 	case MII_ANAR:
   1720   1.1       jdc 		reg = CAS_MII_ANAR;
   1721   1.1       jdc 		break;
   1722   1.1       jdc 	case MII_ANLPAR:
   1723   1.1       jdc 		reg = CAS_MII_ANLPAR;
   1724   1.1       jdc 		break;
   1725   1.1       jdc 	default:
   1726  1.30   msaitoh 		return 0;
   1727   1.1       jdc 	}
   1728   1.1       jdc 
   1729   1.1       jdc 	bus_space_write_4(t, pcs, reg, val);
   1730   1.1       jdc 
   1731   1.1       jdc 	if (reset)
   1732   1.1       jdc 		cas_bitwait(sc, pcs, CAS_MII_CONTROL, CAS_MII_CONTROL_RESET, 0);
   1733   1.1       jdc 
   1734   1.1       jdc 	if (reg == CAS_MII_ANAR || reset)
   1735   1.1       jdc 		bus_space_write_4(t, pcs, CAS_MII_CONFIG,
   1736   1.1       jdc 		    CAS_MII_CONFIG_ENABLE);
   1737  1.30   msaitoh 
   1738  1.30   msaitoh 	return 0;
   1739   1.1       jdc }
   1740   1.1       jdc 
   1741   1.1       jdc int
   1742   1.1       jdc cas_mediachange(struct ifnet *ifp)
   1743   1.1       jdc {
   1744   1.1       jdc 	struct cas_softc *sc = ifp->if_softc;
   1745   1.1       jdc 	struct mii_data *mii = &sc->sc_mii;
   1746   1.1       jdc 
   1747   1.1       jdc 	if (mii->mii_instance) {
   1748   1.1       jdc 		struct mii_softc *miisc;
   1749   1.1       jdc 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
   1750   1.1       jdc 			mii_phy_reset(miisc);
   1751   1.1       jdc 	}
   1752   1.1       jdc 
   1753   1.1       jdc 	return (mii_mediachg(&sc->sc_mii));
   1754   1.1       jdc }
   1755   1.1       jdc 
   1756   1.1       jdc void
   1757   1.1       jdc cas_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1758   1.1       jdc {
   1759   1.1       jdc 	struct cas_softc *sc = ifp->if_softc;
   1760   1.1       jdc 
   1761   1.1       jdc 	mii_pollstat(&sc->sc_mii);
   1762   1.1       jdc 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1763   1.1       jdc 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1764   1.1       jdc }
   1765   1.1       jdc 
   1766   1.1       jdc /*
   1767   1.1       jdc  * Process an ioctl request.
   1768   1.1       jdc  */
   1769   1.1       jdc int
   1770   1.1       jdc cas_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1771   1.1       jdc {
   1772   1.1       jdc 	struct cas_softc *sc = ifp->if_softc;
   1773   1.1       jdc 	int s, error = 0;
   1774   1.1       jdc 
   1775   1.1       jdc 	s = splnet();
   1776   1.1       jdc 
   1777   1.1       jdc 	if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1778   1.1       jdc 		error = 0;
   1779   1.1       jdc 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   1780   1.1       jdc 			;
   1781   1.1       jdc 		else if (ifp->if_flags & IFF_RUNNING) {
   1782   1.1       jdc 			/*
   1783   1.1       jdc 			 * Multicast list has changed; set the hardware filter
   1784   1.1       jdc 			 * accordingly.
   1785   1.1       jdc 			 */
   1786   1.1       jdc 			cas_iff(sc);
   1787   1.1       jdc 		}
   1788   1.1       jdc 	}
   1789   1.1       jdc 
   1790   1.1       jdc 	splx(s);
   1791   1.1       jdc 	return (error);
   1792   1.1       jdc }
   1793   1.1       jdc 
   1794   1.1       jdc static bool
   1795   1.6    dyoung cas_suspend(device_t self, const pmf_qual_t *qual)
   1796   1.1       jdc {
   1797   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1798   1.3       jdc 	bus_space_tag_t t = sc->sc_memt;
   1799   1.3       jdc 	bus_space_handle_t h = sc->sc_memh;
   1800   1.1       jdc 
   1801   1.3       jdc 	bus_space_write_4(t, h, CAS_INTMASK, ~(uint32_t)0);
   1802   1.1       jdc 	if (sc->sc_ih != NULL) {
   1803   1.1       jdc 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
   1804   1.1       jdc 		sc->sc_ih = NULL;
   1805   1.1       jdc 	}
   1806   1.1       jdc 
   1807   1.1       jdc 	return true;
   1808   1.1       jdc }
   1809   1.1       jdc 
   1810   1.1       jdc static bool
   1811   1.6    dyoung cas_resume(device_t self, const pmf_qual_t *qual)
   1812   1.1       jdc {
   1813   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1814   1.1       jdc 
   1815   1.3       jdc 	return cas_estintr(sc, CAS_INTR_PCI | CAS_INTR_REG);
   1816   1.1       jdc }
   1817   1.1       jdc 
   1818   1.1       jdc static bool
   1819   1.3       jdc cas_estintr(struct cas_softc *sc, int what)
   1820   1.1       jdc {
   1821   1.3       jdc 	bus_space_tag_t t = sc->sc_memt;
   1822   1.3       jdc 	bus_space_handle_t h = sc->sc_memh;
   1823   1.1       jdc 	const char *intrstr = NULL;
   1824  1.21  christos 	char intrbuf[PCI_INTRSTR_LEN];
   1825   1.1       jdc 
   1826   1.3       jdc 	/* PCI interrupts */
   1827   1.3       jdc 	if (what & CAS_INTR_PCI) {
   1828  1.21  christos 		intrstr = pci_intr_string(sc->sc_pc, sc->sc_handle, intrbuf, sizeof(intrbuf));
   1829  1.29  jdolecek 		sc->sc_ih = pci_intr_establish_xname(sc->sc_pc, sc->sc_handle,
   1830  1.29  jdolecek 		    IPL_NET, cas_intr, sc, device_xname(sc->sc_dev));
   1831   1.3       jdc 		if (sc->sc_ih == NULL) {
   1832   1.3       jdc 			aprint_error_dev(sc->sc_dev,
   1833   1.3       jdc 			    "unable to establish interrupt");
   1834   1.3       jdc 			if (intrstr != NULL)
   1835   1.3       jdc 				aprint_error(" at %s", intrstr);
   1836   1.3       jdc 			aprint_error("\n");
   1837   1.3       jdc 			return false;
   1838   1.3       jdc 		}
   1839   1.3       jdc 
   1840   1.3       jdc 		aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
   1841   1.1       jdc 	}
   1842   1.1       jdc 
   1843   1.3       jdc 	/* Interrupt register */
   1844   1.3       jdc 	if (what & CAS_INTR_REG) {
   1845   1.3       jdc 		bus_space_write_4(t, h, CAS_INTMASK,
   1846   1.3       jdc 		    ~(CAS_INTR_TX_INTME|CAS_INTR_TX_EMPTY|
   1847   1.3       jdc 		    CAS_INTR_TX_TAG_ERR|
   1848   1.3       jdc 		    CAS_INTR_RX_DONE|CAS_INTR_RX_NOBUF|
   1849   1.3       jdc 		    CAS_INTR_RX_TAG_ERR|
   1850   1.3       jdc 		    CAS_INTR_RX_COMP_FULL|CAS_INTR_PCS|
   1851   1.3       jdc 		    CAS_INTR_MAC_CONTROL|CAS_INTR_MIF|
   1852   1.3       jdc 		    CAS_INTR_BERR));
   1853   1.3       jdc 		bus_space_write_4(t, h, CAS_MAC_RX_MASK,
   1854   1.3       jdc 		    CAS_MAC_RX_DONE|CAS_MAC_RX_FRAME_CNT);
   1855   1.3       jdc 		bus_space_write_4(t, h, CAS_MAC_TX_MASK, CAS_MAC_TX_XMIT_DONE);
   1856   1.3       jdc 		bus_space_write_4(t, h, CAS_MAC_CONTROL_MASK, 0); /* XXXX */
   1857   1.3       jdc 	}
   1858   1.1       jdc 	return true;
   1859   1.1       jdc }
   1860   1.1       jdc 
   1861   1.1       jdc bool
   1862   1.1       jdc cas_shutdown(device_t self, int howto)
   1863   1.1       jdc {
   1864   1.1       jdc 	struct cas_softc *sc = device_private(self);
   1865   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1866   1.1       jdc 
   1867   1.1       jdc 	cas_stop(ifp, 1);
   1868   1.1       jdc 
   1869   1.1       jdc 	return true;
   1870   1.1       jdc }
   1871   1.1       jdc 
   1872   1.1       jdc void
   1873   1.1       jdc cas_iff(struct cas_softc *sc)
   1874   1.1       jdc {
   1875   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1876   1.1       jdc 	struct ethercom *ec = &sc->sc_ethercom;
   1877   1.1       jdc 	struct ether_multi *enm;
   1878   1.1       jdc 	struct ether_multistep step;
   1879   1.1       jdc 	bus_space_tag_t t = sc->sc_memt;
   1880   1.1       jdc 	bus_space_handle_t h = sc->sc_memh;
   1881   1.1       jdc 	u_int32_t crc, hash[16], rxcfg;
   1882   1.1       jdc 	int i;
   1883   1.1       jdc 
   1884   1.1       jdc 	rxcfg = bus_space_read_4(t, h, CAS_MAC_RX_CONFIG);
   1885   1.1       jdc 	rxcfg &= ~(CAS_MAC_RX_HASH_FILTER | CAS_MAC_RX_PROMISCUOUS |
   1886   1.1       jdc 	    CAS_MAC_RX_PROMISC_GRP);
   1887   1.1       jdc 	ifp->if_flags &= ~IFF_ALLMULTI;
   1888   1.1       jdc 
   1889   1.1       jdc 	if (ifp->if_flags & IFF_PROMISC || ec->ec_multicnt > 0) {
   1890   1.1       jdc 		ifp->if_flags |= IFF_ALLMULTI;
   1891   1.1       jdc 		if (ifp->if_flags & IFF_PROMISC)
   1892   1.1       jdc 			rxcfg |= CAS_MAC_RX_PROMISCUOUS;
   1893   1.1       jdc 		else
   1894   1.1       jdc 			rxcfg |= CAS_MAC_RX_PROMISC_GRP;
   1895   1.1       jdc         } else {
   1896   1.1       jdc 		/*
   1897   1.1       jdc 		 * Set up multicast address filter by passing all multicast
   1898   1.1       jdc 		 * addresses through a crc generator, and then using the
   1899   1.1       jdc 		 * high order 8 bits as an index into the 256 bit logical
   1900   1.1       jdc 		 * address filter.  The high order 4 bits selects the word,
   1901   1.1       jdc 		 * while the other 4 bits select the bit within the word
   1902   1.1       jdc 		 * (where bit 0 is the MSB).
   1903   1.1       jdc 		 */
   1904   1.1       jdc 
   1905   1.1       jdc 		rxcfg |= CAS_MAC_RX_HASH_FILTER;
   1906   1.1       jdc 
   1907   1.1       jdc 		/* Clear hash table */
   1908   1.1       jdc 		for (i = 0; i < 16; i++)
   1909   1.1       jdc 			hash[i] = 0;
   1910   1.1       jdc 
   1911   1.1       jdc 		ETHER_FIRST_MULTI(step, ec, enm);
   1912   1.1       jdc 		while (enm != NULL) {
   1913   1.1       jdc                         crc = ether_crc32_le(enm->enm_addrlo,
   1914   1.1       jdc                             ETHER_ADDR_LEN);
   1915   1.1       jdc 
   1916   1.1       jdc                         /* Just want the 8 most significant bits. */
   1917   1.1       jdc                         crc >>= 24;
   1918   1.1       jdc 
   1919   1.1       jdc                         /* Set the corresponding bit in the filter. */
   1920   1.1       jdc                         hash[crc >> 4] |= 1 << (15 - (crc & 15));
   1921   1.1       jdc 
   1922   1.1       jdc 			ETHER_NEXT_MULTI(step, enm);
   1923   1.1       jdc 		}
   1924   1.1       jdc 
   1925   1.1       jdc 		/* Now load the hash table into the chip (if we are using it) */
   1926   1.1       jdc 		for (i = 0; i < 16; i++) {
   1927   1.1       jdc 			bus_space_write_4(t, h,
   1928   1.1       jdc 			    CAS_MAC_HASH0 + i * (CAS_MAC_HASH1 - CAS_MAC_HASH0),
   1929   1.1       jdc 			    hash[i]);
   1930   1.1       jdc 		}
   1931   1.1       jdc 	}
   1932   1.1       jdc 
   1933   1.1       jdc 	bus_space_write_4(t, h, CAS_MAC_RX_CONFIG, rxcfg);
   1934   1.1       jdc }
   1935   1.1       jdc 
   1936   1.1       jdc int
   1937   1.1       jdc cas_encap(struct cas_softc *sc, struct mbuf *mhead, u_int32_t *bixp)
   1938   1.1       jdc {
   1939   1.1       jdc 	u_int64_t flags;
   1940   1.1       jdc 	u_int32_t cur, frag, i;
   1941   1.1       jdc 	bus_dmamap_t map;
   1942   1.1       jdc 
   1943   1.1       jdc 	cur = frag = *bixp;
   1944   1.1       jdc 	map = sc->sc_txd[cur].sd_map;
   1945   1.1       jdc 
   1946   1.1       jdc 	if (bus_dmamap_load_mbuf(sc->sc_dmatag, map, mhead,
   1947   1.1       jdc 	    BUS_DMA_NOWAIT) != 0) {
   1948   1.1       jdc 		return (ENOBUFS);
   1949   1.1       jdc 	}
   1950   1.1       jdc 
   1951   1.1       jdc 	if ((sc->sc_tx_cnt + map->dm_nsegs) > (CAS_NTXDESC - 2)) {
   1952   1.1       jdc 		bus_dmamap_unload(sc->sc_dmatag, map);
   1953   1.1       jdc 		return (ENOBUFS);
   1954   1.1       jdc 	}
   1955   1.1       jdc 
   1956   1.1       jdc 	bus_dmamap_sync(sc->sc_dmatag, map, 0, map->dm_mapsize,
   1957   1.1       jdc 	    BUS_DMASYNC_PREWRITE);
   1958   1.1       jdc 
   1959   1.1       jdc 	for (i = 0; i < map->dm_nsegs; i++) {
   1960   1.1       jdc 		sc->sc_txdescs[frag].cd_addr =
   1961   1.1       jdc 		    CAS_DMA_WRITE(map->dm_segs[i].ds_addr);
   1962   1.1       jdc 		flags = (map->dm_segs[i].ds_len & CAS_TD_BUFSIZE) |
   1963   1.1       jdc 		    (i == 0 ? CAS_TD_START_OF_PACKET : 0) |
   1964   1.1       jdc 		    ((i == (map->dm_nsegs - 1)) ? CAS_TD_END_OF_PACKET : 0);
   1965   1.1       jdc 		sc->sc_txdescs[frag].cd_flags = CAS_DMA_WRITE(flags);
   1966   1.1       jdc 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_cddmamap,
   1967   1.1       jdc 		    CAS_CDTXOFF(frag), sizeof(struct cas_desc),
   1968   1.1       jdc 		    BUS_DMASYNC_PREWRITE);
   1969   1.1       jdc 		cur = frag;
   1970   1.1       jdc 		if (++frag == CAS_NTXDESC)
   1971   1.1       jdc 			frag = 0;
   1972   1.1       jdc 	}
   1973   1.1       jdc 
   1974   1.1       jdc 	sc->sc_tx_cnt += map->dm_nsegs;
   1975   1.1       jdc 	sc->sc_txd[*bixp].sd_map = sc->sc_txd[cur].sd_map;
   1976   1.1       jdc 	sc->sc_txd[cur].sd_map = map;
   1977   1.1       jdc 	sc->sc_txd[cur].sd_mbuf = mhead;
   1978   1.1       jdc 
   1979   1.1       jdc 	bus_space_write_4(sc->sc_memt, sc->sc_memh, CAS_TX_KICK, frag);
   1980   1.1       jdc 
   1981   1.1       jdc 	*bixp = frag;
   1982   1.1       jdc 
   1983   1.1       jdc 	/* sync descriptors */
   1984   1.1       jdc 
   1985   1.1       jdc 	return (0);
   1986   1.1       jdc }
   1987   1.1       jdc 
   1988   1.1       jdc /*
   1989   1.1       jdc  * Transmit interrupt.
   1990   1.1       jdc  */
   1991   1.1       jdc int
   1992   1.1       jdc cas_tint(struct cas_softc *sc, u_int32_t status)
   1993   1.1       jdc {
   1994   1.1       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1995   1.1       jdc 	struct cas_sxd *sd;
   1996   1.1       jdc 	u_int32_t cons, comp;
   1997   1.1       jdc 
   1998   1.1       jdc 	comp = bus_space_read_4(sc->sc_memt, sc->sc_memh, CAS_TX_COMPLETION);
   1999   1.1       jdc 	cons = sc->sc_tx_cons;
   2000   1.1       jdc 	while (cons != comp) {
   2001   1.1       jdc 		sd = &sc->sc_txd[cons];
   2002   1.1       jdc 		if (sd->sd_mbuf != NULL) {
   2003   1.1       jdc 			bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
   2004   1.1       jdc 			    sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2005   1.1       jdc 			bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
   2006   1.1       jdc 			m_freem(sd->sd_mbuf);
   2007   1.1       jdc 			sd->sd_mbuf = NULL;
   2008   1.1       jdc 			ifp->if_opackets++;
   2009   1.1       jdc 		}
   2010   1.1       jdc 		sc->sc_tx_cnt--;
   2011   1.1       jdc 		if (++cons == CAS_NTXDESC)
   2012   1.1       jdc 			cons = 0;
   2013   1.1       jdc 	}
   2014   1.1       jdc 	sc->sc_tx_cons = cons;
   2015   1.1       jdc 
   2016   1.1       jdc 	if (sc->sc_tx_cnt < CAS_NTXDESC - 2)
   2017   1.1       jdc 		ifp->if_flags &= ~IFF_OACTIVE;
   2018   1.1       jdc 	if (sc->sc_tx_cnt == 0)
   2019   1.1       jdc 		ifp->if_timer = 0;
   2020   1.1       jdc 
   2021  1.25     ozaki 	if_schedule_deferred_start(ifp);
   2022   1.1       jdc 
   2023   1.1       jdc 	return (1);
   2024   1.1       jdc }
   2025   1.1       jdc 
   2026   1.1       jdc void
   2027   1.1       jdc cas_start(struct ifnet *ifp)
   2028   1.1       jdc {
   2029   1.1       jdc 	struct cas_softc *sc = ifp->if_softc;
   2030   1.1       jdc 	struct mbuf *m;
   2031   1.1       jdc 	u_int32_t bix;
   2032   1.1       jdc 
   2033   1.1       jdc 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   2034   1.1       jdc 		return;
   2035   1.1       jdc 
   2036   1.1       jdc 	bix = sc->sc_tx_prod;
   2037   1.1       jdc 	while (sc->sc_txd[bix].sd_mbuf == NULL) {
   2038   1.1       jdc 		IFQ_POLL(&ifp->if_snd, m);
   2039   1.1       jdc 		if (m == NULL)
   2040   1.1       jdc 			break;
   2041   1.1       jdc 
   2042   1.1       jdc 		/*
   2043   1.1       jdc 		 * If BPF is listening on this interface, let it see the
   2044   1.1       jdc 		 * packet before we commit it to the wire.
   2045   1.1       jdc 		 */
   2046  1.27   msaitoh 		bpf_mtap(ifp, m, BPF_D_OUT);
   2047   1.1       jdc 
   2048   1.1       jdc 		/*
   2049   1.1       jdc 		 * Encapsulate this packet and start it going...
   2050   1.1       jdc 		 * or fail...
   2051   1.1       jdc 		 */
   2052   1.1       jdc 		if (cas_encap(sc, m, &bix)) {
   2053   1.1       jdc 			ifp->if_flags |= IFF_OACTIVE;
   2054   1.1       jdc 			break;
   2055   1.1       jdc 		}
   2056   1.1       jdc 
   2057   1.1       jdc 		IFQ_DEQUEUE(&ifp->if_snd, m);
   2058   1.1       jdc 		ifp->if_timer = 5;
   2059   1.1       jdc 	}
   2060   1.1       jdc 
   2061   1.1       jdc 	sc->sc_tx_prod = bix;
   2062   1.1       jdc }
   2063  1.13  jmcneill 
   2064  1.14  jmcneill MODULE(MODULE_CLASS_DRIVER, if_cas, "pci");
   2065  1.13  jmcneill 
   2066  1.13  jmcneill #ifdef _MODULE
   2067  1.13  jmcneill #include "ioconf.c"
   2068  1.13  jmcneill #endif
   2069  1.13  jmcneill 
   2070  1.13  jmcneill static int
   2071  1.13  jmcneill if_cas_modcmd(modcmd_t cmd, void *opaque)
   2072  1.13  jmcneill {
   2073  1.13  jmcneill 	int error = 0;
   2074  1.13  jmcneill 
   2075  1.13  jmcneill 	switch (cmd) {
   2076  1.13  jmcneill 	case MODULE_CMD_INIT:
   2077  1.13  jmcneill #ifdef _MODULE
   2078  1.13  jmcneill 		error = config_init_component(cfdriver_ioconf_cas,
   2079  1.13  jmcneill 		    cfattach_ioconf_cas, cfdata_ioconf_cas);
   2080  1.13  jmcneill #endif
   2081  1.13  jmcneill 		return error;
   2082  1.13  jmcneill 	case MODULE_CMD_FINI:
   2083  1.13  jmcneill #ifdef _MODULE
   2084  1.13  jmcneill 		error = config_fini_component(cfdriver_ioconf_cas,
   2085  1.13  jmcneill 		    cfattach_ioconf_cas, cfdata_ioconf_cas);
   2086  1.13  jmcneill #endif
   2087  1.13  jmcneill 		return error;
   2088  1.13  jmcneill 	default:
   2089  1.13  jmcneill 		return ENOTTY;
   2090  1.13  jmcneill 	}
   2091  1.13  jmcneill }
   2092