2 * Copyright (c) 2011-2012 Stefan Bethke.
3 * Copyright (c) 2012 Adrian Chadd.
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
30 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
41 #include <net/if_arp.h>
42 #include <net/ethernet.h>
43 #include <net/if_dl.h>
44 #include <net/if_media.h>
45 #include <net/if_types.h>
47 #include <machine/bus.h>
48 #include <dev/iicbus/iic.h>
49 #include <dev/iicbus/iiconf.h>
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include <dev/etherswitch/mdio.h>
55 #include <dev/etherswitch/etherswitch.h>
57 #include <dev/etherswitch/arswitch/arswitchreg.h>
58 #include <dev/etherswitch/arswitch/arswitchvar.h>
59 #include <dev/etherswitch/arswitch/arswitch_reg.h>
60 #include <dev/etherswitch/arswitch/arswitch_phy.h>
61 #include <dev/etherswitch/arswitch/arswitch_vlans.h>
63 #include <dev/etherswitch/arswitch/arswitch_7240.h>
64 #include <dev/etherswitch/arswitch/arswitch_8216.h>
65 #include <dev/etherswitch/arswitch/arswitch_8226.h>
66 #include <dev/etherswitch/arswitch/arswitch_8316.h>
69 #include "miibus_if.h"
70 #include "etherswitch_if.h"
73 static SYSCTL_NODE(_debug, OID_AUTO, arswitch, CTLFLAG_RD, 0, "arswitch");
76 static inline int arswitch_portforphy(int phy);
77 static void arswitch_tick(void *arg);
78 static int arswitch_ifmedia_upd(struct ifnet *);
79 static void arswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *);
82 arswitch_probe(device_t dev)
84 struct arswitch_softc *sc;
86 char *chipname, desc[256];
88 sc = device_get_softc(dev);
89 bzero(sc, sizeof(*sc));
93 if (ar7240_probe(dev) == 0) {
95 sc->sc_switchtype = AR8X16_SWITCH_AR7240;
101 id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL);
102 switch ((id & AR8X16_MASK_CTRL_VER_MASK) >>
103 AR8X16_MASK_CTRL_VER_SHIFT) {
106 sc->sc_switchtype = AR8X16_SWITCH_AR8216;
110 sc->sc_switchtype = AR8X16_SWITCH_AR8226;
114 sc->sc_switchtype = AR8X16_SWITCH_AR8316;
121 DPRINTF(dev, "chipname=%s, rev=%02x\n", chipname,
122 id & AR8X16_MASK_CTRL_REV_MASK);
123 if (chipname != NULL) {
124 snprintf(desc, sizeof(desc),
125 "Atheros %s Ethernet Switch",
127 device_set_desc_copy(dev, desc);
128 return (BUS_PROBE_DEFAULT);
134 arswitch_attach_phys(struct arswitch_softc *sc)
139 /* PHYs need an interface, so we generate a dummy one */
140 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
141 for (phy = 0; phy < sc->numphys; phy++) {
142 sc->ifp[phy] = if_alloc(IFT_ETHER);
143 sc->ifp[phy]->if_softc = sc;
144 sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST |
145 IFF_DRV_RUNNING | IFF_SIMPLEX;
146 sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK);
147 bcopy(name, sc->ifname[phy], strlen(name)+1);
148 if_initname(sc->ifp[phy], sc->ifname[phy],
149 arswitch_portforphy(phy));
150 err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy],
151 arswitch_ifmedia_upd, arswitch_ifmedia_sts, \
152 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
153 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
154 device_get_nameunit(sc->miibus[phy]),
155 sc->ifp[phy]->if_xname);
157 device_printf(sc->sc_dev,
158 "attaching PHY %d failed\n",
166 arswitch_reset(device_t dev)
169 arswitch_writereg(dev, AR8X16_REG_MASK_CTRL,
170 AR8X16_MASK_CTRL_SOFT_RESET);
172 if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) &
173 AR8X16_MASK_CTRL_SOFT_RESET) {
174 device_printf(dev, "unable to reset switch\n");
181 arswitch_set_vlan_mode(struct arswitch_softc *sc, uint32_t mode)
184 /* Check for invalid modes. */
185 if ((mode & sc->info.es_vlan_caps) != mode)
189 case ETHERSWITCH_VLAN_DOT1Q:
190 sc->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
192 case ETHERSWITCH_VLAN_PORT:
193 sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
200 arswitch_reset_vlans(sc);
206 arswitch_ports_init(struct arswitch_softc *sc)
211 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_STS(0),
212 (AR8X16_IS_SWITCH(sc, AR8216) ?
213 AR8X16_PORT_STS_SPEED_100 : AR8X16_PORT_STS_SPEED_1000) |
214 (AR8X16_IS_SWITCH(sc, AR8216) ? 0 : AR8X16_PORT_STS_RXFLOW) |
215 (AR8X16_IS_SWITCH(sc, AR8216) ? 0 : AR8X16_PORT_STS_TXFLOW) |
216 AR8X16_PORT_STS_RXMAC |
217 AR8X16_PORT_STS_TXMAC |
218 AR8X16_PORT_STS_DUPLEX);
219 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_CTRL(0),
220 arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(0)) &
221 ~AR8X16_PORT_CTRL_HEADER);
223 for (port = 1; port <= sc->numphys; port++) {
224 /* Set ports to auto negotiation. */
225 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_STS(port),
226 AR8X16_PORT_STS_LINK_AUTO);
227 arswitch_writereg(sc->sc_dev, AR8X16_REG_PORT_CTRL(port),
228 arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(port)) &
229 ~AR8X16_PORT_CTRL_HEADER);
234 arswitch_attach(device_t dev)
236 struct arswitch_softc *sc;
239 sc = device_get_softc(dev);
241 /* sc->sc_switchtype is already decided in arswitch_probe() */
243 mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF);
245 strlcpy(sc->info.es_name, device_get_desc(dev),
246 sizeof(sc->info.es_name));
249 * Attach switch related functions
251 if (AR8X16_IS_SWITCH(sc, AR7240))
253 else if (AR8X16_IS_SWITCH(sc, AR8216))
255 else if (AR8X16_IS_SWITCH(sc, AR8226))
257 else if (AR8X16_IS_SWITCH(sc, AR8316))
262 /* Common defaults. */
263 sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */
265 /* XXX Defaults for externally connected AR8316 */
271 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
272 "numphys", &sc->numphys);
273 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
274 "phy4cpu", &sc->phy4cpu);
275 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
276 "is_rgmii", &sc->is_rgmii);
277 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
278 "is_gmii", &sc->is_gmii);
280 if (sc->numphys > AR8X16_NUM_PHYS)
281 sc->numphys = AR8X16_NUM_PHYS;
283 /* Reset the switch. */
284 if (arswitch_reset(dev))
287 err = sc->hal.arswitch_hw_global_setup(sc);
291 /* Initialize the switch ports. */
292 arswitch_ports_init(sc);
295 * Attach the PHYs and complete the bus enumeration.
297 err = arswitch_attach_phys(sc);
301 /* Default to ingress filters off. */
302 err = arswitch_set_vlan_mode(sc, 0);
306 err = sc->hal.arswitch_hw_setup(sc);
310 bus_generic_probe(dev);
311 bus_enumerate_hinted_children(dev);
312 err = bus_generic_attach(dev);
316 callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0);
326 arswitch_detach(device_t dev)
328 struct arswitch_softc *sc = device_get_softc(dev);
331 callout_drain(&sc->callout_tick);
333 for (i=0; i < sc->numphys; i++) {
334 if (sc->miibus[i] != NULL)
335 device_delete_child(dev, sc->miibus[i]);
336 if (sc->ifp[i] != NULL)
338 free(sc->ifname[i], M_DEVBUF);
341 bus_generic_detach(dev);
342 mtx_destroy(&sc->sc_mtx);
348 * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to
352 arswitch_portforphy(int phy)
357 static inline struct mii_data *
358 arswitch_miiforport(struct arswitch_softc *sc, int port)
362 if (phy < 0 || phy >= sc->numphys)
364 return (device_get_softc(sc->miibus[phy]));
367 static inline struct ifnet *
368 arswitch_ifpforport(struct arswitch_softc *sc, int port)
372 if (phy < 0 || phy >= sc->numphys)
374 return (sc->ifp[phy]);
378 * Convert port status to ifmedia.
381 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active)
383 *media_active = IFM_ETHER;
384 *media_status = IFM_AVALID;
386 if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0)
387 *media_status |= IFM_ACTIVE;
389 *media_active |= IFM_NONE;
392 switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) {
393 case AR8X16_PORT_STS_SPEED_10:
394 *media_active |= IFM_10_T;
396 case AR8X16_PORT_STS_SPEED_100:
397 *media_active |= IFM_100_TX;
399 case AR8X16_PORT_STS_SPEED_1000:
400 *media_active |= IFM_1000_T;
403 if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0)
404 *media_active |= IFM_FDX;
406 *media_active |= IFM_HDX;
407 if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0)
408 *media_active |= IFM_ETH_TXPAUSE;
409 if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0)
410 *media_active |= IFM_ETH_RXPAUSE;
414 * Poll the status for all PHYs. We're using the switch port status because
415 * thats a lot quicker to read than talking to all the PHYs. Care must be
416 * taken that the resulting ifmedia_active is identical to what the PHY will
417 * compute, or gratuitous link status changes will occur whenever the PHYs
418 * update function is called.
421 arswitch_miipollstat(struct arswitch_softc *sc)
424 struct mii_data *mii;
425 struct mii_softc *miisc;
428 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED);
430 for (i = 0; i < sc->numphys; i++) {
431 if (sc->miibus[i] == NULL)
433 mii = device_get_softc(sc->miibus[i]);
434 portstatus = arswitch_readreg(sc->sc_dev,
435 AR8X16_REG_PORT_STS(arswitch_portforphy(i)));
437 DPRINTF(sc->sc_dev, "p[%d]=%b\n",
440 "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7"
441 "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE");
443 arswitch_update_ifmedia(portstatus, &mii->mii_media_status,
444 &mii->mii_media_active);
445 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
446 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
449 mii_phy_update(miisc, MII_POLLSTAT);
455 arswitch_tick(void *arg)
457 struct arswitch_softc *sc = arg;
459 arswitch_miipollstat(sc);
460 callout_reset(&sc->callout_tick, hz, arswitch_tick, sc);
464 arswitch_lock(device_t dev)
466 struct arswitch_softc *sc = device_get_softc(dev);
468 ARSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
473 arswitch_unlock(device_t dev)
475 struct arswitch_softc *sc = device_get_softc(dev);
477 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED);
481 static etherswitch_info_t *
482 arswitch_getinfo(device_t dev)
484 struct arswitch_softc *sc = device_get_softc(dev);
490 arswitch_getport(device_t dev, etherswitch_port_t *p)
492 struct arswitch_softc *sc;
493 struct ifmediareq *ifmr;
494 struct mii_data *mii;
498 sc = device_get_softc(dev);
499 if (p->es_port < 0 || p->es_port > sc->numphys)
504 /* Retrieve the PVID. */
505 arswitch_get_pvid(sc, p->es_port, &p->es_pvid);
508 reg = arswitch_readreg(sc->sc_dev, AR8X16_REG_PORT_CTRL(p->es_port));
509 if (reg & AR8X16_PORT_CTRL_DOUBLE_TAG)
510 p->es_flags |= ETHERSWITCH_PORT_DOUBLE_TAG;
511 reg >>= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT;
512 if ((reg & 0x3) == AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_ADD)
513 p->es_flags |= ETHERSWITCH_PORT_ADDTAG;
514 if ((reg & 0x3) == AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_STRIP)
515 p->es_flags |= ETHERSWITCH_PORT_STRIPTAG;
518 mii = arswitch_miiforport(sc, p->es_port);
519 if (p->es_port == 0) {
520 /* fill in fixed values for CPU port */
521 p->es_flags |= ETHERSWITCH_PORT_CPU;
524 ifmr->ifm_current = ifmr->ifm_active =
525 IFM_ETHER | IFM_1000_T | IFM_FDX;
527 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
528 } else if (mii != NULL) {
529 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
530 &mii->mii_media, SIOCGIFMEDIA);
540 arswitch_setport(device_t dev, etherswitch_port_t *p)
544 struct arswitch_softc *sc;
546 struct mii_data *mii;
549 sc = device_get_softc(dev);
550 if (p->es_port < 0 || p->es_port > sc->numphys)
554 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
559 arswitch_set_pvid(sc, p->es_port, p->es_pvid);
561 /* Mutually exclusive. */
562 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG &&
563 p->es_flags & ETHERSWITCH_PORT_STRIPTAG) {
569 if (p->es_flags & ETHERSWITCH_PORT_DOUBLE_TAG)
570 reg |= AR8X16_PORT_CTRL_DOUBLE_TAG;
571 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG)
572 reg |= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_ADD <<
573 AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT;
574 if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG)
575 reg |= AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_STRIP <<
576 AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT;
578 err = arswitch_modifyreg(sc->sc_dev,
579 AR8X16_REG_PORT_CTRL(p->es_port),
580 0x3 << AR8X16_PORT_CTRL_EGRESS_VLAN_MODE_SHIFT |
581 AR8X16_PORT_CTRL_DOUBLE_TAG, reg);
588 /* Do not allow media changes on CPU port. */
589 if (p->es_port == AR8X16_PORT_CPU)
592 mii = arswitch_miiforport(sc, p->es_port);
596 ifp = arswitch_ifpforport(sc, p->es_port);
598 ifm = &mii->mii_media;
599 return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA));
603 arswitch_statchg(device_t dev)
606 DPRINTF(dev, "%s\n", __func__);
610 arswitch_ifmedia_upd(struct ifnet *ifp)
612 struct arswitch_softc *sc = ifp->if_softc;
613 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
622 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
624 struct arswitch_softc *sc = ifp->if_softc;
625 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
627 DPRINTF(sc->sc_dev, "%s\n", __func__);
632 ifmr->ifm_active = mii->mii_media_active;
633 ifmr->ifm_status = mii->mii_media_status;
637 arswitch_getconf(device_t dev, etherswitch_conf_t *conf)
639 struct arswitch_softc *sc;
641 sc = device_get_softc(dev);
643 /* Return the VLAN mode. */
644 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
645 conf->vlan_mode = sc->vlan_mode;
651 arswitch_setconf(device_t dev, etherswitch_conf_t *conf)
653 struct arswitch_softc *sc;
656 sc = device_get_softc(dev);
658 /* Set the VLAN mode. */
659 if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE) {
660 err = arswitch_set_vlan_mode(sc, conf->vlan_mode);
668 static device_method_t arswitch_methods[] = {
669 /* Device interface */
670 DEVMETHOD(device_probe, arswitch_probe),
671 DEVMETHOD(device_attach, arswitch_attach),
672 DEVMETHOD(device_detach, arswitch_detach),
675 DEVMETHOD(bus_add_child, device_add_child_ordered),
678 DEVMETHOD(miibus_readreg, arswitch_readphy),
679 DEVMETHOD(miibus_writereg, arswitch_writephy),
680 DEVMETHOD(miibus_statchg, arswitch_statchg),
683 DEVMETHOD(mdio_readreg, arswitch_readphy),
684 DEVMETHOD(mdio_writereg, arswitch_writephy),
686 /* etherswitch interface */
687 DEVMETHOD(etherswitch_lock, arswitch_lock),
688 DEVMETHOD(etherswitch_unlock, arswitch_unlock),
689 DEVMETHOD(etherswitch_getinfo, arswitch_getinfo),
690 DEVMETHOD(etherswitch_readreg, arswitch_readreg),
691 DEVMETHOD(etherswitch_writereg, arswitch_writereg),
692 DEVMETHOD(etherswitch_readphyreg, arswitch_readphy),
693 DEVMETHOD(etherswitch_writephyreg, arswitch_writephy),
694 DEVMETHOD(etherswitch_getport, arswitch_getport),
695 DEVMETHOD(etherswitch_setport, arswitch_setport),
696 DEVMETHOD(etherswitch_getvgroup, arswitch_getvgroup),
697 DEVMETHOD(etherswitch_setvgroup, arswitch_setvgroup),
698 DEVMETHOD(etherswitch_getconf, arswitch_getconf),
699 DEVMETHOD(etherswitch_setconf, arswitch_setconf),
704 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods,
705 sizeof(struct arswitch_softc));
706 static devclass_t arswitch_devclass;
708 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0);
709 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0);
710 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0);
711 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0);
712 MODULE_VERSION(arswitch, 1);
713 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */
714 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */