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