Home | History | Annotate | Line # | Download | only in pci
if_re_pci.c revision 1.30.4.1
      1 /*	$NetBSD: if_re_pci.c,v 1.30.4.1 2007/12/26 19:46:54 ad 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 __KERNEL_RCSID(0, "$NetBSD: if_re_pci.c,v 1.30.4.1 2007/12/26 19:46:54 ad Exp $");
     50 
     51 #include "bpfilter.h"
     52 #include "vlan.h"
     53 
     54 #include <sys/types.h>
     55 
     56 #include <sys/param.h>
     57 #include <sys/endian.h>
     58 #include <sys/systm.h>
     59 #include <sys/sockio.h>
     60 #include <sys/mbuf.h>
     61 #include <sys/malloc.h>
     62 #include <sys/kernel.h>
     63 #include <sys/socket.h>
     64 #include <sys/device.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_arp.h>
     68 #include <net/if_dl.h>
     69 #include <net/if_ether.h>
     70 #include <net/if_media.h>
     71 #include <net/if_vlanvar.h>
     72 
     73 #include <sys/bus.h>
     74 
     75 #include <dev/mii/mii.h>
     76 #include <dev/mii/miivar.h>
     77 
     78 #include <dev/pci/pcireg.h>
     79 #include <dev/pci/pcivar.h>
     80 #include <dev/pci/pcidevs.h>
     81 
     82 #include <dev/ic/rtl81x9reg.h>
     83 #include <dev/ic/rtl81x9var.h>
     84 #include <dev/ic/rtl8169var.h>
     85 
     86 struct re_pci_softc {
     87 	struct rtk_softc sc_rtk;
     88 
     89 	void *sc_ih;
     90 	pci_chipset_tag_t sc_pc;
     91 	pcitag_t sc_pcitag;
     92 };
     93 
     94 static int	re_pci_match(struct device *, struct cfdata *, void *);
     95 static void	re_pci_attach(struct device *, struct device *, void *);
     96 
     97 /*
     98  * Various supported device vendors/types and their names.
     99  */
    100 static const struct rtk_type re_devs[] = {
    101 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
    102 	    RTK_8139CPLUS,
    103 	    "RealTek 8139C+ 10/100BaseTX" },
    104 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E,
    105 	    RTK_8101E,
    106 	    "RealTek 8100E/8101E PCIe 10/100BaseTX" },
    107 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168,
    108 	    RTK_8168,
    109 	    "RealTek 8168B/8111B PCIe Gigabit Ethernet" },
    110 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
    111 	    RTK_8169,
    112 	    "RealTek 8169/8110 Gigabit Ethernet" },
    113 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC,
    114 	    RTK_8169,
    115 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
    116 	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT,
    117 	    RTK_8169,
    118 	    "Corega CG-LAPCIGT Gigabit Ethernet" },
    119 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T,
    120 	    RTK_8169,
    121 	    "D-Link DGE-528T Gigabit Ethernet" },
    122 	{ PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902,
    123 	    RTK_8169,
    124 	    "US Robotics (3Com) USR997902 Gigabit Ethernet" },
    125 	{ PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032,
    126 	    RTK_8169,
    127 	    "Linksys EG1032 rev. 3 Gigabit Ethernet" },
    128 	{ 0, 0, 0, NULL }
    129 };
    130 
    131 #define RE_LINKSYS_EG1032_SUBID	0x00241737
    132 
    133 CFATTACH_DECL(re_pci, sizeof(struct re_pci_softc), re_pci_match, re_pci_attach,
    134 	      NULL, NULL);
    135 
    136 /*
    137  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
    138  * IDs against our list and return a device name if we find a match.
    139  */
    140 static int
    141 re_pci_match(struct device *parent, struct cfdata *match, void *aux)
    142 {
    143 	const struct rtk_type	*t;
    144 	struct pci_attach_args	*pa = aux;
    145 	pcireg_t subid;
    146 
    147 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    148 
    149 	/* special-case Linksys EG1032, since rev 2 uses sk(4) */
    150 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
    151 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032) {
    152 		if (subid != RE_LINKSYS_EG1032_SUBID)
    153 			return 0;
    154 	}
    155 	/* Don't match 8139 other than C-PLUS */
    156 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
    157 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139) {
    158 		if (PCI_REVISION(pa->pa_class) != 0x20)
    159 			return 0;
    160 	}
    161 
    162 	t = re_devs;
    163 
    164 	while (t->rtk_name != NULL) {
    165 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    166 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    167 			return 2;	/* defect rtk(4) */
    168 		}
    169 		t++;
    170 	}
    171 
    172 	return 0;
    173 }
    174 
    175 static void
    176 re_pci_attach(struct device *parent, struct device *self, void *aux)
    177 {
    178 	struct re_pci_softc	*psc = (void *)self;
    179 	struct rtk_softc	*sc = &psc->sc_rtk;
    180 	struct pci_attach_args 	*pa = aux;
    181 	pci_chipset_tag_t pc = pa->pa_pc;
    182 	pci_intr_handle_t ih;
    183 	const char *intrstr = NULL;
    184 	const struct rtk_type	*t;
    185 	uint32_t		hwrev;
    186 	int			error = 0;
    187 	pcireg_t		memtype;
    188 	bool			ioh_valid, memh_valid;
    189 	pcireg_t		command;
    190 	bus_space_tag_t		iot, memt;
    191 	bus_space_handle_t	ioh, memh;
    192 	bus_size_t		iosize, memsize, bsize;
    193 
    194 	/*
    195 	 * Map control/status registers.
    196 	 */
    197 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    198 	command |= PCI_COMMAND_MASTER_ENABLE;
    199 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
    200 
    201 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    202 	    &iot, &ioh, NULL, &iosize) == 0);
    203 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTK_PCI_LOMEM);
    204 	switch (memtype) {
    205 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    206 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    207 		memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM,
    208 		    memtype, 0, &memt, &memh, NULL, &memsize) == 0);
    209 		break;
    210 	default:
    211 		memh_valid = 0;
    212 		break;
    213 	}
    214 
    215 	if (ioh_valid) {
    216 		sc->rtk_btag = iot;
    217 		sc->rtk_bhandle = ioh;
    218 		bsize = iosize;
    219 	} else if (memh_valid) {
    220 		sc->rtk_btag = memt;
    221 		sc->rtk_bhandle = memh;
    222 		bsize = memsize;
    223 	} else {
    224 		aprint_error("%s: can't map registers\n", sc->sc_dev.dv_xname);
    225 		return;
    226 	}
    227 
    228 	t = re_devs;
    229 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    230 
    231 	while (t->rtk_name != NULL) {
    232 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    233 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    234 			break;
    235 		}
    236 		t++;
    237 	}
    238 
    239 	if (t->rtk_basetype == RTK_8139CPLUS)
    240 		sc->sc_quirk |= RTKQ_8139CPLUS;
    241 
    242 	if (t->rtk_basetype == RTK_8168 ||
    243 	    t->rtk_basetype == RTK_8101E)
    244 		sc->sc_quirk |= RTKQ_PCIE;
    245 
    246 	aprint_normal(": %s (rev. 0x%02x)\n",
    247 	    t->rtk_name, PCI_REVISION(pa->pa_class));
    248 
    249 	sc->sc_dmat = pa->pa_dmat;
    250 
    251 	/*
    252 	 * No power/enable/disable machinery for PCI attach;
    253 	 * mark the card enabled now.
    254 	 */
    255 	sc->sc_flags |= RTK_ENABLED;
    256 
    257 	/* Hook interrupt last to avoid having to lock softc */
    258 	/* Allocate interrupt */
    259 	if (pci_intr_map(pa, &ih)) {
    260 		aprint_error("%s: couldn't map interrupt\n",
    261 		    sc->sc_dev.dv_xname);
    262 		return;
    263 	}
    264 	intrstr = pci_intr_string(pc, ih);
    265 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
    266 	if (psc->sc_ih == NULL) {
    267 		aprint_error("%s: couldn't establish interrupt",
    268 		    sc->sc_dev.dv_xname);
    269 		if (intrstr != NULL)
    270 			aprint_error(" at %s", intrstr);
    271 		aprint_error("\n");
    272 		return;
    273 	}
    274 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    275 
    276 	re_attach(sc);
    277 
    278 	/*
    279 	 * Perform hardware diagnostic on the original RTL8169.
    280 	 * Some 32-bit cards were incorrectly wired and would
    281 	 * malfunction if plugged into a 64-bit slot.
    282 	 */
    283 	if (hwrev == RTK_HWREV_8169) {
    284 		error = re_diag(sc);
    285 		if (error) {
    286 			aprint_error(
    287 			    "%s: attach aborted due to hardware diag failure\n",
    288 			    sc->sc_dev.dv_xname);
    289 
    290 			re_detach(sc);
    291 
    292 			if (psc->sc_ih != NULL) {
    293 				pci_intr_disestablish(pc, psc->sc_ih);
    294 				psc->sc_ih = NULL;
    295 			}
    296 
    297 			if (ioh_valid)
    298 				bus_space_unmap(iot, ioh, iosize);
    299 			if (memh_valid)
    300 				bus_space_unmap(memt, memh, memsize);
    301 		}
    302 	}
    303 
    304 	if (!pmf_device_register(self, NULL, NULL))
    305 		aprint_error_dev(self, "couldn't establish power handler\n");
    306 	else
    307 		pmf_class_network_register(self, &sc->ethercom.ec_if);
    308 }
    309