2 * Copyright (c) 2015 Semihalf
3 * Copyright (c) 2015 Stormshield
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/module.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
41 #include <net/if_media.h>
42 #include <net/if_types.h>
44 #include <dev/etherswitch/etherswitch.h>
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
48 #include <dev/fdt/fdt_common.h>
49 #include <dev/ofw/ofw_bus.h>
51 #include "e6000swreg.h"
52 #include "etherswitch_if.h"
53 #include "miibus_if.h"
56 MALLOC_DECLARE(M_E6000SW);
57 MALLOC_DEFINE(M_E6000SW, "e6000sw", "e6000sw switch");
59 #define E6000SW_LOCK(_sc) sx_xlock(&(_sc)->sx)
60 #define E6000SW_UNLOCK(_sc) sx_unlock(&(_sc)->sx)
61 #define E6000SW_LOCK_ASSERT(_sc, _what) sx_assert(&(_sc)->sx, (_what))
62 #define E6000SW_TRYLOCK(_sc) sx_tryxlock(&(_sc)->sx)
64 typedef struct e6000sw_softc {
69 struct ifnet *ifp[E6000SW_MAX_PORTS];
70 char *ifname[E6000SW_MAX_PORTS];
71 device_t miibus[E6000SW_MAX_PORTS];
74 uint32_t cpuports_mask;
82 int vid[E6000SW_NUM_VGROUPS];
83 int members[E6000SW_NUM_VGROUPS];
84 int vgroup[E6000SW_MAX_PORTS];
87 static etherswitch_info_t etherswitch_info = {
89 .es_nvlangroups = E6000SW_NUM_VGROUPS,
90 .es_vlan_caps = ETHERSWITCH_VLAN_PORT,
91 .es_name = "Marvell 6000 series switch"
94 static void e6000sw_identify(driver_t *, device_t);
95 static int e6000sw_probe(device_t);
96 static int e6000sw_attach(device_t);
97 static int e6000sw_detach(device_t);
98 static int e6000sw_readphy(device_t, int, int);
99 static int e6000sw_writephy(device_t, int, int, int);
100 static etherswitch_info_t* e6000sw_getinfo(device_t);
101 static int e6000sw_getconf(device_t, etherswitch_conf_t *);
102 static void e6000sw_lock(device_t);
103 static void e6000sw_unlock(device_t);
104 static int e6000sw_getport(device_t, etherswitch_port_t *);
105 static int e6000sw_setport(device_t, etherswitch_port_t *);
106 static int e6000sw_readreg_wrapper(device_t, int);
107 static int e6000sw_writereg_wrapper(device_t, int, int);
108 static int e6000sw_readphy_wrapper(device_t, int, int);
109 static int e6000sw_writephy_wrapper(device_t, int, int, int);
110 static int e6000sw_getvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
111 static int e6000sw_setvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
112 static int e6000sw_setvgroup(device_t, etherswitch_vlangroup_t *);
113 static int e6000sw_getvgroup(device_t, etherswitch_vlangroup_t *);
114 static void e6000sw_setup(device_t, e6000sw_softc_t *);
115 static void e6000sw_port_vlan_conf(e6000sw_softc_t *);
116 static void e6000sw_tick(void *);
117 static void e6000sw_set_atustat(device_t, e6000sw_softc_t *, int, int);
118 static int e6000sw_atu_flush(device_t, e6000sw_softc_t *, int);
119 static __inline void e6000sw_writereg(e6000sw_softc_t *, int, int, int);
120 static __inline uint32_t e6000sw_readreg(e6000sw_softc_t *, int, int);
121 static int e6000sw_ifmedia_upd(struct ifnet *);
122 static void e6000sw_ifmedia_sts(struct ifnet *, struct ifmediareq *);
123 static int e6000sw_atu_mac_table(device_t, e6000sw_softc_t *, struct atu_opt *,
125 static int e6000sw_get_pvid(e6000sw_softc_t *, int, int *);
126 static int e6000sw_set_pvid(e6000sw_softc_t *, int, int);
127 static __inline bool e6000sw_is_cpuport(e6000sw_softc_t *, int);
128 static __inline bool e6000sw_is_fixedport(e6000sw_softc_t *, int);
129 static __inline bool e6000sw_is_phyport(e6000sw_softc_t *, int);
130 static __inline bool e6000sw_is_portenabled(e6000sw_softc_t *, int);
131 static __inline struct mii_data *e6000sw_miiforphy(e6000sw_softc_t *,
134 static device_method_t e6000sw_methods[] = {
135 /* device interface */
136 DEVMETHOD(device_identify, e6000sw_identify),
137 DEVMETHOD(device_probe, e6000sw_probe),
138 DEVMETHOD(device_attach, e6000sw_attach),
139 DEVMETHOD(device_detach, e6000sw_detach),
142 DEVMETHOD(bus_add_child, device_add_child_ordered),
145 DEVMETHOD(miibus_readreg, e6000sw_readphy),
146 DEVMETHOD(miibus_writereg, e6000sw_writephy),
148 /* etherswitch interface */
149 DEVMETHOD(etherswitch_getinfo, e6000sw_getinfo),
150 DEVMETHOD(etherswitch_getconf, e6000sw_getconf),
151 DEVMETHOD(etherswitch_lock, e6000sw_lock),
152 DEVMETHOD(etherswitch_unlock, e6000sw_unlock),
153 DEVMETHOD(etherswitch_getport, e6000sw_getport),
154 DEVMETHOD(etherswitch_setport, e6000sw_setport),
155 DEVMETHOD(etherswitch_readreg, e6000sw_readreg_wrapper),
156 DEVMETHOD(etherswitch_writereg, e6000sw_writereg_wrapper),
157 DEVMETHOD(etherswitch_readphyreg, e6000sw_readphy_wrapper),
158 DEVMETHOD(etherswitch_writephyreg, e6000sw_writephy_wrapper),
159 DEVMETHOD(etherswitch_setvgroup, e6000sw_setvgroup_wrapper),
160 DEVMETHOD(etherswitch_getvgroup, e6000sw_getvgroup_wrapper),
165 static devclass_t e6000sw_devclass;
167 DEFINE_CLASS_0(e6000sw, e6000sw_driver, e6000sw_methods,
168 sizeof(e6000sw_softc_t));
170 DRIVER_MODULE(e6000sw, mdio, e6000sw_driver, e6000sw_devclass, 0, 0);
171 DRIVER_MODULE(etherswitch, e6000sw, etherswitch_driver, etherswitch_devclass, 0,
173 DRIVER_MODULE(miibus, e6000sw, miibus_driver, miibus_devclass, 0, 0);
174 MODULE_DEPEND(e6000sw, mdio, 1, 1, 1);
177 #define SMI_CMD_BUSY (1 << 15)
178 #define SMI_CMD_OP_READ ((2 << 10) | SMI_CMD_BUSY | (1 << 12))
179 #define SMI_CMD_OP_WRITE ((1 << 10) | SMI_CMD_BUSY | (1 << 12))
182 #define MDIO_READ(dev, addr, reg) \
183 MDIO_READREG(device_get_parent(dev), (addr), (reg))
184 #define MDIO_WRITE(dev, addr, reg, val) \
185 MDIO_WRITEREG(device_get_parent(dev), (addr), (reg), (val))
188 e6000sw_identify(driver_t *driver, device_t parent)
191 if (device_find_child(parent, "e6000sw", -1) == NULL)
192 BUS_ADD_CHILD(parent, 0, "e6000sw", -1);
196 e6000sw_probe(device_t dev)
199 const char *description;
200 phandle_t dsa_node, switch_node;
203 dsa_node = fdt_find_compatible(OF_finddevice("/"),
205 switch_node = OF_child(dsa_node);
207 if (switch_node == 0)
210 sc = device_get_softc(dev);
212 sc->node = switch_node;
214 if (OF_getencprop(sc->node, "reg", &sc->sw_addr,
215 sizeof(sc->sw_addr)) < 0)
217 if (sc->sw_addr != 0 && (sc->sw_addr % 2) == 0)
218 sc->multi_chip = true;
221 * Create temporary lock, just to satisfy assertions,
222 * when obtaining the switch ID. Destroy immediately afterwards.
224 sx_init(&sc->sx, "e6000sw_tmp");
226 id = e6000sw_readreg(sc, REG_PORT(0), SWITCH_ID);
230 switch (id & 0xfff0) {
232 description = "Marvell 88E6141";
237 description = "Marvell 88E6341";
242 description = "Marvell 88E6352";
246 description = "Marvell 88E6172";
250 description = "Marvell 88E6176";
254 device_printf(dev, "Unrecognized device, id 0x%x.\n", id);
258 device_set_desc(dev, description);
260 return (BUS_PROBE_DEFAULT);
264 e6000sw_parse_child_fdt(e6000sw_softc_t *sc, phandle_t child,
265 uint32_t *fixed_mask, uint32_t *cpu_mask, int *pport, int *pvlangroup)
267 boolean_t fixed_link;
269 uint32_t port, vlangroup;
271 if (fixed_mask == NULL || cpu_mask == NULL || pport == NULL)
274 OF_getprop(child, "label", (void *)portlabel, sizeof(portlabel));
275 OF_getencprop(child, "reg", (void *)&port, sizeof(port));
277 if (OF_getencprop(child, "vlangroup", (void *)&vlangroup,
278 sizeof(vlangroup)) > 0) {
279 if (vlangroup >= E6000SW_NUM_VGROUPS)
281 *pvlangroup = vlangroup;
286 if (port >= sc->num_ports)
290 if (strncmp(portlabel, "cpu", 3) == 0) {
291 device_printf(sc->dev, "CPU port at %d\n", port);
292 *cpu_mask |= (1 << port);
295 fixed_link = OF_child(child);
297 *fixed_mask |= (1 << port);
298 device_printf(sc->dev, "fixed port at %d\n", port);
300 device_printf(sc->dev, "PHY at port %d\n", port);
307 e6000sw_init_interface(e6000sw_softc_t *sc, int port)
311 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->dev));
313 sc->ifp[port] = if_alloc(IFT_ETHER);
314 if (sc->ifp[port] == NULL)
316 sc->ifp[port]->if_softc = sc;
317 sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
318 IFF_DRV_RUNNING | IFF_SIMPLEX;
319 sc->ifname[port] = malloc(strlen(name) + 1, M_E6000SW, M_NOWAIT);
320 if (sc->ifname[port] == NULL) {
321 if_free(sc->ifp[port]);
324 memcpy(sc->ifname[port], name, strlen(name) + 1);
325 if_initname(sc->ifp[port], sc->ifname[port], port);
331 e6000sw_attach_miibus(e6000sw_softc_t *sc, int port)
335 err = mii_attach(sc->dev, &sc->miibus[port], sc->ifp[port],
336 e6000sw_ifmedia_upd, e6000sw_ifmedia_sts, BMSR_DEFCAPMASK,
337 port + sc->phy_base, MII_OFFSET_ANY, 0);
345 e6000sw_attach(device_t dev)
349 int err, port, vlangroup;
350 int member_ports[E6000SW_NUM_VGROUPS];
351 etherswitch_vlangroup_t vg;
354 sc = device_get_softc(dev);
357 device_printf(dev, "multi-chip addressing mode\n");
359 device_printf(dev, "single-chip addressing mode\n");
361 sx_init(&sc->sx, "e6000sw");
364 e6000sw_setup(dev, sc);
365 bzero(member_ports, sizeof(member_ports));
367 for (child = OF_child(sc->node); child != 0; child = OF_peer(child)) {
368 err = e6000sw_parse_child_fdt(sc, child, &sc->fixed_mask,
369 &sc->cpuports_mask, &port, &vlangroup);
371 device_printf(sc->dev, "failed to parse DTS\n");
376 member_ports[vlangroup] |= (1 << port);
378 /* Port is in use. */
379 sc->ports_mask |= (1 << port);
381 err = e6000sw_init_interface(sc, port);
383 device_printf(sc->dev, "failed to init interface\n");
387 /* Don't attach miibus at CPU/fixed ports */
388 if (!e6000sw_is_phyport(sc, port))
391 err = e6000sw_attach_miibus(sc, port);
393 device_printf(sc->dev, "failed to attach miibus\n");
398 etherswitch_info.es_nports = sc->num_ports;
399 for (port = 0; port < sc->num_ports; port++)
400 sc->vgroup[port] = E6000SW_PORT_NO_VGROUP;
402 /* Set VLAN configuration */
403 e6000sw_port_vlan_conf(sc);
406 for (vlangroup = 0; vlangroup < E6000SW_NUM_VGROUPS; vlangroup++)
407 if (member_ports[vlangroup] != 0) {
408 vg.es_vlangroup = vg.es_vid = vlangroup;
409 vg.es_member_ports = vg.es_untagged_ports =
410 member_ports[vlangroup];
411 e6000sw_setvgroup(dev, &vg);
416 bus_generic_probe(dev);
417 bus_generic_attach(dev);
419 kproc_create(e6000sw_tick, sc, &sc->kproc, 0, 0, "e6000sw tick kproc");
431 e6000sw_poll_done(e6000sw_softc_t *sc)
435 for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
437 if ((e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG) &
438 (1 << PHY_CMD_SMI_BUSY)) == 0)
441 pause("e6000sw PHY poll", hz/1000);
448 * PHY registers are paged. Put page index in reg 22 (accessible from every
449 * page), then access specific register.
452 e6000sw_readphy(device_t dev, int phy, int reg)
458 sc = device_get_softc(dev);
459 if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
460 device_printf(dev, "Wrong register address.\n");
464 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
466 err = e6000sw_poll_done(sc);
468 device_printf(dev, "Timeout while waiting for switch\n");
472 val = 1 << PHY_CMD_SMI_BUSY;
473 val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
474 val |= PHY_CMD_OPCODE_READ << PHY_CMD_OPCODE;
475 val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
476 val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
477 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
479 err = e6000sw_poll_done(sc);
481 device_printf(dev, "Timeout while waiting for switch\n");
485 val = e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG);
487 return (val & PHY_DATA_MASK);
491 e6000sw_writephy(device_t dev, int phy, int reg, int data)
497 sc = device_get_softc(dev);
498 if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
499 device_printf(dev, "Wrong register address.\n");
503 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
505 err = e6000sw_poll_done(sc);
507 device_printf(dev, "Timeout while waiting for switch\n");
511 val = 1 << PHY_CMD_SMI_BUSY;
512 val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
513 val |= PHY_CMD_OPCODE_WRITE << PHY_CMD_OPCODE;
514 val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
515 val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
516 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG,
517 data & PHY_DATA_MASK);
518 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
520 err = e6000sw_poll_done(sc);
522 device_printf(dev, "Timeout while waiting for switch\n");
528 e6000sw_detach(device_t dev)
533 sc = device_get_softc(dev);
534 bus_generic_detach(dev);
536 for (phy = 0; phy < sc->num_ports; phy++) {
537 if (sc->miibus[phy] != NULL)
538 device_delete_child(dev, sc->miibus[phy]);
539 if (sc->ifp[phy] != NULL)
540 if_free(sc->ifp[phy]);
541 if (sc->ifname[phy] != NULL)
542 free(sc->ifname[phy], M_E6000SW);
548 static etherswitch_info_t*
549 e6000sw_getinfo(device_t dev)
552 return (ðerswitch_info);
556 e6000sw_getconf(device_t dev __unused, etherswitch_conf_t *conf)
559 /* Return the VLAN mode. */
560 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
561 conf->vlan_mode = ETHERSWITCH_VLAN_PORT;
567 e6000sw_lock(device_t dev)
569 struct e6000sw_softc *sc;
571 sc = device_get_softc(dev);
573 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
578 e6000sw_unlock(device_t dev)
580 struct e6000sw_softc *sc;
582 sc = device_get_softc(dev);
584 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
589 e6000sw_getport(device_t dev, etherswitch_port_t *p)
591 struct mii_data *mii;
593 struct ifmediareq *ifmr;
595 e6000sw_softc_t *sc = device_get_softc(dev);
596 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
598 if (p->es_port >= sc->num_ports || p->es_port < 0)
600 if (!e6000sw_is_portenabled(sc, p->es_port))
605 e6000sw_get_pvid(sc, p->es_port, &p->es_pvid);
607 if (e6000sw_is_cpuport(sc, p->es_port)) {
608 p->es_flags |= ETHERSWITCH_PORT_CPU;
610 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
612 ifmr->ifm_current = ifmr->ifm_active =
613 IFM_ETHER | IFM_1000_T | IFM_FDX;
615 } else if (e6000sw_is_fixedport(sc, p->es_port)) {
617 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
619 ifmr->ifm_current = ifmr->ifm_active =
620 IFM_ETHER | IFM_1000_T | IFM_FDX;
623 mii = e6000sw_miiforphy(sc, p->es_port);
624 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
625 &mii->mii_media, SIOCGIFMEDIA);
633 e6000sw_setport(device_t dev, etherswitch_port_t *p)
637 struct mii_data *mii;
639 sc = device_get_softc(dev);
640 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
642 if (p->es_port >= sc->num_ports || p->es_port < 0)
644 if (!e6000sw_is_portenabled(sc, p->es_port))
650 e6000sw_set_pvid(sc, p->es_port, p->es_pvid);
651 if (!e6000sw_is_cpuport(sc, p->es_port) &&
652 !e6000sw_is_fixedport(sc, p->es_port)) {
653 mii = e6000sw_miiforphy(sc, p->es_port);
654 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, &mii->mii_media,
663 * Registers in this switch are divided into sections, specified in
664 * documentation. So as to access any of them, section index and reg index
665 * is necessary. etherswitchcfg uses only one variable, so indexes were
666 * compressed into addr_reg: 32 * section_index + reg_index.
669 e6000sw_readreg_wrapper(device_t dev, int addr_reg)
672 if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
673 (addr_reg < (REG_PORT(0) * 32))) {
674 device_printf(dev, "Wrong register address.\n");
678 return (e6000sw_readreg(device_get_softc(dev), addr_reg / 32,
683 e6000sw_writereg_wrapper(device_t dev, int addr_reg, int val)
686 if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
687 (addr_reg < (REG_PORT(0) * 32))) {
688 device_printf(dev, "Wrong register address.\n");
691 e6000sw_writereg(device_get_softc(dev), addr_reg / 5,
698 * These wrappers are necessary because PHY accesses from etherswitchcfg
699 * need to be synchronized with locks, while miibus PHY accesses do not.
702 e6000sw_readphy_wrapper(device_t dev, int phy, int reg)
707 sc = device_get_softc(dev);
708 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
711 ret = e6000sw_readphy(dev, phy, reg);
718 e6000sw_writephy_wrapper(device_t dev, int phy, int reg, int data)
723 sc = device_get_softc(dev);
724 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
727 ret = e6000sw_writephy(dev, phy, reg, data);
734 * setvgroup/getvgroup called from etherswitchfcg need to be locked,
735 * while internal calls do not.
738 e6000sw_setvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
743 sc = device_get_softc(dev);
744 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
747 ret = e6000sw_setvgroup(dev, vg);
754 e6000sw_getvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
759 sc = device_get_softc(dev);
760 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
763 ret = e6000sw_getvgroup(dev, vg);
770 e6000sw_flush_port(e6000sw_softc_t *sc, int port)
774 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
775 reg &= ~PORT_VLAN_MAP_TABLE_MASK;
776 reg &= ~PORT_VLAN_MAP_FID_MASK;
777 e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
778 if (sc->vgroup[port] != E6000SW_PORT_NO_VGROUP) {
780 * If port belonged somewhere, owner-group
781 * should have its entry removed.
783 sc->members[sc->vgroup[port]] &= ~(1 << port);
784 sc->vgroup[port] = E6000SW_PORT_NO_VGROUP;
789 e6000sw_port_assign_vgroup(e6000sw_softc_t *sc, int port, int fid, int vgroup,
794 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
795 reg &= ~PORT_VLAN_MAP_TABLE_MASK;
796 reg &= ~PORT_VLAN_MAP_FID_MASK;
797 reg |= members & ~(1 << port);
798 reg |= (fid << PORT_VLAN_MAP_FID) & PORT_VLAN_MAP_FID_MASK;
799 e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
800 sc->vgroup[port] = vgroup;
804 e6000sw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
809 sc = device_get_softc(dev);
810 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
812 if (vg->es_vlangroup >= E6000SW_NUM_VGROUPS)
814 if (vg->es_member_ports != vg->es_untagged_ports) {
815 device_printf(dev, "Tagged ports not supported.\n");
819 vg->es_untagged_ports &= PORT_VLAN_MAP_TABLE_MASK;
820 fid = vg->es_vlangroup + 1;
821 for (port = 0; port < sc->num_ports; port++) {
822 if ((sc->members[sc->vgroup[port]] & (1 << port)))
823 e6000sw_flush_port(sc, port);
824 if (vg->es_untagged_ports & (1 << port))
825 e6000sw_port_assign_vgroup(sc, port, fid,
826 vg->es_vlangroup, vg->es_untagged_ports);
828 sc->vid[vg->es_vlangroup] = vg->es_vid;
829 sc->members[vg->es_vlangroup] = vg->es_untagged_ports;
835 e6000sw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
839 sc = device_get_softc(dev);
840 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
842 if (vg->es_vlangroup >= E6000SW_NUM_VGROUPS)
844 vg->es_untagged_ports = vg->es_member_ports =
845 sc->members[vg->es_vlangroup];
846 if (vg->es_untagged_ports != 0)
847 vg->es_vid = ETHERSWITCH_VID_VALID;
852 static __inline struct mii_data*
853 e6000sw_miiforphy(e6000sw_softc_t *sc, unsigned int phy)
856 if (!e6000sw_is_phyport(sc, phy))
859 return (device_get_softc(sc->miibus[phy]));
863 e6000sw_ifmedia_upd(struct ifnet *ifp)
866 struct mii_data *mii;
869 mii = e6000sw_miiforphy(sc, ifp->if_dunit);
878 e6000sw_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
881 struct mii_data *mii;
884 mii = e6000sw_miiforphy(sc, ifp->if_dunit);
890 ifmr->ifm_active = mii->mii_media_active;
891 ifmr->ifm_status = mii->mii_media_status;
895 e6000sw_smi_waitready(e6000sw_softc_t *sc, int phy)
899 for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
900 if ((MDIO_READ(sc->dev, phy, SMI_CMD) & SMI_CMD_BUSY) == 0)
908 static __inline uint32_t
909 e6000sw_readreg(e6000sw_softc_t *sc, int addr, int reg)
912 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
915 return (MDIO_READ(sc->dev, addr, reg) & 0xffff);
917 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
918 printf("e6000sw: readreg timeout\n");
921 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
922 SMI_CMD_OP_READ | (addr << 5) | reg);
923 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
924 printf("e6000sw: readreg timeout\n");
928 return (MDIO_READ(sc->dev, sc->sw_addr, SMI_DATA) & 0xffff);
932 e6000sw_writereg(e6000sw_softc_t *sc, int addr, int reg, int val)
935 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
937 if (!sc->multi_chip) {
938 MDIO_WRITE(sc->dev, addr, reg, val);
942 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
943 printf("e6000sw: readreg timeout\n");
946 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_DATA, val);
947 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
948 SMI_CMD_OP_WRITE | (addr << 5) | reg);
949 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
950 printf("e6000sw: readreg timeout\n");
956 e6000sw_is_cpuport(e6000sw_softc_t *sc, int port)
959 return ((sc->cpuports_mask & (1 << port)) ? true : false);
963 e6000sw_is_fixedport(e6000sw_softc_t *sc, int port)
966 return ((sc->fixed_mask & (1 << port)) ? true : false);
970 e6000sw_is_phyport(e6000sw_softc_t *sc, int port)
973 phy_mask = ~(sc->fixed_mask | sc->cpuports_mask);
975 return ((phy_mask & (1 << port)) ? true : false);
979 e6000sw_is_portenabled(e6000sw_softc_t *sc, int port)
982 return ((sc->ports_mask & (1 << port)) ? true : false);
986 e6000sw_set_pvid(e6000sw_softc_t *sc, int port, int pvid)
989 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, pvid &
990 PORT_VID_DEF_VID_MASK);
996 e6000sw_get_pvid(e6000sw_softc_t *sc, int port, int *pvid)
1002 *pvid = e6000sw_readreg(sc, REG_PORT(port), PORT_VID) &
1003 PORT_VID_DEF_VID_MASK;
1009 * Convert port status to ifmedia.
1012 e6000sw_update_ifmedia(uint16_t portstatus, u_int *media_status, u_int *media_active)
1014 *media_active = IFM_ETHER;
1015 *media_status = IFM_AVALID;
1017 if ((portstatus & PORT_STATUS_LINK_MASK) != 0)
1018 *media_status |= IFM_ACTIVE;
1020 *media_active |= IFM_NONE;
1024 switch (portstatus & PORT_STATUS_SPEED_MASK) {
1025 case PORT_STATUS_SPEED_10:
1026 *media_active |= IFM_10_T;
1028 case PORT_STATUS_SPEED_100:
1029 *media_active |= IFM_100_TX;
1031 case PORT_STATUS_SPEED_1000:
1032 *media_active |= IFM_1000_T;
1036 if ((portstatus & PORT_STATUS_DUPLEX_MASK) == 0)
1037 *media_active |= IFM_FDX;
1039 *media_active |= IFM_HDX;
1043 e6000sw_tick (void *arg)
1045 e6000sw_softc_t *sc;
1046 struct mii_data *mii;
1047 struct mii_softc *miisc;
1048 uint16_t portstatus;
1053 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
1057 for (port = 0; port < sc->num_ports; port++) {
1058 /* Tick only on PHY ports */
1059 if (!e6000sw_is_portenabled(sc, port) ||
1060 !e6000sw_is_phyport(sc, port))
1063 mii = e6000sw_miiforphy(sc, port);
1067 portstatus = e6000sw_readreg(sc, REG_PORT(port),
1070 e6000sw_update_ifmedia(portstatus,
1071 &mii->mii_media_status, &mii->mii_media_active);
1073 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1074 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media)
1077 mii_phy_update(miisc, MII_POLLSTAT);
1081 pause("e6000sw tick", 1000);
1086 e6000sw_setup(device_t dev, e6000sw_softc_t *sc)
1088 uint16_t atu_ctrl, atu_age;
1090 /* Set aging time */
1091 e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL,
1092 (E6000SW_DEFAULT_AGETIME << ATU_CONTROL_AGETIME) |
1093 (1 << ATU_CONTROL_LEARN2ALL));
1095 /* Send all with specific mac address to cpu port */
1096 e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_2x, MGMT_EN_ALL);
1097 e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_0x, MGMT_EN_ALL);
1099 /* Disable Remote Management */
1100 e6000sw_writereg(sc, REG_GLOBAL, SWITCH_GLOBAL_CONTROL2, 0);
1102 /* Disable loopback filter and flow control messages */
1103 e6000sw_writereg(sc, REG_GLOBAL2, SWITCH_MGMT,
1104 SWITCH_MGMT_PRI_MASK |
1105 (1 << SWITCH_MGMT_RSVD2CPU) |
1106 SWITCH_MGMT_FC_PRI_MASK |
1107 (1 << SWITCH_MGMT_FORCEFLOW));
1109 e6000sw_atu_flush(dev, sc, NO_OPERATION);
1110 e6000sw_atu_mac_table(dev, sc, NULL, NO_OPERATION);
1111 e6000sw_set_atustat(dev, sc, 0, COUNT_ALL);
1113 /* Set ATU AgeTime to 15 seconds */
1116 atu_ctrl = e6000sw_readreg(sc, REG_GLOBAL, ATU_CONTROL);
1118 /* Set new AgeTime field */
1119 atu_ctrl &= ~ATU_CONTROL_AGETIME_MASK;
1120 e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL, atu_ctrl |
1121 (atu_age << ATU_CONTROL_AGETIME));
1125 e6000sw_port_vlan_conf(e6000sw_softc_t *sc)
1131 /* Disable all ports */
1132 for (port = 0; port < sc->num_ports; port++) {
1133 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1134 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
1135 (ret & ~PORT_CONTROL_ENABLE));
1138 /* Set port priority */
1139 for (port = 0; port < sc->num_ports; port++) {
1140 if (!e6000sw_is_portenabled(sc, port))
1142 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1143 ret &= ~PORT_VID_PRIORITY_MASK;
1144 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1148 for (port = 0; port < sc->num_ports; port++) {
1149 if (!e6000sw_is_portenabled(sc, port))
1151 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1152 ret &= ~PORT_VID_DEF_VID_MASK;
1154 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1157 /* Enable all ports */
1158 for (port = 0; port < sc->num_ports; port++) {
1159 if (!e6000sw_is_portenabled(sc, port))
1161 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1162 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL, (ret |
1163 PORT_CONTROL_ENABLE));
1168 e6000sw_set_atustat(device_t dev, e6000sw_softc_t *sc, int bin, int flag)
1172 ret = e6000sw_readreg(sc, REG_GLOBAL2, ATU_STATS);
1173 e6000sw_writereg(sc, REG_GLOBAL2, ATU_STATS, (bin << ATU_STATS_BIN ) |
1174 (flag << ATU_STATS_FLAG));
1178 e6000sw_atu_mac_table(device_t dev, e6000sw_softc_t *sc, struct atu_opt *atu,
1185 if (flag == NO_OPERATION)
1187 else if ((flag & (LOAD_FROM_FIB | PURGE_FROM_FIB | GET_NEXT_IN_FIB |
1188 GET_VIOLATION_DATA | CLEAR_VIOLATION_DATA)) == 0) {
1189 device_printf(dev, "Wrong Opcode for ATU operation\n");
1193 ret_opt = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1195 if (ret_opt & ATU_UNIT_BUSY) {
1196 device_printf(dev, "ATU unit is busy, cannot access"
1200 if(flag & LOAD_FROM_FIB) {
1201 ret_data = e6000sw_readreg(sc, REG_GLOBAL, ATU_DATA);
1202 e6000sw_writereg(sc, REG_GLOBAL2, ATU_DATA, (ret_data &
1205 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR01, atu->mac_01);
1206 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR23, atu->mac_23);
1207 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR45, atu->mac_45);
1208 e6000sw_writereg(sc, REG_GLOBAL, ATU_FID, atu->fid);
1210 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret_opt |
1211 ATU_UNIT_BUSY | flag));
1213 retries = E6000SW_RETRIES;
1214 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1215 ATU_OPERATION) & ATU_UNIT_BUSY))
1219 device_printf(dev, "Timeout while flushing\n");
1220 else if (flag & GET_NEXT_IN_FIB) {
1221 atu->mac_01 = e6000sw_readreg(sc, REG_GLOBAL,
1223 atu->mac_23 = e6000sw_readreg(sc, REG_GLOBAL,
1225 atu->mac_45 = e6000sw_readreg(sc, REG_GLOBAL,
1234 e6000sw_atu_flush(device_t dev, e6000sw_softc_t *sc, int flag)
1239 if (flag == NO_OPERATION)
1242 ret = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1243 if (ret & ATU_UNIT_BUSY) {
1244 device_printf(dev, "Atu unit is busy, cannot flush\n");
1247 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret |
1248 ATU_UNIT_BUSY | flag));
1249 retries = E6000SW_RETRIES;
1250 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1251 ATU_OPERATION) & ATU_UNIT_BUSY))
1255 device_printf(dev, "Timeout while flushing\n");