2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2012 Juli Mallett <jmallett@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
48 #include <net/ethernet.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
53 #include <net/if_var.h>
54 #include <net/if_vlan_var.h>
57 #include <netinet/in.h>
58 #include <netinet/if_ether.h>
61 #include <machine/cpuregs.h>
63 #include <dev/gxemul/ether/gxreg.h>
70 struct ifmedia sc_ifmedia;
71 struct resource *sc_intr;
76 #define GXEMUL_ETHER_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
77 #define GXEMUL_ETHER_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
79 static void gx_identify(driver_t *, device_t);
80 static int gx_probe(device_t);
81 static int gx_attach(device_t);
82 static int gx_detach(device_t);
83 static int gx_shutdown(device_t);
85 static void gx_init(void *);
86 static int gx_transmit(struct ifnet *, struct mbuf *);
88 static int gx_medchange(struct ifnet *);
89 static void gx_medstat(struct ifnet *, struct ifmediareq *);
91 static int gx_ioctl(struct ifnet *, u_long, caddr_t);
93 static void gx_rx_intr(void *);
95 static device_method_t gx_methods[] = {
96 /* Device interface */
97 DEVMETHOD(device_identify, gx_identify),
98 DEVMETHOD(device_probe, gx_probe),
99 DEVMETHOD(device_attach, gx_attach),
100 DEVMETHOD(device_detach, gx_detach),
101 DEVMETHOD(device_shutdown, gx_shutdown),
106 static driver_t gx_driver = {
109 sizeof (struct gx_softc),
112 static devclass_t gx_devclass;
114 DRIVER_MODULE(gx, nexus, gx_driver, gx_devclass, 0, 0);
117 gx_identify(driver_t *drv, device_t parent)
119 BUS_ADD_CHILD(parent, 0, "gx", 0);
123 gx_probe(device_t dev)
125 if (device_get_unit(dev) != 0)
128 device_set_desc(dev, "GXemul test Ethernet");
130 return (BUS_PROBE_NOWILDCARD);
134 gx_attach(device_t dev)
142 sc = device_get_softc(dev);
144 sc->sc_port = device_get_unit(dev);
146 /* Read MAC address. */
147 GXEMUL_ETHER_DEV_WRITE(GXEMUL_ETHER_DEV_MAC, (uintptr_t)mac);
149 /* Allocate and establish interrupt. */
151 sc->sc_intr = bus_alloc_resource(sc->sc_dev, SYS_RES_IRQ, &rid,
152 GXEMUL_ETHER_DEV_IRQ - 2, GXEMUL_ETHER_DEV_IRQ - 2, 1, RF_ACTIVE);
153 if (sc->sc_intr == NULL) {
154 device_printf(dev, "unable to allocate IRQ.\n");
158 error = bus_setup_intr(sc->sc_dev, sc->sc_intr, INTR_TYPE_NET, NULL,
159 gx_rx_intr, sc, &sc->sc_intr_cookie);
161 device_printf(dev, "unable to setup interrupt.\n");
162 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr);
166 bus_describe_intr(sc->sc_dev, sc->sc_intr, sc->sc_intr_cookie, "rx");
168 ifp = if_alloc(IFT_ETHER);
170 device_printf(dev, "cannot allocate ifnet.\n");
171 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr);
175 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
176 ifp->if_mtu = ETHERMTU;
177 ifp->if_init = gx_init;
179 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_ALLMULTI;
180 ifp->if_ioctl = gx_ioctl;
183 sc->sc_flags = ifp->if_flags;
185 ifmedia_init(&sc->sc_ifmedia, 0, gx_medchange, gx_medstat);
187 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
188 ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO);
190 mtx_init(&sc->sc_mtx, "GXemul Ethernet", NULL, MTX_DEF);
192 ether_ifattach(ifp, mac);
194 ifp->if_transmit = gx_transmit;
196 return (bus_generic_attach(dev));
200 gx_detach(device_t dev)
204 sc = device_get_softc(dev);
206 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr);
207 /* XXX Incomplete. */
213 gx_shutdown(device_t dev)
215 return (gx_detach(dev));
227 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
228 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
230 ifp->if_drv_flags |= IFF_DRV_RUNNING;
234 gx_transmit(struct ifnet *ifp, struct mbuf *m)
240 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) {
245 GXEMUL_ETHER_LOCK(sc);
246 GXEMUL_ETHER_DEV_WRITE(GXEMUL_ETHER_DEV_LENGTH, m->m_pkthdr.len);
247 m_copydata(m, 0, m->m_pkthdr.len, (void *)(uintptr_t)GXEMUL_ETHER_DEV_FUNCTION(GXEMUL_ETHER_DEV_BUFFER));
248 GXEMUL_ETHER_DEV_WRITE(GXEMUL_ETHER_DEV_COMMAND, GXEMUL_ETHER_DEV_COMMAND_TX);
249 GXEMUL_ETHER_UNLOCK(sc);
251 ETHER_BPF_MTAP(ifp, m);
253 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
254 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
262 gx_medchange(struct ifnet *ifp)
268 gx_medstat(struct ifnet *ifp, struct ifmediareq *ifm)
275 ifm->ifm_status = IFM_AVALID | IFM_ACTIVE;
276 ifm->ifm_active = IFT_ETHER | IFM_1000_T | IFM_FDX;
280 gx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
290 ifr = (struct ifreq *)data;
292 ifa = (struct ifaddr *)data;
299 * Avoid reinitialization unless it's necessary.
301 if (ifa->ifa_addr->sa_family == AF_INET) {
302 ifp->if_flags |= IFF_UP;
303 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
305 arp_ifinit(ifp, ifa);
310 error = ether_ioctl(ifp, cmd, data);
316 if (ifp->if_flags == sc->sc_flags)
318 if ((ifp->if_flags & IFF_UP) != 0) {
321 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
322 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
325 sc->sc_flags = ifp->if_flags;
329 if (ifr->ifr_mtu + ifp->if_hdrlen > GXEMUL_ETHER_DEV_MTU)
335 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
341 error = ether_ioctl(ifp, cmd, data);
349 gx_rx_intr(void *arg)
351 struct gx_softc *sc = arg;
353 GXEMUL_ETHER_LOCK(sc);
355 uint64_t status, length;
360 * Limit number of packets received at once?
362 status = GXEMUL_ETHER_DEV_READ(GXEMUL_ETHER_DEV_STATUS);
363 if (status == GXEMUL_ETHER_DEV_STATUS_RX_MORE) {
364 GXEMUL_ETHER_DEV_WRITE(GXEMUL_ETHER_DEV_COMMAND, GXEMUL_ETHER_DEV_COMMAND_RX);
367 if (status != GXEMUL_ETHER_DEV_STATUS_RX_OK)
369 length = GXEMUL_ETHER_DEV_READ(GXEMUL_ETHER_DEV_LENGTH);
370 if (length > MCLBYTES - ETHER_ALIGN) {
371 if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1);
375 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
377 device_printf(sc->sc_dev, "no memory for receive mbuf.\n");
378 if_inc_counter(sc->sc_ifp, IFCOUNTER_IQDROPS, 1);
379 GXEMUL_ETHER_UNLOCK(sc);
383 /* Align incoming frame so IP headers are aligned. */
384 m->m_data += ETHER_ALIGN;
386 memcpy(m->m_data, (const void *)(uintptr_t)GXEMUL_ETHER_DEV_FUNCTION(GXEMUL_ETHER_DEV_BUFFER), length);
388 m->m_pkthdr.rcvif = sc->sc_ifp;
389 m->m_pkthdr.len = m->m_len = length;
391 if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
393 GXEMUL_ETHER_UNLOCK(sc);
395 (*sc->sc_ifp->if_input)(sc->sc_ifp, m);
397 GXEMUL_ETHER_LOCK(sc);
399 GXEMUL_ETHER_UNLOCK(sc);