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_generic_probe(dev); 261 bus_enumerate_hinted_children(dev); 262 err = bus_generic_attach(dev); 263 if (err != 0) 264 return (err); 265 266 if (sc->miipoll) { 267 callout_init(&sc->callout_tick, 0); 268 269 ip17x_tick(sc); 270 } 271 272 return (0); 273 } 274 275 static int 276 ip17x_detach(device_t dev) 277 { 278 struct ip17x_softc *sc; 279 int i, port; 280 281 sc = device_get_softc(dev); 282 if (sc->miipoll) 283 callout_drain(&sc->callout_tick); 284 285 for (i=0; i < MII_NPHY; i++) { 286 if (((1 << i) & sc->phymask) == 0) 287 continue; 288 port = sc->phyport[i]; 289 if (sc->miibus[port] != NULL) 290 device_delete_child(dev, (*sc->miibus[port])); 291 if (sc->ifp[port] != NULL) 292 if_free(sc->ifp[port]); 293 free(sc->miibus[port], M_IP17X); 294 } 295 296 free(sc->portphy, M_IP17X); 297 free(sc->miibus, M_IP17X); 298 free(sc->pvid, M_IP17X); 299 free(sc->ifp, M_IP17X); 300 301 /* Reset the switch. */ 302 sc->hal.ip17x_reset(sc); 303 304 bus_generic_detach(dev); 305 mtx_destroy(&sc->sc_mtx); 306 307 return (0); 308 } 309 310 static inline struct mii_data * 311 ip17x_miiforport(struct ip17x_softc *sc, int port) 312 { 313 314 if (port < 0 || port > sc->numports) 315 return (NULL); 316 return (device_get_softc(*sc->miibus[port])); 317 } 318 319 static inline if_t 320 ip17x_ifpforport(struct ip17x_softc *sc, int port) 321 { 322 323 if (port < 0 || port > sc->numports) 324 return (NULL); 325 return (sc->ifp[port]); 326 } 327 328 /* 329 * Poll the status for all PHYs. 330 */ 331 static void 332 ip17x_miipollstat(struct ip17x_softc *sc) 333 { 334 struct mii_softc *miisc; 335 struct mii_data *mii; 336 int i, port; 337 338 IP17X_LOCK_ASSERT(sc, MA_NOTOWNED); 339 340 for (i = 0; i < MII_NPHY; i++) { 341 if (((1 << i) & sc->phymask) == 0) 342 continue; 343 port = sc->phyport[i]; 344 if ((*sc->miibus[port]) == NULL) 345 continue; 346 mii = device_get_softc(*sc->miibus[port]); 347 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 348 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != 349 miisc->mii_inst) 350 continue; 351 ukphy_status(miisc); 352 mii_phy_update(miisc, MII_POLLSTAT); 353 } 354 } 355 } 356 357 static void 358 ip17x_tick(void *arg) 359 { 360 struct ip17x_softc *sc; 361 362 sc = arg; 363 ip17x_miipollstat(sc); 364 callout_reset(&sc->callout_tick, hz, ip17x_tick, sc); 365 } 366 367 static void 368 ip17x_lock(device_t dev) 369 { 370 struct ip17x_softc *sc; 371 372 sc = device_get_softc(dev); 373 IP17X_LOCK_ASSERT(sc, MA_NOTOWNED); 374 IP17X_LOCK(sc); 375 } 376 377 static void 378 ip17x_unlock(device_t dev) 379 { 380 struct ip17x_softc *sc; 381 382 sc = device_get_softc(dev); 383 IP17X_LOCK_ASSERT(sc, MA_OWNED); 384 IP17X_UNLOCK(sc); 385 } 386 387 static etherswitch_info_t * 388 ip17x_getinfo(device_t dev) 389 { 390 struct ip17x_softc *sc; 391 392 sc = device_get_softc(dev); 393 return (&sc->info); 394 } 395 396 static int 397 ip17x_getport(device_t dev, etherswitch_port_t *p) 398 { 399 struct ip17x_softc *sc; 400 struct ifmediareq *ifmr; 401 struct mii_data *mii; 402 int err, phy; 403 404 sc = device_get_softc(dev); 405 if (p->es_port < 0 || p->es_port >= sc->numports) 406 return (ENXIO); 407 408 phy = sc->portphy[p->es_port]; 409 410 /* Retrieve the PVID. */ 411 p->es_pvid = sc->pvid[phy]; 412 413 /* Port flags. */ 414 if (sc->addtag & (1 << phy)) 415 p->es_flags |= ETHERSWITCH_PORT_ADDTAG; 416 if (sc->striptag & (1 << phy)) 417 p->es_flags |= ETHERSWITCH_PORT_STRIPTAG; 418 419 ifmr = &p->es_ifmr; 420 421 /* No media settings ? */ 422 if (p->es_ifmr.ifm_count == 0) 423 return (0); 424 425 mii = ip17x_miiforport(sc, p->es_port); 426 if (mii == NULL) 427 return (ENXIO); 428 if (phy == sc->cpuport) { 429 /* fill in fixed values for CPU port */ 430 p->es_flags |= ETHERSWITCH_PORT_CPU; 431 ifmr->ifm_count = 0; 432 if (sc->media == 100) 433 ifmr->ifm_current = ifmr->ifm_active = 434 IFM_ETHER | IFM_100_TX | IFM_FDX; 435 else 436 ifmr->ifm_current = ifmr->ifm_active = 437 IFM_ETHER | IFM_1000_T | IFM_FDX; 438 ifmr->ifm_mask = 0; 439 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 440 } else { 441 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, 442 &mii->mii_media, SIOCGIFMEDIA); 443 if (err) 444 return (err); 445 } 446 return (0); 447 } 448 449 static int 450 ip17x_setport(device_t dev, etherswitch_port_t *p) 451 { 452 struct ip17x_softc *sc; 453 struct ifmedia *ifm; 454 if_t ifp; 455 struct mii_data *mii; 456 int phy; 457 458 sc = device_get_softc(dev); 459 if (p->es_port < 0 || p->es_port >= sc->numports) 460 return (ENXIO); 461 462 phy = sc->portphy[p->es_port]; 463 ifp = ip17x_ifpforport(sc, p->es_port); 464 mii = ip17x_miiforport(sc, p->es_port); 465 if (ifp == NULL || mii == NULL) 466 return (ENXIO); 467 468 /* Port flags. */ 469 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 470 471 /* Set the PVID. */ 472 if (p->es_pvid != 0) { 473 if (IP17X_IS_SWITCH(sc, IP175C) && 474 p->es_pvid > IP175C_LAST_VLAN) 475 return (ENXIO); 476 sc->pvid[phy] = p->es_pvid; 477 } 478 479 /* Mutually exclusive. */ 480 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG && 481 p->es_flags & ETHERSWITCH_PORT_STRIPTAG) 482 return (EINVAL); 483 484 /* Reset the settings for this port. */ 485 sc->addtag &= ~(1 << phy); 486 sc->striptag &= ~(1 << phy); 487 488 /* And then set it to the new value. */ 489 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG) 490 sc->addtag |= (1 << phy); 491 if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG) 492 sc->striptag |= (1 << phy); 493 } 494 495 /* Update the switch configuration. */ 496 if (sc->hal.ip17x_hw_setup(sc)) 497 return (ENXIO); 498 499 /* Do not allow media changes on CPU port. */ 500 if (phy == sc->cpuport) 501 return (0); 502 503 /* No media settings ? */ 504 if (p->es_ifmr.ifm_count == 0) 505 return (0); 506 507 ifm = &mii->mii_media; 508 return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA)); 509 } 510 511 static void 512 ip17x_statchg(device_t dev) 513 { 514 515 DPRINTF(dev, "%s\n", __func__); 516 } 517 518 static int 519 ip17x_ifmedia_upd(if_t ifp) 520 { 521 struct ip17x_softc *sc; 522 struct mii_data *mii; 523 524 sc = if_getsoftc(ifp); 525 DPRINTF(sc->sc_dev, "%s\n", __func__); 526 mii = ip17x_miiforport(sc, if_getdunit(ifp)); 527 if (mii == NULL) 528 return (ENXIO); 529 mii_mediachg(mii); 530 531 return (0); 532 } 533 534 static void 535 ip17x_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) 536 { 537 struct ip17x_softc *sc; 538 struct mii_data *mii; 539 540 sc = if_getsoftc(ifp); 541 DPRINTF(sc->sc_dev, "%s\n", __func__); 542 mii = ip17x_miiforport(sc, if_getdunit(ifp)); 543 if (mii == NULL) 544 return; 545 mii_pollstat(mii); 546 ifmr->ifm_active = mii->mii_media_active; 547 ifmr->ifm_status = mii->mii_media_status; 548 } 549 550 static int 551 ip17x_readreg(device_t dev, int addr) 552 { 553 struct ip17x_softc *sc __diagused; 554 555 sc = device_get_softc(dev); 556 IP17X_LOCK_ASSERT(sc, MA_OWNED); 557 558 /* Not supported. */ 559 return (0); 560 } 561 562 static int 563 ip17x_writereg(device_t dev, int addr, int value) 564 { 565 struct ip17x_softc *sc __diagused; 566 567 sc = device_get_softc(dev); 568 IP17X_LOCK_ASSERT(sc, MA_OWNED); 569 570 /* Not supported. */ 571 return (0); 572 } 573 574 static int 575 ip17x_getconf(device_t dev, etherswitch_conf_t *conf) 576 { 577 struct ip17x_softc *sc; 578 579 sc = device_get_softc(dev); 580 581 /* Return the VLAN mode. */ 582 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 583 conf->vlan_mode = sc->hal.ip17x_get_vlan_mode(sc); 584 585 return (0); 586 } 587 588 static int 589 ip17x_setconf(device_t dev, etherswitch_conf_t *conf) 590 { 591 struct ip17x_softc *sc; 592 593 sc = device_get_softc(dev); 594 595 /* Set the VLAN mode. */ 596 if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) 597 sc->hal.ip17x_set_vlan_mode(sc, conf->vlan_mode); 598 599 return (0); 600 } 601 602 static device_method_t ip17x_methods[] = { 603 /* Device interface */ 604 DEVMETHOD(device_identify, ip17x_identify), 605 DEVMETHOD(device_probe, ip17x_probe), 606 DEVMETHOD(device_attach, ip17x_attach), 607 DEVMETHOD(device_detach, ip17x_detach), 608 609 /* bus interface */ 610 DEVMETHOD(bus_add_child, device_add_child_ordered), 611 612 /* MII interface */ 613 DEVMETHOD(miibus_readreg, ip17x_readphy), 614 DEVMETHOD(miibus_writereg, ip17x_writephy), 615 DEVMETHOD(miibus_statchg, ip17x_statchg), 616 617 /* MDIO interface */ 618 DEVMETHOD(mdio_readreg, ip17x_readphy), 619 DEVMETHOD(mdio_writereg, ip17x_writephy), 620 621 /* etherswitch interface */ 622 DEVMETHOD(etherswitch_lock, ip17x_lock), 623 DEVMETHOD(etherswitch_unlock, ip17x_unlock), 624 DEVMETHOD(etherswitch_getinfo, ip17x_getinfo), 625 DEVMETHOD(etherswitch_readreg, ip17x_readreg), 626 DEVMETHOD(etherswitch_writereg, ip17x_writereg), 627 DEVMETHOD(etherswitch_readphyreg, ip17x_readphy), 628 DEVMETHOD(etherswitch_writephyreg, ip17x_writephy), 629 DEVMETHOD(etherswitch_getport, ip17x_getport), 630 DEVMETHOD(etherswitch_setport, ip17x_setport), 631 DEVMETHOD(etherswitch_getvgroup, ip17x_getvgroup), 632 DEVMETHOD(etherswitch_setvgroup, ip17x_setvgroup), 633 DEVMETHOD(etherswitch_getconf, ip17x_getconf), 634 DEVMETHOD(etherswitch_setconf, ip17x_setconf), 635 636 DEVMETHOD_END 637 }; 638 639 DEFINE_CLASS_0(ip17x, ip17x_driver, ip17x_methods, 640 sizeof(struct ip17x_softc)); 641 642 DRIVER_MODULE(ip17x, mdio, ip17x_driver, 0, 0); 643 DRIVER_MODULE(miibus, ip17x, miibus_driver, 0, 0); 644 DRIVER_MODULE(etherswitch, ip17x, etherswitch_driver, 0, 0); 645 MODULE_VERSION(ip17x, 1); 646 647 #ifdef FDT 648 MODULE_DEPEND(ip17x, mdio, 1, 1, 1); /* XXX which versions? */ 649 #else 650 DRIVER_MODULE(mdio, ip17x, mdio_driver, 0, 0); 651 MODULE_DEPEND(ip17x, miibus, 1, 1, 1); /* XXX which versions? */ 652 MODULE_DEPEND(ip17x, etherswitch, 1, 1, 1); /* XXX which versions? */ 653 #endif 654