2 * Copyright (c) 2013 Luiz Otavio O Souza.
3 * Copyright (c) 2011-2012 Stefan Bethke.
4 * Copyright (c) 2012 Adrian Chadd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
42 #include <net/if_arp.h>
43 #include <net/ethernet.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
48 #include <machine/bus.h>
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 #include <dev/etherswitch/mdio.h>
53 #include <dev/etherswitch/etherswitch.h>
56 #include "miibus_if.h"
57 #include "etherswitch_if.h"
59 MALLOC_DECLARE(M_UKSWITCH);
60 MALLOC_DEFINE(M_UKSWITCH, "ukswitch", "ukswitch data structures");
62 struct ukswitch_softc {
63 struct mtx sc_mtx; /* serialize access to softc */
65 int media; /* cpu port media */
66 int cpuport; /* which PHY is connected to the CPU */
67 int phymask; /* PHYs we manage */
68 int numports; /* number of ports */
69 int ifpport[MII_NPHY];
74 struct callout callout_tick;
75 etherswitch_info_t info;
78 #define UKSWITCH_LOCK(_sc) \
79 mtx_lock(&(_sc)->sc_mtx)
80 #define UKSWITCH_UNLOCK(_sc) \
81 mtx_unlock(&(_sc)->sc_mtx)
82 #define UKSWITCH_LOCK_ASSERT(_sc, _what) \
83 mtx_assert(&(_sc)->sc_mtx, (_what))
84 #define UKSWITCH_TRYLOCK(_sc) \
85 mtx_trylock(&(_sc)->sc_mtx)
88 #define DPRINTF(dev, args...) device_printf(dev, args)
90 #define DPRINTF(dev, args...)
93 static inline int ukswitch_portforphy(struct ukswitch_softc *, int);
94 static void ukswitch_tick(void *);
95 static int ukswitch_ifmedia_upd(struct ifnet *);
96 static void ukswitch_ifmedia_sts(struct ifnet *, struct ifmediareq *);
99 ukswitch_probe(device_t dev)
101 struct ukswitch_softc *sc;
103 sc = device_get_softc(dev);
104 bzero(sc, sizeof(*sc));
106 device_set_desc_copy(dev, "Generic MDIO switch driver");
107 return (BUS_PROBE_DEFAULT);
111 ukswitch_attach_phys(struct ukswitch_softc *sc)
113 int phy, port = 0, err = 0;
116 /* PHYs need an interface, so we generate a dummy one */
117 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
118 for (phy = 0; phy < MII_NPHY; phy++) {
119 if (((1 << phy) & sc->phymask) == 0)
121 sc->ifpport[phy] = port;
122 sc->portphy[port] = phy;
123 sc->ifp[port] = if_alloc(IFT_ETHER);
124 sc->ifp[port]->if_softc = sc;
125 sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
126 IFF_DRV_RUNNING | IFF_SIMPLEX;
127 sc->ifname[port] = malloc(strlen(name)+1, M_UKSWITCH, M_WAITOK);
128 bcopy(name, sc->ifname[port], strlen(name)+1);
129 if_initname(sc->ifp[port], sc->ifname[port], port);
130 sc->miibus[port] = malloc(sizeof(device_t), M_UKSWITCH,
132 err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port],
133 ukswitch_ifmedia_upd, ukswitch_ifmedia_sts, \
134 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
135 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
136 device_get_nameunit(*sc->miibus[port]),
137 sc->ifp[port]->if_xname);
139 device_printf(sc->sc_dev,
140 "attaching PHY %d failed\n",
144 sc->info.es_nports = port + 1;
145 if (++port >= sc->numports)
152 ukswitch_attach(device_t dev)
154 struct ukswitch_softc *sc;
157 sc = device_get_softc(dev);
160 mtx_init(&sc->sc_mtx, "ukswitch", NULL, MTX_DEF);
161 strlcpy(sc->info.es_name, device_get_desc(dev),
162 sizeof(sc->info.es_name));
170 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
171 "numports", &sc->numports);
172 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
173 "phymask", &sc->phymask);
174 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
175 "cpuport", &sc->cpuport);
176 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
177 "media", &sc->media);
179 /* Support only fast and giga ethernet. */
180 if (sc->media != 100 && sc->media != 1000)
183 if (sc->cpuport != -1)
184 /* Always attach the cpu port. */
185 sc->phymask |= (1 << sc->cpuport);
187 /* We do not support any vlan groups. */
188 sc->info.es_nvlangroups = 0;
190 sc->ifp = malloc(sizeof(struct ifnet *) * sc->numports, M_UKSWITCH,
192 sc->ifname = malloc(sizeof(char *) * sc->numports, M_UKSWITCH,
194 sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_UKSWITCH,
196 sc->portphy = malloc(sizeof(int) * sc->numports, M_UKSWITCH,
200 * Attach the PHYs and complete the bus enumeration.
202 err = ukswitch_attach_phys(sc);
206 bus_generic_probe(dev);
207 bus_enumerate_hinted_children(dev);
208 err = bus_generic_attach(dev);
212 callout_init(&sc->callout_tick, 0);
220 ukswitch_detach(device_t dev)
222 struct ukswitch_softc *sc = device_get_softc(dev);
225 callout_drain(&sc->callout_tick);
227 for (i=0; i < MII_NPHY; i++) {
228 if (((1 << i) & sc->phymask) == 0)
230 port = ukswitch_portforphy(sc, i);
231 if (sc->miibus[port] != NULL)
232 device_delete_child(dev, (*sc->miibus[port]));
233 if (sc->ifp[port] != NULL)
234 if_free(sc->ifp[port]);
235 free(sc->ifname[port], M_UKSWITCH);
236 free(sc->miibus[port], M_UKSWITCH);
239 free(sc->portphy, M_UKSWITCH);
240 free(sc->miibus, M_UKSWITCH);
241 free(sc->ifname, M_UKSWITCH);
242 free(sc->ifp, M_UKSWITCH);
244 bus_generic_detach(dev);
245 mtx_destroy(&sc->sc_mtx);
251 * Convert PHY number to port number.
254 ukswitch_portforphy(struct ukswitch_softc *sc, int phy)
257 return (sc->ifpport[phy]);
260 static inline struct mii_data *
261 ukswitch_miiforport(struct ukswitch_softc *sc, int port)
264 if (port < 0 || port > sc->numports)
266 return (device_get_softc(*sc->miibus[port]));
269 static inline struct ifnet *
270 ukswitch_ifpforport(struct ukswitch_softc *sc, int port)
273 if (port < 0 || port > sc->numports)
275 return (sc->ifp[port]);
279 * Poll the status for all PHYs.
282 ukswitch_miipollstat(struct ukswitch_softc *sc)
285 struct mii_data *mii;
286 struct mii_softc *miisc;
288 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
290 for (i = 0; i < MII_NPHY; i++) {
291 if (((1 << i) & sc->phymask) == 0)
293 port = ukswitch_portforphy(sc, i);
294 if ((*sc->miibus[port]) == NULL)
296 mii = device_get_softc(*sc->miibus[port]);
297 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
298 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
302 mii_phy_update(miisc, MII_POLLSTAT);
308 ukswitch_tick(void *arg)
310 struct ukswitch_softc *sc = arg;
312 ukswitch_miipollstat(sc);
313 callout_reset(&sc->callout_tick, hz, ukswitch_tick, sc);
317 ukswitch_lock(device_t dev)
319 struct ukswitch_softc *sc = device_get_softc(dev);
321 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
326 ukswitch_unlock(device_t dev)
328 struct ukswitch_softc *sc = device_get_softc(dev);
330 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
334 static etherswitch_info_t *
335 ukswitch_getinfo(device_t dev)
337 struct ukswitch_softc *sc = device_get_softc(dev);
343 ukswitch_getport(device_t dev, etherswitch_port_t *p)
345 struct ukswitch_softc *sc = device_get_softc(dev);
346 struct mii_data *mii;
347 struct ifmediareq *ifmr = &p->es_ifmr;
350 if (p->es_port < 0 || p->es_port >= sc->numports)
354 phy = sc->portphy[p->es_port];
355 mii = ukswitch_miiforport(sc, p->es_port);
356 if (sc->cpuport != -1 && phy == sc->cpuport) {
357 /* fill in fixed values for CPU port */
358 p->es_flags |= ETHERSWITCH_PORT_CPU;
360 if (sc->media == 100)
361 ifmr->ifm_current = ifmr->ifm_active =
362 IFM_ETHER | IFM_100_TX | IFM_FDX;
364 ifmr->ifm_current = ifmr->ifm_active =
365 IFM_ETHER | IFM_1000_T | IFM_FDX;
367 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
368 } else if (mii != NULL) {
369 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
370 &mii->mii_media, SIOCGIFMEDIA);
380 ukswitch_setport(device_t dev, etherswitch_port_t *p)
382 struct ukswitch_softc *sc = device_get_softc(dev);
384 struct mii_data *mii;
388 if (p->es_port < 0 || p->es_port >= sc->numports)
391 if (sc->portphy[p->es_port] == sc->cpuport)
394 mii = ukswitch_miiforport(sc, p->es_port);
398 ifp = ukswitch_ifpforport(sc, p->es_port);
400 ifm = &mii->mii_media;
401 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA);
406 ukswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
411 vg->es_member_ports = 0;
412 vg->es_untagged_ports = 0;
418 ukswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
426 ukswitch_statchg(device_t dev)
429 DPRINTF(dev, "%s\n", __func__);
433 ukswitch_ifmedia_upd(struct ifnet *ifp)
435 struct ukswitch_softc *sc = ifp->if_softc;
436 struct mii_data *mii = ukswitch_miiforport(sc, ifp->if_dunit);
438 DPRINTF(sc->sc_dev, "%s\n", __func__);
446 ukswitch_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
448 struct ukswitch_softc *sc = ifp->if_softc;
449 struct mii_data *mii = ukswitch_miiforport(sc, ifp->if_dunit);
451 DPRINTF(sc->sc_dev, "%s\n", __func__);
456 ifmr->ifm_active = mii->mii_media_active;
457 ifmr->ifm_status = mii->mii_media_status;
461 ukswitch_readphy(device_t dev, int phy, int reg)
463 struct ukswitch_softc *sc;
466 sc = device_get_softc(dev);
467 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
469 if (phy < 0 || phy >= 32)
471 if (reg < 0 || reg >= 32)
475 data = MDIO_READREG(device_get_parent(dev), phy, reg);
482 ukswitch_writephy(device_t dev, int phy, int reg, int data)
484 struct ukswitch_softc *sc;
487 sc = device_get_softc(dev);
488 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
490 if (phy < 0 || phy >= 32)
492 if (reg < 0 || reg >= 32)
496 err = MDIO_WRITEREG(device_get_parent(dev), phy, reg, data);
503 ukswitch_readreg(device_t dev, int addr)
505 struct ukswitch_softc *sc;
507 sc = device_get_softc(dev);
508 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
515 ukswitch_writereg(device_t dev, int addr, int value)
517 struct ukswitch_softc *sc;
519 sc = device_get_softc(dev);
520 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
526 static device_method_t ukswitch_methods[] = {
527 /* Device interface */
528 DEVMETHOD(device_probe, ukswitch_probe),
529 DEVMETHOD(device_attach, ukswitch_attach),
530 DEVMETHOD(device_detach, ukswitch_detach),
533 DEVMETHOD(bus_add_child, device_add_child_ordered),
536 DEVMETHOD(miibus_readreg, ukswitch_readphy),
537 DEVMETHOD(miibus_writereg, ukswitch_writephy),
538 DEVMETHOD(miibus_statchg, ukswitch_statchg),
541 DEVMETHOD(mdio_readreg, ukswitch_readphy),
542 DEVMETHOD(mdio_writereg, ukswitch_writephy),
544 /* etherswitch interface */
545 DEVMETHOD(etherswitch_lock, ukswitch_lock),
546 DEVMETHOD(etherswitch_unlock, ukswitch_unlock),
547 DEVMETHOD(etherswitch_getinfo, ukswitch_getinfo),
548 DEVMETHOD(etherswitch_readreg, ukswitch_readreg),
549 DEVMETHOD(etherswitch_writereg, ukswitch_writereg),
550 DEVMETHOD(etherswitch_readphyreg, ukswitch_readphy),
551 DEVMETHOD(etherswitch_writephyreg, ukswitch_writephy),
552 DEVMETHOD(etherswitch_getport, ukswitch_getport),
553 DEVMETHOD(etherswitch_setport, ukswitch_setport),
554 DEVMETHOD(etherswitch_getvgroup, ukswitch_getvgroup),
555 DEVMETHOD(etherswitch_setvgroup, ukswitch_setvgroup),
560 DEFINE_CLASS_0(ukswitch, ukswitch_driver, ukswitch_methods,
561 sizeof(struct ukswitch_softc));
562 static devclass_t ukswitch_devclass;
564 DRIVER_MODULE(ukswitch, mdio, ukswitch_driver, ukswitch_devclass, 0, 0);
565 DRIVER_MODULE(miibus, ukswitch, miibus_driver, miibus_devclass, 0, 0);
566 DRIVER_MODULE(mdio, ukswitch, mdio_driver, mdio_devclass, 0, 0);
567 DRIVER_MODULE(etherswitch, ukswitch, etherswitch_driver, etherswitch_devclass, 0, 0);
568 MODULE_VERSION(ukswitch, 1);
569 MODULE_DEPEND(ukswitch, miibus, 1, 1, 1); /* XXX which versions? */
570 MODULE_DEPEND(ukswitch, etherswitch, 1, 1, 1); /* XXX which versions? */