1 /* $OpenBSD: if_rl_pci.c,v 1.31 2015/03/14 03:38:48 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998 5 * Bill Paul <wpaul@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 35 #include "bpfilter.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/sockio.h> 40 #include <sys/mbuf.h> 41 #include <sys/malloc.h> 42 #include <sys/kernel.h> 43 #include <sys/socket.h> 44 #include <sys/device.h> 45 #include <sys/timeout.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_types.h> 50 51 #include <netinet/in.h> 52 #include <netinet/if_ether.h> 53 54 #include <net/if_media.h> 55 56 #if NBPFILTER > 0 57 #include <net/bpf.h> 58 #endif 59 60 #include <machine/bus.h> 61 62 #include <dev/mii/miivar.h> 63 #include <dev/pci/pcireg.h> 64 #include <dev/pci/pcivar.h> 65 #include <dev/pci/pcidevs.h> 66 67 /* 68 * Default to using PIO access for this driver. On SMP systems, 69 * there appear to be problems with memory mapped mode: it looks like 70 * doing too many memory mapped access back to back in rapid succession 71 * can hang the bus. I'm inclined to blame this on crummy design/construction 72 * on the part of Realtek. Memory mapped mode does appear to work on 73 * uniprocessor systems though. 74 */ 75 #define RL_USEIOSPACE 76 77 #include <dev/ic/rtl81x9reg.h> 78 79 int rl_pci_match(struct device *, void *, void *); 80 void rl_pci_attach(struct device *, struct device *, void *); 81 int rl_pci_detach(struct device *, int); 82 83 struct rl_pci_softc { 84 struct rl_softc psc_softc; 85 pci_chipset_tag_t psc_pc; 86 bus_size_t psc_mapsize; 87 }; 88 89 struct cfattach rl_pci_ca = { 90 sizeof(struct rl_pci_softc), rl_pci_match, rl_pci_attach, rl_pci_detach, 91 rl_activate 92 }; 93 94 const struct pci_matchid rl_pci_devices[] = { 95 { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_FE2000VX }, 96 { PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_5030 }, 97 { PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139 }, 98 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_2CB_TXD }, 99 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CB_TXD }, 100 { PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139 }, 101 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE520TX_C1 }, 102 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS }, 103 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE690TXD }, 104 { PCI_VENDOR_DLINK2, PCI_PRODUCT_DLINK2_DFE530TXPLUS2 }, 105 { PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BS21 }, 106 { PCI_VENDOR_PLANEX, PCI_PRODUCT_PLANEX_FNW_3603_TX }, 107 { PCI_VENDOR_PLANEX, PCI_PRODUCT_PLANEX_FNW_3800_TX }, 108 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129 }, 109 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8138 }, 110 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139D } 111 }; 112 113 int 114 rl_pci_match(struct device *parent, void *match, void *aux) 115 { 116 struct pci_attach_args *pa = aux; 117 118 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK && 119 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139 && 120 PCI_REVISION(pa->pa_class) == 0x10) 121 return (1); 122 123 return (pci_matchbyid((struct pci_attach_args *)aux, rl_pci_devices, 124 nitems(rl_pci_devices))); 125 } 126 127 void 128 rl_pci_attach(struct device *parent, struct device *self, void *aux) 129 { 130 struct rl_pci_softc *psc = (void *)self; 131 struct rl_softc *sc = &psc->psc_softc; 132 struct pci_attach_args *pa = aux; 133 pci_chipset_tag_t pc = pa->pa_pc; 134 pci_intr_handle_t ih; 135 const char *intrstr = NULL; 136 137 /* 138 * Map control/status registers. 139 */ 140 141 #ifdef RL_USEIOSPACE 142 if (pci_mapreg_map(pa, RL_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0, 143 &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->psc_mapsize, 0)) { 144 printf(": can't map i/o space\n"); 145 return; 146 } 147 #else 148 if (pci_mapreg_map(pa, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0, 149 &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->psc_mapsize, 0)){ 150 printf(": can't map mem space\n"); 151 return; 152 } 153 #endif 154 155 /* 156 * Allocate our interrupt. 157 */ 158 if (pci_intr_map(pa, &ih)) { 159 printf(": couldn't map interrupt\n"); 160 bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->psc_mapsize); 161 return; 162 } 163 164 psc->psc_pc = pa->pa_pc; 165 intrstr = pci_intr_string(pc, ih); 166 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rl_intr, sc, 167 self->dv_xname); 168 if (sc->sc_ih == NULL) { 169 printf(": couldn't establish interrupt"); 170 if (intrstr != NULL) 171 printf(" at %s", intrstr); 172 printf("\n"); 173 bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->psc_mapsize); 174 return; 175 } 176 printf(": %s", intrstr); 177 178 sc->sc_dmat = pa->pa_dmat; 179 180 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8129) 181 sc->rl_type = RL_8129; 182 else 183 sc->rl_type = RL_8139; 184 185 rl_attach(sc); 186 } 187 188 int 189 rl_pci_detach(struct device *self, int flags) 190 { 191 struct rl_pci_softc *psc = (void *)self; 192 struct rl_softc *sc = &psc->psc_softc; 193 int rv; 194 195 rv = rl_detach(sc); 196 if (rv) 197 return (rv); 198 199 if (sc->sc_ih != NULL) 200 pci_intr_disestablish(psc->psc_pc, sc->sc_ih); 201 202 bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->psc_mapsize); 203 204 return (0); 205 } 206