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/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.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];
76 uint32_t cpuports_mask;
78 uint32_t fixed25_mask;
86 static etherswitch_info_t etherswitch_info = {
89 .es_vlan_caps = ETHERSWITCH_VLAN_PORT,
90 .es_name = "Marvell 6000 series switch"
93 static void e6000sw_identify(driver_t *, device_t);
94 static int e6000sw_probe(device_t);
95 static int e6000sw_parse_fixed_link(e6000sw_softc_t *, phandle_t, uint32_t);
96 static int e6000sw_parse_ethernet(e6000sw_softc_t *, phandle_t, uint32_t);
97 static int e6000sw_attach(device_t);
98 static int e6000sw_detach(device_t);
99 static int e6000sw_readphy(device_t, int, int);
100 static int e6000sw_writephy(device_t, int, int, int);
101 static etherswitch_info_t* e6000sw_getinfo(device_t);
102 static int e6000sw_getconf(device_t, etherswitch_conf_t *);
103 static void e6000sw_lock(device_t);
104 static void e6000sw_unlock(device_t);
105 static int e6000sw_getport(device_t, etherswitch_port_t *);
106 static int e6000sw_setport(device_t, etherswitch_port_t *);
107 static int e6000sw_readreg_wrapper(device_t, int);
108 static int e6000sw_writereg_wrapper(device_t, int, int);
109 static int e6000sw_readphy_wrapper(device_t, int, int);
110 static int e6000sw_writephy_wrapper(device_t, int, int, int);
111 static int e6000sw_getvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
112 static int e6000sw_setvgroup_wrapper(device_t, etherswitch_vlangroup_t *);
113 static int e6000sw_setvgroup(device_t, etherswitch_vlangroup_t *);
114 static int e6000sw_getvgroup(device_t, etherswitch_vlangroup_t *);
115 static void e6000sw_setup(device_t, e6000sw_softc_t *);
116 static void e6000sw_port_vlan_conf(e6000sw_softc_t *);
117 static void e6000sw_tick(void *);
118 static void e6000sw_set_atustat(device_t, e6000sw_softc_t *, int, int);
119 static int e6000sw_atu_flush(device_t, e6000sw_softc_t *, int);
120 static __inline void e6000sw_writereg(e6000sw_softc_t *, int, int, int);
121 static __inline uint32_t e6000sw_readreg(e6000sw_softc_t *, int, int);
122 static int e6000sw_ifmedia_upd(struct ifnet *);
123 static void e6000sw_ifmedia_sts(struct ifnet *, struct ifmediareq *);
124 static int e6000sw_atu_mac_table(device_t, e6000sw_softc_t *, struct atu_opt *,
126 static int e6000sw_get_pvid(e6000sw_softc_t *, int, int *);
127 static int e6000sw_set_pvid(e6000sw_softc_t *, int, int);
128 static __inline bool e6000sw_is_cpuport(e6000sw_softc_t *, int);
129 static __inline bool e6000sw_is_fixedport(e6000sw_softc_t *, int);
130 static __inline bool e6000sw_is_fixed25port(e6000sw_softc_t *, int);
131 static __inline bool e6000sw_is_phyport(e6000sw_softc_t *, int);
132 static __inline bool e6000sw_is_portenabled(e6000sw_softc_t *, int);
133 static __inline struct mii_data *e6000sw_miiforphy(e6000sw_softc_t *,
136 static device_method_t e6000sw_methods[] = {
137 /* device interface */
138 DEVMETHOD(device_identify, e6000sw_identify),
139 DEVMETHOD(device_probe, e6000sw_probe),
140 DEVMETHOD(device_attach, e6000sw_attach),
141 DEVMETHOD(device_detach, e6000sw_detach),
144 DEVMETHOD(bus_add_child, device_add_child_ordered),
147 DEVMETHOD(miibus_readreg, e6000sw_readphy),
148 DEVMETHOD(miibus_writereg, e6000sw_writephy),
150 /* etherswitch interface */
151 DEVMETHOD(etherswitch_getinfo, e6000sw_getinfo),
152 DEVMETHOD(etherswitch_getconf, e6000sw_getconf),
153 DEVMETHOD(etherswitch_lock, e6000sw_lock),
154 DEVMETHOD(etherswitch_unlock, e6000sw_unlock),
155 DEVMETHOD(etherswitch_getport, e6000sw_getport),
156 DEVMETHOD(etherswitch_setport, e6000sw_setport),
157 DEVMETHOD(etherswitch_readreg, e6000sw_readreg_wrapper),
158 DEVMETHOD(etherswitch_writereg, e6000sw_writereg_wrapper),
159 DEVMETHOD(etherswitch_readphyreg, e6000sw_readphy_wrapper),
160 DEVMETHOD(etherswitch_writephyreg, e6000sw_writephy_wrapper),
161 DEVMETHOD(etherswitch_setvgroup, e6000sw_setvgroup_wrapper),
162 DEVMETHOD(etherswitch_getvgroup, e6000sw_getvgroup_wrapper),
167 static devclass_t e6000sw_devclass;
169 DEFINE_CLASS_0(e6000sw, e6000sw_driver, e6000sw_methods,
170 sizeof(e6000sw_softc_t));
172 DRIVER_MODULE(e6000sw, mdio, e6000sw_driver, e6000sw_devclass, 0, 0);
173 DRIVER_MODULE(etherswitch, e6000sw, etherswitch_driver, etherswitch_devclass, 0,
175 DRIVER_MODULE(miibus, e6000sw, miibus_driver, miibus_devclass, 0, 0);
176 MODULE_DEPEND(e6000sw, mdio, 1, 1, 1);
179 #define SMI_CMD_BUSY (1 << 15)
180 #define SMI_CMD_OP_READ ((2 << 10) | SMI_CMD_BUSY | (1 << 12))
181 #define SMI_CMD_OP_WRITE ((1 << 10) | SMI_CMD_BUSY | (1 << 12))
184 #define MDIO_READ(dev, addr, reg) \
185 MDIO_READREG(device_get_parent(dev), (addr), (reg))
186 #define MDIO_WRITE(dev, addr, reg, val) \
187 MDIO_WRITEREG(device_get_parent(dev), (addr), (reg), (val))
190 e6000sw_identify(driver_t *driver, device_t parent)
193 if (device_find_child(parent, "e6000sw", -1) == NULL)
194 BUS_ADD_CHILD(parent, 0, "e6000sw", -1);
198 e6000sw_probe(device_t dev)
201 const char *description;
202 phandle_t switch_node;
204 switch_node = ofw_bus_find_compatible(OF_finddevice("/"),
205 "marvell,mv88e6085");
207 if (switch_node == 0)
211 device_printf(dev, "Found switch_node: 0x%x\n", switch_node);
213 sc = device_get_softc(dev);
215 sc->node = switch_node;
217 if (OF_getencprop(sc->node, "reg", &sc->sw_addr,
218 sizeof(sc->sw_addr)) < 0)
222 * According to the Linux source code, all of the Switch IDs we support
223 * are multi_chip capable, and should go into multi-chip mode if the
226 if (!OF_hasprop(sc->node, "single-chip-addressing") && sc->sw_addr != 0)
227 sc->multi_chip = true;
230 * Create temporary lock, just to satisfy assertions,
231 * when obtaining the switch ID. Destroy immediately afterwards.
233 sx_init(&sc->sx, "e6000sw_tmp");
235 sc->swid = e6000sw_readreg(sc, REG_PORT(0), SWITCH_ID) & 0xfff0;
241 description = "Marvell 88E6141";
246 description = "Marvell 88E6341";
251 description = "Marvell 88E6352";
255 description = "Marvell 88E6172";
259 description = "Marvell 88E6176";
263 device_printf(dev, "Unrecognized device, id 0x%x.\n", sc->swid);
267 device_set_desc(dev, description);
269 return (BUS_PROBE_DEFAULT);
273 e6000sw_parse_fixed_link(e6000sw_softc_t *sc, phandle_t node, uint32_t port)
276 phandle_t fixed_link;
278 fixed_link = ofw_bus_find_child(node, "fixed-link");
280 if (fixed_link != 0) {
281 sc->fixed_mask |= (1 << port);
283 if (OF_getencprop(fixed_link, "speed", &speed, sizeof(speed))> 0) {
285 (MVSWITCH(sc, MV88E6141) ||
286 MVSWITCH(sc, MV88E6341)))
287 sc->fixed25_mask |= (1 << port);
289 device_printf(sc->dev,
290 "Port %d has a fixed-link node without a speed "
301 e6000sw_parse_ethernet(e6000sw_softc_t *sc, phandle_t port_handle, uint32_t port) {
302 phandle_t switch_eth, switch_eth_handle;
304 if (OF_getencprop(port_handle, "ethernet", (void*)&switch_eth_handle,
305 sizeof(switch_eth_handle)) > 0) {
306 if (switch_eth_handle > 0) {
307 switch_eth = OF_node_from_xref(switch_eth_handle);
309 device_printf(sc->dev, "CPU port at %d\n", port);
310 sc->cpuports_mask |= (1 << port);
312 return (e6000sw_parse_fixed_link(sc, switch_eth, port));
314 device_printf(sc->dev,
315 "Port %d has ethernet property but it points "
316 "to an invalid location\n", port);
323 e6000sw_parse_child_fdt(e6000sw_softc_t *sc, phandle_t child, int *pport)
330 if (OF_getencprop(child, "reg", (void *)&port, sizeof(port)) < 0)
332 if (port >= sc->num_ports)
336 if (e6000sw_parse_fixed_link(sc, child, port) != 0)
339 if (e6000sw_parse_ethernet(sc, child, port) != 0)
342 if ((sc->fixed_mask & (1 << port)) != 0)
343 device_printf(sc->dev, "fixed port at %d\n", port);
345 device_printf(sc->dev, "PHY at port %d\n", port);
351 e6000sw_init_interface(e6000sw_softc_t *sc, int port)
355 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->dev));
357 sc->ifp[port] = if_alloc(IFT_ETHER);
358 if (sc->ifp[port] == NULL)
360 sc->ifp[port]->if_softc = sc;
361 sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
362 IFF_DRV_RUNNING | IFF_SIMPLEX;
363 sc->ifname[port] = malloc(strlen(name) + 1, M_E6000SW, M_NOWAIT);
364 if (sc->ifname[port] == NULL) {
365 if_free(sc->ifp[port]);
368 memcpy(sc->ifname[port], name, strlen(name) + 1);
369 if_initname(sc->ifp[port], sc->ifname[port], port);
375 e6000sw_attach_miibus(e6000sw_softc_t *sc, int port)
379 err = mii_attach(sc->dev, &sc->miibus[port], sc->ifp[port],
380 e6000sw_ifmedia_upd, e6000sw_ifmedia_sts, BMSR_DEFCAPMASK,
381 port + sc->phy_base, MII_OFFSET_ANY, 0);
389 e6000sw_attach(device_t dev)
392 phandle_t child, ports;
397 sc = device_get_softc(dev);
400 device_printf(dev, "multi-chip addressing mode\n");
402 device_printf(dev, "single-chip addressing mode\n");
404 sx_init(&sc->sx, "e6000sw");
407 e6000sw_setup(dev, sc);
409 ports = ofw_bus_find_child(sc->node, "ports");
412 device_printf(dev, "failed to parse DTS: no ports found for "
417 for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
418 err = e6000sw_parse_child_fdt(sc, child, &port);
420 device_printf(sc->dev, "failed to parse DTS\n");
424 /* Port is in use. */
425 sc->ports_mask |= (1 << port);
427 err = e6000sw_init_interface(sc, port);
429 device_printf(sc->dev, "failed to init interface\n");
433 if (e6000sw_is_fixedport(sc, port)) {
434 /* Link must be down to change speed force value. */
435 reg = e6000sw_readreg(sc, REG_PORT(port), PSC_CONTROL);
436 reg &= ~PSC_CONTROL_LINK_UP;
437 reg |= PSC_CONTROL_FORCED_LINK;
438 e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
441 * Force speed, full-duplex, EEE off and flow-control
444 reg &= ~(PSC_CONTROL_SPD2500 | PSC_CONTROL_ALT_SPD |
445 PSC_CONTROL_FORCED_EEE);
446 if (e6000sw_is_fixed25port(sc, port))
447 reg |= PSC_CONTROL_SPD2500;
449 reg |= PSC_CONTROL_SPD1000;
450 reg |= PSC_CONTROL_FORCED_DPX | PSC_CONTROL_FULLDPX |
451 PSC_CONTROL_FORCED_LINK | PSC_CONTROL_LINK_UP |
452 PSC_CONTROL_FORCED_FC | PSC_CONTROL_FC_ON |
453 PSC_CONTROL_FORCED_SPD;
454 if (MVSWITCH(sc, MV88E6141) || MVSWITCH(sc, MV88E6341))
455 reg |= PSC_CONTROL_FORCED_EEE;
456 e6000sw_writereg(sc, REG_PORT(port), PSC_CONTROL, reg);
459 /* Don't attach miibus at CPU/fixed ports */
460 if (!e6000sw_is_phyport(sc, port))
463 err = e6000sw_attach_miibus(sc, port);
465 device_printf(sc->dev, "failed to attach miibus\n");
470 etherswitch_info.es_nports = sc->num_ports;
472 /* Default to port vlan. */
473 e6000sw_port_vlan_conf(sc);
476 bus_generic_probe(dev);
477 bus_generic_attach(dev);
479 kproc_create(e6000sw_tick, sc, &sc->kproc, 0, 0, "e6000sw tick kproc");
491 e6000sw_poll_done(e6000sw_softc_t *sc)
495 for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
497 if ((e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG) &
498 (1 << PHY_CMD_SMI_BUSY)) == 0)
501 pause("e6000sw PHY poll", hz/1000);
508 * PHY registers are paged. Put page index in reg 22 (accessible from every
509 * page), then access specific register.
512 e6000sw_readphy(device_t dev, int phy, int reg)
518 sc = device_get_softc(dev);
519 if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
520 device_printf(dev, "Wrong register address.\n");
524 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
526 err = e6000sw_poll_done(sc);
528 device_printf(dev, "Timeout while waiting for switch\n");
532 val = 1 << PHY_CMD_SMI_BUSY;
533 val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
534 val |= PHY_CMD_OPCODE_READ << PHY_CMD_OPCODE;
535 val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
536 val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
537 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
539 err = e6000sw_poll_done(sc);
541 device_printf(dev, "Timeout while waiting for switch\n");
545 val = e6000sw_readreg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG);
547 return (val & PHY_DATA_MASK);
551 e6000sw_writephy(device_t dev, int phy, int reg, int data)
557 sc = device_get_softc(dev);
558 if (!e6000sw_is_phyport(sc, phy) || reg >= E6000SW_NUM_PHY_REGS) {
559 device_printf(dev, "Wrong register address.\n");
563 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
565 err = e6000sw_poll_done(sc);
567 device_printf(dev, "Timeout while waiting for switch\n");
571 val = 1 << PHY_CMD_SMI_BUSY;
572 val |= PHY_CMD_MODE_MDIO << PHY_CMD_MODE;
573 val |= PHY_CMD_OPCODE_WRITE << PHY_CMD_OPCODE;
574 val |= (reg << PHY_CMD_REG_ADDR) & PHY_CMD_REG_ADDR_MASK;
575 val |= (phy << PHY_CMD_DEV_ADDR) & PHY_CMD_DEV_ADDR_MASK;
576 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_DATA_REG,
577 data & PHY_DATA_MASK);
578 e6000sw_writereg(sc, REG_GLOBAL2, SMI_PHY_CMD_REG, val);
580 err = e6000sw_poll_done(sc);
582 device_printf(dev, "Timeout while waiting for switch\n");
588 e6000sw_detach(device_t dev)
593 sc = device_get_softc(dev);
594 bus_generic_detach(dev);
596 for (phy = 0; phy < sc->num_ports; phy++) {
597 if (sc->miibus[phy] != NULL)
598 device_delete_child(dev, sc->miibus[phy]);
599 if (sc->ifp[phy] != NULL)
600 if_free(sc->ifp[phy]);
601 if (sc->ifname[phy] != NULL)
602 free(sc->ifname[phy], M_E6000SW);
608 static etherswitch_info_t*
609 e6000sw_getinfo(device_t dev)
612 return (ðerswitch_info);
616 e6000sw_getconf(device_t dev, etherswitch_conf_t *conf)
618 struct e6000sw_softc *sc;
620 /* Return the VLAN mode. */
621 sc = device_get_softc(dev);
622 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
623 conf->vlan_mode = sc->vlan_mode;
629 e6000sw_lock(device_t dev)
631 struct e6000sw_softc *sc;
633 sc = device_get_softc(dev);
635 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
640 e6000sw_unlock(device_t dev)
642 struct e6000sw_softc *sc;
644 sc = device_get_softc(dev);
646 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
651 e6000sw_getport(device_t dev, etherswitch_port_t *p)
653 struct mii_data *mii;
655 struct ifmediareq *ifmr;
657 e6000sw_softc_t *sc = device_get_softc(dev);
658 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
660 if (p->es_port >= sc->num_ports || p->es_port < 0)
662 if (!e6000sw_is_portenabled(sc, p->es_port))
667 e6000sw_get_pvid(sc, p->es_port, &p->es_pvid);
669 if (e6000sw_is_fixedport(sc, p->es_port)) {
670 if (e6000sw_is_cpuport(sc, p->es_port))
671 p->es_flags |= ETHERSWITCH_PORT_CPU;
673 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
675 if (e6000sw_is_fixed25port(sc, p->es_port))
676 ifmr->ifm_active = IFM_2500_T;
678 ifmr->ifm_active = IFM_1000_T;
679 ifmr->ifm_active |= IFM_ETHER | IFM_FDX;
680 ifmr->ifm_current = ifmr->ifm_active;
683 mii = e6000sw_miiforphy(sc, p->es_port);
684 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
685 &mii->mii_media, SIOCGIFMEDIA);
693 e6000sw_setport(device_t dev, etherswitch_port_t *p)
697 struct mii_data *mii;
699 sc = device_get_softc(dev);
700 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
702 if (p->es_port >= sc->num_ports || p->es_port < 0)
704 if (!e6000sw_is_portenabled(sc, p->es_port))
710 e6000sw_set_pvid(sc, p->es_port, p->es_pvid);
711 if (e6000sw_is_phyport(sc, p->es_port)) {
712 mii = e6000sw_miiforphy(sc, p->es_port);
713 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr, &mii->mii_media,
722 * Registers in this switch are divided into sections, specified in
723 * documentation. So as to access any of them, section index and reg index
724 * is necessary. etherswitchcfg uses only one variable, so indexes were
725 * compressed into addr_reg: 32 * section_index + reg_index.
728 e6000sw_readreg_wrapper(device_t dev, int addr_reg)
731 if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
732 (addr_reg < (REG_PORT(0) * 32))) {
733 device_printf(dev, "Wrong register address.\n");
737 return (e6000sw_readreg(device_get_softc(dev), addr_reg / 32,
742 e6000sw_writereg_wrapper(device_t dev, int addr_reg, int val)
745 if ((addr_reg > (REG_GLOBAL2 * 32 + REG_NUM_MAX)) ||
746 (addr_reg < (REG_PORT(0) * 32))) {
747 device_printf(dev, "Wrong register address.\n");
750 e6000sw_writereg(device_get_softc(dev), addr_reg / 5,
757 * These wrappers are necessary because PHY accesses from etherswitchcfg
758 * need to be synchronized with locks, while miibus PHY accesses do not.
761 e6000sw_readphy_wrapper(device_t dev, int phy, int reg)
766 sc = device_get_softc(dev);
767 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
770 ret = e6000sw_readphy(dev, phy, reg);
777 e6000sw_writephy_wrapper(device_t dev, int phy, int reg, int data)
782 sc = device_get_softc(dev);
783 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
786 ret = e6000sw_writephy(dev, phy, reg, data);
793 * setvgroup/getvgroup called from etherswitchfcg need to be locked,
794 * while internal calls do not.
797 e6000sw_setvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
802 sc = device_get_softc(dev);
803 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
806 ret = e6000sw_setvgroup(dev, vg);
813 e6000sw_getvgroup_wrapper(device_t dev, etherswitch_vlangroup_t *vg)
818 sc = device_get_softc(dev);
819 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
822 ret = e6000sw_getvgroup(dev, vg);
829 e6000sw_port_vlan_assign(e6000sw_softc_t *sc, int port, uint32_t fid,
834 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
835 reg &= ~PORT_VLAN_MAP_TABLE_MASK;
836 reg &= ~PORT_VLAN_MAP_FID_MASK;
837 reg |= members & PORT_VLAN_MAP_TABLE_MASK & ~(1 << port);
838 reg |= (fid << PORT_VLAN_MAP_FID) & PORT_VLAN_MAP_FID_MASK;
839 e6000sw_writereg(sc, REG_PORT(port), PORT_VLAN_MAP, reg);
840 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL_1);
841 reg &= ~PORT_CONTROL_1_FID_MASK;
842 reg |= (fid >> 4) & PORT_CONTROL_1_FID_MASK;
843 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL_1, reg);
847 e6000sw_set_port_vlan(e6000sw_softc_t *sc, etherswitch_vlangroup_t *vg)
851 port = vg->es_vlangroup;
852 if (port > sc->num_ports)
855 if (vg->es_member_ports != vg->es_untagged_ports) {
856 device_printf(sc->dev, "Tagged ports not supported.\n");
860 e6000sw_port_vlan_assign(sc, port, port + 1, vg->es_untagged_ports);
861 vg->es_vid = port | ETHERSWITCH_VID_VALID;
867 e6000sw_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
871 sc = device_get_softc(dev);
872 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
874 if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT)
875 return (e6000sw_set_port_vlan(sc, vg));
881 e6000sw_get_port_vlan(e6000sw_softc_t *sc, etherswitch_vlangroup_t *vg)
885 port = vg->es_vlangroup;
886 if (port > sc->num_ports)
889 if (!e6000sw_is_portenabled(sc, port)) {
894 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_VLAN_MAP);
895 vg->es_untagged_ports = vg->es_member_ports =
896 reg & PORT_VLAN_MAP_TABLE_MASK;
897 vg->es_vid = port | ETHERSWITCH_VID_VALID;
898 vg->es_fid = (reg & PORT_VLAN_MAP_FID_MASK) >> PORT_VLAN_MAP_FID;
899 reg = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL_1);
900 vg->es_fid |= (reg & PORT_CONTROL_1_FID_MASK) << 4;
906 e6000sw_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
910 sc = device_get_softc(dev);
911 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
913 if (sc->vlan_mode == ETHERSWITCH_VLAN_PORT)
914 return (e6000sw_get_port_vlan(sc, vg));
919 static __inline struct mii_data*
920 e6000sw_miiforphy(e6000sw_softc_t *sc, unsigned int phy)
923 if (!e6000sw_is_phyport(sc, phy))
926 return (device_get_softc(sc->miibus[phy]));
930 e6000sw_ifmedia_upd(struct ifnet *ifp)
933 struct mii_data *mii;
936 mii = e6000sw_miiforphy(sc, ifp->if_dunit);
945 e6000sw_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
948 struct mii_data *mii;
951 mii = e6000sw_miiforphy(sc, ifp->if_dunit);
957 ifmr->ifm_active = mii->mii_media_active;
958 ifmr->ifm_status = mii->mii_media_status;
962 e6000sw_smi_waitready(e6000sw_softc_t *sc, int phy)
966 for (i = 0; i < E6000SW_SMI_TIMEOUT; i++) {
967 if ((MDIO_READ(sc->dev, phy, SMI_CMD) & SMI_CMD_BUSY) == 0)
975 static __inline uint32_t
976 e6000sw_readreg(e6000sw_softc_t *sc, int addr, int reg)
979 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
982 return (MDIO_READ(sc->dev, addr, reg) & 0xffff);
984 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
985 printf("e6000sw: readreg timeout\n");
988 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
989 SMI_CMD_OP_READ | (addr << 5) | reg);
990 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
991 printf("e6000sw: readreg timeout\n");
995 return (MDIO_READ(sc->dev, sc->sw_addr, SMI_DATA) & 0xffff);
999 e6000sw_writereg(e6000sw_softc_t *sc, int addr, int reg, int val)
1002 E6000SW_LOCK_ASSERT(sc, SA_XLOCKED);
1004 if (!sc->multi_chip) {
1005 MDIO_WRITE(sc->dev, addr, reg, val);
1009 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
1010 printf("e6000sw: readreg timeout\n");
1013 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_DATA, val);
1014 MDIO_WRITE(sc->dev, sc->sw_addr, SMI_CMD,
1015 SMI_CMD_OP_WRITE | (addr << 5) | reg);
1016 if (e6000sw_smi_waitready(sc, sc->sw_addr)) {
1017 printf("e6000sw: readreg timeout\n");
1022 static __inline bool
1023 e6000sw_is_cpuport(e6000sw_softc_t *sc, int port)
1026 return ((sc->cpuports_mask & (1 << port)) ? true : false);
1029 static __inline bool
1030 e6000sw_is_fixedport(e6000sw_softc_t *sc, int port)
1033 return ((sc->fixed_mask & (1 << port)) ? true : false);
1036 static __inline bool
1037 e6000sw_is_fixed25port(e6000sw_softc_t *sc, int port)
1040 return ((sc->fixed25_mask & (1 << port)) ? true : false);
1043 static __inline bool
1044 e6000sw_is_phyport(e6000sw_softc_t *sc, int port)
1047 phy_mask = ~(sc->fixed_mask | sc->cpuports_mask);
1049 return ((phy_mask & (1 << port)) ? true : false);
1052 static __inline bool
1053 e6000sw_is_portenabled(e6000sw_softc_t *sc, int port)
1056 return ((sc->ports_mask & (1 << port)) ? true : false);
1060 e6000sw_set_pvid(e6000sw_softc_t *sc, int port, int pvid)
1063 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, pvid &
1064 PORT_VID_DEF_VID_MASK);
1070 e6000sw_get_pvid(e6000sw_softc_t *sc, int port, int *pvid)
1076 *pvid = e6000sw_readreg(sc, REG_PORT(port), PORT_VID) &
1077 PORT_VID_DEF_VID_MASK;
1083 * Convert port status to ifmedia.
1086 e6000sw_update_ifmedia(uint16_t portstatus, u_int *media_status, u_int *media_active)
1088 *media_active = IFM_ETHER;
1089 *media_status = IFM_AVALID;
1091 if ((portstatus & PORT_STATUS_LINK_MASK) != 0)
1092 *media_status |= IFM_ACTIVE;
1094 *media_active |= IFM_NONE;
1098 switch (portstatus & PORT_STATUS_SPEED_MASK) {
1099 case PORT_STATUS_SPEED_10:
1100 *media_active |= IFM_10_T;
1102 case PORT_STATUS_SPEED_100:
1103 *media_active |= IFM_100_TX;
1105 case PORT_STATUS_SPEED_1000:
1106 *media_active |= IFM_1000_T;
1110 if ((portstatus & PORT_STATUS_DUPLEX_MASK) == 0)
1111 *media_active |= IFM_FDX;
1113 *media_active |= IFM_HDX;
1117 e6000sw_tick (void *arg)
1119 e6000sw_softc_t *sc;
1120 struct mii_data *mii;
1121 struct mii_softc *miisc;
1122 uint16_t portstatus;
1127 E6000SW_LOCK_ASSERT(sc, SA_UNLOCKED);
1131 for (port = 0; port < sc->num_ports; port++) {
1132 /* Tick only on PHY ports */
1133 if (!e6000sw_is_portenabled(sc, port) ||
1134 !e6000sw_is_phyport(sc, port))
1137 mii = e6000sw_miiforphy(sc, port);
1141 portstatus = e6000sw_readreg(sc, REG_PORT(port),
1144 e6000sw_update_ifmedia(portstatus,
1145 &mii->mii_media_status, &mii->mii_media_active);
1147 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1148 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media)
1151 mii_phy_update(miisc, MII_POLLSTAT);
1155 pause("e6000sw tick", 1000);
1160 e6000sw_setup(device_t dev, e6000sw_softc_t *sc)
1162 uint16_t atu_ctrl, atu_age;
1164 /* Set aging time */
1165 e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL,
1166 (E6000SW_DEFAULT_AGETIME << ATU_CONTROL_AGETIME) |
1167 (1 << ATU_CONTROL_LEARN2ALL));
1169 /* Send all with specific mac address to cpu port */
1170 e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_2x, MGMT_EN_ALL);
1171 e6000sw_writereg(sc, REG_GLOBAL2, MGMT_EN_0x, MGMT_EN_ALL);
1173 /* Disable Remote Management */
1174 e6000sw_writereg(sc, REG_GLOBAL, SWITCH_GLOBAL_CONTROL2, 0);
1176 /* Disable loopback filter and flow control messages */
1177 e6000sw_writereg(sc, REG_GLOBAL2, SWITCH_MGMT,
1178 SWITCH_MGMT_PRI_MASK |
1179 (1 << SWITCH_MGMT_RSVD2CPU) |
1180 SWITCH_MGMT_FC_PRI_MASK |
1181 (1 << SWITCH_MGMT_FORCEFLOW));
1183 e6000sw_atu_flush(dev, sc, NO_OPERATION);
1184 e6000sw_atu_mac_table(dev, sc, NULL, NO_OPERATION);
1185 e6000sw_set_atustat(dev, sc, 0, COUNT_ALL);
1187 /* Set ATU AgeTime to 15 seconds */
1190 atu_ctrl = e6000sw_readreg(sc, REG_GLOBAL, ATU_CONTROL);
1192 /* Set new AgeTime field */
1193 atu_ctrl &= ~ATU_CONTROL_AGETIME_MASK;
1194 e6000sw_writereg(sc, REG_GLOBAL, ATU_CONTROL, atu_ctrl |
1195 (atu_age << ATU_CONTROL_AGETIME));
1199 e6000sw_port_vlan_conf(e6000sw_softc_t *sc)
1204 /* Disable all ports */
1205 for (port = 0; port < sc->num_ports; port++) {
1206 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1207 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
1208 (ret & ~PORT_CONTROL_ENABLE));
1211 /* Set port priority */
1212 for (port = 0; port < sc->num_ports; port++) {
1213 if (!e6000sw_is_portenabled(sc, port))
1215 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1216 ret &= ~PORT_VID_PRIORITY_MASK;
1217 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1221 for (port = 0; port < sc->num_ports; port++) {
1222 if (!e6000sw_is_portenabled(sc, port))
1224 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_VID);
1225 ret &= ~PORT_VID_DEF_VID_MASK;
1227 e6000sw_writereg(sc, REG_PORT(port), PORT_VID, ret);
1230 /* Enable all ports */
1231 for (port = 0; port < sc->num_ports; port++) {
1232 if (!e6000sw_is_portenabled(sc, port))
1234 ret = e6000sw_readreg(sc, REG_PORT(port), PORT_CONTROL);
1235 e6000sw_writereg(sc, REG_PORT(port), PORT_CONTROL,
1236 (ret | PORT_CONTROL_ENABLE));
1239 /* Set VLAN mode. */
1240 sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
1241 etherswitch_info.es_nvlangroups = sc->num_ports;
1242 for (port = 0; port < sc->num_ports; port++) {
1244 if (e6000sw_is_portenabled(sc, port)) {
1245 for (i = 0; i < sc->num_ports; i++) {
1246 if (i == port || !e6000sw_is_portenabled(sc, i))
1248 members |= (1 << i);
1251 e6000sw_port_vlan_assign(sc, port, port + 1, members);
1256 e6000sw_set_atustat(device_t dev, e6000sw_softc_t *sc, int bin, int flag)
1260 ret = e6000sw_readreg(sc, REG_GLOBAL2, ATU_STATS);
1261 e6000sw_writereg(sc, REG_GLOBAL2, ATU_STATS, (bin << ATU_STATS_BIN ) |
1262 (flag << ATU_STATS_FLAG));
1266 e6000sw_atu_mac_table(device_t dev, e6000sw_softc_t *sc, struct atu_opt *atu,
1273 if (flag == NO_OPERATION)
1275 else if ((flag & (LOAD_FROM_FIB | PURGE_FROM_FIB | GET_NEXT_IN_FIB |
1276 GET_VIOLATION_DATA | CLEAR_VIOLATION_DATA)) == 0) {
1277 device_printf(dev, "Wrong Opcode for ATU operation\n");
1281 ret_opt = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1283 if (ret_opt & ATU_UNIT_BUSY) {
1284 device_printf(dev, "ATU unit is busy, cannot access"
1288 if(flag & LOAD_FROM_FIB) {
1289 ret_data = e6000sw_readreg(sc, REG_GLOBAL, ATU_DATA);
1290 e6000sw_writereg(sc, REG_GLOBAL2, ATU_DATA, (ret_data &
1293 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR01, atu->mac_01);
1294 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR23, atu->mac_23);
1295 e6000sw_writereg(sc, REG_GLOBAL, ATU_MAC_ADDR45, atu->mac_45);
1296 e6000sw_writereg(sc, REG_GLOBAL, ATU_FID, atu->fid);
1298 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret_opt |
1299 ATU_UNIT_BUSY | flag));
1301 retries = E6000SW_RETRIES;
1302 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1303 ATU_OPERATION) & ATU_UNIT_BUSY))
1307 device_printf(dev, "Timeout while flushing\n");
1308 else if (flag & GET_NEXT_IN_FIB) {
1309 atu->mac_01 = e6000sw_readreg(sc, REG_GLOBAL,
1311 atu->mac_23 = e6000sw_readreg(sc, REG_GLOBAL,
1313 atu->mac_45 = e6000sw_readreg(sc, REG_GLOBAL,
1322 e6000sw_atu_flush(device_t dev, e6000sw_softc_t *sc, int flag)
1327 if (flag == NO_OPERATION)
1330 ret = e6000sw_readreg(sc, REG_GLOBAL, ATU_OPERATION);
1331 if (ret & ATU_UNIT_BUSY) {
1332 device_printf(dev, "Atu unit is busy, cannot flush\n");
1335 e6000sw_writereg(sc, REG_GLOBAL, ATU_OPERATION, (ret |
1336 ATU_UNIT_BUSY | flag));
1337 retries = E6000SW_RETRIES;
1338 while (--retries & (e6000sw_readreg(sc, REG_GLOBAL,
1339 ATU_OPERATION) & ATU_UNIT_BUSY))
1343 device_printf(dev, "Timeout while flushing\n");