1 /* $NetBSD: if_nfe.c,v 1.65 2018/06/26 06:48:01 msaitoh Exp $ */ 2 /* $OpenBSD: if_nfe.c,v 1.77 2008/02/05 16:52:50 brad Exp $ */ 3 4 /*- 5 * Copyright (c) 2006, 2007 Damien Bergamini <damien.bergamini@free.fr> 6 * Copyright (c) 2005, 2006 Jonathan Gray <jsg@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /* Driver for NVIDIA nForce MCP Fast Ethernet and Gigabit Ethernet */ 22 23 #include <sys/cdefs.h> 24 __KERNEL_RCSID(0, "$NetBSD: if_nfe.c,v 1.65 2018/06/26 06:48:01 msaitoh Exp $"); 25 26 #include "opt_inet.h" 27 #include "vlan.h" 28 29 #include <sys/param.h> 30 #include <sys/endian.h> 31 #include <sys/systm.h> 32 #include <sys/types.h> 33 #include <sys/sockio.h> 34 #include <sys/mbuf.h> 35 #include <sys/mutex.h> 36 #include <sys/queue.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 #include <sys/callout.h> 40 #include <sys/socket.h> 41 42 #include <sys/bus.h> 43 44 #include <net/if.h> 45 #include <net/if_dl.h> 46 #include <net/if_media.h> 47 #include <net/if_ether.h> 48 #include <net/if_arp.h> 49 50 #ifdef INET 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/in_var.h> 54 #include <netinet/ip.h> 55 #include <netinet/if_inarp.h> 56 #endif 57 58 #if NVLAN > 0 59 #include <net/if_types.h> 60 #endif 61 62 #include <net/bpf.h> 63 64 #include <dev/mii/mii.h> 65 #include <dev/mii/miivar.h> 66 67 #include <dev/pci/pcireg.h> 68 #include <dev/pci/pcivar.h> 69 #include <dev/pci/pcidevs.h> 70 71 #include <dev/pci/if_nfereg.h> 72 #include <dev/pci/if_nfevar.h> 73 74 static int nfe_ifflags_cb(struct ethercom *); 75 76 int nfe_match(device_t, cfdata_t, void *); 77 void nfe_attach(device_t, device_t, void *); 78 int nfe_detach(device_t, int); 79 void nfe_power(int, void *); 80 void nfe_miibus_statchg(struct ifnet *); 81 int nfe_miibus_readreg(device_t, int, int); 82 void nfe_miibus_writereg(device_t, int, int, int); 83 int nfe_intr(void *); 84 int nfe_ioctl(struct ifnet *, u_long, void *); 85 void nfe_txdesc32_sync(struct nfe_softc *, struct nfe_desc32 *, int); 86 void nfe_txdesc64_sync(struct nfe_softc *, struct nfe_desc64 *, int); 87 void nfe_txdesc32_rsync(struct nfe_softc *, int, int, int); 88 void nfe_txdesc64_rsync(struct nfe_softc *, int, int, int); 89 void nfe_rxdesc32_sync(struct nfe_softc *, struct nfe_desc32 *, int); 90 void nfe_rxdesc64_sync(struct nfe_softc *, struct nfe_desc64 *, int); 91 void nfe_rxeof(struct nfe_softc *); 92 void nfe_txeof(struct nfe_softc *); 93 int nfe_encap(struct nfe_softc *, struct mbuf *); 94 void nfe_start(struct ifnet *); 95 void nfe_watchdog(struct ifnet *); 96 int nfe_init(struct ifnet *); 97 void nfe_stop(struct ifnet *, int); 98 struct nfe_jbuf *nfe_jalloc(struct nfe_softc *, int); 99 void nfe_jfree(struct mbuf *, void *, size_t, void *); 100 int nfe_jpool_alloc(struct nfe_softc *); 101 void nfe_jpool_free(struct nfe_softc *); 102 int nfe_alloc_rx_ring(struct nfe_softc *, struct nfe_rx_ring *); 103 void nfe_reset_rx_ring(struct nfe_softc *, struct nfe_rx_ring *); 104 void nfe_free_rx_ring(struct nfe_softc *, struct nfe_rx_ring *); 105 int nfe_alloc_tx_ring(struct nfe_softc *, struct nfe_tx_ring *); 106 void nfe_reset_tx_ring(struct nfe_softc *, struct nfe_tx_ring *); 107 void nfe_free_tx_ring(struct nfe_softc *, struct nfe_tx_ring *); 108 void nfe_setmulti(struct nfe_softc *); 109 void nfe_get_macaddr(struct nfe_softc *, uint8_t *); 110 void nfe_set_macaddr(struct nfe_softc *, const uint8_t *); 111 void nfe_tick(void *); 112 void nfe_poweron(device_t); 113 bool nfe_resume(device_t, const pmf_qual_t *); 114 115 CFATTACH_DECL_NEW(nfe, sizeof(struct nfe_softc), 116 nfe_match, nfe_attach, nfe_detach, NULL); 117 118 /* #define NFE_NO_JUMBO */ 119 120 #ifdef NFE_DEBUG 121 int nfedebug = 0; 122 #define DPRINTF(x) do { if (nfedebug) printf x; } while (0) 123 #define DPRINTFN(n,x) do { if (nfedebug >= (n)) printf x; } while (0) 124 #else 125 #define DPRINTF(x) 126 #define DPRINTFN(n,x) 127 #endif 128 129 /* deal with naming differences */ 130 131 #define PCI_PRODUCT_NVIDIA_NFORCE3_LAN2 \ 132 PCI_PRODUCT_NVIDIA_NFORCE2_400_LAN1 133 #define PCI_PRODUCT_NVIDIA_NFORCE3_LAN3 \ 134 PCI_PRODUCT_NVIDIA_NFORCE2_400_LAN2 135 #define PCI_PRODUCT_NVIDIA_NFORCE3_LAN5 \ 136 PCI_PRODUCT_NVIDIA_NFORCE3_250_LAN 137 138 #define PCI_PRODUCT_NVIDIA_CK804_LAN1 \ 139 PCI_PRODUCT_NVIDIA_NFORCE4_LAN1 140 #define PCI_PRODUCT_NVIDIA_CK804_LAN2 \ 141 PCI_PRODUCT_NVIDIA_NFORCE4_LAN2 142 143 #define PCI_PRODUCT_NVIDIA_MCP51_LAN1 \ 144 PCI_PRODUCT_NVIDIA_NFORCE430_LAN1 145 #define PCI_PRODUCT_NVIDIA_MCP51_LAN2 \ 146 PCI_PRODUCT_NVIDIA_NFORCE430_LAN2 147 148 #ifdef _LP64 149 #define __LP64__ 1 150 #endif 151 152 const struct nfe_product { 153 pci_vendor_id_t vendor; 154 pci_product_id_t product; 155 } nfe_devices[] = { 156 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_LAN }, 157 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_LAN }, 158 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN1 }, 159 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN2 }, 160 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN3 }, 161 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN4 }, 162 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN5 }, 163 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_CK804_LAN1 }, 164 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_CK804_LAN2 }, 165 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN1 }, 166 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN2 }, 167 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP51_LAN1 }, 168 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP51_LAN2 }, 169 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN1 }, 170 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN2 }, 171 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN1 }, 172 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN2 }, 173 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN3 }, 174 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN4 }, 175 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN1 }, 176 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN2 }, 177 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN3 }, 178 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN4 }, 179 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN1 }, 180 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN2 }, 181 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN3 }, 182 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN4 }, 183 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN1 }, 184 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN2 }, 185 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN3 }, 186 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN4 }, 187 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN1 }, 188 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN2 }, 189 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN3 }, 190 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN4 }, 191 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN1 }, 192 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN2 }, 193 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN3 }, 194 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN4 } 195 }; 196 197 int 198 nfe_match(device_t dev, cfdata_t match, void *aux) 199 { 200 struct pci_attach_args *pa = aux; 201 const struct nfe_product *np; 202 int i; 203 204 for (i = 0; i < __arraycount(nfe_devices); i++) { 205 np = &nfe_devices[i]; 206 if (PCI_VENDOR(pa->pa_id) == np->vendor && 207 PCI_PRODUCT(pa->pa_id) == np->product) 208 return 1; 209 } 210 return 0; 211 } 212 213 void 214 nfe_attach(device_t parent, device_t self, void *aux) 215 { 216 struct nfe_softc *sc = device_private(self); 217 struct pci_attach_args *pa = aux; 218 pci_chipset_tag_t pc = pa->pa_pc; 219 pci_intr_handle_t ih; 220 const char *intrstr; 221 struct ifnet *ifp; 222 pcireg_t memtype, csr; 223 int mii_flags = 0; 224 char intrbuf[PCI_INTRSTR_LEN]; 225 226 sc->sc_dev = self; 227 sc->sc_pc = pa->pa_pc; 228 pci_aprint_devinfo(pa, NULL); 229 230 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, NFE_PCI_BA); 231 switch (memtype) { 232 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 233 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 234 if (pci_mapreg_map(pa, NFE_PCI_BA, memtype, 0, &sc->sc_memt, 235 &sc->sc_memh, NULL, &sc->sc_mems) == 0) 236 break; 237 /* FALLTHROUGH */ 238 default: 239 aprint_error_dev(self, "could not map mem space\n"); 240 return; 241 } 242 243 if (pci_intr_map(pa, &ih) != 0) { 244 aprint_error_dev(self, "could not map interrupt\n"); 245 goto fail; 246 } 247 248 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); 249 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, nfe_intr, sc); 250 if (sc->sc_ih == NULL) { 251 aprint_error_dev(self, "could not establish interrupt"); 252 if (intrstr != NULL) 253 aprint_error(" at %s", intrstr); 254 aprint_error("\n"); 255 goto fail; 256 } 257 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 258 259 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 260 csr |= PCI_COMMAND_MASTER_ENABLE; 261 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr); 262 263 sc->sc_flags = 0; 264 265 switch (PCI_PRODUCT(pa->pa_id)) { 266 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN2: 267 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN3: 268 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN4: 269 case PCI_PRODUCT_NVIDIA_NFORCE3_LAN5: 270 sc->sc_flags |= NFE_JUMBO_SUP | NFE_HW_CSUM; 271 break; 272 case PCI_PRODUCT_NVIDIA_MCP51_LAN1: 273 case PCI_PRODUCT_NVIDIA_MCP51_LAN2: 274 sc->sc_flags |= NFE_40BIT_ADDR | NFE_PWR_MGMT; 275 break; 276 case PCI_PRODUCT_NVIDIA_MCP61_LAN1: 277 case PCI_PRODUCT_NVIDIA_MCP61_LAN2: 278 case PCI_PRODUCT_NVIDIA_MCP61_LAN3: 279 case PCI_PRODUCT_NVIDIA_MCP61_LAN4: 280 case PCI_PRODUCT_NVIDIA_MCP67_LAN1: 281 case PCI_PRODUCT_NVIDIA_MCP67_LAN2: 282 case PCI_PRODUCT_NVIDIA_MCP67_LAN3: 283 case PCI_PRODUCT_NVIDIA_MCP67_LAN4: 284 case PCI_PRODUCT_NVIDIA_MCP73_LAN1: 285 case PCI_PRODUCT_NVIDIA_MCP73_LAN2: 286 case PCI_PRODUCT_NVIDIA_MCP73_LAN3: 287 case PCI_PRODUCT_NVIDIA_MCP73_LAN4: 288 sc->sc_flags |= NFE_40BIT_ADDR | NFE_CORRECT_MACADDR | 289 NFE_PWR_MGMT; 290 break; 291 case PCI_PRODUCT_NVIDIA_MCP77_LAN1: 292 case PCI_PRODUCT_NVIDIA_MCP77_LAN2: 293 case PCI_PRODUCT_NVIDIA_MCP77_LAN3: 294 case PCI_PRODUCT_NVIDIA_MCP77_LAN4: 295 sc->sc_flags |= NFE_40BIT_ADDR | NFE_HW_CSUM | 296 NFE_CORRECT_MACADDR | NFE_PWR_MGMT; 297 break; 298 case PCI_PRODUCT_NVIDIA_MCP79_LAN1: 299 case PCI_PRODUCT_NVIDIA_MCP79_LAN2: 300 case PCI_PRODUCT_NVIDIA_MCP79_LAN3: 301 case PCI_PRODUCT_NVIDIA_MCP79_LAN4: 302 sc->sc_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM | 303 NFE_CORRECT_MACADDR | NFE_PWR_MGMT; 304 break; 305 case PCI_PRODUCT_NVIDIA_CK804_LAN1: 306 case PCI_PRODUCT_NVIDIA_CK804_LAN2: 307 case PCI_PRODUCT_NVIDIA_MCP04_LAN1: 308 case PCI_PRODUCT_NVIDIA_MCP04_LAN2: 309 sc->sc_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM; 310 break; 311 case PCI_PRODUCT_NVIDIA_MCP65_LAN1: 312 case PCI_PRODUCT_NVIDIA_MCP65_LAN2: 313 case PCI_PRODUCT_NVIDIA_MCP65_LAN3: 314 case PCI_PRODUCT_NVIDIA_MCP65_LAN4: 315 sc->sc_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | 316 NFE_CORRECT_MACADDR | NFE_PWR_MGMT; 317 mii_flags = MIIF_DOPAUSE; 318 break; 319 case PCI_PRODUCT_NVIDIA_MCP55_LAN1: 320 case PCI_PRODUCT_NVIDIA_MCP55_LAN2: 321 sc->sc_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM | 322 NFE_HW_VLAN | NFE_PWR_MGMT; 323 break; 324 } 325 326 if (pci_dma64_available(pa) && (sc->sc_flags & NFE_40BIT_ADDR) != 0) 327 sc->sc_dmat = pa->pa_dmat64; 328 else 329 sc->sc_dmat = pa->pa_dmat; 330 331 nfe_poweron(self); 332 333 #ifndef NFE_NO_JUMBO 334 /* enable jumbo frames for adapters that support it */ 335 if (sc->sc_flags & NFE_JUMBO_SUP) 336 sc->sc_flags |= NFE_USE_JUMBO; 337 #endif 338 339 /* Check for reversed ethernet address */ 340 if ((NFE_READ(sc, NFE_TX_UNK) & NFE_MAC_ADDR_INORDER) != 0) 341 sc->sc_flags |= NFE_CORRECT_MACADDR; 342 343 nfe_get_macaddr(sc, sc->sc_enaddr); 344 aprint_normal_dev(self, "Ethernet address %s\n", 345 ether_sprintf(sc->sc_enaddr)); 346 347 /* 348 * Allocate Tx and Rx rings. 349 */ 350 if (nfe_alloc_tx_ring(sc, &sc->txq) != 0) { 351 aprint_error_dev(self, "could not allocate Tx ring\n"); 352 goto fail; 353 } 354 355 mutex_init(&sc->rxq.mtx, MUTEX_DEFAULT, IPL_NET); 356 357 if (nfe_alloc_rx_ring(sc, &sc->rxq) != 0) { 358 aprint_error_dev(self, "could not allocate Rx ring\n"); 359 nfe_free_tx_ring(sc, &sc->txq); 360 goto fail; 361 } 362 363 ifp = &sc->sc_ethercom.ec_if; 364 ifp->if_softc = sc; 365 ifp->if_mtu = ETHERMTU; 366 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 367 ifp->if_ioctl = nfe_ioctl; 368 ifp->if_start = nfe_start; 369 ifp->if_stop = nfe_stop; 370 ifp->if_watchdog = nfe_watchdog; 371 ifp->if_init = nfe_init; 372 ifp->if_baudrate = IF_Gbps(1); 373 IFQ_SET_MAXLEN(&ifp->if_snd, NFE_IFQ_MAXLEN); 374 IFQ_SET_READY(&ifp->if_snd); 375 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 376 377 if (sc->sc_flags & NFE_USE_JUMBO) 378 sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU; 379 380 #if NVLAN > 0 381 if (sc->sc_flags & NFE_HW_VLAN) 382 sc->sc_ethercom.ec_capabilities |= 383 ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_MTU; 384 #endif 385 if (sc->sc_flags & NFE_HW_CSUM) { 386 ifp->if_capabilities |= 387 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | 388 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | 389 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx; 390 } 391 392 sc->sc_mii.mii_ifp = ifp; 393 sc->sc_mii.mii_readreg = nfe_miibus_readreg; 394 sc->sc_mii.mii_writereg = nfe_miibus_writereg; 395 sc->sc_mii.mii_statchg = nfe_miibus_statchg; 396 397 sc->sc_ethercom.ec_mii = &sc->sc_mii; 398 ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange, 399 ether_mediastatus); 400 401 mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 0, mii_flags); 402 403 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 404 aprint_error_dev(self, "no PHY found!\n"); 405 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL, 406 0, NULL); 407 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL); 408 } else 409 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO); 410 411 if_attach(ifp); 412 if_deferred_start_init(ifp, NULL); 413 ether_ifattach(ifp, sc->sc_enaddr); 414 ether_set_ifflags_cb(&sc->sc_ethercom, nfe_ifflags_cb); 415 416 callout_init(&sc->sc_tick_ch, 0); 417 callout_setfunc(&sc->sc_tick_ch, nfe_tick, sc); 418 419 if (pmf_device_register(self, NULL, nfe_resume)) 420 pmf_class_network_register(self, ifp); 421 else 422 aprint_error_dev(self, "couldn't establish power handler\n"); 423 424 return; 425 426 fail: 427 if (sc->sc_ih != NULL) { 428 pci_intr_disestablish(pc, sc->sc_ih); 429 sc->sc_ih = NULL; 430 } 431 if (sc->sc_mems != 0) { 432 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems); 433 sc->sc_mems = 0; 434 } 435 } 436 437 int 438 nfe_detach(device_t self, int flags) 439 { 440 struct nfe_softc *sc = device_private(self); 441 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 442 int s; 443 444 s = splnet(); 445 446 nfe_stop(ifp, 1); 447 448 pmf_device_deregister(self); 449 callout_destroy(&sc->sc_tick_ch); 450 ether_ifdetach(ifp); 451 if_detach(ifp); 452 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 453 454 nfe_free_rx_ring(sc, &sc->rxq); 455 mutex_destroy(&sc->rxq.mtx); 456 nfe_free_tx_ring(sc, &sc->txq); 457 458 if (sc->sc_ih != NULL) { 459 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 460 sc->sc_ih = NULL; 461 } 462 463 if ((sc->sc_flags & NFE_CORRECT_MACADDR) != 0) { 464 nfe_set_macaddr(sc, sc->sc_enaddr); 465 } else { 466 NFE_WRITE(sc, NFE_MACADDR_LO, 467 sc->sc_enaddr[0] << 8 | sc->sc_enaddr[1]); 468 NFE_WRITE(sc, NFE_MACADDR_HI, 469 sc->sc_enaddr[2] << 24 | sc->sc_enaddr[3] << 16 | 470 sc->sc_enaddr[4] << 8 | sc->sc_enaddr[5]); 471 } 472 473 if (sc->sc_mems != 0) { 474 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems); 475 sc->sc_mems = 0; 476 } 477 478 splx(s); 479 480 return 0; 481 } 482 483 void 484 nfe_miibus_statchg(struct ifnet *ifp) 485 { 486 struct nfe_softc *sc = ifp->if_softc; 487 struct mii_data *mii = &sc->sc_mii; 488 uint32_t phy, seed, misc = NFE_MISC1_MAGIC, link = NFE_MEDIA_SET; 489 490 phy = NFE_READ(sc, NFE_PHY_IFACE); 491 phy &= ~(NFE_PHY_HDX | NFE_PHY_100TX | NFE_PHY_1000T); 492 493 seed = NFE_READ(sc, NFE_RNDSEED); 494 seed &= ~NFE_SEED_MASK; 495 496 if ((mii->mii_media_active & IFM_GMASK) == IFM_HDX) { 497 phy |= NFE_PHY_HDX; /* half-duplex */ 498 misc |= NFE_MISC1_HDX; 499 } 500 501 switch (IFM_SUBTYPE(mii->mii_media_active)) { 502 case IFM_1000_T: /* full-duplex only */ 503 link |= NFE_MEDIA_1000T; 504 seed |= NFE_SEED_1000T; 505 phy |= NFE_PHY_1000T; 506 break; 507 case IFM_100_TX: 508 link |= NFE_MEDIA_100TX; 509 seed |= NFE_SEED_100TX; 510 phy |= NFE_PHY_100TX; 511 break; 512 case IFM_10_T: 513 link |= NFE_MEDIA_10T; 514 seed |= NFE_SEED_10T; 515 break; 516 } 517 518 NFE_WRITE(sc, NFE_RNDSEED, seed); /* XXX: gigabit NICs only? */ 519 520 NFE_WRITE(sc, NFE_PHY_IFACE, phy); 521 NFE_WRITE(sc, NFE_MISC1, misc); 522 NFE_WRITE(sc, NFE_LINKSPEED, link); 523 } 524 525 int 526 nfe_miibus_readreg(device_t dev, int phy, int reg) 527 { 528 struct nfe_softc *sc = device_private(dev); 529 uint32_t val; 530 int ntries; 531 532 NFE_WRITE(sc, NFE_PHY_STATUS, 0xf); 533 534 if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) { 535 NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY); 536 DELAY(100); 537 } 538 539 NFE_WRITE(sc, NFE_PHY_CTL, (phy << NFE_PHYADD_SHIFT) | reg); 540 541 for (ntries = 0; ntries < 1000; ntries++) { 542 DELAY(100); 543 if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY)) 544 break; 545 } 546 if (ntries == 1000) { 547 DPRINTFN(2, ("%s: timeout waiting for PHY\n", 548 device_xname(sc->sc_dev))); 549 return 0; 550 } 551 552 if (NFE_READ(sc, NFE_PHY_STATUS) & NFE_PHY_ERROR) { 553 DPRINTFN(2, ("%s: could not read PHY\n", 554 device_xname(sc->sc_dev))); 555 return 0; 556 } 557 558 val = NFE_READ(sc, NFE_PHY_DATA); 559 if (val != 0xffffffff && val != 0) 560 sc->mii_phyaddr = phy; 561 562 DPRINTFN(2, ("%s: mii read phy %d reg 0x%x ret 0x%x\n", 563 device_xname(sc->sc_dev), phy, reg, val)); 564 565 return val; 566 } 567 568 void 569 nfe_miibus_writereg(device_t dev, int phy, int reg, int val) 570 { 571 struct nfe_softc *sc = device_private(dev); 572 uint32_t ctl; 573 int ntries; 574 575 NFE_WRITE(sc, NFE_PHY_STATUS, 0xf); 576 577 if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) { 578 NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY); 579 DELAY(100); 580 } 581 582 NFE_WRITE(sc, NFE_PHY_DATA, val); 583 ctl = NFE_PHY_WRITE | (phy << NFE_PHYADD_SHIFT) | reg; 584 NFE_WRITE(sc, NFE_PHY_CTL, ctl); 585 586 for (ntries = 0; ntries < 1000; ntries++) { 587 DELAY(100); 588 if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY)) 589 break; 590 } 591 #ifdef NFE_DEBUG 592 if (nfedebug >= 2 && ntries == 1000) 593 printf("could not write to PHY\n"); 594 #endif 595 } 596 597 int 598 nfe_intr(void *arg) 599 { 600 struct nfe_softc *sc = arg; 601 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 602 uint32_t r; 603 int handled; 604 605 if ((ifp->if_flags & IFF_UP) == 0) 606 return 0; 607 608 handled = 0; 609 610 for (;;) { 611 r = NFE_READ(sc, NFE_IRQ_STATUS); 612 if ((r & NFE_IRQ_WANTED) == 0) 613 break; 614 615 NFE_WRITE(sc, NFE_IRQ_STATUS, r); 616 handled = 1; 617 DPRINTFN(5, ("nfe_intr: interrupt register %x\n", r)); 618 619 if ((r & (NFE_IRQ_RXERR|NFE_IRQ_RX_NOBUF|NFE_IRQ_RX)) != 0) { 620 /* check Rx ring */ 621 nfe_rxeof(sc); 622 } 623 if ((r & (NFE_IRQ_TXERR|NFE_IRQ_TXERR2|NFE_IRQ_TX_DONE)) != 0) { 624 /* check Tx ring */ 625 nfe_txeof(sc); 626 } 627 if ((r & NFE_IRQ_LINK) != 0) { 628 NFE_READ(sc, NFE_PHY_STATUS); 629 NFE_WRITE(sc, NFE_PHY_STATUS, 0xf); 630 DPRINTF(("%s: link state changed\n", 631 device_xname(sc->sc_dev))); 632 } 633 } 634 635 if (handled) 636 if_schedule_deferred_start(ifp); 637 638 return handled; 639 } 640 641 static int 642 nfe_ifflags_cb(struct ethercom *ec) 643 { 644 struct ifnet *ifp = &ec->ec_if; 645 struct nfe_softc *sc = ifp->if_softc; 646 int change = ifp->if_flags ^ sc->sc_if_flags; 647 648 /* 649 * If only the PROMISC flag changes, then 650 * don't do a full re-init of the chip, just update 651 * the Rx filter. 652 */ 653 if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0) 654 return ENETRESET; 655 else if ((change & IFF_PROMISC) != 0) 656 nfe_setmulti(sc); 657 658 return 0; 659 } 660 661 int 662 nfe_ioctl(struct ifnet *ifp, u_long cmd, void *data) 663 { 664 struct nfe_softc *sc = ifp->if_softc; 665 struct ifaddr *ifa = (struct ifaddr *)data; 666 int s, error = 0; 667 668 s = splnet(); 669 670 switch (cmd) { 671 case SIOCINITIFADDR: 672 ifp->if_flags |= IFF_UP; 673 nfe_init(ifp); 674 switch (ifa->ifa_addr->sa_family) { 675 #ifdef INET 676 case AF_INET: 677 arp_ifinit(ifp, ifa); 678 break; 679 #endif 680 default: 681 break; 682 } 683 break; 684 default: 685 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) 686 break; 687 688 error = 0; 689 690 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI) 691 ; 692 else if (ifp->if_flags & IFF_RUNNING) 693 nfe_setmulti(sc); 694 break; 695 } 696 sc->sc_if_flags = ifp->if_flags; 697 698 splx(s); 699 700 return error; 701 } 702 703 void 704 nfe_txdesc32_sync(struct nfe_softc *sc, struct nfe_desc32 *desc32, int ops) 705 { 706 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 707 (char *)desc32 - (char *)sc->txq.desc32, 708 sizeof (struct nfe_desc32), ops); 709 } 710 711 void 712 nfe_txdesc64_sync(struct nfe_softc *sc, struct nfe_desc64 *desc64, int ops) 713 { 714 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 715 (char *)desc64 - (char *)sc->txq.desc64, 716 sizeof (struct nfe_desc64), ops); 717 } 718 719 void 720 nfe_txdesc32_rsync(struct nfe_softc *sc, int start, int end, int ops) 721 { 722 if (end > start) { 723 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 724 (char *)&sc->txq.desc32[start] - (char *)sc->txq.desc32, 725 (char *)&sc->txq.desc32[end] - 726 (char *)&sc->txq.desc32[start], ops); 727 return; 728 } 729 /* sync from 'start' to end of ring */ 730 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 731 (char *)&sc->txq.desc32[start] - (char *)sc->txq.desc32, 732 (char *)&sc->txq.desc32[NFE_TX_RING_COUNT] - 733 (char *)&sc->txq.desc32[start], ops); 734 735 /* sync from start of ring to 'end' */ 736 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 0, 737 (char *)&sc->txq.desc32[end] - (char *)sc->txq.desc32, ops); 738 } 739 740 void 741 nfe_txdesc64_rsync(struct nfe_softc *sc, int start, int end, int ops) 742 { 743 if (end > start) { 744 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 745 (char *)&sc->txq.desc64[start] - (char *)sc->txq.desc64, 746 (char *)&sc->txq.desc64[end] - 747 (char *)&sc->txq.desc64[start], ops); 748 return; 749 } 750 /* sync from 'start' to end of ring */ 751 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 752 (char *)&sc->txq.desc64[start] - (char *)sc->txq.desc64, 753 (char *)&sc->txq.desc64[NFE_TX_RING_COUNT] - 754 (char *)&sc->txq.desc64[start], ops); 755 756 /* sync from start of ring to 'end' */ 757 bus_dmamap_sync(sc->sc_dmat, sc->txq.map, 0, 758 (char *)&sc->txq.desc64[end] - (char *)sc->txq.desc64, ops); 759 } 760 761 void 762 nfe_rxdesc32_sync(struct nfe_softc *sc, struct nfe_desc32 *desc32, int ops) 763 { 764 bus_dmamap_sync(sc->sc_dmat, sc->rxq.map, 765 (char *)desc32 - (char *)sc->rxq.desc32, 766 sizeof (struct nfe_desc32), ops); 767 } 768 769 void 770 nfe_rxdesc64_sync(struct nfe_softc *sc, struct nfe_desc64 *desc64, int ops) 771 { 772 bus_dmamap_sync(sc->sc_dmat, sc->rxq.map, 773 (char *)desc64 - (char *)sc->rxq.desc64, 774 sizeof (struct nfe_desc64), ops); 775 } 776 777 void 778 nfe_rxeof(struct nfe_softc *sc) 779 { 780 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 781 struct nfe_desc32 *desc32; 782 struct nfe_desc64 *desc64; 783 struct nfe_rx_data *data; 784 struct nfe_jbuf *jbuf; 785 struct mbuf *m, *mnew; 786 bus_addr_t physaddr; 787 uint16_t flags; 788 int error, len, i; 789 790 desc32 = NULL; 791 desc64 = NULL; 792 for (i = sc->rxq.cur;; i = NFE_RX_NEXTDESC(i)) { 793 data = &sc->rxq.data[i]; 794 795 if (sc->sc_flags & NFE_40BIT_ADDR) { 796 desc64 = &sc->rxq.desc64[i]; 797 nfe_rxdesc64_sync(sc, desc64, 798 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 799 800 flags = le16toh(desc64->flags); 801 len = le16toh(desc64->length) & 0x3fff; 802 } else { 803 desc32 = &sc->rxq.desc32[i]; 804 nfe_rxdesc32_sync(sc, desc32, 805 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 806 807 flags = le16toh(desc32->flags); 808 len = le16toh(desc32->length) & 0x3fff; 809 } 810 811 if ((flags & NFE_RX_READY) != 0) 812 break; 813 814 if ((sc->sc_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) { 815 if ((flags & NFE_RX_VALID_V1) == 0) 816 goto skip; 817 818 if ((flags & NFE_RX_FIXME_V1) == NFE_RX_FIXME_V1) { 819 flags &= ~NFE_RX_ERROR; 820 len--; /* fix buffer length */ 821 } 822 } else { 823 if ((flags & NFE_RX_VALID_V2) == 0) 824 goto skip; 825 826 if ((flags & NFE_RX_FIXME_V2) == NFE_RX_FIXME_V2) { 827 flags &= ~NFE_RX_ERROR; 828 len--; /* fix buffer length */ 829 } 830 } 831 832 if (flags & NFE_RX_ERROR) { 833 ifp->if_ierrors++; 834 goto skip; 835 } 836 837 /* 838 * Try to allocate a new mbuf for this ring element and load 839 * it before processing the current mbuf. If the ring element 840 * cannot be loaded, drop the received packet and reuse the 841 * old mbuf. In the unlikely case that the old mbuf can't be 842 * reloaded either, explicitly panic. 843 */ 844 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 845 if (mnew == NULL) { 846 ifp->if_ierrors++; 847 goto skip; 848 } 849 850 if (sc->sc_flags & NFE_USE_JUMBO) { 851 physaddr = 852 sc->rxq.jbuf[sc->rxq.jbufmap[i]].physaddr; 853 if ((jbuf = nfe_jalloc(sc, i)) == NULL) { 854 if (len > MCLBYTES) { 855 m_freem(mnew); 856 ifp->if_ierrors++; 857 goto skip1; 858 } 859 MCLGET(mnew, M_DONTWAIT); 860 if ((mnew->m_flags & M_EXT) == 0) { 861 m_freem(mnew); 862 ifp->if_ierrors++; 863 goto skip1; 864 } 865 866 (void)memcpy(mtod(mnew, void *), 867 mtod(data->m, const void *), len); 868 m = mnew; 869 goto mbufcopied; 870 } else { 871 MEXTADD(mnew, jbuf->buf, NFE_JBYTES, 0, nfe_jfree, sc); 872 bus_dmamap_sync(sc->sc_dmat, sc->rxq.jmap, 873 mtod(data->m, char *) - (char *)sc->rxq.jpool, 874 NFE_JBYTES, BUS_DMASYNC_POSTREAD); 875 876 physaddr = jbuf->physaddr; 877 } 878 } else { 879 MCLGET(mnew, M_DONTWAIT); 880 if ((mnew->m_flags & M_EXT) == 0) { 881 m_freem(mnew); 882 ifp->if_ierrors++; 883 goto skip; 884 } 885 886 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 887 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD); 888 bus_dmamap_unload(sc->sc_dmat, data->map); 889 890 error = bus_dmamap_load(sc->sc_dmat, data->map, 891 mtod(mnew, void *), MCLBYTES, NULL, 892 BUS_DMA_READ | BUS_DMA_NOWAIT); 893 if (error != 0) { 894 m_freem(mnew); 895 896 /* try to reload the old mbuf */ 897 error = bus_dmamap_load(sc->sc_dmat, data->map, 898 mtod(data->m, void *), MCLBYTES, NULL, 899 BUS_DMA_READ | BUS_DMA_NOWAIT); 900 if (error != 0) { 901 /* very unlikely that it will fail.. */ 902 panic("%s: could not load old rx mbuf", 903 device_xname(sc->sc_dev)); 904 } 905 ifp->if_ierrors++; 906 goto skip; 907 } 908 physaddr = data->map->dm_segs[0].ds_addr; 909 } 910 911 /* 912 * New mbuf successfully loaded, update Rx ring and continue 913 * processing. 914 */ 915 m = data->m; 916 data->m = mnew; 917 918 mbufcopied: 919 /* finalize mbuf */ 920 m->m_pkthdr.len = m->m_len = len; 921 m_set_rcvif(m, ifp); 922 923 if ((sc->sc_flags & NFE_HW_CSUM) != 0) { 924 /* 925 * XXX 926 * no way to check M_CSUM_IPv4_BAD or non-IPv4 packets? 927 */ 928 if (flags & NFE_RX_IP_CSUMOK) { 929 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 930 DPRINTFN(3, ("%s: ip4csum-rx ok\n", 931 device_xname(sc->sc_dev))); 932 } 933 /* 934 * XXX 935 * no way to check M_CSUM_TCP_UDP_BAD or 936 * other protocols? 937 */ 938 if (flags & NFE_RX_UDP_CSUMOK) { 939 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4; 940 DPRINTFN(3, ("%s: udp4csum-rx ok\n", 941 device_xname(sc->sc_dev))); 942 } else if (flags & NFE_RX_TCP_CSUMOK) { 943 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4; 944 DPRINTFN(3, ("%s: tcp4csum-rx ok\n", 945 device_xname(sc->sc_dev))); 946 } 947 } 948 if_percpuq_enqueue(ifp->if_percpuq, m); 949 950 skip1: 951 /* update mapping address in h/w descriptor */ 952 if (sc->sc_flags & NFE_40BIT_ADDR) { 953 #if defined(__LP64__) 954 desc64->physaddr[0] = htole32(physaddr >> 32); 955 #endif 956 desc64->physaddr[1] = htole32(physaddr & 0xffffffff); 957 } else { 958 desc32->physaddr = htole32(physaddr); 959 } 960 961 skip: 962 if (sc->sc_flags & NFE_40BIT_ADDR) { 963 desc64->length = htole16(sc->rxq.bufsz); 964 desc64->flags = htole16(NFE_RX_READY); 965 966 nfe_rxdesc64_sync(sc, desc64, 967 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 968 } else { 969 desc32->length = htole16(sc->rxq.bufsz); 970 desc32->flags = htole16(NFE_RX_READY); 971 972 nfe_rxdesc32_sync(sc, desc32, 973 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 974 } 975 } 976 /* update current RX pointer */ 977 sc->rxq.cur = i; 978 } 979 980 void 981 nfe_txeof(struct nfe_softc *sc) 982 { 983 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 984 struct nfe_desc32 *desc32; 985 struct nfe_desc64 *desc64; 986 struct nfe_tx_data *data = NULL; 987 int i; 988 uint16_t flags; 989 char buf[128]; 990 991 for (i = sc->txq.next; 992 sc->txq.queued > 0; 993 i = NFE_TX_NEXTDESC(i), sc->txq.queued--) { 994 if (sc->sc_flags & NFE_40BIT_ADDR) { 995 desc64 = &sc->txq.desc64[i]; 996 nfe_txdesc64_sync(sc, desc64, 997 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 998 999 flags = le16toh(desc64->flags); 1000 } else { 1001 desc32 = &sc->txq.desc32[i]; 1002 nfe_txdesc32_sync(sc, desc32, 1003 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1004 1005 flags = le16toh(desc32->flags); 1006 } 1007 1008 if ((flags & NFE_TX_VALID) != 0) 1009 break; 1010 1011 data = &sc->txq.data[i]; 1012 1013 if ((sc->sc_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) { 1014 if ((flags & NFE_TX_LASTFRAG_V1) == 0 && 1015 data->m == NULL) 1016 continue; 1017 1018 if ((flags & NFE_TX_ERROR_V1) != 0) { 1019 snprintb(buf, sizeof(buf), NFE_V1_TXERR, flags); 1020 aprint_error_dev(sc->sc_dev, "tx v1 error %s\n", 1021 buf); 1022 ifp->if_oerrors++; 1023 } else 1024 ifp->if_opackets++; 1025 } else { 1026 if ((flags & NFE_TX_LASTFRAG_V2) == 0 && 1027 data->m == NULL) 1028 continue; 1029 1030 if ((flags & NFE_TX_ERROR_V2) != 0) { 1031 snprintb(buf, sizeof(buf), NFE_V2_TXERR, flags); 1032 aprint_error_dev(sc->sc_dev, "tx v2 error %s\n", 1033 buf); 1034 ifp->if_oerrors++; 1035 } else 1036 ifp->if_opackets++; 1037 } 1038 1039 if (data->m == NULL) { /* should not get there */ 1040 aprint_error_dev(sc->sc_dev, 1041 "last fragment bit w/o associated mbuf!\n"); 1042 continue; 1043 } 1044 1045 /* last fragment of the mbuf chain transmitted */ 1046 bus_dmamap_sync(sc->sc_dmat, data->active, 0, 1047 data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1048 bus_dmamap_unload(sc->sc_dmat, data->active); 1049 m_freem(data->m); 1050 data->m = NULL; 1051 } 1052 1053 sc->txq.next = i; 1054 1055 if (sc->txq.queued < NFE_TX_RING_COUNT) { 1056 /* at least one slot freed */ 1057 ifp->if_flags &= ~IFF_OACTIVE; 1058 } 1059 1060 if (sc->txq.queued == 0) { 1061 /* all queued packets are sent */ 1062 ifp->if_timer = 0; 1063 } 1064 } 1065 1066 int 1067 nfe_encap(struct nfe_softc *sc, struct mbuf *m0) 1068 { 1069 struct nfe_desc32 *desc32; 1070 struct nfe_desc64 *desc64; 1071 struct nfe_tx_data *data; 1072 bus_dmamap_t map; 1073 uint16_t flags, csumflags; 1074 #if NVLAN > 0 1075 uint32_t vtag = 0; 1076 #endif 1077 int error, i, first; 1078 1079 desc32 = NULL; 1080 desc64 = NULL; 1081 data = NULL; 1082 1083 flags = 0; 1084 csumflags = 0; 1085 first = sc->txq.cur; 1086 1087 map = sc->txq.data[first].map; 1088 1089 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m0, BUS_DMA_NOWAIT); 1090 if (error != 0) { 1091 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", 1092 error); 1093 return error; 1094 } 1095 1096 if (sc->txq.queued + map->dm_nsegs >= NFE_TX_RING_COUNT - 1) { 1097 bus_dmamap_unload(sc->sc_dmat, map); 1098 return ENOBUFS; 1099 } 1100 1101 #if NVLAN > 0 1102 /* setup h/w VLAN tagging */ 1103 if (vlan_has_tag(m0)) 1104 vtag = NFE_TX_VTAG | vlan_get_tag(m0); 1105 #endif 1106 if ((sc->sc_flags & NFE_HW_CSUM) != 0) { 1107 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) 1108 csumflags |= NFE_TX_IP_CSUM; 1109 if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) 1110 csumflags |= NFE_TX_TCP_UDP_CSUM; 1111 } 1112 1113 for (i = 0; i < map->dm_nsegs; i++) { 1114 data = &sc->txq.data[sc->txq.cur]; 1115 1116 if (sc->sc_flags & NFE_40BIT_ADDR) { 1117 desc64 = &sc->txq.desc64[sc->txq.cur]; 1118 #if defined(__LP64__) 1119 desc64->physaddr[0] = 1120 htole32(map->dm_segs[i].ds_addr >> 32); 1121 #endif 1122 desc64->physaddr[1] = 1123 htole32(map->dm_segs[i].ds_addr & 0xffffffff); 1124 desc64->length = htole16(map->dm_segs[i].ds_len - 1); 1125 desc64->flags = htole16(flags); 1126 desc64->vtag = 0; 1127 } else { 1128 desc32 = &sc->txq.desc32[sc->txq.cur]; 1129 1130 desc32->physaddr = htole32(map->dm_segs[i].ds_addr); 1131 desc32->length = htole16(map->dm_segs[i].ds_len - 1); 1132 desc32->flags = htole16(flags); 1133 } 1134 1135 /* 1136 * Setting of the valid bit in the first descriptor is 1137 * deferred until the whole chain is fully setup. 1138 */ 1139 flags |= NFE_TX_VALID; 1140 1141 sc->txq.queued++; 1142 sc->txq.cur = NFE_TX_NEXTDESC(sc->txq.cur); 1143 } 1144 1145 /* the whole mbuf chain has been setup */ 1146 if (sc->sc_flags & NFE_40BIT_ADDR) { 1147 /* fix last descriptor */ 1148 flags |= NFE_TX_LASTFRAG_V2; 1149 desc64->flags = htole16(flags); 1150 1151 /* Checksum flags and vtag belong to the first fragment only. */ 1152 #if NVLAN > 0 1153 sc->txq.desc64[first].vtag = htole32(vtag); 1154 #endif 1155 sc->txq.desc64[first].flags |= htole16(csumflags); 1156 1157 /* finally, set the valid bit in the first descriptor */ 1158 sc->txq.desc64[first].flags |= htole16(NFE_TX_VALID); 1159 } else { 1160 /* fix last descriptor */ 1161 if (sc->sc_flags & NFE_JUMBO_SUP) 1162 flags |= NFE_TX_LASTFRAG_V2; 1163 else 1164 flags |= NFE_TX_LASTFRAG_V1; 1165 desc32->flags = htole16(flags); 1166 1167 /* Checksum flags belong to the first fragment only. */ 1168 sc->txq.desc32[first].flags |= htole16(csumflags); 1169 1170 /* finally, set the valid bit in the first descriptor */ 1171 sc->txq.desc32[first].flags |= htole16(NFE_TX_VALID); 1172 } 1173 1174 data->m = m0; 1175 data->active = map; 1176 1177 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize, 1178 BUS_DMASYNC_PREWRITE); 1179 1180 return 0; 1181 } 1182 1183 void 1184 nfe_start(struct ifnet *ifp) 1185 { 1186 struct nfe_softc *sc = ifp->if_softc; 1187 int old = sc->txq.queued; 1188 struct mbuf *m0; 1189 1190 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 1191 return; 1192 1193 for (;;) { 1194 IFQ_POLL(&ifp->if_snd, m0); 1195 if (m0 == NULL) 1196 break; 1197 1198 if (nfe_encap(sc, m0) != 0) { 1199 ifp->if_flags |= IFF_OACTIVE; 1200 break; 1201 } 1202 1203 /* packet put in h/w queue, remove from s/w queue */ 1204 IFQ_DEQUEUE(&ifp->if_snd, m0); 1205 1206 bpf_mtap(ifp, m0, BPF_D_OUT); 1207 } 1208 1209 if (sc->txq.queued != old) { 1210 /* packets are queued */ 1211 if (sc->sc_flags & NFE_40BIT_ADDR) 1212 nfe_txdesc64_rsync(sc, old, sc->txq.cur, 1213 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1214 else 1215 nfe_txdesc32_rsync(sc, old, sc->txq.cur, 1216 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1217 /* kick Tx */ 1218 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_KICKTX | sc->rxtxctl); 1219 1220 /* 1221 * Set a timeout in case the chip goes out to lunch. 1222 */ 1223 ifp->if_timer = 5; 1224 } 1225 } 1226 1227 void 1228 nfe_watchdog(struct ifnet *ifp) 1229 { 1230 struct nfe_softc *sc = ifp->if_softc; 1231 1232 aprint_error_dev(sc->sc_dev, "watchdog timeout\n"); 1233 1234 ifp->if_flags &= ~IFF_RUNNING; 1235 nfe_init(ifp); 1236 1237 ifp->if_oerrors++; 1238 } 1239 1240 int 1241 nfe_init(struct ifnet *ifp) 1242 { 1243 struct nfe_softc *sc = ifp->if_softc; 1244 uint32_t tmp; 1245 int rc = 0, s; 1246 1247 if (ifp->if_flags & IFF_RUNNING) 1248 return 0; 1249 1250 nfe_stop(ifp, 0); 1251 1252 NFE_WRITE(sc, NFE_TX_UNK, 0); 1253 NFE_WRITE(sc, NFE_STATUS, 0); 1254 1255 sc->rxtxctl = NFE_RXTX_BIT2; 1256 if (sc->sc_flags & NFE_40BIT_ADDR) 1257 sc->rxtxctl |= NFE_RXTX_V3MAGIC; 1258 else if (sc->sc_flags & NFE_JUMBO_SUP) 1259 sc->rxtxctl |= NFE_RXTX_V2MAGIC; 1260 if (sc->sc_flags & NFE_HW_CSUM) 1261 sc->rxtxctl |= NFE_RXTX_RXCSUM; 1262 #if NVLAN > 0 1263 /* 1264 * Although the adapter is capable of stripping VLAN tags from received 1265 * frames (NFE_RXTX_VTAG_STRIP), we do not enable this functionality on 1266 * purpose. This will be done in software by our network stack. 1267 */ 1268 if (sc->sc_flags & NFE_HW_VLAN) 1269 sc->rxtxctl |= NFE_RXTX_VTAG_INSERT; 1270 #endif 1271 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | sc->rxtxctl); 1272 DELAY(10); 1273 NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl); 1274 1275 #if NVLAN 1276 if (sc->sc_flags & NFE_HW_VLAN) 1277 NFE_WRITE(sc, NFE_VTAG_CTL, NFE_VTAG_ENABLE); 1278 #endif 1279 1280 NFE_WRITE(sc, NFE_SETUP_R6, 0); 1281 1282 /* set MAC address */ 1283 nfe_set_macaddr(sc, sc->sc_enaddr); 1284 1285 /* tell MAC where rings are in memory */ 1286 #ifdef __LP64__ 1287 NFE_WRITE(sc, NFE_RX_RING_ADDR_HI, sc->rxq.physaddr >> 32); 1288 #endif 1289 NFE_WRITE(sc, NFE_RX_RING_ADDR_LO, sc->rxq.physaddr & 0xffffffff); 1290 #ifdef __LP64__ 1291 NFE_WRITE(sc, NFE_TX_RING_ADDR_HI, sc->txq.physaddr >> 32); 1292 #endif 1293 NFE_WRITE(sc, NFE_TX_RING_ADDR_LO, sc->txq.physaddr & 0xffffffff); 1294 1295 NFE_WRITE(sc, NFE_RING_SIZE, 1296 (NFE_RX_RING_COUNT - 1) << 16 | 1297 (NFE_TX_RING_COUNT - 1)); 1298 1299 NFE_WRITE(sc, NFE_RXBUFSZ, sc->rxq.bufsz); 1300 1301 /* force MAC to wakeup */ 1302 tmp = NFE_READ(sc, NFE_PWR_STATE); 1303 NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_WAKEUP); 1304 DELAY(10); 1305 tmp = NFE_READ(sc, NFE_PWR_STATE); 1306 NFE_WRITE(sc, NFE_PWR_STATE, tmp | NFE_PWR_VALID); 1307 1308 s = splnet(); 1309 NFE_WRITE(sc, NFE_IRQ_MASK, 0); 1310 nfe_intr(sc); /* XXX clear IRQ status registers */ 1311 NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED); 1312 splx(s); 1313 1314 #if 1 1315 /* configure interrupts coalescing/mitigation */ 1316 NFE_WRITE(sc, NFE_IMTIMER, NFE_IM_DEFAULT); 1317 #else 1318 /* no interrupt mitigation: one interrupt per packet */ 1319 NFE_WRITE(sc, NFE_IMTIMER, 970); 1320 #endif 1321 1322 NFE_WRITE(sc, NFE_SETUP_R1, NFE_R1_MAGIC); 1323 NFE_WRITE(sc, NFE_SETUP_R2, NFE_R2_MAGIC); 1324 NFE_WRITE(sc, NFE_SETUP_R6, NFE_R6_MAGIC); 1325 1326 /* update MAC knowledge of PHY; generates a NFE_IRQ_LINK interrupt */ 1327 NFE_WRITE(sc, NFE_STATUS, sc->mii_phyaddr << 24 | NFE_STATUS_MAGIC); 1328 1329 NFE_WRITE(sc, NFE_SETUP_R4, NFE_R4_MAGIC); 1330 NFE_WRITE(sc, NFE_WOL_CTL, NFE_WOL_ENABLE); 1331 1332 sc->rxtxctl &= ~NFE_RXTX_BIT2; 1333 NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl); 1334 DELAY(10); 1335 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT1 | sc->rxtxctl); 1336 1337 /* set Rx filter */ 1338 nfe_setmulti(sc); 1339 1340 if ((rc = ether_mediachange(ifp)) != 0) 1341 goto out; 1342 1343 nfe_tick(sc); 1344 1345 /* enable Rx */ 1346 NFE_WRITE(sc, NFE_RX_CTL, NFE_RX_START); 1347 1348 /* enable Tx */ 1349 NFE_WRITE(sc, NFE_TX_CTL, NFE_TX_START); 1350 1351 NFE_WRITE(sc, NFE_PHY_STATUS, 0xf); 1352 1353 /* enable interrupts */ 1354 NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED); 1355 1356 callout_schedule(&sc->sc_tick_ch, hz); 1357 1358 ifp->if_flags |= IFF_RUNNING; 1359 ifp->if_flags &= ~IFF_OACTIVE; 1360 1361 out: 1362 return rc; 1363 } 1364 1365 void 1366 nfe_stop(struct ifnet *ifp, int disable) 1367 { 1368 struct nfe_softc *sc = ifp->if_softc; 1369 1370 callout_stop(&sc->sc_tick_ch); 1371 1372 ifp->if_timer = 0; 1373 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1374 1375 mii_down(&sc->sc_mii); 1376 1377 /* abort Tx */ 1378 NFE_WRITE(sc, NFE_TX_CTL, 0); 1379 1380 /* disable Rx */ 1381 NFE_WRITE(sc, NFE_RX_CTL, 0); 1382 1383 /* disable interrupts */ 1384 NFE_WRITE(sc, NFE_IRQ_MASK, 0); 1385 1386 /* reset Tx and Rx rings */ 1387 nfe_reset_tx_ring(sc, &sc->txq); 1388 nfe_reset_rx_ring(sc, &sc->rxq); 1389 } 1390 1391 int 1392 nfe_alloc_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring) 1393 { 1394 struct nfe_desc32 *desc32; 1395 struct nfe_desc64 *desc64; 1396 struct nfe_rx_data *data; 1397 struct nfe_jbuf *jbuf; 1398 void **desc; 1399 bus_addr_t physaddr; 1400 int i, nsegs, error, descsize; 1401 1402 if (sc->sc_flags & NFE_40BIT_ADDR) { 1403 desc = (void **)&ring->desc64; 1404 descsize = sizeof (struct nfe_desc64); 1405 } else { 1406 desc = (void **)&ring->desc32; 1407 descsize = sizeof (struct nfe_desc32); 1408 } 1409 1410 ring->cur = ring->next = 0; 1411 ring->bufsz = MCLBYTES; 1412 1413 error = bus_dmamap_create(sc->sc_dmat, NFE_RX_RING_COUNT * descsize, 1, 1414 NFE_RX_RING_COUNT * descsize, 0, BUS_DMA_NOWAIT, &ring->map); 1415 if (error != 0) { 1416 aprint_error_dev(sc->sc_dev, 1417 "could not create desc DMA map\n"); 1418 ring->map = NULL; 1419 goto fail; 1420 } 1421 1422 error = bus_dmamem_alloc(sc->sc_dmat, NFE_RX_RING_COUNT * descsize, 1423 PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT); 1424 if (error != 0) { 1425 aprint_error_dev(sc->sc_dev, 1426 "could not allocate DMA memory\n"); 1427 goto fail; 1428 } 1429 1430 error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs, 1431 NFE_RX_RING_COUNT * descsize, (void **)desc, BUS_DMA_NOWAIT); 1432 if (error != 0) { 1433 aprint_error_dev(sc->sc_dev, 1434 "could not map desc DMA memory\n"); 1435 goto fail; 1436 } 1437 1438 error = bus_dmamap_load(sc->sc_dmat, ring->map, *desc, 1439 NFE_RX_RING_COUNT * descsize, NULL, BUS_DMA_NOWAIT); 1440 if (error != 0) { 1441 aprint_error_dev(sc->sc_dev, "could not load desc DMA map\n"); 1442 goto fail; 1443 } 1444 1445 memset(*desc, 0, NFE_RX_RING_COUNT * descsize); 1446 ring->physaddr = ring->map->dm_segs[0].ds_addr; 1447 1448 if (sc->sc_flags & NFE_USE_JUMBO) { 1449 ring->bufsz = NFE_JBYTES; 1450 if ((error = nfe_jpool_alloc(sc)) != 0) { 1451 aprint_error_dev(sc->sc_dev, 1452 "could not allocate jumbo frames\n"); 1453 goto fail; 1454 } 1455 } 1456 1457 /* 1458 * Pre-allocate Rx buffers and populate Rx ring. 1459 */ 1460 for (i = 0; i < NFE_RX_RING_COUNT; i++) { 1461 data = &sc->rxq.data[i]; 1462 1463 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 1464 if (data->m == NULL) { 1465 aprint_error_dev(sc->sc_dev, 1466 "could not allocate rx mbuf\n"); 1467 error = ENOMEM; 1468 goto fail; 1469 } 1470 1471 if (sc->sc_flags & NFE_USE_JUMBO) { 1472 if ((jbuf = nfe_jalloc(sc, i)) == NULL) { 1473 aprint_error_dev(sc->sc_dev, 1474 "could not allocate jumbo buffer\n"); 1475 goto fail; 1476 } 1477 MEXTADD(data->m, jbuf->buf, NFE_JBYTES, 0, nfe_jfree, 1478 sc); 1479 1480 physaddr = jbuf->physaddr; 1481 } else { 1482 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, 1483 MCLBYTES, 0, BUS_DMA_NOWAIT, &data->map); 1484 if (error != 0) { 1485 aprint_error_dev(sc->sc_dev, 1486 "could not create DMA map\n"); 1487 data->map = NULL; 1488 goto fail; 1489 } 1490 MCLGET(data->m, M_DONTWAIT); 1491 if (!(data->m->m_flags & M_EXT)) { 1492 aprint_error_dev(sc->sc_dev, 1493 "could not allocate mbuf cluster\n"); 1494 error = ENOMEM; 1495 goto fail; 1496 } 1497 1498 error = bus_dmamap_load(sc->sc_dmat, data->map, 1499 mtod(data->m, void *), MCLBYTES, NULL, 1500 BUS_DMA_READ | BUS_DMA_NOWAIT); 1501 if (error != 0) { 1502 aprint_error_dev(sc->sc_dev, 1503 "could not load rx buf DMA map"); 1504 goto fail; 1505 } 1506 physaddr = data->map->dm_segs[0].ds_addr; 1507 } 1508 1509 if (sc->sc_flags & NFE_40BIT_ADDR) { 1510 desc64 = &sc->rxq.desc64[i]; 1511 #if defined(__LP64__) 1512 desc64->physaddr[0] = htole32(physaddr >> 32); 1513 #endif 1514 desc64->physaddr[1] = htole32(physaddr & 0xffffffff); 1515 desc64->length = htole16(sc->rxq.bufsz); 1516 desc64->flags = htole16(NFE_RX_READY); 1517 } else { 1518 desc32 = &sc->rxq.desc32[i]; 1519 desc32->physaddr = htole32(physaddr); 1520 desc32->length = htole16(sc->rxq.bufsz); 1521 desc32->flags = htole16(NFE_RX_READY); 1522 } 1523 } 1524 1525 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize, 1526 BUS_DMASYNC_PREWRITE); 1527 1528 return 0; 1529 1530 fail: nfe_free_rx_ring(sc, ring); 1531 return error; 1532 } 1533 1534 void 1535 nfe_reset_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring) 1536 { 1537 int i; 1538 1539 for (i = 0; i < NFE_RX_RING_COUNT; i++) { 1540 if (sc->sc_flags & NFE_40BIT_ADDR) { 1541 ring->desc64[i].length = htole16(ring->bufsz); 1542 ring->desc64[i].flags = htole16(NFE_RX_READY); 1543 } else { 1544 ring->desc32[i].length = htole16(ring->bufsz); 1545 ring->desc32[i].flags = htole16(NFE_RX_READY); 1546 } 1547 } 1548 1549 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize, 1550 BUS_DMASYNC_PREWRITE); 1551 1552 ring->cur = ring->next = 0; 1553 } 1554 1555 void 1556 nfe_free_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring) 1557 { 1558 struct nfe_rx_data *data; 1559 void *desc; 1560 int i, descsize; 1561 1562 if (sc->sc_flags & NFE_40BIT_ADDR) { 1563 desc = ring->desc64; 1564 descsize = sizeof (struct nfe_desc64); 1565 } else { 1566 desc = ring->desc32; 1567 descsize = sizeof (struct nfe_desc32); 1568 } 1569 1570 if (desc != NULL) { 1571 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, 1572 ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1573 bus_dmamap_unload(sc->sc_dmat, ring->map); 1574 bus_dmamem_unmap(sc->sc_dmat, (void *)desc, 1575 NFE_RX_RING_COUNT * descsize); 1576 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1); 1577 } 1578 1579 for (i = 0; i < NFE_RX_RING_COUNT; i++) { 1580 data = &ring->data[i]; 1581 1582 if (data->map != NULL) { 1583 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1584 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD); 1585 bus_dmamap_unload(sc->sc_dmat, data->map); 1586 bus_dmamap_destroy(sc->sc_dmat, data->map); 1587 } 1588 if (data->m != NULL) 1589 m_freem(data->m); 1590 } 1591 1592 nfe_jpool_free(sc); 1593 } 1594 1595 struct nfe_jbuf * 1596 nfe_jalloc(struct nfe_softc *sc, int i) 1597 { 1598 struct nfe_jbuf *jbuf; 1599 1600 mutex_enter(&sc->rxq.mtx); 1601 jbuf = SLIST_FIRST(&sc->rxq.jfreelist); 1602 if (jbuf != NULL) 1603 SLIST_REMOVE_HEAD(&sc->rxq.jfreelist, jnext); 1604 mutex_exit(&sc->rxq.mtx); 1605 if (jbuf == NULL) 1606 return NULL; 1607 sc->rxq.jbufmap[i] = 1608 ((char *)jbuf->buf - (char *)sc->rxq.jpool) / NFE_JBYTES; 1609 return jbuf; 1610 } 1611 1612 /* 1613 * This is called automatically by the network stack when the mbuf is freed. 1614 * Caution must be taken that the NIC might be reset by the time the mbuf is 1615 * freed. 1616 */ 1617 void 1618 nfe_jfree(struct mbuf *m, void *buf, size_t size, void *arg) 1619 { 1620 struct nfe_softc *sc = arg; 1621 struct nfe_jbuf *jbuf; 1622 int i; 1623 1624 /* find the jbuf from the base pointer */ 1625 i = ((char *)buf - (char *)sc->rxq.jpool) / NFE_JBYTES; 1626 if (i < 0 || i >= NFE_JPOOL_COUNT) { 1627 aprint_error_dev(sc->sc_dev, 1628 "request to free a buffer (%p) not managed by us\n", buf); 1629 return; 1630 } 1631 jbuf = &sc->rxq.jbuf[i]; 1632 1633 /* ..and put it back in the free list */ 1634 mutex_enter(&sc->rxq.mtx); 1635 SLIST_INSERT_HEAD(&sc->rxq.jfreelist, jbuf, jnext); 1636 mutex_exit(&sc->rxq.mtx); 1637 1638 if (m != NULL) 1639 pool_cache_put(mb_cache, m); 1640 } 1641 1642 int 1643 nfe_jpool_alloc(struct nfe_softc *sc) 1644 { 1645 struct nfe_rx_ring *ring = &sc->rxq; 1646 struct nfe_jbuf *jbuf; 1647 bus_addr_t physaddr; 1648 char *buf; 1649 int i, nsegs, error; 1650 1651 /* 1652 * Allocate a big chunk of DMA'able memory. 1653 */ 1654 error = bus_dmamap_create(sc->sc_dmat, NFE_JPOOL_SIZE, 1, 1655 NFE_JPOOL_SIZE, 0, BUS_DMA_NOWAIT, &ring->jmap); 1656 if (error != 0) { 1657 aprint_error_dev(sc->sc_dev, 1658 "could not create jumbo DMA map\n"); 1659 ring->jmap = NULL; 1660 goto fail; 1661 } 1662 1663 error = bus_dmamem_alloc(sc->sc_dmat, NFE_JPOOL_SIZE, PAGE_SIZE, 0, 1664 &ring->jseg, 1, &nsegs, BUS_DMA_NOWAIT); 1665 if (error != 0) { 1666 aprint_error_dev(sc->sc_dev, 1667 "could not allocate jumbo DMA memory\n"); 1668 goto fail; 1669 } 1670 1671 error = bus_dmamem_map(sc->sc_dmat, &ring->jseg, nsegs, NFE_JPOOL_SIZE, 1672 &ring->jpool, BUS_DMA_NOWAIT); 1673 if (error != 0) { 1674 aprint_error_dev(sc->sc_dev, 1675 "could not map jumbo DMA memory\n"); 1676 goto fail; 1677 } 1678 1679 error = bus_dmamap_load(sc->sc_dmat, ring->jmap, ring->jpool, 1680 NFE_JPOOL_SIZE, NULL, BUS_DMA_READ | BUS_DMA_NOWAIT); 1681 if (error != 0) { 1682 aprint_error_dev(sc->sc_dev, 1683 "could not load jumbo DMA map\n"); 1684 goto fail; 1685 } 1686 1687 /* ..and split it into 9KB chunks */ 1688 SLIST_INIT(&ring->jfreelist); 1689 1690 buf = ring->jpool; 1691 physaddr = ring->jmap->dm_segs[0].ds_addr; 1692 for (i = 0; i < NFE_JPOOL_COUNT; i++) { 1693 jbuf = &ring->jbuf[i]; 1694 1695 jbuf->buf = buf; 1696 jbuf->physaddr = physaddr; 1697 1698 SLIST_INSERT_HEAD(&ring->jfreelist, jbuf, jnext); 1699 1700 buf += NFE_JBYTES; 1701 physaddr += NFE_JBYTES; 1702 } 1703 1704 return 0; 1705 1706 fail: nfe_jpool_free(sc); 1707 return error; 1708 } 1709 1710 void 1711 nfe_jpool_free(struct nfe_softc *sc) 1712 { 1713 struct nfe_rx_ring *ring = &sc->rxq; 1714 1715 if (ring->jmap != NULL) { 1716 bus_dmamap_sync(sc->sc_dmat, ring->jmap, 0, 1717 ring->jmap->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1718 bus_dmamap_unload(sc->sc_dmat, ring->jmap); 1719 bus_dmamap_destroy(sc->sc_dmat, ring->jmap); 1720 ring->jmap = NULL; 1721 } 1722 if (ring->jpool != NULL) { 1723 bus_dmamem_unmap(sc->sc_dmat, ring->jpool, NFE_JPOOL_SIZE); 1724 bus_dmamem_free(sc->sc_dmat, &ring->jseg, 1); 1725 ring->jpool = NULL; 1726 } 1727 } 1728 1729 int 1730 nfe_alloc_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring) 1731 { 1732 int i, nsegs, error; 1733 void **desc; 1734 int descsize; 1735 1736 if (sc->sc_flags & NFE_40BIT_ADDR) { 1737 desc = (void **)&ring->desc64; 1738 descsize = sizeof (struct nfe_desc64); 1739 } else { 1740 desc = (void **)&ring->desc32; 1741 descsize = sizeof (struct nfe_desc32); 1742 } 1743 1744 ring->queued = 0; 1745 ring->cur = ring->next = 0; 1746 1747 error = bus_dmamap_create(sc->sc_dmat, NFE_TX_RING_COUNT * descsize, 1, 1748 NFE_TX_RING_COUNT * descsize, 0, BUS_DMA_NOWAIT, &ring->map); 1749 1750 if (error != 0) { 1751 aprint_error_dev(sc->sc_dev, 1752 "could not create desc DMA map\n"); 1753 ring->map = NULL; 1754 goto fail; 1755 } 1756 1757 error = bus_dmamem_alloc(sc->sc_dmat, NFE_TX_RING_COUNT * descsize, 1758 PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT); 1759 if (error != 0) { 1760 aprint_error_dev(sc->sc_dev, 1761 "could not allocate DMA memory\n"); 1762 goto fail; 1763 } 1764 1765 error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs, 1766 NFE_TX_RING_COUNT * descsize, (void **)desc, BUS_DMA_NOWAIT); 1767 if (error != 0) { 1768 aprint_error_dev(sc->sc_dev, 1769 "could not map desc DMA memory\n"); 1770 goto fail; 1771 } 1772 1773 error = bus_dmamap_load(sc->sc_dmat, ring->map, *desc, 1774 NFE_TX_RING_COUNT * descsize, NULL, BUS_DMA_NOWAIT); 1775 if (error != 0) { 1776 aprint_error_dev(sc->sc_dev, "could not load desc DMA map\n"); 1777 goto fail; 1778 } 1779 1780 memset(*desc, 0, NFE_TX_RING_COUNT * descsize); 1781 ring->physaddr = ring->map->dm_segs[0].ds_addr; 1782 1783 for (i = 0; i < NFE_TX_RING_COUNT; i++) { 1784 error = bus_dmamap_create(sc->sc_dmat, NFE_JBYTES, 1785 NFE_MAX_SCATTER, NFE_JBYTES, 0, BUS_DMA_NOWAIT, 1786 &ring->data[i].map); 1787 if (error != 0) { 1788 aprint_error_dev(sc->sc_dev, 1789 "could not create DMA map\n"); 1790 ring->data[i].map = NULL; 1791 goto fail; 1792 } 1793 } 1794 1795 return 0; 1796 1797 fail: nfe_free_tx_ring(sc, ring); 1798 return error; 1799 } 1800 1801 void 1802 nfe_reset_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring) 1803 { 1804 struct nfe_tx_data *data; 1805 int i; 1806 1807 for (i = 0; i < NFE_TX_RING_COUNT; i++) { 1808 if (sc->sc_flags & NFE_40BIT_ADDR) 1809 ring->desc64[i].flags = 0; 1810 else 1811 ring->desc32[i].flags = 0; 1812 1813 data = &ring->data[i]; 1814 1815 if (data->m != NULL) { 1816 bus_dmamap_sync(sc->sc_dmat, data->active, 0, 1817 data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1818 bus_dmamap_unload(sc->sc_dmat, data->active); 1819 m_freem(data->m); 1820 data->m = NULL; 1821 } 1822 } 1823 1824 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize, 1825 BUS_DMASYNC_PREWRITE); 1826 1827 ring->queued = 0; 1828 ring->cur = ring->next = 0; 1829 } 1830 1831 void 1832 nfe_free_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring) 1833 { 1834 struct nfe_tx_data *data; 1835 void *desc; 1836 int i, descsize; 1837 1838 if (sc->sc_flags & NFE_40BIT_ADDR) { 1839 desc = ring->desc64; 1840 descsize = sizeof (struct nfe_desc64); 1841 } else { 1842 desc = ring->desc32; 1843 descsize = sizeof (struct nfe_desc32); 1844 } 1845 1846 if (desc != NULL) { 1847 bus_dmamap_sync(sc->sc_dmat, ring->map, 0, 1848 ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1849 bus_dmamap_unload(sc->sc_dmat, ring->map); 1850 bus_dmamem_unmap(sc->sc_dmat, (void *)desc, 1851 NFE_TX_RING_COUNT * descsize); 1852 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1); 1853 } 1854 1855 for (i = 0; i < NFE_TX_RING_COUNT; i++) { 1856 data = &ring->data[i]; 1857 1858 if (data->m != NULL) { 1859 bus_dmamap_sync(sc->sc_dmat, data->active, 0, 1860 data->active->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1861 bus_dmamap_unload(sc->sc_dmat, data->active); 1862 m_freem(data->m); 1863 } 1864 } 1865 1866 /* ..and now actually destroy the DMA mappings */ 1867 for (i = 0; i < NFE_TX_RING_COUNT; i++) { 1868 data = &ring->data[i]; 1869 if (data->map == NULL) 1870 continue; 1871 bus_dmamap_destroy(sc->sc_dmat, data->map); 1872 } 1873 } 1874 1875 void 1876 nfe_setmulti(struct nfe_softc *sc) 1877 { 1878 struct ethercom *ec = &sc->sc_ethercom; 1879 struct ifnet *ifp = &ec->ec_if; 1880 struct ether_multi *enm; 1881 struct ether_multistep step; 1882 uint8_t addr[ETHER_ADDR_LEN], mask[ETHER_ADDR_LEN]; 1883 uint32_t filter = NFE_RXFILTER_MAGIC; 1884 int i; 1885 1886 if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) { 1887 memset(addr, 0, ETHER_ADDR_LEN); 1888 memset(mask, 0, ETHER_ADDR_LEN); 1889 goto done; 1890 } 1891 1892 memcpy(addr, etherbroadcastaddr, ETHER_ADDR_LEN); 1893 memcpy(mask, etherbroadcastaddr, ETHER_ADDR_LEN); 1894 1895 ETHER_FIRST_MULTI(step, ec, enm); 1896 while (enm != NULL) { 1897 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 1898 ifp->if_flags |= IFF_ALLMULTI; 1899 memset(addr, 0, ETHER_ADDR_LEN); 1900 memset(mask, 0, ETHER_ADDR_LEN); 1901 goto done; 1902 } 1903 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1904 addr[i] &= enm->enm_addrlo[i]; 1905 mask[i] &= ~enm->enm_addrlo[i]; 1906 } 1907 ETHER_NEXT_MULTI(step, enm); 1908 } 1909 for (i = 0; i < ETHER_ADDR_LEN; i++) 1910 mask[i] |= addr[i]; 1911 1912 done: 1913 addr[0] |= 0x01; /* make sure multicast bit is set */ 1914 1915 NFE_WRITE(sc, NFE_MULTIADDR_HI, 1916 addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]); 1917 NFE_WRITE(sc, NFE_MULTIADDR_LO, 1918 addr[5] << 8 | addr[4]); 1919 NFE_WRITE(sc, NFE_MULTIMASK_HI, 1920 mask[3] << 24 | mask[2] << 16 | mask[1] << 8 | mask[0]); 1921 NFE_WRITE(sc, NFE_MULTIMASK_LO, 1922 mask[5] << 8 | mask[4]); 1923 1924 filter |= (ifp->if_flags & IFF_PROMISC) ? NFE_PROMISC : NFE_U2M; 1925 NFE_WRITE(sc, NFE_RXFILTER, filter); 1926 } 1927 1928 void 1929 nfe_get_macaddr(struct nfe_softc *sc, uint8_t *addr) 1930 { 1931 uint32_t tmp; 1932 1933 if ((sc->sc_flags & NFE_CORRECT_MACADDR) != 0) { 1934 tmp = NFE_READ(sc, NFE_MACADDR_HI); 1935 addr[0] = (tmp & 0xff); 1936 addr[1] = (tmp >> 8) & 0xff; 1937 addr[2] = (tmp >> 16) & 0xff; 1938 addr[3] = (tmp >> 24) & 0xff; 1939 1940 tmp = NFE_READ(sc, NFE_MACADDR_LO); 1941 addr[4] = (tmp & 0xff); 1942 addr[5] = (tmp >> 8) & 0xff; 1943 1944 } else { 1945 tmp = NFE_READ(sc, NFE_MACADDR_LO); 1946 addr[0] = (tmp >> 8) & 0xff; 1947 addr[1] = (tmp & 0xff); 1948 1949 tmp = NFE_READ(sc, NFE_MACADDR_HI); 1950 addr[2] = (tmp >> 24) & 0xff; 1951 addr[3] = (tmp >> 16) & 0xff; 1952 addr[4] = (tmp >> 8) & 0xff; 1953 addr[5] = (tmp & 0xff); 1954 } 1955 } 1956 1957 void 1958 nfe_set_macaddr(struct nfe_softc *sc, const uint8_t *addr) 1959 { 1960 NFE_WRITE(sc, NFE_MACADDR_LO, 1961 addr[5] << 8 | addr[4]); 1962 NFE_WRITE(sc, NFE_MACADDR_HI, 1963 addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]); 1964 } 1965 1966 void 1967 nfe_tick(void *arg) 1968 { 1969 struct nfe_softc *sc = arg; 1970 int s; 1971 1972 s = splnet(); 1973 mii_tick(&sc->sc_mii); 1974 splx(s); 1975 1976 callout_schedule(&sc->sc_tick_ch, hz); 1977 } 1978 1979 void 1980 nfe_poweron(device_t self) 1981 { 1982 struct nfe_softc *sc = device_private(self); 1983 1984 if ((sc->sc_flags & NFE_PWR_MGMT) != 0) { 1985 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | NFE_RXTX_BIT2); 1986 NFE_WRITE(sc, NFE_MAC_RESET, NFE_MAC_RESET_MAGIC); 1987 DELAY(100); 1988 NFE_WRITE(sc, NFE_MAC_RESET, 0); 1989 DELAY(100); 1990 NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT2); 1991 NFE_WRITE(sc, NFE_PWR2_CTL, 1992 NFE_READ(sc, NFE_PWR2_CTL) & ~NFE_PWR2_WAKEUP_MASK); 1993 } 1994 } 1995 1996 bool 1997 nfe_resume(device_t dv, const pmf_qual_t *qual) 1998 { 1999 nfe_poweron(dv); 2000 2001 return true; 2002 } 2003