1 /*- 2 * Copyright (c) 2011-2012 Stefan Bethke. 3 * Copyright (c) 2012 Adrian Chadd. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/bus.h> 32 #include <sys/errno.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/socket.h> 36 #include <sys/sockio.h> 37 #include <sys/sysctl.h> 38 #include <sys/systm.h> 39 40 #include <net/if.h> 41 #include <net/if_arp.h> 42 #include <net/ethernet.h> 43 #include <net/if_dl.h> 44 #include <net/if_media.h> 45 #include <net/if_types.h> 46 47 #include <machine/bus.h> 48 #include <dev/iicbus/iic.h> 49 #include <dev/iicbus/iiconf.h> 50 #include <dev/iicbus/iicbus.h> 51 #include <dev/mii/mii.h> 52 #include <dev/mii/miivar.h> 53 #include <dev/etherswitch/mdio.h> 54 55 #include <dev/etherswitch/etherswitch.h> 56 57 #include <dev/etherswitch/arswitch/arswitchreg.h> 58 #include <dev/etherswitch/arswitch/arswitchvar.h> 59 #include <dev/etherswitch/arswitch/arswitch_reg.h> 60 #include <dev/etherswitch/arswitch/arswitch_phy.h> 61 62 #include <dev/etherswitch/arswitch/arswitch_7240.h> 63 #include <dev/etherswitch/arswitch/arswitch_8216.h> 64 #include <dev/etherswitch/arswitch/arswitch_8226.h> 65 #include <dev/etherswitch/arswitch/arswitch_8316.h> 66 67 #include "mdio_if.h" 68 #include "miibus_if.h" 69 #include "etherswitch_if.h" 70 71 #if defined(DEBUG) 72 static SYSCTL_NODE(_debug, OID_AUTO, arswitch, CTLFLAG_RD, 0, "arswitch"); 73 #endif 74 75 static inline int arswitch_portforphy(int phy); 76 static void arswitch_tick(void *arg); 77 static int arswitch_ifmedia_upd(struct ifnet *); 78 static void arswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *); 79 80 static int 81 arswitch_probe(device_t dev) 82 { 83 struct arswitch_softc *sc; 84 uint32_t id; 85 char *chipname, desc[256]; 86 87 sc = device_get_softc(dev); 88 bzero(sc, sizeof(*sc)); 89 sc->page = -1; 90 91 /* AR7240 probe */ 92 if (ar7240_probe(dev) == 0) { 93 chipname = "AR7240"; 94 sc->sc_switchtype = AR8X16_SWITCH_AR7240; 95 id = 0; 96 goto done; 97 } 98 99 /* AR8xxx probe */ 100 id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL); 101 switch ((id & AR8X16_MASK_CTRL_VER_MASK) >> 102 AR8X16_MASK_CTRL_VER_SHIFT) { 103 case 1: 104 chipname = "AR8216"; 105 sc->sc_switchtype = AR8X16_SWITCH_AR8216; 106 break; 107 case 2: 108 chipname = "AR8226"; 109 sc->sc_switchtype = AR8X16_SWITCH_AR8226; 110 break; 111 case 16: 112 chipname = "AR8316"; 113 sc->sc_switchtype = AR8X16_SWITCH_AR8316; 114 break; 115 default: 116 chipname = NULL; 117 } 118 119 done: 120 DPRINTF(dev, "chipname=%s, rev=%02x\n", chipname, 121 id & AR8X16_MASK_CTRL_REV_MASK); 122 if (chipname != NULL) { 123 snprintf(desc, sizeof(desc), 124 "Atheros %s Ethernet Switch", 125 chipname); 126 device_set_desc_copy(dev, desc); 127 return (BUS_PROBE_DEFAULT); 128 } 129 return (ENXIO); 130 } 131 132 static int 133 arswitch_attach_phys(struct arswitch_softc *sc) 134 { 135 int phy, err = 0; 136 char name[IFNAMSIZ]; 137 138 /* PHYs need an interface, so we generate a dummy one */ 139 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev)); 140 for (phy = 0; phy < sc->numphys; phy++) { 141 sc->ifp[phy] = if_alloc(IFT_ETHER); 142 sc->ifp[phy]->if_softc = sc; 143 sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST | 144 IFF_DRV_RUNNING | IFF_SIMPLEX; 145 sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK); 146 bcopy(name, sc->ifname[phy], strlen(name)+1); 147 if_initname(sc->ifp[phy], sc->ifname[phy], 148 arswitch_portforphy(phy)); 149 err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy], 150 arswitch_ifmedia_upd, arswitch_ifmedia_sts, \ 151 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0); 152 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n", 153 device_get_nameunit(sc->miibus[phy]), 154 sc->ifp[phy]->if_xname); 155 if (err != 0) { 156 device_printf(sc->sc_dev, 157 "attaching PHY %d failed\n", 158 phy); 159 } 160 } 161 return (err); 162 } 163 164 static int 165 arswitch_attach(device_t dev) 166 { 167 struct arswitch_softc *sc; 168 int err = 0; 169 170 sc = device_get_softc(dev); 171 172 /* sc->sc_switchtype is already decided in arswitch_probe() */ 173 sc->sc_dev = dev; 174 mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF); 175 sc->page = -1; 176 strlcpy(sc->info.es_name, device_get_desc(dev), 177 sizeof(sc->info.es_name)); 178 179 /* 180 * Attach switch related functions 181 */ 182 if (AR8X16_IS_SWITCH(sc, AR7240)) 183 ar7240_attach(sc); 184 else if (AR8X16_IS_SWITCH(sc, AR8216)) 185 ar8216_attach(sc); 186 else if (AR8X16_IS_SWITCH(sc, AR8226)) 187 ar8226_attach(sc); 188 else if (AR8X16_IS_SWITCH(sc, AR8316)) 189 ar8316_attach(sc); 190 else 191 return (ENXIO); 192 193 /* 194 * XXX these two should be part of the switch attach function 195 */ 196 sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */ 197 sc->info.es_nvlangroups = 16; 198 199 /* XXX Defaults for externally connected AR8316 */ 200 sc->numphys = 4; 201 sc->phy4cpu = 1; 202 sc->is_rgmii = 1; 203 sc->is_gmii = 0; 204 205 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 206 "numphys", &sc->numphys); 207 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 208 "phy4cpu", &sc->phy4cpu); 209 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 210 "is_rgmii", &sc->is_rgmii); 211 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 212 "is_gmii", &sc->is_gmii); 213 214 /* 215 * This requires much more setup depending upon each chip, including: 216 * 217 * + Proper reinitialisation of the PHYs; 218 * + Initialising the VLAN table; 219 * + Initialising the port access table and CPU flood/broadcast 220 * configuration; 221 * + Other things I haven't yet thought of. 222 */ 223 #ifdef NOTYET 224 arswitch_writereg(dev, AR8X16_REG_MASK_CTRL, 225 AR8X16_MASK_CTRL_SOFT_RESET); 226 DELAY(1000); 227 if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) & 228 AR8X16_MASK_CTRL_SOFT_RESET) { 229 device_printf(dev, "unable to reset switch\n"); 230 return (ENXIO); 231 } 232 #endif 233 234 err = sc->hal.arswitch_hw_setup(sc); 235 if (err != 0) 236 return (err); 237 238 err = sc->hal.arswitch_hw_global_setup(sc); 239 if (err != 0) 240 return (err); 241 242 /* 243 * Attach the PHYs and complete the bus enumeration. 244 */ 245 err = arswitch_attach_phys(sc); 246 if (err != 0) 247 return (err); 248 249 bus_generic_probe(dev); 250 bus_enumerate_hinted_children(dev); 251 err = bus_generic_attach(dev); 252 if (err != 0) 253 return (err); 254 255 callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0); 256 257 ARSWITCH_LOCK(sc); 258 arswitch_tick(sc); 259 ARSWITCH_UNLOCK(sc); 260 261 return (err); 262 } 263 264 static int 265 arswitch_detach(device_t dev) 266 { 267 struct arswitch_softc *sc = device_get_softc(dev); 268 int i; 269 270 callout_drain(&sc->callout_tick); 271 272 for (i=0; i < sc->numphys; i++) { 273 if (sc->miibus[i] != NULL) 274 device_delete_child(dev, sc->miibus[i]); 275 if (sc->ifp[i] != NULL) 276 if_free(sc->ifp[i]); 277 free(sc->ifname[i], M_DEVBUF); 278 } 279 280 bus_generic_detach(dev); 281 mtx_destroy(&sc->sc_mtx); 282 283 return (0); 284 } 285 286 /* 287 * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to 288 * port 2, etc. 289 */ 290 static inline int 291 arswitch_portforphy(int phy) 292 { 293 return (phy+1); 294 } 295 296 static inline struct mii_data * 297 arswitch_miiforport(struct arswitch_softc *sc, int port) 298 { 299 int phy = port-1; 300 301 if (phy < 0 || phy >= sc->numphys) 302 return (NULL); 303 return (device_get_softc(sc->miibus[phy])); 304 } 305 306 static inline struct ifnet * 307 arswitch_ifpforport(struct arswitch_softc *sc, int port) 308 { 309 int phy = port-1; 310 311 if (phy < 0 || phy >= sc->numphys) 312 return (NULL); 313 return (sc->ifp[phy]); 314 } 315 316 /* 317 * Convert port status to ifmedia. 318 */ 319 static void 320 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 321 { 322 *media_active = IFM_ETHER; 323 *media_status = IFM_AVALID; 324 325 if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0) 326 *media_status |= IFM_ACTIVE; 327 else { 328 *media_active |= IFM_NONE; 329 return; 330 } 331 switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) { 332 case AR8X16_PORT_STS_SPEED_10: 333 *media_active |= IFM_10_T; 334 break; 335 case AR8X16_PORT_STS_SPEED_100: 336 *media_active |= IFM_100_TX; 337 break; 338 case AR8X16_PORT_STS_SPEED_1000: 339 *media_active |= IFM_1000_T; 340 break; 341 } 342 if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0) 343 *media_active |= IFM_FDX; 344 else 345 *media_active |= IFM_HDX; 346 if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0) 347 *media_active |= IFM_ETH_TXPAUSE; 348 if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0) 349 *media_active |= IFM_ETH_RXPAUSE; 350 } 351 352 /* 353 * Poll the status for all PHYs. We're using the switch port status because 354 * thats a lot quicker to read than talking to all the PHYs. Care must be 355 * taken that the resulting ifmedia_active is identical to what the PHY will 356 * compute, or gratuitous link status changes will occur whenever the PHYs 357 * update function is called. 358 */ 359 static void 360 arswitch_miipollstat(struct arswitch_softc *sc) 361 { 362 int i; 363 struct mii_data *mii; 364 struct mii_softc *miisc; 365 int portstatus; 366 367 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED); 368 369 for (i = 0; i < sc->numphys; i++) { 370 if (sc->miibus[i] == NULL) 371 continue; 372 mii = device_get_softc(sc->miibus[i]); 373 portstatus = arswitch_readreg(sc->sc_dev, 374 AR8X16_REG_PORT_STS(arswitch_portforphy(i))); 375 #if 0 376 DPRINTF(sc->sc_dev, "p[%d]=%b\n", 377 arge_portforphy(i), 378 portstatus, 379 "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7" 380 "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE"); 381 #endif 382 arswitch_update_ifmedia(portstatus, &mii->mii_media_status, 383 &mii->mii_media_active); 384 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 385 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != 386 miisc->mii_inst) 387 continue; 388 mii_phy_update(miisc, MII_POLLSTAT); 389 } 390 } 391 } 392 393 static void 394 arswitch_tick(void *arg) 395 { 396 struct arswitch_softc *sc = arg; 397 398 arswitch_miipollstat(sc); 399 callout_reset(&sc->callout_tick, hz, arswitch_tick, sc); 400 } 401 402 static void 403 arswitch_lock(device_t dev) 404 { 405 struct arswitch_softc *sc = device_get_softc(dev); 406 407 ARSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED); 408 ARSWITCH_LOCK(sc); 409 } 410 411 static void 412 arswitch_unlock(device_t dev) 413 { 414 struct arswitch_softc *sc = device_get_softc(dev); 415 416 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED); 417 ARSWITCH_UNLOCK(sc); 418 } 419 420 static etherswitch_info_t * 421 arswitch_getinfo(device_t dev) 422 { 423 struct arswitch_softc *sc = device_get_softc(dev); 424 425 return (&sc->info); 426 } 427 428 static int 429 arswitch_getport(device_t dev, etherswitch_port_t *p) 430 { 431 struct arswitch_softc *sc = device_get_softc(dev); 432 struct mii_data *mii; 433 struct ifmediareq *ifmr = &p->es_ifmr; 434 int err; 435 436 if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PORTS) 437 return (ENXIO); 438 p->es_vlangroup = 0; 439 440 mii = arswitch_miiforport(sc, p->es_port); 441 if (p->es_port == 0) { 442 /* fill in fixed values for CPU port */ 443 ifmr->ifm_count = 0; 444 ifmr->ifm_current = ifmr->ifm_active = 445 IFM_ETHER | IFM_1000_T | IFM_FDX; 446 ifmr->ifm_mask = 0; 447 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 448 } else if (mii != NULL) { 449 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, 450 &mii->mii_media, SIOCGIFMEDIA); 451 if (err) 452 return (err); 453 } else { 454 return (ENXIO); 455 } 456 return (0); 457 } 458 459 /* 460 * XXX doesn't yet work? 461 */ 462 static int 463 arswitch_setport(device_t dev, etherswitch_port_t *p) 464 { 465 int err; 466 struct arswitch_softc *sc; 467 struct ifmedia *ifm; 468 struct mii_data *mii; 469 struct ifnet *ifp; 470 471 /* 472 * XXX check the sc numphys, or the #define ? 473 */ 474 if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PHYS) 475 return (ENXIO); 476 477 sc = device_get_softc(dev); 478 479 /* 480 * XXX TODO: don't set the CPU port? 481 */ 482 483 mii = arswitch_miiforport(sc, p->es_port); 484 if (mii == NULL) 485 return (ENXIO); 486 487 ifp = arswitch_ifpforport(sc, p->es_port); 488 489 ifm = &mii->mii_media; 490 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA); 491 return (err); 492 } 493 494 static int 495 arswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 496 { 497 498 /* XXX not implemented yet */ 499 vg->es_vid = 0; 500 vg->es_member_ports = 0; 501 vg->es_untagged_ports = 0; 502 vg->es_fid = 0; 503 return (0); 504 } 505 506 static int 507 arswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 508 { 509 510 /* XXX not implemented yet */ 511 return (0); 512 } 513 514 static void 515 arswitch_statchg(device_t dev) 516 { 517 518 DPRINTF(dev, "%s\n", __func__); 519 } 520 521 static int 522 arswitch_ifmedia_upd(struct ifnet *ifp) 523 { 524 struct arswitch_softc *sc = ifp->if_softc; 525 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit); 526 527 if (mii == NULL) 528 return (ENXIO); 529 mii_mediachg(mii); 530 return (0); 531 } 532 533 static void 534 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 535 { 536 struct arswitch_softc *sc = ifp->if_softc; 537 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit); 538 539 DPRINTF(sc->sc_dev, "%s\n", __func__); 540 541 if (mii == NULL) 542 return; 543 mii_pollstat(mii); 544 ifmr->ifm_active = mii->mii_media_active; 545 ifmr->ifm_status = mii->mii_media_status; 546 } 547 548 static device_method_t arswitch_methods[] = { 549 /* Device interface */ 550 DEVMETHOD(device_probe, arswitch_probe), 551 DEVMETHOD(device_attach, arswitch_attach), 552 DEVMETHOD(device_detach, arswitch_detach), 553 554 /* bus interface */ 555 DEVMETHOD(bus_add_child, device_add_child_ordered), 556 557 /* MII interface */ 558 DEVMETHOD(miibus_readreg, arswitch_readphy), 559 DEVMETHOD(miibus_writereg, arswitch_writephy), 560 DEVMETHOD(miibus_statchg, arswitch_statchg), 561 562 /* MDIO interface */ 563 DEVMETHOD(mdio_readreg, arswitch_readphy), 564 DEVMETHOD(mdio_writereg, arswitch_writephy), 565 566 /* etherswitch interface */ 567 DEVMETHOD(etherswitch_lock, arswitch_lock), 568 DEVMETHOD(etherswitch_unlock, arswitch_unlock), 569 DEVMETHOD(etherswitch_getinfo, arswitch_getinfo), 570 DEVMETHOD(etherswitch_readreg, arswitch_readreg), 571 DEVMETHOD(etherswitch_writereg, arswitch_writereg), 572 DEVMETHOD(etherswitch_readphyreg, arswitch_readphy), 573 DEVMETHOD(etherswitch_writephyreg, arswitch_writephy), 574 DEVMETHOD(etherswitch_getport, arswitch_getport), 575 DEVMETHOD(etherswitch_setport, arswitch_setport), 576 DEVMETHOD(etherswitch_getvgroup, arswitch_getvgroup), 577 DEVMETHOD(etherswitch_setvgroup, arswitch_setvgroup), 578 579 DEVMETHOD_END 580 }; 581 582 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods, 583 sizeof(struct arswitch_softc)); 584 static devclass_t arswitch_devclass; 585 586 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0); 587 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0); 588 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0); 589 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0); 590 MODULE_VERSION(arswitch, 1); 591 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */ 592 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */ 593