freebsd compat. layer: MII subsystem (needs testers, more to come).

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21097 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Hugo Santos 2007-05-10 04:52:43 +00:00
parent 94f814b32c
commit 4b2286de82
13 changed files with 1164 additions and 89 deletions

View File

@ -16,6 +16,8 @@ KernelStaticLibrary libfreebsd_network.a :
fbsd_ether.c
fbsd_if_media.c
fbsd_mbuf.c
fbsd_mii.c
fbsd_mii_physubr.c
if.c
mbuf.c
mii.c

View File

@ -327,6 +327,13 @@ bus_generic_resume(device_t dev)
}
void
bus_generic_shutdown(device_t dev)
{
UNIMPLEMENTED();
}
int
bus_generic_print_child(device_t dev, device_t child)
{

View File

@ -18,7 +18,7 @@
#include <compat/dev/pci/pcivar.h>
#include <compat/sys/bus.h>
// TODO a lot.
#include <compat/dev/mii/miivar.h>
#define DEBUG_PCI
@ -197,6 +197,27 @@ device_get_desc(device_t dev)
}
device_t
device_get_parent(device_t dev)
{
return dev->parent;
}
void
device_set_ivars(device_t dev, void *ivars)
{
dev->ivars = ivars;
}
void *
device_get_ivars(device_t dev)
{
return dev->ivars;
}
void
device_sprintf_name(device_t dev, const char *format, ...)
{
@ -237,6 +258,137 @@ device_get_softc(device_t dev)
}
device_t
init_device(device_t dev, driver_t *driver)
{
list_init_etc(&dev->children, offsetof(struct device, link));
if (driver == NULL)
return dev;
if (device_set_driver(dev, driver) < 0)
return NULL;
return dev;
}
int
device_set_driver(device_t dev, driver_t *driver)
{
device_method_signature_t method = NULL;
int i;
dev->softc = malloc(driver->softc_size);
if (dev->softc == NULL)
return -1;
dev->driver = driver;
for (i = 0; method == NULL && driver->methods[i].name != NULL; i++) {
device_method_t *mth = &driver->methods[i];
if (strcmp(mth->name, "device_probe") == 0)
dev->methods.probe = (void *)mth->method;
else if (strcmp(mth->name, "device_attach") == 0)
dev->methods.attach = (void *)mth->method;
else if (strcmp(mth->name, "device_detach") == 0)
dev->methods.detach = (void *)mth->method;
else if (strcmp(mth->name, "device_suspend") == 0)
dev->methods.suspend = (void *)mth->method;
else if (strcmp(mth->name, "device_resume") == 0)
dev->methods.resume = (void *)mth->method;
else if (strcmp(mth->name, "device_shutdown") == 0)
dev->methods.shutdown = (void *)mth->method;
else if (strcmp(mth->name, "miibus_readreg") == 0)
dev->methods.miibus_readreg = (void *)mth->method;
else if (strcmp(mth->name, "miibus_writereg") == 0)
dev->methods.miibus_writereg = (void *)mth->method;
else if (strcmp(mth->name, "miibus_statchg") == 0)
dev->methods.miibus_statchg = (void *)mth->method;
}
return 0;
}
void
uninit_device(device_t dev)
{
if (dev->flags & DEVICE_DESC_ALLOCED)
free((char *)dev->description);
if (dev->softc)
free(dev->softc);
}
static device_t
new_device(driver_t *driver)
{
device_t dev = malloc(sizeof(struct device));
if (dev == NULL)
return NULL;
memset(dev, 0, sizeof(struct device));
if (init_device(dev, driver) == NULL) {
free(dev);
return NULL;
}
return dev;
}
device_t
device_add_child(device_t dev, const char *name, int order)
{
device_t child = NULL;
if (name != NULL) {
if (strcmp(name, "miibus") == 0)
child = new_device(&miibus_driver);
} else
child = new_device(NULL);
if (child == NULL)
return NULL;
child->parent = dev;
list_add_item(&dev->children, child);
return child;
}
void
bus_generic_attach(device_t dev)
{
device_t child;
for (child = list_get_first_item(&dev->children);
child; child = list_get_next_item(&dev->children, child)) {
if (child->driver == NULL) {
if (dev->driver == &miibus_driver) {
driver_t *driver = __haiku_get_miibus_driver(dev);
if (driver) {
device_probe_t *probe = (device_probe_t *)
_resolve_method(driver, "device_probe");
if (probe && probe(child) >= 0)
device_set_driver(child, driver);
}
}
if (dev->driver == NULL)
panic("can't attach unknown driver");
}
child->methods.attach(child);
}
}
int
device_delete_child(device_t dev, device_t child)
{

View File

@ -189,14 +189,26 @@ struct mii_media {
#ifdef _KERNEL
int miibus_readreg(device_t dev, int phy, int reg);
int miibus_writereg(device_t dev, int phy, int reg, int data);
int __haiku_miibus_readreg(device_t dev, int phy, int reg);
int __haiku_miibus_writereg(device_t dev, int phy, int reg, int data);
void __haiku_miibus_statchg(device_t dev);
void __haiku_miibus_linkchg(device_t dev);
void __haiku_miibus_mediainit(device_t dev);
#define MIIBUS_READREG(dev, phy, reg) \
miibus_readreg((dev), (phy), (reg))
__haiku_miibus_readreg((dev), (phy), (reg))
#define MIIBUS_WRITEREG(dev, phy, reg, value) \
miibus_writereg((dev), (phy), (reg), (value))
__haiku_miibus_writereg((dev), (phy), (reg), (value))
#define MIIBUS_STATCHG(dev) \
__haiku_miibus_statchg(dev)
#define MIIBUS_LINKCHG(dev) \
__haiku_miibus_linkchg(dev)
#define MIIBUS_MEDIAINIT(dev) \
__haiku_miibus_mediainit(dev)
#define PHY_READ(p, r) \
MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r))

