Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.3.4.1
      1 /*	$NetBSD: if_rtk_pci.c,v 1.3.4.1 2001/06/11 19:34:39 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  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  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35  */
     36 
     37 /*
     38  * RealTek 8129/8139 PCI NIC driver
     39  *
     40  * Supports several extremely cheap PCI 10/100 adapters based on
     41  * the RealTek chipset. Datasheets can be obtained from
     42  * www.realtek.com.tw.
     43  *
     44  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45  * Electrical Engineering Department
     46  * Columbia University, New York City
     47  */
     48 
     49 /*
     50  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
     51  * probably the worst PCI ethernet controller ever made, with the possible
     52  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
     53  * DMA, but it has a terrible interface that nullifies any performance
     54  * gains that bus-master DMA usually offers.
     55  *
     56  * For transmission, the chip offers a series of four TX descriptor
     57  * registers. Each transmit frame must be in a contiguous buffer, aligned
     58  * on a longword (32-bit) boundary. This means we almost always have to
     59  * do mbuf copies in order to transmit a frame, except in the unlikely
     60  * case where a) the packet fits into a single mbuf, and b) the packet
     61  * is 32-bit aligned within the mbuf's data area. The presence of only
     62  * four descriptor registers means that we can never have more than four
     63  * packets queued for transmission at any one time.
     64  *
     65  * Reception is not much better. The driver has to allocate a single large
     66  * buffer area (up to 64K in size) into which the chip will DMA received
     67  * frames. Because we don't know where within this region received packets
     68  * will begin or end, we have no choice but to copy data from the buffer
     69  * area into mbufs in order to pass the packets up to the higher protocol
     70  * levels.
     71  *
     72  * It's impossible given this rotten design to really achieve decent
     73  * performance at 100Mbps, unless you happen to have a 400Mhz PII or
     74  * some equally overmuscled CPU to drive it.
     75  *
     76  * On the bright side, the 8139 does have a built-in PHY, although
     77  * rather than using an MDIO serial interface like most other NICs, the
     78  * PHY registers are directly accessible through the 8139's register
     79  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
     80  * filter.
     81  *
     82  * The 8129 chip is an older version of the 8139 that uses an external PHY
     83  * chip. The 8129 has a serial MDIO interface for accessing the MII where
     84  * the 8139 lets you directly access the on-board PHY registers. We need
     85  * to select which interface to use depending on the chip type.
     86  */
     87 
     88 #include "opt_inet.h"
     89 #include "opt_ns.h"
     90 #include "bpfilter.h"
     91 #include "rnd.h"
     92 
     93 #include <sys/param.h>
     94 #include <sys/systm.h>
     95 #include <sys/callout.h>
     96 #include <sys/device.h>
     97 #include <sys/sockio.h>
     98 #include <sys/mbuf.h>
     99 #include <sys/malloc.h>
    100 #include <sys/kernel.h>
    101 #include <sys/socket.h>
    102 
    103 #include <net/if.h>
    104 #include <net/if_arp.h>
    105 #include <net/if_ether.h>
    106 #include <net/if_dl.h>
    107 #include <net/if_media.h>
    108 #ifdef INET
    109 #include <netinet/in.h>
    110 #include <netinet/if_inarp.h>
    111 #endif
    112 #ifdef NS
    113 #include <netns/ns.h>
    114 #include <netns/ns_if.h>
    115 #endif
    116 
    117 #if NBPFILTER > 0
    118 #include <net/bpf.h>
    119 #endif
    120 #if NRND > 0
    121 #include <sys/rnd.h>
    122 #endif
    123 
    124 #include <machine/bus.h>
    125 
    126 #include <dev/pci/pcireg.h>
    127 #include <dev/pci/pcivar.h>
    128 #include <dev/pci/pcidevs.h>
    129 
    130 #include <dev/mii/mii.h>
    131 #include <dev/mii/miivar.h>
    132 
    133 /*
    134  * Default to using PIO access for this driver. On SMP systems,
    135  * there appear to be problems with memory mapped mode: it looks like
    136  * doing too many memory mapped access back to back in rapid succession
    137  * can hang the bus. I'm inclined to blame this on crummy design/construction
    138  * on the part of RealTek. Memory mapped mode does appear to work on
    139  * uniprocessor systems though.
    140  */
    141 #define RTK_USEIOSPACE
    142 
    143 #include <dev/ic/rtl81x9reg.h>
    144 #include <dev/ic/rtl81x9var.h>
    145 
    146 struct rtk_pci_softc {
    147 	struct rtk_softc sc_rtk;	/* real rtk softc */
    148 
    149 	/* PCI-specific goo.*/
    150 	void *sc_ih;
    151 	pci_chipset_tag_t sc_pc; 	/* PCI chipset */
    152 	pcitag_t sc_pcitag;		/* PCI tag */
    153 };
    154 
    155 static const struct rtk_type rtk_pci_devs[] = {
    156 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
    157 		"RealTek 8129 10/100BaseTX",
    158 		RTK_8129 },
    159 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
    160 		"RealTek 8139 10/100BaseTX",
    161 		RTK_8139 },
    162 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
    163 		"Accton MPX 5030/5038 10/100BaseTX",
    164 		RTK_8139 },
    165 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
    166 		"Delta Electronics 8139 10/100BaseTX",
    167 		RTK_8139 },
    168 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
    169 		"Addtron Technology 8139 10/100BaseTX",
    170 		RTK_8139 },
    171 	{	PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
    172 		"D-Link Systems DFE 530TX+",
    173 		RTK_8139 },
    174 	{ 0, 0, NULL, 0 }
    175 };
    176 
    177 const struct rtk_type *rtk_pci_lookup __P((const struct pci_attach_args *));
    178 
    179 int	rtk_pci_match __P((struct device *, struct cfdata *, void *));
    180 void	rtk_pci_attach __P((struct device *, struct device *, void *));
    181 
    182 struct cfattach rtk_pci_ca = {
    183 	sizeof(struct rtk_pci_softc), rtk_pci_match, rtk_pci_attach,
    184 };
    185 
    186 const struct rtk_type *
    187 rtk_pci_lookup(pa)
    188 	const struct pci_attach_args *pa;
    189 {
    190 	const struct rtk_type *t;
    191 
    192 	for (t = rtk_pci_devs; t->rtk_name != NULL; t++){
    193 		if (PCI_VENDOR(pa->pa_id) == t->rtk_vid &&
    194 		    PCI_PRODUCT(pa->pa_id) == t->rtk_did) {
    195 			return (t);
    196 		}
    197 	}
    198 	return (NULL);
    199 }
    200 
    201 int
    202 rtk_pci_match(parent, match, aux)
    203 	struct device *parent;
    204 	struct cfdata *match;
    205 	void *aux;
    206 {
    207 	struct pci_attach_args *pa = aux;
    208 
    209 	if (rtk_pci_lookup(pa) != NULL)
    210 		return (1);
    211 
    212 	return (0);
    213 }
    214 
    215 /*
    216  * Attach the interface. Allocate softc structures, do ifmedia
    217  * setup and ethernet/BPF attach.
    218  */
    219 void
    220 rtk_pci_attach(parent, self, aux)
    221 	struct device *parent, *self;
    222 	void *aux;
    223 {
    224 	struct rtk_pci_softc *psc = (struct rtk_pci_softc *)self;
    225 	struct rtk_softc *sc = &psc->sc_rtk;
    226 	pcireg_t command;
    227 	struct pci_attach_args *pa = aux;
    228 	pci_chipset_tag_t pc = pa->pa_pc;
    229 	pci_intr_handle_t ih;
    230 	const char *intrstr = NULL;
    231 	const struct rtk_type *t;
    232 	int pmreg;
    233 
    234 	psc->sc_pc = pa->pa_pc;
    235 	psc->sc_pcitag = pa->pa_tag;
    236 
    237 	t = rtk_pci_lookup(pa);
    238 	if (t == NULL) {
    239 		printf("\n");
    240 		panic("rtk_pci_attach: impossible");
    241 	}
    242 	printf(": %s\n", t->rtk_name);
    243 
    244 	/*
    245 	 * Handle power management nonsense.
    246 	 */
    247 
    248 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    249 		command = pci_conf_read(pc, pa->pa_tag, pmreg + 4);
    250 		if (command & RTK_PSTATE_MASK) {
    251 			pcireg_t iobase, membase, irq;
    252 
    253 			/* Save important PCI config data. */
    254 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    255 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    256 			irq = pci_conf_read(pc, pa->pa_tag,
    257 					    PCI_PRODUCT_DELTA_8139);
    258 
    259 			/* Reset the power state. */
    260 			printf("%s: chip is is in D%d power mode "
    261 			"-- setting to D0\n", sc->sc_dev.dv_xname,
    262 			       command & RTK_PSTATE_MASK);
    263 			command &= 0xFFFFFFFC;
    264 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, command);
    265 
    266 			/* Restore PCI config data. */
    267 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    268 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    269 			pci_conf_write(pc, pa->pa_tag,
    270 				       PCI_PRODUCT_DELTA_8139, irq);
    271 		}
    272 	}
    273 
    274 	/*
    275 	 * Map control/status registers.
    276 	 */
    277 #ifdef RTK_USEIOSPACE
    278 	if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    279 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
    280 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    281 		return;
    282 	}
    283 #else
    284 	if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    285 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
    286 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    287 		return;
    288 	}
    289 #endif
    290 
    291 	/* Allocate interrupt */
    292 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    293 	    pa->pa_intrline, &ih)) {
    294 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    295 		return;
    296 	}
    297 	intrstr = pci_intr_string(pc, ih);
    298 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
    299 	if (psc->sc_ih == NULL) {
    300 		printf("%s: couldn't establish interrupt",
    301 		    sc->sc_dev.dv_xname);
    302 		if (intrstr != NULL)
    303 			printf(" at %s", intrstr);
    304 		printf("\n");
    305 		return;
    306 	}
    307 
    308 	sc->rtk_type = t->rtk_type;
    309 
    310 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    311 
    312 	sc->sc_dmat = pa->pa_dmat;
    313 
    314 	rtk_attach(sc);
    315 }
    316