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>
62 #include <dev/etherswitch/arswitch/arswitch_7240.h>
63 #include <dev/etherswitch/arswitch/arswitch_8216.h>
64 #include <dev/etherswitch/arswitch/arswitch_8226.h>
65 #include <dev/etherswitch/arswitch/arswitch_8316.h>
68 #include "miibus_if.h"
69 #include "etherswitch_if.h"
72 static SYSCTL_NODE(_debug, OID_AUTO, arswitch, CTLFLAG_RD, 0, "arswitch");
75 static inline int arswitch_portforphy(int phy);
76 static void arswitch_tick(void *arg);
77 static int arswitch_ifmedia_upd(struct ifnet *);
78 static void arswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *);
81 arswitch_identify(driver_t *driver, device_t parent)
85 if (device_find_child(parent, driver->name, -1) == NULL) {
86 child = BUS_ADD_CHILD(parent, 0, driver->name, -1);
91 arswitch_probe(device_t dev)
93 struct arswitch_softc *sc;
95 char *chipname, desc[256];
97 sc = device_get_softc(dev);
98 bzero(sc, sizeof(*sc));
102 if (ar7240_probe(dev) == 0) {
104 sc->sc_switchtype = AR8X16_SWITCH_AR7240;
110 id = arswitch_readreg(dev, AR8X16_REG_MASK_CTRL);
111 switch ((id & AR8X16_MASK_CTRL_VER_MASK) >>
112 AR8X16_MASK_CTRL_VER_SHIFT) {
115 sc->sc_switchtype = AR8X16_SWITCH_AR8216;
119 sc->sc_switchtype = AR8X16_SWITCH_AR8226;
123 sc->sc_switchtype = AR8X16_SWITCH_AR8316;
130 DPRINTF(dev, "chipname=%s, rev=%02x\n", chipname,
131 id & AR8X16_MASK_CTRL_REV_MASK);
132 if (chipname != NULL) {
133 snprintf(desc, sizeof(desc),
134 "Atheros %s Ethernet Switch",
136 device_set_desc_copy(dev, desc);
137 return (BUS_PROBE_DEFAULT);
143 arswitch_attach_phys(struct arswitch_softc *sc)
148 /* PHYs need an interface, so we generate a dummy one */
149 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
150 for (phy = 0; phy < sc->numphys; phy++) {
151 sc->ifp[phy] = if_alloc(IFT_ETHER);
152 sc->ifp[phy]->if_softc = sc;
153 sc->ifp[phy]->if_flags |= IFF_UP | IFF_BROADCAST |
154 IFF_DRV_RUNNING | IFF_SIMPLEX;
155 sc->ifname[phy] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK);
156 bcopy(name, sc->ifname[phy], strlen(name)+1);
157 if_initname(sc->ifp[phy], sc->ifname[phy],
158 arswitch_portforphy(phy));
159 err = mii_attach(sc->sc_dev, &sc->miibus[phy], sc->ifp[phy],
160 arswitch_ifmedia_upd, arswitch_ifmedia_sts, \
161 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
162 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
163 device_get_nameunit(sc->miibus[phy]),
164 sc->ifp[phy]->if_xname);
166 device_printf(sc->sc_dev,
167 "attaching PHY %d failed\n",
175 arswitch_attach(device_t dev)
177 struct arswitch_softc *sc;
180 sc = device_get_softc(dev);
182 /* sc->sc_switchtype is already decided in arswitch_probe() */
184 mtx_init(&sc->sc_mtx, "arswitch", NULL, MTX_DEF);
186 strlcpy(sc->info.es_name, device_get_desc(dev),
187 sizeof(sc->info.es_name));
190 * Attach switch related functions
192 if (AR8X16_IS_SWITCH(sc, AR7240))
194 else if (AR8X16_IS_SWITCH(sc, AR8216))
196 else if (AR8X16_IS_SWITCH(sc, AR8226))
198 else if (AR8X16_IS_SWITCH(sc, AR8316))
204 * XXX these two should be part of the switch attach function
206 sc->info.es_nports = 5; /* XXX technically 6, but 6th not used */
207 sc->info.es_nvlangroups = 16;
209 /* XXX Defaults for externally connected AR8316 */
215 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
216 "numphys", &sc->numphys);
217 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
218 "phy4cpu", &sc->phy4cpu);
219 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
220 "is_rgmii", &sc->is_rgmii);
221 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
222 "is_gmii", &sc->is_gmii);
225 * This requires much more setup depending upon each chip, including:
227 * + Proper reinitialisation of the PHYs;
228 * + Initialising the VLAN table;
229 * + Initialising the port access table and CPU flood/broadcast
231 * + Other things I haven't yet thought of.
234 arswitch_writereg(dev, AR8X16_REG_MASK_CTRL,
235 AR8X16_MASK_CTRL_SOFT_RESET);
237 if (arswitch_readreg(dev, AR8X16_REG_MASK_CTRL) &
238 AR8X16_MASK_CTRL_SOFT_RESET) {
239 device_printf(dev, "unable to reset switch\n");
244 err = sc->hal.arswitch_hw_setup(sc);
248 err = sc->hal.arswitch_hw_global_setup(sc);
253 * Attach the PHYs and complete the bus enumeration.
255 err = arswitch_attach_phys(sc);
259 bus_generic_probe(dev);
260 bus_enumerate_hinted_children(dev);
261 err = bus_generic_attach(dev);
265 callout_init_mtx(&sc->callout_tick, &sc->sc_mtx, 0);
275 arswitch_detach(device_t dev)
277 struct arswitch_softc *sc = device_get_softc(dev);
280 callout_drain(&sc->callout_tick);
282 for (i=0; i < sc->numphys; i++) {
283 if (sc->miibus[i] != NULL)
284 device_delete_child(dev, sc->miibus[i]);
285 if (sc->ifp[i] != NULL)
287 free(sc->ifname[i], M_DEVBUF);
290 bus_generic_detach(dev);
291 mtx_destroy(&sc->sc_mtx);
297 * Convert PHY number to port number. PHY0 is connected to port 1, PHY1 to
301 arswitch_portforphy(int phy)
306 static inline struct mii_data *
307 arswitch_miiforport(struct arswitch_softc *sc, int port)
311 if (phy < 0 || phy >= sc->numphys)
313 return (device_get_softc(sc->miibus[phy]));
316 static inline struct ifnet *
317 arswitch_ifpforport(struct arswitch_softc *sc, int port)
321 if (phy < 0 || phy >= sc->numphys)
323 return (sc->ifp[phy]);
327 * Convert port status to ifmedia.
330 arswitch_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active)
332 *media_active = IFM_ETHER;
333 *media_status = IFM_AVALID;
335 if ((portstatus & AR8X16_PORT_STS_LINK_UP) != 0)
336 *media_status |= IFM_ACTIVE;
338 *media_active |= IFM_NONE;
341 switch (portstatus & AR8X16_PORT_STS_SPEED_MASK) {
342 case AR8X16_PORT_STS_SPEED_10:
343 *media_active |= IFM_10_T;
345 case AR8X16_PORT_STS_SPEED_100:
346 *media_active |= IFM_100_TX;
348 case AR8X16_PORT_STS_SPEED_1000:
349 *media_active |= IFM_1000_T;
352 if ((portstatus & AR8X16_PORT_STS_DUPLEX) == 0)
353 *media_active |= IFM_FDX;
355 *media_active |= IFM_HDX;
356 if ((portstatus & AR8X16_PORT_STS_TXFLOW) != 0)
357 *media_active |= IFM_ETH_TXPAUSE;
358 if ((portstatus & AR8X16_PORT_STS_RXFLOW) != 0)
359 *media_active |= IFM_ETH_RXPAUSE;
363 * Poll the status for all PHYs. We're using the switch port status because
364 * thats a lot quicker to read than talking to all the PHYs. Care must be
365 * taken that the resulting ifmedia_active is identical to what the PHY will
366 * compute, or gratuitous link status changes will occur whenever the PHYs
367 * update function is called.
370 arswitch_miipollstat(struct arswitch_softc *sc)
373 struct mii_data *mii;
374 struct mii_softc *miisc;
377 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED);
379 for (i = 0; i < sc->numphys; i++) {
380 if (sc->miibus[i] == NULL)
382 mii = device_get_softc(sc->miibus[i]);
383 portstatus = arswitch_readreg(sc->sc_dev,
384 AR8X16_REG_PORT_STS(arswitch_portforphy(i)));
386 DPRINTF(sc->sc_dev, "p[%d]=%b\n",
389 "\20\3TXMAC\4RXMAC\5TXFLOW\6RXFLOW\7"
390 "DUPLEX\11LINK_UP\12LINK_AUTO\13LINK_PAUSE");
392 arswitch_update_ifmedia(portstatus, &mii->mii_media_status,
393 &mii->mii_media_active);
394 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
395 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
398 mii_phy_update(miisc, MII_POLLSTAT);
404 arswitch_tick(void *arg)
406 struct arswitch_softc *sc = arg;
408 arswitch_miipollstat(sc);
409 callout_reset(&sc->callout_tick, hz, arswitch_tick, sc);
413 arswitch_lock(device_t dev)
415 struct arswitch_softc *sc = device_get_softc(dev);
417 ARSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
422 arswitch_unlock(device_t dev)
424 struct arswitch_softc *sc = device_get_softc(dev);
426 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED);
430 static etherswitch_info_t *
431 arswitch_getinfo(device_t dev)
433 struct arswitch_softc *sc = device_get_softc(dev);
439 arswitch_getport(device_t dev, etherswitch_port_t *p)
441 struct arswitch_softc *sc = device_get_softc(dev);
442 struct mii_data *mii;
443 struct ifmediareq *ifmr = &p->es_ifmr;
446 if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PORTS)
450 mii = arswitch_miiforport(sc, p->es_port);
451 if (p->es_port == 0) {
452 /* fill in fixed values for CPU port */
454 ifmr->ifm_current = ifmr->ifm_active =
455 IFM_ETHER | IFM_1000_T | IFM_FDX;
457 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
458 } else if (mii != NULL) {
459 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
460 &mii->mii_media, SIOCGIFMEDIA);
470 * XXX doesn't yet work?
473 arswitch_setport(device_t dev, etherswitch_port_t *p)
476 struct arswitch_softc *sc;
478 struct mii_data *mii;
482 * XXX check the sc numphys, or the #define ?
484 if (p->es_port < 0 || p->es_port >= AR8X16_NUM_PHYS)
487 sc = device_get_softc(dev);
490 * XXX TODO: don't set the CPU port?
493 mii = arswitch_miiforport(sc, p->es_port);
497 ifp = arswitch_ifpforport(sc, p->es_port);
499 ifm = &mii->mii_media;
500 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA);
505 arswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
508 /* XXX not implemented yet */
510 vg->es_member_ports = 0;
511 vg->es_untagged_ports = 0;
517 arswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
520 /* XXX not implemented yet */
525 arswitch_statchg(device_t dev)
528 DPRINTF(dev, "%s\n", __func__);
532 arswitch_ifmedia_upd(struct ifnet *ifp)
534 struct arswitch_softc *sc = ifp->if_softc;
535 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
544 arswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
546 struct arswitch_softc *sc = ifp->if_softc;
547 struct mii_data *mii = arswitch_miiforport(sc, ifp->if_dunit);
549 DPRINTF(sc->sc_dev, "%s\n", __func__);
554 ifmr->ifm_active = mii->mii_media_active;
555 ifmr->ifm_status = mii->mii_media_status;
558 static device_method_t arswitch_methods[] = {
559 /* Device interface */
560 DEVMETHOD(device_identify, arswitch_identify),
561 DEVMETHOD(device_probe, arswitch_probe),
562 DEVMETHOD(device_attach, arswitch_attach),
563 DEVMETHOD(device_detach, arswitch_detach),
566 DEVMETHOD(bus_add_child, device_add_child_ordered),
569 DEVMETHOD(miibus_readreg, arswitch_readphy),
570 DEVMETHOD(miibus_writereg, arswitch_writephy),
571 DEVMETHOD(miibus_statchg, arswitch_statchg),
574 DEVMETHOD(mdio_readreg, arswitch_readphy),
575 DEVMETHOD(mdio_writereg, arswitch_writephy),
577 /* etherswitch interface */
578 DEVMETHOD(etherswitch_lock, arswitch_lock),
579 DEVMETHOD(etherswitch_unlock, arswitch_unlock),
580 DEVMETHOD(etherswitch_getinfo, arswitch_getinfo),
581 DEVMETHOD(etherswitch_readreg, arswitch_readreg),
582 DEVMETHOD(etherswitch_writereg, arswitch_writereg),
583 DEVMETHOD(etherswitch_readphyreg, arswitch_readphy),
584 DEVMETHOD(etherswitch_writephyreg, arswitch_writephy),
585 DEVMETHOD(etherswitch_getport, arswitch_getport),
586 DEVMETHOD(etherswitch_setport, arswitch_setport),
587 DEVMETHOD(etherswitch_getvgroup, arswitch_getvgroup),
588 DEVMETHOD(etherswitch_setvgroup, arswitch_setvgroup),
593 DEFINE_CLASS_0(arswitch, arswitch_driver, arswitch_methods,
594 sizeof(struct arswitch_softc));
595 static devclass_t arswitch_devclass;
597 DRIVER_MODULE(arswitch, mdio, arswitch_driver, arswitch_devclass, 0, 0);
598 DRIVER_MODULE(miibus, arswitch, miibus_driver, miibus_devclass, 0, 0);
599 DRIVER_MODULE(mdio, arswitch, mdio_driver, mdio_devclass, 0, 0);
600 DRIVER_MODULE(etherswitch, arswitch, etherswitch_driver, etherswitch_devclass, 0, 0);
601 MODULE_VERSION(arswitch, 1);
602 MODULE_DEPEND(arswitch, miibus, 1, 1, 1); /* XXX which versions? */
603 MODULE_DEPEND(arswitch, etherswitch, 1, 1, 1); /* XXX which versions? */