freebsd-nq/sys/mips/cavium/octe/octe.c

498 lines
11 KiB
C
Raw Normal View History

Update the port of FreeBSD to Cavium Octeon to use the Cavium Simple Executive library: o) Increase inline unit / large function growth limits for MIPS to accommodate the needs of the Simple Executive, which uses a shocking amount of inlining. o) Remove TARGET_OCTEON and use CPU_CNMIPS to do things required by cnMIPS and the Octeon SoC. o) Add OCTEON_VENDOR_LANNER to use Lanner's allocation of vendor-specific board numbers, specifically to support the MR320. o) Add OCTEON_BOARD_CAPK_0100ND to hard-wire configuration for the CAPK-0100nd, which improperly uses an evaluation board's board number and breaks board detection at runtime. This board is sold by Portwell as the CAM-0100. o) Add support for the RTC available on some Octeon boards. o) Add support for the Octeon PCI bus. Note that rman_[sg]et_virtual for IO ports can not work unless building for n64. o) Clean up the CompactFlash driver to use Simple Executive macros and structures where possible (it would be advisable to use the Simple Executive API to set the PIO mode, too, but that is not done presently.) Also use structures from FreeBSD's ATA layer rather than structures copied from Linux. o) Print available Octeon SoC features on boot. o) Add support for the Octeon timecounter. o) Use the Simple Executive's routines rather than local copies for doing reads and writes to 64-bit addresses and use its macros for various device addresses rather than using local copies. o) Rename octeon_board_real to octeon_is_simulation to reduce differences with Cavium-provided code originally written for Linux. Also make it use the same simplified test that the Simple Executive and Linux both use rather than our complex one. o) Add support for the Octeon CIU, which is the main interrupt unit, as a bus to use normal interrupt allocation and setup routines. o) Use the Simple Executive's bootmem facility to allocate physical memory for the kernel, rather than assuming we know which addresses we can steal. NB: This may reduce the amount of RAM the kernel reports you as having if you are leaving large temporary allocations made by U-Boot allocated when starting FreeBSD. o) Add a port of the Cavium-provided Ethernet driver for Linux. This changes Ethernet interface naming from rgmxN to octeN. The new driver has vast improvements over the old one, both in performance and functionality, but does still have some features which have not been ported entirely and there may be unimplemented code that can be hit in everyday use. I will make every effort to correct those as they are reported. o) Support loading the kernel on non-contiguous cores. o) Add very conservative support for harvesting randomness from the Octeon random number device. o) Turn SMP on by default. o) Clean up the style of the Octeon kernel configurations a little and make them compile with -march=octeon. o) Add support for the Lanner MR320 and the CAPK-0100nd to the Simple Executive. o) Modify the Simple Executive to build on FreeBSD and to build without executive-config.h or cvmx-config.h. In the future we may want to revert part of these changes and supply executive-config.h and cvmx-config.h and access to the options contained in those files via kernel configuration files. o) Modify the Simple Executive USB routines to support getting and setting of the USB PID.
2010-07-20 19:25:11 +00:00
/*-
* Copyright (c) 2010 Juli Mallett <jmallett@FreeBSD.org>
* All rights reserved.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
/*
* Cavium Octeon Ethernet devices.
*
* XXX This file should be moved to if_octe.c
* XXX The driver may have sufficient locking but we need locking to protect
* the interfaces presented here, right?
*/
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net/if_vlan_var.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include "wrapper-cvmx-includes.h"
#include "cavium-ethernet.h"
#include "ethernet-common.h"
#include "ethernet-defines.h"
#include "ethernet-mdio.h"
#include "ethernet-tx.h"
#include "miibus_if.h"
#define OCTE_TX_LOCK(priv) mtx_lock(&(priv)->tx_mtx)
#define OCTE_TX_UNLOCK(priv) mtx_unlock(&(priv)->tx_mtx)
static int octe_probe(device_t);
static int octe_attach(device_t);
static int octe_detach(device_t);
static int octe_shutdown(device_t);
static int octe_miibus_readreg(device_t, int, int);
static int octe_miibus_writereg(device_t, int, int, int);
static void octe_init(void *);
static void octe_stop(void *);
static void octe_start(struct ifnet *);
static int octe_mii_medchange(struct ifnet *);
static void octe_mii_medstat(struct ifnet *, struct ifmediareq *);
static int octe_medchange(struct ifnet *);
static void octe_medstat(struct ifnet *, struct ifmediareq *);
static int octe_ioctl(struct ifnet *, u_long, caddr_t);
static device_method_t octe_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, octe_probe),
DEVMETHOD(device_attach, octe_attach),
DEVMETHOD(device_detach, octe_detach),
DEVMETHOD(device_shutdown, octe_shutdown),
/* MII interface */
DEVMETHOD(miibus_readreg, octe_miibus_readreg),
DEVMETHOD(miibus_writereg, octe_miibus_writereg),
{ 0, 0 }
};
static driver_t octe_driver = {
"octe",
octe_methods,
sizeof (cvm_oct_private_t),
};
static devclass_t octe_devclass;
DRIVER_MODULE(octe, octebus, octe_driver, octe_devclass, 0, 0);
DRIVER_MODULE(miibus, octe, miibus_driver, miibus_devclass, 0, 0);
static driver_t pow_driver = {
"pow",
octe_methods,
sizeof (cvm_oct_private_t),
};
static devclass_t pow_devclass;
DRIVER_MODULE(pow, octebus, pow_driver, pow_devclass, 0, 0);
static int
octe_probe(device_t dev)
{
return (0);
}
static int
octe_attach(device_t dev)
{
struct ifnet *ifp;
cvm_oct_private_t *priv;
unsigned qos;
int error;
priv = device_get_softc(dev);
ifp = priv->ifp;
if_initname(ifp, device_get_name(dev), device_get_unit(dev));
if (priv->phy_id != -1) {
error = mii_phy_probe(dev, &priv->miibus, octe_mii_medchange,
octe_mii_medstat);
if (error != 0) {
device_printf(dev, "missing phy %u\n", priv->phy_id);
}
}
if (priv->miibus == NULL) {
ifmedia_init(&priv->media, 0, octe_medchange, octe_medstat);
ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO);
}
/*
* XXX
* We don't support programming the multicast filter right now, although it
* ought to be easy enough. (Presumably it's just a matter of putting
* multicast addresses in the CAM?)
*/
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_ALLMULTI;
ifp->if_init = octe_init;
ifp->if_ioctl = octe_ioctl;
ifp->if_start = octe_start;
priv->if_flags = ifp->if_flags;
mtx_init(&priv->tx_mtx, ifp->if_xname, "octe tx send queue", MTX_DEF);
for (qos = 0; qos < 16; qos++) {
mtx_init(&priv->tx_free_queue[qos].ifq_mtx, ifp->if_xname, "octe tx free queue", MTX_DEF);
IFQ_SET_MAXLEN(&priv->tx_free_queue[qos], MAX_OUT_QUEUE_DEPTH);
}
ether_ifattach(ifp, priv->mac);
ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_HWCSUM;
ifp->if_capenable = ifp->if_capabilities;
ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
OCTE_TX_LOCK(priv);
IFQ_SET_MAXLEN(&ifp->if_snd, MAX_OUT_QUEUE_DEPTH);
ifp->if_snd.ifq_drv_maxlen = MAX_OUT_QUEUE_DEPTH;
IFQ_SET_READY(&ifp->if_snd);
OCTE_TX_UNLOCK(priv);
return (0);
}
static int
octe_detach(device_t dev)
{
return (0);
}
static int
octe_shutdown(device_t dev)
{
return (octe_detach(dev));
}
static int
octe_miibus_readreg(device_t dev, int phy, int reg)
{
cvm_oct_private_t *priv;
priv = device_get_softc(dev);
if (phy != priv->phy_id)
return (0);
return (cvm_oct_mdio_read(priv->ifp, phy, reg));
}
static int
octe_miibus_writereg(device_t dev, int phy, int reg, int val)
{
cvm_oct_private_t *priv;
priv = device_get_softc(dev);
KASSERT(phy == priv->phy_id,
("write to phy %u but our phy is %u", phy, priv->phy_id));
cvm_oct_mdio_write(priv->ifp, phy, reg, val);
return (0);
}
static void
octe_init(void *arg)
{
struct ifnet *ifp;
cvm_oct_private_t *priv;
priv = arg;
ifp = priv->ifp;
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
octe_stop(priv);
if (priv->open != NULL)
priv->open(ifp);
if (((ifp->if_flags ^ priv->if_flags) & (IFF_ALLMULTI | IFF_MULTICAST | IFF_PROMISC)) != 0)
cvm_oct_common_set_multicast_list(ifp);
cvm_oct_common_set_mac_address(ifp, IF_LLADDR(ifp));
if (priv->poll != NULL)
priv->poll(ifp);
if (priv->miibus != NULL)
mii_mediachg(device_get_softc(priv->miibus));
ifp->if_drv_flags |= IFF_DRV_RUNNING;
ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
}
static void
octe_stop(void *arg)
{
struct ifnet *ifp;
cvm_oct_private_t *priv;
priv = arg;
ifp = priv->ifp;
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
return;
if (priv->stop != NULL)
priv->stop(ifp);
ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
}
static void
octe_start(struct ifnet *ifp)
{
cvm_oct_private_t *priv;
struct mbuf *m;
int error;
priv = ifp->if_softc;
if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING)
return;
OCTE_TX_LOCK(priv);
while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
OCTE_TX_UNLOCK(priv);
if (priv->queue != -1) {
error = cvm_oct_xmit(m, ifp);
} else {
error = cvm_oct_xmit_pow(m, ifp);
}
if (error != 0) {
ifp->if_drv_flags |= IFF_DRV_OACTIVE;
return;
}
OCTE_TX_LOCK(priv);
}
OCTE_TX_UNLOCK(priv);
}
static int
octe_mii_medchange(struct ifnet *ifp)
{
cvm_oct_private_t *priv;
struct mii_data *mii;
priv = ifp->if_softc;
mii = device_get_softc(priv->miibus);
if (mii->mii_instance) {
struct mii_softc *miisc;
LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
mii_phy_reset(miisc);
}
mii_mediachg(mii);
return (0);
}
static void
octe_mii_medstat(struct ifnet *ifp, struct ifmediareq *ifm)
{
cvm_oct_private_t *priv;
struct mii_data *mii;
priv = ifp->if_softc;
mii = device_get_softc(priv->miibus);
mii_pollstat(mii);
ifm->ifm_active = mii->mii_media_active;
ifm->ifm_status = mii->mii_media_status;
}
static int
octe_medchange(struct ifnet *ifp)
{
return (ENOTSUP);
}
static void
octe_medstat(struct ifnet *ifp, struct ifmediareq *ifm)
{
cvm_oct_private_t *priv;
cvmx_helper_link_info_t link_info;
priv = ifp->if_softc;
ifm->ifm_status = IFM_AVALID;
ifm->ifm_active = IFT_ETHER;
if (priv->poll == NULL)
return;
priv->poll(ifp);
link_info.u64 = priv->link_info;
if (!link_info.s.link_up)
return;
ifm->ifm_status |= IFM_ACTIVE;
switch (link_info.s.speed) {
case 10:
ifm->ifm_active |= IFM_10_T;
break;
case 100:
ifm->ifm_active |= IFM_100_TX;
break;
case 1000:
ifm->ifm_active |= IFM_1000_T;
break;
case 10000:
ifm->ifm_active |= IFM_10G_T;
break;
}
if (link_info.s.full_duplex)
ifm->ifm_active |= IFM_FDX;
else
ifm->ifm_active |= IFM_HDX;
}
static int
octe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
cvm_oct_private_t *priv;
struct mii_data *mii;
struct ifreq *ifr;
#ifdef INET
struct ifaddr *ifa;
#endif
int error;
priv = ifp->if_softc;
ifr = (struct ifreq *)data;
#ifdef INET
ifa = (struct ifaddr *)data;
#endif
switch (cmd) {
case SIOCSIFADDR:
#ifdef INET
/*
* Avoid reinitialization unless it's necessary.
*/
if (ifa->ifa_addr->sa_family == AF_INET) {
ifp->if_flags |= IFF_UP;
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
octe_init(priv);
arp_ifinit(ifp, ifa);
return (0);
}
#endif
error = ether_ioctl(ifp, cmd, data);
if (error != 0)
return (error);
return (0);
case SIOCSIFFLAGS:
if (ifp->if_flags == priv->if_flags)
return (0);
if ((ifp->if_flags & IFF_UP) != 0) {
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
octe_init(priv);
} else {
if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
octe_stop(priv);
}
priv->if_flags = ifp->if_flags;
return (0);
case SIOCSIFCAP:
/*
* Just change the capabilities in software, currently none
* require reprogramming hardware, they just toggle whether we
* make use of already-present facilities in software.
*/
ifp->if_capenable = ifr->ifr_reqcap;
return (0);
case SIOCSIFMTU:
error = cvm_oct_common_change_mtu(ifp, ifr->ifr_mtu);
if (error != 0)
return (EINVAL);
return (0);
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
if (priv->miibus != NULL) {
mii = device_get_softc(priv->miibus);
error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
if (error != 0)
return (error);
return (0);
}
error = ifmedia_ioctl(ifp, ifr, &priv->media, cmd);
if (error != 0)
return (error);
return (0);
default:
error = ether_ioctl(ifp, cmd, data);
if (error != 0)
return (error);
return (0);
}
}