]> git.proxmox.com Git - qemu.git/blobdiff - hw/ne2000.c
user: Restore debug usage message for '-d ?' in user mode emulation
[qemu.git] / hw / ne2000.c
index 0b35495f99eebd6ec9104a67251f153bbdd630cf..f8acaaeeb641d7882bca6bb0fe2eb6fb33f0f2be 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * QEMU NE2000 emulation
- * 
+ *
  * Copyright (c) 2003-2004 Fabrice Bellard
- * 
+ *
  * 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
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <getopt.h>
-#include <inttypes.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <time.h>
-#include <sys/time.h>
-#include <malloc.h>
-#include <termios.h>
-#include <sys/poll.h>
-#include <errno.h>
-#include <sys/wait.h>
-#include <netinet/in.h>
-
-#include "cpu.h"
-#include "vl.h"
+#include "hw.h"
+#include "pci.h"
+#include "net.h"
+#include "ne2000.h"
+#include "loader.h"
+#include "sysemu.h"
 
 /* debug NE2000 card */
 //#define DEBUG_NE2000
 
-/***********************************************************/
-/* ne2000 emulation */
+#define MAX_ETH_FRAME_SIZE 1514
 
 #define E8390_CMD      0x00  /* The command register (for all pages) */
 /* Page 0 register offsets. */
@@ -68,7 +52,9 @@
 #define EN0_CRDAHI     0x09    /* high byte, current remote dma address RD */
 #define EN0_RSARHI     0x09    /* Remote start address reg 1 */
 #define EN0_RCNTLO     0x0a    /* Remote byte count reg WR */
+#define EN0_RTL8029ID0 0x0a    /* Realtek ID byte #1 RD */
 #define EN0_RCNTHI     0x0b    /* Remote byte count reg WR */
+#define EN0_RTL8029ID1 0x0b    /* Realtek ID byte #2 RD */
 #define EN0_RSR                0x0c    /* rx status reg RD */
 #define EN0_RXCR       0x0c    /* RX configuration reg WR */
 #define EN0_TXCR       0x0d    /* TX configuration reg WR */
 #define EN1_CURPAG      0x17
 #define EN1_MULT        0x18
 
+#define EN2_STARTPG    0x21    /* Starting page of ring bfr RD */
+#define EN2_STOPPG     0x22    /* Ending page +1 of ring bfr RD */
+
+#define EN3_CONFIG0    0x33
+#define EN3_CONFIG1    0x34
+#define EN3_CONFIG2    0x35
+#define EN3_CONFIG3    0x36
+
 /*  Register accessed at EN_CMD, the 8390 base addr.  */
 #define E8390_STOP     0x01    /* Stop and reset the chip */
 #define E8390_START    0x02    /* Start the chip, clear reset */
 #define ENTSR_CDH 0x40 /* The collision detect "heartbeat" signal was lost. */
 #define ENTSR_OWC 0x80  /* There was an out-of-window collision. */
 