View File

@ -64,6 +64,7 @@ enum intr_type {
int bus_generic_detach(device_t dev);
int bus_generic_suspend(device_t dev);
int bus_generic_resume(device_t dev);
void bus_generic_shutdown(device_t dev);
typedef void (*driver_intr_t)(void *);
@ -94,12 +95,18 @@ int device_printf(device_t dev, const char *, ...) __printflike(2, 3);
void device_set_desc(device_t dev, const char *desc);
void device_set_desc_copy(device_t dev, const char *desc);
const char *device_get_desc(device_t dev);
device_t device_get_parent(device_t dev);
void device_set_ivars(device_t dev, void *);
void *device_get_ivars(device_t dev);
device_t device_add_child(device_t dev, const char *name, int unit);
int device_delete_child(device_t dev, device_t child);
int device_is_attached(device_t dev);
int bus_generic_print_child(device_t dev, device_t child);
void bus_generic_driver_added(device_t dev, driver_t *driver);
void bus_generic_attach(device_t dev);
int device_set_driver(device_t dev, driver_t *driver);
static inline struct sysctl_ctx_list *
device_get_sysctl_ctx(device_t dev)

View File

@ -0,0 +1,6 @@
#ifndef _FBSD_COMPAT_SYS_ERRNO_H_
#define _FBSD_COMPAT_SYS_ERRNO_H_
#define EJUSTRETURN (B_ERRORS_END + 1)
#endif

View File

@ -52,7 +52,7 @@ status_t _fbsd_init_driver(driver_t *);
void _fbsd_uninit_driver(driver_t *);
extern const char gDriverName[];
driver_t *__haiku_get_miibus_driver(void);
driver_t *__haiku_get_miibus_driver(device_t dev);
/* we define the driver methods with HAIKU_FBSD_DRIVER_GLUE to
* force the rest of the stuff to be linked back with the driver.
@ -80,9 +80,10 @@ driver_t *__haiku_get_miibus_driver(void);
device_hooks *find_device(const char *name) \
{ return &gDeviceHooks; }
#define HAIKU_FBSD_MII_DRIVER(name) \
driver_t *__haiku_get_miibus_driver(device_t dev) \
{ return DRIVER_MODULE_NAME(name, miibus); }
#define HAIKU_FBSD_MII_DRIVER(name) \
driver_t *__haiku_get_miibus_driver(device_t dev) \
{ extern driver_t *DRIVER_MODULE_NAME(name, miibus); \
return DRIVER_MODULE_NAME(name, miibus); }
#define HAIKU_NO_FBSD_MII_DRIVER() \
driver_t *__haiku_get_miibus_driver(device_t dev) \

View File

@ -3,6 +3,7 @@
#define DECLARE_MODULE(name, data, sub, order)
#define MODULE_VERSION(name, version)
#define MODULE_DEPEND(module, mdepend, vmin, vpref, vmax)
#endif

View File

@ -30,52 +30,6 @@ struct network_device *gDevices[MAX_DEVICES];
const char *gDevNameList[MAX_DEVICES + 1];
static device_t
init_device(device_t dev, driver_t *driver)
{
device_method_signature_t method = NULL;
int i;
dev->driver = driver;
dev->softc = malloc(driver->softc_size);
if (dev->softc == NULL)
return NULL;
for (i = 0; method == NULL && driver->methods[i].name != NULL; i++) {
device_method_t *mth = &driver->methods[i];
if (strcmp(mth->name, "device_probe") == 0)
dev->methods.probe = (void *)mth->method;
else if (strcmp(mth->name, "device_attach") == 0)
dev->methods.attach = (void *)mth->method;
else if (strcmp(mth->name, "device_detach") == 0)
dev->methods.detach = (void *)mth->method;
else if (strcmp(mth->name, "device_suspend") == 0)
dev->methods.suspend = (void *)mth->method;
else if (strcmp(mth->name, "device_resume") == 0)
dev->methods.resume = (void *)mth->method;
else if (strcmp(mth->name, "device_shutdown") == 0)
dev->methods.shutdown = (void *)mth->method;
else if (strcmp(mth->name, "miibus_readreg") == 0)
dev->methods.miibus_readreg = (void *)mth->method;
else if (strcmp(mth->name, "miibus_writereg") == 0)
dev->methods.miibus_writereg = (void *)mth->method;
else if (strcmp(mth->name, "miibus_statchg") == 0)
dev->methods.miibus_statchg = (void *)mth->method;
}
return dev;
}
static void
uninit_device(device_t dev)
{
if (dev->flags & DEVICE_DESC_ALLOCED)
free((char *)dev->description);
free(dev->softc);
}
static struct network_device *
allocate_device(driver_t *driver)
{
@ -119,7 +73,7 @@ free_device(struct network_device *dev)
}
static device_method_signature_t
device_method_signature_t
_resolve_method(driver_t *driver, const char *name)
{
device_method_signature_t method = NULL;

View File

@ -16,6 +16,8 @@
#include <KernelExport.h>
#include <drivers/PCI.h>
#include <util/list.h>
#include <net_stack.h>
#include <compat/sys/kernel.h>
@ -29,6 +31,7 @@ struct device {
char dev_name[128];
driver_t *driver;
struct list children;
int32 flags;
@ -36,6 +39,7 @@ struct device {
char nameunit[64];
const char * description;
void * softc;
void * ivars;
struct {
int (*probe)(device_t dev);
@ -48,7 +52,11 @@ struct device {
int (*miibus_readreg)(device_t, int, int);
int (*miibus_writereg)(device_t, int, int, int);
void (*miibus_statchg)(device_t);
void (*miibus_linkchg)(device_t);
void (*miibus_mediainit)(device_t);
} methods;
struct list_link link;
};
@ -113,6 +121,10 @@ void driver_vprintf(const char *format, va_list vl);
void device_sprintf_name(device_t dev, const char *format, ...)
__attribute__ ((format (__printf__, 2, 3)));
device_t init_device(device_t dev, driver_t *driver);
void uninit_device(device_t dev);
device_method_signature_t _resolve_method(driver_t *driver, const char *name);
void ifq_init(struct ifqueue *ifq, const char *name);
void ifq_uninit(struct ifqueue *ifq);

View File

@ -0,0 +1,394 @@
/* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "device.h"
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/sys/dev/mii/mii.c,v 1.26.2.1 2006/03/17 20:17:43 glebius Exp $");
/*
* MII bus layer, glues MII-capable network interface drivers to sharable
* PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
* plus some NetBSD extensions.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/route.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
MODULE_VERSION(miibus, 1);
#include "miibus_if.h"
static int miibus_child_location_str(device_t bus, device_t child, char *buf,
size_t buflen);
static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
size_t buflen);
static int miibus_readreg(device_t, int, int);
static int miibus_writereg(device_t, int, int, int);
static void miibus_statchg(device_t);
static void miibus_linkchg(device_t);
static void miibus_mediainit(device_t);
static device_method_t miibus_methods[] = {
/* device interface */
DEVMETHOD(device_probe, miibus_probe),
DEVMETHOD(device_attach, miibus_attach),
DEVMETHOD(device_detach, miibus_detach),
DEVMETHOD(device_shutdown, bus_generic_shutdown),
/* bus interface */
DEVMETHOD(bus_print_child, bus_generic_print_child),
DEVMETHOD(bus_driver_added, bus_generic_driver_added),
DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
DEVMETHOD(bus_child_location_str, miibus_child_location_str),
/* MII interface */
DEVMETHOD(miibus_readreg, miibus_readreg),
DEVMETHOD(miibus_writereg, miibus_writereg),
DEVMETHOD(miibus_statchg, miibus_statchg),
DEVMETHOD(miibus_linkchg, miibus_linkchg),
DEVMETHOD(miibus_mediainit, miibus_mediainit),
{ 0, 0 }
};
devclass_t miibus_devclass;
driver_t miibus_driver = {
"miibus",
miibus_methods,
sizeof(struct mii_data)
};
/*
* Helper function used by network interface drivers, attaches PHYs
* to the network interface driver parent.
*/
int
miibus_probe(device_t dev)
{
struct mii_attach_args ma, *args;
struct mii_data *mii;
device_t child = NULL, parent;
int bmsr, capmask = 0xFFFFFFFF;
mii = device_get_softc(dev);
parent = device_get_parent(dev);
LIST_INIT(&mii->mii_phys);
for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
/*
* Check to see if there is a PHY at this address. Note,
* many braindead PHYs report 0/0 in their ID registers,
* so we test for media in the BMSR.
*/
bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
if (bmsr == 0 || bmsr == 0xffff ||
(bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
/* Assume no PHY at this address. */
continue;
}
/*
* Extract the IDs. Braindead PHYs will be handled by
* the `ukphy' driver, as we have no ID information to
* match on.
*/
ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
MII_PHYIDR1);
ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
MII_PHYIDR2);
ma.mii_data = mii;
ma.mii_capmask = capmask;
args = kernel_malloc(sizeof(struct mii_attach_args),
M_DEVBUF, M_NOWAIT);
bcopy((char *)&ma, (char *)args, sizeof(ma));
child = device_add_child(dev, NULL, -1);
device_set_ivars(child, args);
}
if (child == NULL)
return(ENXIO);
device_set_desc(dev, "MII bus");
return(0);
}
int
miibus_attach(device_t dev)
{
void **v;
ifm_change_cb_t ifmedia_upd;
ifm_stat_cb_t ifmedia_sts;
struct mii_data *mii;
mii = device_get_softc(dev);
mii->mii_ifp = NETDEV(device_get_parent(dev))->ifp;
v = device_get_ivars(dev);
ifmedia_upd = v[0];
ifmedia_sts = v[1];
ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
bus_generic_attach(dev);
return(0);
}
int
miibus_detach(device_t dev)
{
struct mii_data *mii;
bus_generic_detach(dev);
mii = device_get_softc(dev);
ifmedia_removeall(&mii->mii_media);
mii->mii_ifp = NULL;
return(0);
}
static int
miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
size_t buflen)
{
struct mii_attach_args *maa = device_get_ivars(child);
snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
MII_OUI(maa->mii_id1, maa->mii_id2),
MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2));
return (0);
}
static int
miibus_child_location_str(device_t bus, device_t child, char *buf,
size_t buflen)
{
struct mii_attach_args *maa = device_get_ivars(child);
snprintf(buf, buflen, "phyno=%d", maa->mii_phyno);
return (0);
}
static int
miibus_readreg(device_t dev, int phy, int reg)
{
device_t parent;
parent = device_get_parent(dev);
return(MIIBUS_READREG(parent, phy, reg));
}
static int
miibus_writereg(device_t dev, int phy, int reg, int data)
{
device_t parent;
parent = device_get_parent(dev);
return(MIIBUS_WRITEREG(parent, phy, reg, data));
}
static void
miibus_statchg(device_t dev)
{
device_t parent;
struct mii_data *mii;
struct ifnet *ifp;
parent = device_get_parent(dev);
MIIBUS_STATCHG(parent);
mii = device_get_softc(dev);
ifp = NETDEV(parent)->ifp;
ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
return;
}
static void
miibus_linkchg(device_t dev)
{
struct mii_data *mii;
device_t parent;
int link_state;
parent = device_get_parent(dev);
MIIBUS_LINKCHG(parent);
mii = device_get_softc(dev);
if (mii->mii_media_status & IFM_AVALID) {
if (mii->mii_media_status & IFM_ACTIVE)
link_state = LINK_STATE_UP;
else
link_state = LINK_STATE_DOWN;
} else
link_state = LINK_STATE_UNKNOWN;
if_link_state_change(NETDEV(parent)->ifp, link_state);
}
static void
miibus_mediainit(device_t dev)
{
struct mii_data *mii;
struct ifmedia_entry *m;
int media = 0;
/* Poke the parent in case it has any media of its own to add. */
MIIBUS_MEDIAINIT(device_get_parent(dev));
mii = device_get_softc(dev);
LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
media = m->ifm_media;
if (media == (IFM_ETHER|IFM_AUTO))
break;
}
ifmedia_set(&mii->mii_media, media);
return;
}
int
mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
ifm_stat_cb_t ifmedia_sts)
{
void **v;
int bmsr, i;
v = kernel_malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT);
if (v == 0) {
return (ENOMEM);
}
v[0] = ifmedia_upd;
v[1] = ifmedia_sts;
*child = device_add_child(dev, "miibus", -1);
device_set_ivars(*child, v);
for (i = 0; i < MII_NPHY; i++) {
bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
if (bmsr == 0 || bmsr == 0xffff ||
(bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
/* Assume no PHY at this address. */
continue;
} else
break;
}
if (i == MII_NPHY) {
device_delete_child(dev, *child);
*child = NULL;
return(ENXIO);
}
bus_generic_attach(dev);
return(0);
}
/*
* Media changed; notify all PHYs.
*/
int
mii_mediachg(struct mii_data *mii)
{
struct mii_softc *child;
int rv;
mii->mii_media_status = 0;
mii->mii_media_active = IFM_NONE;
LIST_FOREACH(child, &mii->mii_phys, mii_list) {
rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
if (rv)
return (rv);
}
return (0);
}
/*
* Call the PHY tick routines, used during autonegotiation.
*/
void
mii_tick(struct mii_data *mii)
{
struct mii_softc *child;
LIST_FOREACH(child, &mii->mii_phys, mii_list)
(void) (*child->mii_service)(child, mii, MII_TICK);
}
/*
* Get media status from PHYs.
*/
void
mii_pollstat(struct mii_data *mii)
{
struct mii_softc *child;
mii->mii_media_status = 0;
mii->mii_media_active = IFM_NONE;
LIST_FOREACH(child, &mii->mii_phys, mii_list)
(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
}
/*
* Inform the PHYs that the interface is down.
*/
void
mii_down(struct mii_data *mii)
{
struct mii_softc *child;
LIST_FOREACH(child, &mii->mii_phys, mii_list)
mii_phy_down(child);
}

View File

@ -0,0 +1,544 @@
/* $NetBSD: mii_physubr.c,v 1.5 1999/08/03 19:41:49 drochner Exp $ */
/*-
* Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/sys/dev/mii/mii_physubr.c,v 1.22.2.2 2006/07/29 08:30:12 oleg Exp $");
/*
* Subroutines common to all PHYs.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <net/if.h>
#include <net/if_media.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include "miibus_if.h"
/*
* Media to register setting conversion table. Order matters.
*/
const struct mii_media mii_media_table[MII_NMEDIA] = {
/* None */
{ BMCR_ISO, ANAR_CSMA,
0, },
/* 10baseT */
{ BMCR_S10, ANAR_CSMA|ANAR_10,
0, },
/* 10baseT-FDX */
{ BMCR_S10|BMCR_FDX, ANAR_CSMA|ANAR_10_FD,
0, },
/* 100baseT4 */
{ BMCR_S100, ANAR_CSMA|ANAR_T4,
0, },
/* 100baseTX */
{ BMCR_S100, ANAR_CSMA|ANAR_TX,
0, },
/* 100baseTX-FDX */
{ BMCR_S100|BMCR_FDX, ANAR_CSMA|ANAR_TX_FD,
0, },
/* 1000baseX */
{ BMCR_S1000, ANAR_CSMA,
0, },
/* 1000baseX-FDX */
{ BMCR_S1000|BMCR_FDX, ANAR_CSMA,
0, },
/* 1000baseT */
{ BMCR_S1000, ANAR_CSMA,
GTCR_ADV_1000THDX },
/* 1000baseT-FDX */
{ BMCR_S1000, ANAR_CSMA,
GTCR_ADV_1000TFDX },
};
void
mii_phy_setmedia(struct mii_softc *sc)
{
struct mii_data *mii = sc->mii_pdata;
struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
int bmcr, anar, gtcr;
if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
(void) mii_phy_auto(sc);
return;
}
/*
* Table index is stored in the media entry.
*/
KASSERT(ife->ifm_data >=0 && ife->ifm_data < MII_NMEDIA,
("invalid ife->ifm_data (0x%x) in mii_phy_setmedia",
ife->ifm_data));
anar = mii_media_table[ife->ifm_data].mm_anar;
bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
switch (IFM_SUBTYPE(ife->ifm_media)) {
case IFM_1000_T:
gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
break;
default:
panic("mii_phy_setmedia: MASTER on wrong media");
}
}
if (ife->ifm_media & IFM_LOOP)
bmcr |= BMCR_LOOP;
PHY_WRITE(sc, MII_ANAR, anar);
PHY_WRITE(sc, MII_BMCR, bmcr);
if (sc->mii_flags & MIIF_HAVE_GTCR)
PHY_WRITE(sc, MII_100T2CR, gtcr);
}
int
mii_phy_auto(struct mii_softc *sc)
{
/*
* Check for 1000BASE-X. Autonegotiation is a bit
* different on such devices.
*/
if (sc->mii_flags & MIIF_IS_1000X) {
uint16_t anar = 0;
if (sc->mii_extcapabilities & EXTSR_1000XFDX)
anar |= ANAR_X_FD;
if (sc->mii_extcapabilities & EXTSR_1000XHDX)
anar |= ANAR_X_HD;
if (sc->mii_flags & MIIF_DOPAUSE) {
/* XXX Asymmetric vs. symmetric? */
anar |= ANLPAR_X_PAUSE_TOWARDS;
}
PHY_WRITE(sc, MII_ANAR, anar);
} else {
uint16_t anar;
anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
ANAR_CSMA;
if (sc->mii_flags & MIIF_DOPAUSE)
anar |= ANAR_FC;
PHY_WRITE(sc, MII_ANAR, anar);
if (sc->mii_flags & MIIF_HAVE_GTCR) {
uint16_t gtcr = 0;
if (sc->mii_extcapabilities & EXTSR_1000TFDX)
gtcr |= GTCR_ADV_1000TFDX;
if (sc->mii_extcapabilities & EXTSR_1000THDX)
gtcr |= GTCR_ADV_1000THDX;
PHY_WRITE(sc, MII_100T2CR, gtcr);
}
}
PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
return (EJUSTRETURN);
}
int
mii_phy_tick(struct mii_softc *sc)
{
struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;
struct ifnet *ifp = sc->mii_pdata->mii_ifp;
int reg;
/* Just bail now if the interface is down. */
if ((ifp->if_flags & IFF_UP) == 0)
return (EJUSTRETURN);
/*
* If we're not doing autonegotiation, we don't need to do
* any extra work here. However, we need to check the link
* status so we can generate an announcement if the status
* changes.
*/
if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
sc->mii_ticks = 0; /* reset autonegotiation timer. */
return (0);
}
/* Read the status register twice; BMSR_LINK is latch-low. */
reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
if (reg & BMSR_LINK) {
sc->mii_ticks = 0; /* reset autonegotiation timer. */
/* See above. */
return (0);
}
/* Announce link loss right after it happens */
if (sc->mii_ticks++ == 0)
return (0);
/* XXX: use default value if phy driver did not set mii_anegticks */
if (sc->mii_anegticks == 0)
sc->mii_anegticks = MII_ANEGTICKS_GIGE;
/* Only retry autonegotiation every mii_anegticks ticks. */
if (sc->mii_ticks <= sc->mii_anegticks)
return (EJUSTRETURN);
sc->mii_ticks = 0;
mii_phy_reset(sc);
mii_phy_auto(sc);
return (0);
}
void
mii_phy_reset(struct mii_softc *sc)
{
int reg, i;
if (sc->mii_flags & MIIF_NOISOLATE)
reg = BMCR_RESET;
else
reg = BMCR_RESET | BMCR_ISO;
PHY_WRITE(sc, MII_BMCR, reg);
/* Wait 100ms for it to complete. */
for (i = 0; i < 100; i++) {
reg = PHY_READ(sc, MII_BMCR);
if ((reg & BMCR_RESET) == 0)
break;
DELAY(1000);
}
if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
}
void
mii_phy_down(struct mii_softc *sc)
{
}
void
mii_phy_update(struct mii_softc *sc, int cmd)
{
struct mii_data *mii = sc->mii_pdata;
if (sc->mii_media_active != mii->mii_media_active ||
cmd == MII_MEDIACHG) {
MIIBUS_STATCHG(sc->mii_dev);
sc->mii_media_active = mii->mii_media_active;
}
if (sc->mii_media_status != mii->mii_media_status) {
MIIBUS_LINKCHG(sc->mii_dev);
sc->mii_media_status = mii->mii_media_status;
}
}
/*
* Given an ifmedia word, return the corresponding ANAR value.
*/
int
mii_anar(int media)
{
int rv;
switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
case IFM_ETHER|IFM_10_T:
rv = ANAR_10|ANAR_CSMA;
break;
case IFM_ETHER|IFM_10_T|IFM_FDX:
rv = ANAR_10_FD|ANAR_CSMA;
break;
case IFM_ETHER|IFM_100_TX:
rv = ANAR_TX|ANAR_CSMA;
break;
case IFM_ETHER|IFM_100_TX|IFM_FDX:
rv = ANAR_TX_FD|ANAR_CSMA;
break;
case IFM_ETHER|IFM_100_T4:
rv = ANAR_T4|ANAR_CSMA;
break;
default:
rv = 0;
break;
}
return (rv);
}
/*
* Given a BMCR value, return the corresponding ifmedia word.
*/
int
mii_media_from_bmcr(int bmcr)
{
int rv = IFM_ETHER;
if (bmcr & BMCR_S100)
rv |= IFM_100_TX;
else
rv |= IFM_10_T;
if (bmcr & BMCR_FDX)
rv |= IFM_FDX;
return (rv);
}
/*
* Initialize generic PHY media based on BMSR, called when a PHY is
* attached. We expect to be set up to print a comma-separated list
* of media names. Does not print a newline.
*/
void
mii_add_media(struct mii_softc *sc)
{
const char *sep = "";
struct mii_data *mii;
mii = device_get_softc(sc->mii_dev);
if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0) {
printf("no media present");
return;
}
#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
#define PRINT(s) printf("%s%s", sep, s); sep = ", "
if (sc->mii_capabilities & BMSR_10THDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 0);
PRINT("10baseT");
}
if (sc->mii_capabilities & BMSR_10TFDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
BMCR_FDX);
PRINT("10baseT-FDX");
}
if (sc->mii_capabilities & BMSR_100TXHDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
BMCR_S100);
PRINT("100baseTX");
}
if (sc->mii_capabilities & BMSR_100TXFDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
BMCR_S100|BMCR_FDX);
PRINT("100baseTX-FDX");
}
if (sc->mii_capabilities & BMSR_100T4) {
/*
* XXX How do you enable 100baseT4? I assume we set
* XXX BMCR_S100 and then assume the PHYs will take
* XXX watever action is necessary to switch themselves
* XXX into T4 mode.
*/
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
BMCR_S100);
PRINT("100baseT4");
}
if (sc->mii_capabilities & BMSR_ANEG) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
BMCR_AUTOEN);
PRINT("auto");
}
#undef ADD
#undef PRINT
}
/*
* Initialize generic PHY media based on BMSR, called when a PHY is
* attached. We expect to be set up to print a comma-separated list
* of media names. Does not print a newline.
*/
void
mii_phy_add_media(struct mii_softc *sc)
{
struct mii_data *mii = sc->mii_pdata;
const char *sep = "";
/* Set aneg timer for 10/100 media. Gigabit media handled below. */
sc->mii_anegticks = MII_ANEGTICKS;
#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
#define PRINT(s) printf("%s%s", sep, s); sep = ", "
if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
MII_MEDIA_NONE);
/*
* There are different interpretations for the bits in
* HomePNA PHYs. And there is really only one media type
* that is supported.
*/
if (sc->mii_flags & MIIF_IS_HPNA) {
if (sc->mii_capabilities & BMSR_10THDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
sc->mii_inst),
MII_MEDIA_10_T);
PRINT("HomePNA1");
}
return;
}
if (sc->mii_capabilities & BMSR_10THDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
MII_MEDIA_10_T);
PRINT("10baseT");
}
if (sc->mii_capabilities & BMSR_10TFDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
MII_MEDIA_10_T_FDX);
PRINT("10baseT-FDX");
}
if (sc->mii_capabilities & BMSR_100TXHDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
MII_MEDIA_100_TX);
PRINT("100baseTX");
}
if (sc->mii_capabilities & BMSR_100TXFDX) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
MII_MEDIA_100_TX_FDX);
PRINT("100baseTX-FDX");
}
if (sc->mii_capabilities & BMSR_100T4) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
MII_MEDIA_100_T4);
PRINT("100baseT4");
}
if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
/*
* XXX Right now only handle 1000SX and 1000TX. Need
* XXX to handle 1000LX and 1000CX some how.
*/
if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
sc->mii_anegticks = MII_ANEGTICKS_GIGE;
sc->mii_flags |= MIIF_IS_1000X;
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
sc->mii_inst), MII_MEDIA_1000_X);
PRINT("1000baseSX");
}
if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
sc->mii_anegticks = MII_ANEGTICKS_GIGE;
sc->mii_flags |= MIIF_IS_1000X;
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
sc->mii_inst), MII_MEDIA_1000_X_FDX);
PRINT("1000baseSX-FDX");
}
/*
* 1000baseT media needs to be able to manipulate
* master/slave mode. We set IFM_ETH_MASTER in
* the "don't care mask" and filter it out when
* the media is set.
*
* All 1000baseT PHYs have a 1000baseT control register.
*/
if (sc->mii_extcapabilities & EXTSR_1000THDX) {
sc->mii_anegticks = MII_ANEGTICKS_GIGE;
sc->mii_flags |= MIIF_HAVE_GTCR;
mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
sc->mii_inst), MII_MEDIA_1000_T);
PRINT("1000baseT");
}
if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
sc->mii_anegticks = MII_ANEGTICKS_GIGE;
sc->mii_flags |= MIIF_HAVE_GTCR;
mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
sc->mii_inst), MII_MEDIA_1000_T_FDX);
PRINT("1000baseT-FDX");
}
}
if (sc->mii_capabilities & BMSR_ANEG) {
ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
MII_NMEDIA); /* intentionally invalid index */
PRINT("auto");
}
#undef ADD
#undef PRINT
}
int
mii_phy_detach(device_t dev)
{
struct mii_softc *sc;
sc = device_get_softc(dev);
mii_phy_down(sc);
sc->mii_dev = NULL;
LIST_REMOVE(sc, mii_list);
return(0);
}
const struct mii_phydesc *
mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
{
for (; mpd->mpd_name != NULL; mpd++) {
if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
MII_MODEL(ma->mii_id2) == mpd->mpd_model)
return (mpd);
}
return (NULL);
}

View File

@ -14,62 +14,45 @@
#include <compat/dev/mii/miivar.h>
driver_t miibus_driver = {
"mii",
};
int
miibus_readreg(device_t dev, int phy, int reg)
__haiku_miibus_readreg(device_t dev, int phy, int reg)
{
if (dev->methods.miibus_readreg == NULL) {
if (dev->parent == NULL)
panic("miibus_readreg, no support");
return miibus_readreg(dev->parent, phy, reg);
}
if (dev->methods.miibus_readreg == NULL)
panic("miibus_readreg, no support");
return dev->methods.miibus_readreg(dev, phy, reg);
}
int
miibus_writereg(device_t dev, int phy, int reg, int data)
__haiku_miibus_writereg(device_t dev, int phy, int reg, int data)
{
if (dev->methods.miibus_writereg == NULL) {
if (dev->parent == NULL)
panic("miibus_writereg, no support");
return miibus_writereg(dev->parent, phy, reg, data);
}
if (dev->methods.miibus_writereg == NULL)
panic("miibus_writereg, no support");
return dev->methods.miibus_writereg(dev, phy, reg, data);
}
int
mii_phy_probe(device_t dev, device_t *mii_dev, ifm_change_cb_t change,
ifm_stat_cb_t stat)
void
__haiku_miibus_statchg(device_t dev)
{
UNIMPLEMENTED();
return -1;
if (dev->methods.miibus_statchg)
dev->methods.miibus_statchg(dev);
}
void
mii_tick(struct mii_data *data)
__haiku_miibus_linkchg(device_t dev)
{
UNIMPLEMENTED();
}
int
mii_mediachg(struct mii_data *data)
{
UNIMPLEMENTED();
return -1;
if (dev->methods.miibus_linkchg)
dev->methods.miibus_linkchg(dev);
}
void
mii_pollstat(struct mii_data *data)
__haiku_miibus_mediainit(device_t dev)
{
UNIMPLEMENTED();
if (dev->methods.miibus_mediainit)
dev->methods.miibus_mediainit(dev);
}