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 void 81 arswitch_identify(driver_t *driver, device_t parent) 82 { 83 device_t child; 84 85 if (device_find_child(parent, driver->name, -1) == NULL) { 86 child = BUS_ADD_CHILD(parent, 0, driver->name, -1); 87 } 88 } 89 90 static int 91 arswitch_probe(device_t dev) 92 { 93 struct arswitch_softc *sc; 94 uint32_t id; 95 char *chipname, desc[256]; 96 97 sc = device_get_softc(dev); 98 bzero(sc, sizeof(*sc)); 99 sc->page = -1; 100 101 /* AR7240 probe */ 102 if (ar7240_probe(dev) == 0) { 103 chipname = "AR7240"; 104 sc->sc_switchtype = AR8X16_SWITCH_AR7240; 105 goto done; 106 } 107 108 /* AR8xxx probe */ 109 id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL); 110 switch ((id & AR8X16_MASK_CTRL_VER_MASK) >> 111 AR8X16_MASK_CTRL_VER_SHIFT) { 112 case 1: 113 chipname = "AR8216"; 114 sc->sc_switchtype = AR8X16_SWITCH_AR8216; 115 break; 116 case 2: 117 chipname = "AR8226"; 118 sc->sc_switchtype = AR8X16_SWITCH_AR8226; 119 break; 120 case 16: 121 chipname = "AR8316"; 122 sc->sc_switchtype = AR8X16_SWITCH_AR8316; 123 break; 124 default: 125 chipname = NULL; 126 } 127 128 done: 129 DPRINTF(dev, "chipname=%s, rev=%02x\n", chipname, 130 id & AR8X16_MASK_CTRL_REV_MASK); 131 if (chipname != NULL) { 132 snprintf(desc, sizeof(desc), 133 "Atheros %s Ethernet Switch", 134 chipname); 135 device_set_desc_copy(dev, desc); 136 return (BUS_PROBE_DEFAULT); 137 } 138 return (ENXIO); 139 } 140 141 static int 142 arswitch_attach_phys(struct arswitch_softc *sc) 143 { 144 int phy, err = 0; 145 char name[IFNAMSIZ]; 146 147 /* PHYs need an interface, so we generate a dummy one */ 148 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev)); 149 for (phy = 0; phy < sc->numphys; phy++) { 150 sc->ifp[phy] = if_alloc(IFT_ETHER); 151 sc->ifp[phy]->if_softc = sc; 152 sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST | 153 IFF_DRV_RUNNING | IFF_SIMPLEX; 154 sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK); 155 bcopy(name, sc->ifname[phy], strlen(name)+1); 156 if_initname(sc->ifp[phy], sc->ifname[phy], 157 arswitch_portforphy(phy)); 158 err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy], 159 arswitch_ifmedia_upd, arswitch_ifmedia_sts, \ 160 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0); 161 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n", 162 device_get_nameunit(sc->miibus[phy]), 163 sc->ifp[phy]->if_xname); 164 if (err != 0) { 165 device_printf(sc->sc_dev, 166 "attaching PHY %d failed\n", 167 phy); 168 } 169 } 170 return (err); 171 } 172 173 static int 174 arswitch_attach(device_t dev) 175 { 176 struct arswitch_softc *sc; 177 int err = 0; 178 179 sc = device_get_softc(dev); 180 181 /* sc->sc_switchtype is already decided in arswitch_probe() */ 182 sc->sc_dev = dev; 183 mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF); 184 sc->page = -1; 185 strlcpy(sc->info.es_name, device_get_desc(dev), 186 sizeof(sc->info.es_name)); 187 188 /* 189 * Attach switch related functions 190 */ 191 if (AR8X16_IS_SWITCH(sc, AR7240)) 192 ar7240_attach(sc); 193 else if (AR8X16_IS_SWITCH(sc, AR8216)) 194 ar8216_attach(sc); 195 else if (AR8X16_IS_SWITCH(sc, AR8226)) 196 ar8226_attach(sc); 197 else if (AR8X16_IS_SWITCH(sc, AR8316)) 198 ar8316_attach(sc); 199 else 200 return (ENXIO); 201 202 /* 203 * XXX these two should be part of the switch attach function 204 */ 205 sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */ 206 sc->info.es_nvlangroups = 16; 207 208 /* XXX Defaults for externally connected AR8316 */ 209 sc->numphys = 4; 210 sc->phy4cpu = 1; 211 sc->is_rgmii = 1; 212 sc->is_gmii = 0; 213 214 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 215 "numphys", &sc->numphys); 216 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 217 "phy4cpu", &sc->phy4cpu); 218 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 219 "is_rgmii", &sc->is_rgmii); 220 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 221 "is_gmii", &sc->is_gmii); 222 223 /* 224 * This requires much more setup depending upon each chip, including: 225 * 226 * + Proper reinitialisation of the PHYs; 227 * + Initialising the VLAN table; 228 * + Initialising the port access table and CPU flood/broadcast 229 * configuration; 230 * + Other things I haven't yet thought of. 231 */ 232 #ifdef NOTYET 233 arswitch_writereg(dev, AR8X16_REG_MASK_CTRL, 234 AR8X16_MASK_CTRL_SOFT_RESET); 235 DELAY(1000); 236 if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) & 237 AR8X16_MASK_CTRL_SOFT_RESET) { 238 device_printf(dev, "unable to reset switch\n"); 239 return (ENXIO); 240 } 241 #endif 242 243 err = sc->hal.arswitch_hw_setup(sc); 244 if (err != 0) 245 return (err); 246 247 err = sc->hal.arswitch_hw_global_setup(sc); 248 if (err != 0) 249 return (err); 250 251 /* 252 * Attach the PHYs and complete the bus enumeration. 253 */ 254 err = arswitch_attach_phys(sc); 255 if (err != 0) 256 return (err); 257 258 bus_generic_probe(dev); 259 bus_enumerate_hinted_children(dev); 260 err = bus_generic_attach(dev); 261 if (err != 0) 262 return (err); 263 264 callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0); 265 arswitch_tick(sc); 266 267 return (err); 268 } 269 270 static int 271 arswitch_detach(device_t dev) 272 { 273 struct arswitch_softc *sc = device_get_softc(dev); 274 int i; 275 276 callout_drain(&sc->callout_tick); 277 278 for (i=0; i < sc->numphys; i++) { 279 if (sc->miibus[i] != NULL) 280 device_delete_child(dev, sc->miibus[i]); 281 if (sc->ifp[i] != NULL) 282 if_free(sc->ifp[i]); 283 free(sc->ifname[i], M_DEVBUF); 284 } 285 286 bus_generic_detach(dev); 287 mtx_destroy(&sc->sc_mtx); 288 289 return (0); 290 } 291 292 /* 293 * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to 294 * port 2, etc. 295 */ 296 static inline int 297 arswitch_portforphy(int phy) 298 { 299 return (phy+1); 300 } 301 302 static inline struct mii_data * 303 arswitch_miiforport(struct arswitch_softc *sc, int port) 304 { 305 int phy = port-1; 306 307 if (phy < 0 || phy >= sc->numphys) 308 return (NULL); 309 return (device_get_softc(sc->miibus[phy])); 310 } 311 312 static inline struct ifnet * 313 arswitch_ifpforport(struct arswitch_softc *sc, int port) 314 { 315 int phy = port-1; 316 317 if (phy < 0 || phy >= sc->numphys) 318 return (NULL); 319 return (sc->ifp[phy]); 320 } 321 322 /* 323 * Convert port status to ifmedia. 324 */ 325 static void 326 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 327 { 328 *media_active = IFM_ETHER; 329 *media_status = IFM_AVALID; 330 331 if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0) 332 *media_status |= IFM_ACTIVE; 333 else { 334 *media_active |= IFM_NONE; 335 return; 336 } 337 switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) { 338 case AR8X16_PORT_STS_SPEED_10: 339 *media_active |= IFM_10_T; 340 break; 341 case AR8X16_PORT_STS_SPEED_100: 342 *media_active |= IFM_100_TX; 343 break; 344 case AR8X16_PORT_STS_SPEED_1000: 345 *media_active |= IFM_1000_T; 346 break; 347 } 348 if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0) 349 *media_active |= IFM_FDX; 350 else 351 *media_active |= IFM_HDX; 352 if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0) 353 *media_active |= IFM_ETH_TXPAUSE; 354 if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0) 355 *media_active |= IFM_ETH_RXPAUSE; 356 } 357 358 /* 359 * Poll the status for all PHYs. We're using the switch port status because 360 * thats a lot quicker to read than talking to all the PHYs. Care must be 361 * taken that the resulting ifmedia_active is identical to what the PHY will 362 * compute, or gratuitous link status changes will occur whenever the PHYs 363 * update function is called. 364 */ 365 static void 366 arswitch_miipollstat(struct arswitch_softc *sc) 367 { 368 int i; 369 struct mii_data *mii; 370 struct mii_softc *miisc; 371 int portstatus; 372 373 for (i = 0; i < sc->numphys; i++) { 374 if (sc->miibus[i] == NULL) 375 continue; 376 mii = device_get_softc(sc->miibus[i]); 377 portstatus = arswitch_readreg(sc->sc_dev, 378 AR8X16_REG_PORT_STS(arswitch_portforphy(i))); 379 #if 0 380 DPRINTF(sc->sc_dev, "p[%d]=%b\n", 381 arge_portforphy(i), 382 portstatus, 383 "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7" 384 "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE"); 385 #endif 386 arswitch_update_ifmedia(portstatus, &mii->mii_media_status, 387 &mii->mii_media_active); 388 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 389 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != 390 miisc->mii_inst) 391 continue; 392 mii_phy_update(miisc, MII_POLLSTAT); 393 } 394 } 395 } 396 397 static void 398 arswitch_tick(void *arg) 399 { 400 struct arswitch_softc *sc = arg; 401 402 arswitch_miipollstat(sc); 403 callout_reset(&sc->callout_tick, hz, arswitch_tick, sc); 404 } 405 406 static etherswitch_info_t * 407 arswitch_getinfo(device_t dev) 408 { 409 struct arswitch_softc *sc = device_get_softc(dev); 410 411 return (&sc->info); 412 } 413 414 static int 415 arswitch_getport(device_t dev, etherswitch_port_t *p) 416 { 417 struct arswitch_softc *sc = device_get_softc(dev); 418 struct mii_data *mii; 419 struct ifmediareq *ifmr = &p->es_ifmr; 420 int err; 421 422 if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PORTS) 423 return (ENXIO); 424 p->es_vlangroup = 0; 425 426 mii = arswitch_miiforport(sc, p->es_port); 427 if (p->es_port == 0) { 428 /* fill in fixed values for CPU port */ 429 ifmr->ifm_count = 0; 430 ifmr->ifm_current = ifmr->ifm_active = 431 IFM_ETHER | IFM_1000_T | IFM_FDX; 432 ifmr->ifm_mask = 0; 433 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 434 } else if (mii != NULL) { 435 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, 436 &mii->mii_media, SIOCGIFMEDIA); 437 if (err) 438 return (err); 439 } else { 440 return (ENXIO); 441 } 442 return (0); 443 } 444 445 /* 446 * XXX doesn't yet work? 447 */ 448 static int 449 arswitch_setport(device_t dev, etherswitch_port_t *p) 450 { 451 int err; 452 struct arswitch_softc *sc; 453 struct ifmedia *ifm; 454 struct mii_data *mii; 455 struct ifnet *ifp; 456 457 /* 458 * XXX check the sc numphys, or the #define ? 459 */ 460 if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PHYS) 461 return (ENXIO); 462 463 sc = device_get_softc(dev); 464 465 /* 466 * XXX TODO: don't set the CPU port? 467 */ 468 469 mii = arswitch_miiforport(sc, p->es_port); 470 if (mii == NULL) 471 return (ENXIO); 472 473 ifp = arswitch_ifpforport(sc, p->es_port); 474 475 ifm = &mii->mii_media; 476 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA); 477 return (err); 478 } 479 480 static int 481 arswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 482 { 483 484 /* XXX not implemented yet */ 485 vg->es_vid = 0; 486 vg->es_member_ports = 0; 487 vg->es_untagged_ports = 0; 488 vg->es_fid = 0; 489 return (0); 490 } 491 492 static int 493 arswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 494 { 495 496 /* XXX not implemented yet */ 497 return (0); 498 } 499 500 static void 501 arswitch_statchg(device_t dev) 502 { 503 504 DPRINTF(dev, "%s\n", __func__); 505 } 506 507 static int 508 arswitch_ifmedia_upd(struct ifnet *ifp) 509 { 510 struct arswitch_softc *sc = ifp->if_softc; 511 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit); 512 513 if (mii == NULL) 514 return (ENXIO); 515 mii_mediachg(mii); 516 return (0); 517 } 518 519 static void 520 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 521 { 522 struct arswitch_softc *sc = ifp->if_softc; 523 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit); 524 525 DPRINTF(sc->sc_dev, "%s\n", __func__); 526 527 if (mii == NULL) 528 return; 529 mii_pollstat(mii); 530 ifmr->ifm_active = mii->mii_media_active; 531 ifmr->ifm_status = mii->mii_media_status; 532 } 533 534 static device_method_t arswitch_methods[] = { 535 /* Device interface */ 536 DEVMETHOD(device_identify, arswitch_identify), 537 DEVMETHOD(device_probe, arswitch_probe), 538 DEVMETHOD(device_attach, arswitch_attach), 539 DEVMETHOD(device_detach, arswitch_detach), 540 541 /* bus interface */ 542 DEVMETHOD(bus_add_child, device_add_child_ordered), 543 544 /* MII interface */ 545 DEVMETHOD(miibus_readreg, arswitch_readphy), 546 DEVMETHOD(miibus_writereg, arswitch_writephy), 547 DEVMETHOD(miibus_statchg, arswitch_statchg), 548 549 /* MDIO interface */ 550 DEVMETHOD(mdio_readreg, arswitch_readphy), 551 DEVMETHOD(mdio_writereg, arswitch_writephy), 552 553 /* etherswitch interface */ 554 DEVMETHOD(etherswitch_getinfo, arswitch_getinfo), 555 DEVMETHOD(etherswitch_readreg, arswitch_readreg), 556 DEVMETHOD(etherswitch_writereg, arswitch_writereg), 557 DEVMETHOD(etherswitch_readphyreg, arswitch_readphy), 558 DEVMETHOD(etherswitch_writephyreg, arswitch_writephy), 559 DEVMETHOD(etherswitch_getport, arswitch_getport), 560 DEVMETHOD(etherswitch_setport, arswitch_setport), 561 DEVMETHOD(etherswitch_getvgroup, arswitch_getvgroup), 562 DEVMETHOD(etherswitch_setvgroup, arswitch_setvgroup), 563 564 DEVMETHOD_END 565 }; 566 567 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods, 568 sizeof(struct arswitch_softc)); 569 static devclass_t arswitch_devclass; 570 571 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0); 572 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0); 573 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0); 574 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0); 575 MODULE_VERSION(arswitch, 1); 576 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */ 577 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */ 578