-#define NE2000_MEM_SIZE 32768
-
-typedef struct NE2000State {
-    uint8_t cmd;
-    uint32_t start;
-    uint32_t stop;
-    uint8_t boundary;
-    uint8_t tsr;
-    uint8_t tpsr;
-    uint16_t tcnt;
-    uint16_t rcnt;
-    uint32_t rsar;
-    uint8_t isr;
-    uint8_t dcfg;
-    uint8_t imr;
-    uint8_t phys[6]; /* mac address */
-    uint8_t curpag;
-    uint8_t mult[8]; /* multicast mask array */
-    int irq;
-    uint8_t mem[NE2000_MEM_SIZE];
-} NE2000State;
-
-static NE2000State ne2000_state;
-int net_fd = -1;
-
-static void ne2000_reset(NE2000State *s)
+typedef struct PCINE2000State {
+    PCIDevice dev;
+    NE2000State ne2000;
+} PCINE2000State;
+
+void ne2000_reset(NE2000State *s)
 {
     int i;
 
     s->isr = ENISR_RESET;
-    s->mem[0] = 0x52;
-    s->mem[1] = 0x54;
-    s->mem[2] = 0x00;
-    s->mem[3] = 0x12;
-    s->mem[4] = 0x34;
-    s->mem[5] = 0x56;
+    memcpy(s->mem, &s->c.macaddr, 6);
     s->mem[14] = 0x57;
     s->mem[15] = 0x57;
 
@@ -173,21 +142,42 @@ static void ne2000_reset(NE2000State *s)
 static void ne2000_update_irq(NE2000State *s)
 {
     int isr;
-    isr = s->isr & s->imr;
-    if (isr)
-        pic_set_irq(s->irq, 1);
-    else
-        pic_set_irq(s->irq, 0);
+    isr = (s->isr & s->imr) & 0x7f;
+#if defined(DEBUG_NE2000)
+    printf("NE2000: Set IRQ to %d (%02x %02x)\n",
+          isr ? 1 : 0, s->isr, s->imr);
+#endif
+    qemu_set_irq(s->irq, (isr != 0));
 }
 
-/* return true if the NE2000 can receive more data */
-int ne2000_can_receive(void)
+#define POLYNOMIAL 0x04c11db6
+
+/* From FreeBSD */
+/* XXX: optimize */
+static int compute_mcast_idx(const uint8_t *ep)
+{
+    uint32_t crc;
+    int carry, i, j;
+    uint8_t b;
+
+    crc = 0xffffffff;
+    for (i = 0; i < 6; i++) {
+        b = *ep++;
+        for (j = 0; j < 8; j++) {
+            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
+            crc <<= 1;
+            b >>= 1;
+            if (carry)
+                crc = ((crc ^ POLYNOMIAL) | carry);
+        }
+    }
+    return (crc >> 26);
+}
+
+static int ne2000_buffer_full(NE2000State *s)
 {
-    NE2000State *s = &ne2000_state;
     int avail, index, boundary;
-    
-    if (s->cmd & E8390_STOP)
-        return 0;
+
     index = s->curpag << 8;
     boundary = s->boundary << 8;
     if (index < boundary)
@@ -195,20 +185,74 @@ int ne2000_can_receive(void)
     else
         avail = (s->stop - s->start) - (index - boundary);
     if (avail < (MAX_ETH_FRAME_SIZE + 4))
-        return 0;
-    return 1;
+        return 1;
+    return 0;
 }
 
-void ne2000_receive(uint8_t *buf, int size)
+int ne2000_can_receive(VLANClientState *nc)
 {
-    NE2000State *s = &ne2000_state;
+    NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
+
+    if (s->cmd & E8390_STOP)
+        return 1;
+    return !ne2000_buffer_full(s);
+}
+
+#define MIN_BUF_SIZE 60
+
+ssize_t ne2000_receive(VLANClientState *nc, const uint8_t *buf, size_t size_)
+{
+    NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
+    int size = size_;
     uint8_t *p;
-    int total_len, next, avail, len, index;
+    unsigned int total_len, next, avail, len, index, mcast_idx;
+    uint8_t buf1[60];
+    static const uint8_t broadcast_macaddr[6] =
+        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
 #if defined(DEBUG_NE2000)
     printf("NE2000: received len=%d\n", size);
 #endif
 
+    if (s->cmd & E8390_STOP || ne2000_buffer_full(s))
+        return -1;
+
+    /* XXX: check this */
+    if (s->rxcr & 0x10) {
+        /* promiscuous: receive all */
+    } else {
+        if (!memcmp(buf,  broadcast_macaddr, 6)) {
+            /* broadcast address */
+            if (!(s->rxcr & 0x04))
+                return size;
+        } else if (buf[0] & 0x01) {
+            /* multicast */
+            if (!(s->rxcr & 0x08))
+                return size;
+            mcast_idx = compute_mcast_idx(buf);
+            if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
+                return size;
+        } else if (s->mem[0] == buf[0] &&
+                   s->mem[2] == buf[1] &&
+                   s->mem[4] == buf[2] &&
+                   s->mem[6] == buf[3] &&
+                   s->mem[8] == buf[4] &&
+                   s->mem[10] == buf[5]) {
+            /* match */
+        } else {
+            return size;
+        }
+    }
+
+
+    /* if too small buffer, then expand it */
+    if (size < MIN_BUF_SIZE) {
+        memcpy(buf1, buf, size);
+        memset(buf1 + size, 0, MIN_BUF_SIZE - size);
+        buf = buf1;
+        size = MIN_BUF_SIZE;
+    }
+
     index = s->curpag << 8;
     /* 4 bytes for header */
     total_len = size + 4;
@@ -218,7 +262,11 @@ void ne2000_receive(uint8_t *buf, int size)
         next -= (s->stop - s->start);
     /* prepare packet header */
     p = s->mem + index;
-    p[0] = ENRSR_RXOK; /* receive status */
+    s->rsr = ENRSR_RXOK; /* receive status */
+    /* XXX: check this */
+    if (buf[0] & 0x01)
+        s->rsr |= ENRSR_PHY;
+    p[0] = s->rsr;
     p[1] = next >> 8;
     p[2] = total_len;
     p[3] = total_len >> 8;
@@ -226,7 +274,10 @@ void ne2000_receive(uint8_t *buf, int size)
 
     /* write packet data */
     while (size > 0) {
-        avail = s->stop - index;
+        if (index <= s->stop)
+            avail = s->stop - index;
+        else
+            avail = 0;
         len = size;
         if (len > avail)
             len = avail;
@@ -238,16 +289,18 @@ void ne2000_receive(uint8_t *buf, int size)
         size -= len;
     }
     s->curpag = next >> 8;
-    
-    /* now we can signal we have receive something */
+
+    /* now we can signal we have received something */
     s->isr |= ENISR_RX;
     ne2000_update_irq(s);
+
+    return size_;
 }
 
-static void ne2000_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
+void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
-    NE2000State *s = &ne2000_state;
-    int offset, page;
+    NE2000State *s = opaque;
+    int offset, page, index;
 
     addr &= 0xf;
 #ifdef DEBUG_NE2000
@@ -256,18 +309,27 @@ static void ne2000_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
     if (addr == E8390_CMD) {
         /* control register */
         s->cmd = val;
-        if (val & E8390_START) {
-            /* test specific case: zero length transfert */
+        if (!(val & E8390_STOP)) { /* START bit makes no sense on RTL8029... */
+            s->isr &= ~ENISR_RESET;
+            /* test specific case: zero length transfer */
             if ((val & (E8390_RREAD | E8390_RWRITE)) &&
                 s->rcnt == 0) {
                 s->isr |= ENISR_RDC;
                 ne2000_update_irq(s);
             }
             if (val & E8390_TRANS) {
-                net_send_packet(net_fd, s->mem + (s->tpsr << 8), s->tcnt);
-                /* signal end of transfert */
+                index = (s->tpsr << 8);
+                /* XXX: next 2 lines are a hack to make netware 3.11 work */
+                if (index >= NE2000_PMEM_END)
+                    index -= NE2000_PMEM_SIZE;
+                /* fail safe: check range on the transmitted length  */
+                if (index + s->tcnt <= NE2000_PMEM_END) {
+                    qemu_send_packet(&s->nic->nc, s->mem + index, s->tcnt);
+                }
+                /* signal end of transfer */
                 s->tsr = ENTSR_PTX;
                 s->isr |= ENISR_TX;
+                s->cmd &= ~E8390_TRANS;
                 ne2000_update_irq(s);
             }
         }
@@ -309,11 +371,14 @@ static void ne2000_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
         case EN0_RCNTHI:
             s->rcnt = (s->rcnt & 0x00ff) | (val << 8);
             break;
+        case EN0_RXCR:
+            s->rxcr = val;
+            break;
         case EN0_DCFG:
             s->dcfg = val;
             break;
         case EN0_ISR:
-            s->isr &= ~val;
+            s->isr &= ~(val & 0x7f);
             ne2000_update_irq(s);
             break;
         case EN1_PHYS ... EN1_PHYS + 5:
@@ -329,9 +394,9 @@ static void ne2000_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
     }
 }
 
-static uint32_t ne2000_ioport_read(CPUState *env, uint32_t addr)
+uint32_t ne2000_ioport_read(void *opaque, uint32_t addr)
 {
-    NE2000State *s = &ne2000_state;
+    NE2000State *s = opaque;
     int offset, page, ret;
 
     addr &= 0xf;
@@ -350,6 +415,12 @@ static uint32_t ne2000_ioport_read(CPUState *env, uint32_t addr)
         case EN0_ISR:
             ret = s->isr;
             break;
+       case EN0_RSARLO:
+           ret = s->rsar & 0x00ff;
+           break;
+       case EN0_RSARHI:
+           ret = s->rsar >> 8;
+           break;
         case EN1_PHYS ... EN1_PHYS + 5:
             ret = s->phys[offset - EN1_PHYS];
             break;
@@ -359,6 +430,30 @@ static uint32_t ne2000_ioport_read(CPUState *env, uint32_t addr)
         case EN1_MULT ... EN1_MULT + 7:
             ret = s->mult[offset - EN1_MULT];
             break;
+        case EN0_RSR:
+            ret = s->rsr;
+            break;
+        case EN2_STARTPG:
+            ret = s->start >> 8;
+            break;
+        case EN2_STOPPG:
+            ret = s->stop >> 8;
+            break;
+       case EN0_RTL8029ID0:
+           ret = 0x50;
+           break;
+       case EN0_RTL8029ID1:
+           ret = 0x43;
+           break;
+       case EN3_CONFIG0:
+           ret = 0;            /* 10baseT media */
+           break;
+       case EN3_CONFIG2:
+           ret = 0x40;         /* 10baseT active */
+           break;
+       case EN3_CONFIG3:
+           ret = 0x40;         /* Full duplex */
+           break;
         default:
             ret = 0x00;
             break;
@@ -370,62 +465,118 @@ static uint32_t ne2000_ioport_read(CPUState *env, uint32_t addr)
     return ret;
 }
 
-static void ne2000_asic_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
+static inline void ne2000_mem_writeb(NE2000State *s, uint32_t addr,
+                                     uint32_t val)
 {
-    NE2000State *s = &ne2000_state;
-    uint8_t *p;
+    if (addr < 32 ||
+        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
+        s->mem[addr] = val;
+    }
+}
 
-#ifdef DEBUG_NE2000
-    printf("NE2000: asic write val=0x%04x\n", val);
-#endif
-    p = s->mem + s->rsar;
-    if (s->dcfg & 0x01) {
-        /* 16 bit access */
-        p[0] = val;
-        p[1] = val >> 8;
-        s->rsar += 2;
-        s->rcnt -= 2;
+static inline void ne2000_mem_writew(NE2000State *s, uint32_t addr,
+                                     uint32_t val)
+{
+    addr &= ~1; /* XXX: check exact behaviour if not even */
+    if (addr < 32 ||
+        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
+        *(uint16_t *)(s->mem + addr) = cpu_to_le16(val);
+    }
+}
+
+static inline void ne2000_mem_writel(NE2000State *s, uint32_t addr,
+                                     uint32_t val)
+{
+    addr &= ~1; /* XXX: check exact behaviour if not even */
+    if (addr < 32 ||
+        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
+        cpu_to_le32wu((uint32_t *)(s->mem + addr), val);
+    }
+}
+
+static inline uint32_t ne2000_mem_readb(NE2000State *s, uint32_t addr)
+{
+    if (addr < 32 ||
+        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
+        return s->mem[addr];
     } else {
-        /* 8 bit access */
-        p[0] = val;
-        s->rsar++;
-        s->rcnt--;
+        return 0xff;
+    }
+}
+
+static inline uint32_t ne2000_mem_readw(NE2000State *s, uint32_t addr)
+{
+    addr &= ~1; /* XXX: check exact behaviour if not even */
+    if (addr < 32 ||
+        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
+        return le16_to_cpu(*(uint16_t *)(s->mem + addr));
+    } else {
+        return 0xffff;
+    }
+}
+
+static inline uint32_t ne2000_mem_readl(NE2000State *s, uint32_t addr)
+{
+    addr &= ~1; /* XXX: check exact behaviour if not even */
+    if (addr < 32 ||
+        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
+        return le32_to_cpupu((uint32_t *)(s->mem + addr));
+    } else {
+        return 0xffffffff;
     }
+}
+
+static inline void ne2000_dma_update(NE2000State *s, int len)
+{
+    s->rsar += len;
     /* wrap */
+    /* XXX: check what to do if rsar > stop */
     if (s->rsar == s->stop)
         s->rsar = s->start;
-    if (s->rcnt == 0) {
-        /* signal end of transfert */
+
+    if (s->rcnt <= len) {
+        s->rcnt = 0;
+        /* signal end of transfer */
         s->isr |= ENISR_RDC;
         ne2000_update_irq(s);
+    } else {
+        s->rcnt -= len;
     }
 }
 
-static uint32_t ne2000_asic_ioport_read(CPUState *env, uint32_t addr)
+void ne2000_asic_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
-    NE2000State *s = &ne2000_state;
-    uint8_t *p;
-    int ret;
+    NE2000State *s = opaque;
 
-    p = s->mem + s->rsar;
+#ifdef DEBUG_NE2000
+    printf("NE2000: asic write val=0x%04x\n", val);
+#endif
+    if (s->rcnt == 0)
+        return;
     if (s->dcfg & 0x01) {
         /* 16 bit access */
-        ret = p[0] | (p[1] << 8);
-        s->rsar += 2;
-        s->rcnt -= 2;
+        ne2000_mem_writew(s, s->rsar, val);
+        ne2000_dma_update(s, 2);
     } else {
         /* 8 bit access */
-        ret = p[0];
-        s->rsar++;
-        s->rcnt--;
+        ne2000_mem_writeb(s, s->rsar, val);
+        ne2000_dma_update(s, 1);
     }
-    /* wrap */
-    if (s->rsar == s->stop)
-        s->rsar = s->start;
-    if (s->rcnt == 0) {
-        /* signal end of transfert */
-        s->isr |= ENISR_RDC;
-        ne2000_update_irq(s);
+}
+
+uint32_t ne2000_asic_ioport_read(void *opaque, uint32_t addr)
+{
+    NE2000State *s = opaque;
+    int ret;
+
+    if (s->dcfg & 0x01) {
+        /* 16 bit access */
+        ret = ne2000_mem_readw(s, s->rsar);
+        ne2000_dma_update(s, 2);
+    } else {
+        /* 8 bit access */
+        ret = ne2000_mem_readb(s, s->rsar);
+        ne2000_dma_update(s, 1);
     }
 #ifdef DEBUG_NE2000
     printf("NE2000: asic read val=0x%04x\n", ret);
@@ -433,33 +584,198 @@ static uint32_t ne2000_asic_ioport_read(CPUState *env, uint32_t addr)
     return ret;
 }
 
-static void ne2000_reset_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
+static void ne2000_asic_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
+{
+    NE2000State *s = opaque;
+
+#ifdef DEBUG_NE2000
+    printf("NE2000: asic writel val=0x%04x\n", val);
+#endif
+    if (s->rcnt == 0)
+        return;
+    /* 32 bit access */
+    ne2000_mem_writel(s, s->rsar, val);
+    ne2000_dma_update(s, 4);
+}
+
+static uint32_t ne2000_asic_ioport_readl(void *opaque, uint32_t addr)
+{
+    NE2000State *s = opaque;
+    int ret;
+
+    /* 32 bit access */
+    ret = ne2000_mem_readl(s, s->rsar);
+    ne2000_dma_update(s, 4);
+#ifdef DEBUG_NE2000
+    printf("NE2000: asic readl val=0x%04x\n", ret);
+#endif
+    return ret;
+}
+
+void ne2000_reset_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
     /* nothing to do (end of reset pulse) */
 }
 
-static uint32_t ne2000_reset_ioport_read(CPUState *env, uint32_t addr)
+uint32_t ne2000_reset_ioport_read(void *opaque, uint32_t addr)
 {
-    NE2000State *s = &ne2000_state;
+    NE2000State *s = opaque;
     ne2000_reset(s);
     return 0;
 }
 
-void ne2000_init(int base, int irq)
+static int ne2000_post_load(void* opaque, int version_id)
 {
-    NE2000State *s = &ne2000_state;
+    NE2000State* s = opaque;
 
-    register_ioport_write(base, 16, ne2000_ioport_write, 1);
-    register_ioport_read(base, 16, ne2000_ioport_read, 1);
+    if (version_id < 2) {
+        s->rxcr = 0x0c;
+    }
+    return 0;
+}
 
-    register_ioport_write(base + 0x10, 1, ne2000_asic_ioport_write, 1);
-    register_ioport_read(base + 0x10, 1, ne2000_asic_ioport_read, 1);
-    register_ioport_write(base + 0x10, 2, ne2000_asic_ioport_write, 2);
-    register_ioport_read(base + 0x10, 2, ne2000_asic_ioport_read, 2);
+const VMStateDescription vmstate_ne2000 = {
+    .name = "ne2000",
+    .version_id = 2,
+    .minimum_version_id = 0,
+    .minimum_version_id_old = 0,
+    .post_load = ne2000_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT8_V(rxcr, NE2000State, 2),
+        VMSTATE_UINT8(cmd, NE2000State),
+        VMSTATE_UINT32(start, NE2000State),
+        VMSTATE_UINT32(stop, NE2000State),
+        VMSTATE_UINT8(boundary, NE2000State),
+        VMSTATE_UINT8(tsr, NE2000State),
+        VMSTATE_UINT8(tpsr, NE2000State),
+        VMSTATE_UINT16(tcnt, NE2000State),
+        VMSTATE_UINT16(rcnt, NE2000State),
+        VMSTATE_UINT32(rsar, NE2000State),
+        VMSTATE_UINT8(rsr, NE2000State),
+        VMSTATE_UINT8(isr, NE2000State),
+        VMSTATE_UINT8(dcfg, NE2000State),
+        VMSTATE_UINT8(imr, NE2000State),
+        VMSTATE_BUFFER(phys, NE2000State),
+        VMSTATE_UINT8(curpag, NE2000State),
+        VMSTATE_BUFFER(mult, NE2000State),
+        VMSTATE_UNUSED(4), /* was irq */
+        VMSTATE_BUFFER(mem, NE2000State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_pci_ne2000 = {
+    .name = "ne2000",
+    .version_id = 3,
+    .minimum_version_id = 3,
+    .minimum_version_id_old = 3,
+    .fields      = (VMStateField []) {
+        VMSTATE_PCI_DEVICE(dev, PCINE2000State),
+        VMSTATE_STRUCT(ne2000, PCINE2000State, 0, vmstate_ne2000, NE2000State),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+/***********************************************************/
+/* PCI NE2000 definitions */
+
+static void ne2000_map(PCIDevice *pci_dev, int region_num,
+                       pcibus_t addr, pcibus_t size, int type)
+{
+    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
+    NE2000State *s = &d->ne2000;
+
+    register_ioport_write(addr, 16, 1, ne2000_ioport_write, s);
+    register_ioport_read(addr, 16, 1, ne2000_ioport_read, s);
+
+    register_ioport_write(addr + 0x10, 1, 1, ne2000_asic_ioport_write, s);
+    register_ioport_read(addr + 0x10, 1, 1, ne2000_asic_ioport_read, s);
+    register_ioport_write(addr + 0x10, 2, 2, ne2000_asic_ioport_write, s);
+    register_ioport_read(addr + 0x10, 2, 2, ne2000_asic_ioport_read, s);
+    register_ioport_write(addr + 0x10, 4, 4, ne2000_asic_ioport_writel, s);
+    register_ioport_read(addr + 0x10, 4, 4, ne2000_asic_ioport_readl, s);
+
+    register_ioport_write(addr + 0x1f, 1, 1, ne2000_reset_ioport_write, s);
+    register_ioport_read(addr + 0x1f, 1, 1, ne2000_reset_ioport_read, s);
+}
 
-    register_ioport_write(base + 0x1f, 1, ne2000_reset_ioport_write, 1);
-    register_ioport_read(base + 0x1f, 1, ne2000_reset_ioport_read, 1);
-    s->irq = irq;
+static void ne2000_cleanup(VLANClientState *nc)
+{
+    NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
+
+    s->nic = NULL;
+}
 
+static NetClientInfo net_ne2000_info = {
+    .type = NET_CLIENT_TYPE_NIC,
+    .size = sizeof(NICState),
+    .can_receive = ne2000_can_receive,
+    .receive = ne2000_receive,
+    .cleanup = ne2000_cleanup,
+};
+
+static int pci_ne2000_init(PCIDevice *pci_dev)
+{
+    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
+    NE2000State *s;
+    uint8_t *pci_conf;
+
+    pci_conf = d->dev.config;
+    /* TODO: RST# value should be 0. PCI spec 6.2.4 */
+    pci_conf[PCI_INTERRUPT_PIN] = 1; // interrupt pin 0
+
+    pci_register_bar(&d->dev, 0, 0x100,
+                           PCI_BASE_ADDRESS_SPACE_IO, ne2000_map);
+    s = &d->ne2000;
+    s->irq = d->dev.irq[0];
+
+    qemu_macaddr_default_if_unset(&s->c.macaddr);
     ne2000_reset(s);
+
+    s->nic = qemu_new_nic(&net_ne2000_info, &s->c,
+                          pci_dev->qdev.info->name, pci_dev->qdev.id, s);
+    qemu_format_nic_info_str(&s->nic->nc, s->c.macaddr.a);
+
+    if (!pci_dev->qdev.hotplugged) {
+        static int loaded = 0;
+        if (!loaded) {
+            rom_add_option("pxe-ne2k_pci.rom", -1);
+            loaded = 1;
+        }
+    }
+
+    add_boot_device_path(s->c.bootindex, &pci_dev->qdev, "/ethernet-phy@0");
+
+    return 0;
 }
+
+static int pci_ne2000_exit(PCIDevice *pci_dev)
+{
+    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
+    NE2000State *s = &d->ne2000;
+
+    qemu_del_vlan_client(&s->nic->nc);
+    return 0;
+}
+
+static PCIDeviceInfo ne2000_info = {
+    .qdev.name  = "ne2k_pci",
+    .qdev.size  = sizeof(PCINE2000State),
+    .qdev.vmsd  = &vmstate_pci_ne2000,
+    .init       = pci_ne2000_init,
+    .exit       = pci_ne2000_exit,
+    .vendor_id  = PCI_VENDOR_ID_REALTEK,
+    .device_id  = PCI_DEVICE_ID_REALTEK_8029,
+    .class_id   = PCI_CLASS_NETWORK_ETHERNET,
+    .qdev.props = (Property[]) {
+        DEFINE_NIC_PROPERTIES(PCINE2000State, ne2000.c),
+        DEFINE_PROP_END_OF_LIST(),
+    }
+};
+
+static void ne2000_register_devices(void)
+{
+    pci_qdev_register(&ne2000_info);
+}
+
+device_init(ne2000_register_devices)