Home | History | Annotate | Line # | Download | only in pci
if_re_pci.c revision 1.31
      1 /*	$NetBSD: if_re_pci.c,v 1.31 2007/12/09 20:28:09 jmcneill Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998-2003
      5  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
     36 
     37 /*
     38  * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
     39  *
     40  * Written by Bill Paul <wpaul (at) windriver.com>
     41  * Senior Networking Software Engineer
     42  * Wind River Systems
     43  *
     44  * NetBSD bus-specific frontends for written by
     45  * Jonathan Stone <jonathan (at) netbsd.org>
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 
     50 #include "bpfilter.h"
     51 #include "vlan.h"
     52 
     53 #include <sys/types.h>
     54 
     55 #include <sys/param.h>
     56 #include <sys/endian.h>
     57 #include <sys/systm.h>
     58 #include <sys/sockio.h>
     59 #include <sys/mbuf.h>
     60 #include <sys/malloc.h>
     61 #include <sys/kernel.h>
     62 #include <sys/socket.h>
     63 #include <sys/device.h>
     64 
     65 #include <net/if.h>
     66 #include <net/if_arp.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_ether.h>
     69 #include <net/if_media.h>
     70 #include <net/if_vlanvar.h>
     71 
     72 #include <sys/bus.h>
     73 
     74 #include <dev/mii/mii.h>
     75 #include <dev/mii/miivar.h>
     76 
     77 #include <dev/pci/pcireg.h>
     78 #include <dev/pci/pcivar.h>
     79 #include <dev/pci/pcidevs.h>
     80 
     81 #include <dev/ic/rtl81x9reg.h>
     82 #include <dev/ic/rtl81x9var.h>
     83 #include <dev/ic/rtl8169var.h>
     84 
     85 struct re_pci_softc {
     86 	struct rtk_softc sc_rtk;
     87 
     88 	void *sc_ih;
     89 	pci_chipset_tag_t sc_pc;
     90 	pcitag_t sc_pcitag;
     91 };
     92 
     93 static int	re_pci_match(struct device *, struct cfdata *, void *);
     94 static void	re_pci_attach(struct device *, struct device *, void *);
     95 
     96 /*
     97  * Various supported device vendors/types and their names.
     98  */
     99 static const struct rtk_type re_devs[] = {
    100 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
    101 	    RTK_8139CPLUS,
    102 	    "RealTek 8139C+ 10/100BaseTX" },
    103 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E,
    104 	    RTK_8101E,
    105 	    "RealTek 8100E/8101E PCIe 10/100BaseTX" },
    106 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168,
    107 	    RTK_8168,
    108 	    "RealTek 8168B/8111B PCIe Gigabit Ethernet" },
    109 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
    110 	    RTK_8169,
    111 	    "RealTek 8169/8110 Gigabit Ethernet" },
    112 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC,
    113 	    RTK_8169,
    114 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
    115 	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT,
    116 	    RTK_8169,
    117 	    "Corega CG-LAPCIGT Gigabit Ethernet" },
    118 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T,
    119 	    RTK_8169,
    120 	    "D-Link DGE-528T Gigabit Ethernet" },
    121 	{ PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902,
    122 	    RTK_8169,
    123 	    "US Robotics (3Com) USR997902 Gigabit Ethernet" },
    124 	{ PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032,
    125 	    RTK_8169,
    126 	    "Linksys EG1032 rev. 3 Gigabit Ethernet" },
    127 	{ 0, 0, 0, NULL }
    128 };
    129 
    130 #define RE_LINKSYS_EG1032_SUBID	0x00241737
    131 
    132 CFATTACH_DECL(re_pci, sizeof(struct re_pci_softc), re_pci_match, re_pci_attach,
    133 	      NULL, NULL);
    134 
    135 /*
    136  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
    137  * IDs against our list and return a device name if we find a match.
    138  */
    139 static int
    140 re_pci_match(struct device *parent, struct cfdata *match, void *aux)
    141 {
    142 	const struct rtk_type	*t;
    143 	struct pci_attach_args	*pa = aux;
    144 	pcireg_t subid;
    145 
    146 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    147 
    148 	/* special-case Linksys EG1032, since rev 2 uses sk(4) */
    149 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
    150 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032) {
    151 		if (subid != RE_LINKSYS_EG1032_SUBID)
    152 			return 0;
    153 	}
    154 	/* Don't match 8139 other than C-PLUS */
    155 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
    156 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139) {
    157 		if (PCI_REVISION(pa->pa_class) != 0x20)
    158 			return 0;
    159 	}
    160 
    161 	t = re_devs;
    162 
    163 	while (t->rtk_name != NULL) {
    164 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    165 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    166 			return 2;	/* defect rtk(4) */
    167 		}
    168 		t++;
    169 	}
    170 
    171 	return 0;
    172 }
    173 
    174 static void
    175 re_pci_attach(struct device *parent, struct device *self, void *aux)
    176 {
    177 	struct re_pci_softc	*psc = (void *)self;
    178 	struct rtk_softc	*sc = &psc->sc_rtk;
    179 	struct pci_attach_args 	*pa = aux;
    180 	pci_chipset_tag_t pc = pa->pa_pc;
    181 	pci_intr_handle_t ih;
    182 	const char *intrstr = NULL;
    183 	const struct rtk_type	*t;
    184 	uint32_t		hwrev;
    185 	int			error = 0;
    186 	pcireg_t		memtype;
    187 	bool			ioh_valid, memh_valid;
    188 	pcireg_t		command;
    189 	bus_space_tag_t		iot, memt;
    190 	bus_space_handle_t	ioh, memh;
    191 	bus_size_t		iosize, memsize, bsize;
    192 
    193 	/*
    194 	 * Map control/status registers.
    195 	 */
    196 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    197 	command |= PCI_COMMAND_MASTER_ENABLE;
    198 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
    199 
    200 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    201 	    &iot, &ioh, NULL, &iosize) == 0);
    202 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTK_PCI_LOMEM);
    203 	switch (memtype) {
    204 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    205 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    206 		memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM,
    207 		    memtype, 0, &memt, &memh, NULL, &memsize) == 0);
    208 		break;
    209 	default:
    210 		memh_valid = 0;
    211 		break;
    212 	}
    213 
    214 	if (ioh_valid) {
    215 		sc->rtk_btag = iot;
    216 		sc->rtk_bhandle = ioh;
    217 		bsize = iosize;
    218 	} else if (memh_valid) {
    219 		sc->rtk_btag = memt;
    220 		sc->rtk_bhandle = memh;
    221 		bsize = memsize;
    222 	} else {
    223 		aprint_error("%s: can't map registers\n", sc->sc_dev.dv_xname);
    224 		return;
    225 	}
    226 
    227 	t = re_devs;
    228 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    229 
    230 	while (t->rtk_name != NULL) {
    231 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    232 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    233 			break;
    234 		}
    235 		t++;
    236 	}
    237 
    238 	if (t->rtk_basetype == RTK_8139CPLUS)
    239 		sc->sc_quirk |= RTKQ_8139CPLUS;
    240 
    241 	if (t->rtk_basetype == RTK_8168 ||
    242 	    t->rtk_basetype == RTK_8101E)
    243 		sc->sc_quirk |= RTKQ_PCIE;
    244 
    245 	aprint_normal(": %s (rev. 0x%02x)\n",
    246 	    t->rtk_name, PCI_REVISION(pa->pa_class));
    247 
    248 	sc->sc_dmat = pa->pa_dmat;
    249 
    250 	/*
    251 	 * No power/enable/disable machinery for PCI attach;
    252 	 * mark the card enabled now.
    253 	 */
    254 	sc->sc_flags |= RTK_ENABLED;
    255 
    256 	/* Hook interrupt last to avoid having to lock softc */
    257 	/* Allocate interrupt */
    258 	if (pci_intr_map(pa, &ih)) {
    259 		aprint_error("%s: couldn't map interrupt\n",
    260 		    sc->sc_dev.dv_xname);
    261 		return;
    262 	}
    263 	intrstr = pci_intr_string(pc, ih);
    264 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
    265 	if (psc->sc_ih == NULL) {
    266 		aprint_error("%s: couldn't establish interrupt",
    267 		    sc->sc_dev.dv_xname);
    268 		if (intrstr != NULL)
    269 			aprint_error(" at %s", intrstr);
    270 		aprint_error("\n");
    271 		return;
    272 	}
    273 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    274 
    275 	re_attach(sc);
    276 
    277 	/*
    278 	 * Perform hardware diagnostic on the original RTL8169.
    279 	 * Some 32-bit cards were incorrectly wired and would
    280 	 * malfunction if plugged into a 64-bit slot.
    281 	 */
    282 	if (hwrev == RTK_HWREV_8169) {
    283 		error = re_diag(sc);
    284 		if (error) {
    285 			aprint_error(
    286 			    "%s: attach aborted due to hardware diag failure\n",
    287 			    sc->sc_dev.dv_xname);
    288 
    289 			re_detach(sc);
    290 
    291 			if (psc->sc_ih != NULL) {
    292 				pci_intr_disestablish(pc, psc->sc_ih);
    293 				psc->sc_ih = NULL;
    294 			}
    295 
    296 			if (ioh_valid)
    297 				bus_space_unmap(iot, ioh, iosize);
    298 			if (memh_valid)
    299 				bus_space_unmap(memt, memh, memsize);
    300 		}
    301 	}
    302 
    303 	if (!pmf_device_register(self, NULL, NULL))
    304 		aprint_error_dev(self, "couldn't establish power handler\n");
    305 	else
    306 		pmf_class_network_register(self, &sc->ethercom.ec_if);
    307 }
    308