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