2 * Copyright (c) 2010 Juli Mallett <jmallett@FreeBSD.org>
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
30 * Cavium Octeon Ethernet devices.
32 * XXX This file should be moved to if_octe.c
33 * XXX The driver may have sufficient locking but we need locking to protect
34 * the interfaces presented here, right?
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/endian.h>
43 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
54 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59 #include <net/if_var.h>
60 #include <net/if_vlan_var.h>
63 #include <netinet/in.h>
64 #include <netinet/if_ether.h>
67 #include <dev/mii/mii.h>
68 #include <dev/mii/miivar.h>
70 #include "wrapper-cvmx-includes.h"
71 #include "cavium-ethernet.h"
73 #include "ethernet-common.h"
74 #include "ethernet-defines.h"
75 #include "ethernet-mdio.h"
76 #include "ethernet-tx.h"
78 #include "miibus_if.h"
80 #define OCTE_TX_LOCK(priv) mtx_lock(&(priv)->tx_mtx)
81 #define OCTE_TX_UNLOCK(priv) mtx_unlock(&(priv)->tx_mtx)
83 static int octe_probe(device_t);
84 static int octe_attach(device_t);
85 static int octe_detach(device_t);
86 static int octe_shutdown(device_t);
88 static int octe_miibus_readreg(device_t, int, int);
89 static int octe_miibus_writereg(device_t, int, int, int);
91 static void octe_init(void *);
92 static void octe_stop(void *);
93 static int octe_transmit(struct ifnet *, struct mbuf *);
95 static int octe_mii_medchange(struct ifnet *);
96 static void octe_mii_medstat(struct ifnet *, struct ifmediareq *);
98 static int octe_medchange(struct ifnet *);
99 static void octe_medstat(struct ifnet *, struct ifmediareq *);
101 static int octe_ioctl(struct ifnet *, u_long, caddr_t);
103 static device_method_t octe_methods[] = {
104 /* Device interface */
105 DEVMETHOD(device_probe, octe_probe),
106 DEVMETHOD(device_attach, octe_attach),
107 DEVMETHOD(device_detach, octe_detach),
108 DEVMETHOD(device_shutdown, octe_shutdown),
111 DEVMETHOD(miibus_readreg, octe_miibus_readreg),
112 DEVMETHOD(miibus_writereg, octe_miibus_writereg),
117 static driver_t octe_driver = {
120 sizeof (cvm_oct_private_t),
123 static devclass_t octe_devclass;
125 DRIVER_MODULE(octe, octebus, octe_driver, octe_devclass, 0, 0);
126 DRIVER_MODULE(miibus, octe, miibus_driver, miibus_devclass, 0, 0);
129 octe_probe(device_t dev)
135 octe_attach(device_t dev)
138 cvm_oct_private_t *priv;
143 priv = device_get_softc(dev);
146 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
148 if (priv->phy_id != -1) {
149 if (priv->phy_device == NULL) {
150 error = mii_attach(dev, &priv->miibus, ifp,
151 octe_mii_medchange, octe_mii_medstat,
152 BMSR_DEFCAPMASK, priv->phy_id, MII_OFFSET_ANY, 0);
154 device_printf(dev, "attaching PHYs failed\n");
156 child = device_add_child(dev, priv->phy_device, -1);
158 device_printf(dev, "missing phy %u device %s\n", priv->phy_id, priv->phy_device);
162 if (priv->miibus == NULL) {
163 ifmedia_init(&priv->media, 0, octe_medchange, octe_medstat);
165 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
166 ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO);
171 * We don't support programming the multicast filter right now, although it
172 * ought to be easy enough. (Presumably it's just a matter of putting
173 * multicast addresses in the CAM?)
175 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_ALLMULTI;
176 ifp->if_init = octe_init;
177 ifp->if_ioctl = octe_ioctl;
179 priv->if_flags = ifp->if_flags;
181 mtx_init(&priv->tx_mtx, ifp->if_xname, "octe tx send queue", MTX_DEF);
183 for (qos = 0; qos < 16; qos++) {
184 mtx_init(&priv->tx_free_queue[qos].ifq_mtx, ifp->if_xname, "octe tx free queue", MTX_DEF);
185 IFQ_SET_MAXLEN(&priv->tx_free_queue[qos], MAX_OUT_QUEUE_DEPTH);
188 ether_ifattach(ifp, priv->mac);
190 ifp->if_transmit = octe_transmit;
192 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
193 ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_HWCSUM;
194 ifp->if_capenable = ifp->if_capabilities;
195 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
198 IFQ_SET_MAXLEN(&ifp->if_snd, MAX_OUT_QUEUE_DEPTH);
199 ifp->if_snd.ifq_drv_maxlen = MAX_OUT_QUEUE_DEPTH;
200 IFQ_SET_READY(&ifp->if_snd);
201 OCTE_TX_UNLOCK(priv);
203 return (bus_generic_attach(dev));
207 octe_detach(device_t dev)
213 octe_shutdown(device_t dev)
215 return (octe_detach(dev));
219 octe_miibus_readreg(device_t dev, int phy, int reg)
221 cvm_oct_private_t *priv;
223 priv = device_get_softc(dev);
226 * Try interface-specific MII routine.
228 if (priv->mdio_read != NULL)
229 return (priv->mdio_read(priv->ifp, phy, reg));
232 * Try generic MII routine.
234 KASSERT(phy == priv->phy_id,
235 ("read from phy %u but our phy is %u", phy, priv->phy_id));
236 return (cvm_oct_mdio_read(priv->ifp, phy, reg));
240 octe_miibus_writereg(device_t dev, int phy, int reg, int val)
242 cvm_oct_private_t *priv;
244 priv = device_get_softc(dev);
247 * Try interface-specific MII routine.
249 if (priv->mdio_write != NULL) {
250 priv->mdio_write(priv->ifp, phy, reg, val);
255 * Try generic MII routine.
257 KASSERT(phy == priv->phy_id,
258 ("write to phy %u but our phy is %u", phy, priv->phy_id));
259 cvm_oct_mdio_write(priv->ifp, phy, reg, val);
268 cvm_oct_private_t *priv;
273 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
276 if (priv->open != NULL)
279 if (((ifp->if_flags ^ priv->if_flags) & (IFF_ALLMULTI | IFF_MULTICAST | IFF_PROMISC)) != 0)
280 cvm_oct_common_set_multicast_list(ifp);
282 cvm_oct_common_set_mac_address(ifp, IF_LLADDR(ifp));
284 cvm_oct_common_poll(ifp);
286 if (priv->miibus != NULL)
287 mii_mediachg(device_get_softc(priv->miibus));
289 ifp->if_drv_flags |= IFF_DRV_RUNNING;
290 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
297 cvm_oct_private_t *priv;
302 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
305 if (priv->stop != NULL)
308 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
312 octe_transmit(struct ifnet *ifp, struct mbuf *m)
314 cvm_oct_private_t *priv;
316 priv = ifp->if_softc;
318 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
324 return (cvm_oct_xmit(m, ifp));
328 octe_mii_medchange(struct ifnet *ifp)
330 cvm_oct_private_t *priv;
331 struct mii_data *mii;
332 struct mii_softc *miisc;
334 priv = ifp->if_softc;
335 mii = device_get_softc(priv->miibus);
336 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
344 octe_mii_medstat(struct ifnet *ifp, struct ifmediareq *ifm)
346 cvm_oct_private_t *priv;
347 struct mii_data *mii;
349 priv = ifp->if_softc;
350 mii = device_get_softc(priv->miibus);
353 ifm->ifm_active = mii->mii_media_active;
354 ifm->ifm_status = mii->mii_media_status;
358 octe_medchange(struct ifnet *ifp)
364 octe_medstat(struct ifnet *ifp, struct ifmediareq *ifm)
366 cvm_oct_private_t *priv;
367 cvmx_helper_link_info_t link_info;
369 priv = ifp->if_softc;
371 ifm->ifm_status = IFM_AVALID;
372 ifm->ifm_active = IFT_ETHER;
374 if (priv->poll == NULL)
378 link_info.u64 = priv->link_info;
380 if (!link_info.s.link_up)
383 ifm->ifm_status |= IFM_ACTIVE;
385 switch (link_info.s.speed) {
387 ifm->ifm_active |= IFM_10_T;
390 ifm->ifm_active |= IFM_100_TX;
393 ifm->ifm_active |= IFM_1000_T;
396 ifm->ifm_active |= IFM_10G_T;
400 if (link_info.s.full_duplex)
401 ifm->ifm_active |= IFM_FDX;
403 ifm->ifm_active |= IFM_HDX;
407 octe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
409 cvm_oct_private_t *priv;
410 struct mii_data *mii;
417 priv = ifp->if_softc;
418 ifr = (struct ifreq *)data;
420 ifa = (struct ifaddr *)data;
427 * Avoid reinitialization unless it's necessary.
429 if (ifa->ifa_addr->sa_family == AF_INET) {
430 ifp->if_flags |= IFF_UP;
431 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
433 arp_ifinit(ifp, ifa);
438 error = ether_ioctl(ifp, cmd, data);
444 if (ifp->if_flags == priv->if_flags)
446 if ((ifp->if_flags & IFF_UP) != 0) {
447 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
450 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
453 priv->if_flags = ifp->if_flags;
458 * Just change the capabilities in software, currently none
459 * require reprogramming hardware, they just toggle whether we
460 * make use of already-present facilities in software.
462 ifp->if_capenable = ifr->ifr_reqcap;
466 error = cvm_oct_common_change_mtu(ifp, ifr->ifr_mtu);
473 if (priv->miibus != NULL) {
474 mii = device_get_softc(priv->miibus);
475 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
480 error = ifmedia_ioctl(ifp, ifr, &priv->media, cmd);
486 error = ether_ioctl(ifp, cmd, data);