2009-05-20 22:13:24 +04:00
|
|
|
/*
|
|
|
|
* QEMU model of the Xilinx Ethernet Lite MAC.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Edgar E. Iglesias.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:11 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2023-05-08 15:03:14 +03:00
|
|
|
#include "exec/tswap.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2012-10-24 10:43:34 +04:00
|
|
|
#include "net/net.h"
|
2009-05-20 22:13:24 +04:00
|
|
|
|
|
|
|
#define D(x)
|
|
|
|
#define R_TX_BUF0 0
|
|
|
|
#define R_TX_LEN0 (0x07f4 / 4)
|
|
|
|
#define R_TX_GIE0 (0x07f8 / 4)
|
|
|
|
#define R_TX_CTRL0 (0x07fc / 4)
|
|
|
|
#define R_TX_BUF1 (0x0800 / 4)
|
|
|
|
#define R_TX_LEN1 (0x0ff4 / 4)
|
|
|
|
#define R_TX_CTRL1 (0x0ffc / 4)
|
|
|
|
|
|
|
|
#define R_RX_BUF0 (0x1000 / 4)
|
|
|
|
#define R_RX_CTRL0 (0x17fc / 4)
|
|
|
|
#define R_RX_BUF1 (0x1800 / 4)
|
|
|
|
#define R_RX_CTRL1 (0x1ffc / 4)
|
|
|
|
#define R_MAX (0x2000 / 4)
|
|
|
|
|
|
|
|
#define GIE_GIE 0x80000000
|
|
|
|
|
|
|
|
#define CTRL_I 0x8
|
|
|
|
#define CTRL_P 0x2
|
|
|
|
#define CTRL_S 0x1
|
|
|
|
|
2013-07-27 14:43:06 +04:00
|
|
|
#define TYPE_XILINX_ETHLITE "xlnx.xps-ethernetlite"
|
2020-09-01 00:07:33 +03:00
|
|
|
DECLARE_INSTANCE_CHECKER(struct xlx_ethlite, XILINX_ETHLITE,
|
|
|
|
TYPE_XILINX_ETHLITE)
|
2013-07-27 14:43:06 +04:00
|
|
|
|
2009-05-20 22:13:24 +04:00
|
|
|
struct xlx_ethlite
|
|
|
|
{
|
2013-07-27 14:43:06 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-08-26 02:13:47 +04:00
|
|
|
MemoryRegion mmio;
|
2009-05-20 22:13:24 +04:00
|
|
|
qemu_irq irq;
|
2009-11-25 21:49:26 +03:00
|
|
|
NICState *nic;
|
2009-10-21 17:25:38 +04:00
|
|
|
NICConf conf;
|
2009-05-20 22:13:24 +04:00
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
uint32_t c_tx_pingpong;
|
|
|
|
uint32_t c_rx_pingpong;
|
2009-05-20 22:13:24 +04:00
|
|
|
unsigned int txbuf;
|
|
|
|
unsigned int rxbuf;
|
|
|
|
|
|
|
|
uint32_t regs[R_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void eth_pulse_irq(struct xlx_ethlite *s)
|
|
|
|
{
|
|
|
|
/* Only the first gie reg is active. */
|
|
|
|
if (s->regs[R_TX_GIE0] & GIE_GIE) {
|
|
|
|
qemu_irq_pulse(s->irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 02:13:47 +04:00
|
|
|
static uint64_t
|
2012-10-23 14:30:10 +04:00
|
|
|
eth_read(void *opaque, hwaddr addr, unsigned int size)
|
2009-05-20 22:13:24 +04:00
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = opaque;
|
|
|
|
uint32_t r = 0;
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_TX_GIE0:
|
|
|
|
case R_TX_LEN0:
|
|
|
|
case R_TX_LEN1:
|
|
|
|
case R_TX_CTRL1:
|
|
|
|
case R_TX_CTRL0:
|
|
|
|
case R_RX_CTRL1:
|
|
|
|
case R_RX_CTRL0:
|
|
|
|
r = s->regs[addr];
|
2023-01-11 00:29:47 +03:00
|
|
|
D(qemu_log("%s " HWADDR_FMT_plx "=%x\n", __func__, addr * 4, r));
|
2009-05-20 22:13:24 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-03-10 11:16:52 +03:00
|
|
|
r = tswap32(s->regs[addr]);
|
2009-05-20 22:13:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-23 14:30:10 +04:00
|
|
|
eth_write(void *opaque, hwaddr addr,
|
2011-08-26 02:13:47 +04:00
|
|
|
uint64_t val64, unsigned int size)
|
2009-05-20 22:13:24 +04:00
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = opaque;
|
|
|
|
unsigned int base = 0;
|
2011-08-26 02:13:47 +04:00
|
|
|
uint32_t value = val64;
|
2009-05-20 22:13:24 +04:00
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_TX_CTRL0:
|
|
|
|
case R_TX_CTRL1:
|
|
|
|
if (addr == R_TX_CTRL1)
|
|
|
|
base = 0x800 / 4;
|
|
|
|
|
2023-01-11 00:29:47 +03:00
|
|
|
D(qemu_log("%s addr=" HWADDR_FMT_plx " val=%x\n",
|
2013-01-27 04:05:00 +04:00
|
|
|
__func__, addr * 4, value));
|
2009-05-20 22:13:24 +04:00
|
|
|
if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
|
2013-01-30 15:12:22 +04:00
|
|
|
qemu_send_packet(qemu_get_queue(s->nic),
|
2009-05-20 22:13:24 +04:00
|
|
|
(void *) &s->regs[base],
|
|
|
|
s->regs[base + R_TX_LEN0]);
|
|
|
|
D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
|
|
|
|
if (s->regs[base + R_TX_CTRL0] & CTRL_I)
|
|
|
|
eth_pulse_irq(s);
|
|
|
|
} else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) {
|
2009-10-21 17:25:38 +04:00
|
|
|
memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
|
2009-05-20 22:13:24 +04:00
|
|
|
if (s->regs[base + R_TX_CTRL0] & CTRL_I)
|
|
|
|
eth_pulse_irq(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are fast and get ready pretty much immediately so
|
|
|
|
we actually never flip the S nor P bits to one. */
|
|
|
|
s->regs[addr] = value & ~(CTRL_P | CTRL_S);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Keep these native. */
|
2013-01-27 00:36:23 +04:00
|
|
|
case R_RX_CTRL0:
|
|
|
|
case R_RX_CTRL1:
|
|
|
|
if (!(value & CTRL_S)) {
|
2013-01-30 15:12:22 +04:00
|
|
|
qemu_flush_queued_packets(qemu_get_queue(s->nic));
|
2013-01-27 00:36:23 +04:00
|
|
|
}
|
2015-01-21 16:18:37 +03:00
|
|
|
/* fall through */
|
2009-05-20 22:13:24 +04:00
|
|
|
case R_TX_LEN0:
|
|
|
|
case R_TX_LEN1:
|
|
|
|
case R_TX_GIE0:
|
2023-01-11 00:29:47 +03:00
|
|
|
D(qemu_log("%s addr=" HWADDR_FMT_plx " val=%x\n",
|
2013-01-27 04:05:00 +04:00
|
|
|
__func__, addr * 4, value));
|
2009-05-20 22:13:24 +04:00
|
|
|
s->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-03-10 11:16:52 +03:00
|
|
|
s->regs[addr] = tswap32(value);
|
2009-05-20 22:13:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-26 02:13:47 +04:00
|
|
|
static const MemoryRegionOps eth_ops = {
|
|
|
|
.read = eth_read,
|
|
|
|
.write = eth_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4
|
|
|
|
}
|
2009-05-20 22:13:24 +04:00
|
|
|
};
|
|
|
|
|
2020-03-05 20:56:49 +03:00
|
|
|
static bool eth_can_rx(NetClientState *nc)
|
2009-05-20 22:13:24 +04:00
|
|
|
{
|
2013-01-30 15:12:23 +04:00
|
|
|
struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
|
2013-01-27 00:36:22 +04:00
|
|
|
unsigned int rxbase = s->rxbuf * (0x800 / 4);
|
|
|
|
|
|
|
|
return !(s->regs[rxbase + R_RX_CTRL0] & CTRL_S);
|
2009-05-20 22:13:24 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-05-20 22:13:24 +04:00
|
|
|
{
|
2013-01-30 15:12:23 +04:00
|
|
|
struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
|
2009-05-20 22:13:24 +04:00
|
|
|
unsigned int rxbase = s->rxbuf * (0x800 / 4);
|
|
|
|
|
|
|
|
/* DA filter. */
|
2009-10-21 17:25:38 +04:00
|
|
|
if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
|
2009-06-11 13:42:26 +04:00
|
|
|
return size;
|
2009-05-20 22:13:24 +04:00
|
|
|
|
|
|
|
if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
|
|
|
|
D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
|
2009-06-11 13:42:26 +04:00
|
|
|
return -1;
|
2009-05-20 22:13:24 +04:00
|
|
|
}
|
|
|
|
|
2013-01-27 04:05:00 +04:00
|
|
|
D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
|
2016-08-09 06:52:54 +03:00
|
|
|
if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
|
|
|
|
D(qemu_log("ethlite packet is too big, size=%x\n", size));
|
|
|
|
return -1;
|
|
|
|
}
|
2009-05-20 22:13:24 +04:00
|
|
|
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
|
|
|
|
|
|
|
|
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
2014-05-06 08:39:38 +04:00
|
|
|
if (s->regs[R_RX_CTRL0] & CTRL_I) {
|
2009-05-20 22:13:24 +04:00
|
|
|
eth_pulse_irq(s);
|
2014-05-06 08:39:38 +04:00
|
|
|
}
|
2009-05-20 22:13:24 +04:00
|
|
|
|
|
|
|
/* If c_rx_pingpong was set flip buffers. */
|
|
|
|
s->rxbuf ^= s->c_rx_pingpong;
|
2009-06-11 13:42:26 +04:00
|
|
|
return size;
|
2009-05-20 22:13:24 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:23:54 +04:00
|
|
|
static void xilinx_ethlite_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = XILINX_ETHLITE(dev);
|
|
|
|
|
|
|
|
s->rxbuf = 0;
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:49:26 +03:00
|
|
|
static NetClientInfo net_xilinx_ethlite_info = {
|
qapi: Change Netdev into a flat union
This is a mostly-mechanical conversion that creates a new flat
union 'Netdev' QAPI type that covers all the branches of the
former 'NetClientOptions' simple union, where the branches are
now listed in a new 'NetClientDriver' enum rather than generated
from the simple union. The existence of a flat union has no
change to the command line syntax accepted for new code, and
will make it possible for a future patch to switch the QMP
command to parse a boxed union for no change to valid QMP; but
it does have some ripple effect on the C code when dealing with
the new types.
While making the conversion, note that the 'NetLegacy' type
remains unchanged: it applies only to legacy command line options,
and will not be ported to QMP, so it should remain a wrapper
around a simple union; to avoid confusion, the type named
'NetClientOptions' is now gone, and we introduce 'NetLegacyOptions'
in its place. Then, in the C code, we convert from NetLegacy to
Netdev as soon as possible, so that the bulk of the net stack
only has to deal with one QAPI type, not two. Note that since
the old legacy code always rejected 'hubport', we can just omit
that branch from the new 'NetLegacyOptions' simple union.
Based on an idea originally by Zoltán Kővágó <DirtY.iCE.hu@gmail.com>:
Message-Id: <01a527fbf1a5de880091f98cf011616a78adeeee.1441627176.git.DirtY.iCE.hu@gmail.com>
although the sed script in that patch no longer applies due to
other changes in the tree since then, and I also did some manual
cleanups (such as fixing whitespace to keep checkpatch happy).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1468468228-27827-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Fixup from Eric squashed in]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-07-14 06:50:23 +03:00
|
|
|
.type = NET_CLIENT_DRIVER_NIC,
|
2009-11-25 21:49:26 +03:00
|
|
|
.size = sizeof(NICState),
|
|
|
|
.can_receive = eth_can_rx,
|
|
|
|
.receive = eth_rx,
|
|
|
|
};
|
|
|
|
|
2014-05-29 13:24:29 +04:00
|
|
|
static void xilinx_ethlite_realize(DeviceState *dev, Error **errp)
|
2009-05-20 22:13:24 +04:00
|
|
|
{
|
2013-07-27 14:43:06 +04:00
|
|
|
struct xlx_ethlite *s = XILINX_ETHLITE(dev);
|
2009-05-20 22:13:24 +04:00
|
|
|
|
2009-10-21 17:25:38 +04:00
|
|
|
qemu_macaddr_default_if_unset(&s->conf.macaddr);
|
2009-11-25 21:49:26 +03:00
|
|
|
s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
|
2023-06-01 06:18:58 +03:00
|
|
|
object_get_typename(OBJECT(dev)), dev->id,
|
|
|
|
&dev->mem_reentrancy_guard, s);
|
2013-01-30 15:12:22 +04:00
|
|
|
qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
|
2014-05-29 13:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xilinx_ethlite_init(Object *obj)
|
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = XILINX_ETHLITE(obj);
|
|
|
|
|
|
|
|
sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
|
|
|
|
|
|
|
|
memory_region_init_io(&s->mmio, obj, ð_ops, s,
|
|
|
|
"xlnx.xps-ethernetlite", R_MAX * 4);
|
|
|
|
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
|
2009-05-20 22:13:24 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static Property xilinx_ethlite_properties[] = {
|
2012-06-13 08:46:46 +04:00
|
|
|
DEFINE_PROP_UINT32("tx-ping-pong", struct xlx_ethlite, c_tx_pingpong, 1),
|
|
|
|
DEFINE_PROP_UINT32("rx-ping-pong", struct xlx_ethlite, c_rx_pingpong, 1),
|
2012-01-24 23:12:29 +04:00
|
|
|
DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xilinx_ethlite_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2014-05-29 13:24:29 +04:00
|
|
|
dc->realize = xilinx_ethlite_realize;
|
2014-05-29 13:23:54 +04:00
|
|
|
dc->reset = xilinx_ethlite_reset;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, xilinx_ethlite_properties);
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo xilinx_ethlite_info = {
|
2013-07-27 14:43:06 +04:00
|
|
|
.name = TYPE_XILINX_ETHLITE,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(struct xlx_ethlite),
|
2014-05-29 13:24:29 +04:00
|
|
|
.instance_init = xilinx_ethlite_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = xilinx_ethlite_class_init,
|
2009-07-15 15:43:31 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void xilinx_ethlite_register_types(void)
|
2009-05-20 22:13:24 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&xilinx_ethlite_info);
|
2009-05-20 22:13:24 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(xilinx_ethlite_register_types)
|