2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2013 Luiz Otavio O Souza.
5 * Copyright (c) 2011-2012 Stefan Bethke.
6 * Copyright (c) 2012 Adrian Chadd.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * 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>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
45 #include <net/if_var.h>
46 #include <net/ethernet.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
50 #include <machine/bus.h>
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include <dev/mdio/mdio.h>
55 #include <dev/etherswitch/etherswitch.h>
58 #include "miibus_if.h"
59 #include "etherswitch_if.h"
61 MALLOC_DECLARE(M_UKSWITCH);
62 MALLOC_DEFINE(M_UKSWITCH, "ukswitch", "ukswitch data structures");
64 struct ukswitch_softc {
65 struct mtx sc_mtx; /* serialize access to softc */
67 int media; /* cpu port media */
68 int cpuport; /* which PHY is connected to the CPU */
69 int phymask; /* PHYs we manage */
70 int phyoffset; /* PHYs register offset */
71 int numports; /* number of ports */
72 int ifpport[MII_NPHY];
77 struct callout callout_tick;
78 etherswitch_info_t info;
81 #define UKSWITCH_LOCK(_sc) \
82 mtx_lock(&(_sc)->sc_mtx)
83 #define UKSWITCH_UNLOCK(_sc) \
84 mtx_unlock(&(_sc)->sc_mtx)
85 #define UKSWITCH_LOCK_ASSERT(_sc, _what) \
86 mtx_assert(&(_sc)->sc_mtx, (_what))
87 #define UKSWITCH_TRYLOCK(_sc) \
88 mtx_trylock(&(_sc)->sc_mtx)
91 #define DPRINTF(dev, args...) device_printf(dev, args)
93 #define DPRINTF(dev, args...)
96 static inline int ukswitch_portforphy(struct ukswitch_softc *, int);
97 static void ukswitch_tick(void *);
98 static int ukswitch_ifmedia_upd(if_t);
99 static void ukswitch_ifmedia_sts(if_t, struct ifmediareq *);
102 ukswitch_probe(device_t dev)
104 struct ukswitch_softc *sc;
106 sc = device_get_softc(dev);
107 bzero(sc, sizeof(*sc));
109 device_set_desc_copy(dev, "Generic MDIO switch driver");
110 return (BUS_PROBE_DEFAULT);
114 ukswitch_attach_phys(struct ukswitch_softc *sc)
116 int phy, port = 0, err = 0;
119 /* PHYs need an interface, so we generate a dummy one */
120 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
121 for (phy = 0; phy < MII_NPHY; phy++) {
122 if (((1 << phy) & sc->phymask) == 0)
124 sc->ifpport[phy] = port;
125 sc->portphy[port] = phy;
126 sc->ifp[port] = if_alloc(IFT_ETHER);
127 if (sc->ifp[port] == NULL) {
128 device_printf(sc->sc_dev, "couldn't allocate ifnet structure\n");
133 if_setsoftc(sc->ifp[port], sc);
134 if_setflags(sc->ifp[port], IFF_UP | IFF_BROADCAST |
135 IFF_DRV_RUNNING | IFF_SIMPLEX);
136 sc->ifname[port] = malloc(strlen(name)+1, M_UKSWITCH, M_WAITOK);
137 bcopy(name, sc->ifname[port], strlen(name)+1);
138 if_initname(sc->ifp[port], sc->ifname[port], port);
139 sc->miibus[port] = malloc(sizeof(device_t), M_UKSWITCH,
141 err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port],
142 ukswitch_ifmedia_upd, ukswitch_ifmedia_sts, \
143 BMSR_DEFCAPMASK, phy + sc->phyoffset, MII_OFFSET_ANY, 0);
144 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
145 device_get_nameunit(*sc->miibus[port]),
146 if_name(sc->ifp[port]));
148 device_printf(sc->sc_dev,
149 "attaching PHY %d failed\n",
153 sc->info.es_nports = port + 1;
154 if (++port >= sc->numports)
161 ukswitch_attach(device_t dev)
163 struct ukswitch_softc *sc;
166 sc = device_get_softc(dev);
169 mtx_init(&sc->sc_mtx, "ukswitch", NULL, MTX_DEF);
170 strlcpy(sc->info.es_name, device_get_desc(dev),
171 sizeof(sc->info.es_name));
180 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
181 "numports", &sc->numports);
182 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
183 "phymask", &sc->phymask);
184 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
185 "phyoffset", &sc->phyoffset);
186 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
187 "cpuport", &sc->cpuport);
188 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
189 "media", &sc->media);
191 /* Support only fast and giga ethernet. */
192 if (sc->media != 100 && sc->media != 1000)
195 if (sc->cpuport != -1)
196 /* Always attach the cpu port. */
197 sc->phymask |= (1 << sc->cpuport);
199 /* We do not support any vlan groups. */
200 sc->info.es_nvlangroups = 0;
202 sc->ifp = malloc(sizeof(if_t) * sc->numports, M_UKSWITCH,
204 sc->ifname = malloc(sizeof(char *) * sc->numports, M_UKSWITCH,
206 sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_UKSWITCH,
208 sc->portphy = malloc(sizeof(int) * sc->numports, M_UKSWITCH,
212 * Attach the PHYs and complete the bus enumeration.
214 err = ukswitch_attach_phys(sc);
218 bus_generic_probe(dev);
219 bus_enumerate_hinted_children(dev);
220 err = bus_generic_attach(dev);
224 callout_init(&sc->callout_tick, 0);
232 ukswitch_detach(device_t dev)
234 struct ukswitch_softc *sc = device_get_softc(dev);
237 callout_drain(&sc->callout_tick);
239 for (i=0; i < MII_NPHY; i++) {
240 if (((1 << i) & sc->phymask) == 0)
242 port = ukswitch_portforphy(sc, i);
243 if (sc->miibus[port] != NULL)
244 device_delete_child(dev, (*sc->miibus[port]));
245 if (sc->ifp[port] != NULL)
246 if_free(sc->ifp[port]);
247 free(sc->ifname[port], M_UKSWITCH);
248 free(sc->miibus[port], M_UKSWITCH);
251 free(sc->portphy, M_UKSWITCH);
252 free(sc->miibus, M_UKSWITCH);
253 free(sc->ifname, M_UKSWITCH);
254 free(sc->ifp, M_UKSWITCH);
256 bus_generic_detach(dev);
257 mtx_destroy(&sc->sc_mtx);
263 * Convert PHY number to port number.
266 ukswitch_portforphy(struct ukswitch_softc *sc, int phy)
269 return (sc->ifpport[phy]);
272 static inline struct mii_data *
273 ukswitch_miiforport(struct ukswitch_softc *sc, int port)
276 if (port < 0 || port > sc->numports)
278 return (device_get_softc(*sc->miibus[port]));
282 ukswitch_ifpforport(struct ukswitch_softc *sc, int port)
285 if (port < 0 || port > sc->numports)
287 return (sc->ifp[port]);
291 * Poll the status for all PHYs.
294 ukswitch_miipollstat(struct ukswitch_softc *sc)
297 struct mii_data *mii;
298 struct mii_softc *miisc;
300 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
302 for (i = 0; i < MII_NPHY; i++) {
303 if (((1 << i) & sc->phymask) == 0)
305 port = ukswitch_portforphy(sc, i);
306 if ((*sc->miibus[port]) == NULL)
308 mii = device_get_softc(*sc->miibus[port]);
309 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
310 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
314 mii_phy_update(miisc, MII_POLLSTAT);
320 ukswitch_tick(void *arg)
322 struct ukswitch_softc *sc = arg;
324 ukswitch_miipollstat(sc);
325 callout_reset(&sc->callout_tick, hz, ukswitch_tick, sc);
329 ukswitch_lock(device_t dev)
331 struct ukswitch_softc *sc = device_get_softc(dev);
333 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
338 ukswitch_unlock(device_t dev)
340 struct ukswitch_softc *sc = device_get_softc(dev);
342 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
346 static etherswitch_info_t *
347 ukswitch_getinfo(device_t dev)
349 struct ukswitch_softc *sc = device_get_softc(dev);
355 ukswitch_getport(device_t dev, etherswitch_port_t *p)
357 struct ukswitch_softc *sc = device_get_softc(dev);
358 struct mii_data *mii;
359 struct ifmediareq *ifmr = &p->es_ifmr;
362 if (p->es_port < 0 || p->es_port >= sc->numports)
366 phy = sc->portphy[p->es_port];
367 mii = ukswitch_miiforport(sc, p->es_port);
368 if (sc->cpuport != -1 && phy == sc->cpuport) {
369 /* fill in fixed values for CPU port */
370 p->es_flags |= ETHERSWITCH_PORT_CPU;
372 if (sc->media == 100)
373 ifmr->ifm_current = ifmr->ifm_active =
374 IFM_ETHER | IFM_100_TX | IFM_FDX;
376 ifmr->ifm_current = ifmr->ifm_active =
377 IFM_ETHER | IFM_1000_T | IFM_FDX;
379 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
380 } else if (mii != NULL) {
381 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
382 &mii->mii_media, SIOCGIFMEDIA);
392 ukswitch_setport(device_t dev, etherswitch_port_t *p)
394 struct ukswitch_softc *sc = device_get_softc(dev);
396 struct mii_data *mii;
400 if (p->es_port < 0 || p->es_port >= sc->numports)
403 if (sc->portphy[p->es_port] == sc->cpuport)
406 mii = ukswitch_miiforport(sc, p->es_port);
410 ifp = ukswitch_ifpforport(sc, p->es_port);
412 ifm = &mii->mii_media;
413 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA);
418 ukswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
423 vg->es_member_ports = 0;
424 vg->es_untagged_ports = 0;
430 ukswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
438 ukswitch_statchg(device_t dev)
441 DPRINTF(dev, "%s\n", __func__);
445 ukswitch_ifmedia_upd(if_t ifp)
447 struct ukswitch_softc *sc = if_getsoftc(ifp);
448 struct mii_data *mii = ukswitch_miiforport(sc, if_getdunit(ifp));
450 DPRINTF(sc->sc_dev, "%s\n", __func__);
458 ukswitch_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
460 struct ukswitch_softc *sc = if_getsoftc(ifp);
461 struct mii_data *mii = ukswitch_miiforport(sc, if_getdunit(ifp));
463 DPRINTF(sc->sc_dev, "%s\n", __func__);
468 ifmr->ifm_active = mii->mii_media_active;
469 ifmr->ifm_status = mii->mii_media_status;
473 ukswitch_readphy(device_t dev, int phy, int reg)
475 struct ukswitch_softc *sc;
478 sc = device_get_softc(dev);
479 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
481 if (phy < 0 || phy >= 32)
483 if (reg < 0 || reg >= 32)
487 data = MDIO_READREG(device_get_parent(dev), phy, reg);
494 ukswitch_writephy(device_t dev, int phy, int reg, int data)
496 struct ukswitch_softc *sc;
499 sc = device_get_softc(dev);
500 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
502 if (phy < 0 || phy >= 32)
504 if (reg < 0 || reg >= 32)
508 err = MDIO_WRITEREG(device_get_parent(dev), phy, reg, data);
515 ukswitch_readreg(device_t dev, int addr)
517 struct ukswitch_softc *sc __diagused;
519 sc = device_get_softc(dev);
520 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
527 ukswitch_writereg(device_t dev, int addr, int value)
529 struct ukswitch_softc *sc __diagused;
531 sc = device_get_softc(dev);
532 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
538 static device_method_t ukswitch_methods[] = {
539 /* Device interface */
540 DEVMETHOD(device_probe, ukswitch_probe),
541 DEVMETHOD(device_attach, ukswitch_attach),
542 DEVMETHOD(device_detach, ukswitch_detach),
545 DEVMETHOD(bus_add_child, device_add_child_ordered),
548 DEVMETHOD(miibus_readreg, ukswitch_readphy),
549 DEVMETHOD(miibus_writereg, ukswitch_writephy),
550 DEVMETHOD(miibus_statchg, ukswitch_statchg),
553 DEVMETHOD(mdio_readreg, ukswitch_readphy),
554 DEVMETHOD(mdio_writereg, ukswitch_writephy),
556 /* etherswitch interface */
557 DEVMETHOD(etherswitch_lock, ukswitch_lock),
558 DEVMETHOD(etherswitch_unlock, ukswitch_unlock),
559 DEVMETHOD(etherswitch_getinfo, ukswitch_getinfo),
560 DEVMETHOD(etherswitch_readreg, ukswitch_readreg),
561 DEVMETHOD(etherswitch_writereg, ukswitch_writereg),
562 DEVMETHOD(etherswitch_readphyreg, ukswitch_readphy),
563 DEVMETHOD(etherswitch_writephyreg, ukswitch_writephy),
564 DEVMETHOD(etherswitch_getport, ukswitch_getport),
565 DEVMETHOD(etherswitch_setport, ukswitch_setport),
566 DEVMETHOD(etherswitch_getvgroup, ukswitch_getvgroup),
567 DEVMETHOD(etherswitch_setvgroup, ukswitch_setvgroup),
572 DEFINE_CLASS_0(ukswitch, ukswitch_driver, ukswitch_methods,
573 sizeof(struct ukswitch_softc));
575 DRIVER_MODULE(ukswitch, mdio, ukswitch_driver, 0, 0);
576 DRIVER_MODULE(miibus, ukswitch, miibus_driver, 0, 0);
577 DRIVER_MODULE(mdio, ukswitch, mdio_driver, 0, 0);
578 DRIVER_MODULE(etherswitch, ukswitch, etherswitch_driver, 0, 0);
579 MODULE_VERSION(ukswitch, 1);
580 MODULE_DEPEND(ukswitch, miibus, 1, 1, 1); /* XXX which versions? */
581 MODULE_DEPEND(ukswitch, etherswitch, 1, 1, 1); /* XXX which versions? */