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