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];
75 uint32_t cpuports_mask;
77 uint32_t fixed25_mask;
84 int vid[E6000SW_NUM_VGROUPS];
85 int members[E6000SW_NUM_VGROUPS];
86 int vgroup[E6000SW_MAX_PORTS];
89 static etherswitch_info_t etherswitch_info = {
91 .es_nvlangroups = E6000SW_NUM_VGROUPS,
92 .es_vlan_caps = ETHERSWITCH_VLAN_PORT,
93 .es_name = "Marvell 6000 series switch"
96 static void e6000sw_identify(driver_t *, device_t);
97 static int e6000sw_probe(device_t);
98 static int e6000sw_attach(device_t);
99 static int e6000sw_detach(device_t);
100 static int e6000sw_readphy(device_t, int, int);
101 static int e6000sw_writephy(device_t, int, int, int);
102 static etherswitch_info_t* e6000sw_getinfo(device_t);
103 static int e6000sw_getconf(device_t, etherswitch_conf_t *);
104 static void e6000sw_lock(device_t);
105 static void e6000sw_unlock(device_t);
106 static int e6000sw_getport(device_t, etherswitch_port_t *);
107 static int e6000sw_setport(device_t, etherswitch_port_t *);
108 static int e6000sw_readreg_wrapper(device_t, int);
109 static int e6000sw_writereg_wrapper(device_t, int, int);
110 static int e6000sw_readphy_wrapper(device_t, int, int);
111 static int e6000sw_writephy_wrapper(device_t, int, int, int);
112 static int e6000sw_getvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
113 static int e6000sw_setvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
114 static int e6000sw_setvgroup(device_t, etherswitch_vlangroup_t *);
115 static int e6000sw_getvgroup(device_t, etherswitch_vlangroup_t *);
116 static void e6000sw_setup(device_t, e6000sw_softc_t *);
117 static void e6000sw_port_vlan_conf(e6000sw_softc_t *);
118 static void e6000sw_tick(void *);
119 static void e6000sw_set_atustat(device_t, e6000sw_softc_t *, int, int);
120 static int e6000sw_atu_flush(device_t, e6000sw_softc_t *, int);
121 static __inline void e6000sw_writereg(e6000sw_softc_t *, int, int, int);
122 static __inline uint32_t e6000sw_readreg(e6000sw_softc_t *, int, int);
123 static int e6000sw_ifmedia_upd(struct ifnet *);
124 static void e6000sw_ifmedia_sts(struct ifnet *, struct ifmediareq *);
125 static int e6000sw_atu_mac_table(device_t, e6000sw_softc_t *, struct atu_opt *,
127 static int e6000sw_get_pvid(e6000sw_softc_t *, int, int *);
128 static int e6000sw_set_pvid(e6000sw_softc_t *, int, int);
129 static __inline bool e6000sw_is_cpuport(e6000sw_softc_t *, int);
130 static __inline bool e6000sw_is_fixedport(e6000sw_softc_t *, int);
131 static __inline bool e6000sw_is_fixed25port(e6000sw_softc_t *, int);
132 static __inline bool e6000sw_is_phyport(e6000sw_softc_t *, int);
133 static __inline bool e6000sw_is_portenabled(e6000sw_softc_t *, int);
134 static __inline struct mii_data *e6000sw_miiforphy(e6000sw_softc_t *,
137 static device_method_t e6000sw_methods[] = {
138 /* device interface */
139 DEVMETHOD(device_identify, e6000sw_identify),
140 DEVMETHOD(device_probe, e6000sw_probe),
141 DEVMETHOD(device_attach, e6000sw_attach),
142 DEVMETHOD(device_detach, e6000sw_detach),
145 DEVMETHOD(bus_add_child, device_add_child_ordered),
148 DEVMETHOD(miibus_readreg, e6000sw_readphy),
149 DEVMETHOD(miibus_writereg, e6000sw_writephy),
151 /* etherswitch interface */
152 DEVMETHOD(etherswitch_getinfo, e6000sw_getinfo),
153 DEVMETHOD(etherswitch_getconf, e6000sw_getconf),
154 DEVMETHOD(etherswitch_lock, e6000sw_lock),
155 DEVMETHOD(etherswitch_unlock, e6000sw_unlock),
156 DEVMETHOD(etherswitch_getport, e6000sw_getport),
157 DEVMETHOD(etherswitch_setport, e6000sw_setport),
158 DEVMETHOD(etherswitch_readreg, e6000sw_readreg_wrapper),
159 DEVMETHOD(etherswitch_writereg, e6000sw_writereg_wrapper),
160 DEVMETHOD(etherswitch_readphyreg, e6000sw_readphy_wrapper),
161 DEVMETHOD(etherswitch_writephyreg, e6000sw_writephy_wrapper),
162 DEVMETHOD(etherswitch_setvgroup, e6000sw_setvgroup_wrapper),
163 DEVMETHOD(etherswitch_getvgroup, e6000sw_getvgroup_wrapper),
168 static devclass_t e6000sw_devclass;
170 DEFINE_CLASS_0(e6000sw, e6000sw_driver, e6000sw_methods,
171 sizeof(e6000sw_softc_t));
173 DRIVER_MODULE(e6000sw, mdio, e6000sw_driver, e6000sw_devclass, 0, 0);
174 DRIVER_MODULE(etherswitch, e6000sw, etherswitch_driver, etherswitch_devclass, 0,
176 DRIVER_MODULE(miibus, e6000sw, miibus_driver, miibus_devclass, 0, 0);
177 MODULE_DEPEND(e6000sw, mdio, 1, 1, 1);
180 #define SMI_CMD_BUSY (1 << 15)
181 #define SMI_CMD_OP_READ ((2 << 10) | SMI_CMD_BUSY | (1 << 12))
182 #define SMI_CMD_OP_WRITE ((1 << 10) | SMI_CMD_BUSY | (1 << 12))
185 #define MDIO_READ(dev, addr, reg) \
186 MDIO_READREG(device_get_parent(dev), (addr), (reg))
187 #define MDIO_WRITE(dev, addr, reg, val) \
188 MDIO_WRITEREG(device_get_parent(dev), (addr), (reg), (val))
191 e6000sw_identify(driver_t *driver, device_t parent)
194 if (device_find_child(parent, "e6000sw", -1) == NULL)
195 BUS_ADD_CHILD(parent, 0, "e6000sw", -1);
199 e6000sw_probe(device_t dev)
202 const char *description;
203 phandle_t dsa_node, switch_node;
205 dsa_node = fdt_find_compatible(OF_finddevice("/"),
207 switch_node = OF_child(dsa_node);
209 if (switch_node == 0)
212 sc = device_get_softc(dev);
214 sc->node = switch_node;
216 if (OF_getencprop(sc->node, "reg", &sc->sw_addr,
217 sizeof(sc->sw_addr)) < 0)
219 if (sc->sw_addr != 0 && (sc->sw_addr % 2) == 0)
220 sc->multi_chip = true;
223 * Create temporary lock, just to satisfy assertions,
224 * when obtaining the switch ID. Destroy immediately afterwards.
226 sx_init(&sc->sx, "e6000sw_tmp");
228 sc->swid = e6000sw_readreg(sc, REG_PORT(0), SWITCH_ID) & 0xfff0;
234 description = "Marvell 88E6141";
239 description = "Marvell 88E6341";
244 description = "Marvell 88E6352";
248 description = "Marvell 88E6172";
252 description = "Marvell 88E6176";
256 device_printf(dev, "Unrecognized device, id 0x%x.\n", sc->swid);
260 device_set_desc(dev, description);
262 return (BUS_PROBE_DEFAULT);
266 e6000sw_parse_child_fdt(e6000sw_softc_t *sc, phandle_t child, int *pport,
269 char *name, *portlabel;
271 phandle_t fixed_link;
272 uint32_t port, vlangroup;
274 if (pport == NULL || pvlangroup == NULL)
277 if (OF_getencprop(child, "reg", (void *)&port, sizeof(port)) < 0)
279 if (port >= sc->num_ports)
283 if (OF_getencprop(child, "vlangroup", (void *)&vlangroup,
284 sizeof(vlangroup)) > 0) {
285 if (vlangroup >= E6000SW_NUM_VGROUPS)
287 *pvlangroup = vlangroup;
292 if (OF_getprop_alloc(child, "label", 1, (void **)&portlabel) > 0) {
293 if (strncmp(portlabel, "cpu", 3) == 0) {
294 device_printf(sc->dev, "CPU port at %d\n", port);
295 sc->cpuports_mask |= (1 << port);
296 sc->fixed_mask |= (1 << port);
298 free(portlabel, M_OFWPROP);
301 fixed_link = OF_child(child);
302 if (fixed_link != 0 &&
303 OF_getprop_alloc(fixed_link, "name", 1, (void **)&name) > 0) {
304 if (strncmp(name, "fixed-link", 10) == 0) {
305 /* Assume defaults: 1g - full-duplex. */
306 sc->fixed_mask |= (1 << port);
307 if (OF_getencprop(fixed_link, "speed", &speed,
308 sizeof(speed)) > 0) {
310 (MVSWITCH(sc, MV88E6141) ||
311 MVSWITCH(sc, MV88E6341))) {
312 sc->fixed25_mask |= (1 << port);
316 free(name, M_OFWPROP);
318 if ((sc->fixed_mask & (1 << port)) != 0)
319 device_printf(sc->dev, "fixed port at %d\n", port);
321 device_printf(sc->dev, "PHY at port %d\n", port);
327 e6000sw_init_interface(e6000sw_softc_t *sc, int port)
331 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->dev));
333 sc->ifp[port] = if_alloc(IFT_ETHER);
334 if (sc->ifp[port] == NULL)
336 sc->ifp[port]->if_softc = sc;
337 sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
338 IFF_DRV_RUNNING | IFF_SIMPLEX;
339 sc->ifname[port] = malloc(strlen(name) + 1, M_E6000SW, M_NOWAIT);
340 if (sc->ifname[port] == NULL) {
341 if_free(sc->ifp[port]);
344 memcpy(sc->ifname[port], name, strlen(name) + 1);
345 if_initname(sc->ifp[port], sc->ifname[port], port);
351 e6000sw_attach_miibus(e6000sw_softc_t *sc, int port)
355 err = mii_attach(sc->dev, &sc->miibus[port], sc->ifp[port],
356 e6000sw_ifmedia_upd, e6000sw_ifmedia_sts, BMSR_DEFCAPMASK,
357 port + sc->phy_base, MII_OFFSET_ANY, 0);
365 e6000sw_attach(device_t dev)
367 etherswitch_vlangroup_t vg;
370 int err, port, vlangroup;
371 int member_ports[E6000SW_NUM_VGROUPS];
375 sc = device_get_softc(dev);
378 device_printf(dev, "multi-chip addressing mode\n");
380 device_printf(dev, "single-chip addressing mode\n");
382 sx_init(&sc->sx, "e6000sw");
385 e6000sw_setup(dev, sc);
386 bzero(member_ports, sizeof(member_ports));
388 for (child = OF_child(sc->node); child != 0; child = OF_peer(child)) {
389 err = e6000sw_parse_child_fdt(sc, child, &port, &vlangroup);
391 device_printf(sc->dev, "failed to parse DTS\n");
396 member_ports[vlangroup] |= (1 << port);
398 /* Port is in use. */
399 sc->ports_mask |= (1 << port);
401 err = e6000sw_init_interface(sc, port);
403 device_printf(sc->dev, "failed to init interface\n");
407 if (e6000sw_is_fixedport(sc, port)) {
408 /* Link must be down to change speed force value. */
409 reg = e6000sw_readreg(sc, REG_PORT(port), PSC_CONTROL);
410 reg &= ~PSC_CONTROL_LINK_UP;
411 reg |= PSC_CONTROL_FORCED_LINK;
412 e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
415 * Force speed, full-duplex, EEE off and flow-control
418 if (e6000sw_is_fixed25port(sc, port))
419 reg = PSC_CONTROL_SPD2500;
421 reg = PSC_CONTROL_SPD1000;
422 reg |= PSC_CONTROL_FORCED_DPX | PSC_CONTROL_FULLDPX |
423 PSC_CONTROL_FORCED_LINK | PSC_CONTROL_LINK_UP |
424 PSC_CONTROL_FORCED_FC | PSC_CONTROL_FC_ON |
425 PSC_CONTROL_FORCED_SPD;
426 if (MVSWITCH(sc, MV88E6141) || MVSWITCH(sc, MV88E6341))
427 reg |= PSC_CONTROL_FORCED_EEE;
428 e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
431 /* Don't attach miibus at CPU/fixed ports */
432 if (!e6000sw_is_phyport(sc, port))
435 err = e6000sw_attach_miibus(sc, port);
437 device_printf(sc->dev, "failed to attach miibus\n");
442 etherswitch_info.es_nports = sc->num_ports;
443 for (port = 0; port < sc->num_ports; port++)
444 sc->vgroup[port] = E6000SW_PORT_NO_VGROUP;
446 /* Set VLAN configuration */
447 e6000sw_port_vlan_conf(sc);
450 for (vlangroup = 0; vlangroup < E6000SW_NUM_VGROUPS; vlangroup++)
451 if (member_ports[vlangroup] != 0) {
452 vg.es_vlangroup = vg.es_vid = vlangroup;
453 vg.es_member_ports = vg.es_untagged_ports =
454 member_ports[vlangroup];
455 e6000sw_setvgroup(dev, &vg);
460 bus_generic_probe(dev);
461 bus_generic_attach(dev);
463 kproc_create(e6000sw_tick, sc, &sc->kproc, 0, 0, "e6000sw tick kproc");
475 e6000sw_poll_done(e6000sw_softc_t *sc)
479 for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
481 if ((e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG) &
482 (1 << PHY_CMD_SMI_BUSY)) == 0)
485 pause("e6000sw PHY poll", hz/1000);
492 * PHY registers are paged. Put page index in reg 22 (accessible from every
493 * page), then access specific register.
496 e6000sw_readphy(device_t dev, int phy, int reg)
502 sc = device_get_softc(dev);
503 if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
504 device_printf(dev, "Wrong register address.\n");
508 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
510 err = e6000sw_poll_done(sc);
512 device_printf(dev, "Timeout while waiting for switch\n");
516 val = 1 << PHY_CMD_SMI_BUSY;
517 val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
518 val |= PHY_CMD_OPCODE_READ << PHY_CMD_OPCODE;
519 val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
520 val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
521 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
523 err = e6000sw_poll_done(sc);
525 device_printf(dev, "Timeout while waiting for switch\n");
529 val = e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG);
531 return (val & PHY_DATA_MASK);
535 e6000sw_writephy(device_t dev, int phy, int reg, int data)
541 sc = device_get_softc(dev);
542 if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
543 device_printf(dev, "Wrong register address.\n");
547 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
549 err = e6000sw_poll_done(sc);
551 device_printf(dev, "Timeout while waiting for switch\n");
555 val = 1 << PHY_CMD_SMI_BUSY;
556 val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
557 val |= PHY_CMD_OPCODE_WRITE << PHY_CMD_OPCODE;
558 val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
559 val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
560 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG,
561 data & PHY_DATA_MASK);
562 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
564 err = e6000sw_poll_done(sc);
566 device_printf(dev, "Timeout while waiting for switch\n");
572 e6000sw_detach(device_t dev)
577 sc = device_get_softc(dev);
578 bus_generic_detach(dev);
580 for (phy = 0; phy < sc->num_ports; phy++) {
581 if (sc->miibus[phy] != NULL)
582 device_delete_child(dev, sc->miibus[phy]);
583 if (sc->ifp[phy] != NULL)
584 if_free(sc->ifp[phy]);
585 if (sc->ifname[phy] != NULL)
586 free(sc->ifname[phy], M_E6000SW);
592 static etherswitch_info_t*
593 e6000sw_getinfo(device_t dev)
596 return (ðerswitch_info);
600 e6000sw_getconf(device_t dev __unused, etherswitch_conf_t *conf)
603 /* Return the VLAN mode. */
604 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
605 conf->vlan_mode = ETHERSWITCH_VLAN_PORT;
611 e6000sw_lock(device_t dev)
613 struct e6000sw_softc *sc;
615 sc = device_get_softc(dev);
617 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
622 e6000sw_unlock(device_t dev)
624 struct e6000sw_softc *sc;
626 sc = device_get_softc(dev);
628 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
633 e6000sw_getport(device_t dev, etherswitch_port_t *p)
635 struct mii_data *mii;
637 struct ifmediareq *ifmr;
639 e6000sw_softc_t *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))
649 e6000sw_get_pvid(sc, p->es_port, &p->es_pvid);
651 if (e6000sw_is_fixedport(sc, p->es_port)) {
652 if (e6000sw_is_cpuport(sc, p->es_port))
653 p->es_flags |= ETHERSWITCH_PORT_CPU;
655 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
657 if (e6000sw_is_fixed25port(sc, p->es_port))
658 ifmr->ifm_active = IFM_2500_T;
660 ifmr->ifm_active = IFM_1000_T;
661 ifmr->ifm_active |= IFM_ETHER | IFM_FDX;
662 ifmr->ifm_current = ifmr->ifm_active;
665 mii = e6000sw_miiforphy(sc, p->es_port);
666 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
667 &mii->mii_media, SIOCGIFMEDIA);
675 e6000sw_setport(device_t dev, etherswitch_port_t *p)
679 struct mii_data *mii;
681 sc = device_get_softc(dev);
682 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
684 if (p->es_port >= sc->num_ports || p->es_port < 0)
686 if (!e6000sw_is_portenabled(sc, p->es_port))
692 e6000sw_set_pvid(sc, p->es_port, p->es_pvid);
693 if (e6000sw_is_phyport(sc, p->es_port)) {
694 mii = e6000sw_miiforphy(sc, p->es_port);
695 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, &mii->mii_media,
704 * Registers in this switch are divided into sections, specified in
705 * documentation. So as to access any of them, section index and reg index
706 * is necessary. etherswitchcfg uses only one variable, so indexes were
707 * compressed into addr_reg: 32 * section_index + reg_index.
710 e6000sw_readreg_wrapper(device_t dev, int addr_reg)
713 if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
714 (addr_reg < (REG_PORT(0) * 32))) {
715 device_printf(dev, "Wrong register address.\n");
719 return (e6000sw_readreg(device_get_softc(dev), addr_reg / 32,
724 e6000sw_writereg_wrapper(device_t dev, int addr_reg, int val)
727 if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
728 (addr_reg < (REG_PORT(0) * 32))) {
729 device_printf(dev, "Wrong register address.\n");
732 e6000sw_writereg(device_get_softc(dev), addr_reg / 5,
739 * These wrappers are necessary because PHY accesses from etherswitchcfg
740 * need to be synchronized with locks, while miibus PHY accesses do not.
743 e6000sw_readphy_wrapper(device_t dev, int phy, int reg)
748 sc = device_get_softc(dev);
749 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
752 ret = e6000sw_readphy(dev, phy, reg);
759 e6000sw_writephy_wrapper(device_t dev, int phy, int reg, int data)
764 sc = device_get_softc(dev);
765 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
768 ret = e6000sw_writephy(dev, phy, reg, data);
775 * setvgroup/getvgroup called from etherswitchfcg need to be locked,
776 * while internal calls do not.
779 e6000sw_setvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
784 sc = device_get_softc(dev);
785 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
788 ret = e6000sw_setvgroup(dev, vg);
795 e6000sw_getvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
800 sc = device_get_softc(dev);
801 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
804 ret = e6000sw_getvgroup(dev, vg);
811 e6000sw_flush_port(e6000sw_softc_t *sc, int port)
815 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
816 reg &= ~PORT_VLAN_MAP_TABLE_MASK;
817 reg &= ~PORT_VLAN_MAP_FID_MASK;
818 e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
819 if (sc->vgroup[port] != E6000SW_PORT_NO_VGROUP) {
821 * If port belonged somewhere, owner-group
822 * should have its entry removed.
824 sc->members[sc->vgroup[port]] &= ~(1 << port);
825 sc->vgroup[port] = E6000SW_PORT_NO_VGROUP;
830 e6000sw_port_assign_vgroup(e6000sw_softc_t *sc, int port, int fid, int vgroup,
835 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
836 reg &= ~PORT_VLAN_MAP_TABLE_MASK;
837 reg &= ~PORT_VLAN_MAP_FID_MASK;
838 reg |= members & ~(1 << port);
839 reg |= (fid << PORT_VLAN_MAP_FID) & PORT_VLAN_MAP_FID_MASK;
840 e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
841 sc->vgroup[port] = vgroup;
845 e6000sw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
850 sc = device_get_softc(dev);
851 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
853 if (vg->es_vlangroup >= E6000SW_NUM_VGROUPS)
855 if (vg->es_member_ports != vg->es_untagged_ports) {
856 device_printf(dev, "Tagged ports not supported.\n");
860 vg->es_untagged_ports &= PORT_VLAN_MAP_TABLE_MASK;
861 fid = vg->es_vlangroup + 1;
862 for (port = 0; port < sc->num_ports; port++) {
863 if ((sc->members[sc->vgroup[port]] & (1 << port)))
864 e6000sw_flush_port(sc, port);
865 if (vg->es_untagged_ports & (1 << port))
866 e6000sw_port_assign_vgroup(sc, port, fid,
867 vg->es_vlangroup, vg->es_untagged_ports);
869 sc->vid[vg->es_vlangroup] = vg->es_vid;
870 sc->members[vg->es_vlangroup] = vg->es_untagged_ports;
876 e6000sw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
880 sc = device_get_softc(dev);
881 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
883 if (vg->es_vlangroup >= E6000SW_NUM_VGROUPS)
885 vg->es_untagged_ports = vg->es_member_ports =
886 sc->members[vg->es_vlangroup];
887 if (vg->es_untagged_ports != 0)
888 vg->es_vid = ETHERSWITCH_VID_VALID;
893 static __inline struct mii_data*
894 e6000sw_miiforphy(e6000sw_softc_t *sc, unsigned int phy)
897 if (!e6000sw_is_phyport(sc, phy))
900 return (device_get_softc(sc->miibus[phy]));
904 e6000sw_ifmedia_upd(struct ifnet *ifp)
907 struct mii_data *mii;
910 mii = e6000sw_miiforphy(sc, ifp->if_dunit);
919 e6000sw_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
922 struct mii_data *mii;
925 mii = e6000sw_miiforphy(sc, ifp->if_dunit);
931 ifmr->ifm_active = mii->mii_media_active;
932 ifmr->ifm_status = mii->mii_media_status;
936 e6000sw_smi_waitready(e6000sw_softc_t *sc, int phy)
940 for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
941 if ((MDIO_READ(sc->dev, phy, SMI_CMD) & SMI_CMD_BUSY) == 0)
949 static __inline uint32_t
950 e6000sw_readreg(e6000sw_softc_t *sc, int addr, int reg)
953 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
956 return (MDIO_READ(sc->dev, addr, reg) & 0xffff);
958 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
959 printf("e6000sw: readreg timeout\n");
962 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
963 SMI_CMD_OP_READ | (addr << 5) | reg);
964 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
965 printf("e6000sw: readreg timeout\n");
969 return (MDIO_READ(sc->dev, sc->sw_addr, SMI_DATA) & 0xffff);
973 e6000sw_writereg(e6000sw_softc_t *sc, int addr, int reg, int val)
976 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
978 if (!sc->multi_chip) {
979 MDIO_WRITE(sc->dev, addr, reg, val);
983 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
984 printf("e6000sw: readreg timeout\n");
987 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_DATA, val);
988 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
989 SMI_CMD_OP_WRITE | (addr << 5) | reg);
990 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
991 printf("e6000sw: readreg timeout\n");
997 e6000sw_is_cpuport(e6000sw_softc_t *sc, int port)
1000 return ((sc->cpuports_mask & (1 << port)) ? true : false);
1003 static __inline bool
1004 e6000sw_is_fixedport(e6000sw_softc_t *sc, int port)
1007 return ((sc->fixed_mask & (1 << port)) ? true : false);
1010 static __inline bool
1011 e6000sw_is_fixed25port(e6000sw_softc_t *sc, int port)
1014 return ((sc->fixed25_mask & (1 << port)) ? true : false);
1017 static __inline bool
1018 e6000sw_is_phyport(e6000sw_softc_t *sc, int port)
1021 phy_mask = ~(sc->fixed_mask | sc->cpuports_mask);
1023 return ((phy_mask & (1 << port)) ? true : false);
1026 static __inline bool
1027 e6000sw_is_portenabled(e6000sw_softc_t *sc, int port)
1030 return ((sc->ports_mask & (1 << port)) ? true : false);
1034 e6000sw_set_pvid(e6000sw_softc_t *sc, int port, int pvid)
1037 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, pvid &
1038 PORT_VID_DEF_VID_MASK);
1044 e6000sw_get_pvid(e6000sw_softc_t *sc, int port, int *pvid)
1050 *pvid = e6000sw_readreg(sc, REG_PORT(port), PORT_VID) &
1051 PORT_VID_DEF_VID_MASK;
1057 * Convert port status to ifmedia.
1060 e6000sw_update_ifmedia(uint16_t portstatus, u_int *media_status, u_int *media_active)
1062 *media_active = IFM_ETHER;
1063 *media_status = IFM_AVALID;
1065 if ((portstatus & PORT_STATUS_LINK_MASK) != 0)
1066 *media_status |= IFM_ACTIVE;
1068 *media_active |= IFM_NONE;
1072 switch (portstatus & PORT_STATUS_SPEED_MASK) {
1073 case PORT_STATUS_SPEED_10:
1074 *media_active |= IFM_10_T;
1076 case PORT_STATUS_SPEED_100:
1077 *media_active |= IFM_100_TX;
1079 case PORT_STATUS_SPEED_1000:
1080 *media_active |= IFM_1000_T;
1084 if ((portstatus & PORT_STATUS_DUPLEX_MASK) == 0)
1085 *media_active |= IFM_FDX;
1087 *media_active |= IFM_HDX;
1091 e6000sw_tick (void *arg)
1093 e6000sw_softc_t *sc;
1094 struct mii_data *mii;
1095 struct mii_softc *miisc;
1096 uint16_t portstatus;
1101 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
1105 for (port = 0; port < sc->num_ports; port++) {
1106 /* Tick only on PHY ports */
1107 if (!e6000sw_is_portenabled(sc, port) ||
1108 !e6000sw_is_phyport(sc, port))
1111 mii = e6000sw_miiforphy(sc, port);
1115 portstatus = e6000sw_readreg(sc, REG_PORT(port),
1118 e6000sw_update_ifmedia(portstatus,
1119 &mii->mii_media_status, &mii->mii_media_active);
1121 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1122 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media)
1125 mii_phy_update(miisc, MII_POLLSTAT);
1129 pause("e6000sw tick", 1000);
1134 e6000sw_setup(device_t dev, e6000sw_softc_t *sc)
1136 uint16_t atu_ctrl, atu_age;
1138 /* Set aging time */
1139 e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL,
1140 (E6000SW_DEFAULT_AGETIME << ATU_CONTROL_AGETIME) |
1141 (1 << ATU_CONTROL_LEARN2ALL));
1143 /* Send all with specific mac address to cpu port */
1144 e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_2x, MGMT_EN_ALL);
1145 e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_0x, MGMT_EN_ALL);
1147 /* Disable Remote Management */
1148 e6000sw_writereg(sc, REG_GLOBAL, SWITCH_GLOBAL_CONTROL2, 0);
1150 /* Disable loopback filter and flow control messages */
1151 e6000sw_writereg(sc, REG_GLOBAL2, SWITCH_MGMT,
1152 SWITCH_MGMT_PRI_MASK |
1153 (1 << SWITCH_MGMT_RSVD2CPU) |
1154 SWITCH_MGMT_FC_PRI_MASK |
1155 (1 << SWITCH_MGMT_FORCEFLOW));
1157 e6000sw_atu_flush(dev, sc, NO_OPERATION);
1158 e6000sw_atu_mac_table(dev, sc, NULL, NO_OPERATION);
1159 e6000sw_set_atustat(dev, sc, 0, COUNT_ALL);
1161 /* Set ATU AgeTime to 15 seconds */
1164 atu_ctrl = e6000sw_readreg(sc, REG_GLOBAL, ATU_CONTROL);
1166 /* Set new AgeTime field */
1167 atu_ctrl &= ~ATU_CONTROL_AGETIME_MASK;
1168 e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL, atu_ctrl |
1169 (atu_age << ATU_CONTROL_AGETIME));
1173 e6000sw_port_vlan_conf(e6000sw_softc_t *sc)
1179 /* Disable all ports */
1180 for (port = 0; port < sc->num_ports; port++) {
1181 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1182 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
1183 (ret & ~PORT_CONTROL_ENABLE));
1186 /* Set port priority */
1187 for (port = 0; port < sc->num_ports; port++) {
1188 if (!e6000sw_is_portenabled(sc, port))
1190 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1191 ret &= ~PORT_VID_PRIORITY_MASK;
1192 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1196 for (port = 0; port < sc->num_ports; port++) {
1197 if (!e6000sw_is_portenabled(sc, port))
1199 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1200 ret &= ~PORT_VID_DEF_VID_MASK;
1202 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1205 /* Enable all ports */
1206 for (port = 0; port < sc->num_ports; port++) {
1207 if (!e6000sw_is_portenabled(sc, port))
1209 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1210 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL, (ret |
1211 PORT_CONTROL_ENABLE));
1216 e6000sw_set_atustat(device_t dev, e6000sw_softc_t *sc, int bin, int flag)
1220 ret = e6000sw_readreg(sc, REG_GLOBAL2, ATU_STATS);
1221 e6000sw_writereg(sc, REG_GLOBAL2, ATU_STATS, (bin << ATU_STATS_BIN ) |
1222 (flag << ATU_STATS_FLAG));
1226 e6000sw_atu_mac_table(device_t dev, e6000sw_softc_t *sc, struct atu_opt *atu,
1233 if (flag == NO_OPERATION)
1235 else if ((flag & (LOAD_FROM_FIB | PURGE_FROM_FIB | GET_NEXT_IN_FIB |
1236 GET_VIOLATION_DATA | CLEAR_VIOLATION_DATA)) == 0) {
1237 device_printf(dev, "Wrong Opcode for ATU operation\n");
1241 ret_opt = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1243 if (ret_opt & ATU_UNIT_BUSY) {
1244 device_printf(dev, "ATU unit is busy, cannot access"
1248 if(flag & LOAD_FROM_FIB) {
1249 ret_data = e6000sw_readreg(sc, REG_GLOBAL, ATU_DATA);
1250 e6000sw_writereg(sc, REG_GLOBAL2, ATU_DATA, (ret_data &
1253 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR01, atu->mac_01);
1254 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR23, atu->mac_23);
1255 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR45, atu->mac_45);
1256 e6000sw_writereg(sc, REG_GLOBAL, ATU_FID, atu->fid);
1258 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret_opt |
1259 ATU_UNIT_BUSY | flag));
1261 retries = E6000SW_RETRIES;
1262 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1263 ATU_OPERATION) & ATU_UNIT_BUSY))
1267 device_printf(dev, "Timeout while flushing\n");
1268 else if (flag & GET_NEXT_IN_FIB) {
1269 atu->mac_01 = e6000sw_readreg(sc, REG_GLOBAL,
1271 atu->mac_23 = e6000sw_readreg(sc, REG_GLOBAL,
1273 atu->mac_45 = e6000sw_readreg(sc, REG_GLOBAL,
1282 e6000sw_atu_flush(device_t dev, e6000sw_softc_t *sc, int flag)
1287 if (flag == NO_OPERATION)
1290 ret = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1291 if (ret & ATU_UNIT_BUSY) {
1292 device_printf(dev, "Atu unit is busy, cannot flush\n");
1295 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret |
1296 ATU_UNIT_BUSY | flag));
1297 retries = E6000SW_RETRIES;
1298 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1299 ATU_OPERATION) & ATU_UNIT_BUSY))
1303 device_printf(dev, "Timeout while flushing\n");