1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 Luiz Otavio O Souza. 5 * Copyright (c) 2011-2012 Stefan Bethke. 6 * Copyright (c) 2012 Adrian Chadd. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include "opt_platform.h" 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/errno.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/socket.h> 42 #include <sys/sockio.h> 43 #include <sys/sysctl.h> 44 #include <sys/systm.h> 45 #include <sys/types.h> 46 47 #include <net/if.h> 48 #include <net/ethernet.h> 49 #include <net/if_media.h> 50 #include <net/if_types.h> 51 #include <net/if_var.h> 52 53 #include <machine/bus.h> 54 #include <dev/mii/mii.h> 55 #include <dev/mii/miivar.h> 56 #include <dev/mdio/mdio.h> 57 58 #include <dev/etherswitch/etherswitch.h> 59 #include <dev/etherswitch/ip17x/ip17x_phy.h> 60 #include <dev/etherswitch/ip17x/ip17x_reg.h> 61 #include <dev/etherswitch/ip17x/ip17x_var.h> 62 #include <dev/etherswitch/ip17x/ip17x_vlans.h> 63 #include <dev/etherswitch/ip17x/ip175c.h> 64 #include <dev/etherswitch/ip17x/ip175d.h> 65 66 #ifdef FDT 67 #include <dev/fdt/fdt_common.h> 68 #include <dev/ofw/ofw_bus.h> 69 #include <dev/ofw/ofw_bus_subr.h> 70 #endif 71 72 #include "mdio_if.h" 73 #include "miibus_if.h" 74 #include "etherswitch_if.h" 75 76 MALLOC_DECLARE(M_IP17X); 77 MALLOC_DEFINE(M_IP17X, "ip17x", "ip17x data structures"); 78 79 static void ip17x_tick(void *); 80 static int ip17x_ifmedia_upd(if_t); 81 static void ip17x_ifmedia_sts(if_t, struct ifmediareq *); 82 83 static void 84 ip17x_identify(driver_t *driver, device_t parent) 85 { 86 if (device_find_child(parent, "ip17x", -1) == NULL) 87 BUS_ADD_CHILD(parent, 0, "ip17x", DEVICE_UNIT_ANY); 88 } 89 90 static int 91 ip17x_probe(device_t dev) 92 { 93 struct ip17x_softc *sc; 94 uint32_t oui, model, phy_id1, phy_id2; 95 #ifdef FDT 96 phandle_t ip17x_node; 97 pcell_t cell; 98 99 ip17x_node = fdt_find_compatible(OF_finddevice("/"), 100 "icplus,ip17x", 0); 101 102 if (ip17x_node == 0) 103 return (ENXIO); 104 #endif 105 106 sc = device_get_softc(dev); 107 108 /* Read ID from PHY 0. */ 109 phy_id1 = MDIO_READREG(device_get_parent(dev), 0, MII_PHYIDR1); 110 phy_id2 = MDIO_READREG(device_get_parent(dev), 0, MII_PHYIDR2); 111 112 oui = MII_OUI(phy_id1, phy_id2); 113 model = MII_MODEL(phy_id2); 114 /* We only care about IC+ devices. */ 115 if (oui != IP17X_OUI) { 116 device_printf(dev, 117 "Unsupported IC+ switch. Unknown OUI: %#x\n", oui); 118 return (ENXIO); 119 } 120 121 switch (model) { 122 case IP17X_IP175A: 123 sc->sc_switchtype = IP17X_SWITCH_IP175A; 124 break; 125 case IP17X_IP175C: 126 sc->sc_switchtype = IP17X_SWITCH_IP175C; 127 break; 128 default: 129 device_printf(dev, "Unsupported IC+ switch model: %#x\n", 130 model); 131 return (ENXIO); 132 } 133 134 /* IP175D has a specific ID register. */ 135 model = MDIO_READREG(device_get_parent(dev), IP175D_ID_PHY, 136 IP175D_ID_REG); 137 if (model == 0x175d) 138 sc->sc_switchtype = IP17X_SWITCH_IP175D; 139 else { 140 /* IP178 has more PHYs. Try it. */ 141 model = MDIO_READREG(device_get_parent(dev), 5, MII_PHYIDR1); 142 if (phy_id1 == model) 143 sc->sc_switchtype = IP17X_SWITCH_IP178C; 144 } 145 146 sc->miipoll = 1; 147 #ifdef FDT 148 if ((OF_getencprop(ip17x_node, "mii-poll", 149 &cell, sizeof(cell))) > 0) 150 sc->miipoll = cell ? 1 : 0; 151 #else 152 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 153 "mii-poll", &sc->miipoll); 154 #endif 155 device_set_desc(dev, "IC+ IP17x switch driver"); 156 return (BUS_PROBE_DEFAULT); 157 } 158 159 static int 160 ip17x_attach_phys(struct ip17x_softc *sc) 161 { 162 int err, phy, port; 163 char name[IFNAMSIZ]; 164 165 port = err = 0; 166 167 /* PHYs need an interface, so we generate a dummy one */ 168 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev)); 169 for (phy = 0; phy < MII_NPHY; phy++) { 170 if (((1 << phy) & sc->phymask) == 0) 171 continue; 172 sc->phyport[phy] = port; 173 sc->portphy[port] = phy; 174 sc->ifp[port] = if_alloc(IFT_ETHER); 175 if_setsoftc(sc->ifp[port], sc); 176 if_setflags(sc->ifp[port], IFF_UP | IFF_BROADCAST | 177 IFF_DRV_RUNNING | IFF_SIMPLEX); 178 if_initname(sc->ifp[port], name, port); 179 sc->miibus[port] = malloc(sizeof(device_t), M_IP17X, 180 M_WAITOK | M_ZERO); 181 err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port], 182 ip17x_ifmedia_upd, ip17x_ifmedia_sts, \ 183 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0); 184 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n", 185 device_get_nameunit(*sc->miibus[port]), 186 if_name(sc->ifp[port])); 187 if (err != 0) { 188 device_printf(sc->sc_dev, 189 "attaching PHY %d failed\n", 190 phy); 191 break; 192 } 193 sc->info.es_nports = port + 1; 194 if (++port >= sc->numports) 195 break; 196 } 197 return (err); 198 } 199 200 static int 201 ip17x_attach(device_t dev) 202 { 203 struct ip17x_softc *sc; 204 int err; 205 206 sc = device_get_softc(dev); 207 208 sc->sc_dev = dev; 209 mtx_init(&sc->sc_mtx, "ip17x", NULL, MTX_DEF); 210 strlcpy(sc->info.es_name, device_get_desc(dev), 211 sizeof(sc->info.es_name)); 212 213 /* XXX Defaults */ 214 sc->phymask = 0x0f; 215 sc->media = 100; 216 217 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 218 "phymask", &sc->phymask); 219 220 /* Number of vlans supported by the switch. */ 221 sc->info.es_nvlangroups = IP17X_MAX_VLANS; 222 223 /* Attach the switch related functions. */ 224 if (IP17X_IS_SWITCH(sc, IP175C)) 225 ip175c_attach(sc); 226 else if (IP17X_IS_SWITCH(sc, IP175D)) 227 ip175d_attach(sc); 228 else 229 /* We don't have support to all the models yet :-/ */ 230 return (ENXIO); 231 232 /* Always attach the cpu port. */ 233 sc->phymask |= (1 << sc->cpuport); 234 235 sc->ifp = malloc(sizeof(if_t) * sc->numports, M_IP17X, 236 M_WAITOK | M_ZERO); 237 sc->pvid = malloc(sizeof(uint32_t) * sc->numports, M_IP17X, 238 M_WAITOK | M_ZERO); 239 sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_IP17X, 240 M_WAITOK | M_ZERO); 241 sc->portphy = malloc(sizeof(int) * sc->numports, M_IP17X, 242 M_WAITOK | M_ZERO); 243 244 /* Initialize the switch. */ 245 sc->hal.ip17x_reset(sc); 246 247 /* 248 * Attach the PHYs and complete the bus enumeration. 249 */ 250 err = ip17x_attach_phys(sc); 251 if (err != 0) 252 return (err); 253 254 /* 255 * Set the switch to port based vlans or disabled (if not supported 256 * on this model). 257 */ 258 sc->hal.ip17x_set_vlan_mode(sc, ETHERSWITCH_VLAN_PORT); 259 260 bus_identify_children(dev); 261 bus_enumerate_hinted_children(dev); 262 bus_attach_children(dev); 263 264 if (sc->miipoll) { 265 callout_init(&sc->callout_tick, 0); 266 267 ip17x_tick(sc); 268 } 269 270 return (0); 271 } 272 273 static int 274 ip17x_detach(device_t dev) 275 { 276 struct ip17x_softc *sc; 277 int i, port; 278 279 sc = device_get_softc(dev); 280 if (sc->miipoll) 281 callout_drain(&sc->callout_tick); 282 283 for (i=0; i < MII_NPHY; i++) { 284 if (((1 << i) & sc->phymask) == 0) 285 continue; 286 port = sc->phyport[i]; 287 if (sc->miibus[port] != NULL) 288 device_delete_child(dev, (*sc->miibus[port])); 289 if (sc->ifp[port] != NULL) 290 if_free(sc->ifp[port]); 291 free(sc->miibus[port], M_IP17X); 292 } 293 294 free(sc->portphy, M_IP17X); 295 free(sc->miibus, M_IP17X); 296 free(sc->pvid, M_IP17X); 297 free(sc->ifp, M_IP17X); 298 299 /* Reset the switch. */ 300 sc->hal.ip17x_reset(sc); 301 302 bus_generic_detach(dev); 303 mtx_destroy(&sc->sc_mtx); 304 305 return (0); 306 } 307 308 static inline struct mii_data * 309 ip17x_miiforport(struct ip17x_softc *sc, int port) 310 { 311 312 if (port < 0 || port > sc->numports) 313 return (NULL); 314 return (device_get_softc(*sc->miibus[port])); 315 } 316 317 static inline if_t 318 ip17x_ifpforport(struct ip17x_softc *sc, int port) 319 { 320 321 if (port < 0 || port > sc->numports) 322 return (NULL); 323 return (sc->ifp[port]); 324 } 325 326 /* 327 * Poll the status for all PHYs. 328 */ 329 static void 330 ip17x_miipollstat(struct ip17x_softc *sc) 331 { 332 struct mii_softc *miisc; 333 struct mii_data *mii; 334 int i, port; 335 336 IP17X_LOCK_ASSERT(sc, MA_NOTOWNED); 337 338 for (i = 0; i < MII_NPHY; i++) { 339 if (((1 << i) & sc->phymask) == 0) 340 continue; 341 port = sc->phyport[i]; 342 if ((*sc->miibus[port]) == NULL) 343 continue; 344 mii = device_get_softc(*sc->miibus[port]); 345 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 346 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != 347 miisc->mii_inst) 348 continue; 349 ukphy_status(miisc); 350 mii_phy_update(miisc, MII_POLLSTAT); 351 } 352 } 353 } 354 355 static void 356 ip17x_tick(void *arg) 357 { 358 struct ip17x_softc *sc; 359 360 sc = arg; 361 ip17x_miipollstat(sc); 362 callout_reset(&sc->callout_tick, hz, ip17x_tick, sc); 363 } 364 365 static void 366 ip17x_lock(device_t dev) 367 { 368 struct ip17x_softc *sc; 369 370 sc = device_get_softc(dev); 371 IP17X_LOCK_ASSERT(sc, MA_NOTOWNED); 372 IP17X_LOCK(sc); 373 } 374 375 static void 376 ip17x_unlock(device_t dev) 377 { 378 struct ip17x_softc *sc; 379 380 sc = device_get_softc(dev); 381 IP17X_LOCK_ASSERT(sc, MA_OWNED); 382 IP17X_UNLOCK(sc); 383 } 384 385 static etherswitch_info_t * 386 ip17x_getinfo(device_t dev) 387 { 388 struct ip17x_softc *sc; 389 390 sc = device_get_softc(dev); 391 return (&sc->info); 392 } 393 394 static int 395 ip17x_getport(device_t dev, etherswitch_port_t *p) 396 { 397 struct ip17x_softc *sc; 398 struct ifmediareq *ifmr; 399 struct mii_data *mii; 400 int err, phy; 401 402 sc = device_get_softc(dev); 403 if (p->es_port < 0 || p->es_port >= sc->numports) 404 return (ENXIO); 405 406 phy = sc->portphy[p->es_port]; 407 408 /* Retrieve the PVID. */ 409 p->es_pvid = sc->pvid[phy]; 410 411 /* Port flags. */ 412 if (sc->addtag & (1 << phy)) 413 p->es_flags |= ETHERSWITCH_PORT_ADDTAG; 414 if (sc->striptag & (1 << phy)) 415 p->es_flags |= ETHERSWITCH_PORT_STRIPTAG; 416 417 ifmr = &p->es_ifmr; 418 419 /* No media settings ? */ 420 if (p->es_ifmr.ifm_count == 0) 421 return (0); 422 423 mii = ip17x_miiforport(sc, p->es_port); 424 if (mii == NULL) 425 return (ENXIO); 426 if (phy == sc->cpuport) { 427 /* fill in fixed values for CPU port */ 428 p->es_flags |= ETHERSWITCH_PORT_CPU; 429 ifmr->ifm_count = 0; 430 if (sc->media == 100) 431 ifmr->ifm_current = ifmr->ifm_active = 432 IFM_ETHER | IFM_100_TX | IFM_FDX; 433 else 434 ifmr->ifm_current = ifmr->ifm_active = 435 IFM_ETHER | IFM_1000_T | IFM_FDX; 436 ifmr->ifm_mask = 0; 437 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 438 } else { 439 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, 440 &mii->mii_media, SIOCGIFMEDIA); 441 if (err) 442 return (err); 443 } 444 return (0); 445 } 446 447 static int 448 ip17x_setport(device_t dev, etherswitch_port_t *p) 449 { 450 struct ip17x_softc *sc; 451 struct ifmedia *ifm; 452 if_t ifp; 453 struct mii_data *mii; 454 int phy; 455 456 sc = device_get_softc(dev); 457 if (p->es_port < 0 || p->es_port >= sc->numports) 458 return (ENXIO); 459 460 phy = sc->portphy[p->es_port]; 461 ifp = ip17x_ifpforport(sc, p->es_port); 462 mii = ip17x_miiforport(sc, p->es_port); 463 if (ifp == NULL || mii == NULL) 464 return (ENXIO); 465 466 /* Port flags. */ 467 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 468 469 /* Set the PVID. */ 470 if (p->es_pvid != 0) { 471 if (IP17X_IS_SWITCH(sc, IP175C) && 472 p->es_pvid > IP175C_LAST_VLAN) 473 return (ENXIO); 474 sc->pvid[phy] = p->es_pvid; 475 } 476 477 /* Mutually exclusive. */ 478 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG && 479 p->es_flags & ETHERSWITCH_PORT_STRIPTAG) 480 return (EINVAL); 481 482 /* Reset the settings for this port. */ 483 sc->addtag &= ~(1 << phy); 484 sc->striptag &= ~(1 << phy); 485 486 /* And then set it to the new value. */ 487 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG) 488 sc->addtag |= (1 << phy); 489 if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG) 490 sc->striptag |= (1 << phy); 491 } 492 493 /* Update the switch configuration. */ 494 if (sc->hal.ip17x_hw_setup(sc)) 495 return (ENXIO); 496 497 /* Do not allow media changes on CPU port. */ 498 if (phy == sc->cpuport) 499 return (0); 500 501 /* No media settings ? */ 502 if (p->es_ifmr.ifm_count == 0) 503 return (0); 504 505 ifm = &mii->mii_media; 506 return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA)); 507 } 508 509 static void 510 ip17x_statchg(device_t dev) 511 { 512 513 DPRINTF(dev, "%s\n", __func__); 514 } 515 516 static int 517 ip17x_ifmedia_upd(if_t ifp) 518 { 519 struct ip17x_softc *sc; 520 struct mii_data *mii; 521 522 sc = if_getsoftc(ifp); 523 DPRINTF(sc->sc_dev, "%s\n", __func__); 524 mii = ip17x_miiforport(sc, if_getdunit(ifp)); 525 if (mii == NULL) 526 return (ENXIO); 527 mii_mediachg(mii); 528 529 return (0); 530 } 531 532 static void 533 ip17x_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) 534 { 535 struct ip17x_softc *sc; 536 struct mii_data *mii; 537 538 sc = if_getsoftc(ifp); 539 DPRINTF(sc->sc_dev, "%s\n", __func__); 540 mii = ip17x_miiforport(sc, if_getdunit(ifp)); 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 int 549 ip17x_readreg(device_t dev, int addr) 550 { 551 struct ip17x_softc *sc __diagused; 552 553 sc = device_get_softc(dev); 554 IP17X_LOCK_ASSERT(sc, MA_OWNED); 555 556 /* Not supported. */ 557 return (0); 558 } 559 560 static int 561 ip17x_writereg(device_t dev, int addr, int value) 562 { 563 struct ip17x_softc *sc __diagused; 564 565 sc = device_get_softc(dev); 566 IP17X_LOCK_ASSERT(sc, MA_OWNED); 567 568 /* Not supported. */ 569 return (0); 570 } 571 572 static int 573 ip17x_getconf(device_t dev, etherswitch_conf_t *conf) 574 { 575 struct ip17x_softc *sc; 576 577 sc = device_get_softc(dev); 578 579 /* Return the VLAN mode. */ 580 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 581 conf->vlan_mode = sc->hal.ip17x_get_vlan_mode(sc); 582 583 return (0); 584 } 585 586 static int 587 ip17x_setconf(device_t dev, etherswitch_conf_t *conf) 588 { 589 struct ip17x_softc *sc; 590 591 sc = device_get_softc(dev); 592 593 /* Set the VLAN mode. */ 594 if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) 595 sc->hal.ip17x_set_vlan_mode(sc, conf->vlan_mode); 596 597 return (0); 598 } 599 600 static device_method_t ip17x_methods[] = { 601 /* Device interface */ 602 DEVMETHOD(device_identify, ip17x_identify), 603 DEVMETHOD(device_probe, ip17x_probe), 604 DEVMETHOD(device_attach, ip17x_attach), 605 DEVMETHOD(device_detach, ip17x_detach), 606 607 /* bus interface */ 608 DEVMETHOD(bus_add_child, device_add_child_ordered), 609 610 /* MII interface */ 611 DEVMETHOD(miibus_readreg, ip17x_readphy), 612 DEVMETHOD(miibus_writereg, ip17x_writephy), 613 DEVMETHOD(miibus_statchg, ip17x_statchg), 614 615 /* MDIO interface */ 616 DEVMETHOD(mdio_readreg, ip17x_readphy), 617 DEVMETHOD(mdio_writereg, ip17x_writephy), 618 619 /* etherswitch interface */ 620 DEVMETHOD(etherswitch_lock, ip17x_lock), 621 DEVMETHOD(etherswitch_unlock, ip17x_unlock), 622 DEVMETHOD(etherswitch_getinfo, ip17x_getinfo), 623 DEVMETHOD(etherswitch_readreg, ip17x_readreg), 624 DEVMETHOD(etherswitch_writereg, ip17x_writereg), 625 DEVMETHOD(etherswitch_readphyreg, ip17x_readphy), 626 DEVMETHOD(etherswitch_writephyreg, ip17x_writephy), 627 DEVMETHOD(etherswitch_getport, ip17x_getport), 628 DEVMETHOD(etherswitch_setport, ip17x_setport), 629 DEVMETHOD(etherswitch_getvgroup, ip17x_getvgroup), 630 DEVMETHOD(etherswitch_setvgroup, ip17x_setvgroup), 631 DEVMETHOD(etherswitch_getconf, ip17x_getconf), 632 DEVMETHOD(etherswitch_setconf, ip17x_setconf), 633 634 DEVMETHOD_END 635 }; 636 637 DEFINE_CLASS_0(ip17x, ip17x_driver, ip17x_methods, 638 sizeof(struct ip17x_softc)); 639 640 DRIVER_MODULE(ip17x, mdio, ip17x_driver, 0, 0); 641 DRIVER_MODULE(miibus, ip17x, miibus_driver, 0, 0); 642 DRIVER_MODULE(etherswitch, ip17x, etherswitch_driver, 0, 0); 643 MODULE_VERSION(ip17x, 1); 644 645 #ifdef FDT 646 MODULE_DEPEND(ip17x, mdio, 1, 1, 1); /* XXX which versions? */ 647 #else 648 DRIVER_MODULE(mdio, ip17x, mdio_driver, 0, 0); 649 MODULE_DEPEND(ip17x, miibus, 1, 1, 1); /* XXX which versions? */ 650 MODULE_DEPEND(ip17x, etherswitch, 1, 1, 1); /* XXX which versions? */ 651 #endif 652