if_re_pci.c revision 1.38
1/* $OpenBSD: if_re_pci.c,v 1.38 2013/08/03 15:44:32 kettenis Exp $ */ 2 3/* 4 * Copyright (c) 2005 Peter Valchev <pvalchev@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19/* 20 * PCI front-end for the Realtek 8169 21 */ 22 23#include <sys/param.h> 24#include <sys/endian.h> 25#include <sys/systm.h> 26#include <sys/sockio.h> 27#include <sys/mbuf.h> 28#include <sys/malloc.h> 29#include <sys/kernel.h> 30#include <sys/device.h> 31#include <sys/timeout.h> 32#include <sys/socket.h> 33 34#include <net/if.h> 35#include <net/if_dl.h> 36#include <net/if_media.h> 37 38#ifdef INET 39#include <netinet/in.h> 40#include <netinet/in_systm.h> 41#include <netinet/in_var.h> 42#include <netinet/ip.h> 43#include <netinet/if_ether.h> 44#endif 45 46#include <dev/mii/mii.h> 47#include <dev/mii/miivar.h> 48 49#include <dev/pci/pcireg.h> 50#include <dev/pci/pcivar.h> 51#include <dev/pci/pcidevs.h> 52 53#include <dev/ic/rtl81x9reg.h> 54#include <dev/ic/revar.h> 55 56struct re_pci_softc { 57 /* General */ 58 struct rl_softc sc_rl; 59 60 /* PCI-specific data */ 61 void *sc_ih; 62 pci_chipset_tag_t sc_pc; 63 pcitag_t sc_pcitag; 64 65 bus_size_t sc_iosize; 66}; 67 68const struct pci_matchid re_pci_devices[] = { 69 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CGLAPCIGT }, 70 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T }, 71 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE530T_C1 }, 72 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E }, 73 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168 }, 74 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169 }, 75 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC }, 76 { PCI_VENDOR_TTTECH, PCI_PRODUCT_TTTECH_MC322 }, 77 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902 } 78}; 79 80#define RE_LINKSYS_EG1032_SUBID 0x00241737 81 82int re_pci_probe(struct device *, void *, void *); 83void re_pci_attach(struct device *, struct device *, void *); 84int re_pci_detach(struct device *, int); 85int re_pci_activate(struct device *, int); 86 87/* 88 * PCI autoconfig definitions 89 */ 90struct cfattach re_pci_ca = { 91 sizeof(struct re_pci_softc), 92 re_pci_probe, 93 re_pci_attach, 94 re_pci_detach, 95 re_pci_activate 96}; 97 98/* 99 * Probe for a Realtek 8169/8110 chip. Check the PCI vendor and device 100 * IDs against our list and return a device name if we find a match. 101 */ 102int 103re_pci_probe(struct device *parent, void *match, void *aux) 104{ 105 struct pci_attach_args *pa = aux; 106 pci_chipset_tag_t pc = pa->pa_pc; 107 pcireg_t subid; 108 109 subid = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 110 111 /* C+ mode 8139's */ 112 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK && 113 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139 && 114 PCI_REVISION(pa->pa_class) == 0x20) 115 return (1); 116 117 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS && 118 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032 && 119 subid == RE_LINKSYS_EG1032_SUBID) 120 return (1); 121 122 return (pci_matchbyid((struct pci_attach_args *)aux, re_pci_devices, 123 nitems(re_pci_devices))); 124} 125 126/* 127 * PCI-specific attach routine 128 */ 129void 130re_pci_attach(struct device *parent, struct device *self, void *aux) 131{ 132 struct re_pci_softc *psc = (struct re_pci_softc *)self; 133 struct rl_softc *sc = &psc->sc_rl; 134 struct pci_attach_args *pa = aux; 135 pci_chipset_tag_t pc = pa->pa_pc; 136 pci_intr_handle_t ih; 137 const char *intrstr = NULL; 138 139 /* Only enable MSI on RT810xE for now. */ 140 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_REALTEK || 141 PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_REALTEK_RT8101E) 142 pa->pa_flags &= ~PCI_FLAGS_MSI_ENABLED; 143 144 pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0); 145 146#ifndef SMALL_KERNEL 147 /* Enable power management for wake on lan. */ 148 pci_conf_write(pc, pa->pa_tag, RL_PCI_PMCSR, RL_PME_EN); 149#endif 150 151 /* 152 * Map control/status registers. 153 */ 154 if (pci_mapreg_map(pa, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0, 155 &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->sc_iosize, 0)) { 156 if (pci_mapreg_map(pa, RL_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0, 157 &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->sc_iosize, 0)) { 158 printf(": can't map mem or i/o space\n"); 159 return; 160 } 161 } 162 163 /* Allocate interrupt */ 164 if (pci_intr_map_msi(pa, &ih) != 0 && pci_intr_map(pa, &ih) != 0) { 165 printf(": couldn't map interrupt\n"); 166 return; 167 } 168 intrstr = pci_intr_string(pc, ih); 169 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc, 170 sc->sc_dev.dv_xname); 171 if (psc->sc_ih == NULL) { 172 printf(": couldn't establish interrupt"); 173 if (intrstr != NULL) 174 printf(" at %s", intrstr); 175 return; 176 } 177 178 sc->sc_dmat = pa->pa_dmat; 179 psc->sc_pc = pc; 180 181 /* 182 * PCI Express check. 183 */ 184 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS, 185 NULL, NULL)) 186 sc->rl_flags |= RL_FLAG_PCIE; 187 188 /* Call bus-independent attach routine */ 189 if (re_attach(sc, intrstr)) { 190 pci_intr_disestablish(pc, psc->sc_ih); 191 bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->sc_iosize); 192 } 193} 194 195int 196re_pci_detach(struct device *self, int flags) 197{ 198 struct re_pci_softc *psc = (struct re_pci_softc *)self; 199 struct rl_softc *sc = &psc->sc_rl; 200 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 201 202 /* Remove timeout handler */ 203 timeout_del(&sc->timer_handle); 204 205 /* Detach PHY */ 206 if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) 207 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 208 209 /* Delete media stuff */ 210 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 211 ether_ifdetach(ifp); 212 if_detach(ifp); 213 214 /* Disable interrupts */ 215 if (psc->sc_ih != NULL) 216 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 217 218 /* Free pci resources */ 219 bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->sc_iosize); 220 221 return (0); 222} 223 224int 225re_pci_activate(struct device *self, int act) 226{ 227 struct re_pci_softc *psc = (struct re_pci_softc *)self; 228 struct rl_softc *sc = &psc->sc_rl; 229 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 230 231 switch (act) { 232 case DVACT_SUSPEND: 233 if (ifp->if_flags & IFF_RUNNING) 234 re_stop(ifp); 235 break; 236 case DVACT_RESUME: 237 re_reset(sc); 238 if (ifp->if_flags & IFF_UP) 239 re_init(ifp); 240 break; 241 } 242 243 return (0); 244} 245