1 /*- 2 * Copyright (c) 2016-2017 Hiroki Mori 3 * Copyright (c) 2013 Luiz Otavio O Souza. 4 * Copyright (c) 2011-2012 Stefan Bethke. 5 * Copyright (c) 2012 Adrian Chadd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This code is Marvell 88E6060 ethernet switch support code on etherswitch 32 * framework. 33 * 88E6060 support is only port vlan support. Not support ingress/egress 34 * trailer. 35 * 88E6065 support is port and dot1q vlan. Also group base tag support. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/bus.h> 40 #include <sys/errno.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/module.h> 45 #include <sys/mutex.h> 46 #include <sys/socket.h> 47 #include <sys/sockio.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 51 #include <net/if.h> 52 #include <net/if_var.h> 53 #include <net/ethernet.h> 54 #include <net/if_media.h> 55 #include <net/if_types.h> 56 57 #include <machine/bus.h> 58 #include <dev/mii/mii.h> 59 #include <dev/mii/miivar.h> 60 #include <dev/mdio/mdio.h> 61 62 #include <dev/etherswitch/etherswitch.h> 63 64 #include "mdio_if.h" 65 #include "miibus_if.h" 66 #include "etherswitch_if.h" 67 68 #define CORE_REGISTER 0x8 69 #define SWITCH_ID 3 70 71 #define PORT_CONTROL 4 72 #define ENGRESSFSHIFT 2 73 #define ENGRESSFMASK 3 74 #define ENGRESSTAGSHIFT 12 75 #define ENGRESSTAGMASK 3 76 77 #define PORT_VLAN_MAP 6 78 #define FORCEMAPSHIFT 8 79 #define FORCEMAPMASK 1 80 81 #define PORT_DEFVLAN 7 82 #define DEFVIDMASK 0xfff 83 #define DEFPRIMASK 7 84 85 #define PORT_CONTROL2 8 86 #define DOT1QMODESHIFT 10 87 #define DOT1QMODEMASK 3 88 #define DOT1QNONE 0 89 #define DOT1QFALLBACK 1 90 #define DOT1QCHECK 2 91 #define DOT1QSECURE 3 92 93 #define GLOBAL_REGISTER 0xf 94 95 #define VTU_OPERATION 5 96 #define VTU_VID_REG 6 97 #define VTU_DATA1_REG 7 98 #define VTU_DATA2_REG 8 99 #define VTU_DATA3_REG 9 100 #define VTU_BUSY 0x8000 101 #define VTU_FLASH 1 102 #define VTU_LOAD_PURGE 3 103 #define VTU_GET_NEXT 4 104 #define VTU_VIOLATION 7 105 106 MALLOC_DECLARE(M_E6060SW); 107 MALLOC_DEFINE(M_E6060SW, "e6060sw", "e6060sw data structures"); 108 109 struct e6060sw_softc { 110 struct mtx sc_mtx; /* serialize access to softc */ 111 device_t sc_dev; 112 int vlan_mode; 113 int media; /* cpu port media */ 114 int cpuport; /* which PHY is connected to the CPU */ 115 int phymask; /* PHYs we manage */ 116 int numports; /* number of ports */ 117 int ifpport[MII_NPHY]; 118 int *portphy; 119 char **ifname; 120 device_t **miibus; 121 if_t *ifp; 122 struct callout callout_tick; 123 etherswitch_info_t info; 124 int smi_offset; 125 int sw_model; 126 }; 127 128 /* Switch Identifier DeviceID */ 129 130 #define E6060 0x60 131 #define E6063 0x63 132 #define E6065 0x65 133 134 #define E6060SW_LOCK(_sc) \ 135 mtx_lock(&(_sc)->sc_mtx) 136 #define E6060SW_UNLOCK(_sc) \ 137 mtx_unlock(&(_sc)->sc_mtx) 138 #define E6060SW_LOCK_ASSERT(_sc, _what) \ 139 mtx_assert(&(_sc)->sc_mtx, (_what)) 140 #define E6060SW_TRYLOCK(_sc) \ 141 mtx_trylock(&(_sc)->sc_mtx) 142 143 #if defined(DEBUG) 144 #define DPRINTF(dev, args...) device_printf(dev, args) 145 #else 146 #define DPRINTF(dev, args...) 147 #endif 148 149 static inline int e6060sw_portforphy(struct e6060sw_softc *, int); 150 static void e6060sw_tick(void *); 151 static int e6060sw_ifmedia_upd(if_t); 152 static void e6060sw_ifmedia_sts(if_t, struct ifmediareq *); 153 154 static void e6060sw_setup(device_t dev); 155 static int e6060sw_read_vtu(device_t dev, int num, int *data1, int *data2); 156 static void e6060sw_set_vtu(device_t dev, int num, int data1, int data2); 157 158 static int 159 e6060sw_probe(device_t dev) 160 { 161 int data; 162 struct e6060sw_softc *sc; 163 int devid, i; 164 char *devname; 165 166 sc = device_get_softc(dev); 167 bzero(sc, sizeof(*sc)); 168 169 devid = 0; 170 for (i = 0; i < 2; ++i) { 171 data = MDIO_READREG(device_get_parent(dev), 172 CORE_REGISTER + i * 0x10, SWITCH_ID); 173 if (bootverbose) 174 device_printf(dev,"Switch Identifier Register %x\n", 175 data); 176 177 devid = data >> 4; 178 if (devid == E6060 || 179 devid == E6063 || devid == E6065) { 180 sc->sw_model = devid; 181 sc->smi_offset = i * 0x10; 182 break; 183 } 184 } 185 186 if (devid == E6060) 187 devname = "88E6060"; 188 else if (devid == E6063) 189 devname = "88E6063"; 190 else if (devid == E6065) 191 devname = "88E6065"; 192 else 193 return (ENXIO); 194 195 device_set_descf(dev, "Marvell %s MDIO switch driver at 0x%02x", 196 devname, sc->smi_offset); 197 198 return (BUS_PROBE_DEFAULT); 199 } 200 201 static int 202 e6060sw_attach_phys(struct e6060sw_softc *sc) 203 { 204 int phy, port, err; 205 char name[IFNAMSIZ]; 206 207 port = 0; 208 err = 0; 209 /* PHYs need an interface, so we generate a dummy one */ 210 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev)); 211 for (phy = 0; phy < sc->numports; phy++) { 212 if (((1 << phy) & sc->phymask) == 0) 213 continue; 214 sc->ifpport[phy] = port; 215 sc->portphy[port] = phy; 216 sc->ifp[port] = if_alloc(IFT_ETHER); 217 sc->ifp[port]->if_softc = sc; 218 sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST | 219 IFF_DRV_RUNNING | IFF_SIMPLEX; 220 if_initname(sc->ifp[port], name, port); 221 sc->miibus[port] = malloc(sizeof(device_t), M_E6060SW, 222 M_WAITOK | M_ZERO); 223 err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port], 224 e6060sw_ifmedia_upd, e6060sw_ifmedia_sts, \ 225 BMSR_DEFCAPMASK, phy + sc->smi_offset, MII_OFFSET_ANY, 0); 226 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n", 227 device_get_nameunit(*sc->miibus[port]), 228 sc->ifp[port]->if_xname); 229 if (err != 0) { 230 device_printf(sc->sc_dev, 231 "attaching PHY %d failed\n", 232 phy); 233 break; 234 } 235 ++port; 236 } 237 sc->info.es_nports = port; 238 if (sc->cpuport != -1) { 239 /* assume cpuport is last one */ 240 sc->ifpport[sc->cpuport] = port; 241 sc->portphy[port] = sc->cpuport; 242 ++sc->info.es_nports; 243 } 244 return (err); 245 } 246 247 static int 248 e6060sw_attach(device_t dev) 249 { 250 struct e6060sw_softc *sc; 251 int err; 252 253 sc = device_get_softc(dev); 254 err = 0; 255 256 sc->sc_dev = dev; 257 mtx_init(&sc->sc_mtx, "e6060sw", NULL, MTX_DEF); 258 strlcpy(sc->info.es_name, device_get_desc(dev), 259 sizeof(sc->info.es_name)); 260 261 /* XXX Defaults */ 262 if (sc->sw_model == E6063) { 263 sc->numports = 3; 264 sc->phymask = 0x07; 265 sc->cpuport = 2; 266 } else { 267 sc->numports = 6; 268 sc->phymask = 0x1f; 269 sc->cpuport = 5; 270 } 271 sc->media = 100; 272 273 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 274 "numports", &sc->numports); 275 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 276 "phymask", &sc->phymask); 277 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 278 "cpuport", &sc->cpuport); 279 (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 280 "media", &sc->media); 281 282 if (sc->sw_model == E6060) { 283 sc->info.es_nvlangroups = sc->numports; 284 sc->info.es_vlan_caps = ETHERSWITCH_VLAN_PORT; 285 } else { 286 sc->info.es_nvlangroups = 64; 287 sc->info.es_vlan_caps = ETHERSWITCH_VLAN_PORT | 288 ETHERSWITCH_VLAN_DOT1Q; 289 } 290 291 e6060sw_setup(dev); 292 293 sc->ifp = malloc(sizeof(if_t) * sc->numports, M_E6060SW, 294 M_WAITOK | M_ZERO); 295 sc->ifname = malloc(sizeof(char *) * sc->numports, M_E6060SW, 296 M_WAITOK | M_ZERO); 297 sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_E6060SW, 298 M_WAITOK | M_ZERO); 299 sc->portphy = malloc(sizeof(int) * sc->numports, M_E6060SW, 300 M_WAITOK | M_ZERO); 301 302 /* 303 * Attach the PHYs and complete the bus enumeration. 304 */ 305 err = e6060sw_attach_phys(sc); 306 if (err != 0) 307 return (err); 308 309 bus_identify_children(dev); 310 bus_enumerate_hinted_children(dev); 311 bus_attach_children(dev); 312 313 callout_init(&sc->callout_tick, 0); 314 315 e6060sw_tick(sc); 316 317 return (err); 318 } 319 320 static int 321 e6060sw_detach(device_t dev) 322 { 323 struct e6060sw_softc *sc; 324 int error, i, port; 325 326 sc = device_get_softc(dev); 327 328 error = bus_generic_detach(dev); 329 if (error != 0) 330 return (error); 331 332 callout_drain(&sc->callout_tick); 333 334 for (i = 0; i < MII_NPHY; i++) { 335 if (((1 << i) & sc->phymask) == 0) 336 continue; 337 port = e6060sw_portforphy(sc, i); 338 if (sc->ifp[port] != NULL) 339 if_free(sc->ifp[port]); 340 free(sc->ifname[port], M_E6060SW); 341 free(sc->miibus[port], M_E6060SW); 342 } 343 344 free(sc->portphy, M_E6060SW); 345 free(sc->miibus, M_E6060SW); 346 free(sc->ifname, M_E6060SW); 347 free(sc->ifp, M_E6060SW); 348 349 mtx_destroy(&sc->sc_mtx); 350 351 return (0); 352 } 353 354 /* 355 * Convert PHY number to port number. 356 */ 357 static inline int 358 e6060sw_portforphy(struct e6060sw_softc *sc, int phy) 359 { 360 361 return (sc->ifpport[phy]); 362 } 363 364 static inline struct mii_data * 365 e6060sw_miiforport(struct e6060sw_softc *sc, int port) 366 { 367 368 if (port < 0 || port > sc->numports) 369 return (NULL); 370 if (port == sc->cpuport) 371 return (NULL); 372 return (device_get_softc(*sc->miibus[port])); 373 } 374 375 static inline if_t 376 e6060sw_ifpforport(struct e6060sw_softc *sc, int port) 377 { 378 379 if (port < 0 || port > sc->numports) 380 return (NULL); 381 return (sc->ifp[port]); 382 } 383 384 /* 385 * Poll the status for all PHYs. 386 */ 387 static void 388 e6060sw_miipollstat(struct e6060sw_softc *sc) 389 { 390 int i, port; 391 struct mii_data *mii; 392 struct mii_softc *miisc; 393 394 E6060SW_LOCK_ASSERT(sc, MA_NOTOWNED); 395 396 for (i = 0; i < MII_NPHY; i++) { 397 if (((1 << i) & sc->phymask) == 0) 398 continue; 399 port = e6060sw_portforphy(sc, i); 400 if ((*sc->miibus[port]) == NULL) 401 continue; 402 mii = device_get_softc(*sc->miibus[port]); 403 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 404 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != 405 miisc->mii_inst) 406 continue; 407 ukphy_status(miisc); 408 mii_phy_update(miisc, MII_POLLSTAT); 409 } 410 } 411 } 412 413 static void 414 e6060sw_tick(void *arg) 415 { 416 struct e6060sw_softc *sc; 417 418 sc = arg; 419 420 e6060sw_miipollstat(sc); 421 callout_reset(&sc->callout_tick, hz, e6060sw_tick, sc); 422 } 423 424 static void 425 e6060sw_lock(device_t dev) 426 { 427 struct e6060sw_softc *sc; 428 429 sc = device_get_softc(dev); 430 431 E6060SW_LOCK_ASSERT(sc, MA_NOTOWNED); 432 E6060SW_LOCK(sc); 433 } 434 435 static void 436 e6060sw_unlock(device_t dev) 437 { 438 struct e6060sw_softc *sc; 439 440 sc = device_get_softc(dev); 441 442 E6060SW_LOCK_ASSERT(sc, MA_OWNED); 443 E6060SW_UNLOCK(sc); 444 } 445 446 static etherswitch_info_t * 447 e6060sw_getinfo(device_t dev) 448 { 449 struct e6060sw_softc *sc; 450 451 sc = device_get_softc(dev); 452 453 return (&sc->info); 454 } 455 456 static int 457 e6060sw_getport(device_t dev, etherswitch_port_t *p) 458 { 459 struct e6060sw_softc *sc; 460 struct mii_data *mii; 461 struct ifmediareq *ifmr; 462 int err, phy; 463 464 sc = device_get_softc(dev); 465 ifmr = &p->es_ifmr; 466 467 if (p->es_port < 0 || p->es_port >= sc->numports) 468 return (ENXIO); 469 470 p->es_pvid = 0; 471 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 472 p->es_pvid = MDIO_READREG(device_get_parent(dev), 473 CORE_REGISTER + sc->smi_offset + p->es_port, 474 PORT_DEFVLAN) & 0xfff; 475 } 476 477 phy = sc->portphy[p->es_port]; 478 mii = e6060sw_miiforport(sc, p->es_port); 479 if (sc->cpuport != -1 && phy == sc->cpuport) { 480 /* fill in fixed values for CPU port */ 481 p->es_flags |= ETHERSWITCH_PORT_CPU; 482 ifmr->ifm_count = 0; 483 if (sc->media == 100) 484 ifmr->ifm_current = ifmr->ifm_active = 485 IFM_ETHER | IFM_100_TX | IFM_FDX; 486 else 487 ifmr->ifm_current = ifmr->ifm_active = 488 IFM_ETHER | IFM_1000_T | IFM_FDX; 489 ifmr->ifm_mask = 0; 490 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 491 } else if (mii != NULL) { 492 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, 493 &mii->mii_media, SIOCGIFMEDIA); 494 if (err) 495 return (err); 496 } else { 497 return (ENXIO); 498 } 499 return (0); 500 } 501 502 static int 503 e6060sw_setport(device_t dev, etherswitch_port_t *p) 504 { 505 struct e6060sw_softc *sc; 506 struct ifmedia *ifm; 507 struct mii_data *mii; 508 if_t ifp; 509 int err; 510 int data; 511 512 sc = device_get_softc(dev); 513 514 if (p->es_port < 0 || p->es_port >= sc->numports) 515 return (ENXIO); 516 517 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 518 data = MDIO_READREG(device_get_parent(dev), 519 CORE_REGISTER + sc->smi_offset + p->es_port, 520 PORT_DEFVLAN); 521 data &= ~0xfff; 522 data |= p->es_pvid; 523 data |= 1 << 12; 524 MDIO_WRITEREG(device_get_parent(dev), 525 CORE_REGISTER + sc->smi_offset + p->es_port, 526 PORT_DEFVLAN, data); 527 } 528 529 if (sc->portphy[p->es_port] == sc->cpuport) 530 return(0); 531 532 mii = e6060sw_miiforport(sc, p->es_port); 533 if (mii == NULL) 534 return (ENXIO); 535 536 ifp = e6060sw_ifpforport(sc, p->es_port); 537 538 ifm = &mii->mii_media; 539 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA); 540 return (err); 541 } 542 543 static int 544 e6060sw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 545 { 546 struct e6060sw_softc *sc; 547 int data1, data2; 548 int vid; 549 int i, tag; 550 551 sc = device_get_softc(dev); 552 553 if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT) { 554 vg->es_vid = ETHERSWITCH_VID_VALID; 555 vg->es_vid |= vg->es_vlangroup; 556 data1 = MDIO_READREG(device_get_parent(dev), 557 CORE_REGISTER + sc->smi_offset + vg->es_vlangroup, 558 PORT_VLAN_MAP); 559 vg->es_member_ports = data1 & 0x3f; 560 vg->es_untagged_ports = vg->es_member_ports; 561 vg->es_fid = 0; 562 } else if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 563 if (vg->es_vlangroup == 0) 564 return (0); 565 vid = e6060sw_read_vtu(dev, vg->es_vlangroup, &data1, &data2); 566 if (vid > 0) { 567 vg->es_vid = ETHERSWITCH_VID_VALID; 568 vg->es_vid |= vid; 569 vg->es_member_ports = 0; 570 vg->es_untagged_ports = 0; 571 for (i = 0; i < 4; ++i) { 572 tag = data1 >> (i * 4) & 3; 573 if (tag == 0 || tag == 1) { 574 vg->es_member_ports |= 1 << i; 575 vg->es_untagged_ports |= 1 << i; 576 } else if (tag == 2) { 577 vg->es_member_ports |= 1 << i; 578 } 579 } 580 for (i = 0; i < 2; ++i) { 581 tag = data2 >> (i * 4) & 3; 582 if (tag == 0 || tag == 1) { 583 vg->es_member_ports |= 1 << (i + 4); 584 vg->es_untagged_ports |= 1 << (i + 4); 585 } else if (tag == 2) { 586 vg->es_member_ports |= 1 << (i + 4); 587 } 588 } 589 590 } 591 } else { 592 vg->es_vid = 0; 593 } 594 return (0); 595 } 596 597 static int 598 e6060sw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 599 { 600 struct e6060sw_softc *sc; 601 int data1, data2; 602 int i; 603 604 sc = device_get_softc(dev); 605 606 if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT) { 607 data1 = MDIO_READREG(device_get_parent(dev), 608 CORE_REGISTER + sc->smi_offset + vg->es_vlangroup, 609 PORT_VLAN_MAP); 610 data1 &= ~0x3f; 611 data1 |= vg->es_member_ports; 612 MDIO_WRITEREG(device_get_parent(dev), 613 CORE_REGISTER + sc->smi_offset + vg->es_vlangroup, 614 PORT_VLAN_MAP, data1); 615 } else if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 616 if (vg->es_vlangroup == 0) 617 return (0); 618 data1 = 0; 619 data2 = 0; 620 for (i = 0; i < 6; ++i) { 621 if (vg->es_member_ports & 622 vg->es_untagged_ports & (1 << i)) { 623 if (i < 4) { 624 data1 |= (0xd << i * 4); 625 } else { 626 data2 |= (0xd << (i - 4) * 4); 627 } 628 } else if (vg->es_member_ports & (1 << i)) { 629 if (i < 4) { 630 data1 |= (0xe << i * 4); 631 } else { 632 data2 |= (0xe << (i - 4) * 4); 633 } 634 } else { 635 if (i < 4) { 636 data1 |= (0x3 << i * 4); 637 } else { 638 data2 |= (0x3 << (i - 4) * 4); 639 } 640 } 641 } 642 e6060sw_set_vtu(dev, vg->es_vlangroup, data1, data2); 643 } 644 return (0); 645 } 646 647 static void 648 e6060sw_reset_vlans(device_t dev) 649 { 650 struct e6060sw_softc *sc; 651 uint32_t ports; 652 int i; 653 int data; 654 655 sc = device_get_softc(dev); 656 657 for (i = 0; i <= sc->numports; i++) { 658 ports = (1 << (sc->numports + 1)) - 1; 659 ports &= ~(1 << i); 660 if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT) { 661 data = i << 12; 662 } else if (sc->vlan_mode == 0) { 663 data = 1 << 8; 664 } else { 665 data = 0; 666 } 667 data |= ports; 668 MDIO_WRITEREG(device_get_parent(dev), 669 CORE_REGISTER + sc->smi_offset + i, PORT_VLAN_MAP, data); 670 } 671 } 672 673 static void 674 e6060sw_setup(device_t dev) 675 { 676 struct e6060sw_softc *sc; 677 int i; 678 int data; 679 680 sc = device_get_softc(dev); 681 682 for (i = 0; i <= sc->numports; i++) { 683 if (sc->sw_model == E6063 || sc->sw_model == E6065) { 684 data = MDIO_READREG(device_get_parent(dev), 685 CORE_REGISTER + sc->smi_offset + i, PORT_VLAN_MAP); 686 data &= ~(FORCEMAPMASK << FORCEMAPSHIFT); 687 MDIO_WRITEREG(device_get_parent(dev), 688 CORE_REGISTER + sc->smi_offset + i, 689 PORT_VLAN_MAP, data); 690 691 data = MDIO_READREG(device_get_parent(dev), 692 CORE_REGISTER + sc->smi_offset + i, PORT_CONTROL); 693 data |= 3 << ENGRESSFSHIFT; 694 MDIO_WRITEREG(device_get_parent(dev), 695 CORE_REGISTER + sc->smi_offset + i, 696 PORT_CONTROL, data); 697 } 698 } 699 } 700 701 static void 702 e6060sw_dot1q_mode(device_t dev, int mode) 703 { 704 struct e6060sw_softc *sc; 705 int i; 706 int data; 707 708 sc = device_get_softc(dev); 709 710 for (i = 0; i <= sc->numports; i++) { 711 data = MDIO_READREG(device_get_parent(dev), 712 CORE_REGISTER + sc->smi_offset + i, PORT_CONTROL2); 713 data &= ~(DOT1QMODEMASK << DOT1QMODESHIFT); 714 data |= mode << DOT1QMODESHIFT; 715 MDIO_WRITEREG(device_get_parent(dev), 716 CORE_REGISTER + sc->smi_offset + i, PORT_CONTROL2, data); 717 718 data = MDIO_READREG(device_get_parent(dev), 719 CORE_REGISTER + sc->smi_offset + i, 720 PORT_DEFVLAN); 721 data &= ~0xfff; 722 data |= 1; 723 MDIO_WRITEREG(device_get_parent(dev), 724 CORE_REGISTER + sc->smi_offset + i, 725 PORT_DEFVLAN, data); 726 } 727 } 728 729 static int 730 e6060sw_getconf(device_t dev, etherswitch_conf_t *conf) 731 { 732 struct e6060sw_softc *sc; 733 734 sc = device_get_softc(dev); 735 736 /* Return the VLAN mode. */ 737 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 738 conf->vlan_mode = sc->vlan_mode; 739 740 return (0); 741 } 742 743 static void 744 e6060sw_init_vtu(device_t dev) 745 { 746 struct e6060sw_softc *sc; 747 int busy; 748 749 sc = device_get_softc(dev); 750 751 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 752 VTU_OPERATION, VTU_BUSY | (VTU_FLASH << 12)); 753 while (1) { 754 busy = MDIO_READREG(device_get_parent(dev), 755 GLOBAL_REGISTER + sc->smi_offset, VTU_OPERATION); 756 if ((busy & VTU_BUSY) == 0) 757 break; 758 } 759 760 /* initial member set at vlan 1*/ 761 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 762 VTU_DATA1_REG, 0xcccc); 763 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 764 VTU_DATA2_REG, 0x00cc); 765 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 766 VTU_VID_REG, 0x1000 | 1); 767 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 768 VTU_OPERATION, VTU_BUSY | (VTU_LOAD_PURGE << 12) | 1); 769 while (1) { 770 busy = MDIO_READREG(device_get_parent(dev), 771 GLOBAL_REGISTER + sc->smi_offset, VTU_OPERATION); 772 if ((busy & VTU_BUSY) == 0) 773 break; 774 } 775 } 776 777 static void 778 e6060sw_set_vtu(device_t dev, int num, int data1, int data2) 779 { 780 struct e6060sw_softc *sc; 781 int busy; 782 783 sc = device_get_softc(dev); 784 785 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 786 VTU_DATA1_REG, data1); 787 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 788 VTU_DATA2_REG, data2); 789 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 790 VTU_VID_REG, 0x1000 | num); 791 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 792 VTU_OPERATION, VTU_BUSY | (VTU_LOAD_PURGE << 12) | num); 793 while (1) { 794 busy = MDIO_READREG(device_get_parent(dev), 795 GLOBAL_REGISTER + sc->smi_offset, VTU_OPERATION); 796 if ((busy & VTU_BUSY) == 0) 797 break; 798 } 799 800 } 801 802 static int 803 e6060sw_read_vtu(device_t dev, int num, int *data1, int *data2) 804 { 805 struct e6060sw_softc *sc; 806 int busy; 807 808 sc = device_get_softc(dev); 809 810 num = num - 1; 811 812 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 813 VTU_VID_REG, num & 0xfff); 814 /* Get Next */ 815 MDIO_WRITEREG(device_get_parent(dev), GLOBAL_REGISTER + sc->smi_offset, 816 VTU_OPERATION, VTU_BUSY | (VTU_GET_NEXT << 12)); 817 while (1) { 818 busy = MDIO_READREG(device_get_parent(dev), 819 GLOBAL_REGISTER + sc->smi_offset, VTU_OPERATION); 820 if ((busy & VTU_BUSY) == 0) 821 break; 822 } 823 824 int vid = MDIO_READREG(device_get_parent(dev), 825 GLOBAL_REGISTER + sc->smi_offset, VTU_VID_REG); 826 if (vid & 0x1000) { 827 *data1 = MDIO_READREG(device_get_parent(dev), 828 GLOBAL_REGISTER + sc->smi_offset, VTU_DATA1_REG); 829 *data2 = MDIO_READREG(device_get_parent(dev), 830 GLOBAL_REGISTER + sc->smi_offset, VTU_DATA2_REG); 831 832 return (vid & 0xfff); 833 } 834 835 return (-1); 836 } 837 838 static int 839 e6060sw_setconf(device_t dev, etherswitch_conf_t *conf) 840 { 841 struct e6060sw_softc *sc; 842 843 sc = device_get_softc(dev); 844 845 /* Set the VLAN mode. */ 846 if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) { 847 if (conf->vlan_mode == ETHERSWITCH_VLAN_PORT) { 848 sc->vlan_mode = ETHERSWITCH_VLAN_PORT; 849 e6060sw_dot1q_mode(dev, DOT1QNONE); 850 e6060sw_reset_vlans(dev); 851 } else if ((sc->sw_model == E6063 || sc->sw_model == E6065) && 852 conf->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 853 sc->vlan_mode = ETHERSWITCH_VLAN_DOT1Q; 854 e6060sw_dot1q_mode(dev, DOT1QSECURE); 855 e6060sw_init_vtu(dev); 856 } else { 857 sc->vlan_mode = 0; 858 /* Reset VLANs. */ 859 e6060sw_dot1q_mode(dev, DOT1QNONE); 860 e6060sw_reset_vlans(dev); 861 } 862 } 863 864 return (0); 865 } 866 867 static void 868 e6060sw_statchg(device_t dev) 869 { 870 871 DPRINTF(dev, "%s\n", __func__); 872 } 873 874 static int 875 e6060sw_ifmedia_upd(if_t ifp) 876 { 877 struct e6060sw_softc *sc; 878 struct mii_data *mii; 879 880 sc = if_getsoftc(ifp); 881 mii = e6060sw_miiforport(sc, if_getdunit(ifp)); 882 883 DPRINTF(sc->sc_dev, "%s\n", __func__); 884 if (mii == NULL) 885 return (ENXIO); 886 mii_mediachg(mii); 887 return (0); 888 } 889 890 static void 891 e6060sw_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) 892 { 893 struct e6060sw_softc *sc; 894 struct mii_data *mii; 895 896 sc = if_getsoftc(ifp); 897 mii = e6060sw_miiforport(sc, if_getdunit(ifp)); 898 899 DPRINTF(sc->sc_dev, "%s\n", __func__); 900 901 if (mii == NULL) 902 return; 903 mii_pollstat(mii); 904 ifmr->ifm_active = mii->mii_media_active; 905 ifmr->ifm_status = mii->mii_media_status; 906 } 907 908 static int 909 e6060sw_readphy(device_t dev, int phy, int reg) 910 { 911 struct e6060sw_softc *sc; 912 int data; 913 914 sc = device_get_softc(dev); 915 E6060SW_LOCK_ASSERT(sc, MA_NOTOWNED); 916 917 if (phy < 0 || phy >= 32) 918 return (ENXIO); 919 if (reg < 0 || reg >= 32) 920 return (ENXIO); 921 922 E6060SW_LOCK(sc); 923 data = MDIO_READREG(device_get_parent(dev), phy, reg); 924 E6060SW_UNLOCK(sc); 925 926 return (data); 927 } 928 929 static int 930 e6060sw_writephy(device_t dev, int phy, int reg, int data) 931 { 932 struct e6060sw_softc *sc; 933 int err; 934 935 sc = device_get_softc(dev); 936 E6060SW_LOCK_ASSERT(sc, MA_NOTOWNED); 937 938 if (phy < 0 || phy >= 32) 939 return (ENXIO); 940 if (reg < 0 || reg >= 32) 941 return (ENXIO); 942 943 E6060SW_LOCK(sc); 944 err = MDIO_WRITEREG(device_get_parent(dev), phy, reg, data); 945 E6060SW_UNLOCK(sc); 946 947 return (err); 948 } 949 950 /* addr is 5-8 bit is SMI Device Addres, 0-4 bit is SMI Register Address */ 951 952 static int 953 e6060sw_readreg(device_t dev, int addr) 954 { 955 int devaddr, regaddr; 956 957 devaddr = (addr >> 5) & 0x1f; 958 regaddr = addr & 0x1f; 959 960 return MDIO_READREG(device_get_parent(dev), devaddr, regaddr); 961 } 962 963 /* addr is 5-8 bit is SMI Device Addres, 0-4 bit is SMI Register Address */ 964 965 static int 966 e6060sw_writereg(device_t dev, int addr, int value) 967 { 968 int devaddr, regaddr; 969 970 devaddr = (addr >> 5) & 0x1f; 971 regaddr = addr & 0x1f; 972 973 return (MDIO_WRITEREG(device_get_parent(dev), devaddr, regaddr, value)); 974 } 975 976 static device_method_t e6060sw_methods[] = { 977 /* Device interface */ 978 DEVMETHOD(device_probe, e6060sw_probe), 979 DEVMETHOD(device_attach, e6060sw_attach), 980 DEVMETHOD(device_detach, e6060sw_detach), 981 982 /* bus interface */ 983 DEVMETHOD(bus_add_child, device_add_child_ordered), 984 985 /* MII interface */ 986 DEVMETHOD(miibus_readreg, e6060sw_readphy), 987 DEVMETHOD(miibus_writereg, e6060sw_writephy), 988 DEVMETHOD(miibus_statchg, e6060sw_statchg), 989 990 /* MDIO interface */ 991 DEVMETHOD(mdio_readreg, e6060sw_readphy), 992 DEVMETHOD(mdio_writereg, e6060sw_writephy), 993 994 /* etherswitch interface */ 995 DEVMETHOD(etherswitch_lock, e6060sw_lock), 996 DEVMETHOD(etherswitch_unlock, e6060sw_unlock), 997 DEVMETHOD(etherswitch_getinfo, e6060sw_getinfo), 998 DEVMETHOD(etherswitch_readreg, e6060sw_readreg), 999 DEVMETHOD(etherswitch_writereg, e6060sw_writereg), 1000 DEVMETHOD(etherswitch_readphyreg, e6060sw_readphy), 1001 DEVMETHOD(etherswitch_writephyreg, e6060sw_writephy), 1002 DEVMETHOD(etherswitch_getport, e6060sw_getport), 1003 DEVMETHOD(etherswitch_setport, e6060sw_setport), 1004 DEVMETHOD(etherswitch_getvgroup, e6060sw_getvgroup), 1005 DEVMETHOD(etherswitch_setvgroup, e6060sw_setvgroup), 1006 DEVMETHOD(etherswitch_setconf, e6060sw_setconf), 1007 DEVMETHOD(etherswitch_getconf, e6060sw_getconf), 1008 1009 DEVMETHOD_END 1010 }; 1011 1012 DEFINE_CLASS_0(e6060sw, e6060sw_driver, e6060sw_methods, 1013 sizeof(struct e6060sw_softc)); 1014 1015 DRIVER_MODULE(e6060sw, mdio, e6060sw_driver, 0, 0); 1016 DRIVER_MODULE(miibus, e6060sw, miibus_driver, 0, 0); 1017 DRIVER_MODULE(mdio, e6060sw, mdio_driver, 0, 0); 1018 DRIVER_MODULE(etherswitch, e6060sw, etherswitch_driver, 0, 0); 1019 MODULE_VERSION(e6060sw, 1); 1020 MODULE_DEPEND(e6060sw, miibus, 1, 1, 1); /* XXX which versions? */ 1021 MODULE_DEPEND(e6060sw, etherswitch, 1, 1, 1); /* XXX which versions? */ 1022