1 /* $NetBSD: if_re_pci.c,v 1.55 2025/12/20 06:50:45 mlelstv Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998-2003 5 * Bill Paul <wpaul (at) windriver.com>. 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 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */ 36 37 /* 38 * RealTek 8139C+/8169/8169S/8110S PCI NIC driver 39 * 40 * Written by Bill Paul <wpaul (at) windriver.com> 41 * Senior Networking Software Engineer 42 * Wind River Systems 43 * 44 * NetBSD bus-specific frontends for written by 45 * Jonathan Stone <jonathan (at) netbsd.org> 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: if_re_pci.c,v 1.55 2025/12/20 06:50:45 mlelstv Exp $"); 50 51 #include <sys/types.h> 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/device.h> 56 57 #include <net/if.h> 58 #include <net/if_ether.h> 59 #include <net/if_media.h> 60 61 #include <sys/bus.h> 62 63 #include <dev/mii/mii.h> 64 #include <dev/mii/miivar.h> 65 66 #include <dev/pci/pcireg.h> 67 #include <dev/pci/pcivar.h> 68 #include <dev/pci/pcidevs.h> 69 70 #include <dev/ic/rtl81x9reg.h> 71 #include <dev/ic/rtl81x9var.h> 72 #include <dev/ic/rtl8169var.h> 73 74 struct re_pci_softc { 75 struct rtk_softc sc_rtk; 76 77 void *sc_ih; 78 pci_intr_handle_t *sc_pihp; 79 pci_chipset_tag_t sc_pc; 80 }; 81 82 static int re_pci_match(device_t, cfdata_t, void *); 83 static void re_pci_attach(device_t, device_t, void *); 84 static int re_pci_detach(device_t, int); 85 86 CFATTACH_DECL_NEW(re_pci, sizeof(struct re_pci_softc), 87 re_pci_match, re_pci_attach, re_pci_detach, NULL); 88 89 /* 90 * Various supported device vendors/types and their names. 91 */ 92 static const struct rtk_type re_devs[] = { 93 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139, 94 RTK_8139CPLUS, 95 "RealTek 8139C+ 10/100BaseTX" }, 96 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E, 97 RTK_8101E, 98 "RealTek 8100E/8101E/8102E/8102EL PCIe 10/100BaseTX" }, 99 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168, 100 RTK_8168, 101 "RealTek 8168/8111 PCIe Gigabit Ethernet" }, 102 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168_2, 103 RTK_8168, 104 "RealTek 8161 PCIe Gigabit Ethernet" }, 105 { PCI_VENDOR_NCUBE, PCI_PRODUCT_NCUBE_TG3648, 106 RTK_8168, 107 "TP-Link TG-3468 v2 (RTL8168) Gigabit Ethernet" }, 108 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, 109 RTK_8169, 110 "RealTek 8169/8110 Gigabit Ethernet" }, 111 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC, 112 RTK_8169, 113 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 114 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_E2600, 115 RTK_8169, 116 "Killer E2600 Gigabit Ethernet" }, 117 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT, 118 RTK_8169, 119 "Corega CG-LAPCIGT Gigabit Ethernet" }, 120 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T, 121 RTK_8169, 122 "D-Link DGE-528T Gigabit Ethernet" }, 123 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE530T_C1, 124 RTK_8169, 125 "D-Link DGE-530T C1 Gigabit Ethernet" }, 126 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902, 127 RTK_8169, 128 "US Robotics (3Com) USR997902 Gigabit Ethernet" }, 129 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032, 130 RTK_8169, 131 "Linksys EG1032 rev. 3 Gigabit Ethernet" }, 132 }; 133 134 static const struct rtk_type * 135 re_pci_lookup(const struct pci_attach_args * pa) 136 { 137 int i; 138 139 for (i = 0; i < __arraycount(re_devs); i++) { 140 if (PCI_VENDOR(pa->pa_id) != re_devs[i].rtk_vid) 141 continue; 142 if (PCI_PRODUCT(pa->pa_id) == re_devs[i].rtk_did) 143 return &re_devs[i]; 144 } 145 146 return NULL; 147 } 148 149 #define RE_LINKSYS_EG1032_SUBID 0x00241737 150 151 /* 152 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 153 * IDs against our list and return a device name if we find a match. 154 */ 155 static int 156 re_pci_match(device_t parent, cfdata_t cf, void *aux) 157 { 158 struct pci_attach_args *pa = aux; 159 pcireg_t subid; 160 161 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 162 163 /* special-case Linksys EG1032, since rev 2 uses sk(4) */ 164 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS && 165 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032) { 166 if (subid != RE_LINKSYS_EG1032_SUBID) 167 return 0; 168 } 169 170 /* Don't match 8139 other than C-PLUS */ 171 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK && 172 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139) { 173 if (PCI_REVISION(pa->pa_class) != 0x20) 174 return 0; 175 } 176 177 if (re_pci_lookup(pa) != NULL) 178 return 2; /* defeat rtk(4) */ 179 180 return 0; 181 } 182 183 static void 184 re_pci_attach(device_t parent, device_t self, void *aux) 185 { 186 struct re_pci_softc *psc = device_private(self); 187 struct rtk_softc *sc = &psc->sc_rtk; 188 struct pci_attach_args *pa = aux; 189 pci_chipset_tag_t pc = pa->pa_pc; 190 const char *intrstr = NULL; 191 const struct rtk_type *t; 192 uint32_t hwrev; 193 pcireg_t command, memtype; 194 bool ioh_valid, memh_valid; 195 bus_space_tag_t iot, memt; 196 bus_space_handle_t ioh, memh; 197 bus_size_t iosize, memsize; 198 char intrbuf[PCI_INTRSTR_LEN]; 199 bool prefer_io = false; 200 201 sc->sc_dev = self; 202 psc->sc_pc = pa->pa_pc; 203 204 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 205 command |= PCI_COMMAND_MASTER_ENABLE; 206 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command); 207 208 /* 209 * Map control/status registers. 210 */ 211 ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0, 212 &iot, &ioh, NULL, &iosize) == 0); 213 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTK_PCI_LOMEM); 214 switch (memtype) { 215 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 216 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 217 memh_valid = 218 (pci_mapreg_map(pa, RTK_PCI_LOMEM, 219 memtype, 0, &memt, &memh, NULL, &memsize) == 0) || 220 (pci_mapreg_map(pa, RTK_PCI_LOMEM + 4, 221 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT, 222 0, &memt, &memh, NULL, &memsize) == 0); 223 break; 224 default: 225 memh_valid = 0; 226 break; 227 } 228 229 switch (PCI_VENDOR(pa->pa_id)) { 230 case PCI_VENDOR_REALTEK: 231 switch (PCI_PRODUCT(pa->pa_id)) { 232 case PCI_PRODUCT_REALTEK_RT8169SC: 233 prefer_io = true; 234 break; 235 } 236 break; 237 } 238 if (!memh_valid) 239 prefer_io = true; 240 241 if (ioh_valid && prefer_io) { 242 sc->rtk_btag = iot; 243 sc->rtk_bhandle = ioh; 244 sc->rtk_bsize = iosize; 245 if (memh_valid) 246 bus_space_unmap(memt, memh, memsize); 247 248 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 249 command |= PCI_COMMAND_IO_ENABLE; 250 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command); 251 } else if (memh_valid) { 252 sc->rtk_btag = memt; 253 sc->rtk_bhandle = memh; 254 sc->rtk_bsize = memsize; 255 if (ioh_valid) 256 bus_space_unmap(iot, ioh, iosize); 257 258 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 259 command |= PCI_COMMAND_MEM_ENABLE; 260 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command); 261 } else { 262 aprint_error(": can't map registers\n"); 263 return; 264 } 265 266 t = re_pci_lookup(pa); 267 KASSERT(t != NULL); 268 269 pci_aprint_devinfo_fancy(pa, NULL, t->rtk_name, 1); 270 271 if (t->rtk_basetype == RTK_8139CPLUS) 272 sc->sc_quirk |= RTKQ_8139CPLUS; 273 274 if (t->rtk_basetype == RTK_8168 || 275 t->rtk_basetype == RTK_8101E) 276 sc->sc_quirk |= RTKQ_PCIE; 277 278 if (t->rtk_basetype == RTK_8168) 279 sc->sc_quirk |= RTKQ_IM_HW; 280 281 if (pci_dma64_available(pa) && (sc->sc_quirk & RTKQ_PCIE)) 282 sc->sc_dmat = pa->pa_dmat64; 283 else 284 sc->sc_dmat = pa->pa_dmat; 285 286 /* 287 * No power/enable/disable machinery for PCI attach; 288 * mark the card enabled now. 289 */ 290 sc->sc_flags |= RTK_ENABLED; 291 292 /* Hook interrupt last to avoid having to lock softc */ 293 /* Allocate interrupt */ 294 if (pci_intr_alloc(pa, &psc->sc_pihp, NULL, 0)) { 295 aprint_error_dev(self, "couldn't map interrupt\n"); 296 return; 297 } 298 intrstr = pci_intr_string(pc, psc->sc_pihp[0], intrbuf, 299 sizeof(intrbuf)); 300 psc->sc_ih = pci_intr_establish_xname(pc, psc->sc_pihp[0], IPL_NET, 301 re_intr, sc, device_xname(sc->sc_dev)); 302 if (psc->sc_ih == NULL) { 303 aprint_error_dev(self, "couldn't establish interrupt"); 304 if (intrstr != NULL) 305 aprint_error(" at %s", intrstr); 306 aprint_error("\n"); 307 return; 308 } 309 pci_intr_setattr(pc, psc->sc_ih, PCI_INTR_MPSAFE, true); 310 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 311 312 re_attach(sc); 313 314 hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV; 315 316 /* 317 * Perform hardware diagnostic on the original RTL8169. 318 * Some 32-bit cards were incorrectly wired and would 319 * malfunction if plugged into a 64-bit slot. 320 */ 321 if (hwrev == RTK_HWREV_8169) { 322 if (re_diag(sc)) { 323 re_pci_detach(self, 0); 324 aprint_error_dev(self, "disabled\n"); 325 } 326 } 327 } 328 329 static int 330 re_pci_detach(device_t self, int flags) 331 { 332 struct re_pci_softc *psc = device_private(self); 333 struct rtk_softc *sc = &psc->sc_rtk; 334 int rv; 335 336 if ((sc->sc_flags & RTK_ATTACHED) != 0) 337 /* re_stop() */ 338 sc->ethercom.ec_if.if_stop(&sc->ethercom.ec_if, 0); 339 340 rv = re_detach(sc); 341 if (rv) 342 return rv; 343 344 if (psc->sc_ih != NULL) { 345 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 346 psc->sc_ih = NULL; 347 } 348 349 if (psc->sc_pihp != NULL) { 350 pci_intr_release(psc->sc_pc, psc->sc_pihp, 1); 351 psc->sc_pihp = NULL; 352 } 353 354 if (sc->rtk_bsize != 0) { 355 bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize); 356 sc->rtk_bsize = 0; 357 } 358 359 return 0; 360 } 361