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