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