1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2013 Luiz Otavio O Souza.
5 * Copyright (c) 2011-2012 Stefan Bethke.
6 * Copyright (c) 2012 Adrian Chadd.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.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>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/ethernet.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49
50 #include <machine/bus.h>
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include <dev/mdio/mdio.h>
54
55 #include <dev/etherswitch/etherswitch.h>
56
57 #include "mdio_if.h"
58 #include "miibus_if.h"
59 #include "etherswitch_if.h"
60
61 MALLOC_DECLARE(M_UKSWITCH);
62 MALLOC_DEFINE(M_UKSWITCH, "ukswitch", "ukswitch data structures");
63
64 struct ukswitch_softc {
65 struct mtx sc_mtx; /* serialize access to softc */
66 device_t sc_dev;
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];
73 int *portphy;
74 char **ifname;
75 device_t **miibus;
76 if_t *ifp;
77 struct callout callout_tick;
78 etherswitch_info_t info;
79 };
80
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)
89
90 #if defined(DEBUG)
91 #define DPRINTF(dev, args...) device_printf(dev, args)
92 #else
93 #define DPRINTF(dev, args...)
94 #endif
95
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 *);
100
101 static int
ukswitch_probe(device_t dev)102 ukswitch_probe(device_t dev)
103 {
104 struct ukswitch_softc *sc;
105
106 sc = device_get_softc(dev);
107 bzero(sc, sizeof(*sc));
108
109 device_set_desc(dev, "Generic MDIO switch driver");
110 return (BUS_PROBE_DEFAULT);
111 }
112
113 static int
ukswitch_attach_phys(struct ukswitch_softc * sc)114 ukswitch_attach_phys(struct ukswitch_softc *sc)
115 {
116 int phy, port = 0, err = 0;
117 char name[IFNAMSIZ];
118
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)
123 continue;
124 sc->ifpport[phy] = port;
125 sc->portphy[port] = phy;
126 sc->ifp[port] = if_alloc(IFT_ETHER);
127 if_setsoftc(sc->ifp[port], sc);
128 if_setflags(sc->ifp[port], IFF_UP | IFF_BROADCAST |
129 IFF_DRV_RUNNING | IFF_SIMPLEX);
130 sc->ifname[port] = malloc(strlen(name)+1, M_UKSWITCH, M_WAITOK);
131 bcopy(name, sc->ifname[port], strlen(name)+1);
132 if_initname(sc->ifp[port], sc->ifname[port], port);
133 sc->miibus[port] = malloc(sizeof(device_t), M_UKSWITCH,
134 M_WAITOK | M_ZERO);
135 err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port],
136 ukswitch_ifmedia_upd, ukswitch_ifmedia_sts, \
137 BMSR_DEFCAPMASK, phy + sc->phyoffset, MII_OFFSET_ANY, 0);
138 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
139 device_get_nameunit(*sc->miibus[port]),
140 if_name(sc->ifp[port]));
141 if (err != 0) {
142 device_printf(sc->sc_dev,
143 "attaching PHY %d failed\n",
144 phy);
145 break;
146 }
147 sc->info.es_nports = port + 1;
148 if (++port >= sc->numports)
149 break;
150 }
151 return (err);
152 }
153
154 static int
ukswitch_attach(device_t dev)155 ukswitch_attach(device_t dev)
156 {
157 struct ukswitch_softc *sc;
158 int err = 0;
159
160 sc = device_get_softc(dev);
161
162 sc->sc_dev = dev;
163 mtx_init(&sc->sc_mtx, "ukswitch", NULL, MTX_DEF);
164 strlcpy(sc->info.es_name, device_get_desc(dev),
165 sizeof(sc->info.es_name));
166
167 /* XXX Defaults */
168 sc->numports = 6;
169 sc->phymask = 0x0f;
170 sc->phyoffset = 0;
171 sc->cpuport = -1;
172 sc->media = 100;
173
174 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
175 "numports", &sc->numports);
176 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
177 "phymask", &sc->phymask);
178 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
179 "phyoffset", &sc->phyoffset);
180 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
181 "cpuport", &sc->cpuport);
182 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
183 "media", &sc->media);
184
185 /* Support only fast and giga ethernet. */
186 if (sc->media != 100 && sc->media != 1000)
187 sc->media = 100;
188
189 if (sc->cpuport != -1)
190 /* Always attach the cpu port. */
191 sc->phymask |= (1 << sc->cpuport);
192
193 /* We do not support any vlan groups. */
194 sc->info.es_nvlangroups = 0;
195
196 sc->ifp = malloc(sizeof(if_t) * sc->numports, M_UKSWITCH,
197 M_WAITOK | M_ZERO);
198 sc->ifname = malloc(sizeof(char *) * sc->numports, M_UKSWITCH,
199 M_WAITOK | M_ZERO);
200 sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_UKSWITCH,
201 M_WAITOK | M_ZERO);
202 sc->portphy = malloc(sizeof(int) * sc->numports, M_UKSWITCH,
203 M_WAITOK | M_ZERO);
204
205 /*
206 * Attach the PHYs and complete the bus enumeration.
207 */
208 err = ukswitch_attach_phys(sc);
209 if (err != 0)
210 return (err);
211
212 bus_identify_children(dev);
213 bus_enumerate_hinted_children(dev);
214 bus_attach_children(dev);
215
216 callout_init(&sc->callout_tick, 0);
217
218 ukswitch_tick(sc);
219
220 return (err);
221 }
222
223 static int
ukswitch_detach(device_t dev)224 ukswitch_detach(device_t dev)
225 {
226 struct ukswitch_softc *sc = device_get_softc(dev);
227 int error, i, port;
228
229 error = bus_generic_detach(dev);
230 if (error != 0)
231 return (error);
232
233 callout_drain(&sc->callout_tick);
234
235 for (i=0; i < MII_NPHY; i++) {
236 if (((1 << i) & sc->phymask) == 0)
237 continue;
238 port = ukswitch_portforphy(sc, i);
239 if (sc->ifp[port] != NULL)
240 if_free(sc->ifp[port]);
241 free(sc->ifname[port], M_UKSWITCH);
242 free(sc->miibus[port], M_UKSWITCH);
243 }
244
245 free(sc->portphy, M_UKSWITCH);
246 free(sc->miibus, M_UKSWITCH);
247 free(sc->ifname, M_UKSWITCH);
248 free(sc->ifp, M_UKSWITCH);
249
250 mtx_destroy(&sc->sc_mtx);
251
252 return (0);
253 }
254
255 /*
256 * Convert PHY number to port number.
257 */
258 static inline int
ukswitch_portforphy(struct ukswitch_softc * sc,int phy)259 ukswitch_portforphy(struct ukswitch_softc *sc, int phy)
260 {
261
262 return (sc->ifpport[phy]);
263 }
264
265 static inline struct mii_data *
ukswitch_miiforport(struct ukswitch_softc * sc,int port)266 ukswitch_miiforport(struct ukswitch_softc *sc, int port)
267 {
268
269 if (port < 0 || port > sc->numports)
270 return (NULL);
271 return (device_get_softc(*sc->miibus[port]));
272 }
273
274 static inline if_t
ukswitch_ifpforport(struct ukswitch_softc * sc,int port)275 ukswitch_ifpforport(struct ukswitch_softc *sc, int port)
276 {
277
278 if (port < 0 || port > sc->numports)
279 return (NULL);
280 return (sc->ifp[port]);
281 }
282
283 /*
284 * Poll the status for all PHYs.
285 */
286 static void
ukswitch_miipollstat(struct ukswitch_softc * sc)287 ukswitch_miipollstat(struct ukswitch_softc *sc)
288 {
289 int i, port;
290 struct mii_data *mii;
291 struct mii_softc *miisc;
292
293 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
294
295 for (i = 0; i < MII_NPHY; i++) {
296 if (((1 << i) & sc->phymask) == 0)
297 continue;
298 port = ukswitch_portforphy(sc, i);
299 if ((*sc->miibus[port]) == NULL)
300 continue;
301 mii = device_get_softc(*sc->miibus[port]);
302 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
303 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
304 miisc->mii_inst)
305 continue;
306 ukphy_status(miisc);
307 mii_phy_update(miisc, MII_POLLSTAT);
308 }
309 }
310 }
311
312 static void
ukswitch_tick(void * arg)313 ukswitch_tick(void *arg)
314 {
315 struct ukswitch_softc *sc = arg;
316
317 ukswitch_miipollstat(sc);
318 callout_reset(&sc->callout_tick, hz, ukswitch_tick, sc);
319 }
320
321 static void
ukswitch_lock(device_t dev)322 ukswitch_lock(device_t dev)
323 {
324 struct ukswitch_softc *sc = device_get_softc(dev);
325
326 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
327 UKSWITCH_LOCK(sc);
328 }
329
330 static void
ukswitch_unlock(device_t dev)331 ukswitch_unlock(device_t dev)
332 {
333 struct ukswitch_softc *sc = device_get_softc(dev);
334
335 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
336 UKSWITCH_UNLOCK(sc);
337 }
338
339 static etherswitch_info_t *
ukswitch_getinfo(device_t dev)340 ukswitch_getinfo(device_t dev)
341 {
342 struct ukswitch_softc *sc = device_get_softc(dev);
343
344 return (&sc->info);
345 }
346
347 static int
ukswitch_getport(device_t dev,etherswitch_port_t * p)348 ukswitch_getport(device_t dev, etherswitch_port_t *p)
349 {
350 struct ukswitch_softc *sc = device_get_softc(dev);
351 struct mii_data *mii;
352 struct ifmediareq *ifmr = &p->es_ifmr;
353 int err, phy;
354
355 if (p->es_port < 0 || p->es_port >= sc->numports)
356 return (ENXIO);
357 p->es_pvid = 0;
358
359 phy = sc->portphy[p->es_port];
360 mii = ukswitch_miiforport(sc, p->es_port);
361 if (sc->cpuport != -1 && phy == sc->cpuport) {
362 /* fill in fixed values for CPU port */
363 p->es_flags |= ETHERSWITCH_PORT_CPU;
364 ifmr->ifm_count = 0;
365 if (sc->media == 100)
366 ifmr->ifm_current = ifmr->ifm_active =
367 IFM_ETHER | IFM_100_TX | IFM_FDX;
368 else
369 ifmr->ifm_current = ifmr->ifm_active =
370 IFM_ETHER | IFM_1000_T | IFM_FDX;
371 ifmr->ifm_mask = 0;
372 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
373 } else if (mii != NULL) {
374 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
375 &mii->mii_media, SIOCGIFMEDIA);
376 if (err)
377 return (err);
378 } else {
379 return (ENXIO);
380 }
381 return (0);
382 }
383
384 static int
ukswitch_setport(device_t dev,etherswitch_port_t * p)385 ukswitch_setport(device_t dev, etherswitch_port_t *p)
386 {
387 struct ukswitch_softc *sc = device_get_softc(dev);
388 struct ifmedia *ifm;
389 struct mii_data *mii;
390 if_t ifp;
391 int err;
392
393 if (p->es_port < 0 || p->es_port >= sc->numports)
394 return (ENXIO);
395
396 if (sc->portphy[p->es_port] == sc->cpuport)
397 return (ENXIO);
398
399 mii = ukswitch_miiforport(sc, p->es_port);
400 if (mii == NULL)
401 return (ENXIO);
402
403 ifp = ukswitch_ifpforport(sc, p->es_port);
404
405 ifm = &mii->mii_media;
406 err = ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA);
407 return (err);
408 }
409
410 static int
ukswitch_getvgroup(device_t dev,etherswitch_vlangroup_t * vg)411 ukswitch_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
412 {
413
414 /* Not supported. */
415 vg->es_vid = 0;
416 vg->es_member_ports = 0;
417 vg->es_untagged_ports = 0;
418 vg->es_fid = 0;
419 return (0);
420 }
421
422 static int
ukswitch_setvgroup(device_t dev,etherswitch_vlangroup_t * vg)423 ukswitch_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
424 {
425
426 /* Not supported. */
427 return (0);
428 }
429
430 static void
ukswitch_statchg(device_t dev)431 ukswitch_statchg(device_t dev)
432 {
433
434 DPRINTF(dev, "%s\n", __func__);
435 }
436
437 static int
ukswitch_ifmedia_upd(if_t ifp)438 ukswitch_ifmedia_upd(if_t ifp)
439 {
440 struct ukswitch_softc *sc = if_getsoftc(ifp);
441 struct mii_data *mii = ukswitch_miiforport(sc, if_getdunit(ifp));
442
443 DPRINTF(sc->sc_dev, "%s\n", __func__);
444 if (mii == NULL)
445 return (ENXIO);
446 mii_mediachg(mii);
447 return (0);
448 }
449
450 static void
ukswitch_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)451 ukswitch_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
452 {
453 struct ukswitch_softc *sc = if_getsoftc(ifp);
454 struct mii_data *mii = ukswitch_miiforport(sc, if_getdunit(ifp));
455
456 DPRINTF(sc->sc_dev, "%s\n", __func__);
457
458 if (mii == NULL)
459 return;
460 mii_pollstat(mii);
461 ifmr->ifm_active = mii->mii_media_active;
462 ifmr->ifm_status = mii->mii_media_status;
463 }
464
465 static int
ukswitch_readphy(device_t dev,int phy,int reg)466 ukswitch_readphy(device_t dev, int phy, int reg)
467 {
468 struct ukswitch_softc *sc;
469 int data;
470
471 sc = device_get_softc(dev);
472 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
473
474 if (phy < 0 || phy >= 32)
475 return (ENXIO);
476 if (reg < 0 || reg >= 32)
477 return (ENXIO);
478
479 UKSWITCH_LOCK(sc);
480 data = MDIO_READREG(device_get_parent(dev), phy, reg);
481 UKSWITCH_UNLOCK(sc);
482
483 return (data);
484 }
485
486 static int
ukswitch_writephy(device_t dev,int phy,int reg,int data)487 ukswitch_writephy(device_t dev, int phy, int reg, int data)
488 {
489 struct ukswitch_softc *sc;
490 int err;
491
492 sc = device_get_softc(dev);
493 UKSWITCH_LOCK_ASSERT(sc, MA_NOTOWNED);
494
495 if (phy < 0 || phy >= 32)
496 return (ENXIO);
497 if (reg < 0 || reg >= 32)
498 return (ENXIO);
499
500 UKSWITCH_LOCK(sc);
501 err = MDIO_WRITEREG(device_get_parent(dev), phy, reg, data);
502 UKSWITCH_UNLOCK(sc);
503
504 return (err);
505 }
506
507 static int
ukswitch_readreg(device_t dev,int addr)508 ukswitch_readreg(device_t dev, int addr)
509 {
510 struct ukswitch_softc *sc __diagused;
511
512 sc = device_get_softc(dev);
513 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
514
515 /* Not supported. */
516 return (0);
517 }
518
519 static int
ukswitch_writereg(device_t dev,int addr,int value)520 ukswitch_writereg(device_t dev, int addr, int value)
521 {
522 struct ukswitch_softc *sc __diagused;
523
524 sc = device_get_softc(dev);
525 UKSWITCH_LOCK_ASSERT(sc, MA_OWNED);
526
527 /* Not supported. */
528 return (0);
529 }
530
531 static device_method_t ukswitch_methods[] = {
532 /* Device interface */
533 DEVMETHOD(device_probe, ukswitch_probe),
534 DEVMETHOD(device_attach, ukswitch_attach),
535 DEVMETHOD(device_detach, ukswitch_detach),
536
537 /* bus interface */
538 DEVMETHOD(bus_add_child, device_add_child_ordered),
539
540 /* MII interface */
541 DEVMETHOD(miibus_readreg, ukswitch_readphy),
542 DEVMETHOD(miibus_writereg, ukswitch_writephy),
543 DEVMETHOD(miibus_statchg, ukswitch_statchg),
544
545 /* MDIO interface */
546 DEVMETHOD(mdio_readreg, ukswitch_readphy),
547 DEVMETHOD(mdio_writereg, ukswitch_writephy),
548
549 /* etherswitch interface */
550 DEVMETHOD(etherswitch_lock, ukswitch_lock),
551 DEVMETHOD(etherswitch_unlock, ukswitch_unlock),
552 DEVMETHOD(etherswitch_getinfo, ukswitch_getinfo),
553 DEVMETHOD(etherswitch_readreg, ukswitch_readreg),
554 DEVMETHOD(etherswitch_writereg, ukswitch_writereg),
555 DEVMETHOD(etherswitch_readphyreg, ukswitch_readphy),
556 DEVMETHOD(etherswitch_writephyreg, ukswitch_writephy),
557 DEVMETHOD(etherswitch_getport, ukswitch_getport),
558 DEVMETHOD(etherswitch_setport, ukswitch_setport),
559 DEVMETHOD(etherswitch_getvgroup, ukswitch_getvgroup),
560 DEVMETHOD(etherswitch_setvgroup, ukswitch_setvgroup),
561
562 DEVMETHOD_END
563 };
564
565 DEFINE_CLASS_0(ukswitch, ukswitch_driver, ukswitch_methods,
566 sizeof(struct ukswitch_softc));
567
568 DRIVER_MODULE(ukswitch, mdio, ukswitch_driver, 0, 0);
569 DRIVER_MODULE(miibus, ukswitch, miibus_driver, 0, 0);
570 DRIVER_MODULE(mdio, ukswitch, mdio_driver, 0, 0);
571 DRIVER_MODULE(etherswitch, ukswitch, etherswitch_driver, 0, 0);
572 MODULE_VERSION(ukswitch, 1);
573 MODULE_DEPEND(ukswitch, miibus, 1, 1, 1); /* XXX which versions? */
574 MODULE_DEPEND(ukswitch, etherswitch, 1, 1, 1); /* XXX which versions? */
575