2016-01-26 21:17:11 +03:00
|
|
|
#include "qemu/osdep.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"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2011-09-05 00:29:27 +04:00
|
|
|
#include "trace.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2007-10-17 17:39:42 +04:00
|
|
|
|
|
|
|
/* MIPSnet register offsets */
|
|
|
|
|
2019-12-06 16:58:05 +03:00
|
|
|
#define MIPSNET_DEV_ID 0x00
|
|
|
|
#define MIPSNET_BUSY 0x08
|
|
|
|
#define MIPSNET_RX_DATA_COUNT 0x0c
|
|
|
|
#define MIPSNET_TX_DATA_COUNT 0x10
|
|
|
|
#define MIPSNET_INT_CTL 0x14
|
|
|
|
# define MIPSNET_INTCTL_TXDONE 0x00000001
|
|
|
|
# define MIPSNET_INTCTL_RXDONE 0x00000002
|
|
|
|
# define MIPSNET_INTCTL_TESTBIT 0x80000000
|
|
|
|
#define MIPSNET_INTERRUPT_INFO 0x18
|
|
|
|
#define MIPSNET_RX_DATA_BUFFER 0x1c
|
|
|
|
#define MIPSNET_TX_DATA_BUFFER 0x20
|
|
|
|
|
|
|
|
#define MAX_ETH_FRAME_SIZE 1514
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2013-07-27 17:59:07 +04:00
|
|
|
#define TYPE_MIPS_NET "mipsnet"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(MIPSnetState, MIPS_NET)
|
2013-07-27 17:59:07 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct MIPSnetState {
|
2013-07-27 17:59:07 +04:00
|
|
|
SysBusDevice parent_obj;
|
2011-09-05 00:29:26 +04:00
|
|
|
|
2007-10-17 17:39:42 +04:00
|
|
|
uint32_t busy;
|
|
|
|
uint32_t rx_count;
|
|
|
|
uint32_t rx_read;
|
|
|
|
uint32_t tx_count;
|
|
|
|
uint32_t tx_written;
|
|
|
|
uint32_t intctl;
|
|
|
|
uint8_t rx_buffer[MAX_ETH_FRAME_SIZE];
|
|
|
|
uint8_t tx_buffer[MAX_ETH_FRAME_SIZE];
|
2011-09-05 00:29:26 +04:00
|
|
|
MemoryRegion io;
|
2007-10-17 17:39:42 +04:00
|
|
|
qemu_irq irq;
|
2009-11-25 21:49:21 +03:00
|
|
|
NICState *nic;
|
|
|
|
NICConf conf;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2007-10-17 17:39:42 +04:00
|
|
|
|
|
|
|
static void mipsnet_reset(MIPSnetState *s)
|
|
|
|
{
|
|
|
|
s->busy = 1;
|
|
|
|
s->rx_count = 0;
|
|
|
|
s->rx_read = 0;
|
|
|
|
s->tx_count = 0;
|
|
|
|
s->tx_written = 0;
|
|
|
|
s->intctl = 0;
|
|
|
|
memset(s->rx_buffer, 0, MAX_ETH_FRAME_SIZE);
|
|
|
|
memset(s->tx_buffer, 0, MAX_ETH_FRAME_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mipsnet_update_irq(MIPSnetState *s)
|
|
|
|
{
|
|
|
|
int isr = !!s->intctl;
|
2011-09-05 00:29:27 +04:00
|
|
|
trace_mipsnet_irq(isr, s->intctl);
|
2007-10-17 17:39:42 +04:00
|
|
|
qemu_set_irq(s->irq, isr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mipsnet_buffer_full(MIPSnetState *s)
|
|
|
|
{
|
2019-12-06 16:58:05 +03:00
|
|
|
if (s->rx_count >= MAX_ETH_FRAME_SIZE) {
|
2007-10-17 17:39:42 +04:00
|
|
|
return 1;
|
2019-12-06 16:58:05 +03:00
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static int mipsnet_can_receive(NetClientState *nc)
|
2007-10-17 17:39:42 +04:00
|
|
|
{
|
2013-01-30 15:12:23 +04:00
|
|
|
MIPSnetState *s = qemu_get_nic_opaque(nc);
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2019-12-06 16:58:05 +03:00
|
|
|
if (s->busy) {
|
2007-10-17 17:39:42 +04:00
|
|
|
return 0;
|
2019-12-06 16:58:05 +03:00
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
return !mipsnet_buffer_full(s);
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:58:05 +03:00
|
|
|
static ssize_t mipsnet_receive(NetClientState *nc,
|
|
|
|
const uint8_t *buf, size_t size)
|
2007-10-17 17:39:42 +04:00
|
|
|
{
|
2013-01-30 15:12:23 +04:00
|
|
|
MIPSnetState *s = qemu_get_nic_opaque(nc);
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2011-09-05 00:29:27 +04:00
|
|
|
trace_mipsnet_receive(size);
|
2019-12-06 16:58:05 +03:00
|
|
|
if (!mipsnet_can_receive(nc)) {
|
2015-07-15 13:19:10 +03:00
|
|
|
return 0;
|
2019-12-06 16:58:05 +03:00
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2016-04-07 13:26:02 +03:00
|
|
|
if (size >= sizeof(s->rx_buffer)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
s->busy = 1;
|
|
|
|
|
|
|
|
/* Just accept everything. */
|
|
|
|
|
|
|
|
/* Write packet data. */
|
|
|
|
memcpy(s->rx_buffer, buf, size);
|
|
|
|
|
|
|
|
s->rx_count = size;
|
|
|
|
s->rx_read = 0;
|
|
|
|
|
|
|
|
/* Now we can signal we have received something. */
|
|
|
|
s->intctl |= MIPSNET_INTCTL_RXDONE;
|
|
|
|
mipsnet_update_irq(s);
|
2009-05-18 16:40:55 +04:00
|
|
|
|
|
|
|
return size;
|
2007-10-17 17:39:42 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t mipsnet_ioport_read(void *opaque, hwaddr addr,
|
2011-09-05 00:29:26 +04:00
|
|
|
unsigned int size)
|
2007-10-17 17:39:42 +04:00
|
|
|
{
|
|
|
|
MIPSnetState *s = opaque;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
addr &= 0x3f;
|
|
|
|
switch (addr) {
|
|
|
|
case MIPSNET_DEV_ID:
|
2019-12-06 16:58:05 +03:00
|
|
|
ret = be32_to_cpu(0x4d495053); /* MIPS */
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_DEV_ID + 4:
|
2019-12-06 16:58:05 +03:00
|
|
|
ret = be32_to_cpu(0x4e455430); /* NET0 */
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_BUSY:
|
2018-12-14 01:37:37 +03:00
|
|
|
ret = s->busy;
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_RX_DATA_COUNT:
|
2018-12-14 01:37:37 +03:00
|
|
|
ret = s->rx_count;
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_TX_DATA_COUNT:
|
2018-12-14 01:37:37 +03:00
|
|
|
ret = s->tx_count;
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_INT_CTL:
|
2018-12-14 01:37:37 +03:00
|
|
|
ret = s->intctl;
|
2007-10-17 17:39:42 +04:00
|
|
|
s->intctl &= ~MIPSNET_INTCTL_TESTBIT;
|
|
|
|
break;
|
|
|
|
case MIPSNET_INTERRUPT_INFO:
|
|
|
|
/* XXX: This seems to be a per-VPE interrupt number. */
|
2018-12-14 01:37:37 +03:00
|
|
|
ret = 0;
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_RX_DATA_BUFFER:
|
|
|
|
if (s->rx_count) {
|
|
|
|
s->rx_count--;
|
|
|
|
ret = s->rx_buffer[s->rx_read++];
|
2015-07-15 13:19:10 +03:00
|
|
|
if (mipsnet_can_receive(s->nic->ncs)) {
|
|
|
|
qemu_flush_queued_packets(qemu_get_queue(s->nic));
|
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* Reads as zero. */
|
|
|
|
case MIPSNET_TX_DATA_BUFFER:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-09-05 00:29:27 +04:00
|
|
|
trace_mipsnet_read(addr, ret);
|
2007-10-17 17:39:42 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void mipsnet_ioport_write(void *opaque, hwaddr addr,
|
2011-09-05 00:29:26 +04:00
|
|
|
uint64_t val, unsigned int size)
|
2007-10-17 17:39:42 +04:00
|
|
|
{
|
|
|
|
MIPSnetState *s = opaque;
|
|
|
|
|
|
|
|
addr &= 0x3f;
|
2011-09-05 00:29:27 +04:00
|
|
|
trace_mipsnet_write(addr, val);
|
2007-10-17 17:39:42 +04:00
|
|
|
switch (addr) {
|
|
|
|
case MIPSNET_TX_DATA_COUNT:
|
2018-12-14 01:37:37 +03:00
|
|
|
s->tx_count = (val <= MAX_ETH_FRAME_SIZE) ? val : 0;
|
2007-10-17 17:39:42 +04:00
|
|
|
s->tx_written = 0;
|
|
|
|
break;
|
|
|
|
case MIPSNET_INT_CTL:
|
|
|
|
if (val & MIPSNET_INTCTL_TXDONE) {
|
|
|
|
s->intctl &= ~MIPSNET_INTCTL_TXDONE;
|
|
|
|
} else if (val & MIPSNET_INTCTL_RXDONE) {
|
|
|
|
s->intctl &= ~MIPSNET_INTCTL_RXDONE;
|
|
|
|
} else if (val & MIPSNET_INTCTL_TESTBIT) {
|
|
|
|
mipsnet_reset(s);
|
|
|
|
s->intctl |= MIPSNET_INTCTL_TESTBIT;
|
|
|
|
} else if (!val) {
|
|
|
|
/* ACK testbit interrupt, flag was cleared on read. */
|
|
|
|
}
|
|
|
|
s->busy = !!s->intctl;
|
|
|
|
mipsnet_update_irq(s);
|
2015-07-15 13:19:10 +03:00
|
|
|
if (mipsnet_can_receive(s->nic->ncs)) {
|
|
|
|
qemu_flush_queued_packets(qemu_get_queue(s->nic));
|
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
break;
|
|
|
|
case MIPSNET_TX_DATA_BUFFER:
|
|
|
|
s->tx_buffer[s->tx_written++] = val;
|
2016-06-08 13:37:04 +03:00
|
|
|
if ((s->tx_written >= MAX_ETH_FRAME_SIZE)
|
|
|
|
|| (s->tx_written == s->tx_count)) {
|
2007-10-17 17:39:42 +04:00
|
|
|
/* Send buffer. */
|
2016-06-08 13:37:04 +03:00
|
|
|
trace_mipsnet_send(s->tx_written);
|
|
|
|
qemu_send_packet(qemu_get_queue(s->nic),
|
|
|
|
s->tx_buffer, s->tx_written);
|
2007-10-17 17:39:42 +04:00
|
|
|
s->tx_count = s->tx_written = 0;
|
|
|
|
s->intctl |= MIPSNET_INTCTL_TXDONE;
|
|
|
|
s->busy = 1;
|
|
|
|
mipsnet_update_irq(s);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* Read-only registers */
|
|
|
|
case MIPSNET_DEV_ID:
|
|
|
|
case MIPSNET_BUSY:
|
|
|
|
case MIPSNET_RX_DATA_COUNT:
|
|
|
|
case MIPSNET_INTERRUPT_INFO:
|
|
|
|
case MIPSNET_RX_DATA_BUFFER:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 01:02:56 +03:00
|
|
|
static const VMStateDescription vmstate_mipsnet = {
|
|
|
|
.name = "mipsnet",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
2023-12-21 06:16:22 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2010-12-02 01:02:56 +03:00
|
|
|
VMSTATE_UINT32(busy, MIPSnetState),
|
|
|
|
VMSTATE_UINT32(rx_count, MIPSnetState),
|
|
|
|
VMSTATE_UINT32(rx_read, MIPSnetState),
|
|
|
|
VMSTATE_UINT32(tx_count, MIPSnetState),
|
|
|
|
VMSTATE_UINT32(tx_written, MIPSnetState),
|
|
|
|
VMSTATE_UINT32(intctl, MIPSnetState),
|
|
|
|
VMSTATE_BUFFER(rx_buffer, MIPSnetState),
|
|
|
|
VMSTATE_BUFFER(tx_buffer, MIPSnetState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2009-11-25 21:49:21 +03:00
|
|
|
static NetClientInfo net_mipsnet_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:21 +03:00
|
|
|
.size = sizeof(NICState),
|
|
|
|
.receive = mipsnet_receive,
|
|
|
|
};
|
|
|
|
|
2012-02-05 14:19:07 +04:00
|
|
|
static const MemoryRegionOps mipsnet_ioport_ops = {
|
2011-09-05 00:29:26 +04:00
|
|
|
.read = mipsnet_ioport_read,
|
|
|
|
.write = mipsnet_ioport_write,
|
|
|
|
.impl.min_access_size = 1,
|
|
|
|
.impl.max_access_size = 4,
|
|
|
|
};
|
2009-01-13 22:39:36 +03:00
|
|
|
|
2018-10-01 09:37:58 +03:00
|
|
|
static void mipsnet_realize(DeviceState *dev, Error **errp)
|
2011-09-05 00:29:26 +04:00
|
|
|
{
|
2018-10-01 09:37:58 +03:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
2013-07-27 17:59:07 +04:00
|
|
|
MIPSnetState *s = MIPS_NET(dev);
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&s->io, OBJECT(dev), &mipsnet_ioport_ops, s,
|
|
|
|
"mipsnet-io", 36);
|
2013-07-27 17:59:07 +04:00
|
|
|
sysbus_init_mmio(sbd, &s->io);
|
|
|
|
sysbus_init_irq(sbd, &s->irq);
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2011-09-05 00:29:26 +04:00
|
|
|
s->nic = qemu_new_nic(&net_mipsnet_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);
|
2011-09-05 00:29:26 +04:00
|
|
|
}
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2011-09-05 00:29:26 +04:00
|
|
|
static void mipsnet_sysbus_reset(DeviceState *dev)
|
|
|
|
{
|
2013-07-27 17:59:07 +04:00
|
|
|
MIPSnetState *s = MIPS_NET(dev);
|
2011-09-05 00:29:26 +04:00
|
|
|
mipsnet_reset(s);
|
|
|
|
}
|
2009-11-25 21:49:21 +03:00
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static Property mipsnet_properties[] = {
|
|
|
|
DEFINE_NIC_PROPERTIES(MIPSnetState, conf),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mipsnet_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
|
|
|
|
2018-10-01 09:37:58 +03:00
|
|
|
dc->realize = mipsnet_realize;
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->desc = "MIPS Simulator network device";
|
2024-09-13 17:31:44 +03:00
|
|
|
device_class_set_legacy_reset(dc, mipsnet_sysbus_reset);
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_mipsnet;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, mipsnet_properties);
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo mipsnet_info = {
|
2013-07-27 17:59:07 +04:00
|
|
|
.name = TYPE_MIPS_NET,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(MIPSnetState),
|
|
|
|
.class_init = mipsnet_class_init,
|
2011-09-05 00:29:26 +04:00
|
|
|
};
|
2007-10-17 17:39:42 +04:00
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void mipsnet_register_types(void)
|
2011-09-05 00:29:26 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&mipsnet_info);
|
2007-10-17 17:39:42 +04:00
|
|
|
}
|
2011-09-05 00:29:26 +04:00
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(mipsnet_register_types)
|