xilinx_ethlite.c 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * 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.
 */

#include "sysbus.h"
#include "hw.h"
P
Paolo Bonzini 已提交
27
#include "net/net.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#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

struct xlx_ethlite
{
    SysBusDevice busdev;
53
    MemoryRegion mmio;
54
    qemu_irq irq;
55
    NICState *nic;
56
    NICConf conf;
57

G
Gerd Hoffmann 已提交
58 59
    uint32_t c_tx_pingpong;
    uint32_t c_rx_pingpong;
60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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);
    }
}

74
static uint64_t
A
Avi Kivity 已提交
75
eth_read(void *opaque, hwaddr addr, unsigned int size)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
    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];
92
            D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr * 4, r));
93 94 95
            break;

        default:
96
            r = tswap32(s->regs[addr]);
97 98 99 100 101 102
            break;
    }
    return r;
}

static void
A
Avi Kivity 已提交
103
eth_write(void *opaque, hwaddr addr,
104
          uint64_t val64, unsigned int size)
105 106 107
{
    struct xlx_ethlite *s = opaque;
    unsigned int base = 0;
108
    uint32_t value = val64;
109 110 111 112 113 114 115 116 117

    addr >>= 2;
    switch (addr) 
    {
        case R_TX_CTRL0:
        case R_TX_CTRL1:
            if (addr == R_TX_CTRL1)
                base = 0x800 / 4;

118 119
            D(qemu_log("%s addr=" TARGET_FMT_plx " val=%x\n",
                       __func__, addr * 4, value));
120
            if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
J
Jason Wang 已提交
121
                qemu_send_packet(qemu_get_queue(s->nic),
122 123 124 125 126 127
                                 (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)) {
128
                memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
129 130 131 132 133 134 135 136 137 138
                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.  */
139 140 141
        case R_RX_CTRL0:
        case R_RX_CTRL1:
            if (!(value & CTRL_S)) {
J
Jason Wang 已提交
142
                qemu_flush_queued_packets(qemu_get_queue(s->nic));
143
            }
144 145 146
        case R_TX_LEN0:
        case R_TX_LEN1:
        case R_TX_GIE0:
147 148
            D(qemu_log("%s addr=" TARGET_FMT_plx " val=%x\n",
                       __func__, addr * 4, value));
149 150 151 152
            s->regs[addr] = value;
            break;

        default:
153
            s->regs[addr] = tswap32(value);
154 155 156 157
            break;
    }
}

158 159 160 161 162 163 164 165
static const MemoryRegionOps eth_ops = {
    .read = eth_read,
    .write = eth_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4
    }
166 167
};

168
static int eth_can_rx(NetClientState *nc)
169
{
170
    struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
171 172 173
    unsigned int rxbase = s->rxbuf * (0x800 / 4);

    return !(s->regs[rxbase + R_RX_CTRL0] & CTRL_S);
174 175
}

176
static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
177
{
178
    struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
179 180 181
    unsigned int rxbase = s->rxbuf * (0x800 / 4);

    /* DA filter.  */
182
    if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
183
        return size;
184 185 186

    if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
        D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
187
        return -1;
188 189
    }

190
    D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
191 192 193 194 195 196 197 198
    memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);

    s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
    if (s->regs[rxbase + R_RX_CTRL0] & CTRL_I)
        eth_pulse_irq(s);

    /* If c_rx_pingpong was set flip buffers.  */
    s->rxbuf ^= s->c_rx_pingpong;
199
    return size;
200 201
}

202
static void eth_cleanup(NetClientState *nc)
203
{
204
    struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
205

206
    s->nic = NULL;
207 208
}

209
static NetClientInfo net_xilinx_ethlite_info = {
210
    .type = NET_CLIENT_OPTIONS_KIND_NIC,
211 212 213 214 215 216
    .size = sizeof(NICState),
    .can_receive = eth_can_rx,
    .receive = eth_rx,
    .cleanup = eth_cleanup,
};

217
static int xilinx_ethlite_init(SysBusDevice *dev)
218 219 220 221 222 223
{
    struct xlx_ethlite *s = FROM_SYSBUS(typeof (*s), dev);

    sysbus_init_irq(dev, &s->irq);
    s->rxbuf = 0;

224 225
    memory_region_init_io(&s->mmio, &eth_ops, s, "xlnx.xps-ethernetlite",
                                                                    R_MAX * 4);
226
    sysbus_init_mmio(dev, &s->mmio);
227

228
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
229
    s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
230
                          object_get_typename(OBJECT(dev)), dev->qdev.id, s);
J
Jason Wang 已提交
231
    qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
232
    return 0;
233 234
}

235
static Property xilinx_ethlite_properties[] = {
236 237
    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),
238 239 240 241 242 243
    DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
    DEFINE_PROP_END_OF_LIST(),
};

static void xilinx_ethlite_class_init(ObjectClass *klass, void *data)
{
244
    DeviceClass *dc = DEVICE_CLASS(klass);
245 246 247
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = xilinx_ethlite_init;
248
    dc->props = xilinx_ethlite_properties;
249 250
}

251
static const TypeInfo xilinx_ethlite_info = {
252
    .name          = "xlnx.xps-ethernetlite",
253 254 255
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(struct xlx_ethlite),
    .class_init    = xilinx_ethlite_class_init,
G
Gerd Hoffmann 已提交
256 257
};

A
Andreas Färber 已提交
258
static void xilinx_ethlite_register_types(void)
259
{
260
    type_register_static(&xilinx_ethlite_info);
261 262
}

A
Andreas Färber 已提交
263
type_init(xilinx_ethlite_register_types)