Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.1
      1  1.1  haya /* $NetBSD: if_rtk_pci.c,v 1.1 2000/05/10 00:19:55 haya Exp $ */
      2  1.1  haya 
      3  1.1  haya /*
      4  1.1  haya  * Copyright (c) 1997, 1998
      5  1.1  haya  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6  1.1  haya  *
      7  1.1  haya  * Redistribution and use in source and binary forms, with or without
      8  1.1  haya  * modification, are permitted provided that the following conditions
      9  1.1  haya  * are met:
     10  1.1  haya  * 1. Redistributions of source code must retain the above copyright
     11  1.1  haya  *    notice, this list of conditions and the following disclaimer.
     12  1.1  haya  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  haya  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  haya  *    documentation and/or other materials provided with the distribution.
     15  1.1  haya  * 3. All advertising materials mentioning features or use of this software
     16  1.1  haya  *    must display the following acknowledgement:
     17  1.1  haya  *	This product includes software developed by Bill Paul.
     18  1.1  haya  * 4. Neither the name of the author nor the names of any co-contributors
     19  1.1  haya  *    may be used to endorse or promote products derived from this software
     20  1.1  haya  *    without specific prior written permission.
     21  1.1  haya  *
     22  1.1  haya  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  haya  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  haya  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  haya  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  1.1  haya  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.1  haya  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.1  haya  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1  haya  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1  haya  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.1  haya  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  1.1  haya  * THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  haya  *
     34  1.1  haya  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35  1.1  haya  */
     36  1.1  haya 
     37  1.1  haya /*
     38  1.1  haya  * RealTek 8129/8139 PCI NIC driver
     39  1.1  haya  *
     40  1.1  haya  * Supports several extremely cheap PCI 10/100 adapters based on
     41  1.1  haya  * the RealTek chipset. Datasheets can be obtained from
     42  1.1  haya  * www.realtek.com.tw.
     43  1.1  haya  *
     44  1.1  haya  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45  1.1  haya  * Electrical Engineering Department
     46  1.1  haya  * Columbia University, New York City
     47  1.1  haya  */
     48  1.1  haya 
     49  1.1  haya /*
     50  1.1  haya  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
     51  1.1  haya  * probably the worst PCI ethernet controller ever made, with the possible
     52  1.1  haya  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
     53  1.1  haya  * DMA, but it has a terrible interface that nullifies any performance
     54  1.1  haya  * gains that bus-master DMA usually offers.
     55  1.1  haya  *
     56  1.1  haya  * For transmission, the chip offers a series of four TX descriptor
     57  1.1  haya  * registers. Each transmit frame must be in a contiguous buffer, aligned
     58  1.1  haya  * on a longword (32-bit) boundary. This means we almost always have to
     59  1.1  haya  * do mbuf copies in order to transmit a frame, except in the unlikely
     60  1.1  haya  * case where a) the packet fits into a single mbuf, and b) the packet
     61  1.1  haya  * is 32-bit aligned within the mbuf's data area. The presence of only
     62  1.1  haya  * four descriptor registers means that we can never have more than four
     63  1.1  haya  * packets queued for transmission at any one time.
     64  1.1  haya  *
     65  1.1  haya  * Reception is not much better. The driver has to allocate a single large
     66  1.1  haya  * buffer area (up to 64K in size) into which the chip will DMA received
     67  1.1  haya  * frames. Because we don't know where within this region received packets
     68  1.1  haya  * will begin or end, we have no choice but to copy data from the buffer
     69  1.1  haya  * area into mbufs in order to pass the packets up to the higher protocol
     70  1.1  haya  * levels.
     71  1.1  haya  *
     72  1.1  haya  * It's impossible given this rotten design to really achieve decent
     73  1.1  haya  * performance at 100Mbps, unless you happen to have a 400Mhz PII or
     74  1.1  haya  * some equally overmuscled CPU to drive it.
     75  1.1  haya  *
     76  1.1  haya  * On the bright side, the 8139 does have a built-in PHY, although
     77  1.1  haya  * rather than using an MDIO serial interface like most other NICs, the
     78  1.1  haya  * PHY registers are directly accessible through the 8139's register
     79  1.1  haya  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
     80  1.1  haya  * filter.
     81  1.1  haya  *
     82  1.1  haya  * The 8129 chip is an older version of the 8139 that uses an external PHY
     83  1.1  haya  * chip. The 8129 has a serial MDIO interface for accessing the MII where
     84  1.1  haya  * the 8139 lets you directly access the on-board PHY registers. We need
     85  1.1  haya  * to select which interface to use depending on the chip type.
     86  1.1  haya  */
     87  1.1  haya 
     88  1.1  haya #include "opt_inet.h"
     89  1.1  haya #include "opt_ns.h"
     90  1.1  haya #include "bpfilter.h"
     91  1.1  haya #include "rnd.h"
     92  1.1  haya 
     93  1.1  haya #include <sys/param.h>
     94  1.1  haya #include <sys/systm.h>
     95  1.1  haya #include <sys/callout.h>
     96  1.1  haya #include <sys/device.h>
     97  1.1  haya #include <sys/sockio.h>
     98  1.1  haya #include <sys/mbuf.h>
     99  1.1  haya #include <sys/malloc.h>
    100  1.1  haya #include <sys/kernel.h>
    101  1.1  haya #include <sys/socket.h>
    102  1.1  haya 
    103  1.1  haya #include <net/if.h>
    104  1.1  haya #include <net/if_arp.h>
    105  1.1  haya #include <net/if_ether.h>
    106  1.1  haya #include <net/if_dl.h>
    107  1.1  haya #include <net/if_media.h>
    108  1.1  haya #ifdef INET
    109  1.1  haya #include <netinet/in.h>
    110  1.1  haya #include <netinet/if_inarp.h>
    111  1.1  haya #endif
    112  1.1  haya #ifdef NS
    113  1.1  haya #include <netns/ns.h>
    114  1.1  haya #include <netns/ns_if.h>
    115  1.1  haya #endif
    116  1.1  haya 
    117  1.1  haya #if NBPFILTER > 0
    118  1.1  haya #include <net/bpf.h>
    119  1.1  haya #endif
    120  1.1  haya #if NRND > 0
    121  1.1  haya #include <sys/rnd.h>
    122  1.1  haya #endif
    123  1.1  haya 
    124  1.1  haya #include <machine/bus.h>
    125  1.1  haya 
    126  1.1  haya #include <dev/pci/pcireg.h>
    127  1.1  haya #include <dev/pci/pcivar.h>
    128  1.1  haya #include <dev/pci/pcidevs.h>
    129  1.1  haya 
    130  1.1  haya #include <dev/mii/mii.h>
    131  1.1  haya #include <dev/mii/miivar.h>
    132  1.1  haya 
    133  1.1  haya /*
    134  1.1  haya  * Default to using PIO access for this driver. On SMP systems,
    135  1.1  haya  * there appear to be problems with memory mapped mode: it looks like
    136  1.1  haya  * doing too many memory mapped access back to back in rapid succession
    137  1.1  haya  * can hang the bus. I'm inclined to blame this on crummy design/construction
    138  1.1  haya  * on the part of RealTek. Memory mapped mode does appear to work on
    139  1.1  haya  * uniprocessor systems though.
    140  1.1  haya  */
    141  1.1  haya #define RL_USEIOSPACE
    142  1.1  haya 
    143  1.1  haya #include <dev/ic/rtl81x9reg.h>
    144  1.1  haya #include <dev/ic/rtl81x9var.h>
    145  1.1  haya 
    146  1.1  haya struct rl_pci_softc {
    147  1.1  haya 	struct rl_softc sc_rl;		/* real rl softc */
    148  1.1  haya 
    149  1.1  haya 	/* PCI-specific goo.*/
    150  1.1  haya 	void *sc_ih;
    151  1.1  haya 	pci_chipset_tag_t sc_pc; 	/* PCI chipset */
    152  1.1  haya 	pcitag_t sc_pcitag;		/* PCI tag */
    153  1.1  haya };
    154  1.1  haya 
    155  1.1  haya static struct rl_type rl_pci_devs[] = {
    156  1.1  haya 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
    157  1.1  haya 		"RealTek 8129 10/100BaseTX" },
    158  1.1  haya 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
    159  1.1  haya 		"RealTek 8139 10/100BaseTX" },
    160  1.1  haya 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
    161  1.1  haya 		"Accton MPX 5030/5038 10/100BaseTX" },
    162  1.1  haya 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
    163  1.1  haya 		"Delta Electronics 8139 10/100BaseTX" },
    164  1.1  haya 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
    165  1.1  haya 		"Addtron Technology 8139 10/100BaseTX" },
    166  1.1  haya #if 0
    167  1.1  haya 	{ SIS_VENDORID, SIS_DEVICEID_8139,
    168  1.1  haya 		"SiS 900 10/100BaseTX" },
    169  1.1  haya #endif
    170  1.1  haya 	{ 0, 0, NULL }
    171  1.1  haya };
    172  1.1  haya 
    173  1.1  haya const struct rl_type *rl_pci_lookup
    174  1.1  haya 	__P((const struct pci_attach_args *));
    175  1.1  haya int rl_pci_match	__P((struct device *, struct cfdata *, void *));
    176  1.1  haya void rl_pci_attach	__P((struct device *, struct device *, void *));
    177  1.1  haya 
    178  1.1  haya struct cfattach rtk_pci_ca = {
    179  1.1  haya 	sizeof(struct rl_pci_softc), rl_pci_match, rl_pci_attach,
    180  1.1  haya };
    181  1.1  haya 
    182  1.1  haya const struct rl_type *
    183  1.1  haya rl_pci_lookup(pa)
    184  1.1  haya 	const struct pci_attach_args *pa;
    185  1.1  haya {
    186  1.1  haya 	struct rl_type		*t;
    187  1.1  haya 
    188  1.1  haya 	for (t = rl_pci_devs; t->rl_name != NULL; t++){
    189  1.1  haya 		if (PCI_VENDOR(pa->pa_id) == t->rl_vid &&
    190  1.1  haya 		    PCI_PRODUCT(pa->pa_id)  == t->rl_did) {
    191  1.1  haya 			return (t);
    192  1.1  haya 		}
    193  1.1  haya 	}
    194  1.1  haya 	return (NULL);
    195  1.1  haya }
    196  1.1  haya 
    197  1.1  haya int
    198  1.1  haya rl_pci_match(parent, match, aux)
    199  1.1  haya 	struct device *parent;
    200  1.1  haya 	struct cfdata *match;
    201  1.1  haya 	void *aux;
    202  1.1  haya {
    203  1.1  haya 	struct pci_attach_args *pa = aux;
    204  1.1  haya 
    205  1.1  haya 	if (rl_pci_lookup(pa) != NULL)
    206  1.1  haya 		return (1);
    207  1.1  haya 	return (0);
    208  1.1  haya }
    209  1.1  haya 
    210  1.1  haya /*
    211  1.1  haya  * Attach the interface. Allocate softc structures, do ifmedia
    212  1.1  haya  * setup and ethernet/BPF attach.
    213  1.1  haya  */
    214  1.1  haya void
    215  1.1  haya rl_pci_attach(parent, self, aux)
    216  1.1  haya 	struct device *parent, *self;
    217  1.1  haya 	void *aux;
    218  1.1  haya {
    219  1.1  haya 	int			s, pmreg;
    220  1.1  haya 	pcireg_t		command;
    221  1.1  haya 	struct rl_pci_softc *psc = (struct rl_pci_softc *)self;
    222  1.1  haya 	struct rl_softc *sc = &psc->sc_rl;
    223  1.1  haya 	struct pci_attach_args *pa = aux;
    224  1.1  haya 	pci_chipset_tag_t pc = pa->pa_pc;
    225  1.1  haya 	pci_intr_handle_t ih;
    226  1.1  haya 	const char *intrstr = NULL;
    227  1.1  haya 	const struct rl_type *t;
    228  1.1  haya 
    229  1.1  haya 	psc->sc_pc = pa->pa_pc;
    230  1.1  haya 	psc->sc_pcitag = pa->pa_tag;
    231  1.1  haya 
    232  1.1  haya 	t = rl_pci_lookup(pa);
    233  1.1  haya 	if (t == NULL) {
    234  1.1  haya 		printf("\n");
    235  1.1  haya 		panic("rl_pci_attach: impossible");
    236  1.1  haya 	}
    237  1.1  haya 	printf(": %s\n", t->rl_name);
    238  1.1  haya 
    239  1.1  haya 	s = splimp();
    240  1.1  haya 
    241  1.1  haya 	/*
    242  1.1  haya 	 * Handle power management nonsense.
    243  1.1  haya 	 */
    244  1.1  haya 
    245  1.1  haya 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    246  1.1  haya 		command = pci_conf_read(pc, pa->pa_tag, pmreg + 4);
    247  1.1  haya 		if (command & RL_PSTATE_MASK) {
    248  1.1  haya 			pcireg_t		iobase, membase, irq;
    249  1.1  haya 
    250  1.1  haya 			/* Save important PCI config data. */
    251  1.1  haya 			iobase = pci_conf_read(pc, pa->pa_tag, RL_PCI_LOIO);
    252  1.1  haya 			membase = pci_conf_read(pc, pa->pa_tag, RL_PCI_LOMEM);
    253  1.1  haya 			irq = pci_conf_read(pc, pa->pa_tag,
    254  1.1  haya 					    PCI_PRODUCT_DELTA_8139);
    255  1.1  haya 
    256  1.1  haya 			/* Reset the power state. */
    257  1.1  haya 			printf("%s: chip is is in D%d power mode "
    258  1.1  haya 			"-- setting to D0\n", sc->sc_dev.dv_xname,
    259  1.1  haya 			       command & RL_PSTATE_MASK);
    260  1.1  haya 			command &= 0xFFFFFFFC;
    261  1.1  haya 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, command);
    262  1.1  haya 
    263  1.1  haya 			/* Restore PCI config data. */
    264  1.1  haya 			pci_conf_write(pc, pa->pa_tag, RL_PCI_LOIO, iobase);
    265  1.1  haya 			pci_conf_write(pc, pa->pa_tag, RL_PCI_LOMEM, membase);
    266  1.1  haya 			pci_conf_write(pc, pa->pa_tag,
    267  1.1  haya 				       PCI_PRODUCT_DELTA_8139, irq);
    268  1.1  haya 		}
    269  1.1  haya 	}
    270  1.1  haya 
    271  1.1  haya 	/*
    272  1.1  haya 	 * Map control/status registers.
    273  1.1  haya 	 */
    274  1.1  haya #ifdef RL_USEIOSPACE
    275  1.1  haya 	if (pci_mapreg_map(pa, RL_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    276  1.1  haya 	    &sc->rl_btag, &sc->rl_bhandle, NULL, NULL)) {
    277  1.1  haya 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    278  1.1  haya 		goto fail;
    279  1.1  haya 	}
    280  1.1  haya #else
    281  1.1  haya 	if (pci_mapreg_map(pa, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    282  1.1  haya 	    &sc->rl_btag, &sc->rl_bhandle, NULL, NULL)) {
    283  1.1  haya 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    284  1.1  haya 		goto fail;
    285  1.1  haya 	}
    286  1.1  haya #endif
    287  1.1  haya 
    288  1.1  haya 	/* Allocate interrupt */
    289  1.1  haya 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    290  1.1  haya 	    pa->pa_intrline, &ih)) {
    291  1.1  haya 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    292  1.1  haya 		goto fail;
    293  1.1  haya 	}
    294  1.1  haya 	intrstr = pci_intr_string(pc, ih);
    295  1.1  haya 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rl_intr, sc);
    296  1.1  haya 	if (psc->sc_ih == NULL) {
    297  1.1  haya 		printf("%s: couldn't establish interrupt",
    298  1.1  haya 		    sc->sc_dev.dv_xname);
    299  1.1  haya 		if (intrstr != NULL)
    300  1.1  haya 			printf(" at %s", intrstr);
    301  1.1  haya 		printf("\n");
    302  1.1  haya 		goto fail;
    303  1.1  haya 	}
    304  1.1  haya 
    305  1.1  haya 	if (t->rl_did == PCI_PRODUCT_REALTEK_RT8139 ||
    306  1.1  haya 	    t->rl_did == PCI_PRODUCT_ACCTON_MPX5030 ||
    307  1.1  haya 	    t->rl_did == PCI_PRODUCT_DELTA_8139 ||
    308  1.1  haya 	    t->rl_did == PCI_PRODUCT_ADDTRON_8139
    309  1.1  haya #if 0
    310  1.1  haya 	    || t->rl_did == SIS_DEVICEID_8139
    311  1.1  haya #endif
    312  1.1  haya 	    ) {
    313  1.1  haya 		sc->rl_type = RL_8139;
    314  1.1  haya 	} else if (t->rl_did == PCI_PRODUCT_REALTEK_RT8129) {
    315  1.1  haya 		sc->rl_type = RL_8129;
    316  1.1  haya 	} else {
    317  1.1  haya 		goto fail;
    318  1.1  haya 	}
    319  1.1  haya 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    320  1.1  haya 
    321  1.1  haya 	sc->sc_dmat = pa->pa_dmat;
    322  1.1  haya 
    323  1.1  haya 	rl_attach(sc);
    324  1.1  haya fail:
    325  1.1  haya 	splx(s);
    326  1.1  haya 	return;
    327  1.1  haya }
    328