2 * Copyright (c) 2011-2012 Stefan Bethke.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 #include <sys/systm.h>
39 #include <net/if_media.h>
41 #include <dev/etherswitch/miiproxy.h>
42 #include <dev/mii/mii.h>
43 #include <dev/mii/miivar.h>
46 #include "miibus_if.h"
49 MALLOC_DECLARE(M_MIIPROXY);
50 MALLOC_DEFINE(M_MIIPROXY, "miiproxy", "miiproxy data structures");
52 driver_t miiproxy_driver;
53 driver_t mdioproxy_driver;
55 struct miiproxy_softc {
61 struct mdioproxy_softc {
65 * The rendezvous data structures and functions allow two device endpoints to
66 * match up, so that the proxy endpoint can be associated with a target
67 * endpoint. The proxy has to know the device name of the target that it
68 * wants to associate with, for example through a hint. The rendezvous code
69 * makes no assumptions about the devices that want to meet.
71 struct rendezvous_entry;
78 typedef int (*rendezvous_callback_t)(enum rendezvous_op,
79 struct rendezvous_entry *);
81 static SLIST_HEAD(rendezvoushead, rendezvous_entry) rendezvoushead =
82 SLIST_HEAD_INITIALIZER(rendezvoushead);
84 struct rendezvous_endpoint {
87 rendezvous_callback_t callback;
90 struct rendezvous_entry {
91 SLIST_ENTRY(rendezvous_entry) entries;
92 struct rendezvous_endpoint proxy;
93 struct rendezvous_endpoint target;
97 * Call the callback routines for both the proxy and the target. If either
98 * returns an error, undo the attachment.
101 rendezvous_attach(struct rendezvous_entry *e, struct rendezvous_endpoint *ep)
105 error = e->proxy.callback(RENDEZVOUS_ATTACH, e);
107 error = e->target.callback(RENDEZVOUS_ATTACH, e);
109 e->proxy.callback(RENDEZVOUS_DETACH, e);
118 * Create an entry for the proxy in the rendezvous list. The name parameter
119 * indicates the name of the device that is the target endpoint for this
120 * rendezvous. The callback will be invoked as soon as the target is
121 * registered: either immediately if the target registered itself earlier,
122 * or once the target registers. Returns ENXIO if the target has not yet
126 rendezvous_register_proxy(device_t dev, const char *name,
127 rendezvous_callback_t callback)
129 struct rendezvous_entry *e;
131 KASSERT(callback != NULL, ("callback must be set"));
132 SLIST_FOREACH(e, &rendezvoushead, entries) {
133 if (strcmp(name, e->target.name) == 0) {
134 /* the target is already attached */
135 e->proxy.name = device_get_nameunit(dev);
136 e->proxy.device = dev;
137 e->proxy.callback = callback;
138 return (rendezvous_attach(e, &e->proxy));
141 e = malloc(sizeof(*e), M_MIIPROXY, M_WAITOK | M_ZERO);
142 e->proxy.name = device_get_nameunit(dev);
143 e->proxy.device = dev;
144 e->proxy.callback = callback;
145 e->target.name = name;
146 SLIST_INSERT_HEAD(&rendezvoushead, e, entries);
151 * Create an entry in the rendezvous list for the target.
152 * Returns ENXIO if the proxy has not yet registered.
155 rendezvous_register_target(device_t dev, rendezvous_callback_t callback)
157 struct rendezvous_entry *e;
160 KASSERT(callback != NULL, ("callback must be set"));
161 name = device_get_nameunit(dev);
162 SLIST_FOREACH(e, &rendezvoushead, entries) {
163 if (strcmp(name, e->target.name) == 0) {
164 e->target.device = dev;
165 e->target.callback = callback;
166 return (rendezvous_attach(e, &e->target));
169 e = malloc(sizeof(*e), M_MIIPROXY, M_WAITOK | M_ZERO);
170 e->target.name = name;
171 e->target.device = dev;
172 e->target.callback = callback;
173 SLIST_INSERT_HEAD(&rendezvoushead, e, entries);
178 * Remove the registration for the proxy.
181 rendezvous_unregister_proxy(device_t dev)
183 struct rendezvous_entry *e;
186 SLIST_FOREACH(e, &rendezvoushead, entries) {
187 if (e->proxy.device == dev) {
188 if (e->target.device == NULL) {
189 SLIST_REMOVE(&rendezvoushead, e, rendezvous_entry, entries);
193 e->proxy.callback(RENDEZVOUS_DETACH, e);
194 e->target.callback(RENDEZVOUS_DETACH, e);
196 e->proxy.device = NULL;
197 e->proxy.callback = NULL;
205 * Remove the registration for the target.
208 rendezvous_unregister_target(device_t dev)
210 struct rendezvous_entry *e;
213 SLIST_FOREACH(e, &rendezvoushead, entries) {
214 if (e->target.device == dev) {
215 if (e->proxy.device == NULL) {
216 SLIST_REMOVE(&rendezvoushead, e, rendezvous_entry, entries);
220 e->proxy.callback(RENDEZVOUS_DETACH, e);
221 e->target.callback(RENDEZVOUS_DETACH, e);
223 e->target.device = NULL;
224 e->target.callback = NULL;
232 * Functions of the proxy that is interposed between the ethernet interface
233 * driver and the miibus device.
237 miiproxy_rendezvous_callback(enum rendezvous_op op, struct rendezvous_entry *rendezvous)
239 struct miiproxy_softc *sc = device_get_softc(rendezvous->proxy.device);
242 case RENDEZVOUS_ATTACH:
243 sc->mdio = device_get_parent(rendezvous->target.device);
245 case RENDEZVOUS_DETACH:
253 miiproxy_probe(device_t dev)
255 device_set_desc(dev, "MII/MDIO proxy, MII side");
257 return (BUS_PROBE_SPECIFIC);
261 miiproxy_attach(device_t dev)
265 * The ethernet interface needs to call mii_attach_proxy() to pass
266 * the relevant parameters for rendezvous with the MDIO target.
268 return (bus_generic_attach(dev));
272 miiproxy_detach(device_t dev)
275 rendezvous_unregister_proxy(dev);
276 bus_generic_detach(dev);
281 miiproxy_readreg(device_t dev, int phy, int reg)
283 struct miiproxy_softc *sc = device_get_softc(dev);
285 if (sc->mdio != NULL)
286 return (MDIO_READREG(sc->mdio, phy, reg));
291 miiproxy_writereg(device_t dev, int phy, int reg, int val)
293 struct miiproxy_softc *sc = device_get_softc(dev);
295 if (sc->mdio != NULL)
296 return (MDIO_WRITEREG(sc->mdio, phy, reg, val));
301 miiproxy_statchg(device_t dev)
304 MIIBUS_STATCHG(device_get_parent(dev));
308 miiproxy_linkchg(device_t dev)
311 MIIBUS_LINKCHG(device_get_parent(dev));
315 miiproxy_mediainit(device_t dev)
318 MIIBUS_MEDIAINIT(device_get_parent(dev));
322 * Functions for the MDIO target device driver.
325 mdioproxy_rendezvous_callback(enum rendezvous_op op, struct rendezvous_entry *rendezvous)
331 mdioproxy_identify(driver_t *driver, device_t parent)
335 if (device_find_child(parent, driver->name, -1) == NULL) {
336 child = BUS_ADD_CHILD(parent, 0, driver->name, -1);
341 mdioproxy_probe(device_t dev)
343 device_set_desc(dev, "MII/MDIO proxy, MDIO side");
345 return (BUS_PROBE_SPECIFIC);
349 mdioproxy_attach(device_t dev)
352 rendezvous_register_target(dev, mdioproxy_rendezvous_callback);
353 return (bus_generic_attach(dev));
357 mdioproxy_detach(device_t dev)
360 rendezvous_unregister_target(dev);
361 bus_generic_detach(dev);
366 * Attach this proxy in place of miibus. The target MDIO must be attached
367 * already. Returns NULL on error.
370 mii_attach_proxy(device_t dev)
372 struct miiproxy_softc *sc;
377 if (resource_string_value(device_get_name(dev),
378 device_get_unit(dev), "mdio", &name) != 0) {
380 printf("mii_attach_proxy: not attaching, no mdio"
381 " device hint for %s\n", device_get_nameunit(dev));
385 miiproxy = device_add_child(dev, miiproxy_driver.name, -1);
386 error = bus_generic_attach(dev);
388 device_printf(dev, "can't attach miiproxy\n");
391 sc = device_get_softc(miiproxy);
393 sc->proxy = miiproxy;
394 if (rendezvous_register_proxy(miiproxy, name, miiproxy_rendezvous_callback) != 0) {
395 device_printf(dev, "can't attach proxy\n");
398 device_printf(miiproxy, "attached to target %s\n", device_get_nameunit(sc->mdio));
402 static device_method_t miiproxy_methods[] = {
403 /* device interface */
404 DEVMETHOD(device_probe, miiproxy_probe),
405 DEVMETHOD(device_attach, miiproxy_attach),
406 DEVMETHOD(device_detach, miiproxy_detach),
407 DEVMETHOD(device_shutdown, bus_generic_shutdown),
410 DEVMETHOD(miibus_readreg, miiproxy_readreg),
411 DEVMETHOD(miibus_writereg, miiproxy_writereg),
412 DEVMETHOD(miibus_statchg, miiproxy_statchg),
413 DEVMETHOD(miibus_linkchg, miiproxy_linkchg),
414 DEVMETHOD(miibus_mediainit, miiproxy_mediainit),
419 static device_method_t mdioproxy_methods[] = {
420 /* device interface */
421 DEVMETHOD(device_identify, mdioproxy_identify),
422 DEVMETHOD(device_probe, mdioproxy_probe),
423 DEVMETHOD(device_attach, mdioproxy_attach),
424 DEVMETHOD(device_detach, mdioproxy_detach),
425 DEVMETHOD(device_shutdown, bus_generic_shutdown),
430 DEFINE_CLASS_0(miiproxy, miiproxy_driver, miiproxy_methods,
431 sizeof(struct miiproxy_softc));
432 DEFINE_CLASS_0(mdioproxy, mdioproxy_driver, mdioproxy_methods,
433 sizeof(struct mdioproxy_softc));
435 devclass_t miiproxy_devclass;
436 static devclass_t mdioproxy_devclass;
438 DRIVER_MODULE(mdioproxy, mdio, mdioproxy_driver, mdioproxy_devclass, 0, 0);
439 DRIVER_MODULE(miibus, miiproxy, miibus_driver, miibus_devclass, 0, 0);
440 MODULE_VERSION(miiproxy, 1);
441 MODULE_DEPEND(miiproxy, miibus, 1, 1, 1);