Home | History | Annotate | Line # | Download | only in pci
if_ex_pci.c revision 1.8.8.1
      1  1.8.8.1  wrstuden /*	$NetBSD: if_ex_pci.c,v 1.8.8.1 1999/12/27 18:35:17 wrstuden Exp $	*/
      2      1.1      fvdl 
      3      1.1      fvdl /*-
      4      1.1      fvdl  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5      1.1      fvdl  * All rights reserved.
      6      1.1      fvdl  *
      7      1.1      fvdl  * This code is derived from software contributed to The NetBSD Foundation
      8      1.3   thorpej  * by Frank van der Linden; Jason R. Thorpe of the Numerical Aerospace
      9      1.3   thorpej  * Simulation Facility, NASA Ames Research Center.
     10      1.1      fvdl  *
     11      1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     12      1.1      fvdl  * modification, are permitted provided that the following conditions
     13      1.1      fvdl  * are met:
     14      1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     15      1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     16      1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     18      1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     19      1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     20      1.1      fvdl  *    must display the following acknowledgement:
     21      1.1      fvdl  *	This product includes software developed by the NetBSD
     22      1.1      fvdl  *	Foundation, Inc. and its contributors.
     23      1.1      fvdl  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.1      fvdl  *    contributors may be used to endorse or promote products derived
     25      1.1      fvdl  *    from this software without specific prior written permission.
     26      1.1      fvdl  *
     27      1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.1      fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     38      1.1      fvdl  */
     39      1.1      fvdl 
     40      1.1      fvdl #include "opt_inet.h"
     41      1.1      fvdl #include "opt_ns.h"
     42      1.1      fvdl #include "bpfilter.h"
     43      1.1      fvdl 
     44      1.1      fvdl #include <sys/param.h>
     45      1.1      fvdl #include <sys/systm.h>
     46      1.1      fvdl #include <sys/mbuf.h>
     47      1.1      fvdl #include <sys/socket.h>
     48      1.1      fvdl #include <sys/ioctl.h>
     49      1.1      fvdl #include <sys/errno.h>
     50      1.1      fvdl #include <sys/syslog.h>
     51      1.1      fvdl #include <sys/select.h>
     52      1.1      fvdl #include <sys/device.h>
     53      1.1      fvdl 
     54      1.1      fvdl #include <net/if.h>
     55      1.1      fvdl #include <net/if_dl.h>
     56      1.1      fvdl #include <net/if_ether.h>
     57      1.1      fvdl #include <net/if_media.h>
     58      1.1      fvdl 
     59      1.1      fvdl #ifdef INET
     60      1.1      fvdl #include <netinet/in.h>
     61      1.1      fvdl #include <netinet/in_systm.h>
     62      1.1      fvdl #include <netinet/in_var.h>
     63      1.1      fvdl #include <netinet/ip.h>
     64      1.1      fvdl #include <netinet/if_inarp.h>
     65      1.1      fvdl #endif
     66      1.1      fvdl 
     67      1.1      fvdl #ifdef NS
     68      1.1      fvdl #include <netns/ns.h>
     69      1.1      fvdl #include <netns/ns_if.h>
     70      1.1      fvdl #endif
     71      1.1      fvdl 
     72      1.1      fvdl #if NBPFILTER > 0
     73      1.1      fvdl #include <net/bpf.h>
     74      1.1      fvdl #include <net/bpfdesc.h>
     75      1.1      fvdl #endif
     76      1.1      fvdl 
     77      1.1      fvdl #include <machine/cpu.h>
     78      1.1      fvdl #include <machine/bus.h>
     79      1.1      fvdl #include <machine/intr.h>
     80      1.1      fvdl 
     81      1.1      fvdl #include <dev/mii/miivar.h>
     82      1.1      fvdl #include <dev/mii/mii.h>
     83      1.1      fvdl 
     84      1.1      fvdl #include <dev/ic/elink3var.h>
     85      1.1      fvdl #include <dev/ic/elink3reg.h>
     86      1.1      fvdl #include <dev/ic/elinkxlreg.h>
     87      1.1      fvdl #include <dev/ic/elinkxlvar.h>
     88      1.1      fvdl 
     89      1.1      fvdl #include <dev/pci/pcivar.h>
     90      1.1      fvdl #include <dev/pci/pcireg.h>
     91      1.1      fvdl #include <dev/pci/pcidevs.h>
     92      1.1      fvdl 
     93      1.1      fvdl /*
     94      1.1      fvdl  * PCI constants.
     95      1.1      fvdl  * XXX These should be in a common file!
     96      1.1      fvdl  */
     97      1.1      fvdl #define PCI_CONN		0x48    /* Connector type */
     98      1.1      fvdl #define PCI_CBIO		0x10    /* Configuration Base IO Address */
     99      1.1      fvdl #define PCI_POWERCTL		0xe0
    100      1.1      fvdl 
    101      1.1      fvdl int ex_pci_match __P((struct device *, struct cfdata *, void *));
    102      1.1      fvdl void ex_pci_attach __P((struct device *, struct device *, void *));
    103      1.1      fvdl 
    104      1.1      fvdl struct cfattach ex_pci_ca = {
    105      1.1      fvdl 	sizeof(struct ex_softc), ex_pci_match, ex_pci_attach
    106      1.1      fvdl };
    107      1.1      fvdl 
    108      1.3   thorpej const struct ex_pci_product {
    109      1.3   thorpej 	u_int32_t	epp_prodid;	/* PCI product ID */
    110      1.3   thorpej 	int		epp_flags;	/* initial softc flags */
    111      1.3   thorpej 	const char	*epp_name;	/* device name */
    112      1.3   thorpej } ex_pci_products[] = {
    113      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C900TPO,	0,
    114      1.4   thorpej 	  "3c900-TPO Ethernet" },
    115      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C900COMBO,	0,
    116      1.4   thorpej 	  "3c900-COMBO Ethernet" },
    117      1.3   thorpej 
    118      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C905TX,	EX_CONF_MII,
    119      1.4   thorpej 	  "3c905-TX 10/100 Ethernet" },
    120      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C905T4,	EX_CONF_MII,
    121      1.4   thorpej 	  "3c905-T4 10/100 Ethernet" },
    122      1.3   thorpej 
    123      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C900BTPO,	EX_CONF_90XB,
    124      1.4   thorpej 	  "3c900B-TPO Ethernet" },
    125      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C900BCOMBO,	EX_CONF_90XB,
    126      1.4   thorpej 	  "3c900B-COMBO Ethernet" },
    127      1.6      fvdl 	{ PCI_PRODUCT_3COM_3C900BTPC,   EX_CONF_90XB,
    128      1.6      fvdl 	  "3c900B-TPC Ethernet" },
    129      1.3   thorpej 
    130      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C905BTX,	EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY,
    131      1.4   thorpej 	  "3c905B-TX 10/100 Ethernet" },
    132      1.3   thorpej 	{ PCI_PRODUCT_3COM_3C905BT4,	EX_CONF_90XB|EX_CONF_MII,
    133      1.4   thorpej 	  "3c905B-T4 10/100 Ethernet" },
    134  1.8.8.1  wrstuden 	{ PCI_PRODUCT_3COM_3C905BCOMBO,	EX_CONF_90XB/*|EX_CONF_MII|EX_CONF_INTPHY*/,
    135  1.8.8.1  wrstuden 	  "3c905B-COMBO 10/100 Ethernet" },
    136      1.8      fvdl 	{ PCI_PRODUCT_3COM_3C905BFX,	EX_CONF_90XB,
    137      1.4   thorpej 	  "3c905B-FX 10/100 Ethernet" },
    138      1.4   thorpej 
    139      1.4   thorpej 	/* XXX Internal PHY? */
    140      1.5      ross 	{ PCI_PRODUCT_3COM_3C980SRV,	EX_CONF_90XB|EX_CONF_MII,
    141      1.4   thorpej 	  "3c980 Server Adapter 10/100 Ethernet" },
    142      1.7      ross 	{ PCI_PRODUCT_3COM_3C905CTX,	EX_CONF_90XB|EX_CONF_MII,
    143      1.7      ross 	  "3c905C-TX 10/100 Ethernet with mngmt" },
    144      1.3   thorpej 
    145      1.3   thorpej 	{ 0,				0,
    146      1.3   thorpej 	  NULL },
    147      1.3   thorpej };
    148      1.3   thorpej 
    149      1.3   thorpej const struct ex_pci_product *ex_pci_lookup
    150      1.3   thorpej     __P((const struct pci_attach_args *));
    151      1.3   thorpej 
    152      1.3   thorpej const struct ex_pci_product *
    153      1.3   thorpej ex_pci_lookup(pa)
    154      1.3   thorpej 	const struct pci_attach_args *pa;
    155      1.3   thorpej {
    156      1.3   thorpej 	const struct ex_pci_product *epp;
    157      1.3   thorpej 
    158      1.3   thorpej 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM)
    159      1.3   thorpej 		return (NULL);
    160      1.3   thorpej 
    161      1.3   thorpej 	for (epp = ex_pci_products; epp->epp_name != NULL; epp++)
    162      1.3   thorpej 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
    163      1.3   thorpej 			return (epp);
    164      1.3   thorpej 	return (NULL);
    165      1.3   thorpej }
    166      1.3   thorpej 
    167      1.1      fvdl int
    168      1.1      fvdl ex_pci_match(parent, match, aux)
    169      1.1      fvdl 	struct device *parent;
    170      1.1      fvdl 	struct cfdata *match;
    171      1.1      fvdl 	void *aux;
    172      1.1      fvdl {
    173      1.1      fvdl 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    174      1.1      fvdl 
    175      1.3   thorpej 	if (ex_pci_lookup(pa) != NULL)
    176      1.3   thorpej 		return (2);	/* beat ep_pci */
    177      1.1      fvdl 
    178      1.3   thorpej 	return (0);
    179      1.1      fvdl }
    180      1.1      fvdl 
    181      1.1      fvdl void
    182      1.1      fvdl ex_pci_attach(parent, self, aux)
    183      1.1      fvdl 	struct device *parent, *self;
    184      1.1      fvdl 	void *aux;
    185      1.1      fvdl {
    186      1.1      fvdl 	struct ex_softc *sc = (void *)self;
    187      1.1      fvdl 	struct pci_attach_args *pa = aux;
    188      1.1      fvdl 	pci_chipset_tag_t pc = pa->pa_pc;
    189      1.1      fvdl 	pci_intr_handle_t ih;
    190      1.3   thorpej 	const struct ex_pci_product *epp;
    191      1.1      fvdl 	const char *intrstr = NULL;
    192  1.8.8.1  wrstuden 	int rev, pmode;
    193      1.1      fvdl 
    194      1.1      fvdl 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    195      1.1      fvdl 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    196      1.1      fvdl 		printf(": can't map i/o space\n");
    197      1.1      fvdl 		return;
    198      1.1      fvdl 	}
    199      1.1      fvdl 
    200      1.3   thorpej 	epp = ex_pci_lookup(pa);
    201      1.3   thorpej 	if (epp == NULL) {
    202      1.3   thorpej 		printf("\n");
    203      1.3   thorpej 		panic("ex_pci_attach: impossible");
    204      1.1      fvdl 	}
    205      1.1      fvdl 
    206  1.8.8.1  wrstuden 	rev = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
    207  1.8.8.1  wrstuden 	printf(": 3Com %s (rev. 0x%x)\n", epp->epp_name, rev);
    208      1.1      fvdl 
    209      1.1      fvdl 	sc->enable = NULL;
    210      1.1      fvdl 	sc->disable = NULL;
    211      1.1      fvdl 	sc->enabled = 1;
    212      1.1      fvdl 
    213      1.3   thorpej 	sc->sc_dmat = pa->pa_dmat;
    214      1.3   thorpej 
    215      1.3   thorpej 	sc->ex_bustype = EX_BUS_PCI;
    216      1.3   thorpej 	sc->ex_conf = epp->epp_flags;
    217      1.3   thorpej 
    218      1.1      fvdl 	/* Enable the card. */
    219      1.1      fvdl 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    220      1.1      fvdl 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    221      1.1      fvdl 	    PCI_COMMAND_MASTER_ENABLE);
    222      1.1      fvdl 
    223      1.1      fvdl 	/* Get it out of power save mode if needed (BIOS bugs) */
    224      1.2  drochner 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, 0, 0)) {
    225      1.1      fvdl 		pmode = pci_conf_read(pc, pa->pa_tag, PCI_POWERCTL) & 0x3;
    226      1.1      fvdl 		if (pmode == 3) {
    227      1.1      fvdl 			/*
    228      1.1      fvdl 			 * The card has lost all configuration data in
    229      1.1      fvdl 			 * this state, so punt.
    230      1.1      fvdl 			 */
    231      1.1      fvdl 			printf("%s: unable to wake up from power state D3\n",
    232      1.1      fvdl 			    sc->sc_dev.dv_xname);
    233      1.1      fvdl 			return;
    234      1.1      fvdl 		}
    235      1.1      fvdl 		if (pmode != 0) {
    236      1.1      fvdl 			printf("%s: waking up from power state D%d\n",
    237      1.1      fvdl 			    sc->sc_dev.dv_xname, pmode);
    238      1.1      fvdl 			pci_conf_write(pc, pa->pa_tag, PCI_POWERCTL, 0);
    239      1.1      fvdl 		}
    240      1.1      fvdl 	}
    241      1.1      fvdl 
    242      1.1      fvdl 	/* Map and establish the interrupt. */
    243      1.1      fvdl 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    244      1.1      fvdl 	    pa->pa_intrline, &ih)) {
    245      1.1      fvdl 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    246      1.1      fvdl 		return;
    247      1.1      fvdl 	}
    248      1.1      fvdl 	intrstr = pci_intr_string(pc, ih);
    249      1.1      fvdl 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ex_intr, sc);
    250      1.1      fvdl 	if (sc->sc_ih == NULL) {
    251      1.1      fvdl 		printf("%s: couldn't establish interrupt",
    252      1.1      fvdl 		    sc->sc_dev.dv_xname);
    253      1.1      fvdl 		if (intrstr != NULL)
    254      1.1      fvdl 			printf(" at %s", intrstr);
    255      1.1      fvdl 		printf("\n");
    256      1.1      fvdl 		return;
    257      1.1      fvdl 	}
    258      1.1      fvdl 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    259      1.1      fvdl 
    260      1.1      fvdl 	ex_config(sc);
    261      1.1      fvdl }
    262