stellaris_enet.c 14.9 KB
Newer Older
P
pbrook 已提交
1 2 3 4 5 6
/*
 * Luminary Micro Stellaris Ethernet Controller
 *
 * Copyright (c) 2007 CodeSourcery.
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the GPL.
P
pbrook 已提交
8
 */
P
Peter Maydell 已提交
9
#include "qemu/osdep.h"
10
#include "hw/sysbus.h"
P
Paolo Bonzini 已提交
11
#include "net/net.h"
12
#include "qemu/log.h"
P
pbrook 已提交
13 14 15 16 17
#include <zlib.h>

//#define DEBUG_STELLARIS_ENET 1

#ifdef DEBUG_STELLARIS_ENET
18 19 20 21
#define DPRINTF(fmt, ...) \
do { printf("stellaris_enet: " fmt , ## __VA_ARGS__); } while (0)
#define BADF(fmt, ...) \
do { fprintf(stderr, "stellaris_enet: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
P
pbrook 已提交
22
#else
23 24 25
#define DPRINTF(fmt, ...) do {} while(0)
#define BADF(fmt, ...) \
do { fprintf(stderr, "stellaris_enet: error: " fmt , ## __VA_ARGS__);} while (0)
P
pbrook 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#endif

#define SE_INT_RX       0x01
#define SE_INT_TXER     0x02
#define SE_INT_TXEMP    0x04
#define SE_INT_FOV      0x08
#define SE_INT_RXER     0x10
#define SE_INT_MD       0x20
#define SE_INT_PHY      0x40

#define SE_RCTL_RXEN    0x01
#define SE_RCTL_AMUL    0x02
#define SE_RCTL_PRMS    0x04
#define SE_RCTL_BADCRC  0x08
#define SE_RCTL_RSTFIFO 0x10

#define SE_TCTL_TXEN    0x01
#define SE_TCTL_PADEN   0x02
#define SE_TCTL_CRC     0x04
#define SE_TCTL_DUPLEX  0x08

47 48 49 50
#define TYPE_STELLARIS_ENET "stellaris_enet"
#define STELLARIS_ENET(obj) \
    OBJECT_CHECK(stellaris_enet_state, (obj), TYPE_STELLARIS_ENET)

51 52 53 54 55
typedef struct {
    uint8_t data[2048];
    uint32_t len;
} StellarisEnetRxFrame;

P
pbrook 已提交
56
typedef struct {
57 58
    SysBusDevice parent_obj;

P
pbrook 已提交
59 60 61 62 63 64 65 66 67 68
    uint32_t ris;
    uint32_t im;
    uint32_t rctl;
    uint32_t tctl;
    uint32_t thr;
    uint32_t mctl;
    uint32_t mdv;
    uint32_t mtxd;
    uint32_t mrxd;
    uint32_t np;
69
    uint32_t tx_fifo_len;
P
pbrook 已提交
70 71 72
    uint8_t tx_fifo[2048];
    /* Real hardware has a 2k fifo, which works out to be at most 31 packets.
       We implement a full 31 packet fifo.  */
73 74 75
    StellarisEnetRxFrame rx[31];
    uint32_t rx_fifo_offset;
    uint32_t next_packet;
76
    NICState *nic;
77
    NICConf conf;
P
pbrook 已提交
78
    qemu_irq irq;
79
    MemoryRegion mmio;
P
pbrook 已提交
80 81
} stellaris_enet_state;

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static const VMStateDescription vmstate_rx_frame = {
    .name = "stellaris_enet/rx_frame",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT8_ARRAY(data, StellarisEnetRxFrame, 2048),
        VMSTATE_UINT32(len, StellarisEnetRxFrame),
        VMSTATE_END_OF_LIST()
    }
};

static int stellaris_enet_post_load(void *opaque, int version_id)
{
    stellaris_enet_state *s = opaque;
    int i;

    /* Sanitize inbound state. Note that next_packet is an index but
     * np is a size; hence their valid upper bounds differ.
     */
    if (s->next_packet >= ARRAY_SIZE(s->rx)) {
        return -1;
    }

    if (s->np > ARRAY_SIZE(s->rx)) {
        return -1;
    }

    for (i = 0; i < ARRAY_SIZE(s->rx); i++) {
        if (s->rx[i].len > ARRAY_SIZE(s->rx[i].data)) {
            return -1;
        }
    }

    if (s->rx_fifo_offset > ARRAY_SIZE(s->rx[0].data) - 4) {
        return -1;
    }

    if (s->tx_fifo_len > ARRAY_SIZE(s->tx_fifo)) {
        return -1;
    }

    return 0;
}

static const VMStateDescription vmstate_stellaris_enet = {
    .name = "stellaris_enet",
    .version_id = 2,
    .minimum_version_id = 2,
    .post_load = stellaris_enet_post_load,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(ris, stellaris_enet_state),
        VMSTATE_UINT32(im, stellaris_enet_state),
        VMSTATE_UINT32(rctl, stellaris_enet_state),
        VMSTATE_UINT32(tctl, stellaris_enet_state),
        VMSTATE_UINT32(thr, stellaris_enet_state),
        VMSTATE_UINT32(mctl, stellaris_enet_state),
        VMSTATE_UINT32(mdv, stellaris_enet_state),
        VMSTATE_UINT32(mtxd, stellaris_enet_state),
        VMSTATE_UINT32(mrxd, stellaris_enet_state),
        VMSTATE_UINT32(np, stellaris_enet_state),
        VMSTATE_UINT32(tx_fifo_len, stellaris_enet_state),
        VMSTATE_UINT8_ARRAY(tx_fifo, stellaris_enet_state, 2048),
        VMSTATE_STRUCT_ARRAY(rx, stellaris_enet_state, 31, 1,
                             vmstate_rx_frame, StellarisEnetRxFrame),
        VMSTATE_UINT32(rx_fifo_offset, stellaris_enet_state),
        VMSTATE_UINT32(next_packet, stellaris_enet_state),
        VMSTATE_END_OF_LIST()
    }
};

P
pbrook 已提交
152 153 154 155 156
static void stellaris_enet_update(stellaris_enet_state *s)
{
    qemu_set_irq(s->irq, (s->ris & s->im) != 0);
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/* Return the data length of the packet currently being assembled
 * in the TX fifo.
 */
static inline int stellaris_txpacket_datalen(stellaris_enet_state *s)
{
    return s->tx_fifo[0] | (s->tx_fifo[1] << 8);
}

/* Return true if the packet currently in the TX FIFO is complete,
* ie the FIFO holds enough bytes for the data length, ethernet header,
* payload and optionally CRC.
*/
static inline bool stellaris_txpacket_complete(stellaris_enet_state *s)
{
    int framelen = stellaris_txpacket_datalen(s);
    framelen += 16;
    if (!(s->tctl & SE_TCTL_CRC)) {
        framelen += 4;
    }
    /* Cover the corner case of a 2032 byte payload with auto-CRC disabled:
     * this requires more bytes than will fit in the FIFO. It's not totally
     * clear how the h/w handles this, but if using threshold-based TX
     * it will definitely try to transmit something.
     */
    framelen = MIN(framelen, ARRAY_SIZE(s->tx_fifo));
    return s->tx_fifo_len >= framelen;
}

185 186 187 188 189 190 191 192 193
/* Return true if the TX FIFO threshold is enabled and the FIFO
 * has filled enough to reach it.
 */
static inline bool stellaris_tx_thr_reached(stellaris_enet_state *s)
{
    return (s->thr < 0x3f &&
            (s->tx_fifo_len >= 4 * (s->thr * 8 + 1)));
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* Send the packet currently in the TX FIFO */
static void stellaris_enet_send(stellaris_enet_state *s)
{
    int framelen = stellaris_txpacket_datalen(s);

    /* Ethernet header is in the FIFO but not in the datacount.
     * We don't implement explicit CRC, so just ignore any
     * CRC value in the FIFO.
     */
    framelen += 14;
    if ((s->tctl & SE_TCTL_PADEN) && framelen < 60) {
        memset(&s->tx_fifo[framelen + 2], 0, 60 - framelen);
        framelen = 60;
    }
    /* This MIN will have no effect unless the FIFO data is corrupt
     * (eg bad data from an incoming migration); otherwise the check
     * on the datalen at the start of writing the data into the FIFO
     * will have caught this. Silently write a corrupt half-packet,
     * which is what the hardware does in FIFO underrun situations.
     */
    framelen = MIN(framelen, ARRAY_SIZE(s->tx_fifo) - 2);
    qemu_send_packet(qemu_get_queue(s->nic), s->tx_fifo + 2, framelen);
    s->tx_fifo_len = 0;
    s->ris |= SE_INT_TXEMP;
    stellaris_enet_update(s);
    DPRINTF("Done TX\n");
}

P
pbrook 已提交
222
/* TODO: Implement MAC address filtering.  */
223
static ssize_t stellaris_enet_receive(NetClientState *nc, const uint8_t *buf, size_t size)
P
pbrook 已提交
224
{
J
Jason Wang 已提交
225
    stellaris_enet_state *s = qemu_get_nic_opaque(nc);
P
pbrook 已提交
226 227 228 229 230
    int n;
    uint8_t *p;
    uint32_t crc;

    if ((s->rctl & SE_RCTL_RXEN) == 0)
231
        return -1;
P
pbrook 已提交
232
    if (s->np >= 31) {
233
        return 0;
P
pbrook 已提交
234 235
    }

236
    DPRINTF("Received packet len=%zu\n", size);
P
pbrook 已提交
237 238 239 240
    n = s->next_packet + s->np;
    if (n >= 31)
        n -= 31;

241 242 243 244 245 246 247 248 249 250 251
    if (size >= sizeof(s->rx[n].data) - 6) {
        /* If the packet won't fit into the
         * emulated 2K RAM, this is reported
         * as a FIFO overrun error.
         */
        s->ris |= SE_INT_FOV;
        stellaris_enet_update(s);
        return -1;
    }

    s->np++;
P
pbrook 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    s->rx[n].len = size + 6;
    p = s->rx[n].data;
    *(p++) = (size + 6);
    *(p++) = (size + 6) >> 8;
    memcpy (p, buf, size);
    p += size;
    crc = crc32(~0, buf, size);
    *(p++) = crc;
    *(p++) = crc >> 8;
    *(p++) = crc >> 16;
    *(p++) = crc >> 24;
    /* Clear the remaining bytes in the last word.  */
    if ((size & 3) != 2) {
        memset(p, 0, (6 - size) & 3);
    }

    s->ris |= SE_INT_RX;
    stellaris_enet_update(s);
270 271

    return size;
P
pbrook 已提交
272 273
}

274
static int stellaris_enet_can_receive(stellaris_enet_state *s)
P
pbrook 已提交
275 276 277 278
{
    return (s->np < 31);
}

A
Avi Kivity 已提交
279
static uint64_t stellaris_enet_read(void *opaque, hwaddr offset,
280
                                    unsigned size)
P
pbrook 已提交
281
{
P
Paul Brook 已提交
282
    stellaris_enet_state *s = (stellaris_enet_state *)opaque;
P
pbrook 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
    uint32_t val;

    switch (offset) {
    case 0x00: /* RIS */
        DPRINTF("IRQ status %02x\n", s->ris);
        return s->ris;
    case 0x04: /* IM */
        return s->im;
    case 0x08: /* RCTL */
        return s->rctl;
    case 0x0c: /* TCTL */
        return s->tctl;
    case 0x10: /* DATA */
296 297 298 299 300 301
    {
        uint8_t *rx_fifo;

        if (s->np == 0) {
            BADF("RX underflow\n");
            return 0;
P
pbrook 已提交
302
        }
303 304 305 306 307 308 309 310

        rx_fifo = s->rx[s->next_packet].data + s->rx_fifo_offset;

        val = rx_fifo[0] | (rx_fifo[1] << 8) | (rx_fifo[2] << 16)
              | (rx_fifo[3] << 24);
        s->rx_fifo_offset += 4;
        if (s->rx_fifo_offset >= s->rx[s->next_packet].len) {
            s->rx_fifo_offset = 0;
P
pbrook 已提交
311 312 313 314 315
            s->next_packet++;
            if (s->next_packet >= 31)
                s->next_packet = 0;
            s->np--;
            DPRINTF("RX done np=%d\n", s->np);
316 317 318
            if (!s->np && stellaris_enet_can_receive(s)) {
                qemu_flush_queued_packets(qemu_get_queue(s->nic));
            }
P
pbrook 已提交
319 320
        }
        return val;
321
    }
P
pbrook 已提交
322
    case 0x14: /* IA0 */
323
        return s->conf.macaddr.a[0] | (s->conf.macaddr.a[1] << 8)
324 325
            | (s->conf.macaddr.a[2] << 16)
            | ((uint32_t)s->conf.macaddr.a[3] << 24);
P
pbrook 已提交
326
    case 0x18: /* IA1 */
327
        return s->conf.macaddr.a[4] | (s->conf.macaddr.a[5] << 8);
P
pbrook 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    case 0x1c: /* THR */
        return s->thr;
    case 0x20: /* MCTL */
        return s->mctl;
    case 0x24: /* MDV */
        return s->mdv;
    case 0x28: /* MADD */
        return 0;
    case 0x2c: /* MTXD */
        return s->mtxd;
    case 0x30: /* MRXD */
        return s->mrxd;
    case 0x34: /* NP */
        return s->np;
    case 0x38: /* TR */
        return 0;
344
    case 0x3c: /* Undocumented: Timestamp? */
P
pbrook 已提交
345 346
        return 0;
    default:
347 348 349
        qemu_log_mask(LOG_GUEST_ERROR, "stellaris_enet_rd%d: Illegal register"
                                       " 0x02%" HWADDR_PRIx "\n",
                      size * 8, offset);
P
pbrook 已提交
350 351 352 353
        return 0;
    }
}

A
Avi Kivity 已提交
354
static void stellaris_enet_write(void *opaque, hwaddr offset,
355
                                 uint64_t value, unsigned size)
P
pbrook 已提交
356 357 358 359 360 361
{
    stellaris_enet_state *s = (stellaris_enet_state *)opaque;

    switch (offset) {
    case 0x00: /* IACK */
        s->ris &= ~value;
362
        DPRINTF("IRQ ack %02" PRIx64 "/%02x\n", value, s->ris);
P
pbrook 已提交
363 364
        stellaris_enet_update(s);
        /* Clearing TXER also resets the TX fifo.  */
365 366 367
        if (value & SE_INT_TXER) {
            s->tx_fifo_len = 0;
        }
P
pbrook 已提交
368 369
        break;
    case 0x04: /* IM */
370
        DPRINTF("IRQ mask %02" PRIx64 "/%02x\n", value, s->ris);
P
pbrook 已提交
371 372 373 374 375 376 377
        s->im = value;
        stellaris_enet_update(s);
        break;
    case 0x08: /* RCTL */
        s->rctl = value;
        if (value & SE_RCTL_RSTFIFO) {
            s->np = 0;
378
            s->rx_fifo_offset = 0;
P
pbrook 已提交
379 380 381 382 383 384 385
            stellaris_enet_update(s);
        }
        break;
    case 0x0c: /* TCTL */
        s->tctl = value;
        break;
    case 0x10: /* DATA */
386 387 388 389 390
        if (s->tx_fifo_len == 0) {
            /* The first word is special, it contains the data length */
            int framelen = value & 0xffff;
            if (framelen > 2032) {
                DPRINTF("TX frame too long (%d)\n", framelen);
P
pbrook 已提交
391 392
                s->ris |= SE_INT_TXER;
                stellaris_enet_update(s);
393
                break;
P
pbrook 已提交
394 395
            }
        }
396 397 398 399 400 401 402 403

        if (s->tx_fifo_len + 4 <= ARRAY_SIZE(s->tx_fifo)) {
            s->tx_fifo[s->tx_fifo_len++] = value;
            s->tx_fifo[s->tx_fifo_len++] = value >> 8;
            s->tx_fifo[s->tx_fifo_len++] = value >> 16;
            s->tx_fifo[s->tx_fifo_len++] = value >> 24;
        }

404
        if (stellaris_tx_thr_reached(s) && stellaris_txpacket_complete(s)) {
405 406
            stellaris_enet_send(s);
        }
P
pbrook 已提交
407 408
        break;
    case 0x14: /* IA0 */
409 410 411 412
        s->conf.macaddr.a[0] = value;
        s->conf.macaddr.a[1] = value >> 8;
        s->conf.macaddr.a[2] = value >> 16;
        s->conf.macaddr.a[3] = value >> 24;
P
pbrook 已提交
413 414
        break;
    case 0x18: /* IA1 */
415 416
        s->conf.macaddr.a[4] = value;
        s->conf.macaddr.a[5] = value >> 8;
P
pbrook 已提交
417 418 419 420 421
        break;
    case 0x1c: /* THR */
        s->thr = value;
        break;
    case 0x20: /* MCTL */
422 423 424 425
        /* TODO: MII registers aren't modelled.
         * Clear START, indicating that the operation completes immediately.
         */
        s->mctl = value & ~1;
P
pbrook 已提交
426 427 428 429 430 431 432 433 434 435
        break;
    case 0x24: /* MDV */
        s->mdv = value;
        break;
    case 0x28: /* MADD */
        /* ignored.  */
        break;
    case 0x2c: /* MTXD */
        s->mtxd = value & 0xff;
        break;
436 437 438 439 440
    case 0x38: /* TR */
        if (value & 1) {
            stellaris_enet_send(s);
        }
        break;
P
pbrook 已提交
441 442 443 444 445 446 447
    case 0x30: /* MRXD */
    case 0x34: /* NP */
        /* Ignored.  */
    case 0x3c: /* Undocuented: Timestamp? */
        /* Ignored.  */
        break;
    default:
448 449 450
        qemu_log_mask(LOG_GUEST_ERROR, "stellaris_enet_wr%d: Illegal register "
                                       "0x02%" HWADDR_PRIx " = 0x%" PRIx64 "\n",
                      size * 8, offset, value);
P
pbrook 已提交
451 452 453
    }
}

454 455 456 457
static const MemoryRegionOps stellaris_enet_ops = {
    .read = stellaris_enet_read,
    .write = stellaris_enet_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
P
pbrook 已提交
458 459 460 461 462 463 464 465 466
};

static void stellaris_enet_reset(stellaris_enet_state *s)
{
    s->mdv = 0x80;
    s->rctl = SE_RCTL_BADCRC;
    s->im = SE_INT_PHY | SE_INT_MD | SE_INT_RXER | SE_INT_FOV | SE_INT_TXEMP
            | SE_INT_TXER | SE_INT_RX;
    s->thr = 0x3f;
467
    s->tx_fifo_len = 0;
P
pbrook 已提交
468 469
}

470
static NetClientInfo net_stellaris_enet_info = {
471
    .type = NET_CLIENT_DRIVER_NIC,
472 473 474 475
    .size = sizeof(NICState),
    .receive = stellaris_enet_receive,
};

476
static int stellaris_enet_init(SysBusDevice *sbd)
P
pbrook 已提交
477
{
478 479
    DeviceState *dev = DEVICE(sbd);
    stellaris_enet_state *s = STELLARIS_ENET(dev);
P
pbrook 已提交
480

481 482
    memory_region_init_io(&s->mmio, OBJECT(s), &stellaris_enet_ops, s,
                          "stellaris_enet", 0x1000);
483 484
    sysbus_init_mmio(sbd, &s->mmio);
    sysbus_init_irq(sbd, &s->irq);
485
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
P
Paul Brook 已提交
486

487
    s->nic = qemu_new_nic(&net_stellaris_enet_info, &s->conf,
488
                          object_get_typename(OBJECT(dev)), dev->id, s);
J
Jason Wang 已提交
489
    qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
P
pbrook 已提交
490 491

    stellaris_enet_reset(s);
492
    return 0;
P
pbrook 已提交
493
}
P
Paul Brook 已提交
494

495 496 497 498 499 500 501
static Property stellaris_enet_properties[] = {
    DEFINE_NIC_PROPERTIES(stellaris_enet_state, conf),
    DEFINE_PROP_END_OF_LIST(),
};

static void stellaris_enet_class_init(ObjectClass *klass, void *data)
{
502
    DeviceClass *dc = DEVICE_CLASS(klass);
503 504 505
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = stellaris_enet_init;
506
    dc->props = stellaris_enet_properties;
507
    dc->vmsd = &vmstate_stellaris_enet;
508 509
}

510
static const TypeInfo stellaris_enet_info = {
511
    .name          = TYPE_STELLARIS_ENET,
512 513 514
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(stellaris_enet_state),
    .class_init    = stellaris_enet_class_init,
515 516
};

A
Andreas Färber 已提交
517
static void stellaris_enet_register_types(void)
P
Paul Brook 已提交
518
{
519
    type_register_static(&stellaris_enet_info);
P
Paul Brook 已提交
520 521
}

A
Andreas Färber 已提交
522
type_init(stellaris_enet_register_types)