2007-11-18 01:14:51 +08:00
|
|
|
#include "hw.h"
|
|
|
|
#include "mips.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "isa.h"
|
2007-10-17 21:39:42 +08:00
|
|
|
|
2007-10-28 09:43:52 +08:00
|
|
|
//#define DEBUG_MIPSNET_SEND
|
|
|
|
//#define DEBUG_MIPSNET_RECEIVE
|
2007-10-17 21:39:42 +08:00
|
|
|
//#define DEBUG_MIPSNET_DATA
|
2007-10-28 09:43:52 +08:00
|
|
|
//#define DEBUG_MIPSNET_IRQ
|
2007-10-17 21:39:42 +08:00
|
|
|
|
|
|
|
/* MIPSnet register offsets */
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
typedef struct MIPSnetState {
|
|
|
|
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];
|
2009-04-18 01:11:08 +08:00
|
|
|
int io_base;
|
2007-10-17 21:39:42 +08:00
|
|
|
qemu_irq irq;
|
2009-11-26 02:49:21 +08:00
|
|
|
NICState *nic;
|
|
|
|
NICConf conf;
|
2007-10-17 21:39:42 +08:00
|
|
|
} MIPSnetState;
|
|
|
|
|
|
|
|
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;
|
|
|
|
#ifdef DEBUG_MIPSNET_IRQ
|
|
|
|
printf("mipsnet: Set IRQ to %d (%02x)\n", isr, s->intctl);
|
|
|
|
#endif
|
|
|
|
qemu_set_irq(s->irq, isr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mipsnet_buffer_full(MIPSnetState *s)
|
|
|
|
{
|
|
|
|
if (s->rx_count >= MAX_ETH_FRAME_SIZE)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-26 02:49:21 +08:00
|
|
|
static int mipsnet_can_receive(VLANClientState *nc)
|
2007-10-17 21:39:42 +08:00
|
|
|
{
|
2009-11-26 02:49:21 +08:00
|
|
|
MIPSnetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
2007-10-17 21:39:42 +08:00
|
|
|
|
|
|
|
if (s->busy)
|
|
|
|
return 0;
|
|
|
|
return !mipsnet_buffer_full(s);
|
|
|
|
}
|
|
|
|
|
2009-11-26 02:49:21 +08:00
|
|
|
static ssize_t mipsnet_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
|
2007-10-17 21:39:42 +08:00
|
|
|
{
|
2009-11-26 02:49:21 +08:00
|
|
|
MIPSnetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
2007-10-17 21:39:42 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_MIPSNET_RECEIVE
|
2010-10-01 03:33:15 +08:00
|
|
|
printf("mipsnet: receiving len=%zu\n", size);
|
2007-10-17 21:39:42 +08:00
|
|
|
#endif
|
2009-11-26 02:49:21 +08:00
|
|
|
if (!mipsnet_can_receive(nc))
|
2009-05-18 20:40:55 +08:00
|
|
|
return -1;
|
2007-10-17 21:39:42 +08: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 20:40:55 +08:00
|
|
|
|
|
|
|
return size;
|
2007-10-17 21:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t mipsnet_ioport_read(void *opaque, uint32_t addr)
|
|
|
|
{
|
|
|
|
MIPSnetState *s = opaque;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
addr &= 0x3f;
|
|
|
|
switch (addr) {
|
|
|
|
case MIPSNET_DEV_ID:
|
2008-03-29 06:29:33 +08:00
|
|
|
ret = be32_to_cpu(0x4d495053); /* MIPS */
|
2007-10-17 21:39:42 +08:00
|
|
|
break;
|
|
|
|
case MIPSNET_DEV_ID + 4:
|
2008-03-29 06:29:33 +08:00
|
|
|
ret = be32_to_cpu(0x4e455430); /* NET0 */
|
2007-10-17 21:39:42 +08:00
|
|
|
break;
|
|
|
|
case MIPSNET_BUSY:
|
|
|
|
ret = s->busy;
|
|
|
|
break;
|
|
|
|
case MIPSNET_RX_DATA_COUNT:
|
|
|
|
ret = s->rx_count;
|
|
|
|
break;
|
|
|
|
case MIPSNET_TX_DATA_COUNT:
|
|
|
|
ret = s->tx_count;
|
|
|
|
break;
|
|
|
|
case MIPSNET_INT_CTL:
|
|
|
|
ret = s->intctl;
|
|
|
|
s->intctl &= ~MIPSNET_INTCTL_TESTBIT;
|
|
|
|
break;
|
|
|
|
case MIPSNET_INTERRUPT_INFO:
|
|
|
|
/* XXX: This seems to be a per-VPE interrupt number. */
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case MIPSNET_RX_DATA_BUFFER:
|
|
|
|
if (s->rx_count) {
|
|
|
|
s->rx_count--;
|
|
|
|
ret = s->rx_buffer[s->rx_read++];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* Reads as zero. */
|
|
|
|
case MIPSNET_TX_DATA_BUFFER:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_MIPSNET_DATA
|
|
|
|
printf("mipsnet: read addr=0x%02x val=0x%02x\n", addr, ret);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mipsnet_ioport_write(void *opaque, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
MIPSnetState *s = opaque;
|
|
|
|
|
|
|
|
addr &= 0x3f;
|
|
|
|
#ifdef DEBUG_MIPSNET_DATA
|
|
|
|
printf("mipsnet: write addr=0x%02x val=0x%02x\n", addr, val);
|
|
|
|
#endif
|
|
|
|
switch (addr) {
|
|
|
|
case MIPSNET_TX_DATA_COUNT:
|
|
|
|
s->tx_count = (val <= MAX_ETH_FRAME_SIZE) ? val : 0;
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
case MIPSNET_TX_DATA_BUFFER:
|
|
|
|
s->tx_buffer[s->tx_written++] = val;
|
|
|
|
if (s->tx_written == s->tx_count) {
|
|
|
|
/* Send buffer. */
|
|
|
|
#ifdef DEBUG_MIPSNET_SEND
|
|
|
|
printf("mipsnet: sending len=%d\n", s->tx_count);
|
|
|
|
#endif
|
2009-11-26 02:49:21 +08:00
|
|
|
qemu_send_packet(&s->nic->nc, s->tx_buffer, s->tx_count);
|
2007-10-17 21:39:42 +08: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 06:02:56 +08:00
|
|
|
static const VMStateDescription vmstate_mipsnet = {
|
|
|
|
.name = "mipsnet",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
|
|
|
.minimum_version_id_old = 0,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
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 21:39:42 +08:00
|
|
|
|
2009-11-26 02:49:21 +08:00
|
|
|
static void mipsnet_cleanup(VLANClientState *nc)
|
2009-04-18 01:11:08 +08:00
|
|
|
{
|
2009-11-26 02:49:21 +08:00
|
|
|
MIPSnetState *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
2009-04-18 01:11:08 +08:00
|
|
|
|
2010-12-02 06:02:56 +08:00
|
|
|
vmstate_unregister(NULL, &vmstate_mipsnet, s);
|
2009-04-18 01:11:08 +08:00
|
|
|
|
|
|
|
isa_unassign_ioport(s->io_base, 36);
|
|
|
|
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free(s);
|
2009-04-18 01:11:08 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 02:49:21 +08:00
|
|
|
static NetClientInfo net_mipsnet_info = {
|
|
|
|
.type = NET_CLIENT_TYPE_NIC,
|
|
|
|
.size = sizeof(NICState),
|
|
|
|
.can_receive = mipsnet_can_receive,
|
|
|
|
.receive = mipsnet_receive,
|
|
|
|
.cleanup = mipsnet_cleanup,
|
|
|
|
};
|
|
|
|
|
2007-10-17 21:39:42 +08:00
|
|
|
void mipsnet_init (int base, qemu_irq irq, NICInfo *nd)
|
|
|
|
{
|
|
|
|
MIPSnetState *s;
|
|
|
|
|
2009-01-14 03:39:36 +08:00
|
|
|
qemu_check_nic_model(nd, "mipsnet");
|
|
|
|
|
2011-08-21 11:09:37 +08:00
|
|
|
s = g_malloc0(sizeof(MIPSnetState));
|
2007-10-17 21:39:42 +08:00
|
|
|
|
|
|
|
register_ioport_write(base, 36, 1, mipsnet_ioport_write, s);
|
|
|
|
register_ioport_read(base, 36, 1, mipsnet_ioport_read, s);
|
|
|
|
register_ioport_write(base, 36, 2, mipsnet_ioport_write, s);
|
|
|
|
register_ioport_read(base, 36, 2, mipsnet_ioport_read, s);
|
|
|
|
register_ioport_write(base, 36, 4, mipsnet_ioport_write, s);
|
|
|
|
register_ioport_read(base, 36, 4, mipsnet_ioport_read, s);
|
|
|
|
|
2009-04-18 01:11:08 +08:00
|
|
|
s->io_base = base;
|
2007-10-17 21:39:42 +08:00
|
|
|
s->irq = irq;
|
2009-11-26 02:49:21 +08:00
|
|
|
|
2009-10-09 02:58:30 +08:00
|
|
|
if (nd) {
|
2011-07-20 18:20:22 +08:00
|
|
|
s->conf.macaddr = nd->macaddr;
|
2009-11-26 02:49:21 +08:00
|
|
|
s->conf.vlan = nd->vlan;
|
|
|
|
s->conf.peer = nd->netdev;
|
2007-10-17 21:39:42 +08:00
|
|
|
|
2009-11-26 02:49:21 +08:00
|
|
|
s->nic = qemu_new_nic(&net_mipsnet_info, &s->conf,
|
|
|
|
nd->model, nd->name, s);
|
|
|
|
|
|
|
|
qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
|
|
|
|
}
|
2007-10-17 21:39:42 +08:00
|
|
|
|
|
|
|
mipsnet_reset(s);
|
2010-12-02 06:02:56 +08:00
|
|
|
vmstate_register(NULL, 0, &vmstate_mipsnet, s);
|
2007-10-17 21:39:42 +08:00
|
|
|
}
|