Home | History | Annotate | Line # | Download | only in pci
if_fxp_pci.c revision 1.5
      1 /*	$NetBSD: if_fxp_pci.c,v 1.5 2000/03/16 23:41:40 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * 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 /*
     41  * PCI bus front-end for the Intel i82557 fast Ethernet controller
     42  * driver.  Works with Intel Etherexpress Pro 10+, 100B, 100+ cards.
     43  */
     44 
     45 #include "opt_inet.h"
     46 #include "opt_ns.h"
     47 #include "bpfilter.h"
     48 #include "rnd.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/malloc.h>
     54 #include <sys/kernel.h>
     55 #include <sys/socket.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/device.h>
     59 
     60 #if NRND > 0
     61 #include <sys/rnd.h>
     62 #endif
     63 
     64 #include <machine/endian.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_media.h>
     69 #include <net/if_ether.h>
     70 
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #endif
     74 
     75 #ifdef INET
     76 #include <netinet/in.h>
     77 #include <netinet/if_inarp.h>
     78 #endif
     79 
     80 #ifdef NS
     81 #include <netns/ns.h>
     82 #include <netns/ns_if.h>
     83 #endif
     84 
     85 #include <machine/bus.h>
     86 #include <machine/intr.h>
     87 
     88 #include <dev/mii/miivar.h>
     89 
     90 #include <dev/ic/i82557reg.h>
     91 #include <dev/ic/i82557var.h>
     92 
     93 #include <dev/pci/pcivar.h>
     94 #include <dev/pci/pcireg.h>
     95 #include <dev/pci/pcidevs.h>
     96 
     97 int	fxp_pci_match __P((struct device *, struct cfdata *, void *));
     98 void	fxp_pci_attach __P((struct device *, struct device *, void *));
     99 
    100 struct cfattach fxp_pci_ca = {
    101 	sizeof(struct fxp_softc), fxp_pci_match, fxp_pci_attach
    102 };
    103 
    104 const struct fxp_pci_product {
    105 	u_int32_t	fpp_prodid;	/* PCI product ID */
    106 	const char	*fpp_name;	/* device name */
    107 } fxp_pci_products[] = {
    108 	{ PCI_PRODUCT_INTEL_82557,
    109 	  "Intel i82557 Ethernet" },
    110 	{ PCI_PRODUCT_INTEL_IN_BUSINESS,
    111 	  "Intel InBusiness Ethernet" },
    112 
    113 	{ 0,
    114 	  NULL },
    115 };
    116 
    117 const struct fxp_pci_product *fxp_pci_lookup
    118     __P((const struct pci_attach_args *));
    119 
    120 const struct fxp_pci_product *
    121 fxp_pci_lookup(pa)
    122 	const struct pci_attach_args *pa;
    123 {
    124 	const struct fxp_pci_product *fpp;
    125 
    126 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    127 		return (NULL);
    128 
    129 	for (fpp = fxp_pci_products; fpp->fpp_name != NULL; fpp++)
    130 		if (PCI_PRODUCT(pa->pa_id) == fpp->fpp_prodid)
    131 			return (fpp);
    132 
    133 	return (NULL);
    134 }
    135 
    136 int
    137 fxp_pci_match(parent, match, aux)
    138 	struct device *parent;
    139 	struct cfdata *match;
    140 	void *aux;
    141 {
    142 	struct pci_attach_args *pa = aux;
    143 
    144 	if (fxp_pci_lookup(pa) != NULL)
    145 		return (1);
    146 
    147 	return (0);
    148 }
    149 
    150 void
    151 fxp_pci_attach(parent, self, aux)
    152 	struct device *parent, *self;
    153 	void *aux;
    154 {
    155 	struct fxp_softc *sc = (struct fxp_softc *)self;
    156 	struct pci_attach_args *pa = aux;
    157 	pci_chipset_tag_t pc = pa->pa_pc;
    158 	pci_intr_handle_t ih;
    159 	const struct fxp_pci_product *fpp;
    160 	const char *intrstr = NULL;
    161 	bus_space_tag_t iot, memt;
    162 	bus_space_handle_t ioh, memh;
    163 	int ioh_valid, memh_valid;
    164 	bus_addr_t addr;
    165 	bus_size_t size;
    166 	int flags;
    167 
    168 	sc->sc_enabled = 1;
    169 	sc->sc_enable = NULL;
    170 	sc->sc_disable = NULL;
    171 
    172 	/*
    173 	 * Map control/status registers.
    174 	 */
    175 	ioh_valid = (pci_mapreg_map(pa, FXP_PCI_IOBA,
    176 	    PCI_MAPREG_TYPE_IO, 0,
    177 	    &iot, &ioh, NULL, NULL) == 0);
    178 
    179 	/*
    180 	 * Version 2.1 of the PCI spec, page 196, "Address Maps":
    181 	 *
    182 	 *	Prefetchable
    183 	 *
    184 	 *	Set to one if there are no side effects on reads, the
    185 	 *	device returns all bytes regardless of the byte enables,
    186 	 *	and host bridges can merge processor writes into this
    187 	 *	range without causing errors.  Bit must be set to zero
    188 	 *	otherwise.
    189 	 *
    190 	 * The 82557 incorrectly sets the "prefetchable" bit, resulting
    191 	 * in errors on systems which will do merged reads and writes.
    192 	 * These errors manifest themselves as all-bits-set when reading
    193 	 * from the EEPROM or other < 4 byte registers.
    194 	 *
    195 	 * We must work around this problem by always forcing the mapping
    196 	 * for memory space to be uncacheable.  On systems which cannot
    197 	 * create an uncacheable mapping (because the firmware mapped it
    198 	 * into only cacheable/prefetchable space due to the "prefetchable"
    199 	 * bit), we can fall back onto i/o mapped access.
    200 	 */
    201 	memh_valid = 0;
    202 	memt = pa->pa_memt;
    203 	if (((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) != 0) &&
    204 	    pci_mapreg_info(pa->pa_pc, pa->pa_tag, FXP_PCI_MMBA,
    205 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
    206 	    &addr, &size, &flags) == 0) {
    207 		flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
    208 		if (bus_space_map(memt, addr, size, flags, &memh) == 0)
    209 			memh_valid = 1;
    210 	}
    211 
    212 	if (memh_valid) {
    213 		sc->sc_st = memt;
    214 		sc->sc_sh = memh;
    215 	} else if (ioh_valid) {
    216 		sc->sc_st = iot;
    217 		sc->sc_sh = ioh;
    218 	} else {
    219 		printf(": unable to map device registers\n");
    220 		return;
    221 	}
    222 
    223 	sc->sc_dmat = pa->pa_dmat;
    224 
    225 	fpp = fxp_pci_lookup(pa);
    226 	if (fpp == NULL) {
    227 		printf("\n");
    228 		panic("fxp_pci_attach: impossible");
    229 	}
    230 
    231 	/*
    232 	 * XXX Perhaps report '557, '558, '559 based on revision?
    233 	 */
    234 	printf(": %s, rev %d\n", fpp->fpp_name, PCI_REVISION(pa->pa_class));
    235 
    236 	/* Make sure bus-mastering is enabled. */
    237 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    238 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    239 	    PCI_COMMAND_MASTER_ENABLE);
    240 
    241 	/*
    242 	 * Map and establish our interrupt.
    243 	 */
    244 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    245 	    pa->pa_intrline, &ih)) {
    246 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    247 		return;
    248 	}
    249 	intrstr = pci_intr_string(pc, ih);
    250 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
    251 	if (sc->sc_ih == NULL) {
    252 		printf("%s: couldn't establish interrupt",
    253 		    sc->sc_dev.dv_xname);
    254 		if (intrstr != NULL)
    255 			printf(" at %s", intrstr);
    256 		printf("\n");
    257 		return;
    258 	}
    259 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    260 
    261 	/* Finish off the attach. */
    262 	fxp_attach(sc);
    263 }
    264