1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011-2012 Stefan Bethke. 5 * Copyright (c) 2012 Adrian Chadd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/errno.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/socket.h> 39 #include <sys/sockio.h> 40 #include <sys/sysctl.h> 41 #include <sys/systm.h> 42 43 #include <net/if.h> 44 #include <net/if_var.h> 45 #include <net/if_arp.h> 46 #include <net/ethernet.h> 47 #include <net/if_dl.h> 48 #include <net/if_media.h> 49 #include <net/if_types.h> 50 51 #include <machine/bus.h> 52 #include <dev/iicbus/iic.h> 53 #include <dev/iicbus/iiconf.h> 54 #include <dev/iicbus/iicbus.h> 55 #include <dev/mii/mii.h> 56 #include <dev/mii/miivar.h> 57 #include <dev/mdio/mdio.h> 58 59 #include <dev/etherswitch/etherswitch.h> 60 61 #include <dev/etherswitch/arswitch/arswitchreg.h> 62 #include <dev/etherswitch/arswitch/arswitchvar.h> 63 #include <dev/etherswitch/arswitch/arswitch_reg.h> 64 #include <dev/etherswitch/arswitch/arswitch_phy.h> 65 #include <dev/etherswitch/arswitch/arswitch_vlans.h> 66 67 #include <dev/etherswitch/arswitch/arswitch_7240.h> 68 #include <dev/etherswitch/arswitch/arswitch_8216.h> 69 #include <dev/etherswitch/arswitch/arswitch_8226.h> 70 #include <dev/etherswitch/arswitch/arswitch_8316.h> 71 #include <dev/etherswitch/arswitch/arswitch_8327.h> 72 #include <dev/etherswitch/arswitch/arswitch_9340.h> 73 74 #include "mdio_if.h" 75 #include "miibus_if.h" 76 #include "etherswitch_if.h" 77 78 /* Map ETHERSWITCH_PORT_LED_* to Atheros pattern codes */ 79 static int led_pattern_table[] = { 80 [ETHERSWITCH_PORT_LED_DEFAULT] = 0x3, 81 [ETHERSWITCH_PORT_LED_ON] = 0x2, 82 [ETHERSWITCH_PORT_LED_OFF] = 0x0, 83 [ETHERSWITCH_PORT_LED_BLINK] = 0x1 84 }; 85 86 static inline int arswitch_portforphy(int phy); 87 static void arswitch_tick(void *arg); 88 static int arswitch_ifmedia_upd(struct ifnet *); 89 static void arswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *); 90 static int ar8xxx_port_vlan_setup(struct arswitch_softc *sc, 91 etherswitch_port_t *p); 92 static int ar8xxx_port_vlan_get(struct arswitch_softc *sc, 93 etherswitch_port_t *p); 94 static int arswitch_setled(struct arswitch_softc *sc, int phy, int led, 95 int style); 96 97 static int 98 arswitch_probe(device_t dev) 99 { 100 struct arswitch_softc *sc; 101 uint32_t id; 102 char *chipname, desc[256]; 103 104 sc = device_get_softc(dev); 105 bzero(sc, sizeof(*sc)); 106 sc->page = -1; 107 108 /* AR7240 probe */ 109 if (ar7240_probe(dev) == 0) { 110 chipname = "AR7240"; 111 sc->sc_switchtype = AR8X16_SWITCH_AR7240; 112 sc->is_internal_switch = 1; 113 id = 0; 114 goto done; 115 } 116 117 /* AR9340 probe */ 118 if (ar9340_probe(dev) == 0) { 119 chipname = "AR9340"; 120 sc->sc_switchtype = AR8X16_SWITCH_AR9340; 121 sc->is_internal_switch = 1; 122 id = 0; 123 goto done; 124 } 125 126 /* AR8xxx probe */ 127 id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL); 128 sc->chip_rev = (id & AR8X16_MASK_CTRL_REV_MASK); 129 sc->chip_ver = (id & AR8X16_MASK_CTRL_VER_MASK) > AR8X16_MASK_CTRL_VER_SHIFT; 130 switch (id & (AR8X16_MASK_CTRL_VER_MASK | AR8X16_MASK_CTRL_REV_MASK)) { 131 case 0x0101: 132 chipname = "AR8216"; 133 sc->sc_switchtype = AR8X16_SWITCH_AR8216; 134 break; 135 case 0x0201: 136 chipname = "AR8226"; 137 sc->sc_switchtype = AR8X16_SWITCH_AR8226; 138 break; 139 /* 0x0301 - AR8236 */ 140 case 0x1000: 141 case 0x1001: 142 chipname = "AR8316"; 143 sc->sc_switchtype = AR8X16_SWITCH_AR8316; 144 break; 145 case 0x1202: 146 case 0x1204: 147 chipname = "AR8327"; 148 sc->sc_switchtype = AR8X16_SWITCH_AR8327; 149 sc->mii_lo_first = 1; 150 break; 151 default: 152 chipname = NULL; 153 } 154 155 done: 156 157 DPRINTF(sc, ARSWITCH_DBG_ANY, "chipname=%s, id=%08x\n", chipname, id); 158 if (chipname != NULL) { 159 snprintf(desc, sizeof(desc), 160 "Atheros %s Ethernet Switch (ver %d rev %d)", 161 chipname, 162 sc->chip_ver, 163 sc->chip_rev); 164 device_set_desc_copy(dev, desc); 165 return (BUS_PROBE_DEFAULT); 166 } 167 return (ENXIO); 168 } 169 170 static int 171 arswitch_attach_phys(struct arswitch_softc *sc) 172 { 173 int phy, err = 0; 174 char name[IFNAMSIZ]; 175 176 /* PHYs need an interface, so we generate a dummy one */ 177 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev)); 178 for (phy = 0; phy < sc->numphys; phy++) { 179 sc->ifp[phy] = if_alloc(IFT_ETHER); 180 sc->ifp[phy]->if_softc = sc; 181 sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST | 182 IFF_DRV_RUNNING | IFF_SIMPLEX; 183 sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK); 184 bcopy(name, sc->ifname[phy], strlen(name)+1); 185 if_initname(sc->ifp[phy], sc->ifname[phy], 186 arswitch_portforphy(phy)); 187 err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy], 188 arswitch_ifmedia_upd, arswitch_ifmedia_sts, \ 189 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0); 190 #if 0 191 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n", 192 device_get_nameunit(sc->miibus[phy]), 193 sc->ifp[phy]->if_xname); 194 #endif 195 if (err != 0) { 196 device_printf(sc->sc_dev, 197 "attaching PHY %d failed\n", 198 phy); 199 return (err); 200 } 201 202 if (AR8X16_IS_SWITCH(sc, AR8327)) { 203 int led; 204 char ledname[IFNAMSIZ+4]; 205 206 for (led = 0; led < 3; led++) { 207 sprintf(ledname, "%s%dled%d", name, 208 arswitch_portforphy(phy), led+1); 209 sc->dev_led[phy][led].sc = sc; 210 sc->dev_led[phy][led].phy = phy; 211 sc->dev_led[phy][led].lednum = led; 212 } 213 } 214 } 215 return (0); 216 } 217 218 static int 219 arswitch_reset(device_t dev) 220 { 221 222 arswitch_writereg(dev, AR8X16_REG_MASK_CTRL, 223 AR8X16_MASK_CTRL_SOFT_RESET); 224 DELAY(1000); 225 if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) & 226 AR8X16_MASK_CTRL_SOFT_RESET) { 227 device_printf(dev, "unable to reset switch\n"); 228 return (-1); 229 } 230 return (0); 231 } 232 233 static int 234 arswitch_set_vlan_mode(struct arswitch_softc *sc, uint32_t mode) 235 { 236 237 /* Check for invalid modes. */ 238 if ((mode & sc->info.es_vlan_caps) != mode) 239 return (EINVAL); 240 241 switch (mode) { 242 case ETHERSWITCH_VLAN_DOT1Q: 243 sc->vlan_mode = ETHERSWITCH_VLAN_DOT1Q; 244 break; 245 case ETHERSWITCH_VLAN_PORT: 246 sc->vlan_mode = ETHERSWITCH_VLAN_PORT; 247 break; 248 default: 249 sc->vlan_mode = 0; 250 } 251 252 /* Reset VLANs. */ 253 sc->hal.arswitch_vlan_init_hw(sc); 254 255 return (0); 256 } 257 258 static void 259 ar8xxx_port_init(struct arswitch_softc *sc, int port) 260 { 261 262 /* Port0 - CPU */ 263 if (port == AR8X16_PORT_CPU) { 264 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_STS(0), 265 (AR8X16_IS_SWITCH(sc, AR8216) ? 266 AR8X16_PORT_STS_SPEED_100 : AR8X16_PORT_STS_SPEED_1000) | 267 (AR8X16_IS_SWITCH(sc, AR8216) ? 0 : AR8X16_PORT_STS_RXFLOW) | 268 (AR8X16_IS_SWITCH(sc, AR8216) ? 0 : AR8X16_PORT_STS_TXFLOW) | 269 AR8X16_PORT_STS_RXMAC | 270 AR8X16_PORT_STS_TXMAC | 271 AR8X16_PORT_STS_DUPLEX); 272 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_CTRL(0), 273 arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(0)) & 274 ~AR8X16_PORT_CTRL_HEADER); 275 } else { 276 /* Set ports to auto negotiation. */ 277 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_STS(port), 278 AR8X16_PORT_STS_LINK_AUTO); 279 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_CTRL(port), 280 arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(port)) & 281 ~AR8X16_PORT_CTRL_HEADER); 282 } 283 } 284 285 static int 286 ar8xxx_atu_flush(struct arswitch_softc *sc) 287 { 288 int ret; 289 290 ret = arswitch_waitreg(sc->sc_dev, 291 AR8216_REG_ATU, 292 AR8216_ATU_ACTIVE, 293 0, 294 1000); 295 296 if (ret) 297 device_printf(sc->sc_dev, "%s: waitreg failed\n", __func__); 298 299 if (!ret) 300 arswitch_writereg(sc->sc_dev, 301 AR8216_REG_ATU, 302 AR8216_ATU_OP_FLUSH); 303 304 return (ret); 305 } 306 307 static int 308 arswitch_attach(device_t dev) 309 { 310 struct arswitch_softc *sc = device_get_softc(dev); 311 struct sysctl_ctx_list *ctx; 312 struct sysctl_oid *tree; 313 int err = 0; 314 int port; 315 316 /* sc->sc_switchtype is already decided in arswitch_probe() */ 317 sc->sc_dev = dev; 318 mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF); 319 sc->page = -1; 320 strlcpy(sc->info.es_name, device_get_desc(dev), 321 sizeof(sc->info.es_name)); 322 323 /* Debugging */ 324 ctx = device_get_sysctl_ctx(sc->sc_dev); 325 tree = device_get_sysctl_tree(sc->sc_dev); 326 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 327 "debug", CTLFLAG_RW, &sc->sc_debug, 0, 328 "control debugging printfs"); 329 330 /* Default HAL methods */ 331 sc->hal.arswitch_port_init = ar8xxx_port_init; 332 sc->hal.arswitch_port_vlan_setup = ar8xxx_port_vlan_setup; 333 sc->hal.arswitch_port_vlan_get = ar8xxx_port_vlan_get; 334 sc->hal.arswitch_vlan_init_hw = ar8xxx_reset_vlans; 335 336 sc->hal.arswitch_vlan_getvgroup = ar8xxx_getvgroup; 337 sc->hal.arswitch_vlan_setvgroup = ar8xxx_setvgroup; 338 339 sc->hal.arswitch_vlan_get_pvid = ar8xxx_get_pvid; 340 sc->hal.arswitch_vlan_set_pvid = ar8xxx_set_pvid; 341 342 sc->hal.arswitch_get_dot1q_vlan = ar8xxx_get_dot1q_vlan; 343 sc->hal.arswitch_set_dot1q_vlan = ar8xxx_set_dot1q_vlan; 344 sc->hal.arswitch_flush_dot1q_vlan = ar8xxx_flush_dot1q_vlan; 345 sc->hal.arswitch_purge_dot1q_vlan = ar8xxx_purge_dot1q_vlan; 346 sc->hal.arswitch_get_port_vlan = ar8xxx_get_port_vlan; 347 sc->hal.arswitch_set_port_vlan = ar8xxx_set_port_vlan; 348 349 sc->hal.arswitch_atu_flush = ar8xxx_atu_flush; 350 351 sc->hal.arswitch_phy_read = arswitch_readphy_internal; 352 sc->hal.arswitch_phy_write = arswitch_writephy_internal; 353 354 355 /* 356 * Attach switch related functions 357 */ 358 if (AR8X16_IS_SWITCH(sc, AR7240)) 359 ar7240_attach(sc); 360 else if (AR8X16_IS_SWITCH(sc, AR9340)) 361 ar9340_attach(sc); 362 else if (AR8X16_IS_SWITCH(sc, AR8216)) 363 ar8216_attach(sc); 364 else if (AR8X16_IS_SWITCH(sc, AR8226)) 365 ar8226_attach(sc); 366 else if (AR8X16_IS_SWITCH(sc, AR8316)) 367 ar8316_attach(sc); 368 else if (AR8X16_IS_SWITCH(sc, AR8327)) 369 ar8327_attach(sc); 370 else { 371 DPRINTF(sc, ARSWITCH_DBG_ANY, 372 "%s: unknown switch (%d)?\n", __func__, sc->sc_switchtype); 373 return (ENXIO); 374 } 375 376 /* Common defaults. */ 377 sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */ 378 379 /* XXX Defaults for externally connected AR8316 */ 380 sc->numphys = 4; 381 sc->phy4cpu = 1; 382 sc->is_rgmii = 1; 383 sc->is_gmii = 0; 384 sc->is_mii = 0; 385 386 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 387 "numphys", &sc->numphys); 388 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 389 "phy4cpu", &sc->phy4cpu); 390 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 391 "is_rgmii", &sc->is_rgmii); 392 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 393 "is_gmii", &sc->is_gmii); 394 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 395 "is_mii", &sc->is_mii); 396 397 if (sc->numphys > AR8X16_NUM_PHYS) 398 sc->numphys = AR8X16_NUM_PHYS; 399 400 /* Reset the switch. */ 401 if (arswitch_reset(dev)) { 402 DPRINTF(sc, ARSWITCH_DBG_ANY, 403 "%s: arswitch_reset: failed\n", __func__); 404 return (ENXIO); 405 } 406 407 err = sc->hal.arswitch_hw_setup(sc); 408 if (err != 0) { 409 DPRINTF(sc, ARSWITCH_DBG_ANY, 410 "%s: hw_setup: err=%d\n", __func__, err); 411 return (err); 412 } 413 414 err = sc->hal.arswitch_hw_global_setup(sc); 415 if (err != 0) { 416 DPRINTF(sc, ARSWITCH_DBG_ANY, 417 "%s: hw_global_setup: err=%d\n", __func__, err); 418 return (err); 419 } 420 421 /* Initialize the switch ports. */ 422 for (port = 0; port <= sc->numphys; port++) { 423 sc->hal.arswitch_port_init(sc, port); 424 } 425 426 /* 427 * Attach the PHYs and complete the bus enumeration. 428 */ 429 err = arswitch_attach_phys(sc); 430 if (err != 0) { 431 DPRINTF(sc, ARSWITCH_DBG_ANY, 432 "%s: attach_phys: err=%d\n", __func__, err); 433 return (err); 434 } 435 436 /* Default to ingress filters off. */ 437 err = arswitch_set_vlan_mode(sc, 0); 438 if (err != 0) { 439 DPRINTF(sc, ARSWITCH_DBG_ANY, 440 "%s: set_vlan_mode: err=%d\n", __func__, err); 441 return (err); 442 } 443 444 bus_generic_probe(dev); 445 bus_enumerate_hinted_children(dev); 446 err = bus_generic_attach(dev); 447 if (err != 0) { 448 DPRINTF(sc, ARSWITCH_DBG_ANY, 449 "%s: bus_generic_attach: err=%d\n", __func__, err); 450 return (err); 451 } 452 453 callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0); 454 455 ARSWITCH_LOCK(sc); 456 arswitch_tick(sc); 457 ARSWITCH_UNLOCK(sc); 458 459 return (err); 460 } 461 462 static int 463 arswitch_detach(device_t dev) 464 { 465 struct arswitch_softc *sc = device_get_softc(dev); 466 int i; 467 468 callout_drain(&sc->callout_tick); 469 470 for (i=0; i < sc->numphys; i++) { 471 if (sc->miibus[i] != NULL) 472 device_delete_child(dev, sc->miibus[i]); 473 if (sc->ifp[i] != NULL) 474 if_free(sc->ifp[i]); 475 free(sc->ifname[i], M_DEVBUF); 476 } 477 478 bus_generic_detach(dev); 479 mtx_destroy(&sc->sc_mtx); 480 481 return (0); 482 } 483 484 /* 485 * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to 486 * port 2, etc. 487 */ 488 static inline int 489 arswitch_portforphy(int phy) 490 { 491 return (phy+1); 492 } 493 494 static inline struct mii_data * 495 arswitch_miiforport(struct arswitch_softc *sc, int port) 496 { 497 int phy = port-1; 498 499 if (phy < 0 || phy >= sc->numphys) 500 return (NULL); 501 return (device_get_softc(sc->miibus[phy])); 502 } 503 504 static inline struct ifnet * 505 arswitch_ifpforport(struct arswitch_softc *sc, int port) 506 { 507 int phy = port-1; 508 509 if (phy < 0 || phy >= sc->numphys) 510 return (NULL); 511 return (sc->ifp[phy]); 512 } 513 514 /* 515 * Convert port status to ifmedia. 516 */ 517 static void 518 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 519 { 520 *media_active = IFM_ETHER; 521 *media_status = IFM_AVALID; 522 523 if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0) 524 *media_status |= IFM_ACTIVE; 525 else { 526 *media_active |= IFM_NONE; 527 return; 528 } 529 switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) { 530 case AR8X16_PORT_STS_SPEED_10: 531 *media_active |= IFM_10_T; 532 break; 533 case AR8X16_PORT_STS_SPEED_100: 534 *media_active |= IFM_100_TX; 535 break; 536 case AR8X16_PORT_STS_SPEED_1000: 537 *media_active |= IFM_1000_T; 538 break; 539 } 540 if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0) 541 *media_active |= IFM_FDX; 542 else 543 *media_active |= IFM_HDX; 544 if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0) 545 *media_active |= IFM_ETH_TXPAUSE; 546 if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0) 547 *media_active |= IFM_ETH_RXPAUSE; 548 } 549 550 /* 551 * Poll the status for all PHYs. We're using the switch port status because 552 * thats a lot quicker to read than talking to all the PHYs. Care must be 553 * taken that the resulting ifmedia_active is identical to what the PHY will 554 * compute, or gratuitous link status changes will occur whenever the PHYs 555 * update function is called. 556 */ 557 static void 558 arswitch_miipollstat(struct arswitch_softc *sc) 559 { 560 int i; 561 struct mii_data *mii; 562 struct mii_softc *miisc; 563 int portstatus; 564 int port_flap = 0; 565 566 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED); 567 568 for (i = 0; i < sc->numphys; i++) { 569 if (sc->miibus[i] == NULL) 570 continue; 571 mii = device_get_softc(sc->miibus[i]); 572 /* XXX This would be nice to have abstracted out to be per-chip */ 573 /* AR8327/AR8337 has a different register base */ 574 if (AR8X16_IS_SWITCH(sc, AR8327)) 575 portstatus = arswitch_readreg(sc->sc_dev, 576 AR8327_REG_PORT_STATUS(arswitch_portforphy(i))); 577 else 578 portstatus = arswitch_readreg(sc->sc_dev, 579 AR8X16_REG_PORT_STS(arswitch_portforphy(i))); 580 #if 1 581 DPRINTF(sc, ARSWITCH_DBG_POLL, "p[%d]=0x%08x (%b)\n", 582 i, 583 portstatus, 584 portstatus, 585 "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7" 586 "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE"); 587 #endif 588 /* 589 * If the current status is down, but we have a link 590 * status showing up, we need to do an ATU flush. 591 */ 592 if ((mii->mii_media_status & IFM_ACTIVE) == 0 && 593 (portstatus & AR8X16_PORT_STS_LINK_UP) != 0) { 594 device_printf(sc->sc_dev, "%s: port %d: port -> UP\n", 595 __func__, 596 i); 597 port_flap = 1; 598 } 599 /* 600 * and maybe if a port goes up->down? 601 */ 602 if ((mii->mii_media_status & IFM_ACTIVE) != 0 && 603 (portstatus & AR8X16_PORT_STS_LINK_UP) == 0) { 604 device_printf(sc->sc_dev, "%s: port %d: port -> DOWN\n", 605 __func__, 606 i); 607 port_flap = 1; 608 } 609 arswitch_update_ifmedia(portstatus, &mii->mii_media_status, 610 &mii->mii_media_active); 611 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 612 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != 613 miisc->mii_inst) 614 continue; 615 mii_phy_update(miisc, MII_POLLSTAT); 616 } 617 } 618 619 /* If a port went from down->up, flush the ATU */ 620 if (port_flap) 621 sc->hal.arswitch_atu_flush(sc); 622 } 623 624 static void 625 arswitch_tick(void *arg) 626 { 627 struct arswitch_softc *sc = arg; 628 629 arswitch_miipollstat(sc); 630 callout_reset(&sc->callout_tick, hz, arswitch_tick, sc); 631 } 632 633 static void 634 arswitch_lock(device_t dev) 635 { 636 struct arswitch_softc *sc = device_get_softc(dev); 637 638 ARSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED); 639 ARSWITCH_LOCK(sc); 640 } 641 642 static void 643 arswitch_unlock(device_t dev) 644 { 645 struct arswitch_softc *sc = device_get_softc(dev); 646 647 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED); 648 ARSWITCH_UNLOCK(sc); 649 } 650 651 static etherswitch_info_t * 652 arswitch_getinfo(device_t dev) 653 { 654 struct arswitch_softc *sc = device_get_softc(dev); 655 656 return (&sc->info); 657 } 658 659 static int 660 ar8xxx_port_vlan_get(struct arswitch_softc *sc, etherswitch_port_t *p) 661 { 662 uint32_t reg; 663 664 ARSWITCH_LOCK(sc); 665 666 /* Retrieve the PVID. */ 667 sc->hal.arswitch_vlan_get_pvid(sc, p->es_port, &p->es_pvid); 668 669 /* Port flags. */ 670 reg = arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(p->es_port)); 671 if (reg & AR8X16_PORT_CTRL_DOUBLE_TAG) 672 p->es_flags |= ETHERSWITCH_PORT_DOUBLE_TAG; 673 reg >>= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT; 674 if ((reg & 0x3) == AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_ADD) 675 p->es_flags |= ETHERSWITCH_PORT_ADDTAG; 676 if ((reg & 0x3) == AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_STRIP) 677 p->es_flags |= ETHERSWITCH_PORT_STRIPTAG; 678 ARSWITCH_UNLOCK(sc); 679 680 return (0); 681 } 682 683 static int 684 arswitch_is_cpuport(struct arswitch_softc *sc, int port) 685 { 686 687 return ((port == AR8X16_PORT_CPU) || 688 ((AR8X16_IS_SWITCH(sc, AR8327) && 689 port == AR8327_PORT_GMAC6))); 690 } 691 692 static int 693 arswitch_getport(device_t dev, etherswitch_port_t *p) 694 { 695 struct arswitch_softc *sc; 696 struct mii_data *mii; 697 struct ifmediareq *ifmr; 698 int err; 699 700 sc = device_get_softc(dev); 701 /* XXX +1 is for AR8327; should make this configurable! */ 702 if (p->es_port < 0 || p->es_port > sc->info.es_nports) 703 return (ENXIO); 704 705 err = sc->hal.arswitch_port_vlan_get(sc, p); 706 if (err != 0) 707 return (err); 708 709 mii = arswitch_miiforport(sc, p->es_port); 710 if (arswitch_is_cpuport(sc, p->es_port)) { 711 /* fill in fixed values for CPU port */ 712 /* XXX is this valid in all cases? */ 713 p->es_flags |= ETHERSWITCH_PORT_CPU; 714 ifmr = &p->es_ifmr; 715 ifmr->ifm_count = 0; 716 ifmr->ifm_current = ifmr->ifm_active = 717 IFM_ETHER | IFM_1000_T | IFM_FDX; 718 ifmr->ifm_mask = 0; 719 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 720 } else if (mii != NULL) { 721 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, 722 &mii->mii_media, SIOCGIFMEDIA); 723 if (err) 724 return (err); 725 } else { 726 return (ENXIO); 727 } 728 729 if (!arswitch_is_cpuport(sc, p->es_port) && 730 AR8X16_IS_SWITCH(sc, AR8327)) { 731 int led; 732 p->es_nleds = 3; 733 734 for (led = 0; led < p->es_nleds; led++) 735 { 736 int style; 737 uint32_t val; 738 739 /* Find the right style enum for our pattern */ 740 val = arswitch_readreg(dev, 741 ar8327_led_mapping[p->es_port-1][led].reg); 742 val = (val>>ar8327_led_mapping[p->es_port-1][led].shift)&0x03; 743 744 for (style = 0; style < ETHERSWITCH_PORT_LED_MAX; style++) 745 { 746 if (led_pattern_table[style] == val) break; 747 } 748 749 /* can't happen */ 750 if (style == ETHERSWITCH_PORT_LED_MAX) 751 style = ETHERSWITCH_PORT_LED_DEFAULT; 752 753 p->es_led[led] = style; 754 } 755 } else 756 { 757 p->es_nleds = 0; 758 } 759 760 return (0); 761 } 762 763 static int 764 ar8xxx_port_vlan_setup(struct arswitch_softc *sc, etherswitch_port_t *p) 765 { 766 uint32_t reg; 767 int err; 768 769 ARSWITCH_LOCK(sc); 770 771 /* Set the PVID. */ 772 if (p->es_pvid != 0) 773 sc->hal.arswitch_vlan_set_pvid(sc, p->es_port, p->es_pvid); 774 775 /* Mutually exclusive. */ 776 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG && 777 p->es_flags & ETHERSWITCH_PORT_STRIPTAG) { 778 ARSWITCH_UNLOCK(sc); 779 return (EINVAL); 780 } 781 782 reg = 0; 783 if (p->es_flags & ETHERSWITCH_PORT_DOUBLE_TAG) 784 reg |= AR8X16_PORT_CTRL_DOUBLE_TAG; 785 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG) 786 reg |= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_ADD << 787 AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT; 788 if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG) 789 reg |= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_STRIP << 790 AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT; 791 792 err = arswitch_modifyreg(sc->sc_dev, 793 AR8X16_REG_PORT_CTRL(p->es_port), 794 0x3 << AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT | 795 AR8X16_PORT_CTRL_DOUBLE_TAG, reg); 796 797 ARSWITCH_UNLOCK(sc); 798 return (err); 799 } 800 801 static int 802 arswitch_setport(device_t dev, etherswitch_port_t *p) 803 { 804 int err, i; 805 struct arswitch_softc *sc; 806 struct ifmedia *ifm; 807 struct mii_data *mii; 808 struct ifnet *ifp; 809 810 sc = device_get_softc(dev); 811 if (p->es_port < 0 || p->es_port > sc->info.es_nports) 812 return (ENXIO); 813 814 /* Port flags. */ 815 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 816 err = sc->hal.arswitch_port_vlan_setup(sc, p); 817 if (err) 818 return (err); 819 } 820 821 /* Do not allow media or led changes on CPU port. */ 822 if (arswitch_is_cpuport(sc, p->es_port)) 823 return (0); 824 825 if (AR8X16_IS_SWITCH(sc, AR8327)) 826 { 827 for (i = 0; i < 3; i++) 828 { 829 int err; 830 err = arswitch_setled(sc, p->es_port-1, i, p->es_led[i]); 831 if (err) 832 return (err); 833 } 834 } 835 836 mii = arswitch_miiforport(sc, p->es_port); 837 if (mii == NULL) 838 return (ENXIO); 839 840 ifp = arswitch_ifpforport(sc, p->es_port); 841 842 ifm = &mii->mii_media; 843 return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA)); 844 } 845 846 static int 847 arswitch_setled(struct arswitch_softc *sc, int phy, int led, int style) 848 { 849 int shift; 850 int err; 851 852 if (phy < 0 || phy > sc->numphys) 853 return EINVAL; 854 855 if (style < 0 || style > ETHERSWITCH_PORT_LED_MAX) 856 return (EINVAL); 857 858 ARSWITCH_LOCK(sc); 859 860 shift = ar8327_led_mapping[phy][led].shift; 861 err = (arswitch_modifyreg(sc->sc_dev, 862 ar8327_led_mapping[phy][led].reg, 863 0x03 << shift, led_pattern_table[style] << shift)); 864 ARSWITCH_UNLOCK(sc); 865 866 return (err); 867 } 868 869 static void 870 arswitch_statchg(device_t dev) 871 { 872 struct arswitch_softc *sc = device_get_softc(dev); 873 874 DPRINTF(sc, ARSWITCH_DBG_POLL, "%s\n", __func__); 875 } 876 877 static int 878 arswitch_ifmedia_upd(struct ifnet *ifp) 879 { 880 struct arswitch_softc *sc = ifp->if_softc; 881 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit); 882 883 if (mii == NULL) 884 return (ENXIO); 885 mii_mediachg(mii); 886 return (0); 887 } 888 889 static void 890 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 891 { 892 struct arswitch_softc *sc = ifp->if_softc; 893 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit); 894 895 DPRINTF(sc, ARSWITCH_DBG_POLL, "%s\n", __func__); 896 897 if (mii == NULL) 898 return; 899 mii_pollstat(mii); 900 ifmr->ifm_active = mii->mii_media_active; 901 ifmr->ifm_status = mii->mii_media_status; 902 } 903 904 static int 905 arswitch_getconf(device_t dev, etherswitch_conf_t *conf) 906 { 907 struct arswitch_softc *sc; 908 909 sc = device_get_softc(dev); 910 911 /* Return the VLAN mode. */ 912 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 913 conf->vlan_mode = sc->vlan_mode; 914 915 return (0); 916 } 917 918 static int 919 arswitch_setconf(device_t dev, etherswitch_conf_t *conf) 920 { 921 struct arswitch_softc *sc; 922 int err; 923 924 sc = device_get_softc(dev); 925 926 /* Set the VLAN mode. */ 927 if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) { 928 err = arswitch_set_vlan_mode(sc, conf->vlan_mode); 929 if (err != 0) 930 return (err); 931 } 932 933 return (0); 934 } 935 936 static int 937 arswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *e) 938 { 939 struct arswitch_softc *sc = device_get_softc(dev); 940 941 return (sc->hal.arswitch_vlan_getvgroup(sc, e)); 942 } 943 944 static int 945 arswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *e) 946 { 947 struct arswitch_softc *sc = device_get_softc(dev); 948 949 return (sc->hal.arswitch_vlan_setvgroup(sc, e)); 950 } 951 952 static int 953 arswitch_readphy(device_t dev, int phy, int reg) 954 { 955 struct arswitch_softc *sc = device_get_softc(dev); 956 957 return (sc->hal.arswitch_phy_read(dev, phy, reg)); 958 } 959 960 static int 961 arswitch_writephy(device_t dev, int phy, int reg, int val) 962 { 963 struct arswitch_softc *sc = device_get_softc(dev); 964 965 return (sc->hal.arswitch_phy_write(dev, phy, reg, val)); 966 } 967 968 static device_method_t arswitch_methods[] = { 969 /* Device interface */ 970 DEVMETHOD(device_probe, arswitch_probe), 971 DEVMETHOD(device_attach, arswitch_attach), 972 DEVMETHOD(device_detach, arswitch_detach), 973 974 /* bus interface */ 975 DEVMETHOD(bus_add_child, device_add_child_ordered), 976 977 /* MII interface */ 978 DEVMETHOD(miibus_readreg, arswitch_readphy), 979 DEVMETHOD(miibus_writereg, arswitch_writephy), 980 DEVMETHOD(miibus_statchg, arswitch_statchg), 981 982 /* MDIO interface */ 983 DEVMETHOD(mdio_readreg, arswitch_readphy), 984 DEVMETHOD(mdio_writereg, arswitch_writephy), 985 986 /* etherswitch interface */ 987 DEVMETHOD(etherswitch_lock, arswitch_lock), 988 DEVMETHOD(etherswitch_unlock, arswitch_unlock), 989 DEVMETHOD(etherswitch_getinfo, arswitch_getinfo), 990 DEVMETHOD(etherswitch_readreg, arswitch_readreg), 991 DEVMETHOD(etherswitch_writereg, arswitch_writereg), 992 DEVMETHOD(etherswitch_readphyreg, arswitch_readphy), 993 DEVMETHOD(etherswitch_writephyreg, arswitch_writephy), 994 DEVMETHOD(etherswitch_getport, arswitch_getport), 995 DEVMETHOD(etherswitch_setport, arswitch_setport), 996 DEVMETHOD(etherswitch_getvgroup, arswitch_getvgroup), 997 DEVMETHOD(etherswitch_setvgroup, arswitch_setvgroup), 998 DEVMETHOD(etherswitch_getconf, arswitch_getconf), 999 DEVMETHOD(etherswitch_setconf, arswitch_setconf), 1000 1001 DEVMETHOD_END 1002 }; 1003 1004 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods, 1005 sizeof(struct arswitch_softc)); 1006 static devclass_t arswitch_devclass; 1007 1008 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0); 1009 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0); 1010 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0); 1011 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0); 1012 MODULE_VERSION(arswitch, 1); 1013 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */ 1014 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */ 1